Skip to content

Commit

Permalink
stdin: Handle directory input
Browse files Browse the repository at this point in the history
When handling stdin, GNU diff now behaves as follows:
  * If a file is input, it displays the current time as m_time
  * If a directory is input, it appends the other file_name to
    the canonicalized path of directory and reads and displays
    the m_time of that file
  • Loading branch information
TanmayPatil105 committed May 5, 2024
1 parent d922313 commit b8001cc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
12 changes: 10 additions & 2 deletions src/context_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,16 @@ fn make_diff(

#[must_use]
pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
let from_modified_time = get_modification_time(&params.from.to_string_lossy());
let to_modified_time = get_modification_time(&params.to.to_string_lossy());
let from_modified_time =
match !params.stdin_path.is_empty() && params.from.to_string_lossy().starts_with("-") {
true => get_modification_time(&params.stdin_path.to_string_lossy()),

Check warning on line 273 in src/context_diff.rs

View check run for this annotation

Codecov / codecov/patch

src/context_diff.rs#L273

Added line #L273 was not covered by tests
false => get_modification_time(&params.from.to_string_lossy()),
};
let to_modified_time =
match !params.stdin_path.is_empty() && params.to.to_string_lossy().starts_with("-") {
true => get_modification_time(&params.stdin_path.to_string_lossy()),

Check warning on line 278 in src/context_diff.rs

View check run for this annotation

Codecov / codecov/patch

src/context_diff.rs#L278

Added line #L278 was not covered by tests
false => get_modification_time(&params.to.to_string_lossy()),
};
let mut output = format!(
"*** {0}\t{1}\n--- {2}\t{3}\n",
params.from.to_string_lossy(),
Expand Down
10 changes: 6 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,24 @@ fn main() -> ExitCode {
return ExitCode::SUCCESS;
}
// read files
fn read_file_contents(filepath: &OsString) -> io::Result<Vec<u8>> {
if filepath == "-" {
fn read_file_contents(filepath: &OsString, stdin_path: &OsString) -> io::Result<Vec<u8>> {
if filepath.to_string_lossy().starts_with("-") && !stdin_path.is_empty() {
fs::read(stdin_path)

Check warning on line 51 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L51

Added line #L51 was not covered by tests
} else if filepath == "-" {
let mut content = Vec::new();
io::stdin().read_to_end(&mut content).and(Ok(content))
} else {
fs::read(filepath)
}
}
let from_content = match read_file_contents(&params.from) {
let from_content = match read_file_contents(&params.from, &params.stdin_path) {
Ok(from_content) => from_content,
Err(e) => {
eprintln!("Failed to read from-file: {e}");
return ExitCode::from(2);
}
};
let to_content = match read_file_contents(&params.to) {
let to_content = match read_file_contents(&params.to, &params.stdin_path) {
Ok(to_content) => to_content,
Err(e) => {
eprintln!("Failed to read to-file: {e}");
Expand Down
26 changes: 24 additions & 2 deletions src/params.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::ffi::OsString;
use std::fs;
use std::path::PathBuf;

use std::fs::File;
use std::io::stdin;
use std::os::fd::AsFd;

use regex::Regex;

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
Expand All @@ -16,6 +21,7 @@ pub enum Format {
pub struct Params {
pub from: OsString,
pub to: OsString,
pub stdin_path: OsString,
pub format: Format,
pub context_count: usize,
pub report_identical_files: bool,
Expand All @@ -29,6 +35,7 @@ impl Default for Params {
Self {
from: OsString::default(),
to: OsString::default(),
stdin_path: OsString::default(),
format: Format::default(),
context_count: 3,
report_identical_files: false,
Expand Down Expand Up @@ -178,10 +185,25 @@ 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);

if from_path.is_dir() && to_path.is_file() {
// 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 stdin = fs::canonicalize("/dev/stdin").unwrap();
let mut stdin_path: PathBuf = PathBuf::from(stdin);

Check warning on line 194 in src/params.rs

View check run for this annotation

Codecov / codecov/patch

src/params.rs#L193-L194

Added lines #L193 - L194 were not covered by tests
if params.from == "-" {
stdin_path.push(&to_path.file_name().unwrap());

Check warning on line 196 in src/params.rs

View check run for this annotation

Codecov / codecov/patch

src/params.rs#L196

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

Check warning on line 198 in src/params.rs

View check run for this annotation

Codecov / codecov/patch

src/params.rs#L198

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

Check warning on line 200 in src/params.rs

View check run for this annotation

Codecov / codecov/patch

src/params.rs#L200

Added line #L200 was not covered by tests
}

if (from_path.is_dir() || !params.stdin_path.is_empty()) && to_path.is_file() {
from_path.push(to_path.file_name().unwrap());
params.from = from_path.into_os_string();
} else if from_path.is_file() && to_path.is_dir() {
} else if from_path.is_file() && (to_path.is_dir() || !params.stdin_path.is_empty()) {
to_path.push(from_path.file_name().unwrap());
params.to = to_path.into_os_string();
}
Expand Down
12 changes: 10 additions & 2 deletions src/unified_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,16 @@ fn make_diff(

#[must_use]
pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
let from_modified_time = get_modification_time(&params.from.to_string_lossy());
let to_modified_time = get_modification_time(&params.to.to_string_lossy());
let from_modified_time =
match !params.stdin_path.is_empty() && params.from.to_string_lossy().starts_with("-") {
true => get_modification_time(&params.stdin_path.to_string_lossy()),

Check warning on line 244 in src/unified_diff.rs

View check run for this annotation

Codecov / codecov/patch

src/unified_diff.rs#L244

Added line #L244 was not covered by tests
false => get_modification_time(&params.from.to_string_lossy()),
};
let to_modified_time =
match !params.stdin_path.is_empty() && params.to.to_string_lossy().starts_with("-") {
true => get_modification_time(&params.stdin_path.to_string_lossy()),

Check warning on line 249 in src/unified_diff.rs

View check run for this annotation

Codecov / codecov/patch

src/unified_diff.rs#L249

Added line #L249 was not covered by tests
false => get_modification_time(&params.to.to_string_lossy()),
};
let mut output = format!(
"--- {0}\t{1}\n+++ {2}\t{3}\n",
params.from.to_string_lossy(),
Expand Down

0 comments on commit b8001cc

Please sign in to comment.