Skip to content

Commit

Permalink
implement cross-platform uid,gid support with platform-specific depen…
Browse files Browse the repository at this point in the history
…dencies
  • Loading branch information
Canvinus committed Apr 6, 2024
1 parent 05e24fc commit 40f98ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
4 changes: 3 additions & 1 deletion cargo-near/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ near-abi = { version = "0.4.0", features = ["__chunked-entries"] }
libloading = "0.7.3"
zstd = "0.11"
atty = "0.2.14"
nix = { version = "0.28.0", features = ["user", "process"]}

color-eyre = "0.6"
inquire = "0.6"
Expand All @@ -53,6 +52,9 @@ git2 = "0.14"
cargo_toml = "0.19.1"
reqwest = "0.11.24"

[target.'cfg(unix)'.dependencies]
nix = { version = "0.28.0", features = ["user", "process"] }

[features]
default = ["ledger"]
ledger = ["near-cli-rs/ledger"]
20 changes: 12 additions & 8 deletions cargo-near/src/commands/build_command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::process::Command;
use std::process::{Command, id};
use std::time::{SystemTime, UNIX_EPOCH};
use nix::unistd::{getuid, getgid, getpid};

#[cfg(unix)]
use nix::unistd::{getuid, getgid};

use color_eyre::{
eyre::{ContextCompat, WrapErr},
Expand Down Expand Up @@ -105,11 +107,8 @@ pub fn docker_run(args: BuildCommand) -> color_eyre::eyre::Result<camino::Utf8Pa

let tmp_repo = git2::Repository::clone(contract_path.as_str(), &tmp_contract_path)?;

// get uid, gid and pid using libc
let uid = getuid().to_string();
let gid = getgid().to_string();
let pid = getpid().to_string();

// Cross-platform process ID and timestamp
let pid = id().to_string();
let timestamp = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs().to_string();

let volume = format!(
Expand All @@ -122,7 +121,12 @@ pub fn docker_run(args: BuildCommand) -> color_eyre::eyre::Result<camino::Utf8Pa
let docker_image = "docker.io/sourcescan/cargo-near:0.6.0-builder"; //XXX need to fix version!!! image from cargo.toml for contract
let docker_container_name = format!("cargo-near-{}-{}", timestamp, pid);
let near_build_env_ref = format!("NEAR_BUILD_ENVIRONMENT_REF={}", docker_image);
let uid_gid = format!("{}:{}", uid, gid);

// Platform-specific UID/GID retrieval
#[cfg(unix)]
let uid_gid = format!("{}:{}", getuid(), getgid());
#[cfg(not(unix))]
let uid_gid = "1000:1000".to_string();

let mut docker_args = vec![
"-u", &uid_gid,
Expand Down

0 comments on commit 40f98ba

Please sign in to comment.