From 2ecbdb9ae7f2bb8fc37504c9c108b503720e385f Mon Sep 17 00:00:00 2001 From: Jonathan Krebs Date: Mon, 15 Apr 2024 11:32:23 +0200 Subject: [PATCH] cleanup: fds::open_dir - remove mode argument [w]open_dir does not pass O_CREAT, so the mode argument to open is never used. also, O_CREAT | O_DIRECTORY could not be used (portably) to create a directory. (on POSIX does not specify what should happen, on Linux it is EINVAL.) --- src/builtins/cd.rs | 3 +-- src/fds.rs | 8 ++++---- src/parser.rs | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/builtins/cd.rs b/src/builtins/cd.rs index 040338813..71f0f69ed 100644 --- a/src/builtins/cd.rs +++ b/src/builtins/cd.rs @@ -9,7 +9,6 @@ use crate::{ }; use errno::Errno; use libc::{fchdir, EACCES, ELOOP, ENOENT, ENOTDIR, EPERM}; -use nix::sys::stat::Mode; use std::{os::fd::AsRawFd, sync::Arc}; // The cd builtin. Changes the current directory to the one specified or to $HOME if none is @@ -87,7 +86,7 @@ pub fn cd(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) -> Optio errno::set_errno(Errno(0)); - let res = wopen_dir(&norm_dir, Mode::empty()).map_err(|err| err as i32); + let res = wopen_dir(&norm_dir).map_err(|err| err as i32); let res = res.and_then(|fd| { if unsafe { fchdir(fd.as_raw_fd()) } == 0 { diff --git a/src/fds.rs b/src/fds.rs index 03ebad36f..6ea5c6d10 100644 --- a/src/fds.rs +++ b/src/fds.rs @@ -261,13 +261,13 @@ pub fn open_cloexec(path: &CStr, flags: OFlag, mode: nix::sys::stat::Mode) -> ni /// Wide character version of open_dir() that also sets the close-on-exec flag (atomically when /// possible). -pub fn wopen_dir(pathname: &wstr, mode: nix::sys::stat::Mode) -> nix::Result { - open_dir(wcs2zstring(pathname).as_c_str(), mode) +pub fn wopen_dir(pathname: &wstr) -> nix::Result { + open_dir(wcs2zstring(pathname).as_c_str()) } /// Narrow version of wopen_dir(). -pub fn open_dir(path: &CStr, mode: nix::sys::stat::Mode) -> nix::Result { - open_cloexec(path, OFlag::O_RDONLY | OFlag::O_DIRECTORY, mode).map(OwnedFd::from) +pub fn open_dir(path: &CStr) -> nix::Result { + open_cloexec(path, OFlag::O_RDONLY | OFlag::O_DIRECTORY, nix::sys::stat::Mode::empty()).map(OwnedFd::from) } /// Close a file descriptor `fd`, retrying on EINTR. diff --git a/src/parser.rs b/src/parser.rs index 2478146b9..99ed6ee8c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -352,7 +352,7 @@ impl Parser { global_event_blocks: AtomicU64::new(0), }); - match open_dir(CStr::from_bytes_with_nul(b".\0").unwrap(), Mode::empty()) { + match open_dir(CStr::from_bytes_with_nul(b".\0").unwrap()) { Ok(fd) => { result.libdata_mut().cwd_fd = Some(Arc::new(fd)); }