2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// 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/.
|
2014-08-19 10:43:50 +00:00
|
|
|
|
|
|
|
package osutil_test
|
|
|
|
|
2015-03-07 13:35:29 +00:00
|
|
|
import (
|
2021-11-22 07:59:47 +00:00
|
|
|
"io"
|
2018-05-10 19:39:33 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2015-03-07 13:35:29 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2023-05-03 08:25:36 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2015-03-07 13:35:29 +00:00
|
|
|
)
|
|
|
|
|
2018-05-10 19:39:33 +00:00
|
|
|
func TestIsDeleted(t *testing.T) {
|
|
|
|
type tc struct {
|
|
|
|
path string
|
|
|
|
isDel bool
|
|
|
|
}
|
|
|
|
cases := []tc{
|
|
|
|
{"del", true},
|
|
|
|
{"del.file", false},
|
2020-06-14 18:09:37 +00:00
|
|
|
{filepath.Join("del", "del"), true},
|
2018-05-10 19:39:33 +00:00
|
|
|
{"file", false},
|
|
|
|
{"linkToFile", false},
|
|
|
|
{"linkToDel", false},
|
|
|
|
{"linkToDir", false},
|
2020-06-14 18:09:37 +00:00
|
|
|
{filepath.Join("linkToDir", "file"), true},
|
|
|
|
{filepath.Join("file", "behindFile"), true},
|
2018-05-10 19:39:33 +00:00
|
|
|
{"dir", false},
|
|
|
|
{"dir.file", false},
|
2020-06-14 18:09:37 +00:00
|
|
|
{filepath.Join("dir", "file"), false},
|
|
|
|
{filepath.Join("dir", "del"), true},
|
|
|
|
{filepath.Join("dir", "del", "del"), true},
|
|
|
|
{filepath.Join("del", "del", "del"), true},
|
2018-05-10 19:39:33 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := fs.NewFilesystem(fs.FilesystemTypeFake, "testdata")
|
2018-05-10 19:39:33 +00:00
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs.MkdirAll("dir", 0o777)
|
2020-06-14 18:09:37 +00:00
|
|
|
for _, f := range []string{"file", "del.file", "dir.file", filepath.Join("dir", "file")} {
|
2018-05-10 19:39:33 +00:00
|
|
|
fd, err := testFs.Create(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
}
|
2023-05-03 08:25:36 +00:00
|
|
|
|
2018-05-10 19:39:33 +00:00
|
|
|
for _, n := range []string{"Dir", "File", "Del"} {
|
2023-05-03 08:25:36 +00:00
|
|
|
if err := testFs.CreateSymlink(strings.ToLower(n), "linkTo"+n); err != nil {
|
2018-05-10 19:39:33 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
if osutil.IsDeleted(testFs, c.path) != c.isDel {
|
|
|
|
t.Errorf("IsDeleted(%v) != %v", c.path, c.isDel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-28 22:30:16 +00:00
|
|
|
|
|
|
|
func TestRenameOrCopy(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
sameFs := fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32)+"?content=true")
|
2019-04-28 22:30:16 +00:00
|
|
|
tests := []struct {
|
|
|
|
src fs.Filesystem
|
|
|
|
dst fs.Filesystem
|
|
|
|
file string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
src: sameFs,
|
|
|
|
dst: sameFs,
|
|
|
|
file: "file",
|
|
|
|
},
|
|
|
|
{
|
2023-05-03 08:25:36 +00:00
|
|
|
src: fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32)+"?content=true"),
|
|
|
|
dst: fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32)+"?content=true"),
|
2019-04-28 22:30:16 +00:00
|
|
|
file: "file",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: fs.NewFilesystem(fs.FilesystemTypeFake, `fake://fake/?files=1&seed=42`),
|
2023-05-03 08:25:36 +00:00
|
|
|
dst: fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32)+"?content=true"),
|
2019-04-28 22:30:16 +00:00
|
|
|
file: osutil.NativeFilename(`05/7a/4d52f284145b9fe8`),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
content := test.src.URI()
|
|
|
|
if _, err := test.src.Lstat(test.file); err != nil {
|
|
|
|
if !fs.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if fd, err := test.src.Create(test.file); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
if _, err := fd.Write([]byte(test.src.URI())); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_ = fd.Close()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fd, err := test.src.Open(test.file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
_ = fd.Close()
|
|
|
|
content = string(buf)
|
|
|
|
}
|
|
|
|
|
2020-06-18 06:15:47 +00:00
|
|
|
err := osutil.RenameOrCopy(fs.CopyRangeMethodStandard, test.src, test.dst, test.file, "new")
|
2019-04-28 22:30:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fd, err := test.dst.Open("new"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
2022-04-15 03:44:06 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
_ = fd.Close()
|
|
|
|
})
|
|
|
|
|
2021-11-22 07:59:47 +00:00
|
|
|
if buf, err := io.ReadAll(fd); err != nil {
|
2019-04-28 22:30:16 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
} else if string(buf) != content {
|
|
|
|
t.Fatalf("expected %s got %s", content, string(buf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|