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
use anyhow::Result;
use std::path::Path;
use std::process::Command;

pub enum BackupControl {
    None,
    Off,
    Numbered,
    T,
    Existing,
    Nil,
    Simple,
    Never,
}

#[derive(Default)]
pub struct LnOptions<'a> {
    backup: Option<BackupControl>,
    b: bool,
    directory: bool,
    force: bool,
    logical: bool,
    no_dereference: bool,
    physical: bool,
    relative: bool,
    symbolic: bool,
    suffix: Option<&'a str>,
    target_directory: Option<&'a str>,
    no_target_directory: bool,
}

/// Make links between files
pub fn ln<'a>(
    targets: &[impl AsRef<Path>],
    destination: Option<impl AsRef<Path>>,
    opts: Option<&LnOptions<'a>>,
    workdir: Option<&str>,
) -> Result<i32> {
    let mut cmd = Command::new("/usr/bin/env");
    cmd.arg("ln");

    if let Some(o) = opts {
        match o.backup {
            Some(BackupControl::None) => {
                cmd.arg("--backup=none");
            }
            Some(BackupControl::Off) => {
                cmd.arg("--backup=off");
            }
            Some(BackupControl::Numbered) => {
                cmd.arg("--backup=numbered");
            }
            Some(BackupControl::T) => {
                cmd.arg("--backup=t");
            }
            Some(BackupControl::Existing) => {
                cmd.arg("--backup=existing");
            }
            Some(BackupControl::Nil) => {
                cmd.arg("--backup=nil");
            }
            Some(BackupControl::Simple) => {
                cmd.arg("--backup=simple");
            }
            Some(BackupControl::Never) => {
                cmd.arg("--backup=never");
            }
            None => {
                if o.b {
                    cmd.arg("-b");
                }
            }
        }
        if o.directory {
            cmd.arg("-d");
        }
        if o.force {
            cmd.arg("-f");
        }
        if o.logical {
            cmd.arg("-L");
        }
        if o.no_dereference {
            cmd.arg("-n");
        }
        if o.physical {
            cmd.arg("-P");
        }
        if o.relative {
            cmd.arg("-r");
        }
        if o.symbolic {
            cmd.arg("-s");
        }
        if let Some(s) = o.suffix {
            cmd.args(&["-S", s]);
        }
        if let Some(d) = o.target_directory {
            cmd.args(&["-t", d]);
        }
        if o.no_target_directory {
            cmd.arg("-T");
        }
    }

    cmd.args(targets.iter().map(|t| t.as_ref()));

    if let Some(dest) = destination {
        cmd.arg(dest.as_ref());
    }

    if let Some(dir) = workdir {
        cmd.current_dir(&dir);
    }

    match cmd.status() {
        Ok(status) => Ok(status.code().unwrap_or(-1)),
        Err(err) => Err(err.into()),
    }
}

/// Simple make symlink for file
pub fn force_symlink(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<i32> {
    let opts = LnOptions {
        force: true,
        symbolic: true,
        ..LnOptions::default()
    };
    ln(&[src], Some(&dst), Some(&opts), None)
}

/// Call the unlink function to remove the specified file
pub fn unlink(file: impl AsRef<Path>) -> Result<i32> {
    let status = Command::new("/usr/bin/env")
        .arg("unlink")
        .arg(&file.as_ref())
        .status();
    match status {
        Ok(status) => Ok(status.code().unwrap_or(-1)),
        Err(err) => Err(err.into()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_link_and_unlink() {
        assert_eq!(2 + 2, 4);
        force_symlink("1.txt", "some.txt").unwrap();
        force_symlink("2.txt", "some.txt").unwrap();
        unlink("some.txt").unwrap();
    }
}