Skip to content

Commit

Permalink
fix(Binaries): Set permissions in cross OS way
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Jul 2, 2021
1 parent 4392161 commit 5b69333
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
3 changes: 1 addition & 2 deletions rust/src/binaries.rs
Expand Up @@ -517,8 +517,7 @@ impl Binary {
for file in files {
let path = dir.join(file);
if path.exists() {
use std::os::unix::fs::PermissionsExt;
fs::set_permissions(path, fs::Permissions::from_mode(0o755))?;
crate::utils::fs::set_perms(path, 0o755)?;
}
}
Ok(())
Expand Down
24 changes: 18 additions & 6 deletions rust/src/utils/fs.rs
@@ -1,17 +1,29 @@
use std::path::Path;

///! File system utilities, particularly functionality that requires
///! alternative implementations for alternative operating systems.
use eyre::Result;
use std::{fs, os, path::Path};

/// Set permissions on a file
pub fn set_perms<File: AsRef<Path>>(path: File, mode: u32) -> Result<()> {
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
use os::unix::fs::PermissionsExt;
fs::set_permissions(path, fs::Permissions::from_mode(mode))?;
}

Ok(())
}

/// Create a symbolic (soft) link to a file
pub fn symlink_file<Original: AsRef<Path>, Link: AsRef<Path>>(
original: Original,
link: Link,
) -> Result<()> {
#[cfg(any(target_os = "linux", target_os = "macos"))]
std::os::unix::fs::symlink(original, link)?;
os::unix::fs::symlink(original, link)?;

#[cfg(target_os = "windows")]
std::os::windows::fs::symlink_file(original, link)?;
os::windows::fs::symlink_file(original, link)?;

Ok(())
}
Expand All @@ -22,10 +34,10 @@ pub fn symlink_dir<Original: AsRef<Path>, Link: AsRef<Path>>(
link: Link,
) -> Result<()> {
#[cfg(any(target_os = "linux", target_os = "macos"))]
std::os::unix::fs::symlink(original, link)?;
os::unix::fs::symlink(original, link)?;

#[cfg(target_os = "windows")]
std::os::windows::fs::symlink_dir(original, link)?;
os::windows::fs::symlink_dir(original, link)?;

Ok(())
}

0 comments on commit 5b69333

Please sign in to comment.