Last active
March 4, 2022 22:34
-
-
Save Sir-Photch/54a1975a48b472bafbc96348a9c66939 to your computer and use it in GitHub Desktop.
Recursive shasum generator for entries in filesystem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "traverse" | |
version = "0.1.0" | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
sha2="0.10.2" | |
hex="0.3.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{env, fs}; | |
use std::fs::File; | |
extern crate hex; | |
use std::path::{Path, PathBuf}; | |
use std::io::Read; | |
use sha2::{Sha256, Digest}; | |
fn main() -> Result<(), std::io::Error> { | |
let args: Vec<String> = env::args().collect(); | |
let path : PathBuf; | |
if args.len() == 1 { | |
path = env::current_dir().unwrap(); | |
} else { | |
path = Path::new(&args[1]).to_path_buf(); | |
} | |
assert!(path.exists(), "Path does not exist!"); | |
assert!(path.is_dir(), "Path is not a directory!"); | |
println!("Traversing subdirs of {:?} ...", path); | |
print_children(path) | |
} | |
fn print_children(path: PathBuf) -> Result<(), std::io::Error> { | |
for entry in fs::read_dir(path)? { | |
let entry = entry?; | |
let path : PathBuf = entry.path(); | |
if path.is_dir() { | |
print_children(path)? | |
} else if path.is_file() { | |
let mut f = File::open(path.as_path())?; | |
let mut buf = Vec::<u8>::new(); | |
f.read_to_end(&mut buf)?; | |
let mut hasher : Sha256 = Sha256::new(); | |
hasher.update(buf); | |
let hash = hasher.finalize(); | |
println!("{}\t{:?}",hex::encode(&hash),path); | |
} | |
} | |
Ok(()) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment