From e925895eabe9fa5353131eb84ee8d8d69236e451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asger=20Juul=20Brunsh=C3=B8j?= Date: Wed, 22 Jan 2025 00:14:22 +0100 Subject: [PATCH] create home --- crates/ascend/src/server.rs | 4 +-- crates/ascend/src/server/persistence.rs | 36 ++++++++++++++----------- flake.nix | 1 + 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/crates/ascend/src/server.rs b/crates/ascend/src/server.rs index d36acd3..5e8958e 100644 --- a/crates/ascend/src/server.rs +++ b/crates/ascend/src/server.rs @@ -103,8 +103,8 @@ pub async fn main() { } Command::ResetState => { let s = PersistentState::default(); - let p = camino::Utf8Path::new(STATE_FILE); - tracing::info!("Resetting state to default: {p}"); + let p = Path::new(STATE_FILE); + tracing::info!("Resetting state to default: {}", p.display()); Persistent::persist(p, &s).await.unwrap_or_report(); } } diff --git a/crates/ascend/src/server/persistence.rs b/crates/ascend/src/server/persistence.rs index eb1173d..f6869a7 100644 --- a/crates/ascend/src/server/persistence.rs +++ b/crates/ascend/src/server/persistence.rs @@ -1,19 +1,20 @@ -use camino::Utf8Path; use camino::Utf8PathBuf; use serde::Serialize; use serde::de::DeserializeOwned; +use std::path::Path; +use std::path::PathBuf; use std::sync::Arc; use tokio::sync::Mutex; #[derive(Debug, Clone)] pub struct Persistent { state: Arc>, - file_path: Utf8PathBuf, + file_path: PathBuf, } impl Persistent { #[tracing::instrument(skip(state))] - pub fn new(state: T, file_path: Utf8PathBuf) -> Self { + pub fn new(state: T, file_path: PathBuf) -> Self { Self { state: Arc::new(Mutex::new(state)), file_path, @@ -26,13 +27,18 @@ impl Persistent { where T: DeserializeOwned, { + let file_path = file_path.canonicalize().map_err(|source| Error::BadPath { + path: file_path.clone(), + source, + })?; + let content = tokio::fs::read_to_string(&file_path).await.map_err(|source| Error::Read { - file_path: file_path.to_owned(), + file_path: file_path.clone(), source, })?; let t = ron::from_str(&content).map_err(|source| Error::Deserialize { - file_path: file_path.to_owned(), + file_path: file_path.clone(), source, })?; @@ -93,7 +99,7 @@ impl Persistent { /// /// Implicitly called by `set` and `update`. #[tracing::instrument(skip_all, err)] - pub async fn persist(file_path: &Utf8Path, state: &T) -> Result<(), Error> + pub async fn persist(file_path: &Path, state: &T) -> Result<(), Error> where T: Serialize, { @@ -110,18 +116,18 @@ impl Persistent { #[derive(Debug, derive_more::Error, derive_more::Display)] #[display("Persistent state error: {_variant}")] pub enum Error { - #[display("Failed to read file: {file_path}")] - Read { file_path: Utf8PathBuf, source: std::io::Error }, + #[display("Failed to operate on path: {path}")] + BadPath { path: Utf8PathBuf, source: std::io::Error }, - #[display("Failed to deserialize state from file: {file_path}")] - Deserialize { - file_path: Utf8PathBuf, - source: ron::error::SpannedError, - }, + #[display("Failed to read file: {}", file_path.display())] + Read { file_path: PathBuf, source: std::io::Error }, + + #[display("Failed to deserialize state from file: {}", file_path.display())] + Deserialize { file_path: PathBuf, source: ron::error::SpannedError }, #[display("Failed to serialize state")] Serialize { source: ron::Error }, - #[display("Failed to write file: {file_path}")] - Write { file_path: Utf8PathBuf, source: std::io::Error }, + #[display("Failed to write file: {}", file_path.display())] + Write { file_path: PathBuf, source: std::io::Error }, } diff --git a/flake.nix b/flake.nix index 43e7d28..6f0b889 100644 --- a/flake.nix +++ b/flake.nix @@ -96,6 +96,7 @@ home = "/home/ascend"; group = "ascend"; description = "Ascend service user"; + createHome = true; }; users.groups.ascend = { }; systemd.services.ascend = {