mirror of
https://github.com/rclone/rclone.git
synced 2025-08-12 15:07:24 +02:00
operations: add RemoveExisting to safely remove an existing file
This renames the file first and if the operation is successful then it deletes the renamed file.
This commit is contained in:
@ -1884,3 +1884,76 @@ func TestDirsEqual(t *testing.T) {
|
||||
equal = operations.DirsEqual(ctx, src, dst, opt)
|
||||
assert.True(t, equal)
|
||||
}
|
||||
|
||||
func TestRemoveExisting(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
r := fstest.NewRun(t)
|
||||
if r.Fremote.Features().Move == nil {
|
||||
t.Skip("Skipping as remote can't Move")
|
||||
}
|
||||
|
||||
file1 := r.WriteObject(ctx, "sub dir/test remove existing", "hello world", t1)
|
||||
file2 := r.WriteObject(ctx, "sub dir/test remove existing with long name 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "hello long name world", t1)
|
||||
|
||||
r.CheckRemoteItems(t, file1, file2)
|
||||
|
||||
var returnedError error
|
||||
|
||||
// Check not found first
|
||||
cleanup, err := operations.RemoveExisting(ctx, r.Fremote, "not found", "TEST")
|
||||
assert.Equal(t, err, nil)
|
||||
r.CheckRemoteItems(t, file1, file2)
|
||||
cleanup(&returnedError)
|
||||
r.CheckRemoteItems(t, file1, file2)
|
||||
|
||||
// Remove file1
|
||||
cleanup, err = operations.RemoveExisting(ctx, r.Fremote, file1.Path, "TEST")
|
||||
assert.Equal(t, err, nil)
|
||||
//r.CheckRemoteItems(t, file1, file2)
|
||||
|
||||
// Check file1 with temporary name exists
|
||||
var buf bytes.Buffer
|
||||
err = operations.List(ctx, r.Fremote, &buf)
|
||||
require.NoError(t, err)
|
||||
res := buf.String()
|
||||
assert.NotContains(t, res, " 11 "+file1.Path+"\n")
|
||||
assert.Contains(t, res, " 11 "+file1.Path+".")
|
||||
assert.Contains(t, res, " 21 "+file2.Path+"\n")
|
||||
|
||||
cleanup(&returnedError)
|
||||
r.CheckRemoteItems(t, file2)
|
||||
|
||||
// Remove file2 with an error
|
||||
cleanup, err = operations.RemoveExisting(ctx, r.Fremote, file2.Path, "TEST")
|
||||
assert.Equal(t, err, nil)
|
||||
|
||||
// Check file2 with truncated temporary name exists
|
||||
buf.Reset()
|
||||
err = operations.List(ctx, r.Fremote, &buf)
|
||||
require.NoError(t, err)
|
||||
res = buf.String()
|
||||
assert.NotContains(t, res, " 21 "+file2.Path+"\n")
|
||||
assert.NotContains(t, res, " 21 "+file2.Path+".")
|
||||
assert.Contains(t, res, " 21 "+file2.Path[:100])
|
||||
|
||||
returnedError = errors.New("BOOM")
|
||||
cleanup(&returnedError)
|
||||
r.CheckRemoteItems(t, file2)
|
||||
|
||||
// Remove file2
|
||||
cleanup, err = operations.RemoveExisting(ctx, r.Fremote, file2.Path, "TEST")
|
||||
assert.Equal(t, err, nil)
|
||||
|
||||
// Check file2 with truncated temporary name exists
|
||||
buf.Reset()
|
||||
err = operations.List(ctx, r.Fremote, &buf)
|
||||
require.NoError(t, err)
|
||||
res = buf.String()
|
||||
assert.NotContains(t, res, " 21 "+file2.Path+"\n")
|
||||
assert.NotContains(t, res, " 21 "+file2.Path+".")
|
||||
assert.Contains(t, res, " 21 "+file2.Path[:100])
|
||||
|
||||
returnedError = nil
|
||||
cleanup(&returnedError)
|
||||
r.CheckRemoteItems(t)
|
||||
}
|
||||
|
Reference in New Issue
Block a user