2017-11-06 22:38:52 +01:00
|
|
|
package vfs
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2018-02-18 14:12:26 +01:00
|
|
|
"fmt"
|
2020-03-19 16:54:53 +01:00
|
|
|
"io"
|
2018-02-26 20:37:58 +01:00
|
|
|
"io/ioutil"
|
2017-11-06 22:38:52 +01:00
|
|
|
"os"
|
2017-11-27 20:51:35 +01:00
|
|
|
"runtime"
|
2017-11-06 22:38:52 +01:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/log"
|
|
|
|
"github.com/rclone/rclone/lib/file"
|
2017-11-06 22:38:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// RWFileHandle is a handle that can be open for read and write.
|
|
|
|
//
|
|
|
|
// It will be open to a temporary file which, when closed, will be
|
|
|
|
// transferred to the remote.
|
|
|
|
type RWFileHandle struct {
|
|
|
|
mu sync.Mutex
|
2020-03-19 16:54:53 +01:00
|
|
|
fd *os.File
|
|
|
|
offset int64 // file pointer offset
|
2017-11-06 22:38:52 +01:00
|
|
|
file *File
|
|
|
|
d *Dir
|
2019-12-09 15:25:54 +01:00
|
|
|
flags int // open flags
|
2020-03-19 16:54:53 +01:00
|
|
|
closed bool // set if handle has been closed
|
|
|
|
opened bool
|
2019-12-09 15:25:54 +01:00
|
|
|
writeCalled bool // if any Write() methods have been called
|
|
|
|
changed bool // file contents was changed in any other way
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
|
2019-12-09 15:25:54 +01:00
|
|
|
func newRWFileHandle(d *Dir, f *File, flags int) (fh *RWFileHandle, err error) {
|
2017-11-27 20:51:35 +01:00
|
|
|
// if O_CREATE and O_EXCL are set and if path already exists, then return EEXIST
|
|
|
|
if flags&(os.O_CREATE|os.O_EXCL) == os.O_CREATE|os.O_EXCL && f.exists() {
|
|
|
|
return nil, EEXIST
|
|
|
|
}
|
|
|
|
|
2017-11-06 22:38:52 +01:00
|
|
|
fh = &RWFileHandle{
|
2019-12-09 15:25:54 +01:00
|
|
|
file: f,
|
|
|
|
d: d,
|
|
|
|
flags: flags,
|
2017-12-15 16:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// mark the file as open in the cache - must be done before the mkdir
|
2020-02-28 15:44:15 +01:00
|
|
|
fh.d.VFS().cache.Open(fh.file.Path())
|
2017-12-15 16:42:49 +01:00
|
|
|
|
|
|
|
// Make a place for the file
|
2020-02-28 15:44:15 +01:00
|
|
|
_, err = d.VFS().cache.Mkdir(fh.file.Path())
|
2017-12-15 16:42:49 +01:00
|
|
|
if err != nil {
|
2020-02-28 15:44:15 +01:00
|
|
|
fh.d.VFS().cache.Close(fh.file.Path())
|
2017-12-15 16:42:49 +01:00
|
|
|
return nil, errors.Wrap(err, "open RW handle failed to make cache directory")
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
2017-11-16 10:31:33 +01:00
|
|
|
|
|
|
|
rdwrMode := fh.flags & accessModeMask
|
|
|
|
if rdwrMode != os.O_RDONLY {
|
|
|
|
fh.file.addWriter(fh)
|
|
|
|
}
|
|
|
|
|
2018-02-18 14:12:26 +01:00
|
|
|
// truncate or create files immediately to prepare the cache
|
|
|
|
if fh.flags&os.O_TRUNC != 0 || fh.flags&(os.O_CREATE) != 0 && !f.exists() {
|
|
|
|
if err := fh.openPending(false); err != nil {
|
|
|
|
fh.file.delWriter(fh, false)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 22:38:52 +01:00
|
|
|
return fh, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// openPending opens the file if there is a pending open
|
|
|
|
//
|
|
|
|
// call with the lock held
|
|
|
|
func (fh *RWFileHandle) openPending(truncate bool) (err error) {
|
|
|
|
if fh.opened {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:36:48 +01:00
|
|
|
fh.file.muRW.Lock()
|
|
|
|
defer fh.file.muRW.Unlock()
|
2017-11-27 20:51:35 +01:00
|
|
|
|
2018-03-01 16:50:23 +01:00
|
|
|
o := fh.file.getObject()
|
|
|
|
|
2018-02-18 14:12:26 +01:00
|
|
|
var fd *os.File
|
|
|
|
cacheFileOpenFlags := fh.flags
|
2017-11-06 22:38:52 +01:00
|
|
|
// if not truncating the file, need to read it first
|
|
|
|
if fh.flags&os.O_TRUNC == 0 && !truncate {
|
2018-02-24 12:22:26 +01:00
|
|
|
// If the remote object exists AND its cached file exists locally AND there are no
|
2018-03-02 17:01:13 +01:00
|
|
|
// other RW handles with it open, then attempt to update it.
|
|
|
|
if o != nil && fh.file.rwOpens() == 0 {
|
2020-02-28 15:44:15 +01:00
|
|
|
err = fh.d.VFS().cache.Check(context.TODO(), o, fh.file.Path())
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "open RW handle failed to check cache file")
|
2018-02-24 12:22:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:39:20 +02:00
|
|
|
// try to open an existing cache file
|
2019-12-09 15:25:54 +01:00
|
|
|
fd, err = file.OpenFile(fh.file.osPath(), cacheFileOpenFlags&^os.O_CREATE, 0600)
|
2018-02-18 14:12:26 +01:00
|
|
|
if os.IsNotExist(err) {
|
2018-02-24 12:08:09 +01:00
|
|
|
// cache file does not exist, so need to fetch it if we have an object to fetch
|
|
|
|
// it from
|
2018-03-01 16:50:23 +01:00
|
|
|
if o != nil {
|
2020-02-28 15:44:15 +01:00
|
|
|
err = fh.d.VFS().cache.Fetch(context.TODO(), o, fh.file.Path())
|
2018-02-24 12:08:09 +01:00
|
|
|
if err != nil {
|
|
|
|
cause := errors.Cause(err)
|
|
|
|
if cause != fs.ErrorObjectNotFound && cause != fs.ErrorDirNotFound {
|
|
|
|
// return any non NotFound errors
|
|
|
|
return errors.Wrap(err, "open RW handle failed to cache file")
|
2018-02-18 14:12:26 +01:00
|
|
|
}
|
2018-02-24 12:08:09 +01:00
|
|
|
// continue here with err=fs.Error{Object,Dir}NotFound
|
2018-02-18 14:12:26 +01:00
|
|
|
}
|
2018-02-24 12:08:09 +01:00
|
|
|
}
|
|
|
|
// if err == nil, then we have cached the file successfully, otherwise err is
|
|
|
|
// indicating some kind of non existent file/directory either
|
|
|
|
// os.IsNotExist(err) or fs.Error{Object,Dir}NotFound
|
|
|
|
if err != nil {
|
|
|
|
if fh.flags&os.O_CREATE != 0 {
|
|
|
|
// if the object wasn't found AND O_CREATE is set then
|
|
|
|
// ignore error as we are about to create the file
|
2018-02-18 14:12:26 +01:00
|
|
|
fh.file.setSize(0)
|
2018-02-26 22:26:32 +01:00
|
|
|
fh.changed = true
|
2018-02-18 14:12:26 +01:00
|
|
|
} else {
|
|
|
|
return errors.Wrap(err, "open RW handle failed to cache file")
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-18 14:12:26 +01:00
|
|
|
} else if err != nil {
|
|
|
|
return errors.Wrap(err, "cache open file failed")
|
|
|
|
} else {
|
|
|
|
fs.Debugf(fh.logPrefix(), "Opened existing cached copy with flags=%s", decodeOpenFlags(fh.flags))
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-11-27 20:51:35 +01:00
|
|
|
// Set the size to 0 since we are truncating and flag we need to write it back
|
2017-11-06 22:38:52 +01:00
|
|
|
fh.file.setSize(0)
|
2018-02-26 22:26:32 +01:00
|
|
|
fh.changed = true
|
2018-02-26 20:37:58 +01:00
|
|
|
if fh.flags&os.O_CREATE == 0 && fh.file.exists() {
|
|
|
|
// create an empty file if it exists on the source
|
2019-12-09 15:25:54 +01:00
|
|
|
err = ioutil.WriteFile(fh.file.osPath(), []byte{}, 0600)
|
2018-02-26 20:37:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cache open failed to create zero length file")
|
|
|
|
}
|
2017-11-27 20:51:35 +01:00
|
|
|
}
|
|
|
|
// Windows doesn't seem to deal well with O_TRUNC and
|
2020-05-20 12:39:20 +02:00
|
|
|
// certain access modes so truncate the file if it
|
2017-11-27 20:51:35 +01:00
|
|
|
// exists in these cases.
|
2018-02-26 20:46:38 +01:00
|
|
|
if runtime.GOOS == "windows" && fh.flags&os.O_APPEND != 0 {
|
2017-11-27 20:51:35 +01:00
|
|
|
cacheFileOpenFlags &^= os.O_TRUNC
|
2019-12-09 15:25:54 +01:00
|
|
|
_, err = os.Stat(fh.file.osPath())
|
2017-11-27 20:51:35 +01:00
|
|
|
if err == nil {
|
2019-12-09 15:25:54 +01:00
|
|
|
err = os.Truncate(fh.file.osPath(), 0)
|
2017-11-27 20:51:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cache open failed to truncate")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 14:12:26 +01:00
|
|
|
if fd == nil {
|
|
|
|
fs.Debugf(fh.logPrefix(), "Opening cached copy with flags=%s", decodeOpenFlags(fh.flags))
|
2019-12-09 15:25:54 +01:00
|
|
|
fd, err = file.OpenFile(fh.file.osPath(), cacheFileOpenFlags, 0600)
|
2018-02-18 14:12:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cache open file failed")
|
|
|
|
}
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
2020-03-19 15:55:07 +01:00
|
|
|
fh.fd = fd
|
2017-11-06 22:38:52 +01:00
|
|
|
fh.opened = true
|
2018-03-02 16:58:41 +01:00
|
|
|
fh.file.addRWOpen()
|
2017-11-18 12:53:22 +01:00
|
|
|
fh.d.addObject(fh.file) // make sure the directory has this object in it now
|
2017-11-06 22:38:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String converts it to printable
|
|
|
|
func (fh *RWFileHandle) String() string {
|
|
|
|
if fh == nil {
|
|
|
|
return "<nil *RWFileHandle>"
|
|
|
|
}
|
|
|
|
if fh.file == nil {
|
|
|
|
return "<nil *RWFileHandle.file>"
|
|
|
|
}
|
|
|
|
return fh.file.String() + " (rw)"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Node returns the Node assocuated with this - satisfies Noder interface
|
|
|
|
func (fh *RWFileHandle) Node() Node {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
return fh.file
|
|
|
|
}
|
|
|
|
|
2018-02-26 22:26:32 +01:00
|
|
|
// Returns whether the file needs to be written back.
|
|
|
|
//
|
|
|
|
// If write hasn't been called and the file hasn't been changed in any other
|
|
|
|
// way we haven't modified it so we don't need to transfer it
|
|
|
|
//
|
|
|
|
// Must be called with fh.mu held
|
|
|
|
func (fh *RWFileHandle) modified() bool {
|
|
|
|
if !fh.writeCalled && !fh.changed {
|
|
|
|
fs.Debugf(fh.logPrefix(), "not modified so not transferring")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-10-06 22:05:21 +02:00
|
|
|
// flushWrites flushes any pending writes to cloud storage
|
2017-11-06 22:38:52 +01:00
|
|
|
//
|
2019-10-06 22:05:21 +02:00
|
|
|
// Must be called with fh.muRW held
|
|
|
|
func (fh *RWFileHandle) flushWrites(closeFile bool) error {
|
|
|
|
if fh.closed && !closeFile {
|
|
|
|
return nil
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
2019-10-06 22:05:21 +02:00
|
|
|
|
2017-11-06 22:38:52 +01:00
|
|
|
rdwrMode := fh.flags & accessModeMask
|
2018-02-18 14:12:26 +01:00
|
|
|
writer := rdwrMode != os.O_RDONLY
|
|
|
|
|
|
|
|
// If read only then return
|
|
|
|
if !fh.opened && rdwrMode == os.O_RDONLY {
|
|
|
|
return nil
|
2017-11-16 10:31:33 +01:00
|
|
|
}
|
2018-02-18 14:12:26 +01:00
|
|
|
|
2018-08-04 12:16:43 +02:00
|
|
|
isCopied := false
|
2018-02-18 14:12:26 +01:00
|
|
|
if writer {
|
2018-08-04 12:16:43 +02:00
|
|
|
isCopied = fh.file.delWriter(fh, fh.modified())
|
2018-02-18 14:12:26 +01:00
|
|
|
defer fh.file.finishWriterClose()
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we aren't creating or truncating the file then
|
|
|
|
// we haven't modified it so don't need to transfer it
|
|
|
|
if fh.flags&(os.O_CREATE|os.O_TRUNC) != 0 {
|
2017-11-06 22:38:52 +01:00
|
|
|
if err := fh.openPending(false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-02-18 14:12:26 +01:00
|
|
|
|
|
|
|
if writer && fh.opened {
|
2020-03-19 15:55:07 +01:00
|
|
|
fi, err := fh.fd.Stat()
|
2017-11-06 22:38:52 +01:00
|
|
|
if err != nil {
|
2018-02-18 14:12:26 +01:00
|
|
|
fs.Errorf(fh.logPrefix(), "Failed to stat cache file: %v", err)
|
2017-11-06 22:38:52 +01:00
|
|
|
} else {
|
|
|
|
fh.file.setSize(fi.Size())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the underlying file
|
2019-10-06 22:05:21 +02:00
|
|
|
if fh.opened && closeFile {
|
2020-03-19 15:55:07 +01:00
|
|
|
err := fh.fd.Close()
|
2018-02-18 14:12:26 +01:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "failed to close cache file")
|
|
|
|
return err
|
|
|
|
}
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
|
2018-08-04 12:16:43 +02:00
|
|
|
if isCopied {
|
2020-02-28 15:44:15 +01:00
|
|
|
o, err := fh.d.VFS().cache.Store(context.TODO(), fh.file.getObject(), fh.file.Path())
|
2018-02-18 14:12:26 +01:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(fh.logPrefix(), "%v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fh.file.setObject(o)
|
|
|
|
fs.Debugf(o, "transferred to remote")
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-06 22:05:21 +02:00
|
|
|
// close the file handle returning EBADF if it has been
|
|
|
|
// closed already.
|
|
|
|
//
|
|
|
|
// Must be called with fh.mu held
|
|
|
|
//
|
|
|
|
// Note that we leave the file around in the cache on error conditions
|
|
|
|
// to give the user a chance to recover it.
|
|
|
|
func (fh *RWFileHandle) close() (err error) {
|
|
|
|
defer log.Trace(fh.logPrefix(), "")("err=%v", &err)
|
|
|
|
fh.file.muRW.Lock()
|
|
|
|
defer fh.file.muRW.Unlock()
|
|
|
|
|
|
|
|
if fh.closed {
|
|
|
|
return ECLOSED
|
|
|
|
}
|
|
|
|
fh.closed = true
|
|
|
|
defer func() {
|
|
|
|
if fh.opened {
|
|
|
|
fh.file.delRWOpen()
|
|
|
|
}
|
2020-02-28 15:44:15 +01:00
|
|
|
fh.d.VFS().cache.Close(fh.file.Path())
|
2019-10-06 22:05:21 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
return fh.flushWrites(true)
|
|
|
|
}
|
|
|
|
|
2017-11-06 22:38:52 +01:00
|
|
|
// Close closes the file
|
|
|
|
func (fh *RWFileHandle) Close() error {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
return fh.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush is called each time the file or directory is closed.
|
|
|
|
// Because there can be multiple file descriptors referring to a
|
|
|
|
// single opened file, Flush can be called multiple times.
|
|
|
|
func (fh *RWFileHandle) Flush() error {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
if !fh.opened {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fh.closed {
|
2018-02-18 14:12:26 +01:00
|
|
|
fs.Debugf(fh.logPrefix(), "RWFileHandle.Flush nothing to do")
|
2017-11-06 22:38:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
2018-02-18 14:12:26 +01:00
|
|
|
// fs.Debugf(fh.logPrefix(), "RWFileHandle.Flush")
|
2017-11-06 22:38:52 +01:00
|
|
|
if !fh.opened {
|
2018-02-18 14:12:26 +01:00
|
|
|
fs.Debugf(fh.logPrefix(), "RWFileHandle.Flush ignoring flush on unopened handle")
|
2017-11-06 22:38:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If Write hasn't been called then ignore the Flush - Release
|
|
|
|
// will pick it up
|
|
|
|
if !fh.writeCalled {
|
2018-02-18 14:12:26 +01:00
|
|
|
fs.Debugf(fh.logPrefix(), "RWFileHandle.Flush ignoring flush on unwritten handle")
|
2017-11-06 22:38:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
2019-10-06 22:05:21 +02:00
|
|
|
|
|
|
|
fh.file.muRW.Lock()
|
|
|
|
defer fh.file.muRW.Unlock()
|
|
|
|
err := fh.flushWrites(false)
|
|
|
|
|
2017-11-06 22:38:52 +01:00
|
|
|
if err != nil {
|
2018-02-18 14:12:26 +01:00
|
|
|
fs.Errorf(fh.logPrefix(), "RWFileHandle.Flush error: %v", err)
|
2017-11-06 22:38:52 +01:00
|
|
|
} else {
|
2018-02-18 14:12:26 +01:00
|
|
|
// fs.Debugf(fh.logPrefix(), "RWFileHandle.Flush OK")
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release is called when we are finished with the file handle
|
|
|
|
//
|
|
|
|
// It isn't called directly from userspace so the error is ignored by
|
|
|
|
// the kernel
|
|
|
|
func (fh *RWFileHandle) Release() error {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
if fh.closed {
|
2018-02-18 14:12:26 +01:00
|
|
|
fs.Debugf(fh.logPrefix(), "RWFileHandle.Release nothing to do")
|
2017-11-06 22:38:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
2018-02-18 14:12:26 +01:00
|
|
|
fs.Debugf(fh.logPrefix(), "RWFileHandle.Release closing")
|
2017-11-06 22:38:52 +01:00
|
|
|
err := fh.close()
|
|
|
|
if err != nil {
|
2018-02-18 14:12:26 +01:00
|
|
|
fs.Errorf(fh.logPrefix(), "RWFileHandle.Release error: %v", err)
|
2017-11-06 22:38:52 +01:00
|
|
|
} else {
|
2018-02-18 14:12:26 +01:00
|
|
|
// fs.Debugf(fh.logPrefix(), "RWFileHandle.Release OK")
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:54:53 +01:00
|
|
|
// _size returns the size of the underlying file
|
|
|
|
//
|
|
|
|
// call with the lock held
|
|
|
|
//
|
|
|
|
// FIXME what if a file was partially read in - this may return the wrong thing?
|
|
|
|
// FIXME need to make sure we extend the file to the maximum when creating it
|
|
|
|
func (fh *RWFileHandle) _size() int64 {
|
2017-11-06 22:38:52 +01:00
|
|
|
if !fh.opened {
|
|
|
|
return fh.file.Size()
|
|
|
|
}
|
2020-03-19 15:55:07 +01:00
|
|
|
fi, err := fh.fd.Stat()
|
2017-11-06 22:38:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return fi.Size()
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:54:53 +01:00
|
|
|
// Size returns the size of the underlying file
|
|
|
|
func (fh *RWFileHandle) Size() int64 {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
return fh._size()
|
|
|
|
}
|
|
|
|
|
2017-11-06 22:38:52 +01:00
|
|
|
// Stat returns info about the file
|
|
|
|
func (fh *RWFileHandle) Stat() (os.FileInfo, error) {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
return fh.file, nil
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:54:53 +01:00
|
|
|
// _readAt bytes from the file at off
|
|
|
|
//
|
|
|
|
// call with lock held
|
|
|
|
func (fh *RWFileHandle) _readAt(b []byte, off int64) (n int, err error) {
|
2017-11-06 22:38:52 +01:00
|
|
|
if fh.closed {
|
2020-03-19 16:54:53 +01:00
|
|
|
return n, ECLOSED
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
2017-11-27 20:51:35 +01:00
|
|
|
if fh.flags&accessModeMask == os.O_WRONLY {
|
2020-03-19 16:54:53 +01:00
|
|
|
return n, EBADF
|
2017-11-27 20:51:35 +01:00
|
|
|
}
|
2017-11-06 22:38:52 +01:00
|
|
|
if err = fh.openPending(false); err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
2020-03-19 16:54:53 +01:00
|
|
|
return fh.fd.ReadAt(b, off)
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadAt bytes from the file at off
|
|
|
|
func (fh *RWFileHandle) ReadAt(b []byte, off int64) (n int, err error) {
|
2020-03-19 16:54:53 +01:00
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
return fh._readAt(b, off)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read bytes from the file
|
|
|
|
func (fh *RWFileHandle) Read(b []byte) (n int, err error) {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
n, err = fh._readAt(b, fh.offset)
|
|
|
|
fh.offset += int64(n)
|
|
|
|
return n, err
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Seek to new file position
|
|
|
|
func (fh *RWFileHandle) Seek(offset int64, whence int) (ret int64, err error) {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
if fh.closed {
|
|
|
|
return 0, ECLOSED
|
|
|
|
}
|
2018-02-22 18:28:21 +01:00
|
|
|
if !fh.opened && offset == 0 && whence != 2 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2017-11-06 22:38:52 +01:00
|
|
|
if err = fh.openPending(false); err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
2020-03-19 16:54:53 +01:00
|
|
|
switch whence {
|
|
|
|
case io.SeekStart:
|
|
|
|
fh.offset = 0
|
|
|
|
case io.SeekEnd:
|
|
|
|
fh.offset = fh._size()
|
|
|
|
}
|
|
|
|
fh.offset += offset
|
|
|
|
// we don't check the offset - the next Read will
|
|
|
|
return fh.offset, nil
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 16:54:53 +01:00
|
|
|
// WriteAt bytes to the file at off
|
|
|
|
func (fh *RWFileHandle) _writeAt(b []byte, off int64) (n int, err error) {
|
2017-11-06 22:38:52 +01:00
|
|
|
if fh.closed {
|
2020-03-19 16:54:53 +01:00
|
|
|
return n, ECLOSED
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
2017-11-27 20:51:35 +01:00
|
|
|
if fh.flags&accessModeMask == os.O_RDONLY {
|
2020-03-19 16:54:53 +01:00
|
|
|
return n, EBADF
|
2017-11-27 20:51:35 +01:00
|
|
|
}
|
2017-11-06 22:38:52 +01:00
|
|
|
if err = fh.openPending(false); err != nil {
|
2020-03-19 16:54:53 +01:00
|
|
|
return n, err
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
fh.writeCalled = true
|
2020-03-19 16:54:53 +01:00
|
|
|
|
|
|
|
if fh.flags&os.O_APPEND != 0 {
|
|
|
|
// if append is set, call Write as WriteAt returns an error if append is set
|
|
|
|
n, err = fh.fd.Write(b)
|
|
|
|
} else {
|
|
|
|
n, err = fh.fd.WriteAt(b, off)
|
|
|
|
}
|
2017-11-06 22:38:52 +01:00
|
|
|
if err != nil {
|
2020-03-19 16:54:53 +01:00
|
|
|
return n, err
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
2020-03-19 16:54:53 +01:00
|
|
|
|
2020-03-19 15:55:07 +01:00
|
|
|
fi, err := fh.fd.Stat()
|
2017-11-06 22:38:52 +01:00
|
|
|
if err != nil {
|
2020-03-19 16:54:53 +01:00
|
|
|
return n, errors.Wrap(err, "failed to stat cache file")
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
fh.file.setSize(fi.Size())
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteAt bytes to the file at off
|
|
|
|
func (fh *RWFileHandle) WriteAt(b []byte, off int64) (n int, err error) {
|
2020-03-19 16:54:53 +01:00
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
return fh._writeAt(b, off)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write bytes to the file
|
|
|
|
func (fh *RWFileHandle) Write(b []byte) (n int, err error) {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
n, err = fh._writeAt(b, fh.offset)
|
|
|
|
fh.offset += int64(n)
|
2017-11-06 22:38:52 +01:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteString a string to the file
|
|
|
|
func (fh *RWFileHandle) WriteString(s string) (n int, err error) {
|
2020-03-19 16:54:53 +01:00
|
|
|
return fh.Write([]byte(s))
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate file to given size
|
|
|
|
func (fh *RWFileHandle) Truncate(size int64) (err error) {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
if fh.closed {
|
|
|
|
return ECLOSED
|
|
|
|
}
|
|
|
|
if err = fh.openPending(size == 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-26 22:26:32 +01:00
|
|
|
fh.changed = true
|
2017-11-06 22:38:52 +01:00
|
|
|
fh.file.setSize(size)
|
2020-03-19 15:55:07 +01:00
|
|
|
return fh.fd.Truncate(size)
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
2017-11-18 16:48:49 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2017-11-27 20:51:35 +01:00
|
|
|
if fh.flags&accessModeMask == os.O_RDONLY {
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-19 15:55:07 +01:00
|
|
|
return fh.fd.Sync()
|
2017-11-18 16:48:49 +01:00
|
|
|
}
|
2018-02-18 14:12:26 +01:00
|
|
|
|
|
|
|
func (fh *RWFileHandle) logPrefix() string {
|
2019-12-09 15:25:54 +01:00
|
|
|
return fmt.Sprintf("%s(%p)", fh.file.Path(), fh)
|
2018-02-18 14:12:26 +01:00
|
|
|
}
|
2020-03-19 15:55:07 +01:00
|
|
|
|
|
|
|
// Chdir changes the current working directory to the file, which must
|
|
|
|
// be a directory.
|
|
|
|
func (fh *RWFileHandle) Chdir() error {
|
|
|
|
return ENOSYS
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chmod changes the mode of the file to mode.
|
|
|
|
func (fh *RWFileHandle) Chmod(mode os.FileMode) error {
|
|
|
|
return ENOSYS
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chown changes the numeric uid and gid of the named file.
|
|
|
|
func (fh *RWFileHandle) Chown(uid, gid int) error {
|
|
|
|
return ENOSYS
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fd returns the integer Unix file descriptor referencing the open file.
|
|
|
|
func (fh *RWFileHandle) Fd() uintptr {
|
|
|
|
return fh.fd.Fd()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the file from the underlying Object.
|
|
|
|
func (fh *RWFileHandle) Name() string {
|
|
|
|
return fh.file.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Readdir reads the contents of the directory associated with file.
|
|
|
|
func (fh *RWFileHandle) Readdir(n int) ([]os.FileInfo, error) {
|
|
|
|
return nil, ENOSYS
|
|
|
|
}
|
|
|
|
|
|
|
|
// Readdirnames reads the contents of the directory associated with file.
|
|
|
|
func (fh *RWFileHandle) Readdirnames(n int) (names []string, err error) {
|
|
|
|
return nil, ENOSYS
|
|
|
|
}
|