Skip to content

Commit

Permalink
thanks clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jan 30, 2023
1 parent 1a347fc commit 37dedb3
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 178 deletions.
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ edition = "2018"
use_try_shorthand = true
use_field_init_shorthand = true
use_small_heuristics = "Max"
max_width = 120
111 changes: 33 additions & 78 deletions src/freedesktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ pub fn list() -> Result<Vec<TrashItem>, Error> {
Ok(home_trash) => {
if !home_trash.is_dir() {
home_error = Some(Error::Unknown {
description: "The 'home trash' either does not exist or is not a directory (or a link pointing to a dir)".into()
description:
"The 'home trash' either does not exist or is not a directory (or a link pointing to a dir)"
.into(),
});
} else {
trash_folders.insert(home_trash);
Expand All @@ -83,10 +85,7 @@ pub fn list() -> Result<Vec<TrashItem>, Error> {
})?;
}
if trash_folders.is_empty() {
warn!(
"No trash folder was found. The error when looking for the 'home trash' was: {:?}",
home_error
);
warn!("No trash folder was found. The error when looking for the 'home trash' was: {:?}", home_error);
return Ok(vec![]);
}
// List all items from the set of trash folders
Expand All @@ -96,10 +95,7 @@ pub fn list() -> Result<Vec<TrashItem>, Error> {
let top_dir = get_topdir_for_path(folder, &mount_points);
let info_folder = folder.join("info");
if !info_folder.is_dir() {
warn!(
"The path {:?} did not point to a directory, skipping this trash folder.",
info_folder
);
warn!("The path {:?} did not point to a directory, skipping this trash folder.", info_folder);
continue;
}
let read_dir = match std::fs::read_dir(&info_folder) {
Expand All @@ -108,10 +104,7 @@ pub fn list() -> Result<Vec<TrashItem>, Error> {
// After all the earlier checks, it's still possible that the directory does not exist at this point (or is not readable)
// because another process may have deleted it or modified its access rights in the meantime.
// So let's just pring a warning and continue to the rest of the folders
warn!(
"The trash info folder {:?} could not be read. Error was {:?}",
info_folder, e
);
warn!("The trash info folder {:?} could not be read. Error was {:?}", info_folder, e);
continue;
}
};
Expand Down Expand Up @@ -193,7 +186,10 @@ pub fn list() -> Result<Vec<TrashItem>, Error> {
match time {
Some(time) => time_deleted = Some(time.timestamp()),
None => {
log::error!("Failed to convert the local time to a UTC time. Local time was {:?}", naive_local);
log::error!(
"Failed to convert the local time to a UTC time. Local time was {:?}",
naive_local
);
continue 'trash_item;
}
}
Expand All @@ -205,12 +201,7 @@ pub fn list() -> Result<Vec<TrashItem>, Error> {
if time_deleted.is_none() {
warn!("Could not determine the deletion time of the trash item. (The `DeletionDate` field is probably missing from the info file.) The info file path is: '{:?}'", info_path);
}
result.push(TrashItem {
id,
name,
original_parent,
time_deleted: time_deleted.unwrap_or(-1),
});
result.push(TrashItem { id, name, original_parent, time_deleted: time_deleted.unwrap_or(-1) });
} else {
warn!("Could not determine the original parent folder of the trash item. (The `Path` field is probably missing from the info file.) The info file path is: '{:?}'", info_path);
}
Expand Down Expand Up @@ -277,8 +268,7 @@ where
// if it already exists.
let original_path = item.original_path();
// Make sure the parent exists so that `create_dir` doesn't faile due to that.
create_dir_all(&item.original_parent)
.map_err(|e| fsys_err_to_unknown(&item.original_parent, e))?;
create_dir_all(&item.original_parent).map_err(|e| fsys_err_to_unknown(&item.original_parent, e))?;
let mut collision = false;
if file.is_dir() {
// NOTE create_dir_all succeeds when the path already exist but create_dir
Expand All @@ -302,10 +292,7 @@ where
}
if collision {
let remaining: Vec<_> = std::iter::once(item).chain(iter).collect();
return Err(Error::RestoreCollision {
path: original_path,
remaining_items: remaining,
});
return Err(Error::RestoreCollision { path: original_path, remaining_items: remaining });
}
std::fs::rename(&file, &original_path).map_err(|e| fsys_err_to_unknown(&file, e))?;
std::fs::remove_file(info_file).map_err(|e| fsys_err_to_unknown(info_file, e))?;
Expand Down Expand Up @@ -342,14 +329,11 @@ fn execute_on_mounted_trash_folders<F: FnMut(PathBuf) -> Result<(), Error>>(
}
}
} else {
warn!(
"A Trash folder was found at '{:?}', but it's invalid because it's {:?}",
trash_path, validity
);
warn!("A Trash folder was found at '{:?}', but it's invalid because it's {:?}", trash_path, validity);
}
}
// See if there's a ".Trash-$UID" directory at the mounted location
let trash_path = topdir.join(format!(".Trash-{}", uid));
let trash_path = topdir.join(format!(".Trash-{uid}"));
let should_execute;
if !trash_path.exists() || !trash_path.is_dir() {
if create_folder {
Expand Down Expand Up @@ -405,7 +389,7 @@ fn move_to_trash(
} else {
filename.to_str().unwrap().into()
};
let info_name = format!("{}.trashinfo", in_trash_name);
let info_name = format!("{in_trash_name}.trashinfo");
let info_file_path = info_folder.join(&info_name);
let info_result = OpenOptions::new().create_new(true).write(true).open(&info_file_path);
match info_result {
Expand All @@ -423,7 +407,7 @@ fn move_to_trash(
writeln!(file, "[Trash Info]")
.and_then(|_| {
let absolute_uri = encode_uri_path(src);
writeln!(file, "Path={}", absolute_uri).and_then(|_| {
writeln!(file, "Path={absolute_uri}").and_then(|_| {
#[cfg(feature = "chrono")]
{
let now = chrono::Local::now();
Expand Down Expand Up @@ -495,10 +479,7 @@ where
}

/// An error may mean that a collision was found.
fn move_items_no_replace(
src: impl AsRef<Path>,
dst: impl AsRef<Path>,
) -> Result<(), std::io::Error> {
fn move_items_no_replace(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<(), std::io::Error> {
let src = src.as_ref();
let dst = dst.as_ref();

Expand All @@ -508,10 +489,7 @@ fn move_items_no_replace(
execute_src_to_dst_operation(src, dst, &|_| Ok(()), &|src, dst| {
if let Some(parent) = dst.parent() {
if let Err(err) = std::fs::create_dir_all(parent) {
warn!(
"Failed to create destination directory. It probably already exists. {:?}",
err
);
warn!("Failed to create destination directory. It probably already exists. {:?}", err);
}
}
std::fs::rename(src, dst)
Expand All @@ -524,10 +502,7 @@ fn move_items_no_replace(
Ok(())
}

fn try_creating_placeholders(
src: impl AsRef<Path>,
dst: impl AsRef<Path>,
) -> Result<(), std::io::Error> {
fn try_creating_placeholders(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<(), std::io::Error> {
let src = src.as_ref();
let dst = dst.as_ref();
let metadata = src.symlink_metadata()?;
Expand Down Expand Up @@ -591,9 +566,7 @@ fn home_trash() -> Result<PathBuf, Error> {
return Ok(home_path.join(".local/share/Trash"));
}
}
Err(Error::Unknown {
description: "Neither the XDG_DATA_HOME nor the HOME environment variable was found".into(),
})
Err(Error::Unknown { description: "Neither the XDG_DATA_HOME nor the HOME environment variable was found".into() })
}

fn home_topdir(mnt_points: &[MountPoint]) -> Result<PathBuf, Error> {
Expand All @@ -609,9 +582,7 @@ fn home_topdir(mnt_points: &[MountPoint]) -> Result<PathBuf, Error> {
return Ok(get_topdir_for_path(home_path, mnt_points).to_owned());
}
}
Err(Error::Unknown {
description: "Neither the XDG_DATA_HOME nor the HOME environment variable was found".into(),
})
Err(Error::Unknown { description: "Neither the XDG_DATA_HOME nor the HOME environment variable was found".into() })
}

fn get_topdir_for_path<'a>(path: &Path, mnt_points: &'a [MountPoint]) -> &'a Path {
Expand Down Expand Up @@ -659,16 +630,13 @@ fn get_mount_points() -> Result<Vec<MountPoint>, Error> {
//let file;
let read_arg = CString::new("r").unwrap();
let mounts_path = CString::new("/proc/mounts").unwrap();
let mut file =
unsafe { libc::fopen(mounts_path.as_c_str().as_ptr(), read_arg.as_c_str().as_ptr()) };
let mut file = unsafe { libc::fopen(mounts_path.as_c_str().as_ptr(), read_arg.as_c_str().as_ptr()) };
if file.is_null() {
let mtab_path = CString::new("/etc/mtab").unwrap();
file = unsafe { libc::fopen(mtab_path.as_c_str().as_ptr(), read_arg.as_c_str().as_ptr()) };
}
if file.is_null() {
return Err(Error::Unknown {
description: "Neither '/proc/mounts' nor '/etc/mtab' could be opened.".into(),
});
return Err(Error::Unknown { description: "Neither '/proc/mounts' nor '/etc/mtab' could be opened.".into() });
}
defer! { unsafe { libc::fclose(file); } }
let mut result = Vec::new();
Expand All @@ -692,9 +660,7 @@ fn get_mount_points() -> Result<Vec<MountPoint>, Error> {
}
if result.is_empty() {
return Err(Error::Unknown {
description:
"A mount points file could be opened, but the call to `getmntent` returned NULL."
.into(),
description: "A mount points file could be opened, but the call to `getmntent` returned NULL.".into(),
});
}
Ok(result)
Expand Down Expand Up @@ -729,8 +695,7 @@ fn get_mount_points() -> Result<Vec<MountPoint>, Error> {
if count < 1 {
return Ok(Vec::new());
}
let fs_infos: &[libc::statfs] =
unsafe { std::slice::from_raw_parts(fs_infos as _, count as _) };
let fs_infos: &[libc::statfs] = unsafe { std::slice::from_raw_parts(fs_infos as _, count as _) };

let mut result = Vec::new();
for fs_info in fs_infos {
Expand All @@ -748,11 +713,8 @@ fn get_mount_points() -> Result<Vec<MountPoint>, Error> {
};
let mount_from = c_buf_to_str(&fs_info.f_mntfromname).unwrap_or_default();

let mount_point = MountPoint {
mnt_dir: mount_to.into(),
_mnt_fsname: mount_from.into(),
_mnt_type: fs_type.into(),
};
let mount_point =
MountPoint { mnt_dir: mount_to.into(), _mnt_fsname: mount_from.into(), _mnt_type: fs_type.into() };
result.push(mount_point);
}
Ok(result)
Expand Down Expand Up @@ -788,8 +750,7 @@ mod tests {
let file_name_prefix = get_unique_name();
let batches: usize = 2;
let files_per_batch: usize = 3;
let names: Vec<_> =
(0..files_per_batch).map(|i| format!("{}#{}", file_name_prefix, i)).collect();
let names: Vec<_> = (0..files_per_batch).map(|i| format!("{}#{}", file_name_prefix, i)).collect();
for _ in 0..batches {
for path in names.iter() {
File::create(path).unwrap();
Expand All @@ -798,19 +759,15 @@ mod tests {
let result = delete_all_using_system_program(&names);
if let Err(SystemTrashError::NoTrashProgram) = &result {
// For example may be the case on build systems that don't have a destop environment
warn!(
"No system default trashing utility was found, using this crate's implementation"
);
warn!("No system default trashing utility was found, using this crate's implementation");
delete_all(&names).unwrap();
} else {
result.unwrap();
}
}
let items = list().unwrap();
let items: HashMap<_, Vec<_>> = items
.into_iter()
.filter(|x| x.name.starts_with(&file_name_prefix))
.fold(HashMap::new(), |mut map, x| {
let items: HashMap<_, Vec<_>> =
items.into_iter().filter(|x| x.name.starts_with(&file_name_prefix)).fold(HashMap::new(), |mut map, x| {
match map.entry(x.name.clone()) {
Entry::Occupied(mut entry) => {
entry.get_mut().push(x);
Expand Down Expand Up @@ -863,9 +820,7 @@ mod tests {

/// This is based on the electron library's implementation.
/// See: https://github.com/electron/electron/blob/34c4c8d5088fa183f56baea28809de6f2a427e02/shell/common/platform_util_linux.cc#L96
pub fn delete_all_canonicalized_using_system_program(
full_paths: Vec<PathBuf>,
) -> Result<(), SystemTrashError> {
pub fn delete_all_canonicalized_using_system_program(full_paths: Vec<PathBuf>) -> Result<(), SystemTrashError> {
static DEFAULT_TRASH: &str = "gio";
let trash = {
// Determine desktop environment and set accordingly.
Expand Down
21 changes: 7 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,7 @@ pub enum Error {
/// **freedesktop only**
///
/// Error coming from file system
#[cfg(all(
unix,
not(target_os = "macos"),
not(target_os = "ios"),
not(target_os = "android")
))]
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android")))]
FileSystem {
path: PathBuf,
kind: std::io::ErrorKind,
Expand Down Expand Up @@ -211,12 +206,12 @@ pub enum Error {
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Error during a `trash` operation: {:?}", self)
write!(f, "Error during a `trash` operation: {self:?}")
}
}
impl error::Error for Error {}
pub fn into_unknown<E: std::fmt::Display>(err: E) -> Error {
Error::Unknown { description: format!("{}", err) }
Error::Unknown { description: format!("{err}") }
}

pub(crate) fn canonicalize_paths<I, T>(paths: I) -> Result<Vec<PathBuf>, Error>
Expand All @@ -229,17 +224,15 @@ where
.map(|x| {
let target_ref = x.as_ref();
let target = if target_ref.is_relative() {
let curr_dir = current_dir().map_err(|_| Error::CouldNotAccess {
target: "[Current working directory]".into(),
})?;
let curr_dir = current_dir()
.map_err(|_| Error::CouldNotAccess { target: "[Current working directory]".into() })?;
curr_dir.join(target_ref)
} else {
target_ref.to_owned()
};
let parent = target.parent().ok_or(Error::TargetedRoot)?;
let canonical_parent = parent
.canonicalize()
.map_err(|_| Error::CanonicalizePath { original: parent.to_owned() })?;
let canonical_parent =
parent.canonicalize().map_err(|_| Error::CanonicalizePath { original: parent.to_owned() })?;
if let Some(file_name) = target.file_name() {
Ok(canonical_parent.join(file_name))
} else {
Expand Down
18 changes: 6 additions & 12 deletions src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn delete_using_file_mgr(full_paths: Vec<String>) -> Result<(), Error> {
let url: id = unsafe { msg_send![url_cls, fileURLWithPath:string.ptr] };
if url == nil {
return Err(Error::Unknown {
description: format!("Failed to convert a path to an NSURL. Path: '{}'", path),
description: format!("Failed to convert a path to an NSURL. Path: '{path}'"),
});
}
trace!("Finished fileURLWithPath");
Expand All @@ -121,18 +121,16 @@ fn delete_using_file_mgr(full_paths: Vec<String>) -> Result<(), Error> {
if error == nil {
return Err(Error::Unknown {
description: format!(
"While deleting '{}', `trashItemAtURL` returned with failure but no error was specified.",
path
)
"While deleting '{path}', `trashItemAtURL` returned with failure but no error was specified.",
),
});
}
let code: isize = unsafe { msg_send![error, code] };
let domain: id = unsafe { msg_send![error, domain] };
let domain = unsafe { ns_string_to_rust(domain)? };
return Err(Error::Unknown {
description: format!(
"While deleting '{}', `trashItemAtURL` failed, code: {}, domain: {}",
path, code, domain
"While deleting '{path}', `trashItemAtURL` failed, code: {code}, domain: {domain}",
),
});
}
Expand All @@ -145,12 +143,8 @@ fn delete_using_finder(full_paths: Vec<String>) -> Result<(), Error> {
// osascript -e 'tell application "Finder" to delete { POSIX file "file1", POSIX "file2" }'
// The `-e` flag is used to execute only one line of AppleScript.
let mut command = Command::new("osascript");
let posix_files = full_paths
.into_iter()
.map(|p| format!("POSIX file \"{}\"", p))
.collect::<Vec<String>>()
.join(", ");
let script = format!("tell application \"Finder\" to delete {{ {} }}", posix_files);
let posix_files = full_paths.into_iter().map(|p| format!("POSIX file \"{p}\"")).collect::<Vec<String>>().join(", ");
let script = format!("tell application \"Finder\" to delete {{ {posix_files} }}");

let argv: Vec<OsString> = vec!["-e".into(), script.into()];
command.args(argv);
Expand Down
Loading

0 comments on commit 37dedb3

Please sign in to comment.