Change the data file path to data/data.bin

As a consequence, other methods were also upgraded
next
Sayan Nandan 3 years ago
parent b0bcc9bfa7
commit f60b3098da

1
.gitignore vendored

@ -1,6 +1,7 @@
/target
/.vscode
*.bin
data
/server/snapshots
snapstore.bin
snapstore.partmap

@ -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() {

@ -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 } => {

@ -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<Self> {
pub fn lock(filename: impl Into<PathBuf>) -> Result<Self> {
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();
}

@ -49,11 +49,12 @@ type DiskStoreFromDisk = (Vec<String>, Vec<Vec<u8>>);
/// 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<PathBuf>) -> TResult<Option<HashMap<String, Data>>> {
let file = match fs::read(
location
@ -62,7 +63,34 @@ pub fn get_saved(location: Option<PathBuf>) -> TResult<Option<HashMap<String, Da
) {
Ok(f) => 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<PathBuf>) -> TResult<Option<HashMap<String, Da
/// Flush the in-memory table onto disk
///
/// This functions takes the entire in-memory table and writes it to the disk,
/// more specifically, the `data.bin` file
/// more specifically, the `data/data.bin` file
pub fn flush_data(file: &mut flock::FileLock, data: &HashMap<String, Data>) -> TResult<()> {
let encoded = serialize(&data)?;
file.write(&encoded)?;

Loading…
Cancel
Save