sys: adapt to IO Safety changes in rustc

`OwnedFd`s are now (rustc 1.80+) checked for validity when dropped in a debug
build, to catch usage after closing. Unfortunately those checks don't account
for the special value `AT_FDCWD` (-100) which is not a "real" FD, but a magic
constant used by many libc functions to signify operations starting at the
current working directory.

changing our `cwd` helper to open the CWD for real, instead of just returning
the magic value that pretends to be an FD, works around those limitations with
the least API churn.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
This commit is contained in:
Fabian Grünbichler 2024-08-05 13:59:07 +02:00 committed by Wolfgang Bumiller
parent 9d921901d3
commit dc0b9dec4a
2 changed files with 5 additions and 5 deletions
proxmox-sys/src

@ -2,9 +2,9 @@
use std::os::unix::io::AsRawFd;
use nix::fcntl::OFlag;
use nix::sys::stat::Mode;
use nix::NixPath;
use nix::{fcntl::OFlag, sys::stat};
use std::os::unix::io::{FromRawFd, OwnedFd, RawFd};
@ -23,8 +23,8 @@ pub fn change_cloexec(fd: RawFd, on: bool) -> Result<(), anyhow::Error> {
Ok(())
}
pub fn cwd() -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(libc::AT_FDCWD) }
pub fn cwd() -> Result<OwnedFd, nix::Error> {
open(".", OFlag::O_DIRECTORY, stat::Mode::empty())
}
pub fn open<P>(path: &P, oflag: OFlag, mode: Mode) -> Result<OwnedFd, nix::Error>

@ -120,7 +120,7 @@ fn create_path_do(
}
Some(Component::CurDir) => {
let _ = iter.next();
crate::fd::cwd()
crate::fd::cwd()?
}
Some(Component::ParentDir) => {
let _ = iter.next();
@ -128,7 +128,7 @@ fn create_path_do(
}
Some(Component::Normal(_)) => {
// simply do not advance the iterator, heavy lifting happens in create_path_at_do()
crate::fd::cwd()
crate::fd::cwd()?
}
None => bail!("create_path on empty path?"),
};