1
0
mirror of https://github.com/rclone/rclone.git synced 2025-12-06 00:03:32 +00:00

vfs, cmount: make truncate work properly in the presence or otherwise of open files

This commit is contained in:
Nick Craig-Wood
2017-11-16 09:31:33 +00:00
parent 94adf4f43b
commit dec21ccf63
4 changed files with 104 additions and 39 deletions

View File

@@ -49,7 +49,7 @@ func newWriteFileHandle(d *Dir, f *File, remote string) (*WriteFileHandle, error
fh.o = o
fh.result <- err
}()
fh.file.addWriters(1)
fh.file.addWriter(fh)
fh.file.setSize(0)
return fh, nil
}
@@ -129,6 +129,11 @@ func (fh *WriteFileHandle) Write(p []byte) (n int, err error) {
return fh.writeAt(p, fh.offset)
}
// WriteString a string to the file
func (fh *WriteFileHandle) WriteString(s string) (n int, err error) {
return fh.Write([]byte(s))
}
// Offset returns the offset of the file pointer
func (fh *WriteFileHandle) Offset() (offset int64) {
fh.mu.Lock()
@@ -145,7 +150,7 @@ func (fh *WriteFileHandle) close() error {
return ECLOSED
}
fh.closed = true
fh.file.addWriters(-1)
fh.file.delWriter(fh)
writeCloseErr := fh.pipeWriter.Close()
err := <-fh.result
if err == nil {
@@ -228,3 +233,18 @@ func (fh *WriteFileHandle) Stat() (os.FileInfo, error) {
defer fh.mu.Unlock()
return fh.file, nil
}
// Truncate file to given size
func (fh *WriteFileHandle) Truncate(size int64) (err error) {
fh.mu.Lock()
defer fh.mu.Unlock()
if fh.closed {
return ECLOSED
}
if size != fh.offset {
fs.Errorf(fh.remote, "Truncate: Can't change size without cache")
return EPERM
}
// File is correct size
return nil
}