-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
761efe0
commit 9bb9cec
Showing
5 changed files
with
79 additions
and
15 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"editor.detectIndentation": false | ||
} |
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
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
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 | ||
} | ||
} |
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
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
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 | ||
} | ||
} | ||
} | ||
} |