-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
os: make DirFS implement ReadFile, ReadDir #53761
Comments
I think the difference is that In // If a file claims a small size, read at least 512 bytes.
// In particular, files in Linux's /proc claim size 0 but
// then do not work right if read in small pieces,
// so an initial read of 1 byte would not work correctly.
if size < 512 {
size = 512
} No such workaround exists in One fix would be for |
@ianlancetaylor It's almost like that but with a small difference: if stat returns |
I think we may be saying the same thing. That is the problem that the code in |
Yes, the workaround should do the trick, so making |
To clarify, will a new API be added here? func (dir dirFS) ReadFile(name string) ([]byte, error) {
if !fs.ValidPath(name) || runtime.GOOS == "windows" && containsAny(name, `\:`) {
return nil, &PathError{Op: "readfile", Path: name, Err: ErrInvalid}
}
return ReadFile(string(dir) + "/" + name)
} @ivan4th Please take a look, can this code solve your problem? |
I'm not sure this needs to go through the proposal process, but I guess it can't hurt. I can turn this issue into a proposal if the patch does indeed fix the problem. |
Change https://go.dev/cl/416775 mentions this issue: |
I suspect these were left out because ReadFile and ReadDir were in io/ioutil and os didn't want to depend on those. But now they've moved to os and they are easy to do. We probably should. |
This proposal has been added to the active column of the proposals project |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
If I'm reading the code changes in 4042b90 correctly, |
Thanks, reopening for ReadDir. |
Change https://go.dev/cl/498015 mentions this issue: |
Change https://go.dev/cl/499735 mentions this issue: |
Also add a missing </a> in the preceding section. For #53761 Change-Id: I8e64b86b5b32067f954d58cf9adf86cb4d2eeb2b Reviewed-on: https://go-review.googlesource.com/c/go/+/499735 Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com> Auto-Submit: Eli Bendersky <eliben@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Eli Bendersky <eliben@google.com>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
I'd expect
thread_sibling_list
file to be read correctly:There are no problems with
thread_sibling_list
on Ubuntu 20.04 / kernel 5.4.0:What did you see instead?
When code is run on Ubuntu 22.04 on Linux 5.15.0 the following output is produced:
Note that for
thread_siblings_list
, the empty string is returned.I tested it on my own machine with Ubuntu 22.04 and also on a Digital Ocean instance with Ubuntu 22.04:
Additional notes
The relevant part of
strace
looks like this:I suspect that the culprit is probably
O_NONBLOCK
combined that the fact that seeing the file size of0
in sysfsgo/src/io/fs/readfile.go
Lines 43 to 49 in 5c1a13e
fs.ReadFile
tries to read just one byte from itgo/src/io/fs/readfile.go
Lines 51 to 57 in 5c1a13e
The resulting
read()
syscall returns0
(perhaps due toO_NONBLOCK
?)and
file.Read()
returnsEOF
error.This is quite possibly a Linux kernel bug, nevertheless, it may affect many Go programs, as it's quite tempting to use
io/fs
to make sysfs-dependent code testable.The text was updated successfully, but these errors were encountered: