diff options
Diffstat (limited to '9ns/src/bridge.zig')
| -rw-r--r-- | 9ns/src/bridge.zig | 21 |
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 }); |
