Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/dfx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ mime = "0.3.16"
mime_guess = "2.0.4"
net2 = "0.2.34"
num-traits = "0.2"
os_str_bytes = "6.3.0"
pem = "1.0.2"
petgraph = "0.6.0"
rand = "0.8.5"
Expand All @@ -76,6 +77,7 @@ shell-words = "1.1.0"
slog = { version = "2.5.2", features = ["max_level_trace"] }
slog-async = "2.4.0"
slog-term = "2.9.0"
supports-color = "1.3.0"
sysinfo = "0.24.4"
tar = "0.4.38"
tempfile = "3.3.0"
Expand All @@ -89,6 +91,9 @@ walkdir = "2.2.9"
wasmparser = "0.87.0"
which = "4.2.5"

[target.'cfg(windows)'.dependencies]
junction = "0.2.0"

[dev-dependencies]
env_logger = "0.9"
proptest = "1.0"
Expand Down
9 changes: 8 additions & 1 deletion src/dfx/src/commands/identity/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ pub struct NewIdentityOpts {
/// The identity to create.
new_identity: String,

/// The file path to the opensc-pkcs11 library e.g. "/usr/local/lib/opensc-pkcs11.so"
#[cfg_attr(
not(windows),
doc = r#"The file path to the opensc-pkcs11 library e.g. "/usr/local/lib/opensc-pkcs11.so""#
)]
#[cfg_attr(
windows,
doc = r#"The file path to the opensc-pkcs11 library e.g. "C:\Program Files (x86)\OpenSC Project\OpenSC\pkcs11\opensc-pkcs11.dll"#
)]
#[clap(long, requires("hsm-key-id"))]
hsm_pkcs11_lib_path: Option<String>,

Expand Down
11 changes: 6 additions & 5 deletions src/dfx/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use anyhow::{anyhow, bail, Context, Error};
use clap::Parser;
use fn_error_context::context;
use garcon::{Delay, Waiter};
use os_str_bytes::{OsStrBytes, OsStringBytes};
use slog::{info, warn, Logger};
use std::fs;
use std::fs::create_dir_all;
Expand Down Expand Up @@ -526,20 +527,20 @@ fn create_new_persistent_socket_path(uds_holder_path: &Path, prefix: &str) -> Df
// Unix domain socket names can only be so long.
// An attempt to use a path under .dfx/ resulted in this error:
// path must be shorter than libc::sockaddr_un.sun_path
let uds_path = format!("/tmp/{}.{}.{}", prefix, pid, timestamp_seconds);
std::fs::write(uds_holder_path, &uds_path).with_context(|| {
let uds_path = std::env::temp_dir().join(format!("{}.{}.{}", prefix, pid, timestamp_seconds));
std::fs::write(uds_holder_path, &uds_path.to_raw_bytes()).with_context(|| {
format!(
"unable to write unix domain socket path to {}",
uds_holder_path.to_string_lossy()
)
})?;
Ok(PathBuf::from(uds_path))
Ok(uds_path)
}

#[context("Failed to get persistent socket path for {} at {}.", prefix, uds_holder_path.to_string_lossy())]
fn get_persistent_socket_path(uds_holder_path: &Path, prefix: &str) -> DfxResult<PathBuf> {
if let Ok(uds_path) = std::fs::read_to_string(uds_holder_path) {
Ok(PathBuf::from(uds_path.trim()))
if let Ok(uds_path) = std::fs::read(uds_holder_path) {
Ok(PathBuf::assert_from_raw_vec(uds_path))
} else {
create_new_persistent_socket_path(uds_holder_path, prefix)
}
Expand Down
83 changes: 51 additions & 32 deletions src/dfx/src/config/cache.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
use crate::config::dfx_version;
use crate::lib::error::{CacheError, DfxError, DfxResult};
use crate::util;
#[cfg(windows)]
use crate::util::project_dirs;

use anyhow::{bail, Context};
use fn_error_context::context;
use indicatif::{ProgressBar, ProgressDrawTarget};
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use semver::Version;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;
use std::process::ExitStatus;

// POSIX permissions for files in the cache.
#[cfg(unix)]
const EXEC_READ_USER_ONLY_PERMISSION: u32 = 0o500;

pub trait Cache {
Expand Down Expand Up @@ -68,11 +72,20 @@ impl Cache for DiskBasedCache {

#[context("Failed to get cache root.")]
pub fn get_cache_root() -> DfxResult<PathBuf> {
let cache_root = std::env::var("DFX_CACHE_ROOT").ok();
let home =
std::env::var("HOME").map_err(|_| DfxError::new(CacheError::CannotFindHomeDirectory()))?;
let root = cache_root.unwrap_or(home);
let p = PathBuf::from(root).join(".cache").join("dfinity");
let cache_root = std::env::var_os("DFX_CACHE_ROOT");
// dirs-next is not used for *nix to preserve existing paths
#[cfg(not(windows))]
let p = {
let home = std::env::var_os("HOME")
.ok_or_else(|| DfxError::new(CacheError::CannotFindHomeDirectory()))?;
let root = cache_root.unwrap_or(home);
PathBuf::from(root).join(".cache").join("dfinity")
};
#[cfg(windows)]
let p = match cache_root {
Some(var) => PathBuf::from(var),
None => project_dirs()?.cache_dir().to_owned(),
};
if !p.exists() {
if let Err(_e) = std::fs::create_dir_all(&p) {
return Err(DfxError::new(CacheError::CannotCreateCacheDirectory(p)));
Expand Down Expand Up @@ -180,23 +193,26 @@ pub fn install_version(v: &str, force: bool) -> DfxResult<PathBuf> {
}
file.unpack_in(temp_p.as_path())
.context("Failed to unpack archive asset.")?;

let full_path = temp_p.join(file.path().context("Failed to get file path.")?);
let mut perms = std::fs::metadata(full_path.as_path())
.with_context(|| {
// On *nix we need to set the execute permission as the tgz doesn't include it
#[cfg(unix)]
{
let full_path = temp_p.join(file.path().context("Failed to get file path.")?);
let mut perms = std::fs::metadata(full_path.as_path())
.with_context(|| {
format!(
"Failed to get file metadata for {}.",
full_path.to_string_lossy()
)
})?
.permissions();
perms.set_mode(EXEC_READ_USER_ONLY_PERMISSION);
std::fs::set_permissions(full_path.as_path(), perms).with_context(|| {
format!(
"Failed to get file metadata for {}.",
"Failed to set file permissions for {}.",
full_path.to_string_lossy()
)
})?
.permissions();
perms.set_mode(EXEC_READ_USER_ONLY_PERMISSION);
std::fs::set_permissions(full_path.as_path(), perms).with_context(|| {
format!(
"Failed to set file permissions for {}.",
full_path.to_string_lossy()
)
})?;
})?;
}
}

// Copy our own binary in the cache.
Expand All @@ -211,19 +227,22 @@ pub fn install_version(v: &str, force: bool) -> DfxResult<PathBuf> {
dfx.to_string_lossy()
)
})?;
// And make it executable.
let mut perms = std::fs::metadata(&dfx)
.with_context(|| {
format!(
"Failed to read file metadata for {}.",
dfx.to_string_lossy()
)
})?
.permissions();
perms.set_mode(EXEC_READ_USER_ONLY_PERMISSION);
std::fs::set_permissions(&dfx, perms).with_context(|| {
format!("Failed to set file metadata for {}.", dfx.to_string_lossy())
})?;
// On *nix we need to set the execute permission as the tgz doesn't include it
#[cfg(unix)]
{
let mut perms = std::fs::metadata(&dfx)
.with_context(|| {
format!(
"Failed to read file metadata for {}.",
dfx.to_string_lossy()
)
})?
.permissions();
perms.set_mode(EXEC_READ_USER_ONLY_PERMISSION);
std::fs::set_permissions(&dfx, perms).with_context(|| {
format!("Failed to set file metadata for {}.", dfx.to_string_lossy())
})?;
}

// atomically install cache version into place
if force && p.exists() {
Expand Down
11 changes: 5 additions & 6 deletions src/dfx/src/config/dfinity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ use crate::lib::bitcoin::adapter::config::BitcoinAdapterLogLevel;
use crate::lib::canister_http::adapter::config::HttpAdapterLogLevel;
use crate::lib::config::get_config_dfx_dir_path;
use crate::lib::error::{BuildError, DfxError, DfxResult};
use crate::util::{PossiblyStr, SerdeVec};
use crate::util::{project_dirs, PossiblyStr, SerdeVec};
use crate::{error_invalid_argument, error_invalid_config, error_invalid_data};

use anyhow::{anyhow, Context};
use byte_unit::Byte;
use candid::Principal;
use directories_next::ProjectDirs;
use fn_error_context::context;
use schemars::JsonSchema;
use serde::de::{Error as _, MapAccess, Visitor};
Expand Down Expand Up @@ -883,10 +882,10 @@ impl NetworksConfig {
}
#[context("Failed to determine shared network data directory.")]
pub fn get_network_data_directory(network: &str) -> DfxResult<PathBuf> {
let project_dirs = ProjectDirs::from("org", "dfinity", "dfx").ok_or_else(|| {
anyhow!("Unable to retrieve a valid home directory path from the operating system")
})?;
Ok(project_dirs.data_local_dir().join("network").join(network))
Ok(project_dirs()?
.data_local_dir()
.join("network")
.join(network))
}

#[context("Failed to read shared networks configuration.")]
Expand Down
19 changes: 15 additions & 4 deletions src/dfx/src/lib/config.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
use crate::lib::error::DfxResult;
#[cfg(windows)]
use crate::util::project_dirs;

use anyhow::{bail, Context};
use fn_error_context::context;
use std::path::PathBuf;

#[context("Failed to get path to dfx config dir.")]
pub fn get_config_dfx_dir_path() -> DfxResult<PathBuf> {
let config_root = std::env::var("DFX_CONFIG_ROOT").ok();
let home = std::env::var("HOME").context("Failed to resolve 'HOME' env var.")?;
let root = config_root.unwrap_or(home);
let p = PathBuf::from(root).join(".config").join("dfx");
let config_root = std::env::var_os("DFX_CONFIG_ROOT");
// dirs-next is not used for *nix to preserve existing paths
#[cfg(not(windows))]
let p = {
let home = std::env::var_os("HOME").context("Failed to resolve 'HOME' env var.")?;
let root = config_root.unwrap_or(home);
PathBuf::from(root).join(".config").join("dfx")
};
#[cfg(windows)]
let p = match config_root {
Some(var) => PathBuf::from(var),
None => project_dirs()?.config_dir().to_owned(),
};
if !p.exists() {
std::fs::create_dir_all(&p)
.with_context(|| format!("Cannot create config directory at {}", p.display()))?;
Expand Down
Loading