2016-07-18 00:03:23 +02:00
|
|
|
// +build linux darwin freebsd
|
|
|
|
|
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// File represents a file
|
|
|
|
type File struct {
|
2017-03-30 20:58:11 +02:00
|
|
|
size int64 // size of file - read and written with atomic int64 - must be 64 bit aligned
|
|
|
|
d *Dir // parent directory - read only
|
|
|
|
mu sync.RWMutex // protects the following
|
|
|
|
o fs.Object // NB o may be nil if file is being written
|
|
|
|
writers int // number of writers for this file
|
|
|
|
pendingModTime time.Time // will be applied once o becomes available, i.e. after file was written
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// newFile creates a new File
|
|
|
|
func newFile(d *Dir, o fs.Object) *File {
|
|
|
|
return &File{
|
|
|
|
d: d,
|
|
|
|
o: o,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 22:17:45 +01:00
|
|
|
// rename should be called to update f.o and f.d after a rename
|
|
|
|
func (f *File) rename(d *Dir, o fs.Object) {
|
|
|
|
f.mu.Lock()
|
|
|
|
f.o = o
|
|
|
|
f.d = d
|
|
|
|
f.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-07-18 00:03:23 +02:00
|
|
|
// addWriters increments or decrements the writers
|
|
|
|
func (f *File) addWriters(n int) {
|
|
|
|
f.mu.Lock()
|
|
|
|
f.writers += n
|
|
|
|
f.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.Node = (*File)(nil)
|
|
|
|
|
|
|
|
// Attr fills out the attributes for the file
|
|
|
|
func (f *File) Attr(ctx context.Context, a *fuse.Attr) error {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
2016-09-09 09:39:19 +02:00
|
|
|
a.Gid = gid
|
|
|
|
a.Uid = uid
|
2016-07-18 00:03:23 +02:00
|
|
|
a.Mode = filePerms
|
|
|
|
// if o is nil it isn't valid yet, so return the size so far
|
|
|
|
if f.o == nil {
|
|
|
|
a.Size = uint64(atomic.LoadInt64(&f.size))
|
2017-03-30 20:58:11 +02:00
|
|
|
if !noModTime && !f.pendingModTime.IsZero() {
|
|
|
|
a.Atime = f.pendingModTime
|
|
|
|
a.Mtime = f.pendingModTime
|
|
|
|
a.Ctime = f.pendingModTime
|
|
|
|
a.Crtime = f.pendingModTime
|
|
|
|
}
|
2016-07-18 00:03:23 +02:00
|
|
|
} else {
|
|
|
|
a.Size = uint64(f.o.Size())
|
|
|
|
if !noModTime {
|
|
|
|
modTime := f.o.ModTime()
|
|
|
|
a.Atime = modTime
|
|
|
|
a.Mtime = modTime
|
|
|
|
a.Ctime = modTime
|
|
|
|
a.Crtime = modTime
|
|
|
|
}
|
|
|
|
}
|
2016-11-12 14:10:36 +01:00
|
|
|
a.Blocks = (a.Size + 511) / 512
|
2017-02-06 22:17:45 +01:00
|
|
|
fs.Debugf(f.o, "File.Attr %+v", a)
|
2016-07-18 00:03:23 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-23 20:41:21 +01:00
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeSetattrer = (*File)(nil)
|
|
|
|
|
|
|
|
// Setattr handles attribute changes from FUSE. Currently supports ModTime only.
|
|
|
|
func (f *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
|
|
|
|
if noModTime {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
|
|
|
if req.Valid.MtimeNow() {
|
2017-03-30 20:58:11 +02:00
|
|
|
f.pendingModTime = time.Now()
|
2017-03-23 20:41:21 +01:00
|
|
|
} else if req.Valid.Mtime() {
|
2017-03-30 20:58:11 +02:00
|
|
|
f.pendingModTime = req.Mtime
|
2017-03-23 20:41:21 +01:00
|
|
|
}
|
|
|
|
|
2017-03-30 20:58:11 +02:00
|
|
|
if f.o != nil {
|
|
|
|
return f.applyPendingModTime()
|
|
|
|
}
|
|
|
|
|
|
|
|
// queue up for later, hoping f.o becomes available
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// call with the mutex held
|
|
|
|
func (f *File) applyPendingModTime() error {
|
|
|
|
defer func() { f.pendingModTime = time.Time{} }()
|
|
|
|
|
|
|
|
if f.pendingModTime.IsZero() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.o == nil {
|
|
|
|
return errors.New("Cannot apply ModTime, file object is not available")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := f.o.SetModTime(f.pendingModTime)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
fs.Debugf(f.o, "File.applyPendingModTime OK")
|
|
|
|
case fs.ErrorCantSetModTime:
|
|
|
|
// do nothing, in order to not break "touch somefile" if it exists already
|
|
|
|
default:
|
|
|
|
fs.Errorf(f.o, "File.applyPendingModTime error: %v", err)
|
|
|
|
return err
|
2017-03-23 20:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-18 00:03:23 +02:00
|
|
|
// Update the size while writing
|
|
|
|
func (f *File) written(n int64) {
|
|
|
|
atomic.AddInt64(&f.size, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the object when written
|
|
|
|
func (f *File) setObject(o fs.Object) {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
f.o = o
|
2017-03-30 20:58:11 +02:00
|
|
|
_ = f.applyPendingModTime()
|
2016-07-18 00:03:23 +02:00
|
|
|
f.d.addObject(o, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for f.o to become non nil for a short time returning it or an
|
|
|
|
// error
|
|
|
|
//
|
|
|
|
// Call without the mutex held
|
|
|
|
func (f *File) waitForValidObject() (o fs.Object, err error) {
|
|
|
|
for i := 0; i < 50; i++ {
|
|
|
|
f.mu.Lock()
|
|
|
|
o = f.o
|
|
|
|
writers := f.writers
|
|
|
|
f.mu.Unlock()
|
|
|
|
if o != nil {
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
if writers == 0 {
|
|
|
|
return nil, errors.New("can't open file - writer failed")
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
return nil, fuse.ENOENT
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeOpener = (*File)(nil)
|
|
|
|
|
|
|
|
// Open the file for read or write
|
2017-03-02 23:07:01 +01:00
|
|
|
func (f *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fh fusefs.Handle, err error) {
|
2016-07-18 00:03:23 +02:00
|
|
|
// if o is nil it isn't valid yet
|
|
|
|
o, err := f.waitForValidObject()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-02 23:07:01 +01:00
|
|
|
fs.Debugf(o, "File.Open %v", req.Flags)
|
2016-07-18 00:03:23 +02:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case req.Flags.IsReadOnly():
|
2016-10-06 14:37:45 +02:00
|
|
|
if noSeek {
|
|
|
|
resp.Flags |= fuse.OpenNonSeekable
|
|
|
|
}
|
2017-03-02 23:07:01 +01:00
|
|
|
fh, err = newReadFileHandle(o)
|
|
|
|
err = errors.Wrap(err, "open for read")
|
2016-11-28 18:56:54 +01:00
|
|
|
case req.Flags.IsWriteOnly() || (req.Flags.IsReadWrite() && (req.Flags&fuse.OpenTruncate) != 0):
|
2016-09-10 23:25:26 +02:00
|
|
|
resp.Flags |= fuse.OpenNonSeekable
|
2016-07-18 00:03:23 +02:00
|
|
|
src := newCreateInfo(f.d.f, o.Remote())
|
2017-03-02 23:07:01 +01:00
|
|
|
fh, err = newWriteFileHandle(f.d, f, src)
|
|
|
|
err = errors.Wrap(err, "open for write")
|
2016-07-18 00:03:23 +02:00
|
|
|
case req.Flags.IsReadWrite():
|
2017-03-02 23:07:01 +01:00
|
|
|
err = errors.New("can't open for read and write simultaneously")
|
|
|
|
default:
|
|
|
|
err = errors.Errorf("can't figure out how to open with flags %v", req.Flags)
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
// File was opened in append-only mode, all writes will go to end
|
|
|
|
// of file. OS X does not provide this information.
|
|
|
|
OpenAppend OpenFlags = syscall.O_APPEND
|
|
|
|
OpenCreate OpenFlags = syscall.O_CREAT
|
|
|
|
OpenDirectory OpenFlags = syscall.O_DIRECTORY
|
|
|
|
OpenExclusive OpenFlags = syscall.O_EXCL
|
|
|
|
OpenNonblock OpenFlags = syscall.O_NONBLOCK
|
|
|
|
OpenSync OpenFlags = syscall.O_SYNC
|
|
|
|
OpenTruncate OpenFlags = syscall.O_TRUNC
|
|
|
|
*/
|
2017-03-02 23:07:01 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(o, "File.Open failed: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fh, nil
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
2017-01-29 12:29:42 +01:00
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeFsyncer = (*File)(nil)
|
|
|
|
|
|
|
|
// Fsync the file
|
|
|
|
//
|
|
|
|
// Note that we don't do anything except return OK
|
|
|
|
func (f *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
|
|
|
|
return nil
|
|
|
|
}
|