1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#![recursion_limit = "1024"]

#[macro_use]
extern crate error_chain;

pub mod errors {
    error_chain!{}
}

use errors::*;
use std::path::{Path, MAIN_SEPARATOR};
use std::borrow::Cow::{self, Owned, Borrowed};
use std::os::unix::ffi::OsStrExt;
use std::ops::Deref;

pub struct PathExt<T: AsRef<Path>>(T);

/// PartialEq for PathExt means that both files exists and have the same inode number on the same
/// device.
impl<T: AsRef<Path>, S: AsRef<Path>> PartialEq<S> for PathExt<T> {
    fn eq(&self, other: &S) -> bool {
        use std::os::unix::fs::MetadataExt;

        self.symlink_metadata()
            .and_then(|lhs| {
                other.as_ref()
                    .symlink_metadata()
                    .and_then(|rhs| Ok(lhs.ino() == rhs.ino() && lhs.dev() == rhs.dev()))
            })
            .unwrap_or(false)
    }
}

impl<T: AsRef<Path>> PathExt<T> {
    /// Remove symlink file or empty directory
    pub fn remove(&self) -> Result<()> {
        if let Ok(meta) = self.symlink_metadata() {
            let filetype = meta.file_type();

            if filetype.is_file() || filetype.is_symlink() {
                std::fs::remove_file(self)
                    .chain_err(|| format!("Failed to remove '{}'", self.display()))?;
            } else if filetype.is_dir() {
                std::fs::remove_dir(self)
                    .chain_err(|| format!("Failed to remove '{}'", self.display()))?;
            } else {
                bail!("unknown filetype for '{}'", self.display())
            }
        }

        Ok(())
    }
}

impl<T: AsRef<Path>> From<T> for PathExt<T> {
    fn from(other: T) -> Self {
        PathExt(other)
    }
}

impl<T: AsRef<Path>> Deref for PathExt<T> {
    type Target = Path;

    fn deref(&self) -> &Self::Target {
        self.as_ref()
    }
}

impl<T: AsRef<Path>> AsRef<Path> for PathExt<T> {
    fn as_ref(&self) -> &Path {
        self.0.as_ref()
    }
}


/// Expand dst based on src.
///
/// dst will be expanded according to the following rules.
///
/// # Rules
///
/// * The file_name of src will be returned if dst is empty.
/// * The file_name of src will be appended to dst and then returned if dst ends with /.
/// * Otherwise dst will be returned.
pub fn expand_destination<'a, S, D>(src: &'a S, dst: &'a D) -> Result<Cow<'a, Path>>
    where S: AsRef<Path> + ?Sized,
          D: AsRef<Path> + ?Sized
{
    let src = src.as_ref();
    let dst = dst.as_ref();
    if dst.as_os_str().is_empty() {
        src.file_name()
            .and_then(|basename| Some(Borrowed(Path::new(basename))))
            .ok_or(format!("Failed to find basename of {}", src.display()).into())
    } else if dst.as_os_str().as_bytes().ends_with(&[MAIN_SEPARATOR as u8]) {
        src.file_name()
            .and_then(|basename| Some(Owned(Path::new(dst).join(basename))))
            .ok_or(format!("Failed to find basename of {}", src.display()).into())
    } else {
        Ok(Borrowed(dst))
    }
}

#[cfg(test)]
mod tests {
    use super::expand_destination;
    use std::path::{Path, MAIN_SEPARATOR};

    trait SlashToSep {
        fn slash_to_sep(&self) -> String;
    }

    impl SlashToSep for str {
        fn slash_to_sep(&self) -> String {
            self.replace("/", &MAIN_SEPARATOR.to_string())
        }
    }

    #[test]
    #[should_panic]
    fn expand_destination_empty_src_dst() {
        let src = "";
        let dst = "";
        expand_destination(src, dst).unwrap();
    }

    #[test]
    #[should_panic]
    fn expand_destination_empty_src_dst_ends_with_sep() {
        let src = "";
        let dst = "dest/".slash_to_sep();
        expand_destination(src, &dst).unwrap();
    }

    #[test]
    fn expand_destination_empty_dst() {
        let src = "/test/file".slash_to_sep();
        let dst = "";
        let res = "file";
        assert_eq!(Path::new(res), expand_destination(&src, dst).unwrap());
    }

    #[test]
    fn expand_destination_with_dst() {
        let src = "/test/file".slash_to_sep();
        let dst = "destination";
        let res = "destination";
        assert_eq!(Path::new(res), expand_destination(&src, dst).unwrap());
    }

    #[test]
    fn expand_destination_with_dst_ending_with_sep() {
        let src = "/test/file".slash_to_sep();
        let dst = "destination/".slash_to_sep();
        let res = "destination/file".slash_to_sep();
        assert_eq!(Path::new(&res), expand_destination(&src, &dst).unwrap());
    }
}