Restore the behavior of remembering the CWD fd in the parser

This will be important for concurrent execution, because different parsers will
have different working directories.
This commit is contained in:
ridiculousfish 2023-07-09 13:02:23 -07:00 committed by David Adam
parent 289fbecaa9
commit 57afaf7fb2
3 changed files with 12 additions and 5 deletions

View file

@ -90,7 +90,7 @@ pub fn cd(parser: &mut parser_t, streams: &mut io_streams_t, args: &mut [&wstr])
errno::set_errno(Errno(0));
// We need to keep around the fd for this directory, in the parser.
let dir_fd = AutoCloseFd::new(wopen_cloexec(&norm_dir, O_RDONLY, 0));
let mut dir_fd = AutoCloseFd::new(wopen_cloexec(&norm_dir, O_RDONLY, 0));
if !(dir_fd.is_valid() && unsafe { fchdir(dir_fd.fd()) } == 0) {
// Some errors we skip and only report if nothing worked.
@ -116,11 +116,9 @@ pub fn cd(parser: &mut parser_t, streams: &mut io_streams_t, args: &mut [&wstr])
break;
}
// Port note: sending the AutocloseFd across the FFI interface requires additional work
// It's never actually used in the target parser object (perhaps will be after the port to Rust)
// Keep this commented until the parser is ported.
// Stash the fd for the cwd in the parser.
parser.pin().set_cwd_fd(autocxx::c_int(dir_fd.acquire()));
//parser.libdata().cwd_fd = std::make_shared<const autoclose_fd_t>(std::move(dir_fd));
parser.pin().set_var_and_fire(
&L!("PWD").to_ffi(),
EnvMode::EXPORT.bits() | EnvMode::GLOBAL.bits(),

View file

@ -210,6 +210,11 @@ completion_list_t parser_t::expand_argument_list(const wcstring &arg_list_src,
return result;
}
void parser_t::set_cwd_fd(int fd) {
assert(fd >= 0 && "Invalid fd");
this->libdata().cwd_fd = std::make_shared<autoclose_fd_t>(fd);
}
std::shared_ptr<parser_t> parser_t::shared() { return shared_from_this(); }
cancel_checker_t parser_t::cancel_checker() const {

View file

@ -487,6 +487,10 @@ class parser_t : public std::enable_shared_from_this<parser_t> {
/// Mark whether we should sync universal variables.
void set_syncs_uvars(bool flag) { syncs_uvars_ = flag; }
/// Set the given file descriptor as the working directory for this parser.
/// This acquires ownership.
void set_cwd_fd(int fd);
/// \return a shared pointer reference to this parser.
std::shared_ptr<parser_t> shared();