Skip to content

Commit 1b09ad7

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

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

compio-fs/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,25 @@ pub mod named_pipe;
2121

2222
#[cfg(unix)]
2323
pub mod pipe;
24+
25+
#[cfg(windows)]
26+
pub(crate) fn path_string(
27+
path: impl AsRef<std::path::Path>,
28+
) -> std::io::Result<widestring::U16CString> {
29+
widestring::U16CString::from_os_str(path.as_ref().as_os_str()).map_err(|_| {
30+
std::io::Error::new(
31+
std::io::ErrorKind::InvalidInput,
32+
"file name contained an unexpected NUL byte",
33+
)
34+
})
35+
}
36+
37+
#[cfg(unix)]
38+
pub(crate) fn path_string(path: impl AsRef<std::path::Path>) -> std::io::Result<std::ffi::CString> {
39+
CString::new(path.as_ref().as_os_str().as_bytes().to_vec()).map_err(|_| {
40+
io::Error::new(
41+
io::ErrorKind::InvalidInput,
42+
"file name contained an unexpected NUL byte",
43+
)
44+
})
45+
}

compio-fs/src/metadata/windows.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,21 @@ use std::{
66
};
77

88
use compio_buf::{BufResult, IntoInner};
9-
use compio_driver::op::PathStat;
9+
use compio_driver::{op::PathStat, syscall};
1010
use compio_runtime::Runtime;
1111
use widestring::U16CString;
1212
use windows_sys::Win32::{
1313
Foundation::FILETIME,
1414
Storage::FileSystem::{
15-
BY_HANDLE_FILE_INFORMATION, FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_READONLY,
16-
FILE_ATTRIBUTE_REPARSE_POINT,
15+
SetFileAttributesW, BY_HANDLE_FILE_INFORMATION, FILE_ATTRIBUTE_DIRECTORY,
16+
FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_REPARSE_POINT,
1717
},
1818
};
1919

20+
use crate::path_string;
21+
2022
async fn metadata_impl(path: impl AsRef<Path>, follow_symlink: bool) -> io::Result<Metadata> {
21-
let path = U16CString::from_os_str(path.as_ref().as_os_str()).map_err(|_| {
22-
io::Error::new(
23-
io::ErrorKind::InvalidInput,
24-
"file name contained an unexpected NUL byte",
25-
)
26-
})?;
23+
let path = path_string(path)?;
2724
let op = PathStat::new(path, follow_symlink);
2825
let BufResult(res, op) = Runtime::current().submit(op).await;
2926
res.map(|_| Metadata::from_path_stat(op.into_inner()))
@@ -40,6 +37,17 @@ pub async fn symlink_metadata(path: impl AsRef<Path>) -> io::Result<Metadata> {
4037
metadata_impl(path, false).await
4138
}
4239

40+
/// Changes the permissions found on a file or a directory.
41+
pub async fn set_permissions(path: impl AsRef<Path>, perm: Permissions) -> io::Result<()> {
42+
let path = path_string(path)?;
43+
Runtime::current()
44+
.spawn_blocking(move || {
45+
syscall!(BOOL, SetFileAttributesW(path.as_ptr(), perm.attrs))?;
46+
Ok(())
47+
})
48+
.await
49+
}
50+
4351
const fn create_u64(high: u32, low: u32) -> u64 {
4452
((high as u64) << 32) | (low as u64)
4553
}

compio-fs/src/open_options/windows.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::{io, path::Path, ptr::null};
22

33
use compio_driver::{op::OpenFile, FromRawFd, RawFd};
44
use compio_runtime::Runtime;
5-
use widestring::U16CString;
65
use windows_sys::Win32::{
76
Foundation::{ERROR_INVALID_PARAMETER, GENERIC_READ, GENERIC_WRITE},
87
Security::SECURITY_ATTRIBUTES,
@@ -13,7 +12,7 @@ use windows_sys::Win32::{
1312
},
1413
};
1514

16-
use crate::File;
15+
use crate::{path_string, File};
1716

1817
#[derive(Clone, Debug)]
1918
pub struct OpenOptions {
@@ -131,12 +130,7 @@ impl OpenOptions {
131130
}
132131

133132
pub async fn open(&self, p: impl AsRef<Path>) -> io::Result<File> {
134-
let p = U16CString::from_os_str(p.as_ref().as_os_str()).map_err(|_| {
135-
io::Error::new(
136-
io::ErrorKind::InvalidInput,
137-
"file name contained an unexpected NUL byte",
138-
)
139-
})?;
133+
let p = path_string(p)?;
140134
let op = OpenFile::new(
141135
p,
142136
self.get_access_mode()?,

0 commit comments

Comments
 (0)