cap_primitives/rustix/linux/fs/
procfs.rs1use crate::fs::OpenOptionsExt;
10use crate::fs::{
11 errors, open, read_link_unchecked, set_times_follow_unchecked, OpenOptions, SystemTimeSpec,
12};
13use io_lifetimes::{AsFd, AsFilelike};
14use rustix::fs::{chmodat, AtFlags, Mode, OFlags, RawMode};
15use rustix::path::DecInt;
16use rustix_linux_procfs::proc_self_fd;
17use std::os::unix::fs::PermissionsExt;
18use std::path::{Path, PathBuf};
19use std::{fs, io};
20
21pub(crate) fn get_path_from_proc_self_fd(file: &fs::File) -> io::Result<PathBuf> {
22 read_link_unchecked(
23 &proc_self_fd()?.as_filelike_view::<fs::File>(),
24 DecInt::from_fd(file).as_ref(),
25 PathBuf::new(),
26 )
27}
28
29pub(crate) fn set_permissions_through_proc_self_fd(
36 start: &fs::File,
37 path: &Path,
38 perm: fs::Permissions,
39) -> io::Result<()> {
40 let opath = open(
41 start,
42 path,
43 OpenOptions::new()
44 .read(true)
45 .custom_flags(OFlags::PATH.bits() as i32),
46 )?;
47
48 let dirfd = proc_self_fd()?;
49 let mode = Mode::from_bits(perm.mode() as RawMode).ok_or_else(errors::invalid_flags)?;
50 Ok(chmodat(
51 dirfd,
52 DecInt::from_fd(&opath),
53 mode,
54 AtFlags::empty(),
55 )?)
56}
57
58pub(crate) fn set_times_through_proc_self_fd(
59 start: &fs::File,
60 path: &Path,
61 atime: Option<SystemTimeSpec>,
62 mtime: Option<SystemTimeSpec>,
63) -> io::Result<()> {
64 let opath = open(
65 start,
66 path,
67 OpenOptions::new()
68 .read(true)
69 .custom_flags(OFlags::PATH.bits() as i32),
70 )?;
71
72 set_times_follow_unchecked(
77 proc_self_fd()?.as_fd(),
78 DecInt::from_fd(&opath).as_ref(),
79 atime,
80 mtime,
81 )
82}