Skip to content

Commit 85e87a7

Browse files
committed
Filter out hidden paths in content directories earlier on.
This can speed up initialization since we don't have to traverse into hidden directories, and helps cut down on the number of file descriptors that are open at the same time. The only time I've noticed issues is when there's .git metadata in the content directory, but that'll probably be common.
1 parent 352d42c commit 85e87a7

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

src/content/content_directory.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,17 @@ impl ContentDirectory {
4747
let mut files = Vec::new();
4848
let walker = WalkDir::new(absolute_root_path)
4949
.follow_links(true)
50-
.min_depth(1);
50+
.min_depth(1)
51+
.into_iter()
52+
.filter_entry(|entry| {
53+
// Skip hidden files/directories.
54+
let is_hidden = entry
55+
.file_name()
56+
.to_str()
57+
.map(|name| name.starts_with('.'))
58+
.unwrap_or(false);
59+
!is_hidden
60+
});
5161
for dir_entry_result in walker {
5262
let dir_entry = dir_entry_result.map_err(|walkdir_error| {
5363
ContentDirectoryFromRootError::WalkDirError {
@@ -82,7 +92,6 @@ impl ContentDirectory {
8292
pub struct ContentFile {
8393
absolute_path: String,
8494
relative_path: String,
85-
is_hidden: bool,
8695
is_executable: bool,
8796
relative_path_without_extensions: String,
8897
extensions: Vec<String>,
@@ -154,25 +163,21 @@ impl ContentFile {
154163
// differ across platforms. It wouldn't be hard to implement this, but
155164
// Operator does not currently run its CI checks on non-unix platforms
156165
// so it would be too easy to introduce regressions.
157-
let (extensions, is_hidden, is_executable) = if !cfg!(unix) {
166+
let (extensions, is_executable) = if !cfg!(unix) {
158167
return Err(ContentFileError(format!(
159168
"Operator does not currently support your operating system ({})",
160169
env::consts::OS,
161170
)));
162171
} else {
163-
// If the basename begins with `.` its first chunk isn't considered an "extension".
172+
// If the basename begins with `.` its first chunk isn't considered
173+
// an "extension".
164174
let non_extension_components = if basename.starts_with('.') { 2 } else { 1 };
165175
let extensions = basename
166176
.split('.')
167177
.skip(non_extension_components)
168178
.map(String::from)
169179
.collect::<Vec<String>>();
170180

171-
// The file is hidden if any of its relative path components starts
172-
// with a dot.
173-
let is_hidden = relative_path.starts_with('.')
174-
|| relative_path.contains(&format!("{}.", Self::PATH_SEPARATOR));
175-
176181
let permissions = file
177182
.metadata()
178183
.map_err(|io_error| {
@@ -185,7 +190,7 @@ impl ContentFile {
185190
.permissions();
186191
let is_executable = permissions.mode() & 0o111 != 0;
187192

188-
(extensions, is_hidden, is_executable)
193+
(extensions, is_executable)
189194
};
190195

191196
let relative_path_without_extensions = String::from({
@@ -202,7 +207,6 @@ impl ContentFile {
202207
relative_path_without_extensions,
203208
extensions,
204209
file,
205-
is_hidden,
206210
is_executable,
207211
})
208212
}
@@ -215,10 +219,6 @@ impl ContentFile {
215219
&self.relative_path
216220
}
217221

218-
pub fn is_hidden(&self) -> bool {
219-
self.is_hidden
220-
}
221-
222222
pub fn is_executable(&self) -> bool {
223223
self.is_executable
224224
}

src/content/content_engine.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,8 @@ where
107107
content_directory: ContentDirectory,
108108
server_info: ServerInfo,
109109
) -> Result<Arc<RwLock<Self>>, ContentLoadingError> {
110-
let content_item_entries = content_directory
111-
.into_iter()
112-
.filter(|entry| !entry.is_hidden());
113-
114110
let (index_entries, content_registry, handlebars_registry) =
115-
Self::set_up_registries(content_item_entries)?;
111+
Self::set_up_registries(content_directory)?;
116112

117113
let content_engine = FilesystemBasedContentEngine {
118114
server_info,

tests/integration_tests.rs

-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ async fn render_everything_for_snapshots(
146146

147147
let render_operations = content_directory
148148
.into_iter()
149-
.filter(|content_file| !content_file.is_hidden())
150149
.map(|content_file| async move {
151150
let route = content_file.relative_path_without_extensions();
152151
let empty_string = String::from("");

0 commit comments

Comments
 (0)