From f60b3098da762a514f7af9e844d888e7993dd7eb Mon Sep 17 00:00:00 2001 From: Sayan Nandan Date: Thu, 6 May 2021 19:48:47 +0530 Subject: [PATCH] Change the data file path to data/data.bin As a consequence, other methods were also upgraded --- .gitignore | 1 + server/src/coredb/mod.rs | 2 +- server/src/dbnet/mod.rs | 24 +++++++++++++++------- server/src/diskstore/flock.rs | 7 ++++--- server/src/diskstore/mod.rs | 38 ++++++++++++++++++++++++++++++----- 5 files changed, 56 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index afe79602..8d1b3a57 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ /target /.vscode *.bin +data /server/snapshots snapstore.bin snapstore.partmap diff --git a/server/src/coredb/mod.rs b/server/src/coredb/mod.rs index 76f1fe4f..2a182c19 100644 --- a/server/src/coredb/mod.rs +++ b/server/src/coredb/mod.rs @@ -322,7 +322,7 @@ impl CoreDB { db.clone(), snapshot_cfg, )); - let lock = flock::FileLock::lock("data.bin") + let lock = flock::FileLock::lock(&*PERSIST_FILE) .map_err(|e| format!("Failed to acquire lock on data file with error '{}'", e))?; let cloned_descriptor = lock.try_clone()?; if bgsave.is_disabled() { diff --git a/server/src/dbnet/mod.rs b/server/src/dbnet/mod.rs index 598142fb..f7274f65 100644 --- a/server/src/dbnet/mod.rs +++ b/server/src/dbnet/mod.rs @@ -319,15 +319,17 @@ pub async fn run( ) -> (CoreDB, flock::FileLock) { let (signal, _) = broadcast::channel(1); let (terminate_tx, terminate_rx) = mpsc::channel(1); - let (db, lock, cloned_descriptor) = - match CoreDB::new(bgsave_cfg, snapshot_cfg, restore_filepath) { - Ok((db, lock, cloned_descriptor)) => (db, lock, cloned_descriptor), - Err(e) => { - log::error!("ERROR: {}", e); + match fs::create_dir_all(&*DIR_REMOTE_SNAPSHOT) { + Ok(_) => (), + Err(e) => match e.kind() { + ErrorKind::AlreadyExists => (), + _ => { + log::error!("Failed to create snapshot directories: '{}'", e); process::exit(0x100); } - }; - match fs::create_dir_all(&*DIR_REMOTE_SNAPSHOT) { + }, + } + match fs::create_dir("./data") { Ok(_) => (), Err(e) => match e.kind() { ErrorKind::AlreadyExists => (), @@ -337,6 +339,14 @@ pub async fn run( } }, } + let (db, lock, cloned_descriptor) = + match CoreDB::new(bgsave_cfg, snapshot_cfg, restore_filepath) { + Ok((db, lock, cloned_descriptor)) => (db, lock, cloned_descriptor), + Err(e) => { + log::error!("ERROR: {}", e); + process::exit(0x100); + } + }; let climit = Arc::new(Semaphore::new(50000)); let mut server = match ports { PortConfig::InsecureOnly { host, port } => { diff --git a/server/src/diskstore/flock.rs b/server/src/diskstore/flock.rs index 9f6a3139..551697a3 100644 --- a/server/src/diskstore/flock.rs +++ b/server/src/diskstore/flock.rs @@ -35,6 +35,7 @@ use std::fs::File; use std::fs::OpenOptions; use std::io::Result; use std::io::Write; +use std::path::PathBuf; #[derive(Debug)] /// # File Lock @@ -63,12 +64,12 @@ impl FileLock { /// /// This function will create and lock a file if it doesn't exist or it /// will create and lock a new file - pub fn lock(filename: &str) -> Result { + pub fn lock(filename: impl Into) -> Result { let file = OpenOptions::new() .create(true) .read(false) .write(true) - .open(filename)?; + .open(filename.into())?; Self::_lock(&file)?; Ok(Self { file, @@ -128,7 +129,7 @@ mod tests { use super::*; #[test] fn test_basic_file_lock() { - let mut file = FileLock::lock("data.bin").unwrap(); + let mut file = FileLock::lock("datalock.bin").unwrap(); file.write(&[1, 2, 3]).unwrap(); file.unlock().unwrap(); } diff --git a/server/src/diskstore/mod.rs b/server/src/diskstore/mod.rs index 47ceb1c2..63d58fe7 100644 --- a/server/src/diskstore/mod.rs +++ b/server/src/diskstore/mod.rs @@ -49,11 +49,12 @@ type DiskStoreFromDisk = (Vec, Vec>); /// onto disk type DiskStoreFromMemory<'a> = (Vec<&'a String>, Vec<&'a [u8]>); lazy_static::lazy_static! { - pub static ref PERSIST_FILE: PathBuf = PathBuf::from("./data.bin"); + pub static ref PERSIST_FILE: PathBuf = PathBuf::from("./data/data.bin"); + pub static ref OLD_PATH: PathBuf = PathBuf::from("./data.bin"); } -/// Try to get the saved data from disk. This returns `None`, if the `data.bin` wasn't found -/// otherwise the `data.bin` file is deserialized and parsed into a `HashMap` +/// 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 @@ -62,7 +63,34 @@ pub fn get_saved(location: Option) -> TResult f, Err(e) => match e.kind() { - ErrorKind::NotFound => return Ok(None), + ErrorKind::NotFound => { + // This might be an old installation still not using the data/data.bin path + match fs::read(OLD_PATH.to_path_buf()) { + Ok(f) => { + log::warn!("Your data file was found to be in the current directory and not in data/data.bin"); + if let Err(e) = fs::rename("data.bin", "data/data.bin") { + log::error!("Failed to move data.bin into data/data.bin directory. Consider moving it manually"); + return Err(format!( + "Failed to move data.bin into data/data.bin: {}", + e + ) + .into()); + } else { + log::info!("The data file has been moved into the new directory"); + log::warn!("This backwards compat directory support will be removed in the future"); + } + f + } + Err(e) => match e.kind() { + ErrorKind::NotFound => return Ok(None), + _ => { + return Err( + format!("Coudln't read flushed data from disk: {}", e).into() + ) + } + }, + } + } _ => return Err(format!("Couldn't read flushed data from disk: {}", e).into()), }, }; @@ -83,7 +111,7 @@ pub fn get_saved(location: Option) -> TResult) -> TResult<()> { let encoded = serialize(&data)?; file.write(&encoded)?;