Skip to content

Commit

Permalink
Add home directory specialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementNerma committed May 7, 2024
1 parent 60f386a commit 9f52ea1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trasher"
version = "3.3.1"
version = "3.3.2"
authors = ["Clément Nerma <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
Expand Down
26 changes: 11 additions & 15 deletions src/fsutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,8 @@ pub static ALWAYS_EXCLUDE_DIRS: &[&str] = &[
pub fn determine_trash_dir_for(item: &Path, config: &Config) -> Result<PathBuf> {
debug!("Determining trasher directory for item: {}", item.display());

let parent_dir = match determine_mountpoint_for(item, config)? {
Some(path) => path,
None => dirs::home_dir().context("Failed to determine path to user's home directory")?,
};

Ok(parent_dir.join(TRASH_DIR_NAME))
}
let home_dir = dirs::home_dir().context("Failed to determine path to user's home directory")?;

/// Determine the (canonicalized) path to the mountpoint the provided path is on
pub fn determine_mountpoint_for(item: &Path, config: &Config) -> Result<Option<PathBuf>> {
let mut exclude = config
.exclude
.iter()
Expand Down Expand Up @@ -79,13 +71,18 @@ pub fn determine_mountpoint_for(item: &Path, config: &Config) -> Result<Option<P
// Don't canonicalize excluded item paths
// NOTE: Only works if item path is absolute
if exclude.iter().any(|dir| item.starts_with(dir)) {
return Ok(None);
return Ok(home_dir.join(TRASH_DIR_NAME));
}

let item = fs::canonicalize(item)
.with_context(|| format!("Failed to canonicalize item path: {}\n\nTip: you can exclude this directory using --exclude.", item.display()))?;

let mountpoints = mountpaths().context("Failed to list system mountpoints")?;
let mut mountpoints = mountpaths().context("Failed to list system mountpoints")?;

// Add home directory for specialization
// e.g. if "/home" is a mounted directory, and we delete an item instead "/home/$USER",
// this line will allow the algorithm to pick the more specialized "/home/$USER" instead
mountpoints.push(home_dir.clone());

let mut found = None::<PathBuf>;

Expand Down Expand Up @@ -124,7 +121,8 @@ pub fn determine_mountpoint_for(item: &Path, config: &Config) -> Result<Option<P
}

if exclude.iter().any(|parent| item.starts_with(parent)) {
return Ok(None);
found = None;
break;
}

if found.is_none() || matches!(found, Some(ref prev) if canon_mountpoint.starts_with(prev))
Expand All @@ -133,9 +131,7 @@ pub fn determine_mountpoint_for(item: &Path, config: &Config) -> Result<Option<P
}
}

println!("{found:?}");

Ok(found)
Ok(found.unwrap_or(home_dir).join(TRASH_DIR_NAME))
}

/// List all trash directories
Expand Down

0 comments on commit 9f52ea1

Please sign in to comment.