Skip to content

Commit

Permalink
change: Switch from flate to zstd
Browse files Browse the repository at this point in the history
  • Loading branch information
Nukesor committed Feb 22, 2025
1 parent c9257de commit 848a13f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 51 deletions.
83 changes: 47 additions & 36 deletions 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 pueue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ color-eyre.workspace = true
comfy-table = "7"
command-group.workspace = true
ctrlc = { version = "3", features = ["termination"] }
flate2 = "1"
handlebars.workspace = true
interim = { version = "0.2.0", features = ["chrono_0_4"] }
pest = "2.7"
Expand All @@ -58,6 +57,7 @@ tracing-subscriber = { version = "0.3.19", features = [
"fmt",
"local-time",
] }
zstd = "0.13"

[dev-dependencies]
assert_cmd = "2"
Expand Down
23 changes: 9 additions & 14 deletions pueue/src/daemon/internal_state/state.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::{
collections::BTreeMap,
fs::{File, read_to_string},
io::{Read, Write},
process::Child,
sync::{Arc, Mutex, MutexGuard},
};

use chrono::Local;
use flate2::Compression;
use pueue_lib::{
Group, GroupStatus, Settings, State, TaskResult,
error::Error,
Expand Down Expand Up @@ -258,14 +256,11 @@ impl InternalState {
temp = path.join("state.json.gz.partial");
real = path.join("state.json.gz");

let file = if temp.exists() {
File::open(&temp)?
} else {
File::create(&temp)?
};
let compressed = zstd::stream::encode_all(serialized.as_bytes(), 3)
.context("failed to compress state")?;

let mut encoder = flate2::write::GzEncoder::new(file, Compression::default());
encoder.write_all(serialized.as_bytes())?;
std::fs::write(&temp, compressed)
.context("Failed to write compressed state to temp file while saving state.")?;
} else {
// Write to temporary log file first, to prevent loss due to crashes.
std::fs::write(&temp, serialized)
Expand Down Expand Up @@ -303,12 +298,12 @@ impl InternalState {

// Try to load the file.
let data = if settings.daemon.compress_state_file {
let file = File::open(path)?;
let mut decoder = flate2::read::GzDecoder::new(file);
let mut data = String::new();
decoder.read_to_string(&mut data)?;
let f = File::open(&path).context("State restore: Failed to open file:\n\n{}")?;

Check warning on line 301 in pueue/src/daemon/internal_state/state.rs

View check run for this annotation

Codecov / codecov/patch

pueue/src/daemon/internal_state/state.rs#L301

Added line #L301 was not covered by tests

let bytes =
zstd::stream::decode_all(f).context("State restore: Failed to decompress file")?;

Check warning on line 304 in pueue/src/daemon/internal_state/state.rs

View check run for this annotation

Codecov / codecov/patch

pueue/src/daemon/internal_state/state.rs#L303-L304

Added lines #L303 - L304 were not covered by tests

data
String::from_utf8(bytes).context("State restore: Failed to parse state as UTF-8")?

Check warning on line 306 in pueue/src/daemon/internal_state/state.rs

View check run for this annotation

Codecov / codecov/patch

pueue/src/daemon/internal_state/state.rs#L306

Added line #L306 was not covered by tests
} else {
read_to_string(&path).context("State restore: Failed to read file:\n\n{}")?
};
Expand Down

0 comments on commit 848a13f

Please sign in to comment.