Skip to content

Commit

Permalink
add support to symlink on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Kedap committed Aug 31, 2021
1 parent af41a05 commit ca9ffa0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
63 changes: 61 additions & 2 deletions syncre_lib/src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use {
std::{
fs, io,
io::{Error, ErrorKind},
os::unix,
path::Path,
},
};
Expand Down Expand Up @@ -49,9 +48,17 @@ pub fn copy_sync(source: &Path, target: &Path) -> Result<(), io::Error> {
// Checking if the source is symbolic link
if let Ok(v) = fs::read_link(source) {
//Creation of a new symbolic link if the file contains one, the new link will take the file that listed the source
match unix::fs::symlink(v, target) {

#[cfg(target_family = "windows")]
match create_link_windows(v.as_path(), target) {
Ok(_) => return Ok(()),
Err(e) => return Err(e),
}

#[cfg(target_family = "unix")]
match create_link_unix(v.as_path(), target) {
Ok(_) => return Ok(()),
Err(e) => return Err(e),
}
} else if target.is_file() {
if let Err(e) = fs::copy(source, target) {
Expand All @@ -62,3 +69,55 @@ pub fn copy_sync(source: &Path, target: &Path) -> Result<(), io::Error> {
}
Ok(())
}

/// Make syslink on windows, either directory or file (only on windows)
///
/// # Example
///
/// ```
/// use std::path::Path;
/// use syncre_lib::archive;
/// let orginal = Path::new("testfiles/linked/hello-windows.txt");
/// let link = Path::new("hello-windows-syslink.txt");
/// match archive::create_link_windows(original, link) {
/// Err(e) => panic!("{}", e),
/// Ok(v) => v
/// }
/// ```
#[cfg(target_family = "windows")]
pub fn create_link_windows(orginal: &Path, link: &Path) -> Result<(), io::Error> {
use std::os::windows::fs;
if orginal.is_file() {
match fs::symlink_file(orginal, link) {
Ok(_v) => Ok(()),
Err(e) => Err(e),
}
} else {
match fs::symlink_dir(orginal, link) {
Ok(_v) => Ok(()),
Err(e) => Err(e),
}
}
}

/// Make syslink on unix, either directory or file (only in unix)
///
/// # Example
///
/// ```
/// use std::path::Path;
/// use syncre_lib::archive;
/// let orginal = Path::new("testfiles/linked/hello-link.txt");
/// let link = Path::new("hello-unix-syslink.txt");
/// match archive::create_link_unix(original, link) {
/// Err(e) => panic!("{}", e),
/// Ok(v) => v
/// }
/// ```
#[cfg(target_family = "unix")]
pub fn create_link_unix(orginal: &Path, link: &Path) -> Result<(), io::Error> {
match fs::syslink(orginal, link) {
Ok(_v) => Ok(()),
Err(e) => Err(e),
}
}
1 change: 1 addition & 0 deletions syncre_lib/testfiles/hello-link-windows.txt
1 change: 1 addition & 0 deletions syncre_lib/testfiles/linked/hello-windows.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world for windows link

0 comments on commit ca9ffa0

Please sign in to comment.