Skip to content

Commit

Permalink
Fix: create partial trash directory if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementNerma committed Dec 18, 2023
1 parent a9fe2de commit fe670fd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 26 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.1.5"
version = "3.1.6"
authors = ["Clément Nerma <clement.nerma@gmail.com>"]
edition = "2021"
license = "Apache-2.0"
Expand Down
26 changes: 12 additions & 14 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,15 @@ pub fn remove(action: MoveToTrash) -> Result<()> {
trash_dir.display()
)
})?;
}

let trash_transfer_dir = trash_dir.join(TRASH_TRANSFER_DIRNAME);

fs::create_dir(&trash_dir.join(TRASH_TRANSFER_DIRNAME)).with_context(|| {
if !trash_transfer_dir.exists() {
fs::create_dir(&trash_transfer_dir).with_context(|| {
format!(
"Failed to create trash's partial transfer directory at path '{}'",
trash_dir.join(TRASH_TRANSFER_DIRNAME).display()
trash_transfer_dir.display()
)
})?;
}
Expand All @@ -136,18 +140,12 @@ pub fn remove(action: MoveToTrash) -> Result<()> {
fs::rename(&path, &trash_item_path)
.with_context(|| format!("Failed to move item '{}' to trash", path.display()))?;

for i in 1..=3 {
if let Err(err) = move_transferred_trash_item(&trash_item) {
debug!(
"Failed to rename transferred item in trash (try n°{}): {}",
i, err
);

if i == 3 {
bail!("Failed to move item '{}' to trash: {err:?}", path.display());
}
}
}
fs::rename(&trash_item_path, trash_item.complete_trash_item_path()).with_context(|| {
format!(
"Failed to move fully transferred item '{}' to trash",
path.display()
)
})?;
}

Ok(())
Expand Down
10 changes: 0 additions & 10 deletions src/fsutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,6 @@ pub fn expect_single_trash_item(filename: &str, id: Option<&str>) -> Result<Tras
}
}

/// Move a partial item to the trash's main directory once the transfer is complete
pub fn move_transferred_trash_item(item: &TrashedItem) -> Result<()> {
fs::rename(
item.transfer_trash_item_path(),
item.complete_trash_item_path(),
)?;

Ok(())
}

/// Convert a size in bytes to a human-readable size
pub fn human_readable_size(bytes: u64) -> String {
let names = ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB"];
Expand Down

0 comments on commit fe670fd

Please sign in to comment.