2017-11-06 22:38:52 +01:00
|
|
|
package vfs
|
|
|
|
|
|
|
|
import (
|
2018-02-18 14:12:26 +01:00
|
|
|
"fmt"
|
2020-03-19 16:54:53 +01:00
|
|
|
"io"
|
2017-11-06 22:38:52 +01:00
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/log"
|
2020-02-29 19:08:22 +01:00
|
|
|
"github.com/rclone/rclone/vfs/vfscache"
|
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 {
|
2020-02-29 19:08:22 +01:00
|
|
|
// read only variables
|
|
|
|
file *File
|
|
|
|
d *Dir
|
|
|
|
flags int // open flags
|
|
|
|
item *vfscache.Item // cached file item
|
|
|
|
|
|
|
|
// read write variables protected by mutex
|
2017-11-06 22:38:52 +01:00
|
|
|
mu sync.Mutex
|
2020-03-19 16:54:53 +01:00
|
|
|
offset int64 // file pointer offset
|
2020-02-29 19:08:22 +01:00
|
|
|
closed bool // set if handle has been closed
|
2020-03-19 16:54:53 +01:00
|
|
|
opened bool
|
2019-12-09 15:25:54 +01:00
|
|
|
writeCalled bool // if any Write() methods have been called
|
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) {
|
2020-02-29 19:08:22 +01:00
|
|
|
defer log.Trace(f.Path(), "")("err=%v", &err)
|
|
|
|
// get an item to represent this from the cache
|
|
|
|
item := d.vfs.cache.Item(f.Path())
|
|
|
|
|
2020-11-12 18:46:13 +01:00
|
|
|
exists := f.exists() || (item.Exists() && !item.WrittenBack())
|
2020-02-29 19:08:22 +01:00
|
|
|
|
2017-11-27 20:51:35 +01:00
|
|
|
// if O_CREATE and O_EXCL are set and if path already exists, then return EEXIST
|
2020-02-29 19:08:22 +01:00
|
|
|
if flags&(os.O_CREATE|os.O_EXCL) == os.O_CREATE|os.O_EXCL && exists {
|
2017-11-27 20:51:35 +01:00
|
|
|
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,
|
2020-02-29 19:08:22 +01:00
|
|
|
item: item,
|
2017-12-15 16:42:49 +01:00
|
|
|
}
|
|
|
|
|
2020-02-29 19:08:22 +01:00
|
|
|
// truncate immediately if O_TRUNC is set or O_CREATE is set and file doesn't exist
|
2020-04-17 12:18:58 +02:00
|
|
|
if !fh.readOnly() && (fh.flags&os.O_TRUNC != 0 || (fh.flags&os.O_CREATE != 0 && !exists)) {
|
2020-02-29 19:08:22 +01:00
|
|
|
err = fh.Truncate(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cache open with O_TRUNC: failed to truncate")
|
|
|
|
}
|
|
|
|
// we definitely need to write back the item even if we don't write to it
|
|
|
|
item.Dirty()
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
2017-11-16 10:31:33 +01:00
|
|
|
|
2020-02-29 19:08:22 +01:00
|
|
|
if !fh.readOnly() {
|
2017-11-16 10:31:33 +01:00
|
|
|
fh.file.addWriter(fh)
|
|
|
|
}
|
|
|
|
|
2017-11-06 22:38:52 +01:00
|
|
|
return fh, nil
|
|
|
|
}
|
|
|
|
|
2020-02-29 19:08:22 +01:00
|
|
|
// readOnly returns whether flags say fh is read only
|
|
|
|
func (fh *RWFileHandle) readOnly() bool {
|
|
|
|
return (fh.flags & accessModeMask) == os.O_RDONLY
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeOnly returns whether flags say fh is write only
|
|
|
|
func (fh *RWFileHandle) writeOnly() bool {
|
|
|
|
return (fh.flags & accessModeMask) == os.O_WRONLY
|
|
|
|
}
|
|
|
|
|
2017-11-06 22:38:52 +01:00
|
|
|
// openPending opens the file if there is a pending open
|
|
|
|
//
|
|
|
|
// call with the lock held
|
2020-02-29 19:08:22 +01:00
|
|
|
func (fh *RWFileHandle) openPending() (err error) {
|
2017-11-06 22:38:52 +01:00
|
|
|
if fh.opened {
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-29 19:08:22 +01:00
|
|
|
defer log.Trace(fh.logPrefix(), "")("err=%v", &err)
|
2017-11-06 22:38:52 +01:00
|
|
|
|
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()
|
2020-02-29 19:08:22 +01:00
|
|
|
err = fh.item.Open(o)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "open RW handle failed to open cache file")
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
|
2020-02-29 19:08:22 +01:00
|
|
|
size := fh._size() // update size in file and read size
|
|
|
|
if fh.flags&os.O_APPEND != 0 {
|
|
|
|
fh.offset = size
|
|
|
|
fs.Debugf(fh.logPrefix(), "open at offset %d", fh.offset)
|
|
|
|
} else {
|
|
|
|
fh.offset = 0
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
fh.opened = true
|
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)"
|
|
|
|
}
|
|
|
|
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 02:17:24 +02:00
|
|
|
// Node returns the Node associated with this - satisfies Noder interface
|
2017-11-06 22:38:52 +01:00
|
|
|
func (fh *RWFileHandle) Node() Node {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
return fh.file
|
|
|
|
}
|
|
|
|
|
2020-02-29 19:08:22 +01:00
|
|
|
// updateSize updates the size of the file if necessary
|
2018-02-26 22:26:32 +01:00
|
|
|
//
|
|
|
|
// Must be called with fh.mu held
|
2020-02-29 19:08:22 +01:00
|
|
|
func (fh *RWFileHandle) updateSize() {
|
|
|
|
// If read only or not opened then ignore
|
|
|
|
if fh.readOnly() || !fh.opened {
|
|
|
|
return
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
2020-02-29 19:08:22 +01:00
|
|
|
size := fh._size()
|
|
|
|
fh.file.setSize(size)
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2020-02-29 19:08:22 +01:00
|
|
|
|
2019-10-06 22:05:21 +02:00
|
|
|
fh.closed = true
|
2020-02-29 19:08:22 +01:00
|
|
|
fh.updateSize()
|
|
|
|
if fh.opened {
|
|
|
|
err = fh.item.Close(fh.file.setObject)
|
|
|
|
fh.opened = false
|
|
|
|
}
|
2019-10-06 22:05:21 +02:00
|
|
|
|
2020-02-29 19:08:22 +01:00
|
|
|
if !fh.readOnly() {
|
|
|
|
fh.file.delWriter(fh)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2019-10-06 22:05:21 +02:00
|
|
|
}
|
|
|
|
|
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()
|
2020-02-29 19:08:22 +01:00
|
|
|
fs.Debugf(fh.logPrefix(), "RWFileHandle.Flush")
|
|
|
|
fh.updateSize()
|
|
|
|
fh.mu.Unlock()
|
|
|
|
return nil
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2020-02-29 19:08:22 +01:00
|
|
|
fs.Debugf(fh.logPrefix(), "RWFileHandle.Release")
|
2017-11-06 22:38:52 +01:00
|
|
|
if fh.closed {
|
2020-02-29 19:08:22 +01:00
|
|
|
// Don't return an error if called twice
|
2017-11-06 22:38:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-29 19:08:22 +01:00
|
|
|
// _size returns the size of the underlying file and also sets it in
|
|
|
|
// the owning file
|
2020-03-19 16:54:53 +01:00
|
|
|
//
|
|
|
|
// call with the lock held
|
|
|
|
func (fh *RWFileHandle) _size() int64 {
|
2020-02-29 19:08:22 +01:00
|
|
|
size, err := fh.item.GetSize()
|
2017-11-06 22:38:52 +01:00
|
|
|
if err != nil {
|
2020-02-29 19:08:22 +01:00
|
|
|
o := fh.file.getObject()
|
|
|
|
if o != nil {
|
|
|
|
size = o.Size()
|
|
|
|
} else {
|
|
|
|
fs.Errorf(fh.logPrefix(), "Couldn't read size of file")
|
|
|
|
size = 0
|
|
|
|
}
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
2020-02-29 19:08:22 +01:00
|
|
|
fh.file.setSize(size)
|
|
|
|
return size
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
//
|
2020-06-16 19:32:34 +02:00
|
|
|
// if release is set then it releases the mutex just before doing the IO
|
|
|
|
//
|
2020-03-19 16:54:53 +01:00
|
|
|
// call with lock held
|
2020-06-16 19:32:34 +02:00
|
|
|
func (fh *RWFileHandle) _readAt(b []byte, off int64, release bool) (n int, err error) {
|
2020-02-29 19:08:22 +01:00
|
|
|
defer log.Trace(fh.logPrefix(), "size=%d, off=%d", len(b), off)("n=%d, err=%v", &n, &err)
|
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
|
|
|
}
|
2020-02-29 19:08:22 +01:00
|
|
|
if fh.writeOnly() {
|
2020-03-19 16:54:53 +01:00
|
|
|
return n, EBADF
|
2017-11-27 20:51:35 +01:00
|
|
|
}
|
2020-02-29 19:08:22 +01:00
|
|
|
if off >= fh._size() {
|
|
|
|
return n, io.EOF
|
|
|
|
}
|
|
|
|
if err = fh.openPending(); err != nil {
|
2017-11-06 22:38:52 +01:00
|
|
|
return n, err
|
|
|
|
}
|
2020-06-16 19:32:34 +02:00
|
|
|
if release {
|
|
|
|
// Do the writing with fh.mu unlocked
|
|
|
|
fh.mu.Unlock()
|
|
|
|
}
|
2020-08-25 17:20:29 +02:00
|
|
|
|
2020-06-16 19:32:34 +02:00
|
|
|
n, err = fh.item.ReadAt(b, off)
|
2020-08-25 17:20:29 +02:00
|
|
|
|
2020-06-16 19:32:34 +02:00
|
|
|
if release {
|
|
|
|
fh.mu.Lock()
|
|
|
|
}
|
|
|
|
return n, err
|
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()
|
2020-06-16 19:32:34 +02:00
|
|
|
return fh._readAt(b, off, true)
|
2020-03-19 16:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read bytes from the file
|
|
|
|
func (fh *RWFileHandle) Read(b []byte) (n int, err error) {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
2020-06-16 19:32:34 +02:00
|
|
|
n, err = fh._readAt(b, fh.offset, false)
|
2020-03-19 16:54:53 +01:00
|
|
|
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
|
|
|
|
}
|
2020-02-29 19:08:22 +01:00
|
|
|
if err = fh.openPending(); err != nil {
|
2017-11-06 22:38:52 +01:00
|
|
|
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-06-16 19:32:34 +02:00
|
|
|
// _writeAt bytes to the file at off
|
|
|
|
//
|
|
|
|
// if release is set then it releases the mutex just before doing the IO
|
|
|
|
//
|
|
|
|
// call with lock held
|
|
|
|
func (fh *RWFileHandle) _writeAt(b []byte, off int64, release bool) (n int, err error) {
|
2020-02-29 19:08:22 +01:00
|
|
|
defer log.Trace(fh.logPrefix(), "size=%d, off=%d", len(b), off)("n=%d, err=%v", &n, &err)
|
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
|
|
|
}
|
2020-02-29 19:08:22 +01:00
|
|
|
if fh.readOnly() {
|
2020-03-19 16:54:53 +01:00
|
|
|
return n, EBADF
|
2017-11-27 20:51:35 +01:00
|
|
|
}
|
2020-02-29 19:08:22 +01:00
|
|
|
if err = fh.openPending(); 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
|
|
|
if fh.flags&os.O_APPEND != 0 {
|
2020-02-29 19:08:22 +01:00
|
|
|
// From open(2): Before each write(2), the file offset is
|
|
|
|
// positioned at the end of the file, as if with lseek(2).
|
|
|
|
size := fh._size()
|
|
|
|
fh.offset = size
|
|
|
|
off = fh.offset
|
2020-03-19 16:54:53 +01:00
|
|
|
}
|
2020-02-29 19:08:22 +01:00
|
|
|
fh.writeCalled = true
|
2020-06-16 19:32:34 +02:00
|
|
|
if release {
|
|
|
|
// Do the writing with fh.mu unlocked
|
|
|
|
fh.mu.Unlock()
|
|
|
|
}
|
2020-02-29 19:08:22 +01:00
|
|
|
n, err = fh.item.WriteAt(b, off)
|
2020-06-16 19:32:34 +02:00
|
|
|
if release {
|
|
|
|
fh.mu.Lock()
|
|
|
|
}
|
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-02-29 19:08:22 +01:00
|
|
|
_ = fh._size()
|
2017-11-06 22:38:52 +01:00
|
|
|
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()
|
2020-06-16 19:32:34 +02:00
|
|
|
n, err = fh._writeAt(b, off, true)
|
2020-02-29 19:08:22 +01:00
|
|
|
if fh.flags&os.O_APPEND != 0 {
|
|
|
|
fh.offset += int64(n)
|
|
|
|
}
|
2020-06-16 19:32:34 +02:00
|
|
|
fh.mu.Unlock()
|
2020-02-29 19:08:22 +01:00
|
|
|
return n, err
|
2020-03-19 16:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write bytes to the file
|
|
|
|
func (fh *RWFileHandle) Write(b []byte) (n int, err error) {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
2020-06-16 19:32:34 +02:00
|
|
|
n, err = fh._writeAt(b, fh.offset, false)
|
2020-03-19 16:54:53 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-29 19:08:22 +01:00
|
|
|
// Truncate file to given size
|
|
|
|
//
|
|
|
|
// Call with mutex held
|
|
|
|
func (fh *RWFileHandle) _truncate(size int64) (err error) {
|
|
|
|
if size == fh._size() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fh.file.setSize(size)
|
|
|
|
return fh.item.Truncate(size)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2020-02-29 19:08:22 +01:00
|
|
|
if err = fh.openPending(); err != nil {
|
2017-11-06 22:38:52 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-02-29 19:08:22 +01:00
|
|
|
return fh._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
|
|
|
|
}
|
2020-02-29 19:08:22 +01:00
|
|
|
if fh.readOnly() {
|
2017-11-27 20:51:35 +01:00
|
|
|
return nil
|
|
|
|
}
|
2020-02-29 19:08:22 +01:00
|
|
|
return fh.item.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 {
|
2020-02-29 19:08:22 +01:00
|
|
|
return 0xdeadbeef // FIXME
|
2020-03-19 15:55:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|