Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

vfs: fix open file renaming on drive when using --vfs-cache-mode writes

Nick Craig-Wood63128834dae2
diff --git a/vfs/file.go b/vfs/file.go
index bc213d537..c3a227b74 100644
--- a/vfs/file.go
+++ b/vfs/file.go
@@ -150,13 +150,6 @@ func (f *File) rename(ctx context.Context, destDir *Dir, newName string) error {
newPath := path.Join(destDir.path, newName)
renameCall := func(ctx context.Context) error {
- f.mu.RLock()
- o := f.o
- f.mu.RUnlock()
- if o == nil {
- return errors.New("Cannot rename: file object is not available")
- }
-
// chain rename calls if any
if oldPendingRenameFun != nil {
err := oldPendingRenameFun(ctx)
@@ -165,6 +158,16 @@ func (f *File) rename(ctx context.Context, destDir *Dir, newName string) error {
}
}
+ f.mu.RLock()
+ o := f.o
+ f.mu.RUnlock()
+ if o == nil {
+ return errors.New("Cannot rename: file object is not available")
+ }
+ if o.Remote() == newPath {
+ return nil // no need to rename
+ }
+
// do the move of the remote object
dstOverwritten, _ := d.f.NewObject(ctx, newPath)
newObject, err := operations.Move(ctx, d.f, dstOverwritten, newPath, o)
diff --git a/vfs/read_write.go b/vfs/read_write.go
index 05fcf8a88..2344f1223 100644
--- a/vfs/read_write.go
+++ b/vfs/read_write.go
@@ -284,7 +284,12 @@ func (fh *RWFileHandle) flushWrites(closeFile bool) error {
return err
}
- o, err := copyObj(fh.d.vfs.f, fh.file.getObject(), fh.file.Path(), cacheObj)
+ objPath := fh.file.Path()
+ objOld := fh.file.getObject()
+ if objOld != nil {
+ objPath = objOld.Remote() // use the path of the actual object if available
+ }
+ o, err := copyObj(fh.d.vfs.f, objOld, objPath, cacheObj)
if err != nil {
err = errors.Wrap(err, "failed to transfer file from cache to remote")
fs.Errorf(fh.logPrefix(), "%v", err)