summaryrefslogtreecommitdiff
path: root/src/host/Host.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/host/Host.zig')
-rw-r--r--src/host/Host.zig24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/host/Host.zig b/src/host/Host.zig
index 75b49b3..3a2a25f 100644
--- a/src/host/Host.zig
+++ b/src/host/Host.zig
@@ -106,6 +106,8 @@ commands: std.ArrayList(abi.Command) = .empty,
pending: std.ArrayList(abi.Event) = .empty,
input: abi.Input = .{},
files: std.StringHashMapUnmanaged([]align(memory.page) const u8) = .empty,
+/// Mappings of files `writeFile` replaced: the sketch may still hold them.
+retired: std.ArrayList([]align(memory.page) const u8) = .empty,
/// The paths `appendFile` wrote, each once, 0-terminated: the sketch keeps
/// them.
written: std.StringHashMapUnmanaged(void) = .empty,
@@ -254,6 +256,8 @@ pub fn deinit(self: *Host) void {
std.posix.munmap(entry.value_ptr.*);
}
self.files.deinit(self.gpa);
+ for (self.retired.items) |bytes| std.posix.munmap(bytes);
+ self.retired.deinit(self.gpa);
var written = self.written.keyIterator();
while (written.next()) |path| self.gpa.free(path.ptr[0 .. path.len + 1]);
self.written.deinit(self.gpa);
@@ -1081,7 +1085,7 @@ fn apiReadFile(ctx: *anyopaque, path: []const u8) []const u8 {
self.files.put(self.gpa, key, bytes) catch self.gpa.free(key);
return bytes;
}
-fn apiAppendFile(ctx: *anyopaque, name_ptr: [*]const u8, name_len: usize, bytes: [*]const u8, len: usize) callconv(.c) [*:0]const u8 {
+fn apiAppendFile(ctx: *anyopaque, name_ptr: [*]const u8, name_len: usize, bytes: [*]const u8, len: usize, replace: bool) callconv(.c) [*:0]const u8 {
const self = hostOf(ctx);
const name = name_ptr[0..name_len];
if (name.len == 0 or std.mem.indexOfScalar(u8, name, '/') != null) {
@@ -1090,6 +1094,14 @@ fn apiAppendFile(ctx: *anyopaque, name_ptr: [*]const u8, name_len: usize, bytes:
}
var buf: [std.fs.max_path_bytes]u8 = undefined;
const path = std.mem.print(&buf, "{s}.{s}", .{ self.options.state, name }) catch return "";
+ if (replace) {
+ // A new file: a mapping of the old one stays valid.
+ std.Io.Dir.cwd().deleteFile(self.io, path) catch {};
+ if (self.files.fetchRemove(path)) |old| {
+ self.gpa.free(old.key);
+ self.retired.append(self.gpa, old.value) catch {};
+ }
+ }
const file = std.Io.Dir.cwd().createFile(self.io, path, .{ .truncate = false }) catch |err| {
self.print("appendFile: {s}: {t}", .{ path, err });
return "";
@@ -1319,6 +1331,7 @@ test "appendFile appends to a file beside the state and names it for readFile" {
host.gpa = std.testing.allocator;
host.io = io;
host.written = .empty;
+ host.files = .empty;
host.options = .{ .state = try std.mem.print(&buf, ".zig-cache/tmp/{s}/t.state", .{&tmp.sub_path}) };
defer {
var written = host.written.keyIterator();
@@ -1326,10 +1339,15 @@ test "appendFile appends to a file beside the state and names it for readFile" {
host.written.deinit(host.gpa);
}
const name = "take-1.f32";
- const path = std.mem.span(apiAppendFile(&host, name, name.len, "ab", 2));
+ const path = std.mem.span(apiAppendFile(&host, name, name.len, "ab", 2, false));
try std.testing.expect(std.mem.endsWith(u8, path, "/t.state.take-1.f32"));
- try std.testing.expectEqual(path.ptr, apiAppendFile(&host, name, name.len, "cd", 2));
+ try std.testing.expectEqual(path.ptr, apiAppendFile(&host, name, name.len, "cd", 2, false));
const bytes = try tmp.dir.readFileAlloc(io, "t.state.take-1.f32", host.gpa, .unlimited);
defer host.gpa.free(bytes);
try std.testing.expectEqualStrings("abcd", bytes);
+ // writeFile: a new file, its old bytes gone.
+ _ = apiAppendFile(&host, name, name.len, "e", 1, true);
+ const again = try tmp.dir.readFileAlloc(io, "t.state.take-1.f32", host.gpa, .unlimited);
+ defer host.gpa.free(again);
+ try std.testing.expectEqualStrings("e", again);
}