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:
parent
5ad226ab54
commit
517bdc719b
@ -5,6 +5,7 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -247,7 +248,7 @@ func (s *server) serveFile(w http.ResponseWriter, r *http.Request, remote string
|
|||||||
}
|
}
|
||||||
|
|
||||||
// open the object
|
// open the object
|
||||||
in, err := file.OpenRead()
|
in, err := file.Open(os.O_RDONLY)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalError(remote, w, "Failed to open file", err)
|
internalError(remote, w, "Failed to open file", err)
|
||||||
return
|
return
|
||||||
|
41
vfs/file.go
41
vfs/file.go
@ -242,53 +242,54 @@ func (f *File) waitForValidObject() (o fs.Object, err error) {
|
|||||||
return nil, ENOENT
|
return nil, ENOENT
|
||||||
}
|
}
|
||||||
|
|
||||||
// OpenRead open the file for read
|
// openRead open the file for read
|
||||||
func (f *File) OpenRead() (fh *ReadFileHandle, err error) {
|
func (f *File) openRead() (fh *ReadFileHandle, err error) {
|
||||||
// if o is nil it isn't valid yet
|
// if o is nil it isn't valid yet
|
||||||
o, err := f.waitForValidObject()
|
o, err := f.waitForValidObject()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// fs.Debugf(o, "File.OpenRead")
|
// fs.Debugf(o, "File.openRead")
|
||||||
|
|
||||||
fh, err = newReadFileHandle(f, o)
|
fh, err = newReadFileHandle(f, o)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errors.Wrap(err, "open for read")
|
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 nil, err
|
||||||
}
|
}
|
||||||
return fh, nil
|
return fh, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// OpenWrite open the file for write
|
// openWrite open the file for write
|
||||||
func (f *File) OpenWrite(flags int) (fh *WriteFileHandle, err error) {
|
func (f *File) openWrite(flags int) (fh *WriteFileHandle, err error) {
|
||||||
if f.d.vfs.Opt.ReadOnly {
|
if f.d.vfs.Opt.ReadOnly {
|
||||||
return nil, EROFS
|
return nil, EROFS
|
||||||
}
|
}
|
||||||
// fs.Debugf(o, "File.OpenWrite")
|
// fs.Debugf(o, "File.openWrite")
|
||||||
|
|
||||||
fh, err = newWriteFileHandle(f.d, f, f.Path(), flags)
|
fh, err = newWriteFileHandle(f.d, f, f.Path(), flags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errors.Wrap(err, "open for write")
|
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 nil, err
|
||||||
}
|
}
|
||||||
return fh, nil
|
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.
|
// 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 {
|
if flags&accessModeMask != os.O_RDONLY && f.d.vfs.Opt.ReadOnly {
|
||||||
return nil, EROFS
|
return nil, EROFS
|
||||||
}
|
}
|
||||||
// fs.Debugf(o, "File.OpenRW")
|
// fs.Debugf(o, "File.openRW")
|
||||||
|
|
||||||
fh, err = newRWFileHandle(f.d, f, f.Path(), flags)
|
fh, err = newRWFileHandle(f.d, f, f.Path(), flags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errors.Wrap(err, "open for read write")
|
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 nil, err
|
||||||
}
|
}
|
||||||
return fh, nil
|
return fh, nil
|
||||||
@ -377,12 +378,12 @@ func (f *File) Open(flags int) (fd Handle, err error) {
|
|||||||
return nil, EPERM
|
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 {
|
if flags&os.O_APPEND != 0 {
|
||||||
read = true
|
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 {
|
if flags&os.O_TRUNC != 0 {
|
||||||
write = true
|
write = true
|
||||||
}
|
}
|
||||||
@ -393,24 +394,24 @@ func (f *File) Open(flags int) (fd Handle, err error) {
|
|||||||
CacheMode := f.d.vfs.Opt.CacheMode
|
CacheMode := f.d.vfs.Opt.CacheMode
|
||||||
if read && write {
|
if read && write {
|
||||||
if CacheMode >= CacheModeMinimal {
|
if CacheMode >= CacheModeMinimal {
|
||||||
fd, err = f.OpenRW(flags)
|
fd, err = f.openRW(flags)
|
||||||
} else {
|
} else {
|
||||||
// Open write only and hope the user doesn't
|
// Open write only and hope the user doesn't
|
||||||
// want to read. If they do they will get an
|
// want to read. If they do they will get an
|
||||||
// EPERM plus an Error log.
|
// EPERM plus an Error log.
|
||||||
fd, err = f.OpenWrite(flags)
|
fd, err = f.openWrite(flags)
|
||||||
}
|
}
|
||||||
} else if write {
|
} else if write {
|
||||||
if CacheMode >= CacheModeWrites {
|
if CacheMode >= CacheModeWrites {
|
||||||
fd, err = f.OpenRW(flags)
|
fd, err = f.openRW(flags)
|
||||||
} else {
|
} else {
|
||||||
fd, err = f.OpenWrite(flags)
|
fd, err = f.openWrite(flags)
|
||||||
}
|
}
|
||||||
} else if read {
|
} else if read {
|
||||||
if CacheMode >= CacheModeFull {
|
if CacheMode >= CacheModeFull {
|
||||||
fd, err = f.OpenRW(flags)
|
fd, err = f.openRW(flags)
|
||||||
} else {
|
} else {
|
||||||
fd, err = f.OpenRead()
|
fd, err = f.openRead()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fs.Errorf(f, "Can't figure out how to open with flags: 0x%X", flags)
|
fs.Errorf(f, "Can't figure out how to open with flags: 0x%X", flags)
|
||||||
|
@ -96,7 +96,7 @@ func TestFileOpenRead(t *testing.T) {
|
|||||||
defer r.Finalise()
|
defer r.Finalise()
|
||||||
_, file, _ := fileCreate(t, r)
|
_, file, _ := fileCreate(t, r)
|
||||||
|
|
||||||
fd, err := file.OpenRead()
|
fd, err := file.openRead()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
contents, err := ioutil.ReadAll(fd)
|
contents, err := ioutil.ReadAll(fd)
|
||||||
@ -111,7 +111,7 @@ func TestFileOpenWrite(t *testing.T) {
|
|||||||
defer r.Finalise()
|
defer r.Finalise()
|
||||||
vfs, file, _ := fileCreate(t, r)
|
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)
|
require.NoError(t, err)
|
||||||
|
|
||||||
newContents := []byte("this is some new contents")
|
newContents := []byte("this is some new contents")
|
||||||
@ -123,7 +123,7 @@ func TestFileOpenWrite(t *testing.T) {
|
|||||||
assert.Equal(t, int64(25), file.Size())
|
assert.Equal(t, int64(25), file.Size())
|
||||||
|
|
||||||
vfs.Opt.ReadOnly = true
|
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)
|
assert.Equal(t, EROFS, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user