summaryrefslogtreecommitdiff
path: root/src/ninep/tree.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/ninep/tree.zig')
-rw-r--r--src/ninep/tree.zig47
1 files changed, 28 insertions, 19 deletions
diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig
index f7ca0cb7..bca89466 100644
--- a/src/ninep/tree.zig
+++ b/src/ninep/tree.zig
@@ -447,14 +447,14 @@ pub fn resolveSelf(p: *Pardes, path: []const u8) ?u64 {
// ---- staging ----
-pub fn stageDirent(out: *std.ArrayList(u8), gpa: std.mem.Allocator, node: u64, dir: bool, name: []const u8) void {
+pub fn stageDirent(out: *std.ArrayList(u8), gpa: std.mem.Allocator, node: u64, dir: bool, name: []const u8) error{OutOfMemory}!void {
if (name.len == 0 or name.len > 255) return;
var head: [10]u8 = undefined;
std.mem.writeInt(u64, head[0..8], node, .little);
head[8] = @intFromBool(dir);
head[9] = @intCast(name.len);
// Head and name both or neither: a head alone would corrupt the listing.
- out.ensureUnusedCapacity(gpa, head.len + name.len) catch return;
+ try out.ensureUnusedCapacity(gpa, head.len + name.len);
out.appendSliceAssumeCapacity(&head);
out.appendSliceAssumeCapacity(name);
}
@@ -736,20 +736,30 @@ fn lookup(p: *Pardes, req: Req, target: Target) Reply {
fn readdir(p: *Pardes, req: Req, target: Target) Reply {
const out = p.fs.stage(p.gpa);
- var skip = req.off;
+ stageDir(p, out, target, req.off) catch |err| return Reply.fail(req.tag, switch (err) {
+ error.OutOfMemory => E.NOMEM,
+ error.FileNotFound => E.NOENT,
+ error.NotDir => E.NOTDIR,
+ });
+ return .{ .tag = req.tag, .payload = .{ .staged = @intCast(out.items.len) } };
+}
+
+/// The entries of `target` from the `off`th on, staged into `out`.
+fn stageDir(p: *Pardes, out: *std.ArrayList(u8), target: Target, off: u64) error{ OutOfMemory, FileNotFound, NotDir }!void {
+ var skip = off;
switch (target) {
.top => |f| switch (f) {
.root => {
// The guide, then what describes the session, then the
// files that act, then the directories.
inline for (.{ TopFile.README, TopFile.index, TopFile.status, TopFile.focus, TopFile.ctl, TopFile.commands, TopFile.recent, TopFile.dumps, TopFile.addons, TopFile.look, TopFile.exec, TopFile.pager, TopFile.log, TopFile.screen, TopFile.listeners, TopFile.sessions, TopFile.layout, TopFile.tag, TopFile.tagexec, TopFile.kbdin, TopFile.mousein, TopFile.pane, TopFile.col }) |t| {
- if (skip > 0) skip -= 1 else stageDirent(out, p.gpa, @backingInt(t), t.dir(), t.fileName());
+ if (skip > 0) skip -= 1 else try stageDirent(out, p.gpa, @backingInt(t), t.dir(), t.fileName());
}
- if (skip > 0) skip -= 1 else stageDirent(out, p.gpa, fs.os_root, true, "os");
- sources.stage(p, out, "", &skip);
+ if (skip > 0) skip -= 1 else try stageDirent(out, p.gpa, fs.os_root, true, "os");
+ try sources.stage(p, out, "", &skip);
},
.pane => {
- if (skip > 0) skip -= 1 else stageDirent(out, p.gpa, @backingInt(TopFile.new), false, TopFile.new.fileName());
+ if (skip > 0) skip -= 1 else try stageDirent(out, p.gpa, @backingInt(TopFile.new), false, TopFile.new.fileName());
var last: u32 = 0;
while (nextSerialAfter(p, last)) |serial| {
last = serial;
@@ -760,7 +770,7 @@ fn readdir(p: *Pardes, req: Req, target: Target) Reply {
var buf: [16]u8 = undefined;
// unreachable: a u32 serial fits 16
const name = std.fmt.bufPrint(&buf, "{d}", .{serial}) catch unreachable;
- stageDirent(out, p.gpa, Node.of(serial, .dir), true, name);
+ try stageDirent(out, p.gpa, Node.of(serial, .dir), true, name);
}
},
.col => for (0..p.view().ncol) |c| {
@@ -771,40 +781,39 @@ fn readdir(p: *Pardes, req: Req, target: Target) Reply {
var buf: [16]u8 = undefined;
const serial = layout.columnSerial(p, c);
// unreachable: a u32 serial fits 16
- stageDirent(out, p.gpa, Node.ofCol(serial, .dir), true, std.fmt.bufPrint(&buf, "{d}", .{serial}) catch unreachable);
+ try stageDirent(out, p.gpa, Node.ofCol(serial, .dir), true, std.fmt.bufPrint(&buf, "{d}", .{serial}) catch unreachable);
},
- else => return Reply.fail(req.tag, E.NOTDIR),
+ else => return error.NotDir,
},
.col => |c| {
- if (layout.columnBySerial(p, c.serial) == null) return Reply.fail(req.tag, E.NOENT);
- if (c.file != .dir) return Reply.fail(req.tag, E.NOTDIR);
+ if (layout.columnBySerial(p, c.serial) == null) return error.FileNotFound;
+ if (c.file != .dir) return error.NotDir;
inline for (.{ ColFile.tag, ColFile.ctl, ColFile.exec }) |f| {
- if (skip > 0) skip -= 1 else stageDirent(out, p.gpa, Node.ofCol(c.serial, f), false, f.fileName());
+ if (skip > 0) skip -= 1 else try stageDirent(out, p.gpa, Node.ofCol(c.serial, f), false, f.fileName());
}
},
.pane => |t| {
- const id = p.paneBySerial(t.serial) orelse return Reply.fail(req.tag, E.NOENT);
+ const id = p.paneBySerial(t.serial) orelse return error.FileNotFound;
const terminal = p.panes[id].?.isTerminal();
switch (t.file) {
// The pty files are listed inside pty/, and it only on a terminal.
.dir => inline for (comptime std.enums.values(PaneFile)) |f| {
if (comptime f == .dir or (f.inPty() and f != .pty)) continue;
if (f != .pty or terminal) {
- if (skip > 0) skip -= 1 else stageDirent(out, p.gpa, Node.of(t.serial, f), f.isDir(), f.fileName());
+ if (skip > 0) skip -= 1 else try stageDirent(out, p.gpa, Node.of(t.serial, f), f.isDir(), f.fileName());
}
},
.pty => {
- if (!terminal) return Reply.fail(req.tag, E.NOENT);
+ if (!terminal) return error.FileNotFound;
inline for (comptime std.enums.values(PaneFile)) |f| {
if (comptime !f.inPty() or f == .pty) continue;
- if (skip > 0) skip -= 1 else stageDirent(out, p.gpa, Node.of(t.serial, f), false, f.fileName());
+ if (skip > 0) skip -= 1 else try stageDirent(out, p.gpa, Node.of(t.serial, f), false, f.fileName());
}
},
- else => return Reply.fail(req.tag, E.NOTDIR),
+ else => return error.NotDir,
}
},
}
- return .{ .tag = req.tag, .payload = .{ .staged = @intCast(out.items.len) } };
}
fn open(p: *Pardes, req: Req, target: Target) Reply {