diff --git a/src/lib.rs b/src/lib.rs index 410e35e..e375b5b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ pub mod template; pub use error::{OrgError, OrgResult}; pub use organizer::{ - OrgConfig, OrgStats, organize_from_db, organize_from_directory, organize_track, + OrgConfig, OrgStats, cleanup_empty_dirs, organize_from_db, organize_from_directory, + organize_track, }; pub use template::DEFAULT_FORMAT; diff --git a/src/organizer.rs b/src/organizer.rs index eaafe84..1a40354 100644 --- a/src/organizer.rs +++ b/src/organizer.rs @@ -105,10 +105,10 @@ fn move_or_copy(source: &Path, target: &Path, copy: bool) -> OrgResult<()> { Ok(()) } -/// Remove empty directories starting from `dir` and walking up, stopping at `stop_at`. -fn cleanup_empty_dirs(dir: &Path, stop_at: &Path) { +/// Remove empty directories starting from `dir` and walking up, stopping before `stop_at`. +pub fn cleanup_empty_dirs(dir: &Path, stop_at: &Path) { let mut current = dir.to_owned(); - while current != stop_at && current.starts_with(stop_at) { + while current.starts_with(stop_at) && current != *stop_at { match std::fs::read_dir(¤t) { Ok(mut entries) => { if entries.next().is_none() { @@ -181,11 +181,11 @@ pub async fn organize_track( }; queries::tracks::update_metadata(conn, track.id, active).await?; - // Cleanup empty source directory + // Cleanup empty source directories up to the library root if !config.copy && let Some(dir) = source_dir { - cleanup_empty_dirs(&dir, &dir); + cleanup_empty_dirs(&dir, &config.target_dir); } Ok(true)