summaryrefslogtreecommitdiff
path: root/src/dump.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/dump.zig')
-rw-r--r--src/dump.zig31
1 files changed, 30 insertions, 1 deletions
diff --git a/src/dump.zig b/src/dump.zig
index 9c1dceec..bff8af21 100644
--- a/src/dump.zig
+++ b/src/dump.zig
@@ -1,7 +1,36 @@
const std = @import("std");
const builtin = @import("builtin");
-pub const default_path = "/tmp/pardes-dump.zon";
+/// Where the next dump goes. $PARDES_DUMP wins verbatim (the snapshot harness
+/// pins it for deterministic goldens); else $XDG_DATA_HOME|~/.local/share
+/// /pardes/pardes-<utc>.zon — timestamped so dumps never overwrite each other.
+/// Creates the pardes dir (parents assumed; a failure surfaces at open).
+pub fn outPath(buf: *[1024:0]u8) ?[:0]const u8 {
+ if (std.c.getenv("PARDES_DUMP")) |p|
+ return std.fmt.bufPrintSentinel(buf, "{s}", .{std.mem.span(p)}, 0) catch null;
+ var dir_buf: [900]u8 = undefined;
+ const dir = if (std.c.getenv("XDG_DATA_HOME")) |x|
+ std.fmt.bufPrint(&dir_buf, "{s}/pardes", .{std.mem.span(x)}) catch return null
+ else if (std.c.getenv("HOME")) |h|
+ std.fmt.bufPrint(&dir_buf, "{s}/.local/share/pardes", .{std.mem.span(h)}) catch return null
+ else
+ return null;
+ var dz: [901:0]u8 = undefined;
+ @memcpy(dz[0..dir.len], dir);
+ dz[dir.len] = 0;
+ _ = std.c.mkdir(dz[0..dir.len :0], 0o755); // EEXIST is fine
+ var ts: std.c.timespec = undefined;
+ _ = std.c.clock_gettime(.REALTIME, &ts);
+ const es: std.time.epoch.EpochSeconds = .{ .secs = @intCast(@max(0, ts.sec)) };
+ const yd = es.getEpochDay().calculateYearDay();
+ const md = yd.calculateMonthDay();
+ const ds = es.getDaySeconds();
+ return std.fmt.bufPrintSentinel(buf, "{s}/pardes-{d:0>4}{d:0>2}{d:0>2}-{d:0>2}{d:0>2}{d:0>2}.zon", .{
+ dir, yd.year, md.month.numeric(),
+ @as(u8, md.day_index) + 1, ds.getHoursIntoDay(), ds.getMinutesIntoHour(),
+ ds.getSecondsIntoMinute(),
+ }, 0) catch null;
+}
pub const magic = "pardes-dump";
pub const version: u32 = 1;
pub const max_panes: usize = 16;