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
3 changes: 2 additions & 1 deletion mm2src/adex_cli/src/adex_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const VOLUME_PRECISION_MIN: usize = 2;
const VOLUME_PRECISION_MAX: usize = 5;
const VOLUME_PRECISION: SmartFractPrecision = (VOLUME_PRECISION_MIN, VOLUME_PRECISION_MAX);
const PRICE_PRECISION: SmartFractPrecision = (PRICE_PRECISION_MIN, PRICE_PRECISION_MAX);
const CFG_FILE_PERM_MODE: u32 = 0o660;

pub(super) fn get_config() {
let Ok(adex_cfg) = AdexConfigImpl::from_config_path() else { return; };
Expand Down Expand Up @@ -151,7 +152,7 @@ impl AdexConfigImpl {
let adex_path_str = cfg_path
.to_str()
.ok_or_else(|| error_anyhow!("Failed to get cfg_path as str"))?;
rewrite_json_file(self, adex_path_str)
rewrite_json_file(self, adex_path_str, Some(CFG_FILE_PERM_MODE))
}

fn set_rpc_password(&mut self, rpc_password: String) { self.rpc_password.replace(rpc_password); }
Expand Down
15 changes: 12 additions & 3 deletions mm2src/adex_cli/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use serde::{Deserialize, Serialize};
use std::fs;
use std::io::Write;
use std::ops::Deref;
#[cfg(unix)] use std::os::unix::fs::PermissionsExt;
use std::path::Path;

use crate::error_anyhow;

pub(crate) fn rewrite_data_file<T>(data: T, file: &str) -> Result<()>
pub(crate) fn rewrite_data_file<T>(data: T, file: &str, _unix_mode: Option<u32>) -> Result<()>
Comment thread
onur-ozkan marked this conversation as resolved.
Outdated
where
T: Deref<Target = [u8]>,
{
Expand All @@ -22,15 +23,23 @@ where
writer
.write(&data)
.map_err(|error| error_anyhow!("Failed to write data into {file}: {error}"))?;

#[cfg(unix)]
if let Some(unix_mode) = _unix_mode {
let mut perms = fs::metadata(file)?.permissions();
perms.set_mode(unix_mode);
fs::set_permissions(file, perms)?;
}

Ok(())
}

pub(crate) fn rewrite_json_file<T>(value: &T, file: &str) -> Result<()>
pub(crate) fn rewrite_json_file<T>(value: &T, file: &str, unix_mode: Option<u32>) -> Result<()>
where
T: Serialize,
{
let data = serde_json::to_vec_pretty(value).map_err(|error| error_anyhow!("Failed to serialize data {error}"))?;
rewrite_data_file(data, file)
rewrite_data_file(data, file, unix_mode)
}

pub(crate) fn read_json_file<T>(file: &Path) -> Result<T>
Expand Down
2 changes: 1 addition & 1 deletion mm2src/adex_cli/src/scenarios/init_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub(crate) async fn init_coins(coins_file: &str) -> Result<()> {
.await
.map_err(|error| error_anyhow!("Failed to get coin set from: {FULL_COIN_SET_ADDRESS}, error: {error}"))?;

rewrite_data_file(coins_data, coins_file)?;
rewrite_data_file(coins_data, coins_file, None)?;
info!("Got coins data, written into: {coins_file}");
Ok(())
}
2 changes: 1 addition & 1 deletion mm2src/adex_cli/src/scenarios/init_mm2_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) fn init_mm2_cfg(cfg_file: &str) -> Result<()> {
let mut mm2_cfg = Mm2Cfg::new();
info!("Start collecting mm2_cfg into: {cfg_file}");
mm2_cfg.inquire()?;
helpers::rewrite_json_file(&mm2_cfg, cfg_file)?;
helpers::rewrite_json_file(&mm2_cfg, cfg_file, None)?;
info!("mm2_cfg has been writen into: {cfg_file}");

Ok(())
Expand Down