2017-05-10 18:19:32 +08:00
|
|
|
// +build linux darwin freebsd
|
|
|
|
|
|
|
|
package mounttest
|
|
|
|
|
|
|
|
import (
|
2018-03-09 02:08:01 +08:00
|
|
|
"runtime"
|
2017-05-10 18:19:32 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-10-07 04:05:21 +08:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/vfs"
|
2017-05-10 18:19:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestWriteFileDoubleClose tests double close on write
|
|
|
|
func TestWriteFileDoubleClose(t *testing.T) {
|
|
|
|
run.skipIfNoFUSE(t)
|
2018-03-09 02:08:01 +08:00
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
t.Skip("Skipping test on OSX")
|
|
|
|
}
|
2017-05-10 18:19:32 +08:00
|
|
|
|
2017-11-08 01:27:53 +08:00
|
|
|
out, err := osCreate(run.path("testdoubleclose"))
|
2017-05-10 18:19:32 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
fd := out.Fd()
|
|
|
|
|
2019-10-07 04:05:21 +08:00
|
|
|
fd1, err := unix.Dup(int(fd))
|
2017-05-10 18:19:32 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-10-07 04:05:21 +08:00
|
|
|
fd2, err := unix.Dup(int(fd))
|
2017-05-10 18:19:32 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// close one of the dups - should produce no error
|
2019-10-07 04:05:21 +08:00
|
|
|
err = unix.Close(fd1)
|
2017-05-10 18:19:32 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// write to the file
|
|
|
|
buf := []byte("hello")
|
|
|
|
n, err := out.Write(buf)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 5, n)
|
|
|
|
|
|
|
|
// close it
|
|
|
|
err = out.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-10-07 04:05:21 +08:00
|
|
|
// write to the other dup
|
|
|
|
_, err = unix.Write(fd2, buf)
|
|
|
|
if run.vfs.Opt.CacheMode < vfs.CacheModeWrites {
|
|
|
|
// produces an error if cache mode < writes
|
|
|
|
assert.Error(t, err, "input/output error")
|
|
|
|
} else {
|
|
|
|
// otherwise does not produce an error
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2017-05-10 18:19:32 +08:00
|
|
|
|
2017-10-30 05:14:05 +08:00
|
|
|
// close the dup - should not produce an error
|
2019-10-07 04:05:21 +08:00
|
|
|
err = unix.Close(fd2)
|
2017-10-30 05:14:05 +08:00
|
|
|
assert.NoError(t, err)
|
2017-05-10 18:19:32 +08:00
|
|
|
|
2017-11-18 23:50:38 +08:00
|
|
|
run.waitForWriters()
|
2017-05-10 18:19:32 +08:00
|
|
|
run.rm(t, "testdoubleclose")
|
|
|
|
}
|
2019-10-07 04:05:21 +08:00
|
|
|
|
|
|
|
// writeTestDup performs the platform-specific implementation of the dup() unix
|
|
|
|
func writeTestDup(oldfd uintptr) (uintptr, error) {
|
|
|
|
newfd, err := unix.Dup(int(oldfd))
|
|
|
|
return uintptr(newfd), err
|
|
|
|
}
|