Lower StorageConfig to arti-client crate

This commit is contained in:
Nick Mathewson 2021-11-18 22:13:59 -05:00
parent a7f5b9aefe
commit 065d3dc104
3 changed files with 42 additions and 40 deletions

View File

@ -2,10 +2,11 @@
//!
//! Some of these are re-exported from lower-level crates.
use crate::{Error, Result};
use crate::Error;
use derive_builder::Builder;
use serde::Deserialize;
use std::path::PathBuf;
use tor_config::CfgPath;
pub use tor_config::ConfigBuildError;
@ -51,6 +52,41 @@ impl Default for ClientAddrConfig {
}
}
/// Configuration for where information should be stored on disk.
///
/// This section is for read/write storage.
#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct StorageConfig {
/// Location on disk for cached directory information
cache_dir: CfgPath,
/// Location on disk for less-sensitive persistent state information.
state_dir: CfgPath,
}
impl StorageConfig {
/// Try to expand `state_dir` to be a path buffer.
// TODO(nickm): This won't be public once we're done.
pub fn expand_state_dir(&self) -> Result<PathBuf, ConfigBuildError> {
self.state_dir
.path()
.map_err(|e| ConfigBuildError::Invalid {
field: "state_dir".to_owned(),
problem: e.to_string(),
})
}
/// Try to expand `cache_dir` to be a path buffer.
// TODO(nickm): This won't be public once we're done.
pub fn expand_cache_dir(&self) -> Result<PathBuf, ConfigBuildError> {
self.state_dir
.path()
.map_err(|e| ConfigBuildError::Invalid {
field: "cache_dir".to_owned(),
problem: e.to_string(),
})
}
}
/// A configuration used to bootstrap a [`TorClient`](crate::TorClient).
///
/// In order to connect to the Tor network, Arti needs to know a few
@ -104,7 +140,7 @@ impl TorClientConfig {
/// (On unix, this usually works out to `~/.local/share/arti` and
/// `~/.cache/arti`, depending on your environment. We use the
/// `directories` crate for reasonable defaults on other platforms.)
pub fn sane_defaults() -> Result<Self> {
pub fn sane_defaults() -> crate::Result<Self> {
// Note: this must stay in sync with project_dirs() in the
// tor-config crate.
let dirs =
@ -121,7 +157,7 @@ impl TorClientConfig {
/// Returns a `TorClientConfig` using the specified state and cache directories.
///
/// All other configuration options are set to their defaults.
pub fn with_directories<P, Q>(state_dir: P, cache_dir: Q) -> Result<Self>
pub fn with_directories<P, Q>(state_dir: P, cache_dir: Q) -> crate::Result<Self>
where
P: Into<PathBuf>,
Q: Into<PathBuf>,

View File

@ -42,7 +42,7 @@ mod cmdline;
mod options;
pub use cmdline::CmdLine;
pub use options::{ArtiConfig, LoggingConfig, ProxyConfig, StorageConfig};
pub use options::{ArtiConfig, LoggingConfig, ProxyConfig};
use tor_config::CfgPath;
use std::path::{Path, PathBuf};

View File

@ -1,14 +1,13 @@
//! Handling for arti's configuration formats.
use crate::CfgPath;
use arti_client::config::{
circ::{CircMgrConfig, CircMgrConfigBuilder},
dir::{DirMgrConfig, DirMgrConfigBuilder, DownloadScheduleConfig, NetworkConfig},
ConfigBuildError, TorClientConfig,
StorageConfig, TorClientConfig,
};
use serde::Deserialize;
use std::collections::HashMap;
use std::path::PathBuf;
use tor_config::ConfigBuildError;
/// Default options to use for our configuration.
pub(crate) const ARTI_DEFAULTS: &str = concat!(include_str!("./arti_defaults.toml"),);
@ -94,39 +93,6 @@ pub struct ArtiConfig {
address_filter: arti_client::config::ClientAddrConfig,
}
/// Configuration for where information should be stored on disk.
///
/// This section is for read/write storage.
#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct StorageConfig {
/// Location on disk for cached directory information
cache_dir: CfgPath,
/// Location on disk for less-sensitive persistent state information.
state_dir: CfgPath,
}
impl StorageConfig {
/// Try to expand `state_dir` to be a path buffer.
fn expand_state_dir(&self) -> Result<PathBuf, ConfigBuildError> {
self.state_dir
.path()
.map_err(|e| ConfigBuildError::Invalid {
field: "state_dir".to_owned(),
problem: e.to_string(),
})
}
/// Try to expand `cache_dir` to be a path buffer.
fn expand_cache_dir(&self) -> Result<PathBuf, ConfigBuildError> {
self.state_dir
.path()
.map_err(|e| ConfigBuildError::Invalid {
field: "cache_dir".to_owned(),
problem: e.to_string(),
})
}
}
impl ArtiConfig {
/// Return a [`DirMgrConfig`] object based on the user's selected
/// configuration.