"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files
Go azure-blobazure-blob-storageazure-filesbackblaze-b2cloud-storagedropboxencryptionftpfuse-filesystemgogolanggoogle-cloud-storagegoogle-driveonedriveopenstack-swiftrclones3sftpsyncwebdav
vfs: keep track of number of open RWHandles
Nick Craig-Wood9b011ce7e423
diff --git a/vfs/file.go b/vfs/file.goindex a85aaea00..63513d44d 100644--- a/vfs/file.go+++ b/vfs/file.go@@ -21,6 +21,7 @@ type File struct {mu sync.Mutex // protects the followingo fs.Object // NB o may be nil if file is being writtenleaf string // leaf name of the object+ rwOpenCount int // number of open files on this handlewriters []Handle // writers for this filereadWriters int // how many RWFileHandle are open for writingreadWriterClosing bool // is a RWFileHandle currently cosing?@@ -138,6 +139,31 @@ func (f *File) delWriter(h Handle, modifiedCacheFile bool) (lastWriterAndModifiereturn}+// addRWOpen should be called by ReadWriteHandle when they have+// actually opened the file for read or write.+func (f *File) addRWOpen() {+ f.mu.Lock()+ f.rwOpenCount+++ f.mu.Unlock()+}++// delRWOpen should be called by ReadWriteHandle when they have closed+// an actually opene file for read or write.+func (f *File) delRWOpen() {+ f.mu.Lock()+ f.rwOpenCount--+ f.mu.Unlock()+}++// rwOpens returns how many active open ReadWriteHandles there are.+// Note that file handles which are in pending open state aren't+// counted.+func (f *File) rwOpens() int {+ f.mu.Lock()+ defer f.mu.Unlock()+ return f.rwOpenCount+}+// finishWriterClose resets the readWriterClosing flagfunc (f *File) finishWriterClose() {f.mu.Lock()diff --git a/vfs/read_write.go b/vfs/read_write.goindex 5243220fc..2b8e9525b 100644--- a/vfs/read_write.go+++ b/vfs/read_write.go@@ -192,6 +192,7 @@ func (fh *RWFileHandle) openPending(truncate bool) (err error) {}fh.File = fdfh.opened = true+ fh.file.addRWOpen()fh.d.addObject(fh.file) // make sure the directory has this object in it nowreturn nil}@@ -246,7 +247,12 @@ func (fh *RWFileHandle) close() (err error) {return ECLOSED}fh.closed = true- defer fh.d.vfs.cache.close(fh.remote)+ defer func() {+ if fh.opened {+ fh.file.delRWOpen()+ }+ fh.d.vfs.cache.close(fh.remote)+ }()rdwrMode := fh.flags & accessModeMaskwriter := rdwrMode != os.O_RDONLYdiff --git a/vfs/read_write_test.go b/vfs/read_write_test.goindex 52e7066c8..075a24cb3 100644--- a/vfs/read_write_test.go+++ b/vfs/read_write_test.go@@ -74,9 +74,15 @@ func TestRWFileHandleMethodsRead(t *testing.T) {// Sizeassert.Equal(t, int64(16), fh.Size())+ // No opens yet+ assert.Equal(t, 0, fh.file.rwOpens())+// Read 1assert.Equal(t, "0", rwReadString(t, fh, 1))+ // Open after the read+ assert.Equal(t, 1, fh.file.rwOpens())+// Read remainderassert.Equal(t, "123456789abcdef", rwReadString(t, fh, 256))@@ -101,6 +107,9 @@ func TestRWFileHandleMethodsRead(t *testing.T) {assert.Equal(t, nil, fh.Close())assert.True(t, fh.closed)+ // No opens again+ assert.Equal(t, 0, fh.file.rwOpens())+// Close againassert.Equal(t, ECLOSED, fh.Close())}@@ -266,6 +275,10 @@ func TestRWFileHandleMethodsWrite(t *testing.T) {vfs, fh := rwHandleCreateWriteOnly(t, r)defer cleanup(t, r, vfs)+ // 1 opens since we opened with O_CREATE and the file didn't+ // exist in the cache+ assert.Equal(t, 1, fh.file.rwOpens())+// Stringassert.Equal(t, "file1 (rw)", fh.String())assert.Equal(t, "<nil *RWFileHandle>", (*RWFileHandle)(nil).String())@@ -293,6 +306,9 @@ func TestRWFileHandleMethodsWrite(t *testing.T) {assert.NoError(t, err)assert.Equal(t, 5, n)+ // Open after the write+ assert.Equal(t, 1, fh.file.rwOpens())+// Offset #2assert.Equal(t, int64(5), offset())assert.Equal(t, int64(5), node.Size())@@ -323,6 +339,9 @@ func TestRWFileHandleMethodsWrite(t *testing.T) {// Closeassert.NoError(t, fh.Close())+ // No opens again+ assert.Equal(t, 0, fh.file.rwOpens())+// Check double closeerr = fh.Close()assert.Equal(t, ECLOSED, err)