From 1c0dee8e60d9c1c989a3712e7d1e19debe981e8a Mon Sep 17 00:00:00 2001 From: diogo464 Date: Wed, 14 Jul 2021 15:07:32 +0100 Subject: Small changes to logging and docs. --- dotup_cli/src/utils.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'dotup_cli/src/utils.rs') diff --git a/dotup_cli/src/utils.rs b/dotup_cli/src/utils.rs index fad37ae..d284a22 100644 --- a/dotup_cli/src/utils.rs +++ b/dotup_cli/src/utils.rs @@ -1,4 +1,7 @@ -use std::path::{Path, PathBuf}; +use std::{ + collections::VecDeque, + path::{Path, PathBuf}, +}; use crate::prelude::*; @@ -123,3 +126,26 @@ pub fn collect_link_ids_by_base_paths( .map(|l| l.id()) .collect() } + +/// Returns a list of canonical paths to all the files in `dir`. This includes files in +/// subdirectories. +/// Fails if dir isnt a directory or if there is some other io error. +pub fn collect_files_in_dir(dir: impl Into) -> anyhow::Result> { + let mut paths = Vec::new(); + let mut dirs = VecDeque::new(); + dirs.push_back(dir.into()); + + while let Some(dir) = dirs.pop_front() { + for entry in std::fs::read_dir(dir)? { + let entry = entry?; + let filetype = entry.file_type()?; + if filetype.is_dir() { + dirs.push_back(entry.path()); + } else { + paths.push(entry.path()); + } + } + } + + Ok(paths) +} -- cgit