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: rename Fsync to Sync and implement Sync on Node and Handle

Nick Craig-Woodb4083b4371ee
diff --git a/cmd/mount/dir.go b/cmd/mount/dir.go
index 8cef250a5..d0485f878 100644
--- a/cmd/mount/dir.go
+++ b/cmd/mount/dir.go
@@ -174,7 +174,7 @@ var _ fusefs.NodeFsyncer = (*Dir)(nil)
// Fsync the directory
func (d *Dir) Fsync(ctx context.Context, req *fuse.FsyncRequest) (err error) {
defer fs.Trace(d, "")("err=%v", &err)
- err = d.Dir.Fsync()
+ err = d.Dir.Sync()
if err != nil {
return translateError(err)
}
diff --git a/vfs/dir.go b/vfs/dir.go
index 962aa460d..1637c0bd2 100644
--- a/vfs/dir.go
+++ b/vfs/dir.go
@@ -498,10 +498,10 @@ func (d *Dir) Rename(oldName, newName string, destDir *Dir) error {
return nil
}
-// Fsync the directory
+// Sync the directory
//
// Note that we don't do anything except return OK
-func (d *Dir) Fsync() error {
+func (d *Dir) Sync() error {
return nil
}
diff --git a/vfs/dir_test.go b/vfs/dir_test.go
index 5c9fc0bc6..c8f20614a 100644
--- a/vfs/dir_test.go
+++ b/vfs/dir_test.go
@@ -64,8 +64,8 @@ func TestDirMethods(t *testing.T) {
// Size
assert.Equal(t, int64(0), dir.Size())
- // Fsync
- assert.NoError(t, dir.Fsync())
+ // Sync
+ assert.NoError(t, dir.Sync())
// DirEntry
assert.Equal(t, dir.entry, dir.DirEntry())
diff --git a/vfs/file.go b/vfs/file.go
index f2bce87e0..5fa495c2b 100644
--- a/vfs/file.go
+++ b/vfs/file.go
@@ -293,10 +293,10 @@ func (f *File) OpenRW(flags int) (fh *RWFileHandle, err error) {
return fh, nil
}
-// Fsync the file
+// Sync the file
//
// Note that we don't do anything except return OK
-func (f *File) Fsync() error {
+func (f *File) Sync() error {
return nil
}
diff --git a/vfs/file_test.go b/vfs/file_test.go
index 3c6e206f3..1c9eb9c67 100644
--- a/vfs/file_test.go
+++ b/vfs/file_test.go
@@ -62,8 +62,8 @@ func TestFileMethods(t *testing.T) {
// Size
assert.Equal(t, int64(14), file.Size())
- // Fsync
- assert.NoError(t, file.Fsync())
+ // Sync
+ assert.NoError(t, file.Sync())
// DirEntry
assert.Equal(t, file.o, file.DirEntry())
diff --git a/vfs/read_write.go b/vfs/read_write.go
index 68c2ae569..d66bb3d77 100644
--- a/vfs/read_write.go
+++ b/vfs/read_write.go
@@ -408,3 +408,18 @@ func (fh *RWFileHandle) Truncate(size int64) (err error) {
fh.file.setSize(size)
return fh.File.Truncate(size)
}
+
+// Sync commits the current contents of the file to stable storage. Typically,
+// this means flushing the file system's in-memory copy of recently written
+// data to disk.
+func (fh *RWFileHandle) Sync() error {
+ fh.mu.Lock()
+ defer fh.mu.Unlock()
+ if fh.closed {
+ return ECLOSED
+ }
+ if !fh.opened {
+ return nil
+ }
+ return fh.File.Sync()
+}
diff --git a/vfs/read_write_test.go b/vfs/read_write_test.go
index 49f0b46b2..6f482cf5a 100644
--- a/vfs/read_write_test.go
+++ b/vfs/read_write_test.go
@@ -78,6 +78,10 @@ func TestRWFileHandleMethodsRead(t *testing.T) {
_, err := fh.Read(buf)
assert.Equal(t, io.EOF, err)
+ // Sync
+ err = fh.Sync()
+ assert.NoError(t, err)
+
// Stat
var fi os.FileInfo
fi, err = fh.Stat()
diff --git a/vfs/vfs.go b/vfs/vfs.go
index 14e646a25..4d20e89a9 100644
--- a/vfs/vfs.go
+++ b/vfs/vfs.go
@@ -54,7 +54,7 @@ type Node interface {
IsFile() bool
Inode() uint64
SetModTime(modTime time.Time) error
- Fsync() error
+ Sync() error
Remove() error
RemoveAll() error
DirEntry() fs.DirEntry
diff --git a/vfs/write.go b/vfs/write.go
index 3c107f486..5c68ba540 100644
--- a/vfs/write.go
+++ b/vfs/write.go
@@ -264,3 +264,10 @@ func (fh *WriteFileHandle) ReadAt(p []byte, off int64) (n int, err error) {
fs.Errorf(fh.remote, "ReadAt: Can't read and write to file without cache")
return 0, EPERM
}
+
+// Sync commits the current contents of the file to stable storage. Typically,
+// this means flushing the file system's in-memory copy of recently written
+// data to disk.
+func (fh *WriteFileHandle) Sync() error {
+ return nil
+}
diff --git a/vfs/write_test.go b/vfs/write_test.go
index daadeeea8..45d66f6b4 100644
--- a/vfs/write_test.go
+++ b/vfs/write_test.go
@@ -65,6 +65,10 @@ func TestWriteFileHandleMethods(t *testing.T) {
_, err = fh.ReadAt(buf, 0)
assert.Equal(t, EPERM, err)
+ // Sync
+ err = fh.Sync()
+ assert.NoError(t, err)
+
// Close
assert.NoError(t, fh.Close())