forked from TrueCloudLab/rclone
vfs: Add SetSys() methods to Node to allow caching stuff on a node
This commit is contained in:
parent
7f8d74e903
commit
cfcdc85b26
5 changed files with 31 additions and 2 deletions
12
vfs/dir.go
12
vfs/dir.go
|
@ -32,6 +32,7 @@ type Dir struct {
|
||||||
entry fs.Directory
|
entry fs.Directory
|
||||||
read time.Time // time directory entry last read
|
read time.Time // time directory entry last read
|
||||||
items map[string]Node // directory entries - can be empty but not nil
|
items map[string]Node // directory entries - can be empty but not nil
|
||||||
|
sys interface{} // user defined info to be attached here
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDir(vfs *VFS, f fs.Fs, parent *Dir, fsDir fs.Directory) *Dir {
|
func newDir(vfs *VFS, f fs.Fs, parent *Dir, fsDir fs.Directory) *Dir {
|
||||||
|
@ -92,7 +93,16 @@ func (d *Dir) Path() (name string) {
|
||||||
|
|
||||||
// Sys returns underlying data source (can be nil) - satisfies Node interface
|
// Sys returns underlying data source (can be nil) - satisfies Node interface
|
||||||
func (d *Dir) Sys() interface{} {
|
func (d *Dir) Sys() interface{} {
|
||||||
return nil
|
d.mu.RLock()
|
||||||
|
defer d.mu.RUnlock()
|
||||||
|
return d.sys
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetSys sets the underlying data source (can be nil) - satisfies Node interface
|
||||||
|
func (d *Dir) SetSys(x interface{}) {
|
||||||
|
d.mu.Lock()
|
||||||
|
d.sys = x
|
||||||
|
d.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inode returns the inode number - satisfies Node interface
|
// Inode returns the inode number - satisfies Node interface
|
||||||
|
|
|
@ -54,6 +54,10 @@ func TestDirMethods(t *testing.T) {
|
||||||
// Sys
|
// Sys
|
||||||
assert.Equal(t, nil, dir.Sys())
|
assert.Equal(t, nil, dir.Sys())
|
||||||
|
|
||||||
|
// SetSys
|
||||||
|
dir.SetSys(42)
|
||||||
|
assert.Equal(t, 42, dir.Sys())
|
||||||
|
|
||||||
// Inode
|
// Inode
|
||||||
assert.NotEqual(t, uint64(0), dir.Inode())
|
assert.NotEqual(t, uint64(0), dir.Inode())
|
||||||
|
|
||||||
|
|
12
vfs/file.go
12
vfs/file.go
|
@ -52,6 +52,7 @@ type File struct {
|
||||||
pendingModTime time.Time // will be applied once o becomes available, i.e. after file was written
|
pendingModTime time.Time // will be applied once o becomes available, i.e. after file was written
|
||||||
pendingRenameFun func(ctx context.Context) error // will be run/renamed after all writers close
|
pendingRenameFun func(ctx context.Context) error // will be run/renamed after all writers close
|
||||||
appendMode bool // file was opened with O_APPEND
|
appendMode bool // file was opened with O_APPEND
|
||||||
|
sys interface{} // user defined info to be attached here
|
||||||
|
|
||||||
muRW sync.Mutex // synchonize RWFileHandle.openPending(), RWFileHandle.close() and File.Remove
|
muRW sync.Mutex // synchonize RWFileHandle.openPending(), RWFileHandle.close() and File.Remove
|
||||||
}
|
}
|
||||||
|
@ -130,7 +131,16 @@ func (f *File) osPath() string {
|
||||||
|
|
||||||
// Sys returns underlying data source (can be nil) - satisfies Node interface
|
// Sys returns underlying data source (can be nil) - satisfies Node interface
|
||||||
func (f *File) Sys() interface{} {
|
func (f *File) Sys() interface{} {
|
||||||
return nil
|
f.mu.RLock()
|
||||||
|
defer f.mu.RUnlock()
|
||||||
|
return f.sys
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetSys sets the underlying data source (can be nil) - satisfies Node interface
|
||||||
|
func (f *File) SetSys(x interface{}) {
|
||||||
|
f.mu.Lock()
|
||||||
|
f.sys = x
|
||||||
|
f.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inode returns the inode number - satisfies Node interface
|
// Inode returns the inode number - satisfies Node interface
|
||||||
|
|
|
@ -58,6 +58,10 @@ func TestFileMethods(t *testing.T) {
|
||||||
// Sys
|
// Sys
|
||||||
assert.Equal(t, nil, file.Sys())
|
assert.Equal(t, nil, file.Sys())
|
||||||
|
|
||||||
|
// SetSys
|
||||||
|
file.SetSys(42)
|
||||||
|
assert.Equal(t, 42, file.Sys())
|
||||||
|
|
||||||
// Inode
|
// Inode
|
||||||
assert.NotEqual(t, uint64(0), file.Inode())
|
assert.NotEqual(t, uint64(0), file.Inode())
|
||||||
|
|
||||||
|
|
|
@ -54,6 +54,7 @@ type Node interface {
|
||||||
Open(flags int) (Handle, error)
|
Open(flags int) (Handle, error)
|
||||||
Truncate(size int64) error
|
Truncate(size int64) error
|
||||||
Path() string
|
Path() string
|
||||||
|
SetSys(interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check interfaces
|
// Check interfaces
|
||||||
|
|
Loading…
Reference in a new issue