2021-09-09 14:25:25 +02:00
|
|
|
//go:build linux || darwin || freebsd
|
2017-05-10 12:19:32 +02:00
|
|
|
|
2020-04-16 14:33:46 +02:00
|
|
|
package vfstest
|
2017-05-10 12:19:32 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestReadFileDoubleClose tests double close on read
|
|
|
|
func TestReadFileDoubleClose(t *testing.T) {
|
2020-04-16 14:33:46 +02:00
|
|
|
run.skipIfVFS(t)
|
2017-05-10 12:19:32 +02:00
|
|
|
run.skipIfNoFUSE(t)
|
|
|
|
|
|
|
|
run.createFile(t, "testdoubleclose", "hello")
|
|
|
|
|
2020-04-16 14:33:46 +02:00
|
|
|
in, err := run.os.Open(run.path("testdoubleclose"))
|
2017-05-10 12:19:32 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
fd := in.Fd()
|
|
|
|
|
|
|
|
fd1, err := syscall.Dup(int(fd))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
fd2, err := syscall.Dup(int(fd))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// close one of the dups - should produce no error
|
|
|
|
err = syscall.Close(fd1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// read from the file
|
|
|
|
buf := make([]byte, 1)
|
|
|
|
_, err = in.Read(buf)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// close it
|
|
|
|
err = in.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// read from the other dup - should produce no error as this
|
|
|
|
// file is now buffered
|
|
|
|
n, err := syscall.Read(fd2, buf)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, n)
|
|
|
|
|
|
|
|
// close the dup - should not produce an error
|
|
|
|
err = syscall.Close(fd2)
|
|
|
|
assert.NoError(t, err, "input/output error")
|
|
|
|
|
|
|
|
run.rm(t, "testdoubleclose")
|
|
|
|
}
|