Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 33 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions src/dfx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,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 Down Expand Up @@ -85,6 +86,11 @@ url = "2.1.0"
walkdir = "2.2.9"
wasmparser = "0.87.0"
which = "4.2.5"
dirs-next = "2.0.0"
Comment thread
adamspofford-dfinity marked this conversation as resolved.
Outdated
supports-color = "1.3.0"

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

[dependencies.ic-agent]
version = "0.20.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
78 changes: 47 additions & 31 deletions src/dfx/src/config/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ 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 +70,19 @@ 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");
#[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 = cache_root.map_or_else(
|| dirs_next::cache_dir().unwrap().join("dfinity"),
PathBuf::from,
);
if !p.exists() {
if let Err(_e) = std::fs::create_dir_all(&p) {
return Err(DfxError::new(CacheError::CannotCreateCacheDirectory(p)));
Expand Down Expand Up @@ -181,22 +191,25 @@ 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(|| {
#[cfg(unix)]
Comment thread
adamspofford-dfinity marked this conversation as resolved.
{
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 +224,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())
})?;
#[cfg(unix)]
Comment thread
adamspofford-dfinity marked this conversation as resolved.
{
// 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())
})?;
}

// atomically install cache version into place
if force && p.exists() {
Expand Down
16 changes: 12 additions & 4 deletions src/dfx/src/lib/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ 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");
#[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 = config_root.map_or_else(
|| dirs_next::config_dir().unwrap().join("dfx"),
PathBuf::from,
);
Comment thread
adamspofford-dfinity marked this conversation as resolved.
Outdated
if !p.exists() {
std::fs::create_dir_all(&p)
.with_context(|| format!("Cannot create config directory at {}", p.display()))?;
Expand Down
30 changes: 17 additions & 13 deletions src/dfx/src/lib/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ use indicatif::{ProgressBar, ProgressDrawTarget};
use semver::Version;
use std::fs;
use std::io::Write;
use std::path::Path;
use tar::Archive;

pub static DEFAULT_RELEASE_ROOT: &str = "https://sdk.dfinity.org";
pub static CACHE_ROOT: &str = ".cache/dfinity/versions/";
pub static DOWNLOADS_DIR: &str = ".cache/dfinity/downloads/";
pub static CACHE_ROOT: &str = "dfinity/versions/";
Comment thread
adamspofford-dfinity marked this conversation as resolved.
Outdated
pub static DOWNLOADS_DIR: &str = "dfinity/downloads/";

#[context("Failed to get distribution manifest.")]
pub fn get_manifest() -> DfxResult<Manifest> {
Expand Down Expand Up @@ -58,10 +57,14 @@ pub fn install_version(version: &Version) -> DfxResult<()> {
))
.map_err(|e| error_invalid_argument!("invalid url: {}", e))?;

let home = std::env::var("HOME").context("Failed to resolve env var HOME.")?;
let home = Path::new(&home);
#[cfg(not(windows))]
let cache_dir =
std::path::Path::new(&std::env::var_os("HOME").context("Failed to resolve env var HOME.")?)
.join(".cache");
#[cfg(windows)]
let cache_dir = dirs_next::cache_dir().unwrap();

let download_dir = home.join(DOWNLOADS_DIR);
let download_dir = cache_dir.join(DOWNLOADS_DIR);
if !download_dir.exists() {
fs::create_dir_all(&download_dir)
.with_context(|| format!("Failed to create dir {}.", download_dir.to_string_lossy()))?;
Expand All @@ -88,11 +91,12 @@ pub fn install_version(version: &Version) -> DfxResult<()> {
b.finish_with_message("Download complete");
}

let mut cache_dir = home.join(CACHE_ROOT);
cache_dir.push(version.to_string());
if !cache_dir.exists() {
fs::create_dir_all(&cache_dir)
.with_context(|| format!("Failed to create {}.", cache_dir.to_string_lossy()))?;
let mut version_cache_dir = cache_dir.join(CACHE_ROOT);
version_cache_dir.push(version.to_string());
if !version_cache_dir.exists() {
fs::create_dir_all(&version_cache_dir).with_context(|| {
format!("Failed to create {}.", version_cache_dir.to_string_lossy())
})?;
}

let b = ProgressBar::new_spinner();
Expand All @@ -106,7 +110,7 @@ pub fn install_version(version: &Version) -> DfxResult<()> {
.with_context(|| format!("Failed to open {}.", download_file.to_string_lossy()))?;
let tar = GzDecoder::new(tar_gz);
let mut archive = Archive::new(tar);
archive.unpack(&cache_dir).with_context(|| {
archive.unpack(&version_cache_dir).with_context(|| {
format!(
"Failed to unpack archive at {}.",
download_file.to_string_lossy()
Expand All @@ -115,7 +119,7 @@ pub fn install_version(version: &Version) -> DfxResult<()> {
b.finish_with_message("Unpack complete");

// Install components
let dfx = cache_dir.join("dfx");
let dfx = version_cache_dir.join("dfx");
std::process::Command::new(dfx)
.args(&["cache", "install"])
.status()
Expand Down
1 change: 1 addition & 0 deletions src/dfx/src/lib/error/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum CacheError {
#[error("Cannot find cache directory at '{0}'.")]
CannotFindCacheDirectory(PathBuf),

#[cfg(not(windows))]
#[error("Cannot find home directory.")]
CannotFindHomeDirectory(),

Expand Down
Loading