Show More
@@ -13,7 +13,6 b' use format_bytes::format_bytes;' | |||||
13 | use lazy_static::lazy_static; |
|
13 | use lazy_static::lazy_static; | |
14 | use regex::bytes::Regex; |
|
14 | use regex::bytes::Regex; | |
15 | use std::collections::HashMap; |
|
15 | use std::collections::HashMap; | |
16 | use std::io; |
|
|||
17 | use std::path::{Path, PathBuf}; |
|
16 | use std::path::{Path, PathBuf}; | |
18 |
|
17 | |||
19 | lazy_static! { |
|
18 | lazy_static! { | |
@@ -97,12 +96,16 b' impl ConfigLayer {' | |||||
97 | while let Some((index, bytes)) = lines_iter.next() { |
|
96 | while let Some((index, bytes)) = lines_iter.next() { | |
98 | if let Some(m) = INCLUDE_RE.captures(&bytes) { |
|
97 | if let Some(m) = INCLUDE_RE.captures(&bytes) { | |
99 | let filename_bytes = &m[1]; |
|
98 | let filename_bytes = &m[1]; | |
100 | let filename_to_include = get_path_from_bytes(&filename_bytes); |
|
99 | // `Path::parent` only fails for the root directory, | |
101 | let (include_src, result) = |
|
100 | // which `src` can’t be since we’ve managed to open it as a file. | |
102 | read_include(&src, &filename_to_include); |
|
101 | let dir = src | |
103 | let data = result.for_file(filename_to_include)?; |
|
102 | .parent() | |
|
103 | .expect("Path::parent fail on a file we’ve read"); | |||
|
104 | // `Path::join` with an absolute argument correctly ignores the base path | |||
|
105 | let filename = dir.join(&get_path_from_bytes(&filename_bytes)); | |||
|
106 | let data = std::fs::read(&filename).for_file(&filename)?; | |||
104 | layers.push(current_layer); |
|
107 | layers.push(current_layer); | |
105 |
layers.extend(Self::parse(& |
|
108 | layers.extend(Self::parse(&filename, &data)?); | |
106 | current_layer = Self::new(ConfigOrigin::File(src.to_owned())); |
|
109 | current_layer = Self::new(ConfigOrigin::File(src.to_owned())); | |
107 | } else if let Some(_) = EMPTY_RE.captures(&bytes) { |
|
110 | } else if let Some(_) = EMPTY_RE.captures(&bytes) { | |
108 | } else if let Some(m) = SECTION_RE.captures(&bytes) { |
|
111 | } else if let Some(m) = SECTION_RE.captures(&bytes) { | |
@@ -234,18 +237,3 b' pub enum ConfigError {' | |||||
234 | fn make_regex(pattern: &'static str) -> Regex { |
|
237 | fn make_regex(pattern: &'static str) -> Regex { | |
235 | Regex::new(pattern).expect("expected a valid regex") |
|
238 | Regex::new(pattern).expect("expected a valid regex") | |
236 | } |
|
239 | } | |
237 |
|
||||
238 | /// Includes are relative to the file they're defined in, unless they're |
|
|||
239 | /// absolute. |
|
|||
240 | fn read_include( |
|
|||
241 | old_src: &Path, |
|
|||
242 | new_src: &Path, |
|
|||
243 | ) -> (PathBuf, io::Result<Vec<u8>>) { |
|
|||
244 | if new_src.is_absolute() { |
|
|||
245 | (new_src.to_path_buf(), std::fs::read(&new_src)) |
|
|||
246 | } else { |
|
|||
247 | let dir = old_src.parent().unwrap(); |
|
|||
248 | let new_src = dir.join(&new_src); |
|
|||
249 | (new_src.to_owned(), std::fs::read(&new_src)) |
|
|||
250 | } |
|
|||
251 | } |
|
General Comments 0
You need to be logged in to leave comments.
Login now