Fix startup panic if removing accounts directory fails (#27386)

* Remove contents of accounts directory if deleting the directory fails.
This commit is contained in:
Will Hickey 2022-08-25 20:35:12 -05:00 committed by GitHub
parent b1cff5d740
commit 5eefc256d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 46 additions and 1 deletions

View File

@ -2076,7 +2076,7 @@ fn move_and_async_delete_path(path: impl AsRef<Path> + Copy) {
"Path renaming failed: {}. Falling back to rm_dir in sync mode",
err.to_string()
);
std::fs::remove_dir_all(&path).unwrap();
delete_contents_of_path(path);
return;
}
@ -2088,6 +2088,51 @@ fn move_and_async_delete_path(path: impl AsRef<Path> + Copy) {
.unwrap();
}
/// Delete the files and subdirectories in a directory.
/// This is useful if the process does not have permission
/// to delete the top level directory it might be able to
/// delete the contents of that directory.
fn delete_contents_of_path(path: impl AsRef<Path> + Copy) {
if let Ok(dir_entries) = std::fs::read_dir(&path) {
for entry in dir_entries.flatten() {
let sub_path = entry.path();
let metadata = match entry.metadata() {
Ok(metadata) => metadata,
Err(err) => {
warn!(
"Failed to get metadata for {}. Error: {}",
sub_path.display(),
err.to_string()
);
break;
}
};
if metadata.is_dir() {
if let Err(err) = std::fs::remove_dir_all(&sub_path) {
warn!(
"Failed to remove sub directory {}. Error: {}",
sub_path.display(),
err.to_string()
);
}
} else if metadata.is_file() {
if let Err(err) = std::fs::remove_file(&sub_path) {
warn!(
"Failed to remove file {}. Error: {}",
sub_path.display(),
err.to_string()
);
}
}
}
} else {
warn!(
"Failed to read the sub paths of {}",
path.as_ref().display()
);
}
}
fn cleanup_accounts_paths(config: &ValidatorConfig) {
for accounts_path in &config.account_paths {
move_and_async_delete_path(accounts_path);