|
1 | 1 | use std::{future::Future, io, mem::ManuallyDrop, path::Path};
|
2 | 2 |
|
3 | 3 | 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 | +}; |
5 | 8 | use compio_io::{AsyncReadAt, AsyncWriteAt};
|
6 | 9 | use compio_runtime::{
|
7 | 10 | impl_attachable, impl_try_as_raw_fd, Attacher, Runtime, TryAsRawFd, TryClone,
|
|
12 | 15 | compio_driver::op::{ReadVectoredAt, WriteVectoredAt},
|
13 | 16 | };
|
14 | 17 |
|
15 |
| -use crate::{Metadata, OpenOptions}; |
| 18 | +use crate::{Metadata, OpenOptions, Permissions}; |
16 | 19 |
|
17 | 20 | /// A reference to an open file on the filesystem.
|
18 | 21 | ///
|
@@ -78,6 +81,37 @@ impl File {
|
78 | 81 | res.map(|_| Metadata::from_stat(op.into_inner()))
|
79 | 82 | }
|
80 | 83 |
|
| 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 | + |
81 | 115 | async fn sync_impl(&self, datasync: bool) -> io::Result<()> {
|
82 | 116 | let op = Sync::new(self.try_as_raw_fd()?, datasync);
|
83 | 117 | Runtime::current().submit(op).await.0?;
|
|
0 commit comments