Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: Check trash path #88

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions examples/delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::env;

Check warning on line 1 in examples/delete.rs

View workflow job for this annotation

GitHub Actions / build (macos-latest, stable)

unused import: `std::env`

Check warning on line 1 in examples/delete.rs

View workflow job for this annotation

GitHub Actions / build (macos-latest, stable)

unused import: `std::env`
use std::path::PathBuf;

Check warning on line 2 in examples/delete.rs

View workflow job for this annotation

GitHub Actions / build (macos-latest, stable)

unused import: `std::path::PathBuf`

Check warning on line 2 in examples/delete.rs

View workflow job for this annotation

GitHub Actions / build (macos-latest, stable)

unused import: `std::path::PathBuf`
use trash::{delete_all, Error};

Check warning on line 3 in examples/delete.rs

View workflow job for this annotation

GitHub Actions / build (macos-latest, stable)

unused imports: `Error`, `delete_all`

Check warning on line 3 in examples/delete.rs

View workflow job for this annotation

GitHub Actions / build (macos-latest, stable)

unused imports: `Error`, `delete_all`

#[cfg(target_os = "macos")]
fn main() {
println!("This example is not available on macOS");
}

#[cfg(any(
target_os = "windows",
all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android"))
))]
fn main() -> Result<(), Error> {
let args: Vec<PathBuf> = env::args().skip(1).map(String::into).collect();
delete_all(args)?;

Ok(())
}
21 changes: 21 additions & 0 deletions src/freedesktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ impl TrashContext {
let home_topdir = home_topdir(&mount_points)?;
debug!("The home topdir is {:?}", home_topdir);
let uid = unsafe { libc::getuid() };

full_paths.iter().try_for_each(|path| {
let topdir = get_topdir_for_path(path, &mount_points);

if topdir == home_topdir {
if path.starts_with(home_trash.as_path()) {
return Err(Error::TargetedTrash);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should no action be performed in this case?

} else {
return execute_on_mounted_trash_folders(uid, topdir, true, true, |trash_path| {
if path.starts_with(trash_path.as_path()) {
Err(Error::TargetedTrash)
} else {
Ok(())
}
});
}

Ok(())
})?;

for path in full_paths {
debug!("Deleting {:?}", path);
let topdir = get_topdir_for_path(&path, &mount_points);
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ pub enum Error {
/// and this error is returned, then it's guaranteed that none of the items is removed.
TargetedRoot,

/// One of the target items was a trash folder or inside a trash folder.
/// If a list of items are requested to be removed by a single function call (e.g. `delete_all`)
/// and this error is returned, then it's guaranteed that none of the items is removed.
TargetedTrash,

/// The `target` does not exist or the process has insufficient permissions to access it.
CouldNotAccess {
target: String,
Expand Down
Loading