2019-06-12 21:51:54 +02:00
|
|
|
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};
|
2019-06-12 21:51:54 +02:00
|
|
|
|
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.
|
2020-10-30 16:40:35 +01:00
|
|
|
#[instrument]
|
2022-08-22 15:12:21 +02:00
|
|
|
pub fn count_repositories<P>(repo_path: P) -> Result<usize>
|
2019-06-12 21:51:54 +02:00
|
|
|
where
|
2020-10-30 16:40:35 +01:00
|
|
|
P: AsRef<Path> + std::fmt::Debug,
|
2019-06-12 21:51:54 +02:00
|
|
|
{
|
2020-10-30 16:40:35 +01:00
|
|
|
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
|
2019-06-12 21:51:54 +02:00
|
|
|
.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())
|
2019-06-12 21:51:54 +02:00
|
|
|
}
|