Skip to content

Commit

Permalink
Update the macOS backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturKovacs committed Apr 18, 2021
1 parent e739014 commit eff82e4
Show file tree
Hide file tree
Showing 7 changed files with 358 additions and 108 deletions.
15 changes: 6 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ coinit_speed_over_memory = []
[dev-dependencies]
chrono = "0.4.9"
rand = "0.7.2"
lazy_static = "1.4.0"
once_cell = "1.7.2"
env_logger = "0.8"

[dependencies]
log = "0.4"

[target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2.7"

[target.'cfg(target_os = "linux")'.dependencies]
chrono = "0.4.9"
Expand All @@ -37,11 +42,3 @@ windows = "0.8.0"

[target.'cfg(windows)'.build-dependencies]
windows = "0.8.0"

# [target.'cfg(windows)'.dependencies.winapi]
# git = "https://github.com/ArturKovacs/winapi-rs.git"
# rev = "fe297b9830c804b274e2206d3aebfe9dc33e39cb"
# features = [
# "combaseapi", "objbase", "objidl", "shlobj", "shellapi", "winerror", "shlobj", "shlwapi", "shobjidl_core",
# "shobjidl", "oleauto", "oaidl", "wtypes", "errhandlingapi", "timezoneapi", "winuser"
# ]
23 changes: 23 additions & 0 deletions examples/delete_method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::fs::File;
use trash::TrashContext;

#[cfg(target_os = "macos")]
use trash::macos::{DeleteMethod, TrashContextExtMacos};

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

#[cfg(target_os = "macos")]
fn main() {
env_logger::init();

let mut trash_ctx = TrashContext::default();
trash_ctx.set_delete_method(DeleteMethod::NSFileManager);

let path = "this_file_was_deleted_using_the_ns_file_manager";
File::create(path).unwrap();
trash_ctx.delete(path).unwrap();
assert!(File::open(path).is_err());
}
140 changes: 95 additions & 45 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,87 @@ mod platform;
mod platform;

#[cfg(target_os = "macos")]
#[path = "macos.rs"]
mod platform;
pub mod macos;
use log::trace;
#[cfg(target_os = "macos")]
use macos as platform;

// pub use platform as my_latform;
pub const DEFAULT_TRASH_CTX: TrashContext = TrashContext::new();

/// A collection of preferences for trash operations.
#[derive(Clone, Default, Debug)]
pub struct TrashContext {
platform_specific: platform::PlatformTrashContext,
}
impl TrashContext {
pub const fn new() -> Self {
Self { platform_specific: platform::PlatformTrashContext::new() }
}

/// Removes a single file or directory.
///
/// When a symbolic link is provided to this function, the sybolic link will be removed and the link
/// target will be kept intact.
///
/// # Example
///
/// ```
/// use std::fs::File;
/// use trash::delete;
/// File::create("delete_me").unwrap();
/// trash::delete("delete_me").unwrap();
/// assert!(File::open("delete_me").is_err());
/// ```
pub fn delete<T: AsRef<Path>>(&self, path: T) -> Result<(), Error> {
self.delete_all(&[path])
}

/// Removes all files/directories specified by the collection of paths provided as an argument.
///
/// When a symbolic link is provided to this function, the sybolic link will be removed and the link
/// target will be kept intact.
///
/// # Example
///
/// ```
/// use std::fs::File;
/// use trash::delete_all;
/// File::create("delete_me_1").unwrap();
/// File::create("delete_me_2").unwrap();
/// delete_all(&["delete_me_1", "delete_me_2"]).unwrap();
/// assert!(File::open("delete_me_1").is_err());
/// assert!(File::open("delete_me_2").is_err());
/// ```
pub fn delete_all<I, T>(&self, paths: I) -> Result<(), Error>
where
I: IntoIterator<Item = T>,
T: AsRef<Path>,
{
trace!("Starting canonicalize_paths");
let full_paths = canonicalize_paths(paths)?;
trace!("Finished canonicalize_paths");
self.delete_all_canonicalized(full_paths)
}
}

/// Convenience method for `DEFAULT_TRASH_CTX.delete()`.
///
/// See: [`TrashContext::delete`](TrashContext::delete)
pub fn delete<T: AsRef<Path>>(path: T) -> Result<(), Error> {
DEFAULT_TRASH_CTX.delete(path)
}

/// Convenience method for `DEFAULT_TRASH_CTX.delete_all()`.
///
/// See: [`TrashContext::delete_all`](TrashContext::delete_all)
pub fn delete_all<I, T>(paths: I) -> Result<(), Error>
where
I: IntoIterator<Item = T>,
T: AsRef<Path>,
{
DEFAULT_TRASH_CTX.delete_all(paths)
}

///
/// A type that is contained within [`Error`]. It provides information about why the error was
Expand Down Expand Up @@ -164,49 +243,6 @@ where
.collect::<Result<Vec<_>, _>>()
}

/// Removes a single file or directory.
///
/// When a symbolic link is provided to this function, the sybolic link will be removed and the link
/// target will be kept intact.
///
/// # Example
///
/// ```
/// use std::fs::File;
/// use trash::delete;
/// File::create("delete_me").unwrap();
/// trash::delete("delete_me").unwrap();
/// assert!(File::open("delete_me").is_err());
/// ```
pub fn delete<T: AsRef<Path>>(path: T) -> Result<(), Error> {
delete_all(&[path])
}

/// Removes all files/directories specified by the collection of paths provided as an argument.
///
/// When a symbolic link is provided to this function, the sybolic link will be removed and the link
/// target will be kept intact.
///
/// # Example
///
/// ```
/// use std::fs::File;
/// use trash::delete_all;
/// File::create("delete_me_1").unwrap();
/// File::create("delete_me_2").unwrap();
/// delete_all(&["delete_me_1", "delete_me_2"]).unwrap();
/// assert!(File::open("delete_me_1").is_err());
/// assert!(File::open("delete_me_2").is_err());
/// ```
pub fn delete_all<I, T>(paths: I) -> Result<(), Error>
where
I: IntoIterator<Item = T>,
T: AsRef<Path>,
{
let full_paths = canonicalize_paths(paths)?;
platform::delete_all_canonicalized(full_paths)
}

/// This struct holds information about a single item within the trash.
///
/// Some functions associated with this struct are defined in the `TrahsItemPlatformDep` trait.
Expand Down Expand Up @@ -263,13 +299,27 @@ impl Hash for TrashItem {

#[cfg(any(target_os = "windows", all(unix, not(target_os = "macos"))))]
pub mod extra {
// TODO: Rename this to os_limited
// TODO: rename the linux module to `freedesktop`

use std::{
collections::HashSet,
hash::{Hash, Hasher},
};

use super::{platform, Error, TrashItem};

pub trait TrashContextExtOsLimited {
fn list() -> Result<Vec<TrashItem>, Error>;
fn purge_all<I>(items: I) -> Result<(), Error>
where
I: IntoIterator<Item = TrashItem>;

fn restore_all<I>(items: I) -> Result<(), Error>
where
I: IntoIterator<Item = TrashItem>;
}

/// Returns all [`TrashItem`]s that are currently in the trash.
///
/// The items are in no particular order and must be sorted when any kind of ordering is required.
Expand Down
3 changes: 3 additions & 0 deletions src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ use scopeguard::defer;

use crate::{Error, TrashItem};

#[derive(Clone, Default, Debug)]
pub struct PlatformTrashContext;

pub fn delete_all_canonicalized(full_paths: Vec<PathBuf>) -> Result<(), Error> {
let root = Path::new("/");
let home_trash = home_trash()?;
Expand Down
Loading

0 comments on commit eff82e4

Please sign in to comment.