Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 64 additions & 2 deletions library/std/src/os/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

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

use crate::fs::{self, Metadata, OpenOptions};
use crate::fs::{self, Metadata, OpenOptions, Permissions};
use crate::io::BorrowedCursor;
use crate::path::Path;
use crate::sealed::Sealed;
use crate::sys::{AsInner, AsInnerMut, IntoInner};
use crate::sys::{AsInner, AsInnerMut, FromInner, IntoInner};
use crate::time::SystemTime;
use crate::{io, sys};

Expand Down Expand Up @@ -368,6 +368,68 @@ impl OpenOptionsExt2 for OpenOptions {
}
}

/// Windows-specific extensions to [`fs::Permissions`]. This extension trait
/// provides extra utilities to shows what Windows file attributes are enabled
/// in [`Permissions`] and to manually set file attributes on [`Permissions`].
///
/// See Microsoft's [`File Attribute Constants`] page to know what file
/// attribute metadata are defined and stored on Windows files.
///
/// [`Permissions`]: fs::Permissions
/// [`File Attribute Constants`]:
/// https://learn.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
///
/// # Example
///
/// ```no_run
/// #![feature(windows_permissions_ext)]
/// use std::fs::Permissions;
/// use std::os::windows::fs::PermissionsExt;
///
/// const FILE_ATTRIBUTE_SYSTEM: u32 = 0x4;
/// const FILE_ATTRIBUTE_ARCHIVE: u32 = 0x20;
/// let my_file_attr = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE;
/// let mut permissions = Permissions::from_file_attributes(my_file_attr);
/// assert_eq!(permissions.file_attributes(), my_file_attr);
///
/// const FILE_ATTRIBUTE_HIDDEN: u32 = 0x2;
/// let new_file_attr = permissions.file_attributes() | FILE_ATTRIBUTE_HIDDEN;
/// permissions.set_file_attributes(new_file_attr);
/// assert_eq!(permissions.file_attributes(), new_file_attr);
/// ```
#[unstable(feature = "windows_permissions_ext", issue = "152956")]
pub trait PermissionsExt: Sealed {
/// Returns the file attribute bits.
#[unstable(feature = "windows_permissions_ext", issue = "152956")]
fn file_attributes(&self) -> u32;

/// Sets the file attribute bits.
Comment thread
asder8215 marked this conversation as resolved.
#[unstable(feature = "windows_permissions_ext", issue = "152956")]
fn set_file_attributes(&mut self, mask: u32);

/// Creates a new instance from the given file attribute bits.
#[unstable(feature = "windows_permissions_ext", issue = "152956")]
fn from_file_attributes(mask: u32) -> Self;
}

#[unstable(feature = "windows_permissions_ext", issue = "152956")]
impl Sealed for fs::Permissions {}

#[unstable(feature = "windows_permissions_ext", issue = "152956")]
impl PermissionsExt for fs::Permissions {
fn file_attributes(&self) -> u32 {
self.as_inner().file_attributes()
}

fn set_file_attributes(&mut self, mask: u32) {
*self = Permissions::from_inner(FromInner::from_inner(mask));
}

fn from_file_attributes(mask: u32) -> Self {
Permissions::from_inner(FromInner::from_inner(mask))
}
}

/// Windows-specific extensions to [`fs::Metadata`].
///
/// The data members that this trait exposes correspond to the members
Expand Down
10 changes: 10 additions & 0 deletions library/std/src/sys/fs/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,16 @@ impl FilePermissions {
self.attrs &= !c::FILE_ATTRIBUTE_READONLY;
}
}

pub fn file_attributes(&self) -> u32 {
self.attrs as u32
}
}

impl FromInner<u32> for FilePermissions {
fn from_inner(attrs: u32) -> FilePermissions {
FilePermissions { attrs }
}
}

impl FileTimes {
Expand Down
Loading