1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use std::path::PathBuf;
use crate::{
pack,
store::{compound, loose},
};
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs)]
pub enum Error {
#[error("The objects directory at '{0}' is not an accessible directory")]
Inaccessible(PathBuf),
#[error(transparent)]
Pack(#[from] pack::bundle::init::Error),
#[error(transparent)]
Alternate(#[from] Box<crate::alternate::Error>),
}
impl compound::Store {
pub fn at(objects_directory: impl Into<PathBuf>) -> Result<compound::Store, Error> {
let loose_objects = objects_directory.into();
if !loose_objects.is_dir() {
return Err(Error::Inaccessible(loose_objects));
}
let packs = match std::fs::read_dir(loose_objects.join("pack")) {
Ok(entries) => {
let mut packs_and_sizes = entries
.filter_map(Result::ok)
.filter_map(|e| e.metadata().map(|md| (e.path(), md)).ok())
.filter(|(_, md)| md.file_type().is_file())
.filter(|(p, _)| p.extension().unwrap_or_default() == "idx")
.map(|(p, md)| pack::Bundle::at(p).map(|b| (b, md.len())))
.collect::<Result<Vec<_>, _>>()?;
packs_and_sizes.sort_by_key(|e| e.1);
packs_and_sizes.into_iter().rev().map(|(b, _)| b).collect()
}
Err(_) => Vec::new(),
};
Ok(compound::Store {
loose: loose::Store::at(loose_objects),
bundles: packs,
})
}
}