diff options
Diffstat (limited to 'src/host/root.zig')
| -rw-r--r-- | src/host/root.zig | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/src/host/root.zig b/src/host/root.zig index 5889b42..c64af92 100644 --- a/src/host/root.zig +++ b/src/host/root.zig @@ -96,21 +96,27 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { // A scripted run from fresh state works on files of its own, moved to // the given path at exit: two runs of one build step (two `zig build`s // at once share its output path) never share a state or store file, - // nor fail on the store's lock (its sessions are in memory). An export's are - // removed instead: nothing reads them, and a player may have the given - // ones open. + // nor the files the sketch appends to, nor fail on the store's lock + // (its sessions are in memory). An export's are removed instead: + // nothing reads them, and a player may have the given ones open. var private_buf: [std.fs.max_path_bytes]u8 = undefined; if (options.state.len == 0 and (script_path != null or options.export_to != null)) options.state = "sketch.state"; const given = options.state; const private = (script_path != null or options.export_to != null) and options.fresh; if (!private) sketchbookDir(init.io, given); if (private) options.state = std.mem.print(&private_buf, "{s}.{d}", .{ given, std.os.linux.getpid() }) catch fatal("--state path too long"); - defer if (private) for ([_][]const u8{ "", ".store" }) |suffix| { - var from: [std.fs.max_path_bytes + 16]u8 = undefined; - var to: [std.fs.max_path_bytes + 16]u8 = undefined; - const a = std.mem.printSentinel(&from, "{s}{s}", .{ options.state, suffix }, 0) catch continue; - const b = std.mem.printSentinel(&to, "{s}{s}", .{ given, suffix }, 0) catch continue; - _ = if (options.export_to != null) std.os.linux.unlink(a.ptr) else std.os.linux.rename(a.ptr, b.ptr); + // Its files are `<private>` and `<private>.<name>`, beside the given one. + defer if (private) moved: { + const dir = std.Io.Dir.cwd().openDir(init.io, std.fs.path.dirname(given) orelse ".", .{ .iterate = true }) catch break :moved; + defer dir.close(init.io); + const from = std.fs.path.basename(options.state); + var it = dir.iterate(); + while (it.next(init.io) catch null) |entry| { + if (!std.mem.startsWith(u8, entry.name, from) or (entry.name.len > from.len and entry.name[from.len] != '.')) continue; + var to: [std.fs.max_path_bytes]u8 = undefined; + const name = std.mem.print(&to, "{s}{s}", .{ std.fs.path.basename(given), entry.name[from.len..] }) catch continue; + if (options.export_to != null) dir.deleteFile(init.io, entry.name) catch {} else dir.rename(entry.name, dir, name, init.io) catch {}; + } }; sdl_heap.install(); |
