fix(project): don't abort early on project restore_state

And add traces for project write_state/restore_state.
This commit is contained in:
CJ van den Berg 2025-01-26 15:32:01 +01:00
parent 7ecdc38fdf
commit a1df51a16c
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 26 additions and 10 deletions

View file

@ -80,11 +80,13 @@ pub fn write_state(self: *Self, writer: anytype) !void {
} }
pub fn write_state_v1(self: *Self, writer: anytype) !void { pub fn write_state_v1(self: *Self, writer: anytype) !void {
tp.trace(tp.channel.event, .{"write_state_v1"});
try cbor.writeValue(writer, self.name); try cbor.writeValue(writer, self.name);
var visited: usize = 0; var visited: usize = 0;
for (self.files.items) |file| { for (self.files.items) |file| {
if (file.visited) visited += 1; if (file.visited) visited += 1;
} }
tp.trace(tp.channel.event, .{ "write_state_v1", "files", visited });
try cbor.writeArrayHeader(writer, visited); try cbor.writeArrayHeader(writer, visited);
for (self.files.items) |file| { for (self.files.items) |file| {
if (!file.visited) continue; if (!file.visited) continue;
@ -93,12 +95,15 @@ pub fn write_state_v1(self: *Self, writer: anytype) !void {
try cbor.writeValue(writer, file.mtime); try cbor.writeValue(writer, file.mtime);
try cbor.writeValue(writer, file.row); try cbor.writeValue(writer, file.row);
try cbor.writeValue(writer, file.col); try cbor.writeValue(writer, file.col);
tp.trace(tp.channel.event, .{ "write_state_v1", "file", file.path, file.mtime, file.row, file.col });
} }
try cbor.writeArrayHeader(writer, self.tasks.items.len); try cbor.writeArrayHeader(writer, self.tasks.items.len);
tp.trace(tp.channel.event, .{ "write_state_v1", "tasks", self.tasks.items.len });
for (self.tasks.items) |task| { for (self.tasks.items) |task| {
try cbor.writeArrayHeader(writer, 2); try cbor.writeArrayHeader(writer, 2);
try cbor.writeValue(writer, task.command); try cbor.writeValue(writer, task.command);
try cbor.writeValue(writer, task.mtime); try cbor.writeValue(writer, task.mtime);
tp.trace(tp.channel.event, .{ "write_state_v1", "task", task.command, task.mtime });
} }
} }
@ -115,24 +120,29 @@ pub fn write_state_v0(self: *Self, writer: anytype) !void {
} }
pub fn restore_state(self: *Self, data: []const u8) !void { pub fn restore_state(self: *Self, data: []const u8) !void {
tp.trace(tp.channel.event, .{"restore_state"});
errdefer |e| tp.trace(tp.channel.event, .{ "restore_state", "abort", e });
defer self.sort_files_by_mtime(); defer self.sort_files_by_mtime();
defer self.sort_tasks_by_mtime(); defer self.sort_tasks_by_mtime();
var iter: []const u8 = data; var iter: []const u8 = data;
_ = cbor.matchValue(&iter, tp.string) catch {}; _ = cbor.matchValue(&iter, tp.string) catch {};
_ = cbor.decodeArrayHeader(&iter) catch |e| switch (e) { _ = cbor.decodeArrayHeader(&iter) catch |e| switch (e) {
error.InvalidType => return self.restore_state_v0(data), error.InvalidType => return self.restore_state_v0(data),
else => return e, else => return tp.trace(tp.channel.event, .{ "restore_state", "unknown format", data }),
}; };
return self.restore_state_v1(data); return self.restore_state_v1(data);
} }
pub fn restore_state_v1(self: *Self, data: []const u8) !void { pub fn restore_state_v1(self: *Self, data: []const u8) !void {
tp.trace(tp.channel.event, .{"restore_state_v1"});
var iter: []const u8 = data; var iter: []const u8 = data;
var name: []const u8 = undefined; var name: []const u8 = undefined;
_ = cbor.matchValue(&iter, tp.extract(&name)) catch {}; _ = cbor.matchValue(&iter, tp.extract(&name)) catch {};
tp.trace(tp.channel.event, .{ "restore_state_v1", "name", name });
var files = try cbor.decodeArrayHeader(&iter); var files = try cbor.decodeArrayHeader(&iter);
tp.trace(tp.channel.event, .{ "restore_state_v1", "files", files });
while (files > 0) : (files -= 1) { while (files > 0) : (files -= 1) {
var path: []const u8 = undefined; var path: []const u8 = undefined;
var mtime: i128 = undefined; var mtime: i128 = undefined;
@ -147,16 +157,17 @@ pub fn restore_state_v1(self: *Self, data: []const u8) !void {
try cbor.skipValue(&iter); try cbor.skipValue(&iter);
continue; continue;
} }
tp.trace(tp.channel.event, .{ "restore_state_v1", "file", path, mtime, row, col });
self.longest_file_path = @max(self.longest_file_path, path.len); self.longest_file_path = @max(self.longest_file_path, path.len);
const stat = std.fs.cwd().statFile(path) catch return; const stat = std.fs.cwd().statFile(path) catch continue;
switch (stat.kind) { switch (stat.kind) {
.sym_link, .file => {}, .sym_link, .file => try self.update_mru_internal(path, mtime, row, col),
else => return, else => {},
} }
try self.update_mru_internal(path, mtime, row, col);
} }
var tasks = try cbor.decodeArrayHeader(&iter); var tasks = try cbor.decodeArrayHeader(&iter);
tp.trace(tp.channel.event, .{ "restore_state_v1", "tasks", tasks });
while (tasks > 0) : (tasks -= 1) { while (tasks > 0) : (tasks -= 1) {
var command: []const u8 = undefined; var command: []const u8 = undefined;
var mtime: i64 = undefined; var mtime: i64 = undefined;
@ -167,6 +178,7 @@ pub fn restore_state_v1(self: *Self, data: []const u8) !void {
try cbor.skipValue(&iter); try cbor.skipValue(&iter);
continue; continue;
} }
tp.trace(tp.channel.event, .{ "restore_state_v1", "task", command, mtime });
(try self.tasks.addOne()).* = .{ (try self.tasks.addOne()).* = .{
.command = try self.allocator.dupe(u8, command), .command = try self.allocator.dupe(u8, command),
.mtime = mtime, .mtime = mtime,
@ -174,7 +186,8 @@ pub fn restore_state_v1(self: *Self, data: []const u8) !void {
} }
} }
pub fn restore_state_v0(self: *Self, data: []const u8) !void { pub fn restore_state_v0(self: *Self, data: []const u8) error{ OutOfMemory, IntegerTooLarge, IntegerTooSmall, InvalidType, TooShort }!void {
tp.trace(tp.channel.event, .{"restore_state_v0"});
defer self.sort_files_by_mtime(); defer self.sort_files_by_mtime();
var name: []const u8 = undefined; var name: []const u8 = undefined;
var path: []const u8 = undefined; var path: []const u8 = undefined;
@ -183,6 +196,7 @@ pub fn restore_state_v0(self: *Self, data: []const u8) !void {
var col: usize = undefined; var col: usize = undefined;
var iter: []const u8 = data; var iter: []const u8 = data;
_ = cbor.matchValue(&iter, tp.extract(&name)) catch {}; _ = cbor.matchValue(&iter, tp.extract(&name)) catch {};
tp.trace(tp.channel.event, .{ "restore_state_v0", "name", name });
while (cbor.matchValue(&iter, .{ while (cbor.matchValue(&iter, .{
tp.extract(&path), tp.extract(&path),
tp.extract(&mtime), tp.extract(&mtime),
@ -192,13 +206,13 @@ pub fn restore_state_v0(self: *Self, data: []const u8) !void {
error.TooShort => return, error.TooShort => return,
else => return e, else => return e,
}) { }) {
tp.trace(tp.channel.event, .{ "restore_state_v0", "file", path, mtime, row, col });
self.longest_file_path = @max(self.longest_file_path, path.len); self.longest_file_path = @max(self.longest_file_path, path.len);
const stat = std.fs.cwd().statFile(path) catch return; const stat = std.fs.cwd().statFile(path) catch continue;
switch (stat.kind) { switch (stat.kind) {
.sym_link, .file => {}, .sym_link, .file => try self.update_mru_internal(path, mtime, row, col),
else => return, else => {},
} }
try self.update_mru_internal(path, mtime, row, col);
} }
} }

View file

@ -607,6 +607,7 @@ const Process = struct {
} }
fn persist_project(self: *Process, project: *Project) !void { fn persist_project(self: *Process, project: *Project) !void {
tp.trace(tp.channel.event, .{ "persist_project", project.name });
self.logger.print("saving: {s}", .{project.name}); self.logger.print("saving: {s}", .{project.name});
const file_name = try get_project_state_file_path(self.allocator, project); const file_name = try get_project_state_file_path(self.allocator, project);
defer self.allocator.free(file_name); defer self.allocator.free(file_name);
@ -618,6 +619,7 @@ const Process = struct {
} }
fn restore_project(self: *Process, project: *Project) !void { fn restore_project(self: *Process, project: *Project) !void {
tp.trace(tp.channel.event, .{ "restore_project", project.name });
const file_name = try get_project_state_file_path(self.allocator, project); const file_name = try get_project_state_file_path(self.allocator, project);
defer self.allocator.free(file_name); defer self.allocator.free(file_name);
var file = std.fs.openFileAbsolute(file_name, .{ .mode = .read_only }) catch |e| switch (e) { var file = std.fs.openFileAbsolute(file_name, .{ .mode = .read_only }) catch |e| switch (e) {