1
1
use std:: {
2
- ffi:: CString ,
3
2
io,
4
- os:: unix:: prelude:: { FileTypeExt , MetadataExt , OsStrExt , PermissionsExt } ,
3
+ os:: unix:: prelude:: { FileTypeExt , MetadataExt , PermissionsExt } ,
5
4
path:: Path ,
6
5
time:: { Duration , SystemTime } ,
7
6
} ;
8
7
9
8
use compio_buf:: { BufResult , IntoInner } ;
10
- use compio_driver:: op:: PathStat ;
9
+ use compio_driver:: { op:: PathStat , syscall } ;
11
10
use compio_runtime:: Runtime ;
12
11
12
+ use crate :: path_string;
13
+
13
14
async fn metadata_impl ( path : impl AsRef < Path > , follow_symlink : bool ) -> io:: Result < Metadata > {
14
- let path = CString :: new ( path. as_ref ( ) . as_os_str ( ) . as_bytes ( ) . to_vec ( ) ) . map_err ( |_| {
15
- io:: Error :: new (
16
- io:: ErrorKind :: InvalidInput ,
17
- "file name contained an unexpected NUL byte" ,
18
- )
19
- } ) ?;
15
+ let path = path_string ( path) ?;
20
16
let op = PathStat :: new ( path, follow_symlink) ;
21
17
let BufResult ( res, op) = Runtime :: current ( ) . submit ( op) . await ;
22
18
res. map ( |_| Metadata :: from_stat ( op. into_inner ( ) ) )
@@ -33,6 +29,17 @@ pub async fn symlink_metadata(path: impl AsRef<Path>) -> io::Result<Metadata> {
33
29
metadata_impl ( path, false ) . await
34
30
}
35
31
32
+ /// Changes the permissions found on a file or a directory.
33
+ pub async fn set_permissions ( path : impl AsRef < Path > , perm : Permissions ) -> io:: Result < ( ) > {
34
+ let path = path_string ( path) ?;
35
+ Runtime :: current ( )
36
+ . spawn_blocking ( move || {
37
+ syscall ! ( libc:: chmod( path. as_ptr( ) , perm. 0 ) ) ?;
38
+ Ok ( ( ) )
39
+ } )
40
+ . await
41
+ }
42
+
36
43
/// Metadata information about a file.
37
44
#[ derive( Clone ) ]
38
45
pub struct Metadata ( pub ( crate ) libc:: stat ) ;
0 commit comments