diff options
| author | Gabriel Schneider <[email protected]> | 2026-09-29 12:39:57 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-01 00:12:16 -0300 |
| commit | 7472ae9a2f62f332b1b3f81772c54957adcafab8 (patch) | |
| tree | 37d576f6b8070b3050078e499101dfc222fa164a /src/dump.zig | |
| parent | 24426b141ebc0db5ee67eb238eeec0f81f6e6ef7 (diff) | |
| download | pardes-7472ae9a2f62f332b1b3f81772c54957adcafab8.tar.gz pardes-7472ae9a2f62f332b1b3f81772c54957adcafab8.zip | |
DumpDir reads back absolute and without a trailing slash
A relative DumpDir read back as written, relative, and a trailing / stayed. It now resolves against the directory pardes started in, as a Dump already did, and is shown cleaned.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/dump.zig')
| -rw-r--r-- | src/dump.zig | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/dump.zig b/src/dump.zig index 0e911ec1..45a5e0eb 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -64,9 +64,28 @@ fn hostEnv(name: [*:0]const u8) ?[*:0]u8 { /// setting when set (a leading `~/` is the home directory), else the default. pub fn directory(buf: []u8, dir_setting: []const u8) ?[]const u8 { if (dir_setting.len == 0) return defaultDirectory(buf); - if (std.mem.startsWith(u8, dir_setting, "~/")) if (hostEnv("HOME")) |h| - return std.fmt.bufPrint(buf, "{s}/{s}", .{ std.mem.span(h), dir_setting[2..] }) catch null; - return std.fmt.bufPrint(buf, "{s}", .{dir_setting}) catch null; + var joined: [2048]u8 = undefined; + // `~/x`, else a relative one from the directory pardes started in: the + // same place however it was written, and shown so. + const raw = if (std.mem.startsWith(u8, dir_setting, "~/")) (if (hostEnv("HOME")) |h| + std.fmt.bufPrint(&joined, "{s}/{s}", .{ std.mem.span(h), dir_setting[2..] }) catch return null + else + dir_setting) else dir_setting; + var fba_buf: [4096]u8 = undefined; + var fba = std.heap.FixedBufferAllocator.init(&fba_buf); + var cwd_buf: [1024]u8 = undefined; + // No libc on the web or the board: there a relative one is from `/`. + const cwd: []const u8 = if (comptime !pardes.hosted) "/" else if (std.fs.path.isAbsolute(raw)) "/" else if (std.c.getcwd(&cwd_buf, cwd_buf.len)) |c| std.mem.span(@as([*:0]const u8, @ptrCast(c))) else "/"; + const resolved = std.fs.path.resolvePosix(fba.allocator(), &.{ cwd, raw }) catch return null; + return std.fmt.bufPrint(buf, "{s}", .{resolved}) catch null; +} + +test "a DumpDir reads back absolute, however it was written" { + var buf: [4096]u8 = undefined; + try std.testing.expectEqualStrings("/tmp/dumps", directory(&buf, "/tmp/dumps/").?); + try std.testing.expectEqualStrings("/tmp/dumps", directory(&buf, "/tmp//x/../dumps").?); + const rel = directory(&buf, "rel/"); + try std.testing.expect(std.fs.path.isAbsolute(rel.?) and std.mem.endsWith(u8, rel.?, "/rel")); } /// The standard dump directory, without creating it or generating a filename. |
