oxedyne/fe2o3/fe2o3_o3db_sync/src/file/core.rs
1.7 KiB, 9 runs
created by r1870400018:789, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use crate::{ |
| 2 | prelude::*, |
| 3 | regex_data_file, |
| 4 | }; |
| 5 | |
| 6 | use oxedyne_fe2o3_core::prelude::*; |
| 7 | |
| 8 | use std::{ |
| 9 | fs, |
| 10 | path::{ |
| 11 | Path, |
| 12 | PathBuf, |
| 13 | }, |
| 14 | }; |
| 15 | |
| 16 | use regex::Regex; |
| 17 | |
| 18 | |
| 19 | #[derive(Debug)] |
| 20 | pub enum FileAccess { |
| 21 | Reading, |
| 22 | Writing, |
| 23 | } |
| 24 | |
| 25 | #[repr(u8)] |
| 26 | #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] |
| 27 | pub enum FileType { |
| 28 | Data = 0, |
| 29 | Index = 1, |
| 30 | } |
| 31 | |
| 32 | #[derive(Clone, Debug)] |
| 33 | pub struct FileEntry { |
| 34 | pub typ: String, |
| 35 | pub size: u64, |
| 36 | pub mods: u64, |
| 37 | pub name: String, |
| 38 | } |
| 39 | |
| 40 | pub fn find_files(dir: &Path) -> Outcome<Vec<PathBuf>> { |
| 41 | let pattern = fmt!( |
| 42 | "^{}\\.({}|{})$", |
| 43 | regex_data_file!(), |
| 44 | constant::DATA_FILE_EXT, |
| 45 | constant::INDEX_FILE_EXT, |
| 46 | ); |
| 47 | let re = res!(regex::Regex::new(&pattern)); |
| 48 | |
| 49 | let mut matching_files = Vec::new(); |
| 50 | |
| 51 | res!(search_recursively(dir, &re, &mut matching_files)); |
| 52 | |
| 53 | Ok(matching_files) |
| 54 | } |
| 55 | |
| 56 | fn search_recursively( |
| 57 | dir: &Path, |
| 58 | re: &Regex, |
| 59 | matching_files: &mut Vec<PathBuf>, |
| 60 | ) |
| 61 | -> Outcome<()> |
| 62 | { |
| 63 | if dir.is_dir() { |
| 64 | for entry in res!(fs::read_dir(dir)) { |
| 65 | let entry = res!(entry); |
| 66 | let path = entry.path(); |
| 67 | if path.is_dir() { |
| 68 | debug!(sync_log::stream(), "Looking in {:?}", path); |
| 69 | res!(search_recursively(&path, re, matching_files)); |
| 70 | } else if let Some(file_name) = path.file_name() { |
| 71 | debug!(sync_log::stream(), "Found {:?}", path); |
| 72 | if let Some(file_name_str) = file_name.to_str() { |
| 73 | if re.is_match(file_name_str) { |
| 74 | matching_files.push(path); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | Ok(()) |
| 81 | } |