[pbs-devel] [PATCH proxmox 1/2] sys: adapt to IO Safety changes in rustc
Fabian Grünbichler
f.gruenbichler at proxmox.com
Mon Aug 5 13:59:07 CEST 2024
`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 at proxmox.com>
---
technically the changed return type is a breaking change, but see next
patch..
proxmox-sys/src/fd.rs | 6 +++---
proxmox-sys/src/fs/dir.rs | 4 ++--
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/proxmox-sys/src/fd.rs b/proxmox-sys/src/fd.rs
index ad12e663..073dd08d 100644
--- a/proxmox-sys/src/fd.rs
+++ b/proxmox-sys/src/fd.rs
@@ -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>
diff --git a/proxmox-sys/src/fs/dir.rs b/proxmox-sys/src/fs/dir.rs
index 807a00ad..a7a95b02 100644
--- a/proxmox-sys/src/fs/dir.rs
+++ b/proxmox-sys/src/fs/dir.rs
@@ -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?"),
};
--
2.39.2
More information about the pbs-devel
mailing list