|
|
|
@ -29,3 +29,75 @@ impl PermissionsExt for std::fs::Permissions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature="chmod")]
|
|
|
|
|
mod chmod
|
|
|
|
|
{
|
|
|
|
|
use libc::{
|
|
|
|
|
fchmod,
|
|
|
|
|
chmod,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
|
path::Path,
|
|
|
|
|
io::{
|
|
|
|
|
self,
|
|
|
|
|
ErrorKind,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub trait FChmodExt
|
|
|
|
|
{
|
|
|
|
|
fn chmod(&mut self, mode: impl Into<u32>) -> io::Result<()>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FChmodExt for std::fs::File
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/// Perform `chmod` on this file to `mode`.
|
|
|
|
|
///
|
|
|
|
|
/// Mode can be anything that implements `Into<u32>`. `Permissions` does this, you can also pass raw `mode_t` values.
|
|
|
|
|
/// # Notes
|
|
|
|
|
/// If you pass raw `mode_t` that is outside the range (0..=0o777), any extra bits are ignored.
|
|
|
|
|
fn chmod(&mut self, mode: impl Into<u32>) -> io::Result<()>
|
|
|
|
|
{
|
|
|
|
|
use std::os::unix::io::*;
|
|
|
|
|
unsafe {
|
|
|
|
|
if fchmod(self.as_raw_fd(), mode.into() & 0o777) == 0 {
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
Err(io::Error::new(ErrorKind::Other, "fchmod failed"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait ChmodExt
|
|
|
|
|
{
|
|
|
|
|
fn chmod(&self, mode: impl Into<u32>) -> io::Result<()>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<P> ChmodExt for P
|
|
|
|
|
where P: AsRef<Path>
|
|
|
|
|
{
|
|
|
|
|
/// Perform `chmod` on this Path to `mode`.
|
|
|
|
|
///
|
|
|
|
|
/// Mode can be anything that implements `Into<u32>`. `Permissions` does this, you can also pass raw `mode_t` values.
|
|
|
|
|
/// # Notes
|
|
|
|
|
/// If you pass raw `mode_t` that is outside the range (0..=0o777), any extra bits are ignored.
|
|
|
|
|
fn chmod(&self, mode: impl Into<u32>) -> io::Result<()>
|
|
|
|
|
{
|
|
|
|
|
use std::os::unix::ffi::OsStrExt;
|
|
|
|
|
let bytes = self.as_ref().as_os_str().as_bytes();
|
|
|
|
|
unsafe {
|
|
|
|
|
let path = std::ffi::CString::new(bytes).map_err(|_| io::Error::new(ErrorKind::Other, "invalid path"))?;
|
|
|
|
|
if chmod(path.as_ptr(), mode.into() & 0o777) == 0 {
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
Err(io::Error::new(ErrorKind::Other, "chmod failed"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#[cfg(feature="chmod")]
|
|
|
|
|
pub use chmod::*;
|
|
|
|
|