-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontent_directory.rs
303 lines (268 loc) · 9.27 KB
/
content_directory.rs
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use std::env;
use std::fs::File;
use std::os::unix::fs::PermissionsExt;
use std::path;
use std::path::{Path, PathBuf};
use thiserror::Error;
use walkdir::WalkDir;
#[derive(Error, Debug)]
pub enum ContentDirectoryFromRootError {
#[error("Unable to use directory root '{}': {}", .root.display(), .message)]
InvalidRootPath { root: PathBuf, message: String },
#[error("Unable to use directory root '{}': {}", .root.display(), .source)]
WalkDirError {
root: PathBuf,
source: walkdir::Error,
},
#[error(transparent)]
DirectoryEntryError(#[from] ContentFileError),
}
#[derive(Error, Debug)]
#[error("Content file error: {}", .0)]
pub struct ContentFileError(String);
/// A filesystem directory containing content.
pub struct ContentDirectory {
files: Vec<ContentFile>,
#[cfg(test)]
root: PathBuf,
}
impl ContentDirectory {
pub fn from_root<P: AsRef<Path>>(
absolute_root: &P,
) -> Result<Self, ContentDirectoryFromRootError> {
let absolute_root_path = absolute_root.as_ref();
if !absolute_root_path.is_absolute() {
return Err(ContentDirectoryFromRootError::InvalidRootPath {
message: String::from("Root path must be absolute."),
root: PathBuf::from(absolute_root_path),
});
}
let mut files = Vec::new();
let walker = WalkDir::new(absolute_root_path)
.follow_links(true)
.min_depth(1)
.into_iter()
.filter_entry(|entry| {
// Skip hidden files/directories.
let is_hidden = entry
.file_name()
.to_str()
.map(|name| name.starts_with('.'))
.unwrap_or(false);
!is_hidden
});
for dir_entry_result in walker {
let dir_entry = dir_entry_result.map_err(|walkdir_error| {
ContentDirectoryFromRootError::WalkDirError {
source: walkdir_error,
root: PathBuf::from(absolute_root_path),
}
})?;
{
let entry_path = dir_entry.path().to_path_buf();
if dir_entry.file_type().is_file() {
let content_file =
ContentFile::from_root_and_path(absolute_root_path, entry_path)
.map_err(ContentDirectoryFromRootError::from)?;
files.push(content_file);
}
}
}
Ok(ContentDirectory {
files,
#[cfg(test)]
root: absolute_root_path.into(),
})
}
#[cfg(test)]
pub fn root(&self) -> &Path {
&self.root
}
}
pub struct ContentFile {
absolute_path: String,
relative_path: String,
is_executable: bool,
relative_path_without_extensions: String,
extensions: Vec<String>,
file: File,
}
impl ContentFile {
pub const PATH_SEPARATOR: char = '/';
fn from_root_and_path(
content_directory_root: &Path,
absolute_content_file_path: PathBuf,
) -> Result<Self, ContentFileError> {
if path::MAIN_SEPARATOR != Self::PATH_SEPARATOR {
return Err(ContentFileError(format!(
"Platforms that use '{}' as a path separator are not supported",
path::MAIN_SEPARATOR
)));
}
let root = match content_directory_root.to_str() {
Some(unicode_root) => unicode_root,
None => {
return Err(ContentFileError(format!(
"Non-unicode directory root (path is similar to '{}')",
content_directory_root.display(),
)))
}
};
let absolute_path = String::from(
absolute_content_file_path
.to_str()
.ok_or_else(|| ContentFileError(String::from("Path was not unicode.")))?,
);
let relative_path = absolute_content_file_path
.strip_prefix(root)
.map_err(|strip_prefix_error| {
ContentFileError(format!(
"Content file path '{}' did not start with expected prefix '{}': {}",
absolute_content_file_path.display(),
root,
strip_prefix_error
))
})?
.to_str()
.map(String::from)
.ok_or_else(|| ContentFileError(String::from("Path was not unicode.")))?;
let file = File::open(&absolute_content_file_path).map_err(|io_error| {
ContentFileError(format!(
"Unable to open file '{}' in '{}' for reading: {}",
relative_path, root, io_error
))
})?;
let basename = absolute_content_file_path
.file_name()
.ok_or_else(|| {
ContentFileError(format!(
"Unable to get basename of '{}' in '{}'",
relative_path, root,
))
})?
.to_str()
.ok_or_else(|| ContentFileError(String::from("File had a non-unicode basename.")))?;
// Conventions around hidden files, whether a file is executable, etc
// differ across platforms. It wouldn't be hard to implement this, but
// Operator does not currently run its CI checks on non-unix platforms
// so it would be too easy to introduce regressions.
let (extensions, is_executable) = if !cfg!(unix) {
return Err(ContentFileError(format!(
"Operator does not currently support your operating system ({})",
env::consts::OS,
)));
} else {
// If the basename begins with `.` its first chunk isn't considered
// an "extension".
let non_extension_components = if basename.starts_with('.') { 2 } else { 1 };
let extensions = basename
.split('.')
.skip(non_extension_components)
.map(String::from)
.collect::<Vec<String>>();
let permissions = file
.metadata()
.map_err(|io_error| {
ContentFileError(format!(
"Unable to query metadata for content file '{}': {}",
absolute_content_file_path.display(),
io_error
))
})?
.permissions();
let is_executable = permissions.mode() & 0o111 != 0;
(extensions, is_executable)
};
let relative_path_without_extensions = String::from({
let extensions_len = extensions.iter().fold(0, |len, extension| {
// Extra 1 is to count . in the extensions.
len + extension.len() + 1
});
&relative_path[0..(relative_path.len() - extensions_len)]
});
Ok(ContentFile {
absolute_path,
relative_path,
relative_path_without_extensions,
extensions,
file,
is_executable,
})
}
pub fn absolute_path(&self) -> &str {
&self.absolute_path
}
pub fn relative_path(&self) -> &str {
&self.relative_path
}
pub fn is_executable(&self) -> bool {
self.is_executable
}
pub fn relative_path_without_extensions(&self) -> &str {
&self.relative_path_without_extensions
}
pub fn extensions(&self) -> &[String] {
&self.extensions
}
pub fn file(&self) -> &File {
&self.file
}
pub fn into_file(self) -> File {
self.file
}
}
impl IntoIterator for ContentDirectory {
type Item = ContentFile;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.files.into_iter()
}
}
impl<'a> IntoIterator for &'a ContentDirectory {
type Item = &'a ContentFile;
type IntoIter = std::slice::Iter<'a, ContentFile>;
fn into_iter(self) -> Self::IntoIter {
self.files.iter()
}
}
#[cfg(test)]
impl PartialEq for ContentDirectory {
fn eq(&self, other: &Self) -> bool {
self.root() == other.root()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_lib::*;
use std::fs;
#[test]
fn directory_can_be_created_from_valid_root() {
let path = fs::canonicalize("./src").expect("Canonicalizing path failed");
let result = ContentDirectory::from_root(&path);
assert!(
result.is_ok(),
"Unable to use directory at '{}': {}",
path.display(),
result.err().unwrap().to_string()
);
}
#[test]
fn directory_root_must_exist() {
let result = ContentDirectory::from_root(&sample_path("this/does/not/actually/exist"));
assert!(
result.is_err(),
"Directory was successfully created from non-existent path",
);
}
#[test]
fn directory_root_must_be_absolute_path() {
let non_absolute_path = "./src";
let result = ContentDirectory::from_root(&non_absolute_path);
assert!(
result.is_err(),
"ContentDirectory was successfully created from non-absolute path '{}', but this should have failed",
non_absolute_path,
);
}
}