-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Remove all fs.<foo>Absolute usages throughout the Zig codebase
#16743
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -967,7 +967,7 @@ pub fn getUserInfo(name: []const u8) !UserInfo { | |||||||||||||||||
| /// TODO this reads /etc/passwd. But sometimes the user/id mapping is in something else | ||||||||||||||||||
| /// like NIS, AD, etc. See `man nss` or look at an strace for `id myuser`. | ||||||||||||||||||
| pub fn posixGetUserInfo(name: []const u8) !UserInfo { | ||||||||||||||||||
| const file = try std.fs.openFileAbsolute("/etc/passwd", .{}); | ||||||||||||||||||
| const file = try std.fs.cwd().openFile("/etc/passwd", .{}); | ||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a good example of something that's not an improvement. The code before will use the Another way to put this is that the code before specified the intent more precisely, while the code after specifies intent less precisely. It is for this reason that I think the "absolute" variants of fs functions should continue to exist.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is not true in this case: Lines 2761 to 2764 in f40f81c
The only
All the rest look just like
I came to the opposite conclusion when writing this PR. From #16736 (comment):
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll also add that on some architectures, non
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It would be an improvement if the implementation were changed to match what I said, but it doesn't matter too much if the OS treats it the same. More importantly, it is an impedance mismatch to call pub fn cwd() Dir {
if (builtin.os.tag == .windows) {
return Dir{ .fd = os.windows.peb().ProcessParameters.CurrentDirectory.Handle };
} else if (builtin.os.tag == .wasi) {
return std.options.wasiCwd();
} else {
return Dir{ .fd = os.AT.FDCWD };
}
}We can observe some problems. On Windows, this lowers ultimately to some inline assembly (that is apparently incorrectly marked as having side effects, see #18077 that I just opened) that should not be emitted when accessing an absolute path. On WASI it could cause all sorts of unintended problems because it hooks into a user-overridable function for getting the current working directory. These things are minor issues, but they are the consequences of specifying the wrong intent. I don't really understand your second point. The functions dealing with absolute paths assert that the parameter is an absolute path, so I think you have it precisely backwards - using the absolute functions is the way to make intentions more explicit.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I understand where you're coming from better now. I was mostly focused on the potential downstream benefits of there being one obvious way of doing Before I close this, what are your thoughts on this pattern: Lines 1077 to 1080 in ac95cfe
Do you see that as something that should be done more, or should that be replaced with
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that pattern should always be replaced with |
||||||||||||||||||
| defer file.close(); | ||||||||||||||||||
|
|
||||||||||||||||||
| const reader = file.reader(); | ||||||||||||||||||
|
|
@@ -1207,7 +1207,7 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize { | |||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| fn totalSystemMemoryLinux() !usize { | ||||||||||||||||||
| var file = try std.fs.openFileAbsoluteZ("/proc/meminfo", .{}); | ||||||||||||||||||
| var file = try std.fs.cwd().openFileZ("/proc/meminfo", .{}); | ||||||||||||||||||
| defer file.close(); | ||||||||||||||||||
| var buf: [50]u8 = undefined; | ||||||||||||||||||
| const amt = try file.read(&buf); | ||||||||||||||||||
|
|
||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
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 TODO was added in fd067fb. The current code no longer used that workaround, so this TODO didn't seem relevant anymore.