From 66d5c8588ac2e35681c8261d077a47818724ef22 Mon Sep 17 00:00:00 2001 From: kristof Date: Sun, 15 Jun 2025 21:37:16 +0200 Subject: [PATCH] ... --- examples/containers/nerdctl_install.rhai | 4 +-- src/os/fs.rs | 41 ++++++++++++++++++++++++ src/rhai/os.rs | 8 +++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/examples/containers/nerdctl_install.rhai b/examples/containers/nerdctl_install.rhai index c4ab1c1..24c8458 100644 --- a/examples/containers/nerdctl_install.rhai +++ b/examples/containers/nerdctl_install.rhai @@ -5,13 +5,13 @@ fn nerdctl_download(){ let name="nerdctl"; let url="https://github.com/containerd/nerdctl/releases/download/v2.1.2/nerdctl-2.1.2-linux-amd64.tar.gz"; download(url,`/tmp/${name}`,10000); - copy(`/tmp/${name}/*`,"/root/hero/bin/"); + copy_bin(`/tmp/${name}/*`); delete(`/tmp/${name}`); let name="containerd"; let url="https://github.com/containerd/containerd/releases/download/v2.1.2/containerd-2.1.2-linux-amd64.tar.gz"; download(url,`/tmp/${name}`,20000); - copy(`/tmp/${name}/bin/*`,"/root/hero/bin/"); + copy_bin(`/tmp/${name}/bin/*`); delete(`/tmp/${name}`); run("apt-get -y install buildah runc"); diff --git a/src/os/fs.rs b/src/os/fs.rs index 3b3a50a..26f374c 100644 --- a/src/os/fs.rs +++ b/src/os/fs.rs @@ -4,6 +4,8 @@ use std::fs; use std::io; use std::path::Path; use std::process::Command; +use libc; +use dirs; // Define a custom error type for file system operations #[derive(Debug)] @@ -267,6 +269,45 @@ pub fn copy(src: &str, dest: &str) -> Result { } } } +/** + * Copy a binary to the correct location based on OS and user privileges. + * + * # Arguments + * + * * `src` - The source file path + * + * # Returns + * + * * `Ok(String)` - A success message indicating where the file was copied + * * `Err(FsError)` - An error if the copy operation failed + * + * # Examples + * + * ```no_run + * use sal::os::copy_bin; + * + * fn main() -> Result<(), Box> { + * // Copy a binary + * let result = copy_bin("my_binary")?; + * Ok(()) + * } + * ``` + */ +pub fn copy_bin(src: &str) -> Result { + let dest_path = if cfg!(target_os = "linux") && unsafe { libc::getuid() } == 0 { + Path::new("/usr/local/bin").to_path_buf() + } else { + dirs::home_dir() + .ok_or_else(|| FsError::DirectoryNotFound("Home directory not found".to_string()))? + .join("hero/bin") + }; + + let result = copy(src, dest_path.to_str().unwrap()); + if let Ok(msg) = &result { + println!("{}", msg); + } + result +} /** * Check if a file or directory exists. diff --git a/src/rhai/os.rs b/src/rhai/os.rs index 042e8da..2c5ae7a 100644 --- a/src/rhai/os.rs +++ b/src/rhai/os.rs @@ -22,6 +22,7 @@ pub fn register_os_module(engine: &mut Engine) -> Result<(), Box> // Register file system functions engine.register_fn("copy", copy); + engine.register_fn("copy_bin", copy_bin); engine.register_fn("exist", exist); engine.register_fn("find_file", find_file); engine.register_fn("find_files", find_files); @@ -74,6 +75,13 @@ pub fn copy(src: &str, dest: &str) -> Result> { os::copy(src, dest).to_rhai_error() } +/// Wrapper for os::copy_bin +/// +/// Copy a binary to the correct location based on OS and user privileges. +pub fn copy_bin(src: &str) -> Result> { + os::copy_bin(src).to_rhai_error() +} + /// Wrapper for os::exist /// /// Check if a file or directory exists.