Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to handle empty file edgecase #27

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub enum Error {
#[error("can not remove entry from db: {0}")]
RemoveDbEntry(db::Error),

#[error("can not add to storeo: {0}")]
#[error("can not add to store: {0}")]
AddStore(crate::store::Error),

#[error("db error: {0}")]
Expand Down
20 changes: 10 additions & 10 deletions src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
#[error("can not create log folder: {0}")]
CreateLogFolder(PathBuf, std::io::Error),
#[error("can not create index folder at path {0:?}: {1}")]
CreateIndexFolder(PathBuf, std::io::Error),

#[error("can not open log file: {0}")]
OpenLogFile(PathBuf, std::io::Error),
#[error("can not open index file at path {0:?}: {1}")]
OpenIndexFile(PathBuf, std::io::Error),

#[error("can not serialize entry: {0}")]
SerializeEntry(csv::Error),
Expand All @@ -28,8 +28,8 @@ pub enum Error {
#[error("problem while iterating glob: {0}")]
GlobIteration(glob::GlobError),

#[error("can not read log file {0:?}: {1}")]
ReadLogFile(PathBuf, csv::Error),
#[error("can not read index file {0:?}: {1}")]
ReadIndexFile(PathBuf, csv::Error),

#[error("{0}")]
Filter(#[from] filter::Error),
Expand All @@ -55,7 +55,7 @@ impl Store {
let file_path = folder_path.join(format!("{}.csv", hostname));

fs::create_dir_all(&folder_path)
.map_err(|err| Error::CreateLogFolder(folder_path.to_path_buf(), err))?;
.map_err(|err| Error::CreateIndexFolder(folder_path.to_path_buf(), err))?;

let mut builder = csv::WriterBuilder::new();

Expand All @@ -68,7 +68,7 @@ impl Store {
.append(true)
.create(true)
.open(&file_path)
.map_err(|err| Error::OpenLogFile(file_path.clone(), err))?;
.map_err(|err| Error::OpenIndexFile(file_path.clone(), err))?;

let mut writer = builder.from_writer(index_file);

Expand Down Expand Up @@ -119,12 +119,12 @@ impl Store {

fn read_log_file<P: AsRef<Path>>(file_path: P) -> Result<Vec<Entry>, Error> {
let file = std::fs::File::open(&file_path)
.map_err(|err| Error::OpenLogFile(file_path.as_ref().to_path_buf(), err))?;
.map_err(|err| Error::OpenIndexFile(file_path.as_ref().to_path_buf(), err))?;

let reader = std::io::BufReader::new(file);

Self::read_metadata(reader)
.map_err(|err| Error::ReadLogFile(file_path.as_ref().to_path_buf(), err))
.map_err(|err| Error::ReadIndexFile(file_path.as_ref().to_path_buf(), err))
}

fn read_metadata<R: std::io::Read>(reader: R) -> Result<Vec<Entry>, csv::Error> {
Expand Down
18 changes: 18 additions & 0 deletions tests/client_server_intigration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,21 @@ fn write_newline_command() {

assert_eq!(entries.len(), 0);
}

#[test]
fn existing_empty_file() {
let hostname = "testhostname".to_string();
let data_dir = tempfile::tempdir().unwrap().into_path();
std::fs::File::create(data_dir.join(format!("{}.csv", hostname))).unwrap();

dbg!(&data_dir);

let store = store::new(data_dir.clone());
let entries = store.get_entries(&Filter::default()).unwrap();

dbg!(&entries);

assert_eq!(entries.len(), 0);

std::fs::remove_dir_all(&data_dir).unwrap();
}