Skip to content

Commit

Permalink
Cleanup all directories
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n authored May 29, 2024
1 parent 6d82257 commit 6cf1a9a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 28 deletions.
5 changes: 3 additions & 2 deletions crates/core/src/bin/freenet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use freenet::{
local_node::{Executor, OperationMode},
server::{local_node::run_local_node, network_node::run_network_node},
};
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::Arc};

type DynError = Box<dyn std::error::Error + Send + Sync + 'static>;

Expand All @@ -18,7 +18,8 @@ async fn run(config: Config) -> Result<(), DynError> {
async fn run_local(config: Config) -> Result<(), DynError> {
let port = config.ws_api.port;
let ip = config.ws_api.address;
let executor = Executor::from_config(&config, None).await?;
let executor = Executor::from_config(Arc::new(config), None).await?;

let socket: SocketAddr = (ip, port).into();
run_local_node(executor, socket).await
}
Expand Down
58 changes: 55 additions & 3 deletions crates/core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
io::{Read, Write},
net::{IpAddr, Ipv4Addr},
path::PathBuf,
sync::atomic::AtomicBool,
sync::{atomic::AtomicBool, Arc},
time::Duration,
};

Expand Down Expand Up @@ -246,7 +246,7 @@ impl ConfigArgs {
},
transport_keypair: transport_key.unwrap_or_else(TransportKeypair::new),
log_level: self.log_level.unwrap_or(tracing::log::LevelFilter::Info),
config_paths: self.config_paths.build(self.id.as_deref())?,
config_paths: Arc::new(self.config_paths.build(self.id.as_deref())?),
};

fs::create_dir_all(&this.config_paths.config_dir)?;
Expand Down Expand Up @@ -350,7 +350,7 @@ pub struct Config {
#[serde(with = "serde_log_level_filter")]
pub log_level: tracing::log::LevelFilter,
#[serde(flatten)]
config_paths: ConfigPaths,
config_paths: Arc<ConfigPaths>,
#[serde(skip)]
pub(crate) peer_id: Option<PeerId>,
}
Expand All @@ -359,6 +359,10 @@ impl Config {
pub fn transport_keypair(&self) -> &TransportKeypair {
&self.transport_keypair
}

pub(crate) fn paths(&self) -> Arc<ConfigPaths> {
self.config_paths.clone()
}
}

#[derive(clap::Parser, Debug, Default, Copy, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -666,8 +670,56 @@ impl ConfigPaths {
self.event_log = event_log;
self
}

pub fn iter(&self) -> ConfigPathsIter {
ConfigPathsIter {
curr: 0,
config_paths: self,
}
}

fn path_by_index(&self, index: usize) -> (bool, &PathBuf) {
match index {
0 => (true, &self.contracts_dir),
1 => (true, &self.delegates_dir),
2 => (true, &self.secrets_dir),
3 => (true, &self.db_dir),
4 => (true, &self.data_dir),
5 => (false, &self.event_log),
6 => (true, &self.config_dir),
_ => panic!("invalid path index"),
}
}

const MAX_PATH_INDEX: usize = 6;
}

pub struct ConfigPathsIter<'a> {
curr: usize,
config_paths: &'a ConfigPaths,
}

impl<'a> Iterator for ConfigPathsIter<'a> {
/// The first is whether this path is a directory or a file.
type Item = (bool, &'a PathBuf);

fn next(&mut self) -> Option<Self::Item> {
if self.curr > ConfigPaths::MAX_PATH_INDEX {
None
} else {
let path = self.config_paths.path_by_index(self.curr);
self.curr += 1;
Some(path)
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(ConfigPaths::MAX_PATH_INDEX))
}
}

impl<'a> core::iter::FusedIterator for ConfigPathsIter<'a> {}

impl Config {
pub fn db_dir(&self) -> PathBuf {
self.config_paths.db_dir(self.mode)
Expand Down
8 changes: 4 additions & 4 deletions crates/core/src/contract/executor/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,17 @@ impl ContractExecutor for Executor<Runtime> {

impl Executor<Runtime> {
pub async fn from_config(
config: &Config,
config: Arc<Config>,
event_loop_channel: Option<ExecutorToEventLoopChannel<ExecutorHalve>>,
) -> Result<Self, DynError> {
let (contract_store, delegate_store, secret_store, state_store) =
Self::get_stores(config).await?;
Self::get_stores(&config).await?;
let rt = Runtime::build(contract_store, delegate_store, secret_store, false).unwrap();
Executor::new(
state_store,
|| {
move || {
// FIXME: potentially not cleaning up after exit
crate::util::set_cleanup_on_exit(None)?;
crate::util::set_cleanup_on_exit(config.paths().clone())?;
Ok(())
},
OperationMode::Local,
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/contract/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl ContractHandler for NetworkContractHandler<Runtime> {
where
Self: Sized + 'static,
{
let executor = Executor::from_config(&config, Some(executor_request_sender)).await?;
let executor = Executor::from_config(config.clone(), Some(executor_request_sender)).await?;
Ok(Self { executor, channel })
}

Expand Down
42 changes: 26 additions & 16 deletions crates/core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,45 @@ pub(crate) mod time_source;
use std::{
collections::{BTreeMap, HashSet},
net::{Ipv4Addr, SocketAddr, TcpListener},
sync::Arc,
time::Duration,
};

use crate::{config::ConfigPaths, node::PeerId};
use rand::{
prelude::{Rng, StdRng},
SeedableRng,
};

use crate::node::PeerId;

pub fn set_cleanup_on_exit(id: Option<String>) -> Result<(), ctrlc::Error> {
pub fn set_cleanup_on_exit(config: Arc<ConfigPaths>) -> Result<(), ctrlc::Error> {
ctrlc::set_handler(move || {
tracing::info!("Received Ctrl+C. Cleaning up...");

#[cfg(debug_assertions)]
{
let Ok(path) = crate::config::ConfigPathsArgs::app_data_dir(id.as_deref()) else {
std::process::exit(0);
};
tracing::info!("Removing content stored at {path:?}");

if path.exists() {
let rm = std::fs::remove_dir_all(&path).map_err(|err| {
tracing::warn!("Failed cleaning up directory: {err}");
err
});
if rm.is_err() {
tracing::error!("Failed to remove content at {path:?}");
std::process::exit(-1);
let paths = config.iter();
for (is_dir, path) in paths {
if path.exists() {
tracing::info!("Removing content stored at {path:?}");
let rm = if is_dir {
std::fs::remove_dir_all(path).map_err(|err| {
tracing::warn!("Failed cleaning up directory: {err}");
err
})
} else {
std::fs::remove_file(path).map_err(|err| {
tracing::warn!("Failed cleaning up file: {err}");
err
})
};

match rm {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => {
tracing::error!("Failed to remove directory at {path:?}");
std::process::exit(-1);
}
_ => {}
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/fdev/src/wasm_runtime.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
net::{IpAddr, Ipv4Addr},
path::PathBuf,
sync::Arc,
};

use clap::ArgGroup;
Expand All @@ -18,8 +19,8 @@ pub async fn run_local_executor(config: ExecutorConfig) -> Result<(), anyhow::Er
}

if config.clean_exit {
// FIXME: potentially not cleaning up the correct directory
freenet::util::set_cleanup_on_exit(None)?;
let paths = config.paths.clone().build(None)?;
freenet::util::set_cleanup_on_exit(Arc::new(paths))?;
}

let app_state = state::AppState::new(&config).await?;
Expand Down

0 comments on commit 6cf1a9a

Please sign in to comment.