Skip to content

Commit 782a214

Browse files
committed
Implemented PermissionsExt ACP on Windows, which provides functions/utilities to observe, set, and create a Permissions struct with certain file attributes
1 parent b6100cc commit 782a214

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

library/std/src/os/windows/fs.rs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
#![stable(feature = "rust1", since = "1.0.0")]
66

7-
use crate::fs::{self, Metadata, OpenOptions};
7+
use crate::fs::{self, Metadata, OpenOptions, Permissions};
88
use crate::io::BorrowedCursor;
99
use crate::path::Path;
1010
use crate::sealed::Sealed;
11-
use crate::sys::{AsInner, AsInnerMut, IntoInner};
11+
use crate::sys::{AsInner, AsInnerMut, FromInner, IntoInner};
1212
use crate::time::SystemTime;
1313
use crate::{io, sys};
1414

@@ -368,6 +368,67 @@ impl OpenOptionsExt2 for OpenOptions {
368368
}
369369
}
370370

371+
/// Windows-specific extensions to [`fs::Permissions`]. This extension trait
372+
/// provides extra utilities to shows what Windows file attributes are enabled
373+
/// in [`Permissions`] and to manually set file attributes on [`Permissions`].
374+
///
375+
/// See Microsoft's [`File Attribute Constants`] page to know what file
376+
/// attribute metadata are defined and stored on Windows files.
377+
///
378+
/// [`Permissions`]: fs::Permissions
379+
/// [`File Attribute Constants`]:
380+
/// https://learn.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
381+
///
382+
/// # Example
383+
///
384+
/// ```no_run
385+
/// use std::fs::Permissions;
386+
/// use std::os::windows::fs::PermissionsExt;
387+
///
388+
/// const FILE_ATTRIBUTE_SYSTEM: u32 = 0x4;
389+
/// const FILE_ATTRIBUTE_ARCHIVE: u32 = 0x20;
390+
/// let my_file_attr = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE;
391+
/// let mut permissions = Permissions::from_file_attributes(my_file_attr);
392+
/// assert_eq!(permissions.file_attributes(), my_file_attr);
393+
///
394+
/// const FILE_ATTRIBUTE_HIDDEN: u32 = 0x2;
395+
/// let new_file_attr = permissions.file_attributes() | FILE_ATTRIBUTE_HIDDEN;
396+
/// permissions.set_file_attributes(new_file_attr);
397+
/// assert_eq!(permissions.file_attributes(), new_file_attr);
398+
/// ```
399+
#[unstable(feature = "windows_permissions_ext", issue = "152956")]
400+
pub trait PermissionsExt: Sealed {
401+
/// Returns the file attribute bits.
402+
#[unstable(feature = "windows_permissions_ext", issue = "152956")]
403+
fn file_attributes(&self) -> u32;
404+
405+
/// Sets the file attribute bits.
406+
#[unstable(feature = "windows_permissions_ext", issue = "152956")]
407+
fn set_file_attributes(&mut self, mask: u32);
408+
409+
/// Creates a new instance from the given file attribute bits.
410+
#[unstable(feature = "windows_permissions_ext", issue = "152956")]
411+
fn from_file_attributes(mask: u32) -> Self;
412+
}
413+
414+
#[unstable(feature = "windows_permissions_ext", issue = "152956")]
415+
impl Sealed for fs::Permissions {}
416+
417+
#[unstable(feature = "windows_permissions_ext", issue = "152956")]
418+
impl PermissionsExt for fs::Permissions {
419+
fn file_attributes(&self) -> u32 {
420+
self.as_inner().file_attributes()
421+
}
422+
423+
fn set_file_attributes(&mut self, mask: u32) {
424+
*self = Permissions::from_inner(FromInner::from_inner(mask));
425+
}
426+
427+
fn from_file_attributes(mask: u32) -> Self {
428+
Permissions::from_inner(FromInner::from_inner(mask))
429+
}
430+
}
431+
371432
/// Windows-specific extensions to [`fs::Metadata`].
372433
///
373434
/// The data members that this trait exposes correspond to the members

library/std/src/sys/fs/windows.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,16 @@ impl FilePermissions {
11671167
self.attrs &= !c::FILE_ATTRIBUTE_READONLY;
11681168
}
11691169
}
1170+
1171+
pub fn file_attributes(&self) -> u32 {
1172+
self.attrs as u32
1173+
}
1174+
}
1175+
1176+
impl FromInner<u32> for FilePermissions {
1177+
fn from_inner(attrs: u32) -> FilePermissions {
1178+
FilePermissions { attrs }
1179+
}
11701180
}
11711181

11721182
impl FileTimes {

0 commit comments

Comments
 (0)