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,
path::Path,
io::prelude::*,
};
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() {
return Err(new_box_error(&format!("File not exists: {:?}", 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();
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)));
}
let mut f = File::open(file)?;

View File

@@ -6,7 +6,7 @@ extern crate rust_util;
mod opt;
mod local_util;
use std::{ cell::RefCell, path::Path, time::SystemTime, };
use std::{ cell::Cell, path::Path, time::SystemTime, };
use opt::*;
use rust_util::{
iff,
@@ -48,11 +48,11 @@ fn clear_n_print_message(mt: MessageType, message: &str) {
}
fn find_huge_files(options: &Options, dir_path: &Path) {
let total_file_count_cell = RefCell::new(0u64);
let huge_file_count_cell = RefCell::new(0u64);
let huge_file_size_cell = RefCell::new(0u64);
let total_file_count_cell = Cell::new(0_u64);
let huge_file_count_cell = Cell::new(0_u64);
let huge_file_size_cell = Cell::new(0_u64);
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() {
Some(s) => s, None => return,
};
@@ -63,8 +63,8 @@ fn find_huge_files(options: &Options, dir_path: &Path) {
Ok(metadata) => {
let len = metadata.len();
if len >= options.parsed_huge_file_size {
huge_file_count_cell.replace_with(|&mut c| c + 1);
huge_file_size_cell.replace_with(|&mut c| c + len);
huge_file_count_cell.replace(huge_file_count_cell.get() + 1);
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)));
}
},
@@ -92,7 +92,7 @@ fn match_lines(tag: &str, content: &str, options: &Options) -> bool {
let search_text = &options.search_text;
let lines = content.lines();
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());
for ln in lines {
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 => 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 scaned_file_count_cell = RefCell::new(0u64);
let matched_file_count_cell = RefCell::new(0u64);
let total_dir_count_cell = RefCell::new(0u64);
let scaned_dir_count_cell = RefCell::new(0u64);
let total_file_count_cell = Cell::new(0_u64);
let scaned_file_count_cell = Cell::new(0_u64);
let matched_file_count_cell = Cell::new(0_u64);
let total_dir_count_cell = Cell::new(0_u64);
let scaned_dir_count_cell = Cell::new(0_u64);
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() {
Some(s) => s, None => return,
};
@@ -169,12 +169,12 @@ fn find_text_files(options: &Options, dir_path: &Path) {
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) {
matched_file_count_cell.replace_with(|&mut c| c + 1);
matched_file_count_cell.replace(matched_file_count_cell.get() + 1);
}
}, &|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() {
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)); }
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));
true
}).ok();

View File

@@ -1,4 +1,4 @@
use argparse::{ArgumentParser, StoreTrue, Store};
use argparse::{ ArgumentParser, StoreTrue, Store, };
use rust_util::{ XResult, util_size::*, };
pub struct Options {