summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-09-30 22:50:41 -0300
committerGabriel Schneider <[email protected]>2026-09-30 22:50:50 -0300
commita03d1649108510cda5951e918e561d7cae43d6b0 (patch)
treea2def9245daf72b23798e983258a614460f5cf11
parent705be665742fa0332585a4321640b5e2245610cb (diff)
downloadcloud9-a03d1649108510cda5951e918e561d7cae43d6b0.tar.gz
cloud9-a03d1649108510cda5951e918e561d7cae43d6b0.zip
9ns: a file whose stat length is 0 opens direct and nonseekable, so a splice or a cached size never ends a stream early
A server's stream (pardes's log) or generated view stats 0: its length is no length. Opened with the page cache or a kernel file offset of its own, a splicing reader (coreutils cat into a pipe) could read it from the wrong offset and take the size as its end. Such a file is now opened FOPEN_DIRECT_IO | FOPEN_NONSEEKABLE whatever --no-direct-io says; every other file keeps the option's flags. Co-Authored-By: Claude Opus 5.5 <[email protected]>
-rw-r--r--9ns/src/bridge.zig21
1 files changed, 20 insertions, 1 deletions
diff --git a/9ns/src/bridge.zig b/9ns/src/bridge.zig
index 2226658..07bd5d8 100644
--- a/9ns/src/bridge.zig
+++ b/9ns/src/bridge.zig
@@ -628,14 +628,25 @@ const Bridge = struct {
b.clunkQuiet(fid);
return e;
};
+ // A file whose stat length is 0 is a stream or a view made by each
+ // read (a log, a screen): no page cache, no size taken as its end,
+ // and no offset of the kernel's own, so a splice (cat) reads it from
+ // where the open stands, as read(2) does.
+ const stream = !is_dir and if (b.stat(fid)) |st| st.length == 0 else |_| false;
const fh = try b.newHandle(fid, req.header.nodeid);
const out = fuse.OpenOut{
.fh = fh,
- .open_flags = if (!is_dir and b.opts.direct_io) fuse.FOPEN_DIRECT_IO else 0,
+ .open_flags = openFlags(is_dir, stream, b.opts.direct_io),
};
try b.reply(req.header.unique, &.{std.mem.asBytes(&out)});
}
+ fn openFlags(is_dir: bool, stream: bool, direct_io: bool) u32 {
+ if (is_dir) return 0;
+ if (stream) return fuse.FOPEN_DIRECT_IO | fuse.FOPEN_NONSEEKABLE;
+ return if (direct_io) fuse.FOPEN_DIRECT_IO else 0;
+ }
+
fn newHandle(b: *Bridge, fid: u32, nodeid: u64) HandlerError!u64 {
const fh = b.next_fh;
b.handles.put(b.gpa, fh, .{ .fid = fid, .nodeid = nodeid, .dir = null }) catch |e| {
@@ -2568,6 +2579,14 @@ test "attr mapping: DMDIR → S_IFDIR|perm, length → size/blocks" {
try testing.expectEqual(@as(u64, 2), attrFromStat(testStat("f", 0o600, 513, 4), 4, 0, 0).blocks);
}
+test "a file whose stat length is 0 opens direct and nonseekable, whatever the cache option" {
+ for ([_]bool{ false, true }) |direct_io| {
+ try std.testing.expectEqual(fuse.FOPEN_DIRECT_IO | fuse.FOPEN_NONSEEKABLE, Bridge.openFlags(false, true, direct_io));
+ try std.testing.expectEqual(@as(u32, if (direct_io) fuse.FOPEN_DIRECT_IO else 0), Bridge.openFlags(false, false, direct_io));
+ try std.testing.expectEqual(@as(u32, 0), Bridge.openFlags(true, true, direct_io));
+ }
+}
+
test "open flag → 9P mode mapping" {
const rdonly: u32 = @bitCast(linux.O{ .ACCMODE = .RDONLY });
const wronly: u32 = @bitCast(linux.O{ .ACCMODE = .WRONLY });