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 (
|
2022-09-13 17:20:04 +00:00
|
|
|
"context"
|
2021-11-22 07:59:47 +00:00
|
|
|
"io"
|
2022-09-13 17:20:04 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-06-12 11:04:00 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2017-08-19 14:36:56 +00:00
|
|
|
|
2020-07-14 08:48:50 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2017-08-19 14:36:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2015-06-12 11:04:00 +00:00
|
|
|
)
|
|
|
|
|
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.
|
2022-04-15 03:44:06 +00:00
|
|
|
tmpDir1 := t.TempDir()
|
2019-04-28 22:30:16 +00:00
|
|
|
|
2022-04-15 03:44:06 +00:00
|
|
|
tmpDir2 := t.TempDir()
|
2019-04-28 22:30:16 +00:00
|
|
|
|
2020-06-18 06:15:47 +00:00
|
|
|
cfg := config.FolderConfiguration{
|
|
|
|
FilesystemType: fs.FilesystemTypeBasic,
|
|
|
|
Path: tmpDir1,
|
|
|
|
Versioning: config.VersioningConfiguration{
|
2021-02-26 11:04:05 +00:00
|
|
|
FSType: fs.FilesystemTypeBasic,
|
|
|
|
FSPath: tmpDir2,
|
2020-06-18 06:15:47 +00:00
|
|
|
},
|
|
|
|
}
|
2022-04-10 18:55:05 +00:00
|
|
|
folderFs := cfg.Filesystem(nil)
|
2020-06-18 06:15:47 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 09:34:15 +00:00
|
|
|
func TestTrashcanRestoreDeletedFile(t *testing.T) {
|
|
|
|
// This tests that the Trash Can restore function works correctly when the file
|
|
|
|
// to be restored was deleted/nonexistent in the folder where the file/folder is
|
|
|
|
// going to be restored in. (Issue: #7965)
|
|
|
|
|
|
|
|
tmpDir1 := t.TempDir()
|
|
|
|
|
|
|
|
tmpDir2 := t.TempDir()
|
|
|
|
|
|
|
|
cfg := config.FolderConfiguration{
|
|
|
|
FilesystemType: fs.FilesystemTypeBasic,
|
|
|
|
Path: tmpDir1,
|
|
|
|
Versioning: config.VersioningConfiguration{
|
|
|
|
FSType: fs.FilesystemTypeBasic,
|
|
|
|
FSPath: tmpDir2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
folderFs := cfg.Filesystem(nil)
|
|
|
|
|
|
|
|
versionsFs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir2)
|
|
|
|
|
|
|
|
versioner := newTrashcan(cfg)
|
|
|
|
|
|
|
|
writeFile(t, folderFs, "file", "Some content")
|
|
|
|
|
|
|
|
if err := versioner.Archive("file"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shouldn't be in the default folder anymore, thus "deleted"
|
|
|
|
if _, err := folderFs.Stat("file"); !fs.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It should, however, be in the archive
|
|
|
|
if _, err := versionsFs.Lstat("file"); fs.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
versions, err := versioner.GetVersions()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the file from the archive.
|
|
|
|
if err := versioner.Restore("file", fileVersion.VersionTime); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The file should be correctly restored
|
|
|
|
if content := readFile(t, folderFs, "file"); content != "Some content" {
|
|
|
|
t.Errorf("expected A got %s", content)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It should no longer be in the archive
|
|
|
|
if _, err := versionsFs.Lstat("file"); !fs.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-28 22:30:16 +00:00
|
|
|
func readFile(t *testing.T, filesystem fs.Filesystem, name string) string {
|
2021-02-26 11:04:05 +00:00
|
|
|
t.Helper()
|
2019-04-28 22:30:16 +00:00
|
|
|
fd, err := filesystem.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
2021-11-22 07:59:47 +00:00
|
|
|
buf, err := io.ReadAll(fd)
|
2019-04-28 22:30:16 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2022-09-13 17:20:04 +00:00
|
|
|
|
|
|
|
func TestTrashcanCleanOut(t *testing.T) {
|
|
|
|
testDir := t.TempDir()
|
|
|
|
|
|
|
|
cfg := config.FolderConfiguration{
|
|
|
|
FilesystemType: fs.FilesystemTypeBasic,
|
|
|
|
Path: testDir,
|
|
|
|
Versioning: config.VersioningConfiguration{
|
|
|
|
Params: map[string]string{
|
|
|
|
"cleanoutDays": "7",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fs := cfg.Filesystem(nil)
|
|
|
|
|
|
|
|
v := newTrashcan(cfg)
|
|
|
|
|
|
|
|
var testcases = map[string]bool{
|
|
|
|
".stversions/file1": false,
|
|
|
|
".stversions/file2": true,
|
|
|
|
".stversions/keep1/file1": false,
|
|
|
|
".stversions/keep1/file2": false,
|
|
|
|
".stversions/keep2/file1": false,
|
|
|
|
".stversions/keep2/file2": true,
|
|
|
|
".stversions/keep3/keepsubdir/file1": false,
|
|
|
|
".stversions/remove/file1": true,
|
|
|
|
".stversions/remove/file2": true,
|
|
|
|
".stversions/remove/removesubdir/file1": true,
|
|
|
|
}
|
|
|
|
|
2023-06-19 06:50:53 +00:00
|
|
|
t.Run("trashcan versioner trashcan clean up", func(t *testing.T) {
|
2022-09-13 17:20:04 +00:00
|
|
|
oldTime := time.Now().Add(-8 * 24 * time.Hour)
|
|
|
|
for file, shouldRemove := range testcases {
|
|
|
|
fs.MkdirAll(filepath.Dir(file), 0777)
|
|
|
|
|
|
|
|
writeFile(t, fs, file, "some content")
|
|
|
|
|
|
|
|
if shouldRemove {
|
|
|
|
if err := fs.Chtimes(file, oldTime, oldTime); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.Clean(context.Background()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for file, shouldRemove := range testcases {
|
|
|
|
_, err := fs.Lstat(file)
|
|
|
|
if shouldRemove && !os.IsNotExist(err) {
|
|
|
|
t.Error(file, "should have been removed")
|
|
|
|
} else if !shouldRemove && err != nil {
|
|
|
|
t.Error(file, "should not have been removed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := fs.Lstat(".stversions/keep3"); os.IsNotExist(err) {
|
|
|
|
t.Error("directory with non empty subdirs should not be removed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := fs.Lstat(".stversions/remove"); !os.IsNotExist(err) {
|
|
|
|
t.Error("empty directory should have been removed")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|