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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::collections::BTreeSet;
use crate::{
file::{init, init::Options, Metadata},
File,
};
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Init(#[from] init::Error),
}
impl File<'static> {
pub fn from_path_no_includes(path: impl Into<std::path::PathBuf>, source: crate::Source) -> Result<Self, Error> {
let path = path.into();
let trust = gix_sec::Trust::from_path_ownership(&path)?;
let mut buf = Vec::new();
std::io::copy(&mut std::fs::File::open(&path)?, &mut buf)?;
Ok(File::from_bytes_owned(
&mut buf,
Metadata::from(source).at(path).with(trust),
Default::default(),
)?)
}
pub fn from_paths_metadata(
path_meta: impl IntoIterator<Item = impl Into<Metadata>>,
options: Options<'_>,
) -> Result<Option<Self>, Error> {
let mut buf = Vec::with_capacity(512);
let err_on_nonexisting_paths = true;
Self::from_paths_metadata_buf(path_meta, &mut buf, err_on_nonexisting_paths, options)
}
pub fn from_paths_metadata_buf(
path_meta: impl IntoIterator<Item = impl Into<Metadata>>,
buf: &mut Vec<u8>,
err_on_non_existing_paths: bool,
options: Options<'_>,
) -> Result<Option<Self>, Error> {
let mut target = None;
let mut seen = BTreeSet::default();
for (path, mut meta) in path_meta.into_iter().filter_map(|meta| {
let mut meta = meta.into();
meta.path.take().map(|p| (p, meta))
}) {
if !seen.insert(path.clone()) {
continue;
}
buf.clear();
std::io::copy(
&mut match std::fs::File::open(&path) {
Ok(f) => f,
Err(err) if !err_on_non_existing_paths && err.kind() == std::io::ErrorKind::NotFound => continue,
Err(err) => return Err(err.into()),
},
buf,
)?;
meta.path = Some(path);
let config = Self::from_bytes_owned(buf, meta, options)?;
match &mut target {
None => {
target = Some(config);
}
Some(target) => {
target.append(config);
}
}
}
Ok(target)
}
}