diff --git a/fish-rust/src/builtins/cd.rs b/fish-rust/src/builtins/cd.rs index 5d76253e5..9941f18a2 100644 --- a/fish-rust/src/builtins/cd.rs +++ b/fish-rust/src/builtins/cd.rs @@ -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(std::move(dir_fd)); parser.pin().set_var_and_fire( &L!("PWD").to_ffi(), EnvMode::EXPORT.bits() | EnvMode::GLOBAL.bits(), diff --git a/src/parser.cpp b/src/parser.cpp index a81104899..704cb6928 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -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(fd); +} + std::shared_ptr parser_t::shared() { return shared_from_this(); } cancel_checker_t parser_t::cancel_checker() const { diff --git a/src/parser.h b/src/parser.h index 11b759797..287cb7e1b 100644 --- a/src/parser.h +++ b/src/parser.h @@ -487,6 +487,10 @@ class parser_t : public std::enable_shared_from_this { /// 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 shared();