From dd1d471820251dc9c5f60a5508e0e80711fb221a Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sat, 19 Oct 2024 14:26:28 +0200 Subject: fix(yt/cache): Don't try to delete video cache paths that don't exist anymore This is a quite common problem, because the download dir (residing on `/tmp/yt` by default) is cleared with every reboot, leaving the stale `cache_path` entries in the database. If a user would than run `yt download --force` these already deleted `cache_paths` should not be deleted again. --- yt/src/cache/mod.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/yt/src/cache/mod.rs b/yt/src/cache/mod.rs index 6ceef25..dfbc276 100644 --- a/yt/src/cache/mod.rs +++ b/yt/src/cache/mod.rs @@ -9,7 +9,7 @@ // If not, see . use anyhow::{Context, Result}; -use log::info; +use log::{debug, info}; use tokio::fs; use crate::{ @@ -26,13 +26,25 @@ async fn invalidate_video(app: &App, video: &Video, hard: bool) -> Result<()> { if hard { if let Some(path) = &video.cache_path { info!("Removing cached video at: '{}'", path.display()); - fs::remove_file(path).await.with_context(|| { - format!( - "Failed to delete video ('{}') cache path: '{}'.", - video.title, - path.display() - ) - })?; + if let Err(err) = fs::remove_file(path).await.map_err(|err| err.kind()) { + match err { + std::io::ErrorKind::NotFound => { + // The path is already gone + debug!( + "Not actually removing path: '{}'. \ + It is already gone.", + path.display() + ); + } + err => Err(std::io::Error::from(err)).with_context(|| { + format!( + "Failed to delete video ('{}') cache path: '{}'.", + video.title, + path.display() + ) + })?, + } + } } } -- cgit 1.4.1