diff --git a/proxmox-tools/src/fd.rs b/proxmox-tools/src/fd.rs index 0b5ff05b..5490e9d3 100644 --- a/proxmox-tools/src/fd.rs +++ b/proxmox-tools/src/fd.rs @@ -2,6 +2,10 @@ use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; +use nix::fcntl::OFlag; +use nix::sys::stat::Mode; +use nix::NixPath; + /// Guard a raw file descriptor with a drop handler. This is mostly useful when access to an owned /// `RawFd` is required without the corresponding handler object (such as when only the file /// descriptor number is required in a closure which may be dropped instead of being executed). @@ -9,7 +13,8 @@ pub struct Fd(pub RawFd); impl Drop for Fd { fn drop(&mut self) { - if self.0 != -1 { + // `>= 0` instead of `!= -1` to also handle things like AT_FDCWD + if self.0 >= 0 { unsafe { libc::close(self.0); } @@ -36,3 +41,24 @@ impl FromRawFd for Fd { Self(fd) } } + +impl Fd { + pub const fn cwd() -> Self { + Self(libc::AT_FDCWD) + } + + pub fn open
(path: &P, oflag: OFlag, mode: Mode) -> Result