mirror of
https://github.com/jht5945/finding.git
synced 2025-12-29 22:20:05 +08:00
update
This commit is contained in:
@@ -1,20 +1,15 @@
|
|||||||
|
|
||||||
use std:: {
|
use std:: {
|
||||||
fs::File,
|
fs::File,
|
||||||
path::Path,
|
path::Path,
|
||||||
io::prelude::*,
|
io::prelude::*,
|
||||||
};
|
};
|
||||||
|
use rust_util::{ XResult, new_box_error, };
|
||||||
use rust_util::{
|
|
||||||
XResult,
|
|
||||||
new_box_error,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn read_file_content(file: &Path, large_file_len: u64) -> XResult<String> {
|
pub fn read_file_content(file: &Path, large_file_len: u64) -> XResult<String> {
|
||||||
if ! file.exists() {
|
if !file.exists() {
|
||||||
return Err(new_box_error(&format!("File not exists: {:?}", file)));
|
return Err(new_box_error(&format!("File not exists: {:?}", file)));
|
||||||
}
|
}
|
||||||
if ! file.is_file() {
|
if !file.is_file() {
|
||||||
return Err(new_box_error(&format!("File is not file: {:?}", file)));
|
return Err(new_box_error(&format!("File is not file: {:?}", file)));
|
||||||
}
|
}
|
||||||
let file_len = file.metadata()?.len();
|
let file_len = file.metadata()?.len();
|
||||||
@@ -22,7 +17,7 @@ pub fn read_file_content(file: &Path, large_file_len: u64) -> XResult<String> {
|
|||||||
return Err(new_box_error(&format!("File too large: {:?}, len: {}", file, file_len)));
|
return Err(new_box_error(&format!("File too large: {:?}, len: {}", file, file_len)));
|
||||||
}
|
}
|
||||||
let mut f = File::open(file)?;
|
let mut f = File::open(file)?;
|
||||||
let mut content = String::new();
|
let mut content = String::with_capacity(file_len as usize);
|
||||||
f.read_to_string(&mut content)?;
|
f.read_to_string(&mut content)?;
|
||||||
|
|
||||||
Ok(content)
|
Ok(content)
|
||||||
|
|||||||
50
src/main.rs
50
src/main.rs
@@ -22,6 +22,7 @@ use rust_util::{
|
|||||||
};
|
};
|
||||||
use local_util::*;
|
use local_util::*;
|
||||||
|
|
||||||
|
const EMPTY: &str = "";
|
||||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
const GIT_HASH: &str = env!("GIT_HASH");
|
const GIT_HASH: &str = env!("GIT_HASH");
|
||||||
|
|
||||||
@@ -33,6 +34,11 @@ License MIT <https://opensource.org/licenses/MIT>
|
|||||||
Written by Hatter Jiang"#, VERSION, &GIT_HASH[0..7]);
|
Written by Hatter Jiang"#, VERSION, &GIT_HASH[0..7]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn clear_n_print_message(mt: MessageType, message: &str) {
|
||||||
|
print_lastline(EMPTY);
|
||||||
|
print_message(mt, message);
|
||||||
|
}
|
||||||
|
|
||||||
fn find_huge_files(options: &Options, dir_path: &Path) {
|
fn find_huge_files(options: &Options, dir_path: &Path) {
|
||||||
let total_file_count_cell = RefCell::new(0u64);
|
let total_file_count_cell = RefCell::new(0u64);
|
||||||
let huge_file_count_cell = RefCell::new(0u64);
|
let huge_file_count_cell = RefCell::new(0u64);
|
||||||
@@ -43,8 +49,7 @@ fn find_huge_files(options: &Options, dir_path: &Path) {
|
|||||||
Err(err) => {
|
Err(err) => {
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
if let Some(p_str) = p.to_str() {
|
if let Some(p_str) = p.to_str() {
|
||||||
print_lastline("");
|
clear_n_print_message(MessageType::WARN, &format!("Read file {} meta failed: {}", p_str, err));
|
||||||
print_message(MessageType::WARN, &format!("Read file {} meta failed: {}", p_str, err));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@@ -54,34 +59,25 @@ fn find_huge_files(options: &Options, dir_path: &Path) {
|
|||||||
if len >= options.parsed_huge_file_size {
|
if len >= options.parsed_huge_file_size {
|
||||||
huge_file_count_cell.replace_with(|&mut c| c + 1);
|
huge_file_count_cell.replace_with(|&mut c| c + 1);
|
||||||
huge_file_size_cell.replace_with(|&mut c| c + len);
|
huge_file_size_cell.replace_with(|&mut c| c + len);
|
||||||
match p.to_str() {
|
if let Some(p_str) = p.to_str() {
|
||||||
None => (),
|
clear_n_print_message(MessageType::OK, &format!("{} [{}]", p_str, get_display_size(len as i64)));
|
||||||
Some(p_str) => {
|
|
||||||
print_lastline("");
|
|
||||||
print_message(MessageType::OK, &format!("{} [{}]", p_str, get_display_size(len as i64)));
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}, &|p| {
|
}, &|p| {
|
||||||
match p.to_str() {
|
if let Some(p_str) = p.to_str() {
|
||||||
None => (),
|
|
||||||
Some(p_str) => {
|
|
||||||
if options.skip_link_dir && is_symlink(p) {
|
if options.skip_link_dir && is_symlink(p) {
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
print_lastline("");
|
clear_n_print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str));
|
||||||
print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str));
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
print_lastline(&get_term_width_message(&format!("Scanning: {}", p_str), 10))
|
print_lastline(&get_term_width_message(&format!("Scanning: {}", p_str), 10));
|
||||||
},
|
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}).unwrap_or(());
|
}).unwrap_or(());
|
||||||
print_lastline("");
|
clear_n_print_message(MessageType::OK, &format!("Total file count: {}, huge file count: {}, total huge file size: {}",
|
||||||
print_message(MessageType::OK, &format!("Total file count: {}, huge file count: {}, total huge file size: {}",
|
|
||||||
total_file_count_cell.into_inner(),
|
total_file_count_cell.into_inner(),
|
||||||
huge_file_count_cell.into_inner(),
|
huge_file_count_cell.into_inner(),
|
||||||
get_display_size(huge_file_size_cell.into_inner() as i64)));
|
get_display_size(huge_file_size_cell.into_inner() as i64)));
|
||||||
@@ -115,8 +111,7 @@ fn match_lines(tag: &str, content: &str, options: &Options) -> bool {
|
|||||||
for ln in lines {
|
for ln in lines {
|
||||||
if options.filter_large_line && ln.len() as u64 >= options.parsed_large_line_size {
|
if options.filter_large_line && ln.len() as u64 >= options.parsed_large_line_size {
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
print_lastline("");
|
clear_n_print_message(MessageType::INFO, &format!("Skip large line: {} bytes", ln.len()));
|
||||||
print_message(MessageType::INFO, &format!("Skip large line: {} bytes", ln.len()));
|
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -138,7 +133,7 @@ fn match_lines(tag: &str, content: &str, options: &Options) -> bool {
|
|||||||
|
|
||||||
let mut matches_any = false;
|
let mut matches_any = false;
|
||||||
if !match_lines_vec.is_empty() {
|
if !match_lines_vec.is_empty() {
|
||||||
print_lastline("");
|
print_lastline(EMPTY);
|
||||||
print_message(MessageType::OK, &format!("Find in {}:", tag));
|
print_message(MessageType::OK, &format!("Find in {}:", tag));
|
||||||
for i in 0..match_lines_vec.len() {
|
for i in 0..match_lines_vec.len() {
|
||||||
print!("{}: ", match_lines_vec[i].line_number + 1);
|
print!("{}: ", match_lines_vec[i].line_number + 1);
|
||||||
@@ -206,8 +201,7 @@ fn find_text_files(options: &Options, dir_path: &Path) {
|
|||||||
let file_content = match read_file_content(p, options.parsed_large_text_file_size) {
|
let file_content = match read_file_content(p, options.parsed_large_text_file_size) {
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
print_lastline("");
|
clear_n_print_message(MessageType::WARN, &format!("Read file {} failed: {}", p_str, err));
|
||||||
print_message(MessageType::WARN, &format!("Read file {} failed: {}", p_str, err));
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
},
|
},
|
||||||
@@ -224,8 +218,7 @@ fn find_text_files(options: &Options, dir_path: &Path) {
|
|||||||
Some(p_str) => {
|
Some(p_str) => {
|
||||||
if (! options.scan_dot_git) && p_str.ends_with("/.git") {
|
if (! options.scan_dot_git) && p_str.ends_with("/.git") {
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
print_lastline("");
|
clear_n_print_message(MessageType::INFO, &format!("Skip .git dir: {}", p_str));
|
||||||
print_message(MessageType::INFO, &format!("Skip .git dir: {}", p_str));
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -234,8 +227,7 @@ fn find_text_files(options: &Options, dir_path: &Path) {
|
|||||||
}
|
}
|
||||||
if options.skip_link_dir && is_symlink(p) {
|
if options.skip_link_dir && is_symlink(p) {
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
print_lastline("");
|
clear_n_print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str));
|
||||||
print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str));
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -245,7 +237,7 @@ fn find_text_files(options: &Options, dir_path: &Path) {
|
|||||||
}
|
}
|
||||||
true
|
true
|
||||||
}).unwrap_or(());
|
}).unwrap_or(());
|
||||||
print_lastline("");
|
print_lastline(EMPTY);
|
||||||
print_message(MessageType::OK, &format!("Total dir count: {}, scaned dir count: {}",
|
print_message(MessageType::OK, &format!("Total dir count: {}, scaned dir count: {}",
|
||||||
total_dir_count_cell.into_inner(),
|
total_dir_count_cell.into_inner(),
|
||||||
scaned_dir_count_cell.into_inner()));
|
scaned_dir_count_cell.into_inner()));
|
||||||
@@ -255,7 +247,6 @@ fn find_text_files(options: &Options, dir_path: &Path) {
|
|||||||
matched_file_count_cell.into_inner()));
|
matched_file_count_cell.into_inner()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn main() -> XResult<()> {
|
fn main() -> XResult<()> {
|
||||||
let options = Options::new_and_parse_args()?;
|
let options = Options::new_and_parse_args()?;
|
||||||
|
|
||||||
@@ -265,10 +256,9 @@ fn main() -> XResult<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let dir_path = match get_absolute_path(&options.dir) {
|
let dir_path = match get_absolute_path(&options.dir) {
|
||||||
None => {
|
Some(path) => path, None => {
|
||||||
return Err(new_box_error(&format!("Cannot find dir: {}", options.dir)));
|
return Err(new_box_error(&format!("Cannot find dir: {}", options.dir)));
|
||||||
},
|
},
|
||||||
Some(path) => path,
|
|
||||||
};
|
};
|
||||||
let start = SystemTime::now();
|
let start = SystemTime::now();
|
||||||
match options.target.as_str() {
|
match options.target.as_str() {
|
||||||
|
|||||||
@@ -1,10 +1,5 @@
|
|||||||
|
|
||||||
use argparse::{ArgumentParser, StoreTrue, Store};
|
use argparse::{ArgumentParser, StoreTrue, Store};
|
||||||
|
use rust_util::{ XResult, util_size::*, };
|
||||||
use rust_util::{
|
|
||||||
XResult,
|
|
||||||
util_size::*,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct Options {
|
pub struct Options {
|
||||||
pub version: bool,
|
pub version: bool,
|
||||||
|
|||||||
Reference in New Issue
Block a user