Skip to content

Commit f6dbe94

Browse files
committed
Add -o option to shasum
1 parent a1cf93e commit f6dbe94

File tree

4 files changed

+103
-4
lines changed

4 files changed

+103
-4
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
/build
1+
/target
22
/.idea

Cargo.lock

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "decomp-toolkit"
33
description = "GameCube/Wii decompilation project tools."
44
authors = ["Luke Street <[email protected]>"]
55
license = "MIT OR Apache-2.0"
6-
version = "0.1.0"
6+
version = "0.1.1"
77
edition = "2021"
88
publish = false
99
build = "build.rs"
@@ -20,6 +20,7 @@ anyhow = "1.0.64"
2020
argh = "0.1.8"
2121
base16ct = "0.1.1"
2222
cwdemangle = "0.1.3"
23+
filetime = "0.2.18"
2324
hex = "0.4.3"
2425
lazy_static = "1.4.0"
2526
log = "0.4.17"

src/cmd/shasum.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::{
2-
fs::File,
2+
fs::{File, OpenOptions},
33
io::{BufRead, BufReader, Read},
4+
path::Path,
45
};
56

67
use anyhow::{Context, Error, Result};
78
use argh::FromArgs;
9+
use filetime::{set_file_mtime, FileTime};
810
use sha1::{Digest, Sha1};
911

1012
#[derive(FromArgs, PartialEq, Eq, Debug)]
@@ -17,6 +19,9 @@ pub struct Args {
1719
#[argh(positional)]
1820
/// path to file
1921
file: String,
22+
#[argh(option, short = 'o')]
23+
/// touch output file on successful check
24+
output: Option<String>,
2025
}
2126

2227
const DEFAULT_BUF_SIZE: usize = 8192;
@@ -31,7 +36,7 @@ pub fn run(args: Args) -> Result<()> {
3136
}
3237
}
3338

34-
fn check(_args: Args, file: File) -> Result<()> {
39+
fn check(args: Args, file: File) -> Result<()> {
3540
let reader = BufReader::new(file);
3641
let mut mismatches = 0usize;
3742
for line in reader.lines() {
@@ -63,6 +68,9 @@ fn check(_args: Args, file: File) -> Result<()> {
6368
eprintln!("WARNING: {} computed checksum did NOT match", mismatches);
6469
std::process::exit(1);
6570
}
71+
if let Some(out_path) = args.output {
72+
touch(&out_path).with_context(|| format!("Failed to touch output file '{}'", out_path))?;
73+
}
6674
Ok(())
6775
}
6876

@@ -86,3 +94,14 @@ fn file_sha1(mut file: File) -> Result<sha1::digest::Output<Sha1>> {
8694
hasher.update(&buf[0..read]);
8795
})
8896
}
97+
98+
fn touch<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
99+
if path.as_ref().exists() {
100+
set_file_mtime(path, FileTime::now())
101+
} else {
102+
match OpenOptions::new().create(true).write(true).open(path) {
103+
Ok(_) => Ok(()),
104+
Err(e) => Err(e),
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)