Skip to content

Commit

Permalink
We got da Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Stridsvagn69420 committed Jun 25, 2023
1 parent 761efe0 commit 9bb9cec
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.detectIndentation": false
}
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ documentation = "https://github.com/Stridsvagn69420/rmdup"
categories = ["command-line-utilities", "filesystem"]
keywords = ["coreutils", "command-line", "duplicate"]
exclude = [
".github/",
".vscode/",
".git/",
"target/"
".github/",
".vscode/",
".git/",
"target/"
]

[dependencies]
Expand Down
48 changes: 48 additions & 0 deletions src/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::fs::File;
use std::path::Path;
use std::io;
use std::io::Read;
use blake3::{Hash, Hasher};

/// Hasher
///
/// Gets the hash of a file by reading into a buffer.
/// This is used so that files that are potentially ultra large can still be read without your RAM collapsing.
pub(crate) fn hasher(path: &Path) -> io::Result<Hash> {
// Open file and get metadata
let mut f = File::open(path)?;
let fsize = f.metadata()?.len();

// Start Hashing
let mut hasher = Hasher::new();
let bsize = buffer_size(fsize);
let mut buffer = vec![0; bsize];

loop {
let bytes_read = f.read(&mut buffer)?;
if bytes_read == 0 {
break;
}
hasher.update(&buffer[..bytes_read]);
};
Ok(hasher.finalize())
}

/// Buffer Size
///
/// Selects a buffer size depending on the file size.
fn buffer_size(file_size: u64) -> usize {
const KB: u64 = 1024;
const MB: u64 = 1024 * KB;
const GB: u64 = 1024 * MB;

if file_size <= 512 * MB {
128 * KB as usize
} else if file_size <= 2 * GB {
32 * MB as usize
} else if file_size <= 6 * GB {
192 * MB as usize
} else {
512 * MB as usize
}
}
2 changes: 1 addition & 1 deletion src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub(crate) fn help_message(p: &mut Printer) {
// Flags
.println("FLAGS:", Colors::CyanBright)
.print("-V, --version: ", Colors::Cyan).writeln("Prints metadata of this tool")
.print("-h, --help:", Colors::Cyan).writeln("Prints this help message");
.print("-h, --help: ", Colors::Cyan).writeln("Prints this help message");

}

Expand Down
33 changes: 23 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
use std::env;
use std::path::Path;
use std::process::ExitCode;
use kagero::printer::Printer;
use kagero::printer::{Printer, Colors};
mod hash;
mod help;

fn main() -> ExitCode {
let mut prnt = Printer::default();
let mut p = Printer::default();
let args: Vec<String> = env::args().collect();

// Get file via command-line arguments
// Get file via command-line argument
let Some(file) = args.get(1) else {
help::help_message(&mut prnt);
help::help_message(&mut p);
return ExitCode::FAILURE;
};

// Flag handle
return match file.as_str() {
// Flag handler
match file.as_str() {
"-h" | "--help" => {
help::help_message(&mut prnt);
help::help_message(&mut p);
ExitCode::SUCCESS
},
"-V" | "--version" => {
help::version(&mut prnt);
help::version(&mut p);
ExitCode::SUCCESS
},
_ => ExitCode::SUCCESS // TODO: Replace this with function call that does the acutal logic.
_ => {
// Convert to Path
let filepath = Path::new(file);
if !filepath.exists() || !filepath.is_file() {
p.error(file, Colors::RedBright).errorln(" does not exist or is not a file!", Colors::Red);
return ExitCode::FAILURE;
}

println!("{:#?}", hash::hasher(filepath)); // TODO: Do more stuff. This is just a test atm.

ExitCode::SUCCESS
}
}
}
}

0 comments on commit 9bb9cec

Please sign in to comment.