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 b815cf0
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::PathBuf;

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

use regex::Regex;
Expand Down Expand Up @@ -185,18 +186,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();
if params.from == "-" {
stdin_path.push(to_path.file_name().unwrap());
} else {
stdin_path.push(from_path.file_name().unwrap());
}
params.stdin_path = stdin_path.into();
}
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 b815cf0

Please sign in to comment.