Don't have a duplicate slash in root dir.

This commit is contained in:
Michael Zhang 2020-10-31 20:13:15 -05:00
parent 69f815e653
commit 8379405bb6
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
5 changed files with 29 additions and 11 deletions

View file

@ -25,7 +25,8 @@ impl TrashDir {
/// ///
/// > For every user2 a “home trash” directory MUST be available. /// > For every user2 a “home trash” directory MUST be available.
/// > Its name and location are $XDG_DATA_HOME/Trash /// > 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 { pub fn get_home_trash() -> Self {
TrashDir::from(XDG.get_data_home().join("Trash")) TrashDir::from(XDG.get_data_home().join("Trash"))
} }
@ -38,7 +39,8 @@ impl TrashDir {
.unwrap_or_else(|| TrashDir::get_home_trash()) .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> { pub fn create(&self) -> Result<(), Error> {
let path = &self.0; let path = &self.0;
if !path.exists() { if !path.exists() {

View file

@ -41,7 +41,8 @@ pub struct TrashInfo {
} }
impl 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. /// This is useful for reading files from the Trash.
pub fn from_files( pub fn from_files(

View file

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

View file

@ -98,7 +98,8 @@ enum DeletionStrategy {
Fixed(TrashDir), Fixed(TrashDir),
/// move the candidate files/directories to the trash directory /// 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), MoveTo(TrashDir),
/// recursively copy the candidate files/directories to the trash directory /// recursively copy the candidate files/directories to the trash directory

View file

@ -2,7 +2,7 @@ use std::env;
use std::ffi::{OsStr, OsString}; use std::ffi::{OsStr, OsString};
use std::fs; use std::fs;
use std::os::unix::ffi::OsStrExt; 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 anyhow::Result;
use percent_encoding::AsciiSet; use percent_encoding::AsciiSet;
@ -61,10 +61,23 @@ pub fn recursive_copy(
/// Percent-encodes a path, but only the file names, not the separators. /// Percent-encodes a path, but only the file names, not the separators.
pub fn percent_encode(path: impl AsRef<Path>) -> String { pub fn percent_encode(path: impl AsRef<Path>) -> String {
let path = path.as_ref(); let path = path.as_ref();
path.iter() path.components()
.map(|segment| { .filter_map(|segment| {
percent_encoding::percent_encode(segment.as_bytes(), MASK) match segment {
.to_string() 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<_>>() .collect::<Vec<_>>()
.join(&MAIN_SEPARATOR.to_string()) .join(&MAIN_SEPARATOR.to_string())