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

posix: access/accessZ/faccessat/faccessatZ can return AccessDenied or PermissionDenied #22957

Merged
merged 1 commit into from
Feb 21, 2025

Conversation

rootbeer
Copy link
Contributor

EACCES is returned if the file mode bit (i.e., user/group/other rwx bits) disallow access (mapped to error.AccessDenied). EPERM is returned if something else denies access (immutable bit, SELinux, capabilities, etc) and is mapped to error.PermissionDenied. This somewhat subtle distinction in errors is part of POSIX, and can happen in practice on Linux or MacOS.

This PR is effectively an update/simplification of (abandoned) PR #19193. See #16782 for deeper problems with the AccessDenied/PermissionDenied duopoly in Zig.

Tested locally with an immutable file.

Fixes #22733 and #19162.

@alexrp
Copy link
Member

alexrp commented Feb 20, 2025

What do you think about making this PR map both EPERM and EACCES to PermissionDenied for consistency, and then doing a follow-up change that separates PermissionDenied and AccessDenied across the whole standard library? I don't really love the idea of only doing it for these functions in particular.

@rootbeer
Copy link
Contributor Author

What do you think about making this PR map both EPERM and EACCES to PermissionDenied for consistency, and then doing a follow-up change that separates PermissionDenied and AccessDenied across the whole standard library? I don't really love the idea of only doing it for these functions in particular.

I will make this map both to PermissionDenied for now if that's the least-resistance change.

See #16782 for some notes on the the deeper issues when separating the errors to match the errnos. Its straightforward to directly map each errno to the corresponding Zig error through the std.posix.* calls. However, its not clear to me if the two errors should propagate up through all the std.fs.* and std.os.* APIs too. I can work through it if you're willing to make a call as to which direction the higher layers should go (that is: just one error for all disallowed operations or both AccessDenied/PermissionDenied errors can be returned).

… PermissionDenied

`EACCES` is returned if the file mode bit (i.e., user/group/other rwx
bits) disallow access.  `EPERM` is returned if something else denies
access (immutable bit, SELinux, capabilities, etc).  This somewhat subtle
no-access distinction is part of POSIX.  For now map both to
`error.PermissionDenied` to keep the error signature unchanged.  See
duopoly.

This PR is effecitvely an update/simplification of PR ziglang#19193.

Tested locally with an immutable file.

Fixes ziglang#22733 and ziglang#19162.
@alexrp
Copy link
Member

alexrp commented Feb 20, 2025

See #16782 for some notes on the the deeper issues when separating the errors to match the errnos. Its straightforward to directly map each errno to the corresponding Zig error through the std.posix.* calls. However, its not clear to me if the two errors should propagate up through all the std.fs.* and std.os.* APIs too. I can work through it if you're willing to make a call as to which direction the higher layers should go (that is: just one error for all disallowed operations or both AccessDenied/PermissionDenied errors can be returned).

I think having both errors in std.fs is the right call. After all, the std.fs layer has other system-specific errors like InvalidUtf8, InvalidWtf8, BadPathName, NetworkNotFound, AntivirusInterference, ...

@alexrp alexrp enabled auto-merge (rebase) February 20, 2025 21:15
@alexrp alexrp merged commit 8d9bb97 into ziglang:master Feb 21, 2025
9 checks passed
@rootbeer rootbeer deleted the access-immutable branch February 21, 2025 06:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

std.fs.accessAbsolute when accessing forbidden file returns error.Unexpected but also prints stack trace
2 participants