Skip to main content

agentmux_srv/backend/agent_session/
helpers.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Small shared helpers: monotonic-ish timestamp + FileStore I/O wrappers.
5
6
7use crate::backend::storage::filestore::{FileMeta, FileOpts, FileStore};
8
9pub(crate) use agentmux_common::time::now_ms_u64 as now_ms;
10
11/// Ensure a file exists in `zone`. No-op when present.
12pub(crate) fn ensure_file(filestore: &FileStore, zone: &str, name: &str) -> Result<(), String> {
13    match filestore.stat(zone, name) {
14        Ok(Some(_)) => Ok(()),
15        Ok(None) => filestore
16            .make_file(zone, name, FileMeta::default(), FileOpts::default())
17            .map_err(|e| format!("make_file: {e}")),
18        Err(e) => Err(format!("stat: {e}")),
19    }
20}
21
22/// Write the entire contents of a file in `zone`. Creates the file if
23/// missing, otherwise replaces all parts atomically (FileStore single-tx).
24pub(crate) fn write_zone_file(
25    filestore: &FileStore,
26    zone: &str,
27    name: &str,
28    content: &[u8],
29) -> Result<(), String> {
30    use crate::backend::storage::StoreError;
31    match filestore.write_file(zone, name, content) {
32        Ok(()) => Ok(()),
33        Err(StoreError::NotFound) => {
34            filestore
35                .make_file(zone, name, FileMeta::default(), FileOpts::default())
36                .map_err(|e| format!("make_file: {e}"))?;
37            filestore
38                .write_file(zone, name, content)
39                .map_err(|e| format!("write_file: {e}"))
40        }
41        Err(e) => Err(format!("write_file: {e}")),
42    }
43}