Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sysfs: reopening file doesn't update fd #2319

Merged
merged 8 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions internal/sysfs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,6 @@ func seek(s io.Seeker, offset int64, whence int) (int64, experimentalsys.Errno)
return newOffset, experimentalsys.UnwrapOSError(err)
}

// reopenFile allows re-opening a file for reasons such as applying flags or
// directory iteration.
type reopenFile func() experimentalsys.Errno
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't being used anywhere. The compile time check is not very useful, tbh.


// compile-time check to ensure fsFile.reopen implements reopenFile.
var _ reopenFile = (*fsFile)(nil).reopen

// reopen implements the same method as documented on reopenFile.
func (f *fsFile) reopen() experimentalsys.Errno {
_ = f.close()
Expand Down
45 changes: 45 additions & 0 deletions internal/sysfs/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,50 @@ func TestWriteFdNonblock(t *testing.T) {
t.Fatal("writeFd should return EAGAIN at some point")
}

func TestFileReopenFileUpdatesFD(t *testing.T) {
tmpDir := t.TempDir()
path := path.Join(tmpDir, "file")

// Open the file twice. Closing the first file, frees its fd.
// Then reopening the second file will take over the first fd.
// If reopening the file doesn't update the fd, they won't match.
f0 := requireOpenFile(t, path, experimentalsys.O_RDWR|experimentalsys.O_CREAT, 0o600)
f1 := requireOpenFile(t, path, experimentalsys.O_RDWR, 0o600)
defer f1.Close()
f0.Close()

of, ok := f1.(*osFile)
require.True(t, ok)

require.EqualErrno(t, 0, of.reopen())
require.Equal(t, of.file.Fd(), of.fd)
}

func TestFileReopenFileChecksSameFile(t *testing.T) {
tmpDir := t.TempDir()
path := path.Join(tmpDir, "file")

f := requireOpenFile(t, path, experimentalsys.O_RDWR|experimentalsys.O_CREAT, 0o600)
defer f.Close()

require.NoError(t, os.Remove(path))

of, ok := f.(*osFile)
require.True(t, ok)

// Path does not exist anymore.
require.EqualErrno(t, experimentalsys.ENOENT, of.reopen())
require.Equal(t, of.file.Fd(), of.fd)

tmp, err := os.Create(path)
require.NoError(t, err)
defer tmp.Close()

// Path exists, but is not the same file.
require.EqualErrno(t, experimentalsys.ENOENT, of.reopen())
require.Equal(t, of.file.Fd(), of.fd)
}

func TestFileSetAppend(t *testing.T) {
tmpDir := t.TempDir()

Expand All @@ -124,6 +168,7 @@ func TestFileSetAppend(t *testing.T) {

// Open without APPEND.
f, errno := OpenOSFile(fPath, experimentalsys.O_RDWR, 0o600)
defer f.Close()
require.EqualErrno(t, 0, errno)
require.False(t, f.IsAppend())

Expand Down
30 changes: 24 additions & 6 deletions internal/sysfs/osfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,10 @@ func (f *osFile) SetAppend(enable bool) (errno experimentalsys.Errno) {
f.flag &= ^(experimentalsys.O_CREAT | experimentalsys.O_TRUNC)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't needlessly update the file/flags.


// appendMode (bool) cannot be changed later, so we have to re-open the
// file. https://github.com/golang/go/blob/go1.20/src/os/file_unix.go#L60
// file. https://github.com/golang/go/blob/go1.23/src/os/file_unix.go#L60
return fileError(f, f.closed, f.reopen())
}

// compile-time check to ensure osFile.reopen implements reopenFile.
var _ reopenFile = (*osFile)(nil).reopen

func (f *osFile) reopen() (errno experimentalsys.Errno) {
// Clear any create flag, as we are re-opening, not re-creating.
f.flag &= ^experimentalsys.O_CREAT
Expand All @@ -116,11 +113,17 @@ func (f *osFile) reopen() (errno experimentalsys.Errno) {
}
}

_ = f.close()
f.file, errno = OpenFile(f.path, f.flag, f.perm)
file, errno := OpenFile(f.path, f.flag, f.perm)
if errno != 0 {
return errno
}
errno = f.checkSameFile(file)
if errno != 0 {
return errno
}
_ = f.close()
f.file = file
f.fd = file.Fd()

if !isDir {
_, err = f.file.Seek(offset, io.SeekStart)
Expand All @@ -132,6 +135,21 @@ func (f *osFile) reopen() (errno experimentalsys.Errno) {
return 0
}

func (f *osFile) checkSameFile(osf *os.File) experimentalsys.Errno {
fi1, err := f.file.Stat()
if err != nil {
return experimentalsys.UnwrapOSError(err)
}
fi2, err := osf.Stat()
if err != nil {
return experimentalsys.UnwrapOSError(err)
}
if os.SameFile(fi1, fi2) {
return 0
}
return experimentalsys.ENOENT
}

// IsNonblock implements the same method as documented on fsapi.File
func (f *osFile) IsNonblock() bool {
return isNonblock(f)
Expand Down
4 changes: 1 addition & 3 deletions sys/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ type EpochNanos = int64
// # Notes
//
// - This is used for WebAssembly ABI emulating the POSIX `stat` system call.
// Fields included are required for WebAssembly ABI including wasip1
// (a.k.a. wasix) and wasi-filesystem (a.k.a. wasip2). See
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/stat.html
// See https://pubs.opengroup.org/onlinepubs/9699919799/functions/stat.html
// - Fields here are required for WebAssembly ABI including wasip1
// (a.k.a. wasix) and wasi-filesystem (a.k.a. wasip2).
// - This isn't the same as syscall.Stat_t because wazero supports Windows,
Expand Down
Loading