From fe66ef6a44a38465c12e6a57c6d40af4d33ca7d9 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 5 Nov 2026 00:12:27 -0300 Subject: fs: readRestore reads the file restorePath names, where readRestoreIn decided again, its own way, whether a relative name is the DumpDir's; a DumpDir entry that is a directory is passed over, as restorePath already did, not read and refused; the Restore lookup test runs through readRestore, every case kept Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/fs.zig | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/fs.zig b/src/fs.zig index d8633593..207f1018 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -1126,14 +1126,10 @@ pub fn readFile(gpa: std.mem.Allocator, path: []const u8) ![]u8 { return readFileLimit(gpa, path, limits.max_file_bytes); } -/// Restore first tries a relative argument under the default dump directory, -/// then preserves the ordinary file lookup. Absolute paths remain verbatim. +/// The file a Restore of `typed` reads (restorePath), whole. pub fn readRestore(gpa: std.mem.Allocator, typed: []const u8, dir_setting: []const u8) ![]u8 { - var home_buf: [4096]u8 = undefined; - const path = expandHome(typed, &home_buf); - var dir_buf: [4096]u8 = undefined; - const dir = @import("dump.zig").directory(&dir_buf, dir_setting); - return readRestoreIn(gpa, path, dir); + var buf: [4096]u8 = undefined; + return readFile(gpa, restorePath(typed, dir_setting, &buf)); } /// The file a Restore of `typed` reads, whole: `~` expanded, a relative name @@ -1158,21 +1154,6 @@ pub fn restorePath(typed: []const u8, dir_setting: []const u8, out: *[4096]u8) [ return out[0..n]; } -fn readRestoreIn(gpa: std.mem.Allocator, path: []const u8, dir: ?[]const u8) ![]u8 { - if (!std.fs.path.isAbsolute(path)) { - if (dir) |base| { - var buf: [4096]u8 = undefined; - if (std.fmt.bufPrint(&buf, "{s}/{s}", .{ base, path })) |candidate| { - if (readFile(gpa, candidate)) |bytes| return bytes else |err| switch (err) { - error.FileNotFound => {}, - else => return err, - } - } else |_| {} - } - } - return readFile(gpa, path); -} - test "Restore prefers default directory then falls back to original path" { if (!platform_has_fs) return error.SkipZigTest; const gpa = std.testing.allocator; @@ -1186,25 +1167,25 @@ test "Restore prefers default directory then falls back to original path" { defer gpa.free(nested); try tmp.dir.createDirPath(std.testing.io, nested); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "dumps/a.dump", .data = "short name" }); - const short = try readRestoreIn(gpa, "a.dump", base); + const short = try readRestore(gpa, "a.dump", base); defer gpa.free(short); try std.testing.expectEqualStrings("short name", short); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.dump", .data = "original" }); - const fallback = try readRestoreIn(gpa, relative, base); + const fallback = try readRestore(gpa, relative, base); defer gpa.free(fallback); try std.testing.expectEqualStrings("original", fallback); const candidate = try std.fmt.allocPrint(gpa, "{s}/{s}", .{ base, relative }); defer gpa.free(candidate); try writeFile(candidate, "default"); - const preferred = try readRestoreIn(gpa, relative, base); + const preferred = try readRestore(gpa, relative, base); defer gpa.free(preferred); try std.testing.expectEqualStrings("default", preferred); const absolute = try tmp.dir.realPathFileAlloc(std.testing.io, "a.dump", gpa); defer gpa.free(absolute); - const direct = try readRestoreIn(gpa, absolute, base); + const direct = try readRestore(gpa, absolute, base); defer gpa.free(direct); try std.testing.expectEqualStrings("original", direct); - try std.testing.expectError(error.FileNotFound, readRestoreIn(gpa, "missing-restore-test.dump", base)); + try std.testing.expectError(error.FileNotFound, readRestore(gpa, "missing-restore-test.dump", base)); } fn readFileLimit(gpa: std.mem.Allocator, path: []const u8, limit: usize) ![]u8 { -- cgit v1.3