Skip to content

Commit

Permalink
save to different file and move on success to prevent silent failure …
Browse files Browse the repository at this point in the history
…when disk space is low
  • Loading branch information
hardliner66 committed Apr 28, 2021
1 parent d28721e commit 7585fc6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "timetracking"
description = "Simple time tracker with simple data format"
version = "1.5.20-alpha.0"
version = "1.5.20"
authors = ["hardliner66 <[email protected]>"]
edition = "2018"
license-file = "LICENSE"
Expand Down
34 changes: 25 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{Context, Result};
use chrono::{prelude::*, serde::ts_seconds, Duration, NaiveDate, NaiveDateTime, NaiveTime};
use iif::iif;
use serde::{Deserialize, Serialize};
use std::io;
use std::{fs::File, io::{self, Write}};
use std::path::{Path, PathBuf};
use structopt::StructOpt;

Expand Down Expand Up @@ -238,25 +238,41 @@ fn read_json_data<P: AsRef<Path>>(path: P) -> Result<Vec<TrackingEvent>> {
Ok(serde_json::from_str(&data)?)
}

fn write_with_flush<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
let mut f = File::create(path)?;
f.write_all(contents.as_ref())?;
f.flush()?;
Ok(())
}

#[cfg(feature = "binary")]
fn write_data<P: AsRef<Path>>(path: P, data: &[TrackingEvent]) {
fn write_data<P: AsRef<Path>>(path: P, data: &[TrackingEvent]) -> Result<()> {

let data = bincode::serialize(data).expect("could not serialize data");
std::fs::write(path, data).expect("could not write data file");

let temp_path = path.as_ref().with_extension("bin.bak");

match write_with_flush(&temp_path, &data) {
Ok(_) => {
Ok(std::fs::rename(temp_path, path.as_ref())?)
}
Err(e) => Err(e.into()),
}
}

fn write_json_data<P: AsRef<Path>>(path: P, data: &[TrackingEvent], pretty: bool) {
fn write_json_data<P: AsRef<Path>>(path: P, data: &[TrackingEvent], pretty: bool) -> Result<()> {
let data = iif!(
pretty,
serde_json::to_string_pretty(data),
serde_json::to_string(data)
)
.expect("could not serialize data");
std::fs::write(path, data).expect("could not write data file");
Ok(write_with_flush(&path, &data)?)
}

#[cfg(not(feature = "binary"))]
fn write_data<P: AsRef<Path>>(path: P, data: &[TrackingEvent]) {
write_json_data(path, data, false);
fn write_data<P: AsRef<Path>>(path: P, data: &[TrackingEvent]) -> Result<()> {
write_json_data(path, data, false)
}

fn start_tracking(
Expand Down Expand Up @@ -877,7 +893,7 @@ fn main() -> Result<()> {
if readable {
export_human_readable(expanded_path, &data);
} else {
write_json_data(expanded_path, &data, pretty);
write_json_data(expanded_path, &data, pretty).expect("Could not write file");
}
false
}
Expand All @@ -893,7 +909,7 @@ fn main() -> Result<()> {
if data_changed {
data.sort_by_key(|e| e.time(true));
data.dedup();
write_data(expanded_path, &data);
write_data(expanded_path, &data).expect("Could not write file!");
}

Ok(())
Expand Down

0 comments on commit 7585fc6

Please sign in to comment.