2015-06-12 11:04:00 +00:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-06-12 11:04:00 +00:00
|
|
|
|
|
|
|
package versioner
|
|
|
|
|
|
|
|
import (
|
2020-06-18 06:15:47 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2015-06-12 11:04:00 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2017-08-19 14:36:56 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2015-06-12 11:04:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTrashcanCleanout(t *testing.T) {
|
|
|
|
// Verify that files older than the cutoff are removed, that files newer
|
|
|
|
// than the cutoff are *not* removed, and that empty directories are
|
|
|
|
// removed (best effort).
|
|
|
|
|
|
|
|
var testcases = []struct {
|
|
|
|
file string
|
|
|
|
shouldRemove bool
|
|
|
|
}{
|
|
|
|
{"testdata/.stversions/file1", false},
|
|
|
|
{"testdata/.stversions/file2", true},
|
|
|
|
{"testdata/.stversions/keep1/file1", false},
|
|
|
|
{"testdata/.stversions/keep1/file2", false},
|
|
|
|
{"testdata/.stversions/keep2/file1", false},
|
|
|
|
{"testdata/.stversions/keep2/file2", true},
|
2017-11-18 15:56:53 +00:00
|
|
|
{"testdata/.stversions/keep3/keepsubdir/file1", false},
|
2015-06-12 11:04:00 +00:00
|
|
|
{"testdata/.stversions/remove/file1", true},
|
|
|
|
{"testdata/.stversions/remove/file2", true},
|
2017-11-18 15:56:53 +00:00
|
|
|
{"testdata/.stversions/remove/removesubdir/file1", true},
|
2015-06-12 11:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
os.RemoveAll("testdata")
|
|
|
|
defer os.RemoveAll("testdata")
|
|
|
|
|
|
|
|
oldTime := time.Now().Add(-8 * 24 * time.Hour)
|
|
|
|
for _, tc := range testcases {
|
2019-02-02 11:16:27 +00:00
|
|
|
os.MkdirAll(filepath.Dir(tc.file), 0777)
|
2015-06-12 11:04:00 +00:00
|
|
|
if err := ioutil.WriteFile(tc.file, []byte("data"), 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tc.shouldRemove {
|
|
|
|
if err := os.Chtimes(tc.file, oldTime, oldTime); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 06:15:47 +00:00
|
|
|
cfg := config.FolderConfiguration{
|
|
|
|
FilesystemType: fs.FilesystemTypeBasic,
|
|
|
|
Path: "testdata",
|
|
|
|
Versioning: config.VersioningConfiguration{
|
|
|
|
Params: map[string]string{
|
|
|
|
"cleanoutDays": "7",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
versioner := newTrashcan(cfg).(*trashcan)
|
2015-06-12 11:04:00 +00:00
|
|
|
if err := versioner.cleanoutArchive(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
|
|
|
_, err := os.Lstat(tc.file)
|
|
|
|
if tc.shouldRemove && !os.IsNotExist(err) {
|
|
|
|
t.Error(tc.file, "should have been removed")
|
|
|
|
} else if !tc.shouldRemove && err != nil {
|
|
|
|
t.Error(tc.file, "should not have been removed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-18 15:56:53 +00:00
|
|
|
if _, err := os.Lstat("testdata/.stversions/keep3"); os.IsNotExist(err) {
|
|
|
|
t.Error("directory with non empty subdirs should not be removed")
|
|
|
|
}
|
|
|
|
|
2015-06-12 11:04:00 +00:00
|
|
|
if _, err := os.Lstat("testdata/.stversions/remove"); !os.IsNotExist(err) {
|
|
|
|
t.Error("empty directory should have been removed")
|
|
|
|
}
|
|
|
|
}
|
2019-04-28 22:30:16 +00:00
|
|
|
|
|
|
|
func TestTrashcanArchiveRestoreSwitcharoo(t *testing.T) {
|
|
|
|
// This tests that trashcan versioner restoration correctly archives existing file, because trashcan versioner
|
|
|
|
// files are untagged, archiving existing file to replace with a restored version technically should collide in
|
|
|
|
// in names.
|
|
|
|
tmpDir1, err := ioutil.TempDir("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpDir2, err := ioutil.TempDir("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-06-18 06:15:47 +00:00
|
|
|
cfg := config.FolderConfiguration{
|
|
|
|
FilesystemType: fs.FilesystemTypeBasic,
|
|
|
|
Path: tmpDir1,
|
|
|
|
Versioning: config.VersioningConfiguration{
|
|
|
|
Params: map[string]string{
|
|
|
|
"fsType": "basic",
|
|
|
|
"fsPath": tmpDir2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
folderFs := cfg.Filesystem()
|
|
|
|
|
2019-04-28 22:30:16 +00:00
|
|
|
versionsFs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir2)
|
|
|
|
|
|
|
|
writeFile(t, folderFs, "file", "A")
|
|
|
|
|
2020-06-18 06:15:47 +00:00
|
|
|
versioner := newTrashcan(cfg)
|
2019-04-28 22:30:16 +00:00
|
|
|
|
|
|
|
if err := versioner.Archive("file"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := folderFs.Stat("file"); !fs.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-06-25 05:56:11 +00:00
|
|
|
// Check versions
|
|
|
|
versions, err := versioner.GetVersions()
|
2019-04-28 22:30:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-06-25 05:56:11 +00:00
|
|
|
fileVersions := versions["file"]
|
|
|
|
if len(fileVersions) != 1 {
|
|
|
|
t.Fatalf("unexpected number of versions: %d != 1", len(fileVersions))
|
|
|
|
}
|
|
|
|
|
|
|
|
fileVersion := fileVersions[0]
|
|
|
|
|
|
|
|
if !fileVersion.ModTime.Equal(fileVersion.VersionTime) {
|
|
|
|
t.Error("time mismatch")
|
|
|
|
}
|
|
|
|
|
2019-04-28 22:30:16 +00:00
|
|
|
if content := readFile(t, versionsFs, "file"); content != "A" {
|
|
|
|
t.Errorf("expected A got %s", content)
|
|
|
|
}
|
|
|
|
|
|
|
|
writeFile(t, folderFs, "file", "B")
|
|
|
|
|
2019-06-25 05:56:11 +00:00
|
|
|
versionInfo, err := versionsFs.Stat("file")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !versionInfo.ModTime().Truncate(time.Second).Equal(fileVersion.ModTime) {
|
|
|
|
t.Error("time mismatch")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := versioner.Restore("file", fileVersion.VersionTime); err != nil {
|
2019-04-28 22:30:16 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if content := readFile(t, folderFs, "file"); content != "A" {
|
|
|
|
t.Errorf("expected A got %s", content)
|
|
|
|
}
|
|
|
|
|
|
|
|
if content := readFile(t, versionsFs, "file"); content != "B" {
|
|
|
|
t.Errorf("expected B got %s", content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readFile(t *testing.T, filesystem fs.Filesystem, name string) string {
|
|
|
|
fd, err := filesystem.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
buf, err := ioutil.ReadAll(fd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return string(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeFile(t *testing.T, filesystem fs.Filesystem, name, content string) {
|
|
|
|
fd, err := filesystem.OpenFile(name, fs.OptReadWrite|fs.OptCreate, 0777)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
if err := fd.Truncate(int64(len(content))); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, err := fd.Write([]byte(content)); err != nil || n != len(content) {
|
|
|
|
t.Fatal(n, len(content), err)
|
|
|
|
}
|
|
|
|
}
|