From aa95671996bc55f69305505c5da2e715a85da189 Mon Sep 17 00:00:00 2001 From: jakka Date: Thu, 19 Jun 2025 11:14:37 +0300 Subject: cleaned up abspath --- src/db.rs | 23 ++++++++--------------- src/files.rs | 10 +++++----- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/db.rs b/src/db.rs index 742c003..94642ce 100644 --- a/src/db.rs +++ b/src/db.rs @@ -32,10 +32,9 @@ impl Database { } pub async fn insert_file(&self, filename: impl AsRef) -> Result<()> { - let abs_filename = filename.as_ref().canonicalize()?; - let toencode = !matches!(get_vendor(&abs_filename)?.as_str(), CURRENT_VENDOR); + let toencode = !matches!(get_vendor(&filename)?.as_str(), CURRENT_VENDOR); - let modtime = abs_filename + let modtime = filename.as_ref() .metadata()? .modified()? .duration_since(UNIX_EPOCH)? @@ -44,7 +43,7 @@ impl Database { self.0 .execute( ADD_NEW_ITEM, - params![abs_filename.to_str().unwrap(), toencode, modtime], + params![filename.as_ref().to_str().unwrap(), toencode, modtime], ) .await?; @@ -52,9 +51,7 @@ impl Database { } pub async fn update_file(&self, filename: impl AsRef) -> Result<()> { - let abs_filename = filename.as_ref().canonicalize()?; - - let modtime = abs_filename + let modtime = filename.as_ref() .metadata()? .modified()? .duration_since(UNIX_EPOCH)? @@ -63,7 +60,7 @@ impl Database { self.0 .execute( REPLACE_ITEM, - params![abs_filename.to_str().unwrap(), false, modtime], + params![filename.as_ref().to_str().unwrap(), false, modtime], ) .await?; @@ -71,11 +68,9 @@ impl Database { } pub async fn check_file(&self, filename: impl AsRef) -> Result { - let abs_filename = filename.as_ref().canonicalize()?; - if let Some(row) = self .0 - .query(CHECK_FILE, params!(abs_filename.to_str().unwrap())) + .query(CHECK_FILE, params!(filename.as_ref().to_str().unwrap())) .await? .next() .await? @@ -87,11 +82,9 @@ impl Database { } pub async fn get_modtime(&self, filename: impl AsRef) -> Result { - let abs_filename = filename.as_ref().canonicalize()?; - if let Some(row) = self .0 - .query(FETCH_MODTIME, params!(abs_filename.to_str().unwrap())) + .query(FETCH_MODTIME, params!(filename.as_ref().to_str().unwrap())) .await? .next() .await? @@ -111,7 +104,7 @@ impl Database { self.0.execute(DEDUPE_DB, ()).await?; let mut query_res = self.0.query(FETCH_FILES, ()).await?; while let Ok(Some(row)) = query_res.next().await { - let path = Path::new(row.get_str(0)?).canonicalize()?; + let path = Path::new(row.get_str(0)?).to_path_buf(); let conn = self.0.clone(); tasks.spawn(async move { if !path.exists() { diff --git a/src/files.rs b/src/files.rs index 743e489..045c5b4 100644 --- a/src/files.rs +++ b/src/files.rs @@ -105,16 +105,16 @@ pub async fn index_files_recursively(path: impl AsRef, conn: &Database) -> Ok(()) } -fn check_path(folderpath: Option<&PathBuf>) -> (PathBuf, bool) { +fn check_path(folderpath: Option<&PathBuf>) -> Result<(PathBuf, bool)> { if let Some(real_path) = folderpath { - (real_path.to_owned(), false) + Ok((real_path.canonicalize()?, false)) } else { - (PathBuf::new(), true) + Ok((PathBuf::new(), true)) } } pub async fn reencode_files(folderpath: Option<&PathBuf>, conn: &Database) -> Result<()> { - let (path, nocheck) = check_path(folderpath); + let (path, nocheck) = check_path(folderpath)?; let stream = conn.get_toencode_stream().await?; pin_mut!(stream); @@ -159,7 +159,7 @@ pub async fn reencode_files(folderpath: Option<&PathBuf>, conn: &Database) -> Re } pub async fn count_reencode_files(folderpath: Option<&PathBuf>, conn: &Database) -> Result { - let (path, nocheck) = check_path(folderpath); + let (path, nocheck) = check_path(folderpath)?; let mut counter: u64 = 0; let stream = conn.get_toencode_stream().await?; -- cgit v1.3.1