Skip to content

Commit

Permalink
Fixed file redirection relative to current working directory
Browse files Browse the repository at this point in the history
  • Loading branch information
zakstucke committed Feb 7, 2024
1 parent 27fe554 commit b5613d3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
5 changes: 4 additions & 1 deletion rust/bitbazaar/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ mod tests {
default_stdout_global_logging(tracing::Level::DEBUG).unwrap();
}

// Temp file:
fn tf() -> String {
// Using debug formatting to make sure escaped properly on windows:
format!("{:?}", NamedTempFile::new().unwrap().path())
}

static GLOB_TD: Lazy<tempfile::TempDir> = Lazy::new(|| tempfile::tempdir().unwrap());

static HOME_DIR: Lazy<String> = Lazy::new(|| {
homedir::get_my_home()
.unwrap()
Expand Down Expand Up @@ -129,7 +132,7 @@ mod tests {
// <-- redirection:
// Write:
#[case::redir_1(format!("echo foo > {fp} && rm {fp}", fp=tf()), "", 0, None, None, true)]
#[case::redir_2(format!("echo foo > {fp} && {CAT_CMD} {fp} && rm {fp}", fp=tf()), "foo", 0, None, None, true)]
#[case::redir_2(format!("cd {dp:?} && echo foo > file.txt && {CAT_CMD} file.txt", dp=GLOB_TD.path()), "foo", 0, None, None, true)]
// Write and append together:
#[case::redir_3(
format!("echo foo >> {fp} && echo bar > {fp} && echo ree >> {fp} && {CAT_CMD} {fp} && rm {fp}", fp=tf()),
Expand Down
23 changes: 16 additions & 7 deletions rust/bitbazaar/cli/redirect.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
io::{Read, Write},
path::PathBuf,
process,
};

Expand All @@ -20,23 +21,23 @@ pub fn handle_redirect(
Ok(match redirect {
ast::Redirect::Write(fd, name) => {
let dest = Target::new(shell, name)?.set_write();
Data::new(last_out, fd)?.submit(dest)?
Data::new(last_out, fd)?.submit(shell, dest)?
}
ast::Redirect::Append(fd, name) => {
let dest = Target::new(shell, name)?.set_write().set_append();
Data::new(last_out, fd)?.submit(dest)?
Data::new(last_out, fd)?.submit(shell, dest)?
}
ast::Redirect::DupWrite(fd, name) => {
let dest = Target::new(shell, name)?.set_write();
Data::new(last_out, fd)?.submit(dest)?
Data::new(last_out, fd)?.submit(shell, dest)?
}
ast::Redirect::Read(fd, name) => {
let dest = Target::new(shell, name)?.set_read();
Data::new(last_out, fd)?.submit(dest)?
Data::new(last_out, fd)?.submit(shell, dest)?
}
ast::Redirect::DupRead(fd, name) => {
let dest = Target::new(shell, name)?.set_read();
Data::new(last_out, fd)?.submit(dest)?
Data::new(last_out, fd)?.submit(shell, dest)?
}
ast::Redirect::ReadWrite(..) => {
return Err(err!(
Expand Down Expand Up @@ -203,7 +204,7 @@ impl Data {
})
}

fn submit(self, dest: Target) -> Result<RunnerCmdOut, ShellErr> {
fn submit(self, shell: &Shell, dest: Target) -> Result<RunnerCmdOut, ShellErr> {
let mut conc = ConcreteOutput::default();

match dest.variant {
Expand Down Expand Up @@ -243,7 +244,15 @@ impl Data {
opts.append(true);
}

let mut file = opts.open(name).change_context(ShellErr::InternalError)?;
// Make sure relative to current dir if it is a relative path:
let mut filepath = PathBuf::from(name);
if filepath.is_relative() {
filepath = shell.active_dir()?.join(filepath)
}

let mut file = opts
.open(filepath)
.change_context(ShellErr::InternalError)?;

if dest.write {
self.write(&mut file)?;
Expand Down

0 comments on commit b5613d3

Please sign in to comment.