-
Notifications
You must be signed in to change notification settings - Fork 259
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
Conversation
Signed-off-by: Nuno Cruces <[email protected]>
Signed-off-by: Nuno Cruces <[email protected]>
Signed-off-by: Nuno Cruces <[email protected]>
Signed-off-by: Nuno Cruces <[email protected]>
Signed-off-by: Nuno Cruces <[email protected]>
Signed-off-by: Nuno Cruces <[email protected]>
Signed-off-by: Nuno Cruces <[email protected]>
Signed-off-by: Nuno Cruces <[email protected]>
@@ -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 |
There was a problem hiding this comment.
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.
var err error | ||
f.file, err = f.fs.Open(f.name) | ||
return experimentalsys.UnwrapOSError(err) | ||
func (f *fsFile) rewindDir() experimentalsys.Errno { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fsFile
only really supports reopening dirs, to rewind them.
Own that, check that the reopened file is still a dir.
@@ -83,21 +83,12 @@ func (f *osFile) SetAppend(enable bool) (errno experimentalsys.Errno) { | |||
f.flag &= ^experimentalsys.O_APPEND | |||
} | |||
|
|||
// Clear any create or trunc flag, as we are re-opening, not re-creating. | |||
f.flag &= ^(experimentalsys.O_CREAT | experimentalsys.O_TRUNC) |
There was a problem hiding this comment.
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.
awesome detective work! |
Fixes #2318.
Reopening the file was erroneously not updating the
fd
field.Also, reopening is risky, because the file have been deleted/renamed (yes, even on Windows, because we're using
FILE_SHARE_DELETE
).In theory, on POSIX, we'd never need to reopen (hence why WASI provides this, it tries to offer much of what POSIX allows, neglecting that lots of it is hard to emulate). But... reopen is only is only really used in two places: seeking directories (to rewind them), and setting the file's append flag.
For directory rewind, it's already just a fallback:
wazero/internal/sysfs/osfile.go
Lines 196 to 209 in 6bb9899
If seeking works, it's used. So, no point in trying to "fix" that one.
For setting the append flag, reopen is always used, for good reason:
wazero/internal/sysfs/osfile.go
Lines 78 to 92 in 6bb9899
That link is still valid in 1.23. Go's
os.File
caches this, most importantly, to return a specific error onWriteAt
. There's a good reason for this. The Open Group specifies thatpwrite
should work with, and ignore,O_APPEND
, but Linux does the opposite and appends, which is a bug.So (IMO) we do need to reopen for
SetAppend
, and the best we can do, is check if the new file is still the same file, and error out otherwise.That's what this PR does for
osFile
:fd
not being updated issueFor
fsFile
, this is only used to rewind directories, so: