1
0
mirror of https://github.com/jht5945/finding.git synced 2026-01-12 12:00:03 +08:00

Compare commits

..

7 Commits

Author SHA1 Message Date
68d51461bd len_of_large_file 2020-04-30 01:11:09 +08:00
cb6f3cd4bd len_of_large_file 2020-04-30 01:10:47 +08:00
b2dc1d868c &Path -> AsRef<Path> 2020-04-30 01:09:28 +08:00
29fea6ac49 re format 2020-04-30 01:05:41 +08:00
63afb268ef add a 2020-04-30 01:01:17 +08:00
eefbb3d49e reformat 2020-04-30 00:53:41 +08:00
84a0c444a0 RefCell -> Cell 2020-04-30 00:51:37 +08:00
3 changed files with 24 additions and 23 deletions

View File

@@ -1,19 +1,20 @@
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<P: AsRef<Path>>(p: P, len_of_large_file: u64) -> XResult<String> {
let file = p.as_ref();
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 a file: {:?}", file)));
} }
let file_len = file.metadata()?.len(); let file_len = file.metadata()?.len();
if file_len > large_file_len { if file_len >= len_of_large_file {
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)?;

View File

@@ -6,7 +6,7 @@ extern crate rust_util;
mod opt; mod opt;
mod local_util; mod local_util;
use std::{ cell::RefCell, path::Path, time::SystemTime, }; use std::{ cell::Cell, path::Path, time::SystemTime, };
use opt::*; use opt::*;
use rust_util::{ use rust_util::{
iff, iff,
@@ -48,11 +48,11 @@ fn clear_n_print_message(mt: MessageType, message: &str) {
} }
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 = Cell::new(0_u64);
let huge_file_count_cell = RefCell::new(0u64); let huge_file_count_cell = Cell::new(0_u64);
let huge_file_size_cell = RefCell::new(0u64); let huge_file_size_cell = Cell::new(0_u64);
walk_dir(&dir_path, &|_, _| (/* do not process error */), &|p| { // process file walk_dir(&dir_path, &|_, _| (/* do not process error */), &|p| { // process file
total_file_count_cell.replace_with(|&mut c| c + 1); total_file_count_cell.replace(total_file_count_cell.get() + 1);
let p_str = match p.to_str() { let p_str = match p.to_str() {
Some(s) => s, None => return, Some(s) => s, None => return,
}; };
@@ -63,8 +63,8 @@ fn find_huge_files(options: &Options, dir_path: &Path) {
Ok(metadata) => { Ok(metadata) => {
let len = metadata.len(); let len = metadata.len();
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(huge_file_count_cell.get() + 1);
huge_file_size_cell.replace_with(|&mut c| c + len); huge_file_size_cell.replace(huge_file_size_cell.get() + 1);
clear_n_print_message(MessageType::OK, &format!("{} [{}]", p_str, get_display_size(len as i64))); clear_n_print_message(MessageType::OK, &format!("{} [{}]", p_str, get_display_size(len as i64)));
} }
}, },
@@ -92,7 +92,7 @@ fn match_lines(tag: &str, content: &str, options: &Options) -> bool {
let search_text = &options.search_text; let search_text = &options.search_text;
let lines = content.lines(); let lines = content.lines();
let mut match_lines_vec = vec![]; let mut match_lines_vec = vec![];
let mut line_no = 0usize; let mut line_no = 0_usize;
let the_search_text = &iff!(options.ignore_case, search_text.to_lowercase(), search_text.to_string()); let the_search_text = &iff!(options.ignore_case, search_text.to_lowercase(), search_text.to_string());
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 {
@@ -147,13 +147,13 @@ fn find_text_files(options: &Options, dir_path: &Path) {
ext if ext.is_empty() => vec![], ext if ext.is_empty() => vec![],
ext => ext.split(',').map(|s| s.trim()).filter(|s| !s.is_empty()).map(|s| ".".to_owned() + s).collect(), ext => ext.split(',').map(|s| s.trim()).filter(|s| !s.is_empty()).map(|s| ".".to_owned() + s).collect(),
}; };
let total_file_count_cell = RefCell::new(0u64); let total_file_count_cell = Cell::new(0_u64);
let scaned_file_count_cell = RefCell::new(0u64); let scaned_file_count_cell = Cell::new(0_u64);
let matched_file_count_cell = RefCell::new(0u64); let matched_file_count_cell = Cell::new(0_u64);
let total_dir_count_cell = RefCell::new(0u64); let total_dir_count_cell = Cell::new(0_u64);
let scaned_dir_count_cell = RefCell::new(0u64); let scaned_dir_count_cell = Cell::new(0_u64);
walk_dir(&dir_path, &|_, _| (/* do not process error */), &|p| { // process file walk_dir(&dir_path, &|_, _| (/* do not process error */), &|p| { // process file
total_file_count_cell.replace_with(|&mut c| c + 1); total_file_count_cell.replace(total_file_count_cell.get() + 1);
let p_str = match p.to_str() { let p_str = match p.to_str() {
Some(s) => s, None => return, Some(s) => s, None => return,
}; };
@@ -169,12 +169,12 @@ fn find_text_files(options: &Options, dir_path: &Path) {
return; return;
}, },
}; };
scaned_file_count_cell.replace_with(|&mut c| c + 1); scaned_file_count_cell.replace(scaned_file_count_cell.get() + 1);
if match_lines(p_str, &file_content, &options) { if match_lines(p_str, &file_content, &options) {
matched_file_count_cell.replace_with(|&mut c| c + 1); matched_file_count_cell.replace(matched_file_count_cell.get() + 1);
} }
}, &|p| { // process path }, &|p| { // process path
total_dir_count_cell.replace_with(|&mut c| c + 1); total_dir_count_cell.replace(total_dir_count_cell.get() + 1);
let p_str = match p.to_str() { let p_str = match p.to_str() {
Some(s) => s, None => return false, Some(s) => s, None => return false,
}; };
@@ -194,7 +194,7 @@ fn find_text_files(options: &Options, dir_path: &Path) {
if options.verbose { clear_n_print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str)); } if options.verbose { clear_n_print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str)); }
return false; return false;
} }
scaned_dir_count_cell.replace_with(|&mut c| c + 1); scaned_dir_count_cell.replace(scaned_dir_count_cell.get() + 1);
print_lastline(&get_term_width_message(&format!("Scanning: {}", p_str), 10)); print_lastline(&get_term_width_message(&format!("Scanning: {}", p_str), 10));
true true
}).ok(); }).ok();

View File

@@ -1,4 +1,4 @@
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 {