summaryrefslogtreecommitdiff
path: root/src/fs.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs.zig')
-rw-r--r--src/fs.zig143
1 files changed, 80 insertions, 63 deletions
diff --git a/src/fs.zig b/src/fs.zig
index 9104c93f..e4373bf0 100644
--- a/src/fs.zig
+++ b/src/fs.zig
@@ -171,11 +171,7 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply {
const path = osPath(p, req.node) orelse return Reply.fail(req.tag, E.NOENT);
if (req.op == .release) return .{ .tag = req.tag };
const io = std.Io.Threaded.global_single_threaded.io();
- const stat = stat: {
- pardes.turn.yield();
- defer pardes.turn.back();
- break :stat statPath(io, path, .{}) catch return Reply.fail(req.tag, E.NOENT);
- };
+ const stat = pardes.tape.site(.os_stat, null, statPath, .{ io, path, std.Io.Dir.StatFileOptions{} }) catch return Reply.fail(req.tag, E.NOENT);
const attr: Reply.Attr = .{ .name = if (req.node == os_root) "os" else std.fs.path.basename(path), .node = req.node, .dir = stat.kind == .directory, .size = stat.size, .mode = if (stat.kind == .directory) 0o755 else 0o644, .mtime = std.math.cast(u32, stat.mtime.toSeconds()) orelse 0 };
switch (req.op) {
.getattr => return .{ .tag = req.tag, .attr = attr },
@@ -199,32 +195,10 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply {
// Listed into a buffer of this request's own first; the shared
// reply buffer is another request's to use while the turn is out.
var listing: [16 * 1024]u8 = undefined;
- var fixed: std.heap.FixedBufferAllocator = .init(&listing);
- var listed: std.ArrayList(u8) = .empty;
- {
- pardes.turn.yield();
- defer pardes.turn.back();
- var dir = std.Io.Dir.cwd().openDir(io, path, .{ .iterate = true }) catch return Reply.fail(req.tag, E.NOTDIR);
- defer dir.close(io);
- var it = dir.iterate();
- var skip = req.off;
- while (it.next(io) catch return Reply.fail(req.tag, E.IO)) |entry| {
- var buf: [4096]u8 = undefined;
- const joined = std.fmt.bufPrint(&buf, "{s}/{s}", .{ std.mem.trimEnd(u8, path, "/"), entry.name }) catch continue;
- var normalized_buf: [4096]u8 = undefined;
- const normalized = resolveOs(joined, &normalized_buf) orelse continue;
- const child = dir.statFile(io, entry.name, .{}) catch continue;
- if (skip > 0) {
- skip -= 1;
- continue;
- }
- const node = if (std.mem.eql(u8, normalized.path, "/")) os_root else os_node | (std.hash.Wyhash.hash(0, normalized.path) & (os_node - 1));
- tree.stageDirent(&listed, fixed.allocator(), node, child.kind == .directory, entry.name);
- if (listed.items.len >= @max(req.size, 512)) break;
- }
- }
+ const listed = pardes.tape.site(.os_listing, null, osListing, .{ io, path, req.off, req.size, &listing }) catch |err|
+ return Reply.fail(req.tag, if (err == error.NotDir) E.NOTDIR else E.IO);
const out = p.fs.stage(p.gpa);
- out.appendSlice(p.gpa, listed.items) catch return Reply.fail(req.tag, E.NOMEM);
+ out.appendSlice(p.gpa, listed) catch return Reply.fail(req.tag, E.NOMEM);
return .{ .tag = req.tag, .payload = .{ .staged = @intCast(out.items.len) } };
},
.read, .write, .setattr => {
@@ -237,37 +211,70 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply {
var z: [4096]u8 = undefined;
const path_z = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return Reply.fail(req.tag, E.NOENT);
var buf: [ninep_io.msize]u8 = undefined; // a read never asks for more than a frame
- const got = io: {
- pardes.turn.yield();
- defer pardes.turn.back();
- const fd = libc.open(path_z, .{ .ACCMODE = if (req.op == .read) .RDONLY else .WRONLY, .NONBLOCK = true, .CLOEXEC = true });
- if (fd < 0) return Reply.fail(req.tag, E.PERM);
- defer _ = libc.close(fd);
- if (req.op == .setattr) {
- if (!req.truncate or req.off != 0 or libc.ftruncate(fd, 0) != 0) return tree.failText(req.tag, E.INVAL, "invalid truncate: only to zero");
- var truncated = attr;
- truncated.size = 0;
- return .{ .tag = req.tag, .attr = truncated };
- }
- if (req.off > std.math.maxInt(i64)) return tree.failText(req.tag, E.INVAL, "invalid offset: past what a file may hold");
- if (libc.lseek(fd, @intCast(req.off), libc.SEEK.SET) < 0) return Reply.fail(req.tag, E.IO);
- if (req.op == .write) {
- const written = libc.write(fd, req.data.ptr, req.data.len);
- if (written < 0) return Reply.fail(req.tag, E.IO);
- return .{ .tag = req.tag, .written = @intCast(written) };
- }
- const got = libc.read(fd, &buf, @min(req.size, buf.len));
- if (got < 0) return Reply.fail(req.tag, E.IO);
- break :io @as(usize, @intCast(got));
+ const got = switch (pardes.tape.site(.os_rw, null, osReadWrite, .{ path_z, req, attr, &buf })) {
+ .reply => |reply| return reply,
+ .read => |bytes| bytes,
};
const out = p.fs.stage(p.gpa);
- out.appendSlice(p.gpa, buf[0..got]) catch return Reply.fail(req.tag, E.NOMEM);
- return .{ .tag = req.tag, .payload = .{ .staged = @intCast(got) } };
+ out.appendSlice(p.gpa, got) catch return Reply.fail(req.tag, E.NOMEM);
+ return .{ .tag = req.tag, .payload = .{ .staged = @intCast(got.len) } };
},
else => return Reply.fail(req.tag, E.PERM),
}
}
+/// A directory's listing under /os, the entries from `off` until `size`
+/// bytes of them are staged, into `listing`: the syscalls of a readdir.
+fn osListing(io: std.Io, path: []const u8, off: u64, size: u32, listing: *[16 * 1024]u8) error{ NotDir, Io }![]const u8 {
+ var fixed: std.heap.FixedBufferAllocator = .init(listing);
+ var listed: std.ArrayList(u8) = .empty;
+ var dir = std.Io.Dir.cwd().openDir(io, path, .{ .iterate = true }) catch return error.NotDir;
+ defer dir.close(io);
+ var it = dir.iterate();
+ var skip = off;
+ while (it.next(io) catch return error.Io) |entry| {
+ var buf: [4096]u8 = undefined;
+ const joined = std.fmt.bufPrint(&buf, "{s}/{s}", .{ std.mem.trimEnd(u8, path, "/"), entry.name }) catch continue;
+ var normalized_buf: [4096]u8 = undefined;
+ const normalized = resolveOs(joined, &normalized_buf) orelse continue;
+ const child = dir.statFile(io, entry.name, .{}) catch continue;
+ if (skip > 0) {
+ skip -= 1;
+ continue;
+ }
+ const node = if (std.mem.eql(u8, normalized.path, "/")) os_root else os_node | (std.hash.Wyhash.hash(0, normalized.path) & (os_node - 1));
+ tree.stageDirent(&listed, fixed.allocator(), node, child.kind == .directory, entry.name);
+ if (listed.items.len >= @max(size, 512)) break;
+ }
+ return listed.items;
+}
+
+const OsReadWrite = union(enum) { reply: Reply, read: []const u8 };
+
+/// The syscalls of an /os read, write or truncate: the reply, or the bytes
+/// read into `buf`.
+fn osReadWrite(path_z: [:0]const u8, req: Req, attr: Reply.Attr, buf: []u8) OsReadWrite {
+ const fd = libc.open(path_z, .{ .ACCMODE = if (req.op == .read) .RDONLY else .WRONLY, .NONBLOCK = true, .CLOEXEC = true });
+ if (fd < 0) return .{ .reply = Reply.fail(req.tag, E.PERM) };
+ defer _ = libc.close(fd);
+ if (req.op == .setattr) {
+ if (!req.truncate or req.off != 0 or libc.ftruncate(fd, 0) != 0) return .{ .reply = tree.failText(req.tag, E.INVAL, "invalid truncate: only to zero") };
+ var truncated = attr;
+ truncated.size = 0;
+ return .{ .reply = .{ .tag = req.tag, .attr = truncated } };
+ }
+ if (req.off > std.math.maxInt(i64)) return .{ .reply = tree.failText(req.tag, E.INVAL, "invalid offset: past what a file may hold") };
+ if (libc.lseek(fd, @intCast(req.off), libc.SEEK.SET) < 0) return .{ .reply = Reply.fail(req.tag, E.IO) };
+ if (req.op == .write) {
+ const written = libc.write(fd, req.data.ptr, req.data.len);
+ if (written < 0) return .{ .reply = Reply.fail(req.tag, E.IO) };
+ return .{ .reply = .{ .tag = req.tag, .written = @intCast(written) } };
+ }
+ const got = libc.read(fd, buf.ptr, @min(req.size, buf.len));
+ if (got < 0) return .{ .reply = Reply.fail(req.tag, E.IO) };
+ return .{ .read = buf[0..@intCast(got)] };
+}
+
pub fn validMountName(name: []const u8) bool {
if (name.len == 0 or name.len > name_capacity or std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return false;
if (std.mem.eql(u8, name, "os") or std.mem.eql(u8, name, "self")) return false;
@@ -687,8 +694,10 @@ test "a path with a part over 255 bytes is NameTooLong, never a panic" {
pub fn makeDirs(dir: []const u8) void {
if (comptime !platform_has_fs) return;
if (dir.len == 0) return;
- pardes.turn.yield();
- defer pardes.turn.back();
+ pardes.tape.site(.make_dirs, null, makeDirsHost, .{dir});
+}
+
+fn makeDirsHost(dir: []const u8) void {
_ = std.Io.Dir.cwd().createDirPathStatus(std.Io.Threaded.global_single_threaded.io(), dir, .fromMode(0o755)) catch {};
}
@@ -798,6 +807,12 @@ pub fn dotRelative(word: []const u8) bool {
/// `~` to expand, or no home for it.
pub fn expandHome(path: []const u8, out: *[4096]u8) []const u8 {
if (comptime !pardes.hosted) return path;
+ if (path.len == 0 or path[0] != '~') return path;
+ // Home as the session that recorded found it (src/tape.zig).
+ return pardes.tape.value(.env, expandHomeHost, .{ path, out });
+}
+
+fn expandHomeHost(path: []const u8, out: *[4096]u8) []const u8 {
return expandHomeFrom(path, out, std.c.getenv("HOME"));
}
@@ -846,8 +861,10 @@ pub fn resolveOs(path: []const u8, out: *[4096]u8) ?Resolved {
if (comptime !platform_has_fs) return null;
var z: [4096]u8 = undefined;
const path_z = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return null;
- pardes.turn.yield();
- defer pardes.turn.back();
+ return pardes.tape.site(.resolve, null, resolveHost, .{ path_z, out });
+}
+
+fn resolveHost(path_z: [:0]const u8, out: *[4096]u8) ?Resolved {
const resolved = realpath(path_z, out) orelse return null;
return .{ .path = std.mem.span(resolved), .dir = isDir(resolved) };
}
@@ -904,9 +921,7 @@ pub fn readLimit(p: *pardes.Pardes, path: []const u8, max_bytes: usize) ![]u8 {
if (!std.mem.eql(u8, name, mount.name)) continue;
// A peer answers on its own time, and the peer may be this very
// editor: the turn goes out with the request.
- pardes.turn.yield();
- defer pardes.turn.back();
- return ninep_io.Client.readLimit(p.gpa, mount.dial, remote_path, path, limit);
+ return pardes.tape.site(.read_mount, p.gpa, ninep_io.Client.readLimit, .{ p.gpa, mount.dial, remote_path, path, limit });
}
return error.FileNotFound;
}
@@ -1233,8 +1248,10 @@ fn readFileLimit(gpa: std.mem.Allocator, path: []const u8, limit: usize) ![]u8 {
const path_z = std.fmt.bufPrintSentinel(&pathbuf, "{s}", .{native}, 0) catch return error.PathTooLong;
// The path may be a mount this editor serves: the turn goes out with the
// syscalls, and the bytes come back into memory of the caller's own.
- pardes.turn.yield();
- defer pardes.turn.back();
+ return pardes.tape.site(.read_file, gpa, readHost, .{ gpa, path_z, limit });
+}
+
+fn readHost(gpa: std.mem.Allocator, path_z: [:0]const u8, limit: usize) ![]u8 {
const fd = libc.open(path_z, .{ .ACCMODE = .RDONLY, .NONBLOCK = true });
if (fd < 0) return switch (libc.errno(fd)) {
.ACCES, .PERM => error.PermissionDenied,