use super::procfs::set_permissions_through_proc_self_fd;
use crate::fs::{open, OpenOptions, Permissions};
use rustix::fs::{fchmod, Mode, RawMode};
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::{fs, io};
pub(crate) fn set_permissions_impl(
start: &fs::File,
path: &Path,
perm: Permissions,
) -> io::Result<()> {
let std_perm = perm.into_std(start)?;
let result = set_permissions_through_proc_self_fd(start, path, std_perm.clone());
if let Ok(()) = result {
return Ok(());
}
match open(start, path, OpenOptions::new().read(true)) {
Ok(file) => return set_file_permissions(&file, std_perm),
Err(err) => match rustix::io::Errno::from_io_error(&err) {
Some(rustix::io::Errno::ACCESS) => (),
_ => return Err(err),
},
}
match open(start, path, OpenOptions::new().write(true)) {
Ok(file) => return set_file_permissions(&file, std_perm),
Err(err) => match rustix::io::Errno::from_io_error(&err) {
Some(rustix::io::Errno::ACCESS) | Some(rustix::io::Errno::ISDIR) => (),
_ => return Err(err),
},
}
result
}
fn set_file_permissions(file: &fs::File, perm: fs::Permissions) -> io::Result<()> {
let mode = Mode::from_bits_truncate(perm.mode() as RawMode);
Ok(fchmod(file, mode)?)
}