diff --git a/server/src/config/mod.rs b/server/src/config/mod.rs index 2a131be9..666eff1a 100644 --- a/server/src/config/mod.rs +++ b/server/src/config/mod.rs @@ -26,7 +26,6 @@ //! This module provides tools to handle configuration files and settings -use crate::diskstore::snapshot::DIR_SNAPSHOT; #[cfg(test)] use libsky::TResult; use serde::Deserialize; @@ -36,7 +35,6 @@ use std::fs; #[cfg(test)] use std::net::Ipv6Addr; use std::net::{IpAddr, Ipv4Addr}; -use std::path::PathBuf; use toml; const DEFAULT_IPV4: IpAddr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)); @@ -407,14 +405,10 @@ impl fmt::Display for ConfigError { /// This parses a configuration file if it is supplied as a command line argument /// or it returns the default configuration. **If** the configuration file /// contains an error, then this returns it as an `Err` variant -pub fn get_config_file_or_return_cfg() -> Result, ConfigError> { +pub fn get_config_file_or_return_cfg() -> Result, ConfigError> { let cfg_layout = load_yaml!("../cli.yml"); let matches = App::from_yaml(cfg_layout).get_matches(); - let restorefile = matches.value_of("restore").map(|val| { - let mut path = PathBuf::from(DIR_SNAPSHOT); - path.push(val); - path - }); + let restorefile = matches.value_of("restore").map(|v| v.to_string()); // Check flags let sslonly = matches.is_present("sslonly"); let noart = matches.is_present("noart"); diff --git a/server/src/coredb/mod.rs b/server/src/coredb/mod.rs index 2a182c19..74f335bf 100644 --- a/server/src/coredb/mod.rs +++ b/server/src/coredb/mod.rs @@ -41,7 +41,6 @@ use parking_lot::RwLock; use parking_lot::RwLockReadGuard; use parking_lot::RwLockWriteGuard; use std::collections::HashMap; -use std::path::PathBuf; use std::sync::Arc; use tokio; use tokio::sync::Notify; @@ -285,9 +284,9 @@ impl CoreDB { pub fn new( bgsave: BGSave, snapshot_cfg: SnapshotConfig, - restore_file: Option, + restore_file: Option, ) -> TResult<(Self, Option, flock::FileLock)> { - let coretable = diskstore::get_saved(restore_file)?; + let coretable = diskstore::get_snapshot(restore_file)?; let mut background_tasks: usize = 0; if !bgsave.is_disabled() { background_tasks += 1; diff --git a/server/src/dbnet/mod.rs b/server/src/dbnet/mod.rs index d6ee5e29..481ce0d2 100644 --- a/server/src/dbnet/mod.rs +++ b/server/src/dbnet/mod.rs @@ -53,7 +53,6 @@ use std::fs; use std::future::Future; use std::io::ErrorKind; use std::net::IpAddr; -use std::path::PathBuf; use std::process; use std::sync::Arc; use tls::SslListener; @@ -315,7 +314,7 @@ pub async fn run( bgsave_cfg: BGSave, snapshot_cfg: SnapshotConfig, sig: impl Future, - restore_filepath: Option, + restore_filepath: Option, ) -> (CoreDB, flock::FileLock) { let (signal, _) = broadcast::channel(1); let (terminate_tx, terminate_rx) = mpsc::channel(1); diff --git a/server/src/diskstore/mod.rs b/server/src/diskstore/mod.rs index 63d58fe7..c2e415ec 100644 --- a/server/src/diskstore/mod.rs +++ b/server/src/diskstore/mod.rs @@ -28,6 +28,7 @@ use crate::config::BGSave; use crate::coredb::{self, Data}; +use crate::diskstore::snapshot::{DIR_OLD_SNAPSHOT, DIR_SNAPSHOT}; use bincode; use bytes::Bytes; use libsky::TResult; @@ -53,17 +54,39 @@ lazy_static::lazy_static! { pub static ref OLD_PATH: PathBuf = PathBuf::from("./data.bin"); } +pub fn get_snapshot(path: Option) -> TResult>> { + if let Some(path) = path { + // the path just has the snapshot name, let's improve that + let mut snap_location = PathBuf::from(DIR_SNAPSHOT); + snap_location.push(path); + let file = match fs::read(snap_location) { + Ok(f) => f, + Err(e) => match e.kind() { + ErrorKind::NotFound => { + // Probably the old snapshot directory? + match fs::read(DIR_OLD_SNAPSHOT) { + Ok(f) => f, + _ => return Err(e.into()), + } + } + _ => return Err(e.into()), + }, + }; + let parsed = deserialize(file)?; + Ok(Some(parsed)) + } else { + Ok(None) + } +} + /// Try to get the saved data from disk. This returns `None`, if the `data/data.bin` wasn't found /// otherwise the `data/data.bin` file is deserialized and parsed into a `HashMap` -pub fn get_saved(location: Option) -> TResult>> { - let file = match fs::read( - location - .map(|loc| loc.to_path_buf()) - .unwrap_or(PERSIST_FILE.to_path_buf()), - ) { +pub fn get_saved() -> TResult>> { + let file = match fs::read(&*PERSIST_FILE) { Ok(f) => f, Err(e) => match e.kind() { ErrorKind::NotFound => { + // TODO(@ohsayan): Drop support for this in the future // This might be an old installation still not using the data/data.bin path match fs::read(OLD_PATH.to_path_buf()) { Ok(f) => { @@ -94,6 +117,11 @@ pub fn get_saved(location: Option) -> TResult return Err(format!("Couldn't read flushed data from disk: {}", e).into()), }, }; + let parsed = deserialize(file)?; + Ok(Some(parsed)) +} + +fn deserialize(file: Vec) -> TResult> { let parsed: DiskStoreFromDisk = bincode::deserialize(&file)?; let parsed: HashMap = HashMap::from_iter( parsed @@ -105,7 +133,7 @@ pub fn get_saved(location: Option) -> TResult Iter { self.queue.iter()