2017-10-30 05:14:05 +08:00
|
|
|
package vfs
|
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2021-11-04 18:12:57 +08:00
|
|
|
"errors"
|
2019-08-04 01:24:12 +08:00
|
|
|
"io"
|
2017-10-30 05:14:05 +08:00
|
|
|
"os"
|
2021-11-10 16:49:09 +08:00
|
|
|
"runtime"
|
2019-02-28 01:14:32 +08:00
|
|
|
"sync"
|
2017-10-30 05:14:05 +08:00
|
|
|
"testing"
|
2018-03-07 04:47:11 +08:00
|
|
|
"time"
|
2017-10-30 05:14:05 +08:00
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fstest"
|
2019-08-06 22:18:08 +08:00
|
|
|
"github.com/rclone/rclone/lib/random"
|
2017-10-30 05:14:05 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Open a file for write
|
2022-12-08 20:43:53 +08:00
|
|
|
func writeHandleCreate(t *testing.T) (r *fstest.Run, vfs *VFS, fh *WriteFileHandle) {
|
|
|
|
r, vfs = newTestVFS(t)
|
2017-10-30 05:14:05 +08:00
|
|
|
|
|
|
|
h, err := vfs.OpenFile("file1", os.O_WRONLY|os.O_CREATE, 0777)
|
|
|
|
require.NoError(t, err)
|
|
|
|
fh, ok := h.(*WriteFileHandle)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
2022-12-08 20:43:53 +08:00
|
|
|
return r, vfs, fh
|
2017-10-30 05:14:05 +08:00
|
|
|
}
|
|
|
|
|
2021-11-10 16:49:09 +08:00
|
|
|
// Test write when underlying storage is readonly, must be run as non-root
|
|
|
|
func TestWriteFileHandleReadonly(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skipf("Skipping test on %s", runtime.GOOS)
|
|
|
|
}
|
|
|
|
if *fstest.RemoteName != "" {
|
|
|
|
t.Skip("Skipping test on non local remote")
|
|
|
|
}
|
|
|
|
r, vfs, fh := writeHandleCreate(t)
|
|
|
|
|
2023-08-31 21:28:10 +08:00
|
|
|
// Name
|
|
|
|
assert.Equal(t, "file1", fh.Name())
|
|
|
|
|
2021-11-10 16:49:09 +08:00
|
|
|
// Write a file, so underlying remote will be created
|
|
|
|
_, err := fh.Write([]byte("hello"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = fh.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
var info os.FileInfo
|
|
|
|
info, err = os.Stat(r.FremoteName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Remove write permission
|
|
|
|
oldMode := info.Mode()
|
|
|
|
err = os.Chmod(r.FremoteName, oldMode^(oldMode&0222))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
var h Handle
|
|
|
|
h, err = vfs.OpenFile("file2", os.O_WRONLY|os.O_CREATE, 0777)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
fh, ok = h.(*WriteFileHandle)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
// error is propagated to Close()
|
|
|
|
_, err = fh.Write([]byte("hello"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = fh.Close()
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
// Remove should fail
|
|
|
|
err = vfs.Remove("file1")
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
// Only file1 should exist
|
|
|
|
_, err = vfs.Stat("file1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = vfs.Stat("file2")
|
|
|
|
assert.Equal(t, true, errors.Is(err, os.ErrNotExist))
|
|
|
|
|
|
|
|
// Restore old permission
|
|
|
|
err = os.Chmod(r.FremoteName, oldMode)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2017-10-30 05:14:05 +08:00
|
|
|
func TestWriteFileHandleMethods(t *testing.T) {
|
2022-12-08 20:43:53 +08:00
|
|
|
r, vfs, fh := writeHandleCreate(t)
|
2017-10-30 05:14:05 +08:00
|
|
|
|
|
|
|
// String
|
|
|
|
assert.Equal(t, "file1 (w)", fh.String())
|
|
|
|
assert.Equal(t, "<nil *WriteFileHandle>", (*WriteFileHandle)(nil).String())
|
|
|
|
assert.Equal(t, "<nil *WriteFileHandle.file>", new(WriteFileHandle).String())
|
|
|
|
|
|
|
|
// Node
|
|
|
|
node := fh.Node()
|
|
|
|
assert.Equal(t, "file1", node.Name())
|
|
|
|
|
|
|
|
// Offset #1
|
|
|
|
assert.Equal(t, int64(0), fh.Offset())
|
|
|
|
assert.Equal(t, int64(0), node.Size())
|
|
|
|
|
|
|
|
// Write (smoke test only since heavy lifting done in WriteAt)
|
|
|
|
n, err := fh.Write([]byte("hello"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 5, n)
|
|
|
|
|
|
|
|
// Offset #2
|
|
|
|
assert.Equal(t, int64(5), fh.Offset())
|
|
|
|
assert.Equal(t, int64(5), node.Size())
|
|
|
|
|
|
|
|
// Stat
|
|
|
|
var fi os.FileInfo
|
|
|
|
fi, err = fh.Stat()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(5), fi.Size())
|
|
|
|
assert.Equal(t, "file1", fi.Name())
|
|
|
|
|
2017-11-16 18:55:24 +08:00
|
|
|
// 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)
|
|
|
|
|
2017-11-18 23:48:49 +08:00
|
|
|
// Sync
|
|
|
|
err = fh.Sync()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-11-21 02:01:42 +08:00
|
|
|
// Truncate - can only truncate where the file pointer is
|
|
|
|
err = fh.Truncate(5)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = fh.Truncate(6)
|
|
|
|
assert.Equal(t, EPERM, err)
|
|
|
|
|
2017-10-30 05:14:05 +08:00
|
|
|
// Close
|
|
|
|
assert.NoError(t, fh.Close())
|
|
|
|
|
|
|
|
// Check double close
|
|
|
|
err = fh.Close()
|
2017-11-03 19:35:36 +08:00
|
|
|
assert.Equal(t, ECLOSED, err)
|
2017-10-30 05:14:05 +08:00
|
|
|
|
|
|
|
// check vfs
|
|
|
|
root, err := vfs.Root()
|
2018-05-04 22:19:50 +08:00
|
|
|
require.NoError(t, err)
|
2017-10-30 05:14:05 +08:00
|
|
|
checkListing(t, root, []string{"file1,5,false"})
|
|
|
|
|
|
|
|
// check the underlying r.Fremote but not the modtime
|
|
|
|
file1 := fstest.NewItem("file1", "hello", t1)
|
|
|
|
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1}, []string{}, fs.ModTimeNotSupported)
|
2017-11-21 02:01:42 +08:00
|
|
|
|
|
|
|
// Check trying to open the file now it exists then closing it
|
|
|
|
// immediately is OK
|
|
|
|
h, err := vfs.OpenFile("file1", os.O_WRONLY|os.O_CREATE, 0777)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NoError(t, h.Close())
|
|
|
|
checkListing(t, root, []string{"file1,5,false"})
|
|
|
|
|
|
|
|
// Check trying to open the file and writing it now it exists
|
|
|
|
// returns an error
|
|
|
|
h, err = vfs.OpenFile("file1", os.O_WRONLY|os.O_CREATE, 0777)
|
2018-05-04 22:19:50 +08:00
|
|
|
require.NoError(t, err)
|
2017-11-21 02:01:42 +08:00
|
|
|
_, err = h.Write([]byte("hello1"))
|
|
|
|
require.Equal(t, EPERM, err)
|
|
|
|
assert.NoError(t, h.Close())
|
|
|
|
checkListing(t, root, []string{"file1,5,false"})
|
|
|
|
|
|
|
|
// Check opening the file with O_TRUNC does actually truncate
|
|
|
|
// it even if we don't write to it
|
|
|
|
h, err = vfs.OpenFile("file1", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)
|
|
|
|
require.NoError(t, err)
|
2019-06-29 18:37:10 +08:00
|
|
|
err = h.Close()
|
2021-11-04 18:12:57 +08:00
|
|
|
if !errors.Is(err, fs.ErrorCantUploadEmptyFiles) {
|
2019-06-29 18:37:10 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
checkListing(t, root, []string{"file1,0,false"})
|
|
|
|
}
|
2017-11-21 02:01:42 +08:00
|
|
|
|
|
|
|
// Check opening the file with O_TRUNC and writing does work
|
|
|
|
h, err = vfs.OpenFile("file1", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)
|
2018-05-04 22:19:50 +08:00
|
|
|
require.NoError(t, err)
|
2017-11-21 02:01:42 +08:00
|
|
|
_, err = h.WriteString("hello12")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NoError(t, h.Close())
|
|
|
|
checkListing(t, root, []string{"file1,7,false"})
|
2017-10-30 05:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteFileHandleWriteAt(t *testing.T) {
|
2022-12-08 20:43:53 +08:00
|
|
|
r, vfs, fh := writeHandleCreate(t)
|
2017-10-30 05:14:05 +08:00
|
|
|
|
|
|
|
// Preconditions
|
|
|
|
assert.Equal(t, int64(0), fh.offset)
|
|
|
|
assert.False(t, fh.writeCalled)
|
|
|
|
|
|
|
|
// Write the data
|
|
|
|
n, err := fh.WriteAt([]byte("hello"), 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 5, n)
|
|
|
|
|
|
|
|
// After write
|
|
|
|
assert.Equal(t, int64(5), fh.offset)
|
|
|
|
assert.True(t, fh.writeCalled)
|
|
|
|
|
|
|
|
// Check can't seek
|
|
|
|
n, err = fh.WriteAt([]byte("hello"), 100)
|
|
|
|
assert.Equal(t, ESPIPE, err)
|
|
|
|
assert.Equal(t, 0, n)
|
|
|
|
|
|
|
|
// Write more data
|
|
|
|
n, err = fh.WriteAt([]byte(" world"), 5)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 6, n)
|
|
|
|
|
|
|
|
// Close
|
|
|
|
assert.NoError(t, fh.Close())
|
|
|
|
|
|
|
|
// Check can't write on closed handle
|
|
|
|
n, err = fh.WriteAt([]byte("hello"), 0)
|
2017-11-03 19:35:36 +08:00
|
|
|
assert.Equal(t, ECLOSED, err)
|
2017-10-30 05:14:05 +08:00
|
|
|
assert.Equal(t, 0, n)
|
|
|
|
|
|
|
|
// check vfs
|
|
|
|
root, err := vfs.Root()
|
2018-05-04 22:19:50 +08:00
|
|
|
require.NoError(t, err)
|
2017-10-30 05:14:05 +08:00
|
|
|
checkListing(t, root, []string{"file1,11,false"})
|
|
|
|
|
|
|
|
// check the underlying r.Fremote but not the modtime
|
|
|
|
file1 := fstest.NewItem("file1", "hello world", t1)
|
|
|
|
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1}, []string{}, fs.ModTimeNotSupported)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteFileHandleFlush(t *testing.T) {
|
2022-12-08 20:43:53 +08:00
|
|
|
_, vfs, fh := writeHandleCreate(t)
|
2017-10-30 05:14:05 +08:00
|
|
|
|
2018-01-20 18:10:55 +08:00
|
|
|
// Check Flush already creates file for unwritten handles, without closing it
|
2017-10-30 05:14:05 +08:00
|
|
|
err := fh.Flush()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, fh.closed)
|
2018-01-20 18:10:55 +08:00
|
|
|
root, err := vfs.Root()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
checkListing(t, root, []string{"file1,0,false"})
|
2017-10-30 05:14:05 +08:00
|
|
|
|
|
|
|
// Write some data
|
|
|
|
n, err := fh.Write([]byte("hello"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 5, n)
|
|
|
|
|
|
|
|
// Check Flush closes file if write called
|
|
|
|
err = fh.Flush()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, fh.closed)
|
|
|
|
|
|
|
|
// Check flush does nothing if called again
|
|
|
|
err = fh.Flush()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, fh.closed)
|
2018-01-20 18:10:55 +08:00
|
|
|
|
|
|
|
// Check file was written properly
|
|
|
|
root, err = vfs.Root()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
checkListing(t, root, []string{"file1,5,false"})
|
2017-10-30 05:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteFileHandleRelease(t *testing.T) {
|
2022-12-08 20:43:53 +08:00
|
|
|
_, _, fh := writeHandleCreate(t)
|
2017-10-30 05:14:05 +08:00
|
|
|
|
|
|
|
// Check Release closes file
|
|
|
|
err := fh.Release()
|
2021-11-04 18:12:57 +08:00
|
|
|
if errors.Is(err, fs.ErrorCantUploadEmptyFiles) {
|
2019-06-29 18:37:10 +08:00
|
|
|
t.Logf("skipping test: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-10-30 05:14:05 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, fh.closed)
|
|
|
|
|
|
|
|
// Check Release does nothing if called again
|
|
|
|
err = fh.Release()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, fh.closed)
|
|
|
|
}
|
2018-03-07 04:47:11 +08:00
|
|
|
|
2019-02-28 01:14:32 +08:00
|
|
|
var (
|
|
|
|
canSetModTimeOnce sync.Once
|
|
|
|
canSetModTimeValue = true
|
|
|
|
)
|
|
|
|
|
|
|
|
// returns whether the remote can set modtime
|
|
|
|
func canSetModTime(t *testing.T, r *fstest.Run) bool {
|
|
|
|
canSetModTimeOnce.Do(func() {
|
|
|
|
mtime1 := time.Date(2008, time.November, 18, 17, 32, 31, 0, time.UTC)
|
2019-06-17 16:34:30 +08:00
|
|
|
_ = r.WriteObject(context.Background(), "time_test", "stuff", mtime1)
|
|
|
|
obj, err := r.Fremote.NewObject(context.Background(), "time_test")
|
2019-02-28 01:14:32 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
mtime2 := time.Date(2009, time.November, 18, 17, 32, 31, 0, time.UTC)
|
2019-06-17 16:34:30 +08:00
|
|
|
err = obj.SetModTime(context.Background(), mtime2)
|
2019-02-28 01:14:32 +08:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
canSetModTimeValue = true
|
|
|
|
case fs.ErrorCantSetModTime, fs.ErrorCantSetModTimeWithoutDelete:
|
|
|
|
canSetModTimeValue = false
|
|
|
|
default:
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
require.NoError(t, obj.Remove(context.Background()))
|
2019-02-28 01:14:32 +08:00
|
|
|
fs.Debugf(nil, "Can set mod time: %v", canSetModTimeValue)
|
|
|
|
})
|
|
|
|
return canSetModTimeValue
|
|
|
|
}
|
|
|
|
|
2018-03-07 04:47:11 +08:00
|
|
|
// tests mod time on open files
|
|
|
|
func TestWriteFileModTimeWithOpenWriters(t *testing.T) {
|
2022-12-08 20:43:53 +08:00
|
|
|
r, vfs, fh := writeHandleCreate(t)
|
2020-04-17 18:18:58 +08:00
|
|
|
|
2019-02-28 01:14:32 +08:00
|
|
|
if !canSetModTime(t, r) {
|
2020-04-17 18:18:58 +08:00
|
|
|
t.Skip("can't set mod time")
|
2019-02-28 01:14:32 +08:00
|
|
|
}
|
2018-03-07 04:47:11 +08:00
|
|
|
|
2018-08-04 18:16:43 +08:00
|
|
|
mtime := time.Date(2012, time.November, 18, 17, 32, 31, 0, time.UTC)
|
2018-03-07 04:47:11 +08:00
|
|
|
|
|
|
|
_, err := fh.Write([]byte{104, 105})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = fh.Node().SetModTime(mtime)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = fh.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
info, err := vfs.Stat("file1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-02-28 01:14:32 +08:00
|
|
|
if r.Fremote.Precision() != fs.ModTimeNotSupported {
|
|
|
|
// avoid errors because of timezone differences
|
|
|
|
assert.Equal(t, info.ModTime().Unix(), mtime.Unix())
|
|
|
|
}
|
2018-03-07 04:47:11 +08:00
|
|
|
}
|
2019-08-04 01:24:12 +08:00
|
|
|
|
2019-08-06 22:18:08 +08:00
|
|
|
func testFileReadAt(t *testing.T, n int) {
|
2022-12-08 20:43:53 +08:00
|
|
|
_, vfs, fh := writeHandleCreate(t)
|
2019-08-04 01:24:12 +08:00
|
|
|
|
2019-08-06 22:18:08 +08:00
|
|
|
contents := []byte(random.String(n))
|
|
|
|
if n != 0 {
|
|
|
|
written, err := fh.Write(contents)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, n, written)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the file without writing to it if n==0
|
2019-08-04 01:24:12 +08:00
|
|
|
err := fh.Close()
|
2021-11-04 18:12:57 +08:00
|
|
|
if errors.Is(err, fs.ErrorCantUploadEmptyFiles) {
|
2019-08-04 01:24:12 +08:00
|
|
|
t.Logf("skipping test: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-08-06 22:18:08 +08:00
|
|
|
// read the file back in using ReadAt into a buffer
|
2019-08-04 01:24:12 +08:00
|
|
|
// this simulates what mount does
|
|
|
|
rd, err := vfs.OpenFile("file1", os.O_RDONLY, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
buf := make([]byte, 1024)
|
2019-08-06 22:18:08 +08:00
|
|
|
read, err := rd.ReadAt(buf, 0)
|
2019-08-04 01:24:12 +08:00
|
|
|
if err != io.EOF {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2019-08-06 22:18:08 +08:00
|
|
|
assert.Equal(t, read, n)
|
|
|
|
assert.Equal(t, contents, buf[:read])
|
2019-08-04 01:24:12 +08:00
|
|
|
|
|
|
|
err = rd.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2019-08-06 22:18:08 +08:00
|
|
|
|
|
|
|
func TestFileReadAtZeroLength(t *testing.T) {
|
|
|
|
testFileReadAt(t, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileReadAtNonZeroLength(t *testing.T) {
|
|
|
|
testFileReadAt(t, 100)
|
|
|
|
}
|