2020-04-16 20:33:46 +08:00
|
|
|
package vfstest
|
2016-07-18 06:03:23 +08:00
|
|
|
|
|
|
|
import (
|
2019-10-07 04:05:21 +08:00
|
|
|
"os"
|
2019-10-15 08:57:15 +08:00
|
|
|
"runtime"
|
2016-07-18 06:03:23 +08:00
|
|
|
"testing"
|
|
|
|
|
2020-02-28 22:44:15 +08:00
|
|
|
"github.com/rclone/rclone/vfs/vfscommon"
|
2016-07-18 06:03:23 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-01-29 19:29:42 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-07-18 06:03:23 +08:00
|
|
|
)
|
|
|
|
|
2017-05-09 01:05:12 +08:00
|
|
|
// TestWriteFileNoWrite tests writing a file with no write()'s to it
|
2016-07-18 06:03:23 +08:00
|
|
|
func TestWriteFileNoWrite(t *testing.T) {
|
2016-08-20 19:43:33 +08:00
|
|
|
run.skipIfNoFUSE(t)
|
|
|
|
|
2017-11-08 01:27:53 +08:00
|
|
|
fd, err := osCreate(run.path("testnowrite"))
|
2016-07-18 06:03:23 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = fd.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-11-18 23:50:38 +08:00
|
|
|
run.waitForWriters()
|
2017-05-03 05:35:07 +08:00
|
|
|
|
2016-07-18 06:03:23 +08:00
|
|
|
run.checkDir(t, "testnowrite 0")
|
|
|
|
|
|
|
|
run.rm(t, "testnowrite")
|
|
|
|
}
|
|
|
|
|
2017-05-09 01:05:12 +08:00
|
|
|
// FIXMETestWriteOpenFileInDirListing tests open file in directory listing
|
2016-07-18 06:03:23 +08:00
|
|
|
func FIXMETestWriteOpenFileInDirListing(t *testing.T) {
|
2016-08-20 19:43:33 +08:00
|
|
|
run.skipIfNoFUSE(t)
|
|
|
|
|
2017-11-08 01:27:53 +08:00
|
|
|
fd, err := osCreate(run.path("testnowrite"))
|
2016-07-18 06:03:23 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
run.checkDir(t, "testnowrite 0")
|
|
|
|
|
|
|
|
err = fd.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-11-18 23:50:38 +08:00
|
|
|
run.waitForWriters()
|
|
|
|
|
2016-07-18 06:03:23 +08:00
|
|
|
run.rm(t, "testnowrite")
|
|
|
|
}
|
|
|
|
|
2017-05-09 01:05:12 +08:00
|
|
|
// TestWriteFileWrite tests writing a file and reading it back
|
2016-07-18 06:03:23 +08:00
|
|
|
func TestWriteFileWrite(t *testing.T) {
|
2016-08-20 19:43:33 +08:00
|
|
|
run.skipIfNoFUSE(t)
|
|
|
|
|
2016-07-18 06:03:23 +08:00
|
|
|
run.createFile(t, "testwrite", "data")
|
|
|
|
run.checkDir(t, "testwrite 4")
|
|
|
|
contents := run.readFile(t, "testwrite")
|
|
|
|
assert.Equal(t, "data", contents)
|
|
|
|
run.rm(t, "testwrite")
|
|
|
|
}
|
|
|
|
|
2017-05-09 01:05:12 +08:00
|
|
|
// TestWriteFileOverwrite tests overwriting a file
|
2016-07-18 06:03:23 +08:00
|
|
|
func TestWriteFileOverwrite(t *testing.T) {
|
2016-08-20 19:43:33 +08:00
|
|
|
run.skipIfNoFUSE(t)
|
|
|
|
|
2016-07-18 06:03:23 +08:00
|
|
|
run.createFile(t, "testwrite", "data")
|
|
|
|
run.checkDir(t, "testwrite 4")
|
|
|
|
run.createFile(t, "testwrite", "potato")
|
|
|
|
contents := run.readFile(t, "testwrite")
|
|
|
|
assert.Equal(t, "potato", contents)
|
|
|
|
run.rm(t, "testwrite")
|
|
|
|
}
|
|
|
|
|
2017-05-09 01:05:12 +08:00
|
|
|
// TestWriteFileFsync tests Fsync
|
2017-01-29 19:29:42 +08:00
|
|
|
//
|
|
|
|
// NB the code for this is in file.go rather than write.go
|
|
|
|
func TestWriteFileFsync(t *testing.T) {
|
2017-11-18 23:50:38 +08:00
|
|
|
run.skipIfNoFUSE(t)
|
|
|
|
|
2017-01-29 19:29:42 +08:00
|
|
|
filepath := run.path("to be synced")
|
2017-11-08 01:27:53 +08:00
|
|
|
fd, err := osCreate(filepath)
|
2017-01-29 19:29:42 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = fd.Write([]byte("hello"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = fd.Sync()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = fd.Close()
|
|
|
|
require.NoError(t, err)
|
2017-11-18 23:50:38 +08:00
|
|
|
run.waitForWriters()
|
|
|
|
run.rm(t, "to be synced")
|
2017-01-29 19:29:42 +08:00
|
|
|
}
|
2019-10-07 04:05:21 +08:00
|
|
|
|
|
|
|
// TestWriteFileDup tests behavior of mmap() in Python by using dup() on a file handle
|
|
|
|
func TestWriteFileDup(t *testing.T) {
|
2020-04-16 20:33:46 +08:00
|
|
|
run.skipIfVFS(t)
|
2019-10-07 04:05:21 +08:00
|
|
|
run.skipIfNoFUSE(t)
|
|
|
|
|
2022-06-16 19:08:11 +08:00
|
|
|
if run.vfsOpt.CacheMode < vfscommon.CacheModeWrites {
|
2019-10-07 04:05:21 +08:00
|
|
|
t.Skip("not supported on vfs-cache-mode < writes")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filepath := run.path("to be synced")
|
|
|
|
fh, err := osCreate(filepath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
testData := []byte("0123456789")
|
|
|
|
|
|
|
|
err = fh.Truncate(int64(len(testData) + 2))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = fh.Sync()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var dupFd uintptr
|
|
|
|
dupFd, err = writeTestDup(fh.Fd())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dupFile := os.NewFile(dupFd, fh.Name())
|
|
|
|
_, err = dupFile.Write(testData)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = dupFile.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = fh.Seek(int64(len(testData)), 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = fh.Write([]byte("10"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = fh.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
run.waitForWriters()
|
|
|
|
run.rm(t, "to be synced")
|
|
|
|
}
|
2019-10-15 08:57:15 +08:00
|
|
|
|
|
|
|
// TestWriteFileAppend tests that O_APPEND works on cache backends >= writes
|
|
|
|
func TestWriteFileAppend(t *testing.T) {
|
|
|
|
run.skipIfNoFUSE(t)
|
|
|
|
|
2022-06-16 19:08:11 +08:00
|
|
|
if run.vfsOpt.CacheMode < vfscommon.CacheModeWrites {
|
2019-10-15 08:57:15 +08:00
|
|
|
t.Skip("not supported on vfs-cache-mode < writes")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Windows needs the v1.5 release of WinFsp to handle O_APPEND properly.
|
|
|
|
// Until it gets released, skip this test on Windows.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("currently unsupported on Windows")
|
|
|
|
}
|
|
|
|
|
|
|
|
filepath := run.path("to be synced")
|
|
|
|
fh, err := osCreate(filepath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
testData := []byte("0123456789")
|
|
|
|
appendData := []byte("10")
|
|
|
|
|
|
|
|
_, err = fh.Write(testData)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = fh.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fh, err = osAppend(filepath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = fh.Write(appendData)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = fh.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-04-16 20:33:46 +08:00
|
|
|
info, err := run.os.Stat(filepath)
|
2019-10-15 08:57:15 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, len(testData)+len(appendData), info.Size())
|
|
|
|
|
|
|
|
run.waitForWriters()
|
|
|
|
run.rm(t, "to be synced")
|
|
|
|
}
|