From 4d412d933e682e1d6fcb3bac468397e4036bf659 Mon Sep 17 00:00:00 2001 From: Sayafdine Said Date: Mon, 16 Sep 2024 18:27:27 +0200 Subject: [PATCH] fix(context): use custom open function --- rust/src/context.rs | 18 +++++++++--------- rust/src/utils.rs | 2 ++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/rust/src/context.rs b/rust/src/context.rs index 39b01b82..86197ce9 100644 --- a/rust/src/context.rs +++ b/rust/src/context.rs @@ -1,5 +1,5 @@ use nix::{ - fcntl::{open, OFlag}, + fcntl::OFlag, sys::{ socket::{bind, socket, SockFlag, UnixAddr}, stat::{lstat, mknod, mode_t, umask, Mode, SFlag}, @@ -25,7 +25,7 @@ use strum_macros::EnumIter; use crate::{ config::{Config, DummyAuthEntry, FeaturesConfig}, - utils::{chmod, lchmod, symlink}, + utils::{chmod, lchmod, open, symlink}, }; /// File type, mainly used with [TestContext::create]. @@ -375,7 +375,9 @@ impl FileBuilder { let path = self.final_path(); match self.file_type { - FileType::Regular => open(&path, OFlag::O_CREAT, mode).and_then(close), + FileType::Regular => { + open(&path, OFlag::O_CREAT, mode).and_then(|fd| close(fd.as_raw_fd())) + } FileType::Dir => mkdir(&path, mode), FileType::Fifo => mkfifo(&path, mode), FileType::Block => mknod(&path, SFlag::S_IFBLK, mode, 0), @@ -423,13 +425,11 @@ impl FileBuilder { OFlag::O_CREAT | oflags, self.mode.unwrap_or_else(|| Mode::from_bits_truncate(0o644)), ) - // SAFETY: The file descriptor was initialized only by open and isn't used anywhere else, leaving the ownership - .map(|fd| (path, unsafe { OwnedFd::from_raw_fd(fd) })) + .map(|fd| (path, fd)) } - _ => self.create().and_then(|p| { - // SAFETY: The file descriptor was initialized only by open and isn't used anywhere else, leaving the ownership - open(&p, oflags, Mode::empty()).map(|fd| (p, unsafe { OwnedFd::from_raw_fd(fd) })) - }), + _ => self + .create() + .and_then(|p| open(&p, oflags, Mode::empty()).map(|fd| (p, fd))), } } diff --git a/rust/src/utils.rs b/rust/src/utils.rs index 48bf93b3..3bd459db 100644 --- a/rust/src/utils.rs +++ b/rust/src/utils.rs @@ -90,5 +90,7 @@ pub fn lchflags( /// Wrapper for open which returns [`Ownedfd`] instead of [`RawFd`]. pub fn open(path: &P, oflag: OFlag, mode: Mode) -> nix::Result { + // SAFETY: The file descriptor was initialized only by open and isn't used anywhere else, + // leaving the ownership to the caller. nix::fcntl::open(path, oflag, mode).map(|fd| unsafe { OwnedFd::from_raw_fd(fd) }) }