1 use std::path::PathBuf; 2 3 pub mod cargo_handler; 4 pub mod ld_scripts; 5 6 pub struct FileUtils; 7 8 impl FileUtils { 9 /// 列出指定目录下的所有文件 10 /// 11 /// ## 参数 12 /// 13 /// - `path` - 指定的目录 14 /// - `ext_name` - 文件的扩展名,如果为None,则列出所有文件 15 /// - `recursive` - 是否递归列出所有文件 list_all_files(path: &PathBuf, ext_name: Option<&str>, recursive: bool) -> Vec<PathBuf>16 pub fn list_all_files(path: &PathBuf, ext_name: Option<&str>, recursive: bool) -> Vec<PathBuf> { 17 let mut queue: Vec<PathBuf> = Vec::new(); 18 let mut result = Vec::new(); 19 queue.push(path.clone()); 20 21 while !queue.is_empty() { 22 let path = queue.pop().unwrap(); 23 let d = std::fs::read_dir(path); 24 if d.is_err() { 25 continue; 26 } 27 let d = d.unwrap(); 28 29 d.for_each(|ent| { 30 if let Ok(ent) = ent { 31 if let Ok(file_type) = ent.file_type() { 32 if file_type.is_file() { 33 if let Some(e) = ext_name { 34 if let Some(ext) = ent.path().extension() { 35 if ext == e { 36 result.push(ent.path()); 37 } 38 } 39 } 40 } else if file_type.is_dir() && recursive { 41 queue.push(ent.path()); 42 } 43 } 44 } 45 }); 46 } 47 48 return result; 49 } 50 } 51