|
4 | 4 |
|
5 | 5 | #![stable(feature = "rust1", since = "1.0.0")] |
6 | 6 |
|
7 | | -use crate::fs::{self, Metadata, OpenOptions}; |
| 7 | +use crate::fs::{self, Metadata, OpenOptions, Permissions}; |
8 | 8 | use crate::io::BorrowedCursor; |
9 | 9 | use crate::path::Path; |
10 | 10 | use crate::sealed::Sealed; |
11 | | -use crate::sys::{AsInner, AsInnerMut, IntoInner}; |
| 11 | +use crate::sys::{AsInner, AsInnerMut, FromInner, IntoInner}; |
12 | 12 | use crate::time::SystemTime; |
13 | 13 | use crate::{io, sys}; |
14 | 14 |
|
@@ -368,6 +368,68 @@ impl OpenOptionsExt2 for OpenOptions { |
368 | 368 | } |
369 | 369 | } |
370 | 370 |
|
| 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 | + |
371 | 433 | /// Windows-specific extensions to [`fs::Metadata`]. |
372 | 434 | /// |
373 | 435 | /// The data members that this trait exposes correspond to the members |
|
0 commit comments