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 all 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(())
}
44 changes: 43 additions & 1 deletion 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 Expand Up @@ -740,7 +761,7 @@ mod tests {
env,
ffi::OsString,
fmt,
fs::File,
fs::{remove_dir_all, File},
os::unix,
path::{Path, PathBuf},
process::Command,
Expand Down Expand Up @@ -840,6 +861,27 @@ mod tests {
}
}

#[test]
#[serial]
fn test_delete_trash() {
crate::tests::init_logging();

// don't test in users HOME
env::set_var("HOME", env::current_dir().unwrap().canonicalize().unwrap());
let home_trash = PathBuf::from("./.local/share/Trash");

// delete file to generate Trash folder
let file_path = get_unique_name();
File::create(&file_path).unwrap();
delete(&file_path).unwrap();

// try to delete trash folder
assert!(delete(&home_trash) == Err(Error::TargetedTrash));

// cleanup
remove_dir_all(".local").unwrap();
}

//////////////////////////////////////////////////////////////////////////////////////
/// System
//////////////////////////////////////////////////////////////////////////////////////
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ where
}

/// Provides information about an error.
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
Unknown {
description: String,
Expand All @@ -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