mirror of
https://github.com/rclone/rclone.git
synced 2024-11-07 09:04:52 +01:00
vfs: Don't error a r/w file open without cache; delay error until Read called
If we open a file for r/w without the cache we now always return a handle and return an error if the file is ever read from. This fixes incompatibility with cmount under windows.
This commit is contained in:
parent
dec21ccf63
commit
992647b157
@ -382,11 +382,11 @@ func (f *File) Open(flags int) (fd Handle, err error) {
|
||||
if read && write {
|
||||
if CacheMode >= CacheModeMinimal {
|
||||
fd, err = f.OpenRW(flags)
|
||||
} else if flags&os.O_TRUNC != 0 {
|
||||
fd, err = f.OpenWrite()
|
||||
} else {
|
||||
fs.Errorf(f, "Can't open for read and write without cache")
|
||||
return nil, EPERM
|
||||
// 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()
|
||||
}
|
||||
} else if write {
|
||||
if CacheMode >= CacheModeWrites {
|
||||
|
@ -172,7 +172,9 @@ func TestFileOpen(t *testing.T) {
|
||||
require.NoError(t, fd.Close())
|
||||
|
||||
fd, err = file.Open(os.O_RDWR)
|
||||
assert.Equal(t, EPERM, err)
|
||||
assert.NoError(t, err)
|
||||
_, ok = fd.(*WriteFileHandle)
|
||||
assert.True(t, ok)
|
||||
|
||||
fd, err = file.Open(3)
|
||||
assert.Equal(t, EPERM, err)
|
||||
|
14
vfs/write.go
14
vfs/write.go
@ -248,3 +248,17 @@ func (fh *WriteFileHandle) Truncate(size int64) (err error) {
|
||||
// File is correct size
|
||||
return nil
|
||||
}
|
||||
|
||||
// Read reads up to len(p) bytes into p.
|
||||
func (fh *WriteFileHandle) Read(p []byte) (n int, err error) {
|
||||
fs.Errorf(fh.remote, "Read: Can't read and write to file without cache")
|
||||
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) {
|
||||
fs.Errorf(fh.remote, "ReadAt: Can't read and write to file without cache")
|
||||
return 0, EPERM
|
||||
}
|
||||
|
@ -56,6 +56,15 @@ func TestWriteFileHandleMethods(t *testing.T) {
|
||||
assert.Equal(t, int64(5), fi.Size())
|
||||
assert.Equal(t, "file1", fi.Name())
|
||||
|
||||
// Read
|
||||
var buf = make([]byte, 16)
|
||||
_, err = fh.Read(buf)
|
||||
assert.Equal(t, EPERM, err)
|
||||
|
||||
// ReadAt
|
||||
_, err = fh.ReadAt(buf, 0)
|
||||
assert.Equal(t, EPERM, err)
|
||||
|
||||
// Close
|
||||
assert.NoError(t, fh.Close())
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user