2017-10-29 03:01:34 +08:00
|
|
|
package vfs
|
2017-05-03 05:35:07 +08:00
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2017-05-03 05:35:07 +08:00
|
|
|
"io"
|
2017-10-30 05:11:17 +08:00
|
|
|
"os"
|
2017-05-03 05:35:07 +08:00
|
|
|
"sync"
|
2017-09-17 04:49:08 +08:00
|
|
|
"time"
|
2017-05-03 05:35:07 +08:00
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2017-05-03 05:35:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// WriteFileHandle is an open for write handle on a File
|
|
|
|
type WriteFileHandle struct {
|
2017-10-30 05:11:17 +08:00
|
|
|
baseHandle
|
2017-05-03 05:35:07 +08:00
|
|
|
mu sync.Mutex
|
2022-07-29 00:35:14 +08:00
|
|
|
cond sync.Cond // cond lock for out of sequence writes
|
2017-05-03 05:35:07 +08:00
|
|
|
remote string
|
|
|
|
pipeWriter *io.PipeWriter
|
|
|
|
o fs.Object
|
|
|
|
result chan error
|
|
|
|
file *File
|
|
|
|
offset int64
|
2017-11-21 02:01:42 +08:00
|
|
|
flags int
|
2022-07-29 01:14:04 +08:00
|
|
|
closed bool // set if handle has been closed
|
|
|
|
writeCalled bool // set the first time Write() is called
|
|
|
|
opened bool
|
2017-11-21 02:01:42 +08:00
|
|
|
truncated bool
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
|
2017-10-28 04:41:34 +08:00
|
|
|
// Check interfaces
|
|
|
|
var (
|
|
|
|
_ io.Writer = (*WriteFileHandle)(nil)
|
|
|
|
_ io.WriterAt = (*WriteFileHandle)(nil)
|
|
|
|
_ io.Closer = (*WriteFileHandle)(nil)
|
|
|
|
)
|
|
|
|
|
2017-11-21 02:01:42 +08:00
|
|
|
func newWriteFileHandle(d *Dir, f *File, remote string, flags int) (*WriteFileHandle, error) {
|
2017-05-03 05:35:07 +08:00
|
|
|
fh := &WriteFileHandle{
|
2017-11-06 20:25:54 +08:00
|
|
|
remote: remote,
|
2017-11-21 02:01:42 +08:00
|
|
|
flags: flags,
|
2017-05-03 05:35:07 +08:00
|
|
|
result: make(chan error, 1),
|
|
|
|
file: f,
|
|
|
|
}
|
2022-07-29 00:35:14 +08:00
|
|
|
fh.cond = sync.Cond{L: &fh.mu}
|
2017-11-21 02:01:42 +08:00
|
|
|
fh.file.addWriter(fh)
|
|
|
|
return fh, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns whether it is OK to truncate the file
|
|
|
|
func (fh *WriteFileHandle) safeToTruncate() bool {
|
|
|
|
return fh.truncated || fh.flags&os.O_TRUNC != 0 || !fh.file.exists()
|
|
|
|
}
|
|
|
|
|
|
|
|
// openPending opens the file if there is a pending open
|
|
|
|
//
|
|
|
|
// call with the lock held
|
|
|
|
func (fh *WriteFileHandle) openPending() (err error) {
|
|
|
|
if fh.opened {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !fh.safeToTruncate() {
|
2017-12-02 20:06:05 +08:00
|
|
|
fs.Errorf(fh.remote, "WriteFileHandle: Can't open for write without O_TRUNC on existing file without --vfs-cache-mode >= writes")
|
2017-11-21 02:01:42 +08:00
|
|
|
return EPERM
|
|
|
|
}
|
2017-06-26 18:58:12 +08:00
|
|
|
var pipeReader *io.PipeReader
|
|
|
|
pipeReader, fh.pipeWriter = io.Pipe()
|
2017-05-03 05:35:07 +08:00
|
|
|
go func() {
|
2020-10-14 06:07:12 +08:00
|
|
|
// NB Rcat deals with Stats.Transferring, etc.
|
2022-11-09 01:42:18 +08:00
|
|
|
o, err := operations.Rcat(context.TODO(), fh.file.Fs(), fh.remote, pipeReader, time.Now(), nil)
|
2017-06-26 18:58:12 +08:00
|
|
|
if err != nil {
|
2017-09-17 04:49:08 +08:00
|
|
|
fs.Errorf(fh.remote, "WriteFileHandle.New Rcat failed: %v", err)
|
2017-06-26 18:58:12 +08:00
|
|
|
}
|
2017-09-17 04:49:08 +08:00
|
|
|
// Close the pipeReader so the pipeWriter fails with ErrClosedPipe
|
|
|
|
_ = pipeReader.Close()
|
2017-05-03 05:35:07 +08:00
|
|
|
fh.o = o
|
|
|
|
fh.result <- err
|
|
|
|
}()
|
|
|
|
fh.file.setSize(0)
|
2017-11-21 02:01:42 +08:00
|
|
|
fh.truncated = true
|
2020-04-15 01:03:45 +08:00
|
|
|
fh.file.Dir().addObject(fh.file) // make sure the directory has this object in it now
|
2017-11-21 02:01:42 +08:00
|
|
|
fh.opened = true
|
|
|
|
return nil
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
|
2017-05-09 18:29:02 +08:00
|
|
|
// String converts it to printable
|
|
|
|
func (fh *WriteFileHandle) String() string {
|
|
|
|
if fh == nil {
|
|
|
|
return "<nil *WriteFileHandle>"
|
|
|
|
}
|
2017-11-04 00:11:44 +08:00
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
2017-05-09 18:29:02 +08:00
|
|
|
if fh.file == nil {
|
|
|
|
return "<nil *WriteFileHandle.file>"
|
|
|
|
}
|
|
|
|
return fh.file.String() + " (w)"
|
|
|
|
}
|
|
|
|
|
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 08:17:24 +08:00
|
|
|
// Node returns the Node associated with this - satisfies Noder interface
|
2017-05-03 05:35:07 +08:00
|
|
|
func (fh *WriteFileHandle) Node() Node {
|
2017-11-04 00:11:44 +08:00
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
2017-05-03 05:35:07 +08:00
|
|
|
return fh.file
|
|
|
|
}
|
|
|
|
|
2017-10-28 04:41:34 +08:00
|
|
|
// WriteAt writes len(p) bytes from p to the underlying data stream at offset
|
|
|
|
// off. It returns the number of bytes written from p (0 <= n <= len(p)) and
|
|
|
|
// any error encountered that caused the write to stop early. WriteAt must
|
|
|
|
// return a non-nil error if it returns n < len(p).
|
|
|
|
//
|
|
|
|
// If WriteAt is writing to a destination with a seek offset, WriteAt should
|
|
|
|
// not affect nor be affected by the underlying seek offset.
|
|
|
|
//
|
|
|
|
// Clients of WriteAt can execute parallel WriteAt calls on the same
|
|
|
|
// destination if the ranges do not overlap.
|
|
|
|
//
|
|
|
|
// Implementations must not retain p.
|
|
|
|
func (fh *WriteFileHandle) WriteAt(p []byte, off int64) (n int, err error) {
|
2017-05-03 05:35:07 +08:00
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
2017-11-04 00:11:44 +08:00
|
|
|
return fh.writeAt(p, off)
|
|
|
|
}
|
|
|
|
|
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 08:17:24 +08:00
|
|
|
// Implementation of WriteAt - call with lock held
|
2017-11-04 00:11:44 +08:00
|
|
|
func (fh *WriteFileHandle) writeAt(p []byte, off int64) (n int, err error) {
|
2019-05-10 20:22:55 +08:00
|
|
|
// defer log.Trace(fh.remote, "len=%d off=%d", len(p), off)("n=%d, fh.off=%d, err=%v", &n, &fh.offset, &err)
|
2017-05-03 05:35:07 +08:00
|
|
|
if fh.closed {
|
2017-11-21 02:01:42 +08:00
|
|
|
fs.Errorf(fh.remote, "WriteFileHandle.Write: error: %v", EBADF)
|
2017-11-03 19:35:36 +08:00
|
|
|
return 0, ECLOSED
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
2019-08-07 23:41:45 +08:00
|
|
|
if fh.offset != off {
|
2024-07-03 00:31:51 +08:00
|
|
|
waitSequential("write", fh.remote, &fh.cond, time.Duration(fh.file.VFS().Opt.WriteWait), &fh.offset, off)
|
2019-05-10 20:22:55 +08:00
|
|
|
}
|
2017-10-30 05:14:05 +08:00
|
|
|
if fh.offset != off {
|
2017-12-02 20:06:05 +08:00
|
|
|
fs.Errorf(fh.remote, "WriteFileHandle.Write: can't seek in file without --vfs-cache-mode >= writes")
|
2017-10-30 05:14:05 +08:00
|
|
|
return 0, ESPIPE
|
|
|
|
}
|
2017-11-21 02:01:42 +08:00
|
|
|
if err = fh.openPending(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
fh.writeCalled = true
|
2017-10-28 04:41:34 +08:00
|
|
|
n, err = fh.pipeWriter.Write(p)
|
|
|
|
fh.offset += int64(n)
|
2017-05-03 05:35:07 +08:00
|
|
|
fh.file.setSize(fh.offset)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(fh.remote, "WriteFileHandle.Write error: %v", err)
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(fh.remote, "WriteFileHandle.Write OK (%d bytes written)", n)
|
2019-08-07 23:41:45 +08:00
|
|
|
fh.cond.Broadcast() // wake everyone up waiting for an in-sequence read
|
2017-10-28 04:41:34 +08:00
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes len(p) bytes from p to the underlying data stream. It returns
|
|
|
|
// the number of bytes written from p (0 <= n <= len(p)) and any error
|
|
|
|
// encountered that caused the write to stop early. Write must return a non-nil
|
|
|
|
// error if it returns n < len(p). Write must not modify the slice data, even
|
|
|
|
// temporarily.
|
|
|
|
//
|
|
|
|
// Implementations must not retain p.
|
|
|
|
func (fh *WriteFileHandle) Write(p []byte) (n int, err error) {
|
2017-11-04 00:11:44 +08:00
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
2017-10-28 04:41:34 +08:00
|
|
|
// Since we can't seek, just call WriteAt with the current offset
|
2017-11-04 00:11:44 +08:00
|
|
|
return fh.writeAt(p, fh.offset)
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
|
2017-11-16 17:31:33 +08:00
|
|
|
// WriteString a string to the file
|
|
|
|
func (fh *WriteFileHandle) WriteString(s string) (n int, err error) {
|
|
|
|
return fh.Write([]byte(s))
|
|
|
|
}
|
|
|
|
|
2017-05-09 01:05:12 +08:00
|
|
|
// Offset returns the offset of the file pointer
|
2017-05-03 05:35:07 +08:00
|
|
|
func (fh *WriteFileHandle) Offset() (offset int64) {
|
2017-11-04 00:11:44 +08:00
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
2017-05-03 05:35:07 +08:00
|
|
|
return fh.offset
|
|
|
|
}
|
|
|
|
|
|
|
|
// close the file handle returning EBADF if it has been
|
|
|
|
// closed already.
|
|
|
|
//
|
|
|
|
// Must be called with fh.mu held
|
2017-11-21 02:01:42 +08:00
|
|
|
func (fh *WriteFileHandle) close() (err error) {
|
2017-05-03 05:35:07 +08:00
|
|
|
if fh.closed {
|
2017-11-03 19:35:36 +08:00
|
|
|
return ECLOSED
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
fh.closed = true
|
2017-11-18 19:55:39 +08:00
|
|
|
// leave writer open until file is transferred
|
2018-03-07 04:47:11 +08:00
|
|
|
defer func() {
|
2020-03-01 02:08:22 +08:00
|
|
|
fh.file.delWriter(fh)
|
2018-03-07 04:47:11 +08:00
|
|
|
}()
|
2020-05-20 18:39:20 +08:00
|
|
|
// If file not opened and not safe to truncate then leave file intact
|
2017-11-21 02:01:42 +08:00
|
|
|
if !fh.opened && !fh.safeToTruncate() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err = fh.openPending(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
writeCloseErr := fh.pipeWriter.Close()
|
2017-11-21 02:01:42 +08:00
|
|
|
err = <-fh.result
|
2017-05-03 05:35:07 +08:00
|
|
|
if err == nil {
|
|
|
|
fh.file.setObject(fh.o)
|
|
|
|
err = writeCloseErr
|
2024-05-31 21:18:56 +08:00
|
|
|
} else if fh.file.getObject() == nil {
|
2021-11-10 16:49:09 +08:00
|
|
|
// Remove vfs file entry when no object is present
|
2024-05-31 21:18:56 +08:00
|
|
|
_ = fh.file.Remove()
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-30 05:14:05 +08:00
|
|
|
// Close closes the file
|
|
|
|
func (fh *WriteFileHandle) Close() error {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
return fh.close()
|
|
|
|
}
|
|
|
|
|
2017-05-03 05:35:07 +08:00
|
|
|
// Flush is called on each close() of a file descriptor. So if a
|
|
|
|
// filesystem wants to return write errors in close() and the file has
|
|
|
|
// cached dirty data, this is a good place to write back data and
|
|
|
|
// return any errors. Since many applications ignore close() errors
|
|
|
|
// this is not always useful.
|
|
|
|
//
|
|
|
|
// NOTE: The flush() method may be called more than once for each
|
|
|
|
// open(). This happens if more than one file descriptor refers to an
|
|
|
|
// opened file due to dup(), dup2() or fork() calls. It is not
|
|
|
|
// possible to determine if a flush is final, so each flush should be
|
|
|
|
// treated equally. Multiple write-flush sequences are relatively
|
|
|
|
// rare, so this shouldn't be a problem.
|
|
|
|
//
|
|
|
|
// Filesystems shouldn't assume that flush will always be called after
|
|
|
|
// some writes, or that if will be called at all.
|
|
|
|
func (fh *WriteFileHandle) Flush() error {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
2017-10-30 05:14:05 +08:00
|
|
|
if fh.closed {
|
|
|
|
fs.Debugf(fh.remote, "WriteFileHandle.Flush nothing to do")
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(fh.remote, "WriteFileHandle.Flush")
|
2017-05-03 05:35:07 +08:00
|
|
|
// If Write hasn't been called then ignore the Flush - Release
|
|
|
|
// will pick it up
|
|
|
|
if !fh.writeCalled {
|
2018-01-20 18:10:55 +08:00
|
|
|
fs.Debugf(fh.remote, "WriteFileHandle.Flush unwritten handle, writing 0 bytes to avoid race conditions")
|
|
|
|
_, err := fh.writeAt([]byte{}, fh.offset)
|
|
|
|
return err
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
err := fh.close()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(fh.remote, "WriteFileHandle.Flush error: %v", err)
|
2022-06-09 04:25:17 +08:00
|
|
|
//} else {
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(fh.remote, "WriteFileHandle.Flush OK")
|
2017-05-03 05:35:07 +08: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 *WriteFileHandle) Release() error {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
if fh.closed {
|
|
|
|
fs.Debugf(fh.remote, "WriteFileHandle.Release nothing to do")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fs.Debugf(fh.remote, "WriteFileHandle.Release closing")
|
|
|
|
err := fh.close()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(fh.remote, "WriteFileHandle.Release error: %v", err)
|
2022-06-09 04:25:17 +08:00
|
|
|
//} else {
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(fh.remote, "WriteFileHandle.Release OK")
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2017-10-25 04:06:06 +08:00
|
|
|
|
2017-10-30 05:11:17 +08:00
|
|
|
// Stat returns info about the file
|
|
|
|
func (fh *WriteFileHandle) Stat() (os.FileInfo, error) {
|
2017-11-04 00:11:44 +08:00
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
2017-10-30 05:11:17 +08:00
|
|
|
return fh.file, nil
|
|
|
|
}
|
2017-11-16 17:31:33 +08:00
|
|
|
|
|
|
|
// Truncate file to given size
|
|
|
|
func (fh *WriteFileHandle) Truncate(size int64) (err error) {
|
2020-07-22 17:18:52 +08:00
|
|
|
// defer log.Trace(fh.remote, "size=%d", size)("err=%v", &err)
|
2017-11-16 17:31:33 +08:00
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
if size != fh.offset {
|
2018-01-25 17:51:43 +08:00
|
|
|
fs.Errorf(fh.remote, "WriteFileHandle: Truncate: Can't change size without --vfs-cache-mode >= writes")
|
2017-11-16 17:31:33 +08:00
|
|
|
return EPERM
|
|
|
|
}
|
|
|
|
// File is correct size
|
2017-11-21 02:01:42 +08:00
|
|
|
if size == 0 {
|
|
|
|
fh.truncated = true
|
|
|
|
}
|
2017-11-16 17:31:33 +08:00
|
|
|
return nil
|
|
|
|
}
|
2017-11-16 18:55:24 +08:00
|
|
|
|
|
|
|
// Read reads up to len(p) bytes into p.
|
|
|
|
func (fh *WriteFileHandle) Read(p []byte) (n int, err error) {
|
2018-01-25 17:51:43 +08:00
|
|
|
fs.Errorf(fh.remote, "WriteFileHandle: Read: Can't read and write to file without --vfs-cache-mode >= minimal")
|
2017-11-16 18:55:24 +08:00
|
|
|
return 0, EPERM
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadAt reads len(p) bytes into p starting at offset off in the
|
|
|
|
// underlying input source. It returns the number of bytes read (0 <=
|
|
|
|
// n <= len(p)) and any error encountered.
|
|
|
|
func (fh *WriteFileHandle) ReadAt(p []byte, off int64) (n int, err error) {
|
2018-01-25 17:51:43 +08:00
|
|
|
fs.Errorf(fh.remote, "WriteFileHandle: ReadAt: Can't read and write to file without --vfs-cache-mode >= minimal")
|
2017-11-16 18:55:24 +08:00
|
|
|
return 0, EPERM
|
|
|
|
}
|
2017-11-18 23:48:49 +08: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 *WriteFileHandle) Sync() error {
|
|
|
|
return nil
|
|
|
|
}
|
2023-08-31 21:28:10 +08:00
|
|
|
|
|
|
|
// Name returns the name of the file from the underlying Object.
|
|
|
|
func (fh *WriteFileHandle) Name() string {
|
|
|
|
return fh.file.String()
|
|
|
|
}
|