1
0
mirror of https://github.com/jht5945/finding.git synced 2025-12-27 13:20:03 +08:00
This commit is contained in:
2019-07-22 01:50:40 +08:00
parent 0d81b8e0f8
commit 624641bc8c

View File

@@ -1,6 +1,10 @@
extern crate argparse; extern crate argparse;
extern crate rust_util; extern crate rust_util;
use std::{
path::Path,
};
use argparse::{ArgumentParser, StoreTrue, Store}; use argparse::{ArgumentParser, StoreTrue, Store};
use rust_util::*; use rust_util::*;
@@ -15,7 +19,39 @@ Written by Hatter Jiang
"#, VERSION); "#, VERSION);
} }
// --------------------------------------------------------------------------------------------------------- fn find_huge_files(huge_file_size: &String, dir_path: &Path) {
let huge_file_size_bytes = match parse_size(&huge_file_size) {
Err(err) => {
print_message(MessageType::ERROR, &format!("Parse size failed: {}", err));
return;
},
Ok(bytes) => bytes as u64,
};
walk_dir(&dir_path, &|_, _| (/* do not process error */), &|p| {
match p.metadata() {
Err(_) => (),
Ok(metadata) => {
let len = metadata.len();
if len >= huge_file_size_bytes {
match p.to_str() {
None => (),
Some(p_str) => {
print_lastline("");
print_message(MessageType::OK, &format!("{} [{}]", p_str, get_display_size(len as i64)));
},
}
}
},
}
}, &|p| {
match p.to_str() {
None => (),
Some(p_str) => print_lastline(&format!("Scanning: {}", p_str)),
}
true
}).unwrap_or(());
print_lastline("");
}
fn main() { fn main() {
let mut version = false; let mut version = false;
@@ -26,9 +62,10 @@ fn main() {
let mut ap = ArgumentParser::new(); let mut ap = ArgumentParser::new();
ap.set_description("finding - command line find tool."); ap.set_description("finding - command line find tool.");
ap.refer(&mut target).add_option(&["-t", "--target"], Store, "Target, text, huge[file], default text"); ap.refer(&mut target).add_option(&["-t", "--target"], Store, "Target, text, huge[file], default text");
ap.refer(&mut dir).add_option(&["-d", "--dir"], Store, "Target directory, default current dir(.)");
ap.refer(&mut huge_file_size).add_option(&["--huge-file"], Store, "Huge file size, default 100M"); ap.refer(&mut huge_file_size).add_option(&["--huge-file"], Store, "Huge file size, default 100M");
ap.refer(&mut version).add_option(&["-v", "--version"], StoreTrue, "Print version"); ap.refer(&mut version).add_option(&["-v", "--version"], StoreTrue, "Print version");
ap.refer(&mut dir).add_argument("DIR", Store, "Dir name, default current dir(.)"); //ap.refer(&mut dir).add_argument("DIR", Store, "Dir name, default current dir(.)");
ap.parse_args_or_exit(); ap.parse_args_or_exit();
} }
@@ -45,53 +82,7 @@ fn main() {
Some(path) => path, Some(path) => path,
}; };
match target.as_str() { match target.as_str() {
"huge" | "hugefile" => { "huge" | "hugefile" => find_huge_files(&huge_file_size, &dir_path),
let huge_file_size_bytes = match parse_size(&huge_file_size) { unknown => print_message(MessageType::ERROR, &format!("Unknown command: {}", unknown)),
Err(err) => {
print_message(MessageType::ERROR, &format!("Parse size failed: {}", err));
return;
},
Ok(bytes) => bytes as u64,
};
walk_dir(&dir_path, &|_, _| (/* do not process error */), &|p| {
match p.metadata() {
Err(_) => (),
Ok(metadata) => {
let len = metadata.len();
if len >= huge_file_size_bytes {
match p.to_str() {
None => (),
Some(p_str) => {
print_lastline("");
print_message(MessageType::OK, &format!("{} [{}]", p_str, get_display_size(len as i64)));
},
}
}
},
}
}, &|p| {
match p.to_str() {
None => (),
Some(p_str) => print_lastline(&format!("Scanning: {}", p_str)),
}
true
}).unwrap_or(());
print_lastline("");
return;
},
_ => (),
} }
// --------------------------------------------------------------------------------------------------------
println!("{:?}", get_home_path());
println!("{:?}", get_absolute_path("."));
println!("{:?}", get_absolute_path("../"));
println!("{:?}", get_absolute_path("~"));
println!("{:?}", get_absolute_path("~/.jssp"));
println!("{:?}", get_absolute_path("~/.jsspx"));
walk_dir(get_home_path().unwrap().as_path(), &|_, _| (), &|p| println!("{:?}",p), &|_| false).unwrap();
println!("Hello, world!");
} }