From 607a1b90f27a03297635ebbb2aed55de56dd3387 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sun, 17 Mar 2024 23:18:14 +0200 Subject: [PATCH] implement file traversal (without recursion) --- src/main.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5d18626..3280333 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ use clap::Parser; -use std::fs; +use std::fs::{self, DirEntry, FileType, ReadDir}; use std::collections::HashMap; use std::num::ParseIntError; +use std::path::Path; #[derive(Parser, Debug)] #[command(version, about)] @@ -64,6 +65,62 @@ const GROUP_PATH: &str = "group"; #[cfg(not(debug_assertions))] const GROUP_PATH: &str = "/etc/group"; +fn traverse_filesystem(path: &Path) -> (Vec, Vec){ + let mut files: Vec = Vec::new(); + let mut directories: Vec = Vec::new(); + + let entries: Vec = fs::read_dir(path) + .unwrap() + .filter_map(|f| f.ok()) + .collect(); + let mut entry_list: Vec> = vec!{entries}; + loop { + let drained: Vec> = entry_list.drain(..).collect(); + for entries in drained { + for entry in entries { + let path = match entry.path().canonicalize() { + Ok(path) => match path.to_str() { + Some(path) => path.to_owned(), + None => { + eprintln!("failed to convert {:?} into a string", path); + continue + } + }, + Err(e) => { + eprintln!("failed to get absolute path: {e}"); + continue; + } + }; + + let file_type = match entry.file_type() { + Ok(file_type) => file_type, + Err(e) => { + eprintln!("error {} getting file type for {}", e, path); + continue; + } + }; + + if file_type.is_file() { + files.push(path); + } else if file_type.is_dir() { + let entries: Vec = fs::read_dir(&path) + .unwrap() + .filter_map(|f| f.ok()) + .collect(); + entry_list.push(entries); + directories.push(path); + } + } + } + + if entry_list.is_empty() { + break; + } + } + + (files, directories) +} + fn main() { let args = Args::parse(); @@ -75,5 +132,5 @@ fn main() { let uid = users.get(&args.user).expect("user not found"); let gid = groups.get(&args.group).expect("group not found"); - dbg!(uid, gid); + dbg!(traverse_filesystem(Path::new(&args.path))); }