Skip to content

Commit d95c421

Browse files
committed
Auto merge of #152995 - asder8215:windows_permissions_ext, r=<try>
ACP Implementation of PermissionsExt for Windows try-job: aarch64-msvc-1
2 parents e22c616 + c567332 commit d95c421

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

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

Lines changed: 64 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,68 @@ 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+
/// #![feature(windows_permissions_ext)]
386+
/// use std::fs::Permissions;
387+
/// use std::os::windows::fs::PermissionsExt;
388+
///
389+
/// const FILE_ATTRIBUTE_SYSTEM: u32 = 0x4;
390+
/// const FILE_ATTRIBUTE_ARCHIVE: u32 = 0x20;
391+
/// let my_file_attr = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE;
392+
/// let mut permissions = Permissions::from_file_attributes(my_file_attr);
393+
/// assert_eq!(permissions.file_attributes(), my_file_attr);
394+
///
395+
/// const FILE_ATTRIBUTE_HIDDEN: u32 = 0x2;
396+
/// let new_file_attr = permissions.file_attributes() | FILE_ATTRIBUTE_HIDDEN;
397+
/// permissions.set_file_attributes(new_file_attr);
398+
/// assert_eq!(permissions.file_attributes(), new_file_attr);
399+
/// ```
400+
#[unstable(feature = "windows_permissions_ext", issue = "152956")]
401+
pub trait PermissionsExt: Sealed {
402+
/// Returns the file attribute bits.
403+
#[unstable(feature = "windows_permissions_ext", issue = "152956")]
404+
fn file_attributes(&self) -> u32;
405+
406+
/// Sets the file attribute bits.
407+
#[unstable(feature = "windows_permissions_ext", issue = "152956")]
408+
fn set_file_attributes(&mut self, mask: u32);
409+
410+
/// Creates a new instance from the given file attribute bits.
411+
#[unstable(feature = "windows_permissions_ext", issue = "152956")]
412+
fn from_file_attributes(mask: u32) -> Self;
413+
}
414+
415+
#[unstable(feature = "windows_permissions_ext", issue = "152956")]
416+
impl Sealed for fs::Permissions {}
417+
418+
#[unstable(feature = "windows_permissions_ext", issue = "152956")]
419+
impl PermissionsExt for fs::Permissions {
420+
fn file_attributes(&self) -> u32 {
421+
self.as_inner().file_attributes()
422+
}
423+
424+
fn set_file_attributes(&mut self, mask: u32) {
425+
*self = Permissions::from_inner(FromInner::from_inner(mask));
426+
}
427+
428+
fn from_file_attributes(mask: u32) -> Self {
429+
Permissions::from_inner(FromInner::from_inner(mask))
430+
}
431+
}
432+
371433
/// Windows-specific extensions to [`fs::Metadata`].
372434
///
373435
/// 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)