2017-10-28 21:01:34 +02:00
|
|
|
package vfs
|
2017-05-02 23:35:07 +02:00
|
|
|
|
|
|
|
import (
|
2017-10-25 11:00:26 +02:00
|
|
|
"os"
|
2017-05-09 12:29:02 +02:00
|
|
|
"path"
|
2017-05-02 23:35:07 +02:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
2018-01-12 17:30:54 +01:00
|
|
|
"github.com/ncw/rclone/fs/log"
|
2017-05-02 23:35:07 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// File represents a file
|
|
|
|
type File struct {
|
|
|
|
inode uint64 // inode number
|
|
|
|
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
|
2017-05-09 12:29:02 +02:00
|
|
|
leaf string // leaf name of the object
|
2017-11-16 10:31:33 +01:00
|
|
|
writers []Handle // writers for this file
|
2017-05-02 23:35:07 +02:00
|
|
|
pendingModTime time.Time // will be applied once o becomes available, i.e. after file was written
|
|
|
|
}
|
|
|
|
|
|
|
|
// newFile creates a new File
|
2017-05-09 12:29:02 +02:00
|
|
|
func newFile(d *Dir, o fs.Object, leaf string) *File {
|
2017-05-02 23:35:07 +02:00
|
|
|
return &File{
|
|
|
|
d: d,
|
|
|
|
o: o,
|
2017-05-09 12:29:02 +02:00
|
|
|
leaf: leaf,
|
2017-10-29 18:37:54 +01:00
|
|
|
inode: newInode(),
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 12:29:02 +02:00
|
|
|
// String converts it to printable
|
|
|
|
func (f *File) String() string {
|
|
|
|
if f == nil {
|
|
|
|
return "<nil *File>"
|
|
|
|
}
|
2017-11-18 12:47:21 +01:00
|
|
|
return f.Path()
|
2017-05-09 12:29:02 +02:00
|
|
|
}
|
|
|
|
|
2017-05-02 23:35:07 +02:00
|
|
|
// IsFile returns true for File - satisfies Node interface
|
|
|
|
func (f *File) IsFile() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-10-25 11:00:26 +02:00
|
|
|
// IsDir returns false for File - satisfies Node interface
|
|
|
|
func (f *File) IsDir() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mode bits of the file or directory - satisfies Node interface
|
|
|
|
func (f *File) Mode() (mode os.FileMode) {
|
2017-10-29 22:14:05 +01:00
|
|
|
return f.d.vfs.Opt.FilePerms
|
2017-10-25 11:00:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name (base) of the directory - satisfies Node interface
|
|
|
|
func (f *File) Name() (name string) {
|
2017-10-29 22:14:05 +01:00
|
|
|
return f.leaf
|
2017-10-25 11:00:26 +02:00
|
|
|
}
|
|
|
|
|
2017-11-18 12:47:21 +01:00
|
|
|
// Path returns the full path of the file
|
|
|
|
func (f *File) Path() string {
|
|
|
|
return path.Join(f.d.path, f.leaf)
|
|
|
|
}
|
|
|
|
|
2017-10-25 11:00:26 +02:00
|
|
|
// Sys returns underlying data source (can be nil) - satisfies Node interface
|
|
|
|
func (f *File) Sys() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-02 23:35:07 +02:00
|
|
|
// Inode returns the inode number - satisfies Node interface
|
|
|
|
func (f *File) Inode() uint64 {
|
|
|
|
return f.inode
|
|
|
|
}
|
|
|
|
|
|
|
|
// Node returns the Node assocuated with this - satisfies Noder interface
|
|
|
|
func (f *File) Node() Node {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2017-10-29 22:14:05 +01:00
|
|
|
// rename should be called to update the internals after a rename
|
2017-05-02 23:35:07 +02:00
|
|
|
func (f *File) rename(d *Dir, o fs.Object) {
|
|
|
|
f.mu.Lock()
|
|
|
|
f.o = o
|
|
|
|
f.d = d
|
2017-10-29 22:14:05 +01:00
|
|
|
f.leaf = path.Base(o.Remote())
|
2017-05-02 23:35:07 +02:00
|
|
|
f.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-11-16 10:31:33 +01:00
|
|
|
// addWriter adds a write handle to the file
|
|
|
|
func (f *File) addWriter(h Handle) {
|
2017-05-02 23:35:07 +02:00
|
|
|
f.mu.Lock()
|
2017-11-16 10:31:33 +01:00
|
|
|
f.writers = append(f.writers, h)
|
|
|
|
f.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// delWriter removes a write handle from the file
|
|
|
|
func (f *File) delWriter(h Handle) {
|
|
|
|
f.mu.Lock()
|
|
|
|
var found = -1
|
|
|
|
for i := range f.writers {
|
|
|
|
if f.writers[i] == h {
|
|
|
|
found = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found >= 0 {
|
|
|
|
f.writers = append(f.writers[:found], f.writers[found+1:]...)
|
|
|
|
} else {
|
|
|
|
fs.Debugf(f.o, "File.delWriter couldn't find handle")
|
|
|
|
}
|
2017-05-02 23:35:07 +02:00
|
|
|
f.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-11-18 12:57:40 +01:00
|
|
|
// activeWriters returns the number of writers on the file
|
|
|
|
func (f *File) activeWriters() int {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
return len(f.writers)
|
|
|
|
}
|
|
|
|
|
2017-10-25 11:00:26 +02:00
|
|
|
// ModTime returns the modified time of the file
|
|
|
|
//
|
|
|
|
// if NoModTime is set then it returns the mod time of the directory
|
|
|
|
func (f *File) ModTime() (modTime time.Time) {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
2017-10-29 12:00:56 +01:00
|
|
|
if !f.d.vfs.Opt.NoModTime {
|
2017-10-25 11:00:26 +02:00
|
|
|
// if o is nil it isn't valid yet or there are writers, so return the size so far
|
2017-11-16 10:31:33 +01:00
|
|
|
if f.o == nil || len(f.writers) != 0 {
|
2017-10-25 11:00:26 +02:00
|
|
|
if !f.pendingModTime.IsZero() {
|
|
|
|
return f.pendingModTime
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return f.o.ModTime()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.d.modTime
|
|
|
|
}
|
|
|
|
|
2018-01-31 16:23:01 +01:00
|
|
|
// nonNegative returns 0 if i is -ve, i otherwise
|
|
|
|
func nonNegative(i int64) int64 {
|
|
|
|
if i >= 0 {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-10-25 11:00:26 +02:00
|
|
|
// Size of the file
|
|
|
|
func (f *File) Size() int64 {
|
2017-05-02 23:35:07 +02:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
2017-10-25 11:00:26 +02:00
|
|
|
|
2017-05-02 23:35:07 +02:00
|
|
|
// if o is nil it isn't valid yet or there are writers, so return the size so far
|
2017-11-16 10:31:33 +01:00
|
|
|
if f.o == nil || len(f.writers) != 0 {
|
2017-10-25 11:00:26 +02:00
|
|
|
return atomic.LoadInt64(&f.size)
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
2018-01-31 16:23:01 +01:00
|
|
|
return nonNegative(f.o.Size())
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetModTime sets the modtime for the file
|
|
|
|
func (f *File) SetModTime(modTime time.Time) error {
|
2017-10-29 12:00:56 +01:00
|
|
|
if f.d.vfs.Opt.ReadOnly {
|
2017-05-11 14:15:51 +02:00
|
|
|
return EROFS
|
|
|
|
}
|
2017-05-02 23:35:07 +02:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
|
|
|
f.pendingModTime = modTime
|
|
|
|
|
|
|
|
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")
|
2017-06-13 14:58:39 +02:00
|
|
|
case fs.ErrorCantSetModTime, fs.ErrorCantSetModTimeWithoutDelete:
|
2017-05-02 23:35:07 +02:00
|
|
|
// do nothing, in order to not break "touch somefile" if it exists already
|
|
|
|
default:
|
2017-10-30 11:14:39 +01:00
|
|
|
fs.Errorf(f, "File.applyPendingModTime error: %v", err)
|
2017-05-02 23:35:07 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the size while writing
|
|
|
|
func (f *File) setSize(n int64) {
|
|
|
|
atomic.StoreInt64(&f.size, n)
|
|
|
|
}
|
|
|
|
|
2017-11-18 12:47:21 +01:00
|
|
|
// Update the object when written and add it to the directory
|
2017-05-02 23:35:07 +02:00
|
|
|
func (f *File) setObject(o fs.Object) {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
f.o = o
|
|
|
|
_ = f.applyPendingModTime()
|
2017-10-26 18:21:03 +02:00
|
|
|
f.d.addObject(f)
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
|
|
|
|
2017-11-16 10:31:33 +01:00
|
|
|
// exists returns whether the file exists already
|
|
|
|
func (f *File) exists() bool {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
return f.o != nil
|
|
|
|
}
|
|
|
|
|
2017-05-02 23:35:07 +02:00
|
|
|
// Wait for f.o to become non nil for a short time returning it or an
|
2017-11-06 22:38:52 +01:00
|
|
|
// error. Use when opening a read handle.
|
2017-05-02 23:35:07 +02:00
|
|
|
//
|
|
|
|
// 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
|
2017-11-16 10:31:33 +01:00
|
|
|
nwriters := len(f.writers)
|
2017-05-02 23:35:07 +02:00
|
|
|
f.mu.Unlock()
|
|
|
|
if o != nil {
|
|
|
|
return o, nil
|
|
|
|
}
|
2017-11-16 10:31:33 +01:00
|
|
|
if nwriters == 0 {
|
2017-05-02 23:35:07 +02:00
|
|
|
return nil, errors.New("can't open file - writer failed")
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
return nil, ENOENT
|
|
|
|
}
|
|
|
|
|
2017-12-06 17:38:19 +01:00
|
|
|
// openRead open the file for read
|
|
|
|
func (f *File) openRead() (fh *ReadFileHandle, err error) {
|
2017-05-02 23:35:07 +02:00
|
|
|
// if o is nil it isn't valid yet
|
|
|
|
o, err := f.waitForValidObject()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-06 17:38:19 +01:00
|
|
|
// fs.Debugf(o, "File.openRead")
|
2017-05-02 23:35:07 +02:00
|
|
|
|
2017-05-08 18:52:09 +02:00
|
|
|
fh, err = newReadFileHandle(f, o)
|
2017-05-02 23:35:07 +02:00
|
|
|
if err != nil {
|
2017-11-06 22:38:52 +01:00
|
|
|
err = errors.Wrap(err, "open for read")
|
2017-12-06 17:38:19 +01:00
|
|
|
fs.Errorf(f, "File.openRead failed: %v", err)
|
2017-05-02 23:35:07 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fh, nil
|
|
|
|
}
|
|
|
|
|
2017-12-06 17:38:19 +01:00
|
|
|
// openWrite open the file for write
|
|
|
|
func (f *File) openWrite(flags int) (fh *WriteFileHandle, err error) {
|
2017-10-29 12:00:56 +01:00
|
|
|
if f.d.vfs.Opt.ReadOnly {
|
2017-05-11 14:15:51 +02:00
|
|
|
return nil, EROFS
|
|
|
|
}
|
2017-12-06 17:38:19 +01:00
|
|
|
// fs.Debugf(o, "File.openWrite")
|
2017-05-02 23:35:07 +02:00
|
|
|
|
2017-11-20 19:01:42 +01:00
|
|
|
fh, err = newWriteFileHandle(f.d, f, f.Path(), flags)
|
2017-05-02 23:35:07 +02:00
|
|
|
if err != nil {
|
2017-11-06 22:38:52 +01:00
|
|
|
err = errors.Wrap(err, "open for write")
|
2017-12-06 17:38:19 +01:00
|
|
|
fs.Errorf(f, "File.openWrite failed: %v", err)
|
2017-05-02 23:35:07 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fh, nil
|
|
|
|
}
|
|
|
|
|
2017-12-06 17:38:19 +01:00
|
|
|
// openRW open the file for read and write using a temporay file
|
2017-11-06 22:38:52 +01:00
|
|
|
//
|
|
|
|
// It uses the open flags passed in.
|
2017-12-06 17:38:19 +01:00
|
|
|
func (f *File) openRW(flags int) (fh *RWFileHandle, err error) {
|
|
|
|
// FIXME chunked
|
2017-11-06 22:38:52 +01:00
|
|
|
if flags&accessModeMask != os.O_RDONLY && f.d.vfs.Opt.ReadOnly {
|
|
|
|
return nil, EROFS
|
|
|
|
}
|
2017-12-06 17:38:19 +01:00
|
|
|
// fs.Debugf(o, "File.openRW")
|
2017-11-06 22:38:52 +01:00
|
|
|
|
2017-11-18 12:47:21 +01:00
|
|
|
fh, err = newRWFileHandle(f.d, f, f.Path(), flags)
|
2017-11-06 22:38:52 +01:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "open for read write")
|
2017-12-06 17:38:19 +01:00
|
|
|
fs.Errorf(f, "File.openRW failed: %v", err)
|
2017-11-06 22:38:52 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fh, nil
|
|
|
|
}
|
|
|
|
|
2017-11-18 16:48:49 +01:00
|
|
|
// Sync the file
|
2017-05-02 23:35:07 +02:00
|
|
|
//
|
|
|
|
// Note that we don't do anything except return OK
|
2017-11-18 16:48:49 +01:00
|
|
|
func (f *File) Sync() error {
|
2017-05-02 23:35:07 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-10-26 17:55:40 +02:00
|
|
|
|
|
|
|
// Remove the file
|
|
|
|
func (f *File) Remove() error {
|
2017-10-29 12:00:56 +01:00
|
|
|
if f.d.vfs.Opt.ReadOnly {
|
2017-10-26 17:55:40 +02:00
|
|
|
return EROFS
|
|
|
|
}
|
2017-10-29 22:14:05 +01:00
|
|
|
if f.o != nil {
|
|
|
|
err := f.o.Remove()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(f, "File.Remove file error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2017-10-26 17:55:40 +02:00
|
|
|
}
|
|
|
|
// Remove the item from the directory listing
|
|
|
|
f.d.delObject(f.Name())
|
2017-11-27 20:48:25 +01:00
|
|
|
// Remove the object from the cache
|
|
|
|
if f.d.vfs.Opt.CacheMode >= CacheModeMinimal {
|
|
|
|
f.d.vfs.cache.remove(f.Path())
|
|
|
|
}
|
2017-10-26 17:55:40 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAll the file - same as remove for files
|
|
|
|
func (f *File) RemoveAll() error {
|
|
|
|
return f.Remove()
|
|
|
|
}
|
2017-10-26 18:02:48 +02:00
|
|
|
|
2017-10-29 22:14:05 +01:00
|
|
|
// DirEntry returns the underlying fs.DirEntry - may be nil
|
2017-10-26 18:02:48 +02:00
|
|
|
func (f *File) DirEntry() (entry fs.DirEntry) {
|
|
|
|
return f.o
|
|
|
|
}
|
2017-10-29 12:00:56 +01:00
|
|
|
|
|
|
|
// Dir returns the directory this file is in
|
|
|
|
func (f *File) Dir() *Dir {
|
|
|
|
return f.d
|
|
|
|
}
|
|
|
|
|
|
|
|
// VFS returns the instance of the VFS
|
|
|
|
func (f *File) VFS() *VFS {
|
|
|
|
return f.d.vfs
|
|
|
|
}
|
2017-10-30 11:14:39 +01:00
|
|
|
|
|
|
|
// Open a file according to the flags provided
|
2017-11-06 22:38:52 +01:00
|
|
|
//
|
|
|
|
// O_RDONLY open the file read-only.
|
|
|
|
// O_WRONLY open the file write-only.
|
|
|
|
// O_RDWR open the file read-write.
|
|
|
|
//
|
|
|
|
// O_APPEND append data to the file when writing.
|
|
|
|
// O_CREATE create a new file if none exists.
|
|
|
|
// O_EXCL used with O_CREATE, file must not exist
|
|
|
|
// O_SYNC open for synchronous I/O.
|
|
|
|
// O_TRUNC if possible, truncate file when opene
|
|
|
|
//
|
|
|
|
// We ignore O_SYNC and O_EXCL
|
2017-10-30 11:14:39 +01:00
|
|
|
func (f *File) Open(flags int) (fd Handle, err error) {
|
2018-01-12 17:30:54 +01:00
|
|
|
defer log.Trace(f, "flags=%s", decodeOpenFlags(flags))("fd=%v, err=%v", &fd, &err)
|
2017-11-06 22:38:52 +01:00
|
|
|
var (
|
|
|
|
write bool // if set need write support
|
|
|
|
read bool // if set need read support
|
|
|
|
rdwrMode = flags & (os.O_RDONLY | os.O_WRONLY | os.O_RDWR)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Figure out the read/write intents
|
2017-10-30 11:14:39 +01:00
|
|
|
switch {
|
|
|
|
case rdwrMode == os.O_RDONLY:
|
|
|
|
read = true
|
2017-11-06 22:38:52 +01:00
|
|
|
case rdwrMode == os.O_WRONLY:
|
|
|
|
write = true
|
2017-10-30 11:14:39 +01:00
|
|
|
case rdwrMode == os.O_RDWR:
|
2017-11-06 22:38:52 +01:00
|
|
|
read = true
|
|
|
|
write = true
|
2017-10-30 11:14:39 +01:00
|
|
|
default:
|
|
|
|
fs.Errorf(f, "Can't figure out how to open with flags: 0x%X", flags)
|
2017-10-31 16:33:08 +01:00
|
|
|
return nil, EPERM
|
2017-10-30 11:14:39 +01:00
|
|
|
}
|
2017-11-06 22:38:52 +01:00
|
|
|
|
2017-12-06 17:38:19 +01:00
|
|
|
// If append is set then set read to force openRW
|
2017-11-06 22:38:52 +01:00
|
|
|
if flags&os.O_APPEND != 0 {
|
|
|
|
read = true
|
|
|
|
}
|
|
|
|
|
2017-12-06 17:38:19 +01:00
|
|
|
// If truncate is set then set write to force openRW
|
2017-11-06 22:38:52 +01:00
|
|
|
if flags&os.O_TRUNC != 0 {
|
|
|
|
write = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME discover if file is in cache or not?
|
|
|
|
|
|
|
|
// Open the correct sort of handle
|
|
|
|
CacheMode := f.d.vfs.Opt.CacheMode
|
|
|
|
if read && write {
|
|
|
|
if CacheMode >= CacheModeMinimal {
|
2017-12-06 17:38:19 +01:00
|
|
|
fd, err = f.openRW(flags)
|
2017-11-06 22:38:52 +01:00
|
|
|
} else {
|
2017-11-16 11:55:24 +01:00
|
|
|
// Open write only and hope the user doesn't
|
|
|
|
// want to read. If they do they will get an
|
|
|
|
// EPERM plus an Error log.
|
2017-12-06 17:38:19 +01:00
|
|
|
fd, err = f.openWrite(flags)
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
} else if write {
|
|
|
|
if CacheMode >= CacheModeWrites {
|
2017-12-06 17:38:19 +01:00
|
|
|
fd, err = f.openRW(flags)
|
2017-11-06 22:38:52 +01:00
|
|
|
} else {
|
2017-12-06 17:38:19 +01:00
|
|
|
fd, err = f.openWrite(flags)
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
} else if read {
|
|
|
|
if CacheMode >= CacheModeFull {
|
2017-12-06 17:38:19 +01:00
|
|
|
fd, err = f.openRW(flags)
|
2017-11-06 22:38:52 +01:00
|
|
|
} else {
|
2017-12-06 17:38:19 +01:00
|
|
|
fd, err = f.openRead()
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
2017-10-30 11:14:39 +01:00
|
|
|
} else {
|
2017-11-06 22:38:52 +01:00
|
|
|
fs.Errorf(f, "Can't figure out how to open with flags: 0x%X", flags)
|
|
|
|
return nil, EPERM
|
2017-10-30 11:14:39 +01:00
|
|
|
}
|
|
|
|
return fd, err
|
|
|
|
}
|
2017-11-06 22:38:52 +01:00
|
|
|
|
|
|
|
// Truncate changes the size of the named file.
|
2017-11-16 10:31:33 +01:00
|
|
|
func (f *File) Truncate(size int64) (err error) {
|
|
|
|
// make a copy of fh.writers with the lock held then unlock so
|
|
|
|
// we can call other file methods.
|
|
|
|
f.mu.Lock()
|
|
|
|
writers := make([]Handle, len(f.writers))
|
|
|
|
copy(writers, f.writers)
|
|
|
|
f.mu.Unlock()
|
|
|
|
|
|
|
|
// If have writers then call truncate for each writer
|
|
|
|
if len(writers) != 0 {
|
|
|
|
fs.Debugf(f.o, "Truncating %d file handles", len(writers))
|
|
|
|
for _, h := range writers {
|
|
|
|
truncateErr := h.Truncate(size)
|
|
|
|
if truncateErr != nil {
|
|
|
|
err = truncateErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fs.Debugf(f.o, "Truncating file")
|
|
|
|
|
|
|
|
// Otherwise if no writers then truncate the file by opening
|
|
|
|
// the file and truncating it.
|
|
|
|
flags := os.O_WRONLY
|
|
|
|
if size == 0 {
|
|
|
|
flags |= os.O_TRUNC
|
|
|
|
}
|
|
|
|
fh, err := f.Open(flags)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fs.CheckClose(fh, &err)
|
|
|
|
if size != 0 {
|
|
|
|
return fh.Truncate(size)
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
2017-11-16 10:31:33 +01:00
|
|
|
return nil
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|