hoc/src/count.rs

28 lines
902 B
Rust
Raw Normal View History

use crate::error::Result;
2022-08-18 16:23:09 +02:00
use std::{fs::{read_dir, ReadDir}, path::Path, result::Result as StdResult, iter::once};
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)
.count()
)
}
fn sub_directories(dir: ReadDir) -> impl Iterator<Item = ReadDir> {
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())
}