summaryrefslogtreecommitdiff
path: root/src/ninep/ctl.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/ninep/ctl.zig')
-rw-r--r--src/ninep/ctl.zig77
1 files changed, 36 insertions, 41 deletions
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig
index ce92ba30..dc66663e 100644
--- a/src/ninep/ctl.zig
+++ b/src/ninep/ctl.zig
@@ -711,7 +711,10 @@ pub fn fillPager(p: *Pardes, serial: u32, held: *std.ArrayList(u8)) error{OutOfM
const page = try pardes.pager.parse(p.gpa, held.items);
held.clearAndFree(p.gpa);
if (page.text.len == 0 and f.content.len == 0) return page.deinit(p.gpa);
- if (!pane.fs.nomark) panes.File.pushUndo(p, pane);
+ if (!pane.fs.nomark) panes.File.pushUndo(p, pane) catch |err| {
+ page.deinit(p.gpa);
+ return err;
+ };
panes.File.setContent(p, f, page.text);
f.pager_spans = page.spans;
f.saved_revision = f.revision;
@@ -1101,18 +1104,16 @@ pub fn writePane(p: *Pardes, req: Req, pane: *Pane) Reply {
// It ran and was refused: its words (in ename), not the line
// quoted, as a builtin's refusal (Save's) says it.
if (getRefused(p, pane, "get")) |said| return tree.failText(req.tag, E.IO, said);
- var failed: anyerror = error.Unexpected;
- const errno = get(p, pane, &failed);
// Naming the path, in words 9ns reads as the errno; a peer
// out of reach as a look or a Save says it.
- if (errno != 0) {
+ get(p, pane) catch |err| {
const path = if (pane_files.fileOf(pane)) |f| f.path else "";
var why_buf: pardes.Kind.Buf(48) = .{};
- const why = if (pardes.Messages.dialReason(failed)) |r|
+ const why = if (pardes.Messages.dialReason(err)) |r|
why_buf.print("dial failed: {s}", .{r})
- else if (errno == E.NOENT) "no such file" else "cannot be read";
- return tree.failText(req.tag, errno, std.fmt.bufPrint(&p.fs.ename, "get: {s}: {s}", .{ path[0..@min(path.len, 256)], why[0..@min(why.len, 48)] }) catch &p.fs.ename); // cut to ename's 320
- }
+ else if (err == error.FileNotFound) "no such file" else "cannot be read";
+ return tree.failText(req.tag, if (err == error.FileNotFound) E.NOENT else E.IO, std.fmt.bufPrint(&p.fs.ename, "get: {s}: {s}", .{ path[0..@min(path.len, 256)], why[0..@min(why.len, 48)] }) catch &p.fs.ename); // cut to ename's 320
+ };
},
.lock => {
if (mine == null) return tree.failText(req.tag, E.INVAL, tree.e_bad_ctl);
@@ -1270,22 +1271,18 @@ pub fn getRefused(p: *Pardes, pane: *Pane, word: []const u8) ?[]const u8 {
/// loaded into the pane, which takes its name, in one undo step that puts
/// the old name back with the old text. Read first, so one that cannot be
/// read changes nothing; loaded, the pane is clean. Asking about unsaved
-/// edits is the caller's (getRefused). Returns an errno, 0 when loaded.
-pub fn getFile(p: *Pardes, pane: *Pane, path: []const u8, failed: *anyerror) u16 {
- const f = pane_files.fileOf(pane) orelse return E.INVAL;
- const id = p.paneBySerial(pane.serial) orelse return E.NOENT;
- const bytes = fs.read(p, path) catch |err| {
- failed.* = err;
- return switch (err) {
- error.FileNotFound => E.NOENT,
- else => E.IO,
- };
+/// edits is the caller's (getRefused).
+pub fn getFile(p: *Pardes, pane: *Pane, path: []const u8) GetError!void {
+ const f = pane_files.fileOf(pane) orelse return error.NotAFile;
+ const id = p.paneBySerial(pane.serial) orelse return error.FileNotFound;
+ const bytes = try fs.read(p, path);
+ panes.File.pushUndo(p, pane) catch |err| {
+ p.gpa.free(bytes);
+ return err;
};
- panes.File.pushUndo(p, pane);
pane_files.nameBuffer(p, id, path, false) catch |err| {
p.gpa.free(bytes);
- failed.* = err;
- return E.INVAL;
+ return if (err == error.OutOfMemory) error.OutOfMemory else error.InvalidName;
};
panes.File.setContent(p, f, bytes);
f.saved_revision = f.revision;
@@ -1295,31 +1292,30 @@ pub fn getFile(p: *Pardes, pane: *Pane, path: []const u8, failed: *anyerror) u16
f.disk_newer_warned = false;
f.disk_gone = false;
if (fs.localPath(path) != null) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } });
- return 0;
}
-pub fn get(p: *Pardes, pane: *Pane, failed: *anyerror) u16 {
- const f = pane_files.fileOf(pane) orelse return 0;
+/// What Get and Edit's `e` fail with, as their callers say it (the 9P
+/// reply's errno, FileNotFound ENOENT and the rest EIO; a peer's dial,
+/// Messages.dialReason; Get's words, Messages.errorWords): a read's, and a
+/// name the pane cannot take.
+pub const GetError = fs.ReadError || error{InvalidName};
+
+pub fn get(p: *Pardes, pane: *Pane) GetError!void {
+ const f = pane_files.fileOf(pane) orelse return;
// A directory pane: the directory read again (Dir.zig).
- if (f.listing != null) {
- panes.Dir.refresh(p, p.paneBySerial(pane.serial).?) catch |err| {
- failed.* = err;
- return if (err == error.FileNotFound) E.NOENT else E.IO;
- };
- return 0;
- }
- if (!panes.Output.fileTraits(f.output).saves) return 0;
- const bytes = fs.read(p, f.path) catch |err| {
- failed.* = err;
- return switch (err) {
- error.FileNotFound => E.NOENT,
- else => E.IO,
- };
+ if (f.listing != null) return panes.Dir.refresh(p, p.paneBySerial(pane.serial).?) catch |err| switch (err) {
+ error.FileNotFound, error.OutOfMemory => |e| e,
+ else => error.ReadFailed,
};
+ if (!panes.Output.fileTraits(f.output).saves) return;
+ const bytes = try fs.read(p, f.path);
// Throwing unsaved edits away for it is a reload the log hears of, as a
// clean one from the watch is.
const discarded = f.revision != f.saved_revision;
- panes.File.pushUndo(p, pane);
+ panes.File.pushUndo(p, pane) catch |err| {
+ p.gpa.free(bytes);
+ return err;
+ };
panes.File.setContent(p, f, bytes);
f.saved_revision = f.revision;
f.saved_hash = std.hash.Wyhash.hash(0, f.content);
@@ -1330,7 +1326,6 @@ pub fn get(p: *Pardes, pane: *Pane, failed: *anyerror) u16 {
var rec: [32]u8 = undefined;
pardes.ctlfs.events.notePath(p, "changed", std.fmt.bufPrint(&rec, "{d} reloaded", .{pane.serial}) catch unreachable); // a u32 fits 32
}
- return 0;
}
// ---- tests ----
@@ -2658,7 +2653,7 @@ test "what a /pager open writes after its directory is the text: plain in the bo
try testing.expectEqual(@as(usize, 0), f.pager_spans.len);
// Undone back to the paged text: the text comes back, its colours
// stay dropped (they were the program's, for the text it printed).
- panes.File.undoStep(p, pn, true);
+ try panes.File.undoStep(p, pn, true);
try testing.expectEqualStrings("red /a\nNext\n", f.content);
try testing.expectEqual(@as(usize, 0), f.pager_spans.len);