Skip to content

Commit

Permalink
Make handling stdin directory unix specific
Browse files Browse the repository at this point in the history
  • Loading branch information
TanmayPatil105 committed May 6, 2024
1 parent 4482bdb commit f21689d
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/params.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::ffi::OsString;
use std::fs;
use std::path::PathBuf;

use std::fs::File;
#[cfg(unix)]
use std::fs::{self, File};
#[cfg(unix)]
use std::io::stdin;
#[cfg(unix)]
use std::os::fd::AsFd;

use regex::Regex;
Expand Down Expand Up @@ -185,18 +187,21 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
let mut from_path: PathBuf = PathBuf::from(&params.from);
let mut to_path: PathBuf = PathBuf::from(&params.to);

// check if stdin is a directory
let fd = stdin().as_fd().try_clone_to_owned().unwrap();
let file = File::from(fd);
let meta = file.metadata().unwrap();
if meta.is_dir() {
let mut stdin_path = fs::canonicalize("/dev/stdin").unwrap();
if params.from == "-" {
stdin_path.push(to_path.file_name().unwrap());
} else {
stdin_path.push(from_path.file_name().unwrap());
#[cfg(unix)]
{
// check if stdin is a directory
let fd = stdin().as_fd().try_clone_to_owned().unwrap();
let file = File::from(fd);
let meta = file.metadata().unwrap();
if meta.is_dir() {
let mut stdin_path = fs::canonicalize("/dev/stdin").unwrap();

Check warning on line 197 in src/params.rs

View check run for this annotation

Codecov / codecov/patch

src/params.rs#L197

Added line #L197 was not covered by tests
if params.from == "-" {
stdin_path.push(to_path.file_name().unwrap());

Check warning on line 199 in src/params.rs

View check run for this annotation

Codecov / codecov/patch

src/params.rs#L199

Added line #L199 was not covered by tests
} else {
stdin_path.push(from_path.file_name().unwrap());

Check warning on line 201 in src/params.rs

View check run for this annotation

Codecov / codecov/patch

src/params.rs#L201

Added line #L201 was not covered by tests
}
params.stdin_path = stdin_path.into();

Check warning on line 203 in src/params.rs

View check run for this annotation

Codecov / codecov/patch

src/params.rs#L203

Added line #L203 was not covered by tests
}
params.stdin_path = stdin_path.into();
}

if (from_path.is_dir() || !params.stdin_path.is_empty()) && to_path.is_file() {
Expand Down

0 comments on commit f21689d

Please sign in to comment.