~iptq/garbage

8379405bb6f95d22719df2fb3c90eb64c3e32d8e — Michael Zhang 9 months ago 69f815e
Don't have a duplicate slash in root dir.
5 files changed, 29 insertions(+), 11 deletions(-)

M src/dir.rs
M src/info.rs
M src/main.rs
M src/ops/put.rs
M src/utils.rs
M src/dir.rs => src/dir.rs +4 -2
@@ 25,7 25,8 @@ impl TrashDir {
    ///
    /// > For every user2 a “home trash” directory MUST be available.
    /// > Its name and location are $XDG_DATA_HOME/Trash
    /// > $XDG_DATA_HOME is the base directory for user-specific data, as defined in the Desktop Base Directory Specification.
    /// > $XDG_DATA_HOME is the base directory for user-specific data, as
    /// defined in the Desktop Base Directory Specification.
    pub fn get_home_trash() -> Self {
        TrashDir::from(XDG.get_data_home().join("Trash"))
    }


@@ 38,7 39,8 @@ impl TrashDir {
            .unwrap_or_else(|| TrashDir::get_home_trash())
    }

    /// Actually create the directory on disk corresponding to this trash directory
    /// Actually create the directory on disk corresponding to this trash
    /// directory
    pub fn create(&self) -> Result<(), Error> {
        let path = &self.0;
        if !path.exists() {

M src/info.rs => src/info.rs +2 -1
@@ 41,7 41,8 @@ pub struct TrashInfo {
}

impl TrashInfo {
    /// Create a new TrashInfo based on the .trashinfo path and the deleted file path
    /// Create a new TrashInfo based on the .trashinfo path and the deleted file
    /// path
    ///
    /// This is useful for reading files from the Trash.
    pub fn from_files(

M src/main.rs => src/main.rs +3 -2
@@ 21,8 21,9 @@ enum Command {
    /// Puts files into the trash.
    ///
    /// If a trash directory isn't specified, the best strategy is picked
    /// for each file that's deleted (after shell glob expansion). The algorithm
    /// for deciding a strategy is specified in the FreeDesktop Trash spec.
    /// for each file that's deleted (after shell glob expansion). The
    /// algorithm for deciding a strategy is specified in the FreeDesktop
    /// Trash spec.
    #[structopt(name = "put")]
    Put(PutOptions),


M src/ops/put.rs => src/ops/put.rs +2 -1
@@ 98,7 98,8 @@ enum DeletionStrategy {
    Fixed(TrashDir),

    /// move the candidate files/directories to the trash directory
    /// (this requires that both the candidate and the trash directories be on the same filesystem)
    /// (this requires that both the candidate and the trash directories be on
    /// the same filesystem)
    MoveTo(TrashDir),

    /// recursively copy the candidate files/directories to the trash directory

M src/utils.rs => src/utils.rs +18 -5
@@ 2,7 2,7 @@ use std::env;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf, MAIN_SEPARATOR};
use std::path::{Component, Path, PathBuf, MAIN_SEPARATOR};

use anyhow::Result;
use percent_encoding::AsciiSet;


@@ 61,10 61,23 @@ pub fn recursive_copy(
/// Percent-encodes a path, but only the file names, not the separators.
pub fn percent_encode(path: impl AsRef<Path>) -> String {
    let path = path.as_ref();
    path.iter()
        .map(|segment| {
            percent_encoding::percent_encode(segment.as_bytes(), MASK)
                .to_string()
    path.components()
        .filter_map(|segment| {
            match segment {
                Component::RootDir => Some(String::new()),
                Component::Prefix(_) => unreachable!("windows"),

                // TODO: separate type for normalized path
                Component::ParentDir => {
                    unreachable!("path should be normalized")
                }
                Component::CurDir => unreachable!("path should be normalized"),

                Component::Normal(s) => Some(
                    percent_encoding::percent_encode(s.as_bytes(), MASK)
                        .to_string(),
                ),
            }
        })
        .collect::<Vec<_>>()
        .join(&MAIN_SEPARATOR.to_string())