1
0
mirror of https://github.com/rclone/rclone.git synced 2026-01-08 03:23:26 +00:00

filter: deglobalise to put filter config into the context #4685

This commit is contained in:
Nick Craig-Wood
2020-11-26 17:10:41 +00:00
parent 354b4f19ec
commit c22d04aa30
15 changed files with 176 additions and 89 deletions

View File

@@ -31,6 +31,7 @@ type syncCopyMove struct {
dir string
// internal state
ci *fs.ConfigInfo // global config
fi *filter.Filter // filter config
ctx context.Context // internal context for controlling go-routines
cancel func() // cancel the context
inCtx context.Context // internal context for controlling march
@@ -99,8 +100,10 @@ func newSyncCopyMove(ctx context.Context, fdst, fsrc fs.Fs, deleteMode fs.Delete
return nil, fserrors.FatalError(fs.ErrorOverlapping)
}
ci := fs.GetConfig(ctx)
fi := filter.GetConfig(ctx)
s := &syncCopyMove{
ci: ci,
fi: fi,
fdst: fdst,
fsrc: fsrc,
deleteMode: deleteMode,
@@ -828,7 +831,7 @@ func (s *syncCopyMove) run() error {
Dir: s.dir,
NoTraverse: s.noTraverse,
Callback: s,
DstIncludeAll: filter.Active.Opt.DeleteExcluded,
DstIncludeAll: s.fi.Opt.DeleteExcluded,
NoCheckDest: s.noCheckDest,
NoUnicodeNormalization: s.noUnicodeNormalization,
}
@@ -1087,13 +1090,14 @@ func moveDir(ctx context.Context, fdst, fsrc fs.Fs, deleteEmptySrcDirs bool, cop
// MoveDir moves fsrc into fdst
func MoveDir(ctx context.Context, fdst, fsrc fs.Fs, deleteEmptySrcDirs bool, copyEmptySrcDirs bool) error {
fi := filter.GetConfig(ctx)
if operations.Same(fdst, fsrc) {
fs.Errorf(fdst, "Nothing to do as source and destination are the same")
return nil
}
// First attempt to use DirMover if exists, same Fs and no filters are active
if fdstDirMove := fdst.Features().DirMove; fdstDirMove != nil && operations.SameConfig(fsrc, fdst) && filter.Active.InActive() {
if fdstDirMove := fdst.Features().DirMove; fdstDirMove != nil && operations.SameConfig(fsrc, fdst) && fi.InActive() {
if operations.SkipDestructive(ctx, fdst, "server-side directory move") {
return nil
}

View File

@@ -177,13 +177,12 @@ func testCopyWithFilesFrom(t *testing.T, noTraverse bool) {
require.NoError(t, f.AddFile("potato2"))
require.NoError(t, f.AddFile("notfound"))
// Monkey patch the active filter
oldFilter := filter.Active
// Change the active filter
ctx = filter.ReplaceConfig(ctx, f)
oldNoTraverse := ci.NoTraverse
filter.Active = f
ci.NoTraverse = noTraverse
unpatch := func() {
filter.Active = oldFilter
ci.NoTraverse = oldNoTraverse
}
defer unpatch()
@@ -967,9 +966,10 @@ func TestSyncWithExclude(t *testing.T) {
fstest.CheckItems(t, r.Fremote, file1, file2)
fstest.CheckItems(t, r.Flocal, file1, file2, file3)
filter.Active.Opt.MaxSize = 40
fi := filter.GetConfig(ctx)
fi.Opt.MaxSize = 40
defer func() {
filter.Active.Opt.MaxSize = -1
fi.Opt.MaxSize = -1
}()
accounting.GlobalStats().ResetCounters()
@@ -996,11 +996,12 @@ func TestSyncWithExcludeAndDeleteExcluded(t *testing.T) {
fstest.CheckItems(t, r.Fremote, file1, file2, file3)
fstest.CheckItems(t, r.Flocal, file1, file2, file3)
filter.Active.Opt.MaxSize = 40
filter.Active.Opt.DeleteExcluded = true
fi := filter.GetConfig(ctx)
fi.Opt.MaxSize = 40
fi.Opt.DeleteExcluded = true
defer func() {
filter.Active.Opt.MaxSize = -1
filter.Active.Opt.DeleteExcluded = false
fi.Opt.MaxSize = -1
fi.Opt.DeleteExcluded = false
}()
accounting.GlobalStats().ResetCounters()
@@ -1399,12 +1400,14 @@ func TestServerSideMove(t *testing.T) {
// Test a server-side move if possible, or the backup path if not
func TestServerSideMoveWithFilter(t *testing.T) {
ctx := context.Background()
r := fstest.NewRun(t)
defer r.Finalise()
filter.Active.Opt.MinSize = 40
fi := filter.GetConfig(ctx)
fi.Opt.MinSize = 40
defer func() {
filter.Active.Opt.MinSize = -1
fi.Opt.MinSize = -1
}()
testServerSideMove(t, r, true, false)
@@ -1439,9 +1442,10 @@ func TestServerSideMoveOverlap(t *testing.T) {
assert.EqualError(t, err, fs.ErrorOverlapping.Error())
// Now try with a filter which should also fail with ErrorCantMoveOverlapping
filter.Active.Opt.MinSize = 40
fi := filter.GetConfig(ctx)
fi.Opt.MinSize = 40
defer func() {
filter.Active.Opt.MinSize = -1
fi.Opt.MinSize = -1
}()
err = MoveDir(ctx, FremoteMove, r.Fremote, false, false)
assert.EqualError(t, err, fs.ErrorOverlapping.Error())
@@ -1686,11 +1690,8 @@ func testSyncBackupDir(t *testing.T, backupDir string, suffix string, suffixKeep
flt, err := filter.NewFilter(nil)
require.NoError(t, err)
require.NoError(t, flt.AddRule("- *"+suffix))
oldFlt := filter.Active
filter.Active = flt
defer func() {
filter.Active = oldFlt
}()
// Change the active filter
ctx = filter.ReplaceConfig(ctx, flt)
}
ci.Suffix = suffix
ci.SuffixKeepExtension = suffixKeepExtension