From da6b4a92201a409e75ce7cd1f2e3bb808a9f2db7 Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Tue, 26 Sep 2023 21:35:14 -0400 Subject: [PATCH] Fix dataset file getting cleared on startup --- src/datasets.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/datasets.rs b/src/datasets.rs index bd8950c..8100a4f 100644 --- a/src/datasets.rs +++ b/src/datasets.rs @@ -1,20 +1,25 @@ use crate::queries::*; use sqlite::*; -use std::fs::{self, File}; +use std::fs::{self, OpenOptions}; +use std::io; use std::path::{Path, PathBuf}; /// Return the path to a dataset. -pub fn dataset_path(config_dir: &Path, dataset: &str) -> PathBuf { +pub fn dataset_path(config_dir: &Path, dataset: &str) -> io::Result { // $config_dir/datasets/$dataset.sqlite let mut path = config_dir.to_owned(); path.push("datasets"); // Create datasets path if it doesn't exist - fs::create_dir_all(&path).unwrap(); + fs::create_dir_all(&path)?; path.push(dataset); path.set_extension("db"); - path + + // Create dataset file if it doesn't exist + OpenOptions::new().write(true).create(true).open(&path)?; + + Ok(path) } pub fn open_dataset(dataset: &Path) -> sqlite::Result { @@ -27,13 +32,6 @@ pub fn open_dataset(dataset: &Path) -> sqlite::Result { ) STRICT; "; - File::create(dataset).map_err(|e| Error { - code: { - println!("{:?}", e); - None - }, - message: Some("unable to open database file".to_owned()), - })?; let connection = sqlite::open(dataset)?; connection.execute(query)?; Ok(connection)