Skip to content

Commit

Permalink
refactor(dirs): store all the config and cache files in $HOME/.safe/qp2p
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel-faber committed Nov 6, 2020
1 parent 0f0e804 commit 64333e1
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 149 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ async-std = "1.6.5"
rand = "~0.7.3"

[target."cfg(any(all(unix, not(any(target_os = \"android\", target_os = \"androideabi\", target_os = \"ios\"))), windows))".dependencies]
directories = "2.0.2"
dirs-next = "2.0.0"
7 changes: 2 additions & 5 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use super::{
bootstrap_cache::BootstrapCache,
config::{Config, SerialisableCertificate},
connections::{Connection, RecvStream, SendStream},
dirs::{Dirs, OverRide},
endpoint::Endpoint,
error::{Error, Result},
peer_config::{self, DEFAULT_IDLE_TIMEOUT_MSEC, DEFAULT_KEEP_ALIVE_INTERVAL_MSEC},
Expand All @@ -21,6 +20,7 @@ use bytes::Bytes;
use futures::future::select_ok;
use log::{debug, error, info, trace};
use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket};
use std::path::PathBuf;

/// In the absence of a port supplied by the user via the config we will first try using this
/// before using a random port.
Expand Down Expand Up @@ -152,10 +152,7 @@ impl QuicP2p {
our_complete_cert.obtain_priv_key_and_cert()?
};

let custom_dirs = cfg
.bootstrap_cache_dir
.clone()
.map(|custom_dir| Dirs::Overide(OverRide::new(&custom_dir)));
let custom_dirs = cfg.bootstrap_cache_dir.clone().map(PathBuf::from);

let mut qp2p_config = cfg.clone();

Expand Down
25 changes: 9 additions & 16 deletions src/bootstrap_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@

#![allow(unused)]

use crate::dirs::Dirs;
use crate::utils;
use crate::{Error, Result};
use log::info;
use std::{
collections::{HashSet, VecDeque},
fs, io,
net::SocketAddr,
path::PathBuf,
path::{Path, PathBuf},
};

/// Maximum peers in the cache.
Expand All @@ -42,17 +41,14 @@ impl BootstrapCache {
/// not be cached upon successful connection.
pub fn new(
hard_coded_contacts: HashSet<SocketAddr>,
user_override: Option<&Dirs>,
user_override: Option<&PathBuf>,
fresh: bool,
) -> Result<BootstrapCache> {
let path = |dir: &Dirs| {
let path = dir.cache_dir();
path.join("bootstrap_cache")
};
let get_cache_path = |dir: &PathBuf| dir.join("bootstrap_cache");

let cache_path = user_override.map_or_else(
|| Ok::<_, Error>(path(&utils::project_dir()?)),
|d| Ok(path(d)),
|| Ok::<_, Error>(get_cache_path(&utils::project_dir()?)),
|d| Ok(get_cache_path(d)),
)?;

let peers = if cache_path.exists() {
Expand Down Expand Up @@ -114,15 +110,12 @@ impl BootstrapCache {
self.try_sync_to_disk();
}

pub fn clear_from_disk(user_override: Option<&Dirs>) -> Result<()> {
let path = |dir: &Dirs| {
let path = dir.cache_dir();
path.join("bootstrap_cache")
};
pub fn clear_from_disk(user_override: Option<&PathBuf>) -> Result<()> {
let get_cache_path = |dir: &PathBuf| dir.join("bootstrap_cache");

let cache_path = user_override.map_or_else(
|| Ok::<_, Error>(path(&utils::project_dir()?)),
|d| Ok(path(d)),
|| Ok::<_, Error>(get_cache_path(&utils::project_dir()?)),
|d| Ok(get_cache_path(d)),
)?;

if cache_path.exists() {
Expand Down
17 changes: 7 additions & 10 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
// Software.

use crate::{
dirs::Dirs,
error::{Error, Result},
utils,
};
use bytes::Bytes;
use log::trace;
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::{
collections::HashSet, fmt, fs, io, net::IpAddr, net::SocketAddr, path::PathBuf, str::FromStr,
};
Expand Down Expand Up @@ -78,7 +78,7 @@ impl Config {
/// Try and read the config off the disk first. If such a file-path doesn't exist it'll create
/// a default one with random certificate and write that to the disk, eventually returning that
/// config to the caller.
pub fn read_or_construct_default(user_override: Option<&Dirs>) -> Result<Config> {
pub fn read_or_construct_default(user_override: Option<&Path>) -> Result<Config> {
let config_path = config_path(user_override)?;

if config_path.exists() {
Expand All @@ -99,7 +99,7 @@ impl Config {
}

/// Clear all configuration files from disk
pub fn clear_config_from_disk(user_override: Option<&Dirs>) -> Result<()> {
pub fn clear_config_from_disk(user_override: Option<&Path>) -> Result<()> {
let config_path = config_path(user_override)?;
if config_path.exists() {
fs::remove_file(&config_path)?;
Expand Down Expand Up @@ -175,15 +175,12 @@ impl fmt::Debug for SerialisableCertificate {
}
}

fn config_path(user_override: Option<&Dirs>) -> Result<PathBuf> {
let path = |dir: &Dirs| {
let path = dir.config_dir();
path.join("config")
};
fn config_path(user_override: Option<&Path>) -> Result<PathBuf> {
let get_config_file = |dir: &Path| dir.join("config");

let cfg_path = user_override.map_or_else(
|| Ok::<_, Error>(path(&utils::project_dir()?)),
|d| Ok(path(d)),
|| Ok::<_, Error>(get_config_file(&utils::project_dir()?)),
|d| Ok(get_config_file(d)),
)?;

Ok(cfg_path)
Expand Down
102 changes: 0 additions & 102 deletions src/dirs.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ mod api;
mod bootstrap_cache;
mod config;
mod connections;
mod dirs;
mod endpoint;
mod error;
mod igd;
Expand All @@ -65,7 +64,6 @@ mod wire_msg;
pub use api::{Message, QuicP2p};
pub use config::Config;
pub use connections::{Connection, IncomingConnections, IncomingMessages, RecvStream, SendStream};
pub use dirs::{Dirs, OverRide};
pub use endpoint::Endpoint;
pub use error::{Error, Result};

Expand Down
6 changes: 2 additions & 4 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
// specific language governing permissions and limitations relating to use of the SAFE Network
// Software.

use crate::dirs::{Dirs, OverRide};
use rand::Rng;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::path::PathBuf;
use unwrap::unwrap;

pub(crate) fn test_dirs() -> Dirs {
Dirs::Overide(OverRide::new(&unwrap!(tmp_rand_dir().to_str())))
pub(crate) fn test_dirs() -> PathBuf {
tmp_rand_dir()
}

pub(crate) fn rand_node_addr() -> SocketAddr {
Expand Down
16 changes: 7 additions & 9 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
// specific language governing permissions and limitations relating to use of the SAFE Network
// Software.

use crate::{
dirs::Dirs,
error::{Error, Result},
};
use crate::error::{Error, Result};
use flexi_logger::{DeferredNow, Logger};
use log::Record;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::fs::File;
use std::io::{BufReader, BufWriter, Write};
use std::path::Path;
use std::path::{Path, PathBuf};

/// Get the project directory
#[cfg(any(
Expand All @@ -28,10 +25,11 @@ use std::path::Path;
windows
))]
#[inline]
pub fn project_dir() -> Result<Dirs> {
let dirs = directories::ProjectDirs::from("net", "MaidSafe", "qp2p")
.ok_or_else(|| Error::Io(::std::io::ErrorKind::NotFound.into()))?;
Ok(Dirs::Desktop(dirs))
pub fn project_dir() -> Result<PathBuf> {
let dirs =
dirs_next::home_dir().ok_or_else(|| Error::Io(::std::io::ErrorKind::NotFound.into()))?;
let project_dir = dirs.join(".safe").join("qp2p");
Ok(project_dir)
}

/// Get the project directory
Expand Down

0 comments on commit 64333e1

Please sign in to comment.