Skip to content

Commit 4c68a4a

Browse files
committed
mtime_recursive: Add some comments and debugging diagnostics.
1 parent cee088b commit 4c68a4a

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

src/cargo/util/paths.rs

+49-4
Original file line numberDiff line numberDiff line change
@@ -190,28 +190,73 @@ pub fn mtime_recursive(path: &Path) -> CargoResult<FileTime> {
190190
let max_meta = walkdir::WalkDir::new(path)
191191
.follow_links(true)
192192
.into_iter()
193-
.filter_map(|e| e.ok())
193+
.filter_map(|e| match e {
194+
Ok(e) => Some(e),
195+
Err(e) => {
196+
// Ignore errors while walking. If Cargo can't access it, the
197+
// build script probably can't access it, either.
198+
log::debug!("failed to determine mtime while walking directory: {}", e);
199+
None
200+
}
201+
})
194202
.filter_map(|e| {
195203
if e.path_is_symlink() {
196204
// Use the mtime of both the symlink and its target, to
197205
// handle the case where the symlink is modified to a
198206
// different target.
199-
let sym_meta = std::fs::symlink_metadata(e.path()).ok()?;
207+
let sym_meta = match std::fs::symlink_metadata(e.path()) {
208+
Ok(m) => m,
209+
Err(err) => {
210+
// I'm not sure when this is really possible (maybe a
211+
// race with unlinking?). Regardless, if Cargo can't
212+
// read it, the build script probably can't either.
213+
log::debug!(
214+
"failed to determine mtime while fetching symlink metdata of {}: {}",
215+
e.path().display(),
216+
err
217+
);
218+
return None;
219+
}
220+
};
200221
let sym_mtime = FileTime::from_last_modification_time(&sym_meta);
201222
// Walkdir follows symlinks.
202223
match e.metadata() {
203224
Ok(target_meta) => {
204225
let target_mtime = FileTime::from_last_modification_time(&target_meta);
205226
Some(sym_mtime.max(target_mtime))
206227
}
207-
Err(_) => Some(sym_mtime),
228+
Err(err) => {
229+
// Can't access the symlink target. If Cargo can't
230+
// access it, the build script probably can't access
231+
// it either.
232+
log::debug!(
233+
"failed to determine mtime of symlink target for {}: {}",
234+
e.path().display(),
235+
err
236+
);
237+
Some(sym_mtime)
238+
}
208239
}
209240
} else {
210-
let meta = e.metadata().ok()?;
241+
let meta = match e.metadata() {
242+
Ok(m) => m,
243+
Err(err) => {
244+
// I'm not sure when this is really possible (maybe a
245+
// race with unlinking?). Regardless, if Cargo can't
246+
// read it, the build script probably can't either.
247+
log::debug!(
248+
"failed to determine mtime while fetching metadata of {}: {}",
249+
e.path().display(),
250+
err
251+
);
252+
return None;
253+
}
254+
};
211255
Some(FileTime::from_last_modification_time(&meta))
212256
}
213257
})
214258
.max()
259+
// or_else handles the case where there are no files in the directory.
215260
.unwrap_or_else(|| FileTime::from_last_modification_time(&meta));
216261
Ok(max_meta)
217262
}

0 commit comments

Comments
 (0)