2017-03-17 06:37:56 +08:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
2021-11-24 20:01:02 +08:00
|
|
|
"bytes"
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2022-05-25 01:06:16 +08:00
|
|
|
"fmt"
|
2022-08-20 22:38:02 +08:00
|
|
|
"io"
|
2019-01-28 03:16:26 +08:00
|
|
|
"os"
|
2018-02-01 04:18:31 +08:00
|
|
|
"path"
|
2019-01-28 03:16:26 +08:00
|
|
|
"path/filepath"
|
2022-05-25 01:06:16 +08:00
|
|
|
"runtime"
|
2022-08-11 19:20:34 +08:00
|
|
|
"sort"
|
2017-03-17 06:37:56 +08:00
|
|
|
"testing"
|
2018-02-01 04:18:31 +08:00
|
|
|
"time"
|
2017-03-17 06:37:56 +08:00
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2023-02-27 20:16:49 +08:00
|
|
|
"github.com/rclone/rclone/fs/accounting"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
2022-08-11 19:20:34 +08:00
|
|
|
"github.com/rclone/rclone/fs/filter"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/hash"
|
2021-11-24 20:01:02 +08:00
|
|
|
"github.com/rclone/rclone/fs/object"
|
2023-07-02 00:18:21 +08:00
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fstest"
|
|
|
|
"github.com/rclone/rclone/lib/file"
|
|
|
|
"github.com/rclone/rclone/lib/readers"
|
2017-03-17 06:37:56 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-02-01 04:18:31 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-03-17 06:37:56 +08:00
|
|
|
)
|
|
|
|
|
2018-02-01 04:18:31 +08:00
|
|
|
// TestMain drives the tests
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
fstest.TestMain(m)
|
|
|
|
}
|
|
|
|
|
2018-04-09 22:27:58 +08:00
|
|
|
// Test copy with source file that's updating
|
|
|
|
func TestUpdatingCheck(t *testing.T) {
|
2018-02-01 04:18:31 +08:00
|
|
|
r := fstest.NewRun(t)
|
|
|
|
filePath := "sub dir/local test"
|
|
|
|
r.WriteFile(filePath, "content", time.Now())
|
|
|
|
|
2019-01-10 03:27:15 +08:00
|
|
|
fd, err := file.Open(path.Join(r.LocalName, filePath))
|
2018-02-01 04:18:31 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed opening file %q: %v", filePath, err)
|
|
|
|
}
|
2019-01-29 23:23:42 +08:00
|
|
|
defer func() {
|
|
|
|
require.NoError(t, fd.Close())
|
|
|
|
}()
|
2018-02-01 04:18:31 +08:00
|
|
|
|
|
|
|
fi, err := fd.Stat()
|
2018-05-04 22:19:50 +08:00
|
|
|
require.NoError(t, err)
|
2018-05-15 01:06:57 +08:00
|
|
|
o := &Object{size: fi.Size(), modTime: fi.ModTime(), fs: &Fs{}}
|
2018-02-01 04:18:31 +08:00
|
|
|
wrappedFd := readers.NewLimitedReadCloser(fd, -1)
|
2019-09-23 21:32:36 +08:00
|
|
|
hash, err := hash.NewMultiHasherTypes(hash.Supported())
|
2018-05-04 22:19:50 +08:00
|
|
|
require.NoError(t, err)
|
2018-02-01 04:18:31 +08:00
|
|
|
in := localOpenFile{
|
|
|
|
o: o,
|
|
|
|
in: wrappedFd,
|
|
|
|
hash: hash,
|
|
|
|
fd: fd,
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 1)
|
|
|
|
_, err = in.Read(buf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
r.WriteFile(filePath, "content updated", time.Now())
|
|
|
|
_, err = in.Read(buf)
|
|
|
|
require.Errorf(t, err, "can't copy - source file is being updated")
|
2018-04-09 22:27:58 +08:00
|
|
|
|
|
|
|
// turn the checking off and try again
|
2018-05-15 01:06:57 +08:00
|
|
|
in.o.fs.opt.NoCheckUpdated = true
|
2018-04-09 22:27:58 +08:00
|
|
|
|
|
|
|
r.WriteFile(filePath, "content updated", time.Now())
|
|
|
|
_, err = in.Read(buf)
|
|
|
|
require.NoError(t, err)
|
2017-03-17 06:37:56 +08:00
|
|
|
}
|
2019-01-28 03:16:26 +08:00
|
|
|
|
2024-03-31 13:23:26 +08:00
|
|
|
// Test corrupted on transfer
|
|
|
|
// should error due to size/hash mismatch
|
|
|
|
func TestVerifyCopy(t *testing.T) {
|
2024-04-03 01:46:20 +08:00
|
|
|
t.Skip("FIXME this test is unreliable")
|
2024-03-31 13:23:26 +08:00
|
|
|
r := fstest.NewRun(t)
|
|
|
|
filePath := "sub dir/local test"
|
|
|
|
r.WriteFile(filePath, "some content", time.Now())
|
|
|
|
src, err := r.Flocal.NewObject(context.Background(), filePath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
src.(*Object).fs.opt.NoCheckUpdated = true
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
go r.WriteFile(src.Remote(), fmt.Sprintf("some new content %d", i), src.ModTime(context.Background()))
|
|
|
|
}
|
|
|
|
_, err = operations.Copy(context.Background(), r.Fremote, nil, filePath+"2", src)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2019-01-28 03:16:26 +08:00
|
|
|
func TestSymlink(t *testing.T) {
|
2019-06-17 16:34:30 +08:00
|
|
|
ctx := context.Background()
|
2019-01-28 03:16:26 +08:00
|
|
|
r := fstest.NewRun(t)
|
|
|
|
f := r.Flocal.(*Fs)
|
|
|
|
dir := f.root
|
|
|
|
|
|
|
|
// Write a file
|
|
|
|
modTime1 := fstest.Time("2001-02-03T04:05:10.123123123Z")
|
|
|
|
file1 := r.WriteFile("file.txt", "hello", modTime1)
|
|
|
|
|
|
|
|
// Write a symlink
|
|
|
|
modTime2 := fstest.Time("2002-02-03T04:05:10.123123123Z")
|
|
|
|
symlinkPath := filepath.Join(dir, "symlink.txt")
|
|
|
|
require.NoError(t, os.Symlink("file.txt", symlinkPath))
|
|
|
|
require.NoError(t, lChtimes(symlinkPath, modTime2, modTime2))
|
|
|
|
|
|
|
|
// Object viewed as symlink
|
|
|
|
file2 := fstest.NewItem("symlink.txt"+linkSuffix, "file.txt", modTime2)
|
|
|
|
|
|
|
|
// Object viewed as destination
|
|
|
|
file2d := fstest.NewItem("symlink.txt", "hello", modTime1)
|
|
|
|
|
|
|
|
// Check with no symlink flags
|
2021-11-09 19:43:36 +08:00
|
|
|
r.CheckLocalItems(t, file1)
|
|
|
|
r.CheckRemoteItems(t)
|
2019-01-28 03:16:26 +08:00
|
|
|
|
|
|
|
// Set fs into "-L" mode
|
|
|
|
f.opt.FollowSymlinks = true
|
|
|
|
f.opt.TranslateSymlinks = false
|
|
|
|
f.lstat = os.Stat
|
|
|
|
|
2021-11-09 19:43:36 +08:00
|
|
|
r.CheckLocalItems(t, file1, file2d)
|
|
|
|
r.CheckRemoteItems(t)
|
2019-01-28 03:16:26 +08:00
|
|
|
|
|
|
|
// Set fs into "-l" mode
|
|
|
|
f.opt.FollowSymlinks = false
|
|
|
|
f.opt.TranslateSymlinks = true
|
|
|
|
f.lstat = os.Lstat
|
|
|
|
|
|
|
|
fstest.CheckListingWithPrecision(t, r.Flocal, []fstest.Item{file1, file2}, nil, fs.ModTimeNotSupported)
|
|
|
|
if haveLChtimes {
|
2021-11-09 19:43:36 +08:00
|
|
|
r.CheckLocalItems(t, file1, file2)
|
2019-01-28 03:16:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a symlink
|
|
|
|
modTime3 := fstest.Time("2002-03-03T04:05:10.123123123Z")
|
2019-06-17 16:34:30 +08:00
|
|
|
file3 := r.WriteObjectTo(ctx, r.Flocal, "symlink2.txt"+linkSuffix, "file.txt", modTime3, false)
|
2019-01-28 03:16:26 +08:00
|
|
|
fstest.CheckListingWithPrecision(t, r.Flocal, []fstest.Item{file1, file2, file3}, nil, fs.ModTimeNotSupported)
|
|
|
|
if haveLChtimes {
|
2021-11-09 19:43:36 +08:00
|
|
|
r.CheckLocalItems(t, file1, file2, file3)
|
2019-01-28 03:16:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check it got the correct contents
|
|
|
|
symlinkPath = filepath.Join(dir, "symlink2.txt")
|
|
|
|
fi, err := os.Lstat(symlinkPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, fi.Mode().IsRegular())
|
|
|
|
linkText, err := os.Readlink(symlinkPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "file.txt", linkText)
|
|
|
|
|
|
|
|
// Check that NewObject gets the correct object
|
2019-06-17 16:34:30 +08:00
|
|
|
o, err := r.Flocal.NewObject(ctx, "symlink2.txt"+linkSuffix)
|
2019-01-28 03:16:26 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "symlink2.txt"+linkSuffix, o.Remote())
|
2020-09-26 01:32:31 +08:00
|
|
|
assert.Equal(t, int64(8), o.Size())
|
2019-01-28 03:16:26 +08:00
|
|
|
|
|
|
|
// Check that NewObject doesn't see the non suffixed version
|
2019-06-17 16:34:30 +08:00
|
|
|
_, err = r.Flocal.NewObject(ctx, "symlink2.txt")
|
2019-01-28 03:16:26 +08:00
|
|
|
require.Equal(t, fs.ErrorObjectNotFound, err)
|
|
|
|
|
2023-04-04 00:14:04 +08:00
|
|
|
// Check that NewFs works with the suffixed version and --links
|
|
|
|
f2, err := NewFs(ctx, "local", filepath.Join(dir, "symlink2.txt"+linkSuffix), configmap.Simple{
|
|
|
|
"links": "true",
|
|
|
|
})
|
|
|
|
require.Equal(t, fs.ErrorIsFile, err)
|
|
|
|
require.Equal(t, dir, f2.(*Fs).root)
|
|
|
|
|
|
|
|
// Check that NewFs doesn't see the non suffixed version with --links
|
|
|
|
f2, err = NewFs(ctx, "local", filepath.Join(dir, "symlink2.txt"), configmap.Simple{
|
|
|
|
"links": "true",
|
|
|
|
})
|
|
|
|
require.Equal(t, errLinksNeedsSuffix, err)
|
|
|
|
require.Nil(t, f2)
|
|
|
|
|
2019-01-28 03:16:26 +08:00
|
|
|
// Check reading the object
|
2019-06-17 16:34:30 +08:00
|
|
|
in, err := o.Open(ctx)
|
2019-01-28 03:16:26 +08:00
|
|
|
require.NoError(t, err)
|
2022-08-20 22:38:02 +08:00
|
|
|
contents, err := io.ReadAll(in)
|
2019-01-28 03:16:26 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "file.txt", string(contents))
|
|
|
|
require.NoError(t, in.Close())
|
|
|
|
|
|
|
|
// Check reading the object with range
|
2019-06-17 16:34:30 +08:00
|
|
|
in, err = o.Open(ctx, &fs.RangeOption{Start: 2, End: 5})
|
2019-01-28 03:16:26 +08:00
|
|
|
require.NoError(t, err)
|
2022-08-20 22:38:02 +08:00
|
|
|
contents, err = io.ReadAll(in)
|
2019-01-28 03:16:26 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "file.txt"[2:5+1], string(contents))
|
|
|
|
require.NoError(t, in.Close())
|
|
|
|
}
|
2019-01-28 03:28:57 +08:00
|
|
|
|
|
|
|
func TestSymlinkError(t *testing.T) {
|
|
|
|
m := configmap.Simple{
|
|
|
|
"links": "true",
|
|
|
|
"copy_links": "true",
|
|
|
|
}
|
2020-11-05 23:18:51 +08:00
|
|
|
_, err := NewFs(context.Background(), "local", "/", m)
|
2019-01-28 03:28:57 +08:00
|
|
|
assert.Equal(t, errLinksAndCopyLinks, err)
|
|
|
|
}
|
2021-11-24 20:01:02 +08:00
|
|
|
|
|
|
|
// Test hashes on updating an object
|
|
|
|
func TestHashOnUpdate(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
const filePath = "file.txt"
|
|
|
|
when := time.Now()
|
|
|
|
r.WriteFile(filePath, "content", when)
|
|
|
|
f := r.Flocal.(*Fs)
|
|
|
|
|
|
|
|
// Get the object
|
|
|
|
o, err := f.NewObject(ctx, filePath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Test the hash is as we expect
|
|
|
|
md5, err := o.Hash(ctx, hash.MD5)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "9a0364b9e99bb480dd25e1f0284c8555", md5)
|
|
|
|
|
2022-08-14 10:56:32 +08:00
|
|
|
// Reupload it with different contents but same size and timestamp
|
2024-09-17 04:24:19 +08:00
|
|
|
b := bytes.NewBufferString("CONTENT")
|
2021-11-24 20:01:02 +08:00
|
|
|
src := object.NewStaticObjectInfo(filePath, when, int64(b.Len()), true, nil, f)
|
|
|
|
err = o.Update(ctx, b, src)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Check the hash is as expected
|
|
|
|
md5, err = o.Hash(ctx, hash.MD5)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "45685e95985e20822fb2538a522a5ccf", md5)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test hashes on deleting an object
|
|
|
|
func TestHashOnDelete(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
const filePath = "file.txt"
|
|
|
|
when := time.Now()
|
|
|
|
r.WriteFile(filePath, "content", when)
|
|
|
|
f := r.Flocal.(*Fs)
|
|
|
|
|
|
|
|
// Get the object
|
|
|
|
o, err := f.NewObject(ctx, filePath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Test the hash is as we expect
|
|
|
|
md5, err := o.Hash(ctx, hash.MD5)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "9a0364b9e99bb480dd25e1f0284c8555", md5)
|
|
|
|
|
|
|
|
// Delete the object
|
|
|
|
require.NoError(t, o.Remove(ctx))
|
|
|
|
|
|
|
|
// Test the hash cache is empty
|
|
|
|
require.Nil(t, o.(*Object).hashes)
|
|
|
|
|
|
|
|
// Test the hash returns an error
|
|
|
|
_, err = o.Hash(ctx, hash.MD5)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
2022-05-25 01:06:16 +08:00
|
|
|
|
|
|
|
func TestMetadata(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
const filePath = "metafile.txt"
|
|
|
|
when := time.Now()
|
|
|
|
r.WriteFile(filePath, "metadata file contents", when)
|
|
|
|
f := r.Flocal.(*Fs)
|
|
|
|
|
2024-11-13 19:55:28 +08:00
|
|
|
// Set fs into "-l" / "--links" mode
|
|
|
|
f.opt.TranslateSymlinks = true
|
|
|
|
|
|
|
|
// Write a symlink to the file
|
|
|
|
symlinkPath := "metafile-link.txt"
|
|
|
|
osSymlinkPath := filepath.Join(f.root, symlinkPath)
|
|
|
|
symlinkPath += linkSuffix
|
|
|
|
require.NoError(t, os.Symlink(filePath, osSymlinkPath))
|
|
|
|
symlinkModTime := fstest.Time("2002-02-03T04:05:10.123123123Z")
|
|
|
|
require.NoError(t, lChtimes(osSymlinkPath, symlinkModTime, symlinkModTime))
|
|
|
|
|
2022-05-25 01:06:16 +08:00
|
|
|
// Get the object
|
|
|
|
obj, err := f.NewObject(ctx, filePath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
o := obj.(*Object)
|
|
|
|
|
2024-11-13 19:55:28 +08:00
|
|
|
// Get the symlink object
|
|
|
|
symlinkObj, err := f.NewObject(ctx, symlinkPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
symlinkO := symlinkObj.(*Object)
|
|
|
|
|
|
|
|
// Record metadata for o
|
|
|
|
oMeta, err := o.Metadata(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Test symlink first to check it doesn't mess up file
|
|
|
|
t.Run("Symlink", func(t *testing.T) {
|
|
|
|
testMetadata(t, r, symlinkO, symlinkModTime)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Read it again
|
|
|
|
oMetaNew, err := o.Metadata(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Check that operating on the symlink didn't change the file it was pointing to
|
|
|
|
// See: https://github.com/rclone/rclone/security/advisories/GHSA-hrxh-9w67-g4cv
|
|
|
|
assert.Equal(t, oMeta, oMetaNew, "metadata setting on symlink messed up file")
|
|
|
|
|
|
|
|
// Now run the same tests on the file
|
|
|
|
t.Run("File", func(t *testing.T) {
|
|
|
|
testMetadata(t, r, o, when)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMetadata(t *testing.T, r *fstest.Run, o *Object, when time.Time) {
|
|
|
|
ctx := context.Background()
|
|
|
|
whenRFC := when.Format(time.RFC3339Nano)
|
|
|
|
const dayLength = len("2001-01-01")
|
|
|
|
|
|
|
|
f := r.Flocal.(*Fs)
|
2022-05-25 01:06:16 +08:00
|
|
|
features := f.Features()
|
|
|
|
|
2024-11-13 19:55:28 +08:00
|
|
|
var hasXID, hasAtime, hasBtime, canSetXattrOnLinks bool
|
2022-05-25 01:06:16 +08:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin", "freebsd", "netbsd", "linux":
|
|
|
|
hasXID, hasAtime, hasBtime = true, true, true
|
2024-11-13 19:55:28 +08:00
|
|
|
canSetXattrOnLinks = runtime.GOOS != "linux"
|
2022-05-25 01:06:16 +08:00
|
|
|
case "openbsd", "solaris":
|
|
|
|
hasXID, hasAtime = true, true
|
|
|
|
case "windows":
|
|
|
|
hasAtime, hasBtime = true, true
|
|
|
|
case "plan9", "js":
|
|
|
|
// nada
|
|
|
|
default:
|
|
|
|
t.Errorf("No test cases for OS %q", runtime.GOOS)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.True(t, features.ReadMetadata)
|
|
|
|
assert.True(t, features.WriteMetadata)
|
|
|
|
assert.Equal(t, xattrSupported, features.UserMetadata)
|
|
|
|
|
|
|
|
t.Run("Xattr", func(t *testing.T) {
|
|
|
|
if !xattrSupported {
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
m, err := o.getXattr()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Nil(t, m)
|
|
|
|
|
2024-11-13 19:55:28 +08:00
|
|
|
if !canSetXattrOnLinks && o.translatedLink {
|
|
|
|
t.Skip("Skip remainder of test as can't set xattr on symlinks on this OS")
|
|
|
|
}
|
|
|
|
|
2022-05-25 01:06:16 +08:00
|
|
|
inM := fs.Metadata{
|
|
|
|
"potato": "chips",
|
|
|
|
"cabbage": "soup",
|
|
|
|
}
|
|
|
|
err = o.setXattr(inM)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err = o.getXattr()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, m)
|
|
|
|
assert.Equal(t, inM, m)
|
|
|
|
})
|
|
|
|
|
|
|
|
checkTime := func(m fs.Metadata, key string, when time.Time) {
|
2024-11-13 19:55:28 +08:00
|
|
|
t.Helper()
|
2022-05-25 01:06:16 +08:00
|
|
|
mt, ok := o.parseMetadataTime(m, key)
|
|
|
|
assert.True(t, ok)
|
|
|
|
dt := mt.Sub(when)
|
|
|
|
precision := time.Second
|
2024-11-13 19:55:28 +08:00
|
|
|
assert.True(t, dt >= -precision && dt <= precision, fmt.Sprintf("%s: dt %v outside +/- precision %v want %v got %v", key, dt, precision, mt, when))
|
2022-05-25 01:06:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
checkInt := func(m fs.Metadata, key string, base int) int {
|
2024-11-13 19:55:28 +08:00
|
|
|
t.Helper()
|
2022-05-25 01:06:16 +08:00
|
|
|
value, ok := o.parseMetadataInt(m, key, base)
|
|
|
|
assert.True(t, ok)
|
|
|
|
return value
|
|
|
|
}
|
2024-11-13 19:55:28 +08:00
|
|
|
|
2022-05-25 01:06:16 +08:00
|
|
|
t.Run("Read", func(t *testing.T) {
|
|
|
|
m, err := o.Metadata(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, m)
|
|
|
|
|
|
|
|
// All OSes have these
|
|
|
|
checkInt(m, "mode", 8)
|
|
|
|
checkTime(m, "mtime", when)
|
|
|
|
|
|
|
|
assert.Equal(t, whenRFC[:dayLength], m["mtime"][:dayLength])
|
|
|
|
|
2024-11-13 19:55:28 +08:00
|
|
|
if hasAtime && !o.translatedLink { // symlinks generally don't record atime
|
2022-05-25 01:06:16 +08:00
|
|
|
checkTime(m, "atime", when)
|
|
|
|
}
|
2024-11-13 19:55:28 +08:00
|
|
|
if hasBtime && !o.translatedLink { // symlinks generally don't record btime
|
2022-05-25 01:06:16 +08:00
|
|
|
checkTime(m, "btime", when)
|
|
|
|
}
|
|
|
|
if hasXID {
|
|
|
|
checkInt(m, "uid", 10)
|
|
|
|
checkInt(m, "gid", 10)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Write", func(t *testing.T) {
|
|
|
|
newAtimeString := "2011-12-13T14:15:16.999999999Z"
|
|
|
|
newAtime := fstest.Time(newAtimeString)
|
|
|
|
newMtimeString := "2011-12-12T14:15:16.999999999Z"
|
|
|
|
newMtime := fstest.Time(newMtimeString)
|
|
|
|
newBtimeString := "2011-12-11T14:15:16.999999999Z"
|
|
|
|
newBtime := fstest.Time(newBtimeString)
|
|
|
|
newM := fs.Metadata{
|
|
|
|
"mtime": newMtimeString,
|
|
|
|
"atime": newAtimeString,
|
|
|
|
"btime": newBtimeString,
|
|
|
|
// Can't test uid, gid without being root
|
|
|
|
"mode": "0767",
|
|
|
|
"potato": "wedges",
|
|
|
|
}
|
2024-11-13 19:55:28 +08:00
|
|
|
if !canSetXattrOnLinks && o.translatedLink {
|
|
|
|
// Don't change xattr if not supported on symlinks
|
|
|
|
delete(newM, "potato")
|
|
|
|
}
|
2022-05-25 01:06:16 +08:00
|
|
|
err := o.writeMetadata(newM)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err := o.Metadata(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, m)
|
|
|
|
|
|
|
|
mode := checkInt(m, "mode", 8)
|
|
|
|
if runtime.GOOS != "windows" {
|
2024-11-13 19:55:28 +08:00
|
|
|
expectedMode := 0767
|
|
|
|
if o.translatedLink && runtime.GOOS == "linux" {
|
|
|
|
expectedMode = 0777 // perms of symlinks always read as 0777 on linux
|
|
|
|
}
|
|
|
|
assert.Equal(t, expectedMode, mode&0777, fmt.Sprintf("mode wrong - expecting 0%o got 0%o", expectedMode, mode&0777))
|
2022-05-25 01:06:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
checkTime(m, "mtime", newMtime)
|
|
|
|
if hasAtime {
|
|
|
|
checkTime(m, "atime", newAtime)
|
|
|
|
}
|
|
|
|
if haveSetBTime {
|
|
|
|
checkTime(m, "btime", newBtime)
|
|
|
|
}
|
2024-11-13 19:55:28 +08:00
|
|
|
if xattrSupported && (canSetXattrOnLinks || !o.translatedLink) {
|
2022-05-25 01:06:16 +08:00
|
|
|
assert.Equal(t, "wedges", m["potato"])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-08-11 19:20:34 +08:00
|
|
|
|
|
|
|
func TestFilter(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
when := time.Now()
|
|
|
|
r.WriteFile("included", "included file", when)
|
|
|
|
r.WriteFile("excluded", "excluded file", when)
|
|
|
|
f := r.Flocal.(*Fs)
|
|
|
|
|
2022-09-05 23:19:50 +08:00
|
|
|
// Check set up for filtering
|
|
|
|
assert.True(t, f.Features().FilterAware)
|
|
|
|
|
2022-08-11 19:20:34 +08:00
|
|
|
// Add a filter
|
|
|
|
ctx, fi := filter.AddConfig(ctx)
|
|
|
|
require.NoError(t, fi.AddRule("+ included"))
|
|
|
|
require.NoError(t, fi.AddRule("- *"))
|
|
|
|
|
|
|
|
// Check listing without use filter flag
|
|
|
|
entries, err := f.List(ctx, "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
sort.Sort(entries)
|
|
|
|
require.Equal(t, "[excluded included]", fmt.Sprint(entries))
|
|
|
|
|
|
|
|
// Add user filter flag
|
|
|
|
ctx = filter.SetUseFilter(ctx, true)
|
|
|
|
|
|
|
|
// Check listing with use filter flag
|
|
|
|
entries, err = f.List(ctx, "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
sort.Sort(entries)
|
|
|
|
require.Equal(t, "[included]", fmt.Sprint(entries))
|
|
|
|
}
|
2023-02-27 20:16:49 +08:00
|
|
|
|
2023-04-03 23:28:12 +08:00
|
|
|
func testFilterSymlink(t *testing.T, copyLinks bool) {
|
2023-02-27 20:16:49 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
when := time.Now()
|
|
|
|
f := r.Flocal.(*Fs)
|
|
|
|
|
|
|
|
// Create a file, a directory, a symlink to a file, a symlink to a directory and a dangling symlink
|
|
|
|
r.WriteFile("included.file", "included file", when)
|
|
|
|
r.WriteFile("included.dir/included.sub.file", "included sub file", when)
|
|
|
|
require.NoError(t, os.Symlink("included.file", filepath.Join(r.LocalName, "included.file.link")))
|
|
|
|
require.NoError(t, os.Symlink("included.dir", filepath.Join(r.LocalName, "included.dir.link")))
|
|
|
|
require.NoError(t, os.Symlink("dangling", filepath.Join(r.LocalName, "dangling.link")))
|
|
|
|
|
2023-04-03 23:28:12 +08:00
|
|
|
defer func() {
|
|
|
|
// Reset -L/-l mode
|
|
|
|
f.opt.FollowSymlinks = false
|
|
|
|
f.opt.TranslateSymlinks = false
|
|
|
|
f.lstat = os.Lstat
|
|
|
|
}()
|
|
|
|
if copyLinks {
|
|
|
|
// Set fs into "-L" mode
|
|
|
|
f.opt.FollowSymlinks = true
|
|
|
|
f.opt.TranslateSymlinks = false
|
|
|
|
f.lstat = os.Stat
|
|
|
|
} else {
|
|
|
|
// Set fs into "-l" mode
|
|
|
|
f.opt.FollowSymlinks = false
|
|
|
|
f.opt.TranslateSymlinks = true
|
|
|
|
f.lstat = os.Lstat
|
|
|
|
}
|
2023-02-27 20:16:49 +08:00
|
|
|
|
|
|
|
// Check set up for filtering
|
|
|
|
assert.True(t, f.Features().FilterAware)
|
|
|
|
|
|
|
|
// Reset global error count
|
|
|
|
accounting.Stats(ctx).ResetErrors()
|
|
|
|
assert.Equal(t, int64(0), accounting.Stats(ctx).GetErrors(), "global errors found")
|
|
|
|
|
|
|
|
// Add a filter
|
|
|
|
ctx, fi := filter.AddConfig(ctx)
|
|
|
|
require.NoError(t, fi.AddRule("+ included.file"))
|
|
|
|
require.NoError(t, fi.AddRule("+ included.dir/**"))
|
2023-04-03 23:28:12 +08:00
|
|
|
if copyLinks {
|
|
|
|
require.NoError(t, fi.AddRule("+ included.file.link"))
|
|
|
|
require.NoError(t, fi.AddRule("+ included.dir.link/**"))
|
|
|
|
} else {
|
|
|
|
require.NoError(t, fi.AddRule("+ included.file.link.rclonelink"))
|
|
|
|
require.NoError(t, fi.AddRule("+ included.dir.link.rclonelink"))
|
|
|
|
}
|
2023-02-27 20:16:49 +08:00
|
|
|
require.NoError(t, fi.AddRule("- *"))
|
|
|
|
|
|
|
|
// Check listing without use filter flag
|
|
|
|
entries, err := f.List(ctx, "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-04-03 23:28:12 +08:00
|
|
|
if copyLinks {
|
|
|
|
// Check 1 global errors one for each dangling symlink
|
|
|
|
assert.Equal(t, int64(1), accounting.Stats(ctx).GetErrors(), "global errors found")
|
|
|
|
} else {
|
|
|
|
// Check 0 global errors as dangling symlink copied properly
|
|
|
|
assert.Equal(t, int64(0), accounting.Stats(ctx).GetErrors(), "global errors found")
|
|
|
|
}
|
2023-02-27 20:16:49 +08:00
|
|
|
accounting.Stats(ctx).ResetErrors()
|
|
|
|
|
|
|
|
sort.Sort(entries)
|
2023-04-03 23:28:12 +08:00
|
|
|
if copyLinks {
|
|
|
|
require.Equal(t, "[included.dir included.dir.link included.file included.file.link]", fmt.Sprint(entries))
|
|
|
|
} else {
|
|
|
|
require.Equal(t, "[dangling.link.rclonelink included.dir included.dir.link.rclonelink included.file included.file.link.rclonelink]", fmt.Sprint(entries))
|
|
|
|
}
|
2023-02-27 20:16:49 +08:00
|
|
|
|
|
|
|
// Add user filter flag
|
|
|
|
ctx = filter.SetUseFilter(ctx, true)
|
|
|
|
|
|
|
|
// Check listing with use filter flag
|
|
|
|
entries, err = f.List(ctx, "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(0), accounting.Stats(ctx).GetErrors(), "global errors found")
|
|
|
|
|
|
|
|
sort.Sort(entries)
|
2023-04-03 23:28:12 +08:00
|
|
|
if copyLinks {
|
|
|
|
require.Equal(t, "[included.dir included.dir.link included.file included.file.link]", fmt.Sprint(entries))
|
|
|
|
} else {
|
|
|
|
require.Equal(t, "[included.dir included.dir.link.rclonelink included.file included.file.link.rclonelink]", fmt.Sprint(entries))
|
|
|
|
}
|
2023-02-27 20:16:49 +08:00
|
|
|
|
|
|
|
// Check listing through a symlink still works
|
|
|
|
entries, err = f.List(ctx, "included.dir")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(0), accounting.Stats(ctx).GetErrors(), "global errors found")
|
|
|
|
|
|
|
|
sort.Sort(entries)
|
|
|
|
require.Equal(t, "[included.dir/included.sub.file]", fmt.Sprint(entries))
|
|
|
|
}
|
2023-04-03 23:28:12 +08:00
|
|
|
|
|
|
|
func TestFilterSymlinkCopyLinks(t *testing.T) {
|
|
|
|
testFilterSymlink(t, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilterSymlinkLinks(t *testing.T) {
|
|
|
|
testFilterSymlink(t, false)
|
|
|
|
}
|
2023-07-02 00:18:21 +08:00
|
|
|
|
|
|
|
func TestCopySymlink(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
when := time.Now()
|
|
|
|
f := r.Flocal.(*Fs)
|
|
|
|
|
|
|
|
// Create a file and a symlink to it
|
|
|
|
r.WriteFile("src/file.txt", "hello world", when)
|
|
|
|
require.NoError(t, os.Symlink("file.txt", filepath.Join(r.LocalName, "src", "link.txt")))
|
|
|
|
defer func() {
|
|
|
|
// Reset -L/-l mode
|
|
|
|
f.opt.FollowSymlinks = false
|
|
|
|
f.opt.TranslateSymlinks = false
|
|
|
|
f.lstat = os.Lstat
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Set fs into "-l/--links" mode
|
|
|
|
f.opt.FollowSymlinks = false
|
|
|
|
f.opt.TranslateSymlinks = true
|
|
|
|
f.lstat = os.Lstat
|
|
|
|
|
|
|
|
// Create dst
|
|
|
|
require.NoError(t, f.Mkdir(ctx, "dst"))
|
|
|
|
|
|
|
|
// Do copy from src into dst
|
|
|
|
src, err := f.NewObject(ctx, "src/link.txt.rclonelink")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, src)
|
|
|
|
dst, err := operations.Copy(ctx, f, nil, "dst/link.txt.rclonelink", src)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, dst)
|
|
|
|
|
|
|
|
// Test that we made a symlink and it has the right contents
|
|
|
|
dstPath := filepath.Join(r.LocalName, "dst", "link.txt")
|
|
|
|
linkContents, err := os.Readlink(dstPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "file.txt", linkContents)
|
2024-09-17 04:24:19 +08:00
|
|
|
|
|
|
|
// Set fs into "-L/--copy-links" mode
|
|
|
|
f.opt.FollowSymlinks = true
|
|
|
|
f.opt.TranslateSymlinks = false
|
|
|
|
f.lstat = os.Stat
|
|
|
|
|
|
|
|
// Create dst
|
|
|
|
require.NoError(t, f.Mkdir(ctx, "dst2"))
|
|
|
|
|
|
|
|
// Do copy from src into dst
|
|
|
|
src, err = f.NewObject(ctx, "src/link.txt")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, src)
|
|
|
|
dst, err = operations.Copy(ctx, f, nil, "dst2/link.txt", src)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, dst)
|
|
|
|
|
|
|
|
// Test that we made a NON-symlink and it has the right contents
|
|
|
|
dstPath = filepath.Join(r.LocalName, "dst2", "link.txt")
|
|
|
|
fi, err := os.Lstat(dstPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, fi.Mode()&os.ModeSymlink == 0)
|
|
|
|
want := fstest.NewItem("dst2/link.txt", "hello world", when)
|
|
|
|
fstest.CompareItems(t, []fs.DirEntry{dst}, []fstest.Item{want}, nil, f.precision, "")
|
|
|
|
|
|
|
|
// Test that copying a normal file also works
|
|
|
|
dst, err = operations.Copy(ctx, f, nil, "dst2/file.txt", dst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, dst)
|
|
|
|
want = fstest.NewItem("dst2/file.txt", "hello world", when)
|
|
|
|
fstest.CompareItems(t, []fs.DirEntry{dst}, []fstest.Item{want}, nil, f.precision, "")
|
2023-07-02 00:18:21 +08:00
|
|
|
}
|