Skip to content

Commit db18bab

Browse files
committed
report and skip broken symlinks
1 parent 5f347c5 commit db18bab

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/build/packages.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ fn matches_filter(filter: &Option<regex::Regex>, path: &str) -> bool {
122122
}
123123
}
124124

125+
fn is_broken_symlink<P: AsRef<Path>>(path: P) -> bool {
126+
if let Ok(metadata) = fs::symlink_metadata(&path) {
127+
if metadata.file_type().is_symlink() {
128+
return match fs::read_link(&path) {
129+
Ok(link_target) => fs::metadata(&link_target).is_err(),
130+
Err(_) => true,
131+
};
132+
}
133+
}
134+
false
135+
}
136+
125137
pub fn read_folders(
126138
filter: &Option<regex::Regex>,
127139
package_dir: &Path,
@@ -142,6 +154,10 @@ pub fn read_folders(
142154

143155
for entry in fs::read_dir(package_dir.join(&path_buf))? {
144156
let entry_path_buf = entry.map(|entry| entry.path())?;
157+
if is_broken_symlink(&entry_path_buf) {
158+
println!("Warning: '{}' is a broken symlink", entry_path_buf.display());
159+
continue;
160+
}
145161
let metadata = fs::metadata(&entry_path_buf)?;
146162
let name = entry_path_buf.file_name().unwrap().to_str().unwrap().to_string();
147163

@@ -150,7 +166,7 @@ pub fn read_folders(
150166
if metadata.file_type().is_dir() && recurse {
151167
match read_folders(filter, package_dir, &new_path, recurse) {
152168
Ok(s) => map.extend(s),
153-
Err(e) => println!("Error reading directory: {}", e),
169+
Err(e) => println!("Error reading directory {}: {}", entry_path_buf.display(), e),
154170
}
155171
}
156172

@@ -452,13 +468,14 @@ pub fn get_source_files(
452468
if type_ != &Some("dev".to_string()) {
453469
match read_folders(filter, package_dir, path_dir, recurse) {
454470
Ok(files) => map.extend(files),
455-
Err(_e) if type_ == &Some("dev".to_string()) => {
471+
Err(e) if type_ == &Some("dev".to_string()) => {
456472
println!(
457-
"Could not read folder: {}... Probably ok as type is dev",
458-
path_dir.to_string_lossy()
473+
"Could not read folder {}: {}... Probably ok as type is dev",
474+
path_dir.to_string_lossy(),
475+
e
459476
)
460477
}
461-
Err(_e) => println!("Could not read folder: {}...", path_dir.to_string_lossy()),
478+
Err(e) => println!("Could not read folder {}: {}", path_dir.to_string_lossy(), e),
462479
}
463480
}
464481

0 commit comments

Comments
 (0)