1
mirror of https://github.com/rclone/rclone synced 2024-12-22 13:03:02 +01:00

vfs: make specialized file Open functions private

This commit is contained in:
Nick Craig-Wood 2017-12-06 16:38:19 +00:00
parent 5ad226ab54
commit 517bdc719b
3 changed files with 26 additions and 24 deletions

View File

@ -5,6 +5,7 @@ import (
"html/template"
"log"
"net/http"
"os"
"path"
"strconv"
"strings"
@ -247,7 +248,7 @@ func (s *server) serveFile(w http.ResponseWriter, r *http.Request, remote string
}
// open the object
in, err := file.OpenRead()
in, err := file.Open(os.O_RDONLY)
if err != nil {
internalError(remote, w, "Failed to open file", err)
return

View File

@ -242,53 +242,54 @@ func (f *File) waitForValidObject() (o fs.Object, err error) {
return nil, ENOENT
}
// OpenRead open the file for read
func (f *File) OpenRead() (fh *ReadFileHandle, err error) {
// openRead open the file for read
func (f *File) openRead() (fh *ReadFileHandle, err error) {
// if o is nil it isn't valid yet
o, err := f.waitForValidObject()
if err != nil {
return nil, err
}
// fs.Debugf(o, "File.OpenRead")
// fs.Debugf(o, "File.openRead")
fh, err = newReadFileHandle(f, o)
if err != nil {
err = errors.Wrap(err, "open for read")
fs.Errorf(f, "File.OpenRead failed: %v", err)
fs.Errorf(f, "File.openRead failed: %v", err)
return nil, err
}
return fh, nil
}
// OpenWrite open the file for write
func (f *File) OpenWrite(flags int) (fh *WriteFileHandle, err error) {
// openWrite open the file for write
func (f *File) openWrite(flags int) (fh *WriteFileHandle, err error) {
if f.d.vfs.Opt.ReadOnly {
return nil, EROFS
}
// fs.Debugf(o, "File.OpenWrite")
// fs.Debugf(o, "File.openWrite")
fh, err = newWriteFileHandle(f.d, f, f.Path(), flags)
if err != nil {
err = errors.Wrap(err, "open for write")
fs.Errorf(f, "File.OpenWrite failed: %v", err)
fs.Errorf(f, "File.openWrite failed: %v", err)
return nil, err
}
return fh, nil
}
// OpenRW open the file for read and write using a temporay file
// openRW open the file for read and write using a temporay file
//
// It uses the open flags passed in.
func (f *File) OpenRW(flags int) (fh *RWFileHandle, err error) {
func (f *File) openRW(flags int) (fh *RWFileHandle, err error) {
// FIXME chunked
if flags&accessModeMask != os.O_RDONLY && f.d.vfs.Opt.ReadOnly {
return nil, EROFS
}
// fs.Debugf(o, "File.OpenRW")
// fs.Debugf(o, "File.openRW")
fh, err = newRWFileHandle(f.d, f, f.Path(), flags)
if err != nil {
err = errors.Wrap(err, "open for read write")
fs.Errorf(f, "File.OpenRW failed: %v", err)
fs.Errorf(f, "File.openRW failed: %v", err)
return nil, err
}
return fh, nil
@ -377,12 +378,12 @@ func (f *File) Open(flags int) (fd Handle, err error) {
return nil, EPERM
}
// If append is set then set read to force OpenRW
// If append is set then set read to force openRW
if flags&os.O_APPEND != 0 {
read = true
}
// If truncate is set then set write to force OpenRW
// If truncate is set then set write to force openRW
if flags&os.O_TRUNC != 0 {
write = true
}
@ -393,24 +394,24 @@ func (f *File) Open(flags int) (fd Handle, err error) {
CacheMode := f.d.vfs.Opt.CacheMode
if read && write {
if CacheMode >= CacheModeMinimal {
fd, err = f.OpenRW(flags)
fd, err = f.openRW(flags)
} else {
// Open write only and hope the user doesn't
// want to read. If they do they will get an
// EPERM plus an Error log.
fd, err = f.OpenWrite(flags)
fd, err = f.openWrite(flags)
}
} else if write {
if CacheMode >= CacheModeWrites {
fd, err = f.OpenRW(flags)
fd, err = f.openRW(flags)
} else {
fd, err = f.OpenWrite(flags)
fd, err = f.openWrite(flags)
}
} else if read {
if CacheMode >= CacheModeFull {
fd, err = f.OpenRW(flags)
fd, err = f.openRW(flags)
} else {
fd, err = f.OpenRead()
fd, err = f.openRead()
}
} else {
fs.Errorf(f, "Can't figure out how to open with flags: 0x%X", flags)

View File

@ -96,7 +96,7 @@ func TestFileOpenRead(t *testing.T) {
defer r.Finalise()
_, file, _ := fileCreate(t, r)
fd, err := file.OpenRead()
fd, err := file.openRead()
require.NoError(t, err)
contents, err := ioutil.ReadAll(fd)
@ -111,7 +111,7 @@ func TestFileOpenWrite(t *testing.T) {
defer r.Finalise()
vfs, file, _ := fileCreate(t, r)
fd, err := file.OpenWrite(os.O_WRONLY | os.O_TRUNC)
fd, err := file.openWrite(os.O_WRONLY | os.O_TRUNC)
require.NoError(t, err)
newContents := []byte("this is some new contents")
@ -123,7 +123,7 @@ func TestFileOpenWrite(t *testing.T) {
assert.Equal(t, int64(25), file.Size())
vfs.Opt.ReadOnly = true
_, err = file.OpenWrite(os.O_WRONLY | os.O_TRUNC)
_, err = file.openWrite(os.O_WRONLY | os.O_TRUNC)
assert.Equal(t, EROFS, err)
}