hoc/src/count.rs

31 lines
907 B
Rust
Raw Permalink Normal View History

use crate::error::Result;
2022-08-22 15:20:24 +02:00
use std::{
fs::{read_dir, ReadDir},
iter::once,
path::Path,
result::Result as StdResult,
};
2022-08-22 09:51:57 +02:00
/// The on disk layout for served repos is `<service>/<user>/<repo>`
/// so to get the amount of repos, we just have to count everything
/// in `*/*/*` to get the count.
#[instrument]
2022-08-22 15:12:21 +02:00
pub fn count_repositories<P>(repo_path: P) -> Result<usize>
where
P: AsRef<Path> + std::fmt::Debug,
{
trace!("Counting repositories");
2020-05-15 13:34:48 +02:00
std::fs::create_dir_all(&repo_path)?;
2022-08-18 16:23:09 +02:00
Ok(once(read_dir(repo_path)?)
.flat_map(sub_directories)
.flat_map(sub_directories)
.flat_map(sub_directories)
2022-08-22 15:20:24 +02:00
.count())
2022-08-18 16:23:09 +02:00
}
fn sub_directories(dir: ReadDir) -> impl Iterator<Item = ReadDir> {
2022-08-22 15:20:24 +02:00
dir.filter_map(StdResult::ok)
.filter(|entry| entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
2022-08-18 16:23:09 +02:00
.filter_map(|entry| read_dir(entry.path()).ok())
}