Fix new instance detection

next
Sayan Nandan 3 years ago
parent 0e7340c965
commit 0b17f7b0e4
No known key found for this signature in database
GPG Key ID: 8BC07A0A4D41DD52

@ -21,6 +21,7 @@ All changes in this project will be noted in this file.
- (skysh) Fixed handling of EOF keystroke
- (skyd) Respect logging level for all terminal output
- (skyd) Fixed LF check in protocol impl
- (skyd) Fixed new instance detection (now checks if data directory is empty or not)
## Version 0.7.4

@ -45,7 +45,7 @@ use crate::{
util::Wrapper,
};
use core::mem::transmute;
use std::{fs, path::Path, sync::Arc};
use std::{fs, io::ErrorKind, path::Path, sync::Arc};
type PreloadSet = std::collections::HashSet<ObjectID>;
const PRELOAD_PATH: &str = "data/ks/PRELOAD";
@ -217,7 +217,7 @@ pub fn read_preload() -> StorageEngineResult<PreloadSet> {
/// is also created. If this is an already initialized instance then the store
/// is read and returned (and any possible errors that are encountered are returned)
pub fn read_full() -> StorageEngineResult<Memstore> {
if is_new_instance() {
if is_new_instance()? {
log::trace!("Detected new instance. Creating data directory");
/*
Since the `PRELOAD` file doesn't exist -- this is a new instance
@ -251,8 +251,14 @@ pub fn read_full() -> StorageEngineResult<Memstore> {
Ok(Memstore::init_with_all(ksmap, system_keyspace))
}
/// Check if the data/ks/PRELOAD file exists (if not: we're on a new instance)
pub fn is_new_instance() -> bool {
let path = Path::new("data/ks/PRELOAD");
!(path.exists() && path.is_file())
/// Check if the `data` directory is non-empty (if not: we're on a new instance)
pub fn is_new_instance() -> StorageEngineResult<bool> {
match fs::read_dir("data") {
Ok(mut dir) => Ok(dir.next().is_none()),
Err(e) if e.kind().eq(&ErrorKind::NotFound) => Ok(true),
Err(e) => Err(StorageEngineError::ioerror_extra(
e,
"while checking data directory",
)),
}
}

Loading…
Cancel
Save