ACP Implementation of PermissionsExt for Windows #152995
ACP Implementation of PermissionsExt for Windows #152995asder8215 wants to merge 1 commit intorust-lang:mainfrom
Conversation
|
|
This comment has been minimized.
This comment has been minimized.
00739a8 to
2daf248
Compare
This comment has been minimized.
This comment has been minimized.
2daf248 to
29689a6
Compare
This comment has been minimized.
This comment has been minimized.
29689a6 to
b752775
Compare
This comment has been minimized.
This comment has been minimized.
b752775 to
23a096f
Compare
library/std/src/os/windows/fs.rs
Outdated
| /// let mut permissions = Permissions::set_file_attributes(my_file_attr); | ||
| /// assert_eq!(permissions.mode(), my_file_attr); | ||
| /// ``` | ||
| pub trait PermissionsExt { |
There was a problem hiding this comment.
Permissions struct doesn't have a Sealed trait bound (the Unix version of PermissionsExt doesn't use a Sealed trait bound either), so I'm unsure if this should be done here.
There was a problem hiding this comment.
Any new structs like this should be sealed. The reason the Unix version of PermissionExt doesn't is an historical artefact and one we'd love to fix. Otherwise it makes adding any functions a breaking change (this can be worked around by adding PermissionExt2 but we'd rather avoid that if possible).
This comment has been minimized.
This comment has been minimized.
23a096f to
3ed985e
Compare
This comment has been minimized.
This comment has been minimized.
3ed985e to
696ce60
Compare
library/std/src/os/windows/fs.rs
Outdated
| /// | ||
| /// // readonly and hidden file attributes that we | ||
| /// // want to add to existing file | ||
| /// let my_file_attr = 0x1 | 0x2; |
There was a problem hiding this comment.
Given this is a public example, this should use constants instead of magic values, even if that means defining the constants right about this line.
library/std/src/os/windows/fs.rs
Outdated
| /// ); | ||
| /// Ok(()) | ||
| /// } | ||
| /// ``` |
There was a problem hiding this comment.
Actually this whole example seems much more like a test than an example. I think it's better for users if examples are kept short and to the point.
library/std/src/sys/fs/windows.rs
Outdated
| // According to SetFileAttributes, any other values | ||
| // passed to this should override FILE_ATTRIBUTE_NORMAL |
There was a problem hiding this comment.
Windows itself will override FILE_ATTRIBUTE_NORMAL if another attribute is present so I'm not sure it's necessary for us to do it. But if we do do it then I'd rather use a bitmask rather than repeating if self.normal() each time. Or maybe an internal-only helper function.
|
I'll get to updating this PR later today! To note, the doctest/example was inspired from the Unix version. I mirrored writing it in the way Unix doc for I'll try and see if I can apply the Thanks for taking a look at this PR @ChrisDenton! |
You should just be able implement
Yeah, after looking at the Unix example I do find that a bit verbose. In either case, considering the example is |
|
If there are no compilation errors, then yea I've added your feedback. Let me know if the doc comments for On a side note, I've been looking at |
This comment has been minimized.
This comment has been minimized.
I'm down to update the Unix example in a separate PR to reduce verbosity. As for testing the functions here, where would be the appropriate place to do that for this PR? Moreover, does CI here run Windows tests? I've been wondering that a bit because in my Reverse Ancestor PR, I have Windows tests there for paths with Prefix components, but I didn't see anything in CI that mentions that it ran my Windows test (and passes). |
|
You'll need to |
Oh okay, I could do that. I was worried that I may not be allowed to do that if it's a breaking change. |
b53af07 to
09f0506
Compare
This comment has been minimized.
This comment has been minimized.
09f0506 to
31b3b10
Compare
|
@ChrisDenton Apologies for the ping, is there anything else I should do or take care of for this PR? |
|
r? libs |
|
r? libs |
| // &mut self like how unix uses `set_mode()`? | ||
| // (Note to self: if we do the above, I need to make changes to | ||
| // the doc comments). | ||
| /// Sets the file attribute bits. |
There was a problem hiding this comment.
I would mirror the unix PermissionsExt (file_attributes/set_file_attributes/from_file_attributes).
library/std/src/sys/fs/windows.rs
Outdated
| @@ -1155,18 +1155,231 @@ fn to_u64(ft: &c::FILETIME) -> u64 { | |||
| (ft.dwLowDateTime as u64) | ((ft.dwHighDateTime as u64) << 32) | |||
| } | |||
|
|
|||
| #[allow(dead_code)] | |||
There was a problem hiding this comment.
I think we should, for this PR, drop all the dead code here. It sounds like that's for potential future exposure:
to be decided whether we use them or not
and since that seems like it ought to have its own ACP(s) I'd suggest deleting it from this PR, merging it (with the other comment on methods in the extension trait fixed), and then if you want the additional helpers that can be its own, likely separate, proposal.
There was a problem hiding this comment.
Alright, I've removed the dead code. I'll likely create an ACP or discussion in the tracking issue on if we should have additional helper functions to set/check for specific file attributes.
library/std/src/sys/fs/windows.rs
Outdated
|
|
||
| fn set_hidden(&mut self, hidden: bool) { | ||
| // According to SetFileAttributes, any other values | ||
| // passed to this should override FILE_ATTRIBUTE_NORMAL |
There was a problem hiding this comment.
I'm confused by the phrasing here. Why are we toggling the FILE_ATTRIBUTE_NORMAL bit? If we want to reset them, shouldn't that be &= !c::FILE_ATTRIBUTE_NORMAL?
The docs in https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfileattributesa suggest that other values will override normal (which makes sense, since it's actually the absence of other bits...) but do we need to do that in user code? Even if yes, it seems much clearer to do it by removing normal with &= !... as I outlined above.
If it's illegal to call SetFileAttributes with an all-zero attributes mask, then perhaps we should determine whether FILE_ATTRIBUTE_NORMAL is set or not just before the call, and internally store just the non-normal bits? It's not very clear to me why that's a special bit rather than just being part of the general mask.
There was a problem hiding this comment.
I'm confused by the phrasing here. Why are we toggling the FILE_ATTRIBUTE_NORMAL bit? If we want to reset them, shouldn't that be &= !c::FILE_ATTRIBUTE_NORMAL?
Yeah, that's my mistake. I think I tunnel visioned myself on how xor could 0 out values that are both 1, but I forgot that this sets values to 1 if we had 0 ^ 1. I've gotta remove all these functions besides readonly/set_readonly/file_attributes functions. &= !c::FILE_ATTRIBUTE_NORMAL is much clearer.
The docs in https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfileattributesa suggest that other values will override normal (which makes sense, since it's actually the absence of other bits...) but do we need to do that in user code? Even if yes, it seems much clearer to do it by removing normal with &= !... as I outlined above.
I think I was unsure if what they meant by override FILE_ATTRIBUTE_NORMAL, that our set functions should clear out FILE_ATTRIBUTE_NORMAL always and then set the accordingly flag, or that the presence and behavior of FILE_ATTRIBUTE_NORMAL is not acknowledged if other file attributes are set. I opted for the first one out of caution, but I think what you say here:
since it's actually the absence of other bits...
Makes sense to me and we may not need to do this set FILE_ATTRIBUTE_NORMAL in user code. I would also assume that in the SetFileAttributesA file attributes chart, the meaning behind FILE_ATTRIBUTE_NORMAL:
A file that does not have other attributes set. This attribute is valid only when used alone.
Implies that if we were to set a file with FILE_ATTRIBUTE_NORMAL attribute, it doesn't necessarily mean that the other file attributes are zeroed out.
|
Reminder, once the PR becomes ready for a review, use |
31b3b10 to
57010d7
Compare
This comment has been minimized.
This comment has been minimized.
57010d7 to
bd53742
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
bd53742 to
22199d8
Compare
…tilities to observe, set, and create a Permissions struct with certain file attributes
22199d8 to
782a214
Compare

View all comments
This PR implements the
PermissionsExtfor Windows ACP and adds file attribute methods inFilePermissionsstruct (to be decided whether we use them or not). See this tracking issue for further detail and links.I also added some comments in the code for clarifications about the ACP (e.g. whether we should have a
set_file_attributes()+from_file_attributes()method to mirror what unix'sPermissionsExtis doing).Also, some relevant links on this:
attribcommandNote: Apologies for the multiple forced push. I haven't set up my Windows VM up yet to compile and check the code, so I've been using the CI to help me with that.
r? @ChrisDenton