summaryrefslogtreecommitdiff
path: root/core/ninep/sources.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-18 22:03:14 -0300
committerGabriel Schneider <[email protected]>2026-11-18 22:23:51 -0300
commit99f5b10eccd95aa0066ea6fe6bb75f367ab56a47 (patch)
tree813596ea84318a7bae4fd7416ba463de8e3b7107 /core/ninep/sources.zig
parent92d553123673376adf6126ab90d49a22c5b833d2 (diff)
downloadpardes-99f5b10eccd95aa0066ea6fe6bb75f367ab56a47.tar.gz
pardes-99f5b10eccd95aa0066ea6fe6bb75f367ab56a47.zip
core: src/ becomes core/, pardes.zig its root.zig (the reorganisation's tree, step 2d), and every mention of a src/ path follows: the build's paths, the core's imports of its root, the tests that read the repository's files by path (lspbench's, look.zig's spans over core/look.zig), the comments that cite files, the documents and the manual (the typst corpus reads core/tutor.txt and core/plumbing); left as they were: the /src tree's served names (src/pardes.zig and the rest are the 9P tree's, not the repository's), the CHANGELOG's history, fixtures that are only strings; fs-unit-test runs from the core's root (a module of the core rooted at fs.zig had the runtime's and the devices' `core` be fs.zig); nothing else changes: the ledger (pardes.test.* now root.test.*), frames byte-identical, replicas and process-test the base's
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/ninep/sources.zig')
-rw-r--r--core/ninep/sources.zig148
1 files changed, 148 insertions, 0 deletions
diff --git a/core/ninep/sources.zig b/core/ninep/sources.zig
new file mode 100644
index 00000000..1d804e31
--- /dev/null
+++ b/core/ninep/sources.zig
@@ -0,0 +1,148 @@
+//! The editor's embedded sources, served read-only from the root when the
+//! build carries them (-Dembed-sources=true).
+const std = @import("std");
+const pardes = @import("../root.zig");
+const fs = @import("../fs.zig");
+const tree = @import("tree.zig");
+
+const Pardes = pardes.Pardes;
+const Req = tree.Req;
+const Reply = tree.Reply;
+const E = tree.E;
+const sources = fs.sources;
+
+pub const archive_node: u64 = 1 << 61;
+
+/// The node of an archive path, or null when nothing embedded lives there.
+/// The empty path is the archive root, which is never listed by itself.
+pub fn node(path: []const u8) ?u64 {
+ if (path.len == 0) return archive_node;
+ if (path.len >= 4096) return null;
+ for (sources, 0..) |source, i| {
+ if (std.mem.eql(u8, source.path, path) or
+ (source.path.len > path.len and source.path[path.len] == '/' and std.mem.startsWith(u8, source.path, path)))
+ return archive_node | (@as(u64, i) << 12) | path.len;
+ }
+ return null;
+}
+
+fn info(n: u64) ?struct { path: []const u8, contents: []const u8, dir: bool } {
+ if (n == archive_node) return .{ .path = "", .contents = "", .dir = true };
+ const index = (n & ~archive_node) >> 12;
+ if (index >= sources.len) return null;
+ const source = sources[@intCast(index)];
+ const len: usize = @intCast(n & 4095);
+ if (len > source.path.len) return null;
+ return .{ .path = source.path[0..len], .contents = source.contents, .dir = len < source.path.len };
+}
+
+/// Stages the children of one archive directory; "" lists the top-level
+/// directories the served root shows.
+pub fn stage(p: *Pardes, out: *std.ArrayList(u8), path: []const u8, skip: *u64) error{OutOfMemory}!void {
+ var seen: [sources.len][]const u8 = undefined;
+ var count: usize = 0;
+ for (sources, 0..) |source, i| {
+ if (path.len != 0 and
+ (source.path.len <= path.len or source.path[path.len] != '/' or !std.mem.startsWith(u8, source.path, path))) continue;
+ const start = if (path.len == 0) 0 else path.len + 1;
+ const rest = source.path[start..];
+ const len = std.mem.indexOfScalar(u8, rest, '/') orelse rest.len;
+ const name = rest[0..len];
+ var duplicate = false;
+ for (seen[0..count]) |previous| if (std.mem.eql(u8, name, previous)) {
+ duplicate = true;
+ break;
+ };
+ if (duplicate) continue;
+ seen[count] = name;
+ count += 1;
+ if (skip.* > 0) {
+ skip.* -= 1;
+ continue;
+ }
+ const child = archive_node | (@as(u64, i) << 12) | (start + len);
+ try tree.stageDirent(out, p.gpa, child, len < rest.len, name);
+ }
+}
+
+pub fn handle(p: *Pardes, req: Req) Reply {
+ const found = info(req.node) orelse return Reply.fail(req.tag, E.NOENT);
+ const attr: Reply.Attr = .{
+ .node = req.node,
+ .name = std.Io.Dir.path.basename(found.path),
+ .dir = found.dir,
+ .size = if (found.dir) 0 else found.contents.len,
+ .mode = if (found.dir) 0o555 else 0o444,
+ .mtime = p.fs.started,
+ };
+ switch (req.op) {
+ .getattr => return .{ .tag = req.tag, .attr = attr },
+ .open => return .{ .tag = req.tag, .handle = 1 },
+ .release => return .{ .tag = req.tag },
+ .lookup => {
+ if (!found.dir) return Reply.fail(req.tag, E.NOTDIR);
+ if (std.mem.eql(u8, req.data, "..")) {
+ const parent = std.Io.Dir.path.dirname(found.path) orelse "";
+ if (parent.len == 0) return tree.handle(p, .{ .tag = req.tag, .op = .getattr, .node = tree.root });
+ const parent_node = node(parent) orelse return Reply.fail(req.tag, E.NOENT);
+ return handle(p, .{ .tag = req.tag, .op = .getattr, .node = parent_node });
+ }
+ if (req.data.len == 0 or std.mem.indexOfAny(u8, req.data, "/\x00") != null) return Reply.fail(req.tag, E.NOENT);
+ var buf: [4096]u8 = undefined;
+ const path = if (found.path.len == 0) req.data else std.fmt.bufPrint(&buf, "{s}/{s}", .{ found.path, req.data }) catch return Reply.fail(req.tag, E.NOENT);
+ const child = node(path) orelse return Reply.fail(req.tag, E.NOENT);
+ return handle(p, .{ .tag = req.tag, .op = .getattr, .node = child });
+ },
+ .readdir => {
+ if (!found.dir) return Reply.fail(req.tag, E.NOTDIR);
+ const out = p.fs.stage(p.gpa);
+ var skip = req.off;
+ stage(p, out, found.path, &skip) catch return Reply.fail(req.tag, E.NOMEM);
+ return .{ .tag = req.tag, .payload = .{ .staged = @intCast(out.items.len) } };
+ },
+ .read => {
+ if (found.dir) return Reply.fail(req.tag, E.PERM);
+ return tree.window(p, req, found.contents);
+ },
+ else => return Reply.fail(req.tag, E.PERM),
+ }
+}
+
+const testing = std.testing;
+const th = @import("testing.zig");
+const limits = @import("../memory.zig").limits;
+
+test "embedded sources appear under the root only when the build carries them" {
+ const p = try th.withFile(testing.allocator, "x\n");
+ defer p.deinit();
+ var buf: [64]th.Dirent = undefined;
+ const top = th.dirents(th.rdir(p, tree.root, 0).bytes, &buf);
+ const listed = th.nameAt(top, "src");
+ try testing.expectEqual(limits.embedded_sources, listed != null);
+ const looked = th.look_up(p, tree.root, "src");
+ try testing.expectEqual(limits.embedded_sources, looked.reply.status == .ok);
+ try testing.expect(tree.resolveSelf(p, "shaders/../src/../") == tree.root);
+ if (!limits.embedded_sources) {
+ try testing.expect(tree.resolveSelf(p, "src/pardes.zig") == null);
+ try testing.expect(tree.resolveSelf(p, "src") == null);
+ try testing.expect(th.nameAt(top, "build.zig") == null);
+ return;
+ }
+ try testing.expect(looked.reply.attr.dir);
+ try testing.expectEqual(@as(u16, 0o555), looked.reply.attr.mode);
+ try testing.expectEqual(listed.?.node, looked.reply.attr.node);
+ try testing.expect(th.nameAt(top, "build.zig") == null);
+ try testing.expect(th.nameAt(top, "build.zig.zon") == null);
+ const file = th.look_up(p, looked.reply.attr.node, "pardes.zig");
+ try testing.expectEqual(tree.Status.ok, file.reply.status);
+ try testing.expectEqual(@as(u16, 0o444), file.reply.attr.mode);
+ try testing.expectEqual(@as(u64, fs.sourceBytes("src/pardes.zig").?.len), file.reply.attr.size);
+ try testing.expectEqual(file.reply.attr.node, tree.resolveSelf(p, "src/pardes.zig").?);
+ const opened = th.call(p, .{ .tag = 1, .op = .open, .node = file.reply.attr.node });
+ const head = th.call(p, .{ .tag = 2, .op = .read, .node = file.reply.attr.node, .handle = opened.reply.handle, .size = 16 });
+ try testing.expectEqualStrings(fs.sourceBytes("src/pardes.zig").?[0..16], head.bytes);
+ const parent = th.look_up(p, looked.reply.attr.node, "..");
+ try testing.expectEqual(tree.root, parent.reply.attr.node);
+ try testing.expectEqual(E.NOENT, th.look_up(p, tree.root, "build.zig").errno());
+ try testing.expectEqual(E.NOENT, th.look_up(p, looked.reply.attr.node, "missing.zig").errno());
+}