Skip to content

Commit

Permalink
feat(config): use atomic writes for safer configuration writing
Browse files Browse the repository at this point in the history
- Replaced `fs::write` with `atomicwrites` for atomic file writes, ensuring files are either fully written or not written at all.
- Improved error handling and directory creation logic in `src/config.rs`.
- Bumped package version to `1.1.0`.
- Added `atomicwrites` as a new dependency in `Cargo.toml`.
- Updated Nix build configuration (`default.nix` and `flake.lock`) to reflect new dependencies.

These changes improve the reliability of file operations, preventing partial writes or data corruption in case of failures.

This aligns with what is used by libcosmic applications to write
configurations
  • Loading branch information
HeitorAugustoLN committed Dec 29, 2024
1 parent 75eda93 commit 12e48f4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "cosmic-ctl"
description = "CLI for COSMIC Desktop configuration management"
version = "1.0.0"
version = "1.1.0"
authors = ["Heitor Augusto <IAm.HeitorALN@proton.me>"]
homepage = "https://github.com/cosmic-utils/cosmic-ctl"
repository = "https://github.com/cosmic-utils/cosmic-ctl"
Expand All @@ -13,6 +13,7 @@ documentation = "https://github.com/cosmic-utils/cosmic-ctl/wiki"
readme = "README.md"

[dependencies]
atomicwrites = { git = "https://github.com/jackpot51/rust-atomicwrites" }
bracoxide = "0.1.4"
clap = { version = "4.5.21", features = ["derive"] }
etcetera = "0.8.0"
Expand Down
5 changes: 3 additions & 2 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pkgs.callPackage (
cosmic-comp,
}:
let
version = "1.0.0";
version = "1.1.0";
in
rustPlatform.buildRustPackage {
pname = "cosmic-ctl";
Expand All @@ -21,7 +21,8 @@ pkgs.callPackage (
path = ./.;
};

cargoHash = "sha256-Nrg7NOAUrVQcwBz7nV3hILRYHr1dprQ5VJj2u7Zf3Q0=";
useFetchCargoVendor = true;
cargoHash = "sha256-EReo2hkBaIO1YOBx4D9rQSXlx+3NK5VQtj59jfZZI/0=";

doInstallCheck = true;
nativeInstallCheckInputs = [ versionCheckHook ];
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

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

32 changes: 19 additions & 13 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use atomicwrites::{AtomicFile, OverwriteBehavior};
use etcetera::{
base_strategy::{BaseStrategy, Xdg},
choose_base_strategy,
};
use std::{
fs,
io::{Error, ErrorKind},
io::{Error, ErrorKind, Write},
path::{Path, PathBuf},
};
use unescaper::unescape;
Expand Down Expand Up @@ -60,18 +61,23 @@ pub fn write_configuration(
}
}

fs::create_dir_all(path.parent().unwrap_or_else(|| Path::new(""))).map_err(|e| {
Error::new(
ErrorKind::Other,
format!("Failed to create directory structure: {}", e),
)
})?;
fs::write(&path, unescaped_value).map_err(|e| {
Error::new(
ErrorKind::Other,
format!("Failed to write configuration to {}: {}", path.display(), e),
)
})?;
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).map_err(|e| {
Error::new(
ErrorKind::Other,
format!("Failed to create directory structure: {}", e),
)
})?;
}

let af = AtomicFile::new(&path, OverwriteBehavior::AllowOverwrite);
af.write(|f| f.write_all(unescaped_value.as_bytes()))
.map_err(|e| {
Error::new(
ErrorKind::Other,
format!("Failed to write configuration to {}: {}", path.display(), e),
)
})?;

Ok(true)
}
Expand Down

0 comments on commit 12e48f4

Please sign in to comment.