add os error handling

This commit is contained in:
blek 2023-10-27 00:12:32 +10:00
parent 06d295cf49
commit 9b1e78a21c
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 23 additions and 11 deletions

View File

@ -42,17 +42,17 @@ async fn check_key(key: String, mut client: redis::Client) -> bool {
false false
} }
pub async fn check_file(file: String, keys: Vec<String>, prefix: String) -> bool { pub async fn check_file(file: String, keys: Vec<String>, prefix: String) -> Result<bool, String> {
if ! keys.iter().find(|x| x.chars().skip(prefix.len() + 4 + 2).collect::<String>() == file).is_none() { if ! keys.iter().find(|x| x.chars().skip(prefix.len() + 4 + 2).collect::<String>() == file).is_none() {
#[cfg(debug_assertions)] { #[cfg(debug_assertions)] {
log::debug!("File {file} is marked for deletion because it exists in the filesystem, but is not in the database"); log::debug!("File {file} is marked for deletion because it exists in the filesystem, but is not in the database");
} }
let _ = tokio::fs::remove_file(file).await; let _ = tokio::fs::remove_file(file).await.map_err(|err| err.to_string())?;
return true return Ok(true)
} }
false Ok(false)
} }
// check that all files in filesystem exist in the database // check that all files in filesystem exist in the database
@ -78,12 +78,11 @@ pub async fn fskeep(state: State) -> Result<(), Box<dyn Error>> {
let mut redis = state.redis.clone(); let mut redis = state.redis.clone();
let keys: Vec<String> = redis.keys(format!("{}*", state.env.redis.prefix))?; let keys: Vec<String> = redis.keys(format!("{}*", state.env.redis.prefix))?;
let objects = keys.len();
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
log::debug!("Got {} DB objects", files.len()); log::debug!("Got {} DB objects", files.len());
let mut set: JoinSet<bool> = JoinSet::new(); let mut set: JoinSet<Result<bool, String>> = JoinSet::new();
for file in files { for file in files {
set.spawn(check_file(file, keys.clone(), state.env.redis.prefix.clone())); set.spawn(check_file(file, keys.clone(), state.env.redis.prefix.clone()));
@ -92,13 +91,26 @@ pub async fn fskeep(state: State) -> Result<(), Box<dyn Error>> {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
let mut del_count = 0_u32; let mut del_count = 0_u32;
while let Some(_deleted) = set.join_next().await { let mut errors: Vec<String> = vec![];
#[cfg(debug_assertions)] {
if _deleted.is_ok() { while let Some(deleted) = set.join_next().await {
if _deleted.unwrap() { if let Ok(deleted) = deleted {
del_count += 1; if let Ok(deleted) = deleted {
#[cfg(debug_assertions)]
if deleted {
del_count += 1
} }
} }
if let Err(err) = deleted {
errors.push(err);
}
}
}
if errors.len() != 0 {
log::error!("Got the following errors while deleting files:");
for error in errors.iter() {
log::error!("\t {}", error)
} }
} }