Skip to content

Commit e2aafcc

Browse files
committed
feat(fs): add File::set_permissions for Windows
1 parent 1b09ad7 commit e2aafcc

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

compio-fs/src/file.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use std::{future::Future, io, mem::ManuallyDrop, path::Path};
22

33
use compio_buf::{buf_try, BufResult, IntoInner, IoBuf, IoBufMut};
4-
use compio_driver::op::{BufResultExt, CloseFile, FileStat, ReadAt, Sync, WriteAt};
4+
use compio_driver::{
5+
op::{BufResultExt, CloseFile, FileStat, ReadAt, Sync, WriteAt},
6+
syscall,
7+
};
58
use compio_io::{AsyncReadAt, AsyncWriteAt};
69
use compio_runtime::{
710
impl_attachable, impl_try_as_raw_fd, Attacher, Runtime, TryAsRawFd, TryClone,
@@ -12,7 +15,7 @@ use {
1215
compio_driver::op::{ReadVectoredAt, WriteVectoredAt},
1316
};
1417

15-
use crate::{Metadata, OpenOptions};
18+
use crate::{Metadata, OpenOptions, Permissions};
1619

1720
/// A reference to an open file on the filesystem.
1821
///
@@ -78,6 +81,37 @@ impl File {
7881
res.map(|_| Metadata::from_stat(op.into_inner()))
7982
}
8083

84+
/// Changes the permissions on the underlying file.
85+
#[cfg(windows)]
86+
pub async fn set_permissions(&self, perm: Permissions) -> io::Result<()> {
87+
use windows_sys::Win32::Storage::FileSystem::{
88+
FileBasicInfo, SetFileInformationByHandle, FILE_BASIC_INFO,
89+
};
90+
91+
let fd = self.try_as_raw_fd()? as _;
92+
Runtime::current()
93+
.spawn_blocking(move || {
94+
let info = FILE_BASIC_INFO {
95+
CreationTime: 0,
96+
LastAccessTime: 0,
97+
LastWriteTime: 0,
98+
ChangeTime: 0,
99+
FileAttributes: perm.attrs,
100+
};
101+
syscall!(
102+
BOOL,
103+
SetFileInformationByHandle(
104+
fd,
105+
FileBasicInfo,
106+
&info as *const _ as _,
107+
std::mem::size_of::<FILE_BASIC_INFO>() as _
108+
)
109+
)?;
110+
Ok(())
111+
})
112+
.await
113+
}
114+
81115
async fn sync_impl(&self, datasync: bool) -> io::Result<()> {
82116
let op = Sync::new(self.try_as_raw_fd()?, datasync);
83117
Runtime::current().submit(op).await.0?;

compio-fs/src/metadata/windows.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::{
88
use compio_buf::{BufResult, IntoInner};
99
use compio_driver::{op::PathStat, syscall};
1010
use compio_runtime::Runtime;
11-
use widestring::U16CString;
1211
use windows_sys::Win32::{
1312
Foundation::FILETIME,
1413
Storage::FileSystem::{
@@ -256,7 +255,7 @@ impl FileType {
256255
/// Representation of the various permissions on a file.
257256
#[derive(Clone, PartialEq, Eq, Debug)]
258257
pub struct Permissions {
259-
attrs: u32,
258+
pub(crate) attrs: u32,
260259
}
261260

262261
impl Permissions {

0 commit comments

Comments
 (0)