symlink command

This commit is contained in:
veeso
2021-12-14 15:52:56 +01:00
committed by Christian Visintin
parent 24788fa894
commit ced7573241
26 changed files with 433 additions and 162 deletions

View File

@@ -672,6 +672,21 @@ impl Localhost {
self.iter_search(self.wrkdir.as_path(), &WildMatch::new(search))
}
/// Create a symlink at path pointing at target
#[cfg(target_family = "unix")]
pub fn symlink(&self, path: &Path, target: &Path) -> Result<(), HostError> {
let path = self.to_path(path);
std::os::unix::fs::symlink(target, path.as_path()).map_err(|e| {
error!(
"Failed to create symlink at {} pointing at {}: {}",
path.display(),
target.display(),
e
);
HostError::new(HostErrorType::CouldNotCreateFile, Some(e), path.as_path())
})
}
// -- privates
/// Recursive call for `find` method.
@@ -1179,6 +1194,24 @@ mod tests {
assert_eq!(result[1].name(), "examples.csv");
}
#[test]
fn should_create_symlink() {
let tmpdir: tempfile::TempDir = tempfile::TempDir::new().unwrap();
let dir_path: &Path = tmpdir.path();
// Make file
assert!(make_file_at(dir_path, "pippo.txt").is_ok());
let host: Localhost = Localhost::new(PathBuf::from(dir_path)).ok().unwrap();
let mut p = dir_path.to_path_buf();
p.push("pippo.txt");
// Make symlink
assert!(host.symlink(Path::new("link.txt"), p.as_path()).is_ok());
// Fail symlink
assert!(host.symlink(Path::new("link.txt"), p.as_path()).is_err());
assert!(host
.symlink(Path::new("/tmp/oooo/aaaa"), p.as_path())
.is_err());
}
#[test]
fn test_host_fmt_error() {
let err: HostError = HostError::new(