Commit e663cddc authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

In pathfs.LockingFileSystem, lock file operations too.

parent d7ac6b93
package nodefs
import (
"fmt"
"time"
"sync"
"github.com/hanwen/go-fuse/fuse"
)
type lockingFile struct {
mu *sync.Mutex
file File
}
// NewDefaultFile returns a File instance that returns ENOSYS for
// every operation.
func NewLockingFile(mu *sync.Mutex, f File) File {
return &lockingFile{
mu: mu,
file: f,
}
}
func (f *lockingFile) SetInode(*Inode) {
}
func (f *lockingFile) InnerFile() File {
return nil
}
func (f *lockingFile) String() string {
return fmt.Sprintf("lockingFile(%s)", f.file.String())
}
func (f *lockingFile) Read(buf []byte, off int64) (fuse.ReadResult, fuse.Status) {
f.mu.Lock()
defer f.mu.Unlock()
return f.file.Read(buf, off)
}
func (f *lockingFile) Write(data []byte, off int64) (uint32, fuse.Status) {
f.mu.Lock()
defer f.mu.Unlock()
return f.file.Write(data, off)
}
func (f *lockingFile) Flush() fuse.Status {
f.mu.Lock()
defer f.mu.Unlock()
return f.file.Flush()
}
func (f *lockingFile) Release() {
f.mu.Lock()
defer f.mu.Unlock()
f.file.Release()
}
func (f *lockingFile) GetAttr(a *fuse.Attr) fuse.Status {
f.mu.Lock()
defer f.mu.Unlock()
return f.file.GetAttr(a)
}
func (f *lockingFile) Fsync(flags int) (code fuse.Status) {
f.mu.Lock()
defer f.mu.Unlock()
return f.file.Fsync(flags)
}
func (f *lockingFile) Utimens(atime *time.Time, mtime *time.Time) fuse.Status {
f.mu.Lock()
defer f.mu.Unlock()
return f.file.Utimens(atime, mtime)
}
func (f *lockingFile) Truncate(size uint64) fuse.Status {
f.mu.Lock()
defer f.mu.Unlock()
return f.file.Truncate(size)
}
func (f *lockingFile) Chown(uid uint32, gid uint32) fuse.Status {
f.mu.Lock()
defer f.mu.Unlock()
return f.file.Chown(uid, gid)
}
func (f *lockingFile) Chmod(perms uint32) fuse.Status {
f.mu.Lock()
defer f.mu.Unlock()
return f.file.Chmod(perms)
}
func (f *lockingFile) Allocate(off uint64, size uint64, mode uint32) (code fuse.Status) {
f.mu.Lock()
defer f.mu.Unlock()
return f.file.Allocate(off, size, mode)
}
......@@ -15,8 +15,6 @@ type lockingFileSystem struct {
lock sync.Mutex
}
var _ = ((FileSystem)((*lockingFileSystem)(nil)))
// NewLockingFileSystem is a wrapper that makes a FileSystem
// threadsafe by serializing each operation.
func NewLockingFileSystem(pfs FileSystem) FileSystem {
......@@ -101,7 +99,9 @@ func (fs *lockingFileSystem) Truncate(name string, offset uint64, context *fuse.
}
func (fs *lockingFileSystem) Open(name string, flags uint32, context *fuse.Context) (file nodefs.File, code fuse.Status) {
return fs.FS.Open(name, flags, context)
file, code = fs.FS.Open(name, flags, context)
file = nodefs.NewLockingFile(&fs.lock, file)
return
}
func (fs *lockingFileSystem) OpenDir(name string, context *fuse.Context) (stream []fuse.DirEntry, status fuse.Status) {
......@@ -126,7 +126,10 @@ func (fs *lockingFileSystem) Access(name string, mode uint32, context *fuse.Cont
func (fs *lockingFileSystem) Create(name string, flags uint32, mode uint32, context *fuse.Context) (file nodefs.File, code fuse.Status) {
defer fs.locked()()
return fs.FS.Create(name, flags, mode, context)
file, code = fs.FS.Create(name, flags, mode, context)
file = nodefs.NewLockingFile(&fs.lock, file)
return file, code
}
func (fs *lockingFileSystem) Utimens(name string, Atime *time.Time, Mtime *time.Time, context *fuse.Context) (code fuse.Status) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment