1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
//! A blocking 9P2000 client: one connection to one tree, its calls made
//! one at a time from one thread, each waiting for its answer
//! (plan9port's lib9pclient `CFsys`). `Client` speaks the protocol; this
//! adds the socket, the fids and the server's words. A refusal is
//! `error.Refused` and `said()` is what the server said: the string a
//! mount would have turned into an errno.
//!
//! Paths are slash-separated names from the tree's root ("pane/3/body").
//! The walk to a path goes out with the request that uses it, in one
//! write (a walk never parks, so the request always follows it), and a
//! fid is let go of without waiting for the answer: an open, a stat or a
//! remove costs one round trip. The buffers are in the struct
//! (2 × `msize`): an `Fsys` stays where it is while it is used.
//!
//! A call waits `timeout` for its answer (30 s from `init`), so a server
//! that accepts and then says nothing is `error.Timeout`, not a caller
//! blocked for ever.
const std = @import("std");
const Io = std.Io;
const c9 = @import("root.zig");
const wire = @import("wire.zig");
const client = @import("client.zig");
const transport = @import("transport.zig");
const post = @import("post.zig");
const fs = @import("fs.zig");
const Client = client.Client;
const Stat = wire.Stat;
const Qid = wire.Qid;
const max_welem = wire.max_welem;
const Fsys = @This();
/// The largest message asked for; the server may answer less.
pub const msize: u32 = 65536;
pub const Error = error{
/// The server refused: `said()` holds its words.
Refused,
/// The connection ended.
Hangup,
/// The server broke the protocol, or a request could not be made
/// (a name with a slash or longer than a message).
Botch,
/// No answer within `timeout`. The answer may come yet, under a tag
/// the next call does not expect: `close` the connection.
Timeout,
};
/// What `init` sets `timeout` to: pardes's client waits this long for a
/// write that runs a command.
pub const default_timeout: Io.Timeout = .{ .duration = .{ .raw = .fromSeconds(30), .clock = .awake } };
io: Io,
stream: Io.net.Stream,
cl: Client,
in: [msize]u8,
out: [msize]u8,
next_fid: u32,
/// Clunks sent and not waited for, a bit per tag: their answers are
/// dropped as they come.
unwaited: u32,
/// The walk sent ahead of the request that uses its fid, and the names
/// it asked for: `rpc` checks its answer on the way to the request's.
walk_tag: u16,
walk_asked: usize,
words: [fs.errmax]u8,
words_len: usize,
/// How long a call waits for its answer, from when it is made: `init`
/// sets `default_timeout`, before it speaks; `.none` waits for ever, as
/// following an event file wants.
timeout: Io.Timeout,
/// Speaks 9P on `stream`, which is this one's from here (`close`), on a
/// refusal too: the version, then an attach as `uname` to the tree's root.
pub fn init(f: *Fsys, io: Io, stream: Io.net.Stream, uname: []const u8) Error!void {
f.io = io;
f.stream = stream;
f.cl = .init(.{ .in = &f.in, .out = &f.out });
f.next_fid = 1;
f.unwaited = 0;
f.walk_tag = wire.notag;
f.walk_asked = 0;
f.words_len = 0;
f.timeout = default_timeout;
errdefer f.close();
const v = (try f.rpc(.{ .version = .{ .msize = msize } })).version;
if (!std.mem.eql(u8, v.version, "9P2000")) return f.refuse("the server does not speak 9P2000");
_ = try f.rpc(.{ .attach = .{ .fid = 0, .uname = uname } });
}
pub const DialError = error{
/// Not `unix!PATH`, `tcp!HOST!PORT`, an absolute path or a legal name.
BadAddress,
/// $XDG_RUNTIME_DIR is unset, so there is no registry to find a name in.
NotRuntimeDir,
} || Error || Io.net.IpAddress.ConnectError || Io.net.UnixAddress.ConnectError || Io.net.UnixAddress.InitError;
/// Dials `dest` and attaches (`init`). `unix!PATH`, or a path starting
/// with a slash, is a Unix socket; `tcp!HOST!PORT` a numeric address;
/// anything else a name posted in the registry (`post`), which may name
/// an entry of a subdirectory: `pardes/main` is
/// `$XDG_RUNTIME_DIR/9p/pardes/main`.
pub fn dial(f: *Fsys, io: Io, env: post.Env, dest: []const u8, uname: []const u8) DialError!void {
var buf: [transport.sun_path_len]u8 = undefined;
const address: transport.Address = if (std.mem.startsWith(u8, dest, "tcp!")) blk: {
const bang = std.mem.lastIndexOfScalar(u8, dest, '!').?;
if (bang < 5) return error.BadAddress;
const port = std.fmt.parseInt(u16, dest[bang + 1 ..], 10) catch return error.BadAddress;
break :blk .{ .tcp = Io.net.IpAddress.parse(dest[4..bang], port) catch return error.BadAddress };
} else if (std.mem.startsWith(u8, dest, "unix!") or std.mem.startsWith(u8, dest, "/")) blk: {
const path = if (dest[0] == '/') dest else dest["unix!".len..];
break :blk .{ .unix = std.fmt.bufPrintSentinel(&buf, "{s}", .{path}, 0) catch return error.BadAddress };
} else blk: {
var it = std.mem.splitScalar(u8, dest, '/');
while (it.next()) |name| if (!post.legalName(name)) return error.BadAddress;
var dir_buf: [transport.sun_path_len]u8 = undefined;
const dir = post.registryDir(env, &dir_buf) catch |err| switch (err) {
error.NotRuntimeDir => return error.NotRuntimeDir,
else => return error.BadAddress,
};
break :blk .{ .unix = std.fmt.bufPrintSentinel(&buf, "{s}/{s}", .{ dir, dest }, 0) catch return error.BadAddress };
};
try f.init(io, try transport.connect(io, address), uname);
}
/// Hangs up: the server lets go of every fid.
pub fn close(f: *Fsys) void {
f.stream.close(f.io);
}
/// The server's words for the last `error.Refused`.
pub fn said(f: *const Fsys) []const u8 {
return f.words[0..f.words_len];
}
pub const Opened = struct { fid: u32, qid: Qid, iounit: u32 };
/// Opens `path` with `mode` (`cloud9.oread`, `owrite`, `ordwr`, with
/// `otrunc` and the rest): its fid, which `clunk` lets go of.
pub fn open(f: *Fsys, path: []const u8, mode: u8) Error!Opened {
const fid = try f.walkAhead(path);
errdefer f.clunk(fid);
const o = (try f.rpc(.{ .open = .{ .fid = fid, .mode = mode } })).open;
return .{ .fid = fid, .qid = o.qid, .iounit = o.iounit };
}
/// Creates `path` in its directory with `perm` (`cloud9.dmdir` for a
/// directory) and opens it with `mode`.
pub fn create(f: *Fsys, path: []const u8, perm: u32, mode: u8) Error!Opened {
const trimmed = std.mem.trim(u8, path, "/");
const slash = std.mem.lastIndexOfScalar(u8, trimmed, '/');
const fid = try f.walkAhead(if (slash) |s| trimmed[0..s] else "");
errdefer f.clunk(fid);
const name = if (slash) |s| trimmed[s + 1 ..] else trimmed;
const c = (try f.rpc(.{ .create = .{ .fid = fid, .name = name, .perm = perm, .mode = mode } })).create;
return .{ .fid = fid, .qid = c.qid, .iounit = c.iounit };
}
/// Removes `path`.
pub fn remove(f: *Fsys, path: []const u8) Error!void {
// The fid is gone whatever the answer.
_ = try f.rpc(.{ .remove = .{ .fid = try f.walkAhead(path) } });
}
/// The stat of `path`; its strings are good until the next call.
pub fn stat(f: *Fsys, path: []const u8) Error!Stat {
const fid = try f.walkAhead(path);
defer f.clunk(fid);
return (try f.rpc(.{ .stat = .{ .fid = fid } })).stat;
}
/// One Tread of up to `count` bytes (and the message size) at `offset`:
/// the bytes, good until the next call; none at the end.
pub fn read(f: *Fsys, fid: u32, offset: u64, count: u32) Error![]const u8 {
return (try f.rpc(.{ .read = .{ .fid = fid, .offset = offset, .count = @min(count, f.cl.maxRead()) } })).read;
}
/// One Twrite of as much of `bytes` as a message holds: the count written.
pub fn write(f: *Fsys, fid: u32, offset: u64, bytes: []const u8) Error!u32 {
const n = @min(bytes.len, f.cl.maxWrite());
return (try f.rpc(.{ .write = .{ .fid = fid, .offset = offset, .data = bytes[0..n] } })).write;
}
/// Lets go of `fid`, without waiting for the answer.
pub fn clunk(f: *Fsys, fid: u32) void {
const tag = f.cl.submit(.{ .clunk = .{ .fid = fid } }) catch return;
if (tag < 32) f.unwaited |= @as(u32, 1) << @intCast(tag);
f.ship() catch {};
}
/// The file at `path`, read from the start until a read comes back
/// empty, into `out`, flushed after each answer: a file whose reads the
/// server parks (an event file) streams as it comes.
pub fn readFile(f: *Fsys, path: []const u8, out: *Io.Writer) (Error || Io.Writer.Error)!void {
const o = try f.open(path, c9.oread);
defer f.clunk(o.fid);
try f.drain(o, out);
}
/// `bytes` as the whole of the file at `path`, as `>` would put them:
/// opened truncated, written in as many writes as it takes.
pub fn writeFile(f: *Fsys, path: []const u8, bytes: []const u8) Error!void {
const o = try f.open(path, c9.owrite | c9.otrunc);
defer f.clunk(o.fid);
try f.put(o, bytes);
}
/// Writes `request` to `path` and reads the answer on the same open from
/// the start, into `out` (as `readFile`): Plan 9's write, seek 0, read of
/// /net/cs. An answer that never ends (a log written `follow`) streams
/// until the server hangs up.
pub fn ask(f: *Fsys, path: []const u8, request: []const u8, out: *Io.Writer) (Error || Io.Writer.Error)!void {
const o = try f.open(path, c9.ordwr);
defer f.clunk(o.fid);
try f.put(o, request);
try f.drain(o, out);
}
/// Directory entries as a directory read answers them: `next` decodes
/// them one at a time (their strings borrow the bytes).
pub const Entries = struct {
bytes: []const u8,
pos: usize = 0,
pub fn next(e: *Entries) Error!?Stat {
if (e.pos == e.bytes.len) return null;
if (e.bytes.len - e.pos < 2) return error.Botch;
const len = 2 + @as(usize, std.mem.readInt(u16, e.bytes[e.pos..][0..2], .little));
if (len > e.bytes.len - e.pos) return error.Botch;
defer e.pos += len;
return Stat.decode(e.bytes[e.pos..][0..len]) catch error.Botch;
}
};
// ---- internals ----
/// Writes all of `bytes` from the start, in writes no larger than the
/// open's iounit.
fn put(f: *Fsys, o: Opened, bytes: []const u8) Error!void {
var done: usize = 0;
while (done < bytes.len) {
const chunk = bytes[done..][0..@min(bytes.len - done, if (o.iounit != 0) o.iounit else bytes.len)];
const n = try f.write(o.fid, done, chunk);
if (n == 0) return f.refuse("short write");
done += n;
}
}
/// Reads from the start until a read comes back empty, each answer into
/// `out` and flushed.
fn drain(f: *Fsys, o: Opened, out: *Io.Writer) (Error || Io.Writer.Error)!void {
var at: u64 = 0;
while (true) {
const data = try f.read(o.fid, at, if (o.iounit != 0) o.iounit else msize);
if (data.len == 0) return;
try out.writeAll(data);
try out.flush();
at += data.len;
}
}
/// A new fid for `path`. Its walk (the last 16 names of it) is sent and
/// not waited for: the next `rpc` sends its request behind it and checks
/// its answer first. Names before the last 16 are walked a round trip
/// each, the fid stepping from itself.
fn walkAhead(f: *Fsys, path: []const u8) Error!u32 {
f.words_len = 0;
const fid = f.next_fid;
f.next_fid = if (fid == std.math.maxInt(u32) - 1) 1 else fid + 1;
var it = std.mem.tokenizeScalar(u8, path, '/');
var from: u32 = 0;
while (true) {
var names: [max_welem][]const u8 = undefined;
var n: usize = 0;
while (n < names.len) : (n += 1) names[n] = it.next() orelse break;
if (it.peek() == null) {
f.walk_tag = f.cl.submit(.{ .walk = .{ .fid = from, .newfid = fid, .names = names[0..n] } }) catch return error.Botch;
f.walk_asked = n;
return fid;
}
const w = f.rpc(.{ .walk = .{ .fid = from, .newfid = fid, .names = names[0..n] } }) catch |err| {
if (from != 0) f.clunk(fid);
return err;
};
if (w.walk.nwqid < n) {
if (from != 0) f.clunk(fid);
return f.refuse(fs.errString(fs.E.NOENT));
}
from = fid;
}
}
/// Sends what the client holds, then reads until `req` is answered. On
/// the way, the answer of a walk sent ahead is checked (a refusal or a
/// walk short of its names is this call's refusal: the request behind it
/// had no fid) and those of unwaited clunks are dropped.
fn rpc(f: *Fsys, req: Client.Request) Error!Client.Result {
if (f.walk_tag == wire.notag) f.words_len = 0;
const tag = f.cl.submit(req) catch {
// A request that cannot be made (a name too long for a message):
// the walk ahead of it still goes, its answer not waited for.
if (f.walk_tag < 32) f.unwaited |= @as(u32, 1) << @intCast(f.walk_tag);
f.walk_tag = wire.notag;
return error.Botch;
};
try f.ship();
const deadline = f.timeout.toDeadline(f.io);
while (true) {
const done = try f.next(deadline);
if (f.walk_tag != wire.notag and done.tag == f.walk_tag) {
f.walk_tag = wire.notag;
switch (done.result) {
.fail => |words| f.keep(words),
.walk => |w| if (w.nwqid < f.walk_asked) f.keep(fs.errString(fs.E.NOENT)),
else => return error.Botch,
}
continue;
}
if (done.tag == tag) {
if (f.words_len != 0) return error.Refused;
return switch (done.result) {
.fail => |words| f.refuse(words),
else => done.result,
};
}
if (done.tag < 32 and f.unwaited & @as(u32, 1) << @intCast(done.tag) != 0) {
f.unwaited &= ~(@as(u32, 1) << @intCast(done.tag));
continue;
}
return error.Botch;
}
}
/// The next answer, read from the socket until `deadline`; its slices
/// are good until the next.
fn next(f: *Fsys, deadline: Io.Timeout) Error!Client.Done {
while (true) {
if (f.cl.take()) |done| return done;
if (f.cl.dead) return error.Botch;
// take() found no whole frame and the client has dropped the last
// one, so the free end of its input is where the next bytes go
// (what `push` would copy them to).
var room = [1][]u8{f.cl.in[f.cl.in_len..]};
if (room[0].len == 0) return error.Botch;
const n = ((f.io.operateTimeout(.{ .net_read = .{ .socket_handle = f.stream.socket.handle, .data = &room } }, deadline) catch |err| switch (err) {
error.Timeout => return error.Timeout,
else => return error.Hangup,
}).net_read catch return error.Hangup).data_len;
if (n == 0) return error.Hangup;
f.cl.in_len += n;
}
}
fn ship(f: *Fsys) Error!void {
var w = f.stream.writer(f.io, &.{});
const bytes = f.cl.output();
w.interface.writeAll(bytes) catch return error.Hangup;
f.cl.wrote(bytes.len);
}
/// Keeps `words` as the refusal, unless one is kept already (the walk's,
/// before the request it failed).
fn keep(f: *Fsys, words: []const u8) void {
if (f.words_len != 0) return;
f.words_len = @min(words.len, f.words.len);
@memcpy(f.words[0..f.words_len], words[0..f.words_len]);
}
fn refuse(f: *Fsys, words: []const u8) error{Refused} {
f.keep(words);
return error.Refused;
}
// ---- tests: against cloud9's own engine, served by `serve.Runner` ----
const testing = std.testing;
const TestTree = @import("TestTree.zig");
const no_env = [_:null]?[*:0]const u8{};
const Dest = enum { unix, tcp };
fn dialed(t: *TestTree, dest: Dest) !*Fsys {
const f = try testing.allocator.create(Fsys);
errdefer testing.allocator.destroy(f);
var buf: [128]u8 = undefined;
const address = switch (dest) {
.unix => try std.fmt.bufPrint(&buf, "unix!{s}", .{t.unix}),
.tcp => try std.fmt.bufPrint(&buf, "tcp!127.0.0.1!{d}", .{t.tcp.getPort()}),
};
try f.dial(testing.io, &no_env, address, "glenda");
return f;
}
fn hangUp(f: *Fsys) void {
f.close();
testing.allocator.destroy(f);
}
fn readAll(f: *Fsys, path: []const u8) ![]u8 {
var out: Io.Writer.Allocating = .init(testing.allocator);
errdefer out.deinit();
try f.readFile(path, &out.writer);
return out.toOwnedSlice();
}
test "fsys: whole files, stats and walks over a Unix socket and TCP" {
const t = try testing.allocator.create(TestTree);
defer testing.allocator.destroy(t);
try t.start();
defer t.stop();
for ([_]Dest{ .unix, .tcp }) |dest| {
const f = try dialed(t, dest);
defer hangUp(f);
const readme = try readAll(f, "README");
defer testing.allocator.free(readme);
try testing.expectEqualStrings(TestTree.readme, readme);
// Longer than a message (the server's is 8192): read on to the end.
const big = try readAll(f, "/big");
defer testing.allocator.free(big);
try testing.expectEqual(TestTree.big_len, big.len);
for (big, 0..) |b, i| if (b != TestTree.bigByte(i)) return error.TestUnexpectedResult;
try testing.expectEqualStrings("deep", (try f.stat("sub/deep")).name);
try testing.expectEqual(@as(u64, TestTree.readme.len), (try f.stat("README")).length);
// More names than a walk carries: the first 16 a round trip of their own.
try testing.expectEqualStrings("deep", (try f.stat("sub/../sub/../sub/../sub/../sub/../sub/../sub/../sub/../sub/../sub/deep")).name);
try testing.expectError(error.Refused, f.stat("nowhere"));
try testing.expectEqualStrings("No such file or directory", f.said());
// A walk short of its names is refused in the same words.
try testing.expectError(error.Refused, f.stat("sub/nowhere"));
try testing.expectEqualStrings("No such file or directory", f.said());
try testing.expectError(error.Refused, f.stat("sub/../sub/../sub/../sub/../sub/../sub/../sub/../sub/../sub/../nowhere/deep"));
// Refusals leave the connection as good as before.
try testing.expectEqualStrings("README", (try f.stat("README")).name);
}
}
test "fsys: a refused write's words reach the caller" {
const t = try testing.allocator.create(TestTree);
defer testing.allocator.destroy(t);
try t.start();
defer t.stop();
const f = try dialed(t, .unix);
defer hangUp(f);
try testing.expectError(error.Refused, f.writeFile("ctl", "bogus\n"));
try testing.expectEqualStrings("unknown command: bogus", f.said());
try f.writeFile("ctl", "ok\n");
const ctl = try readAll(f, "ctl");
defer testing.allocator.free(ctl);
try testing.expectEqualStrings("ok\n", ctl);
try testing.expectError(error.Refused, f.writeFile("README", "x"));
try testing.expectEqualStrings("permission denied", f.said());
}
test "fsys: a short read is not the end of the file" {
const t = try testing.allocator.create(TestTree);
defer testing.allocator.destroy(t);
try t.start();
defer t.stop();
const f = try dialed(t, .unix);
defer hangUp(f);
const short = try readAll(f, "short");
defer testing.allocator.free(short);
try testing.expectEqualStrings("a short answer, three bytes a read", short);
// One Tread is one answer, a short one included.
const o = try f.open("short", c9.oread);
defer f.clunk(o.fid);
try testing.expectEqualStrings("a s", try f.read(o.fid, 0, 100));
}
test "fsys: ask reads the answer on the open it wrote, from the start" {
const t = try testing.allocator.create(TestTree);
defer testing.allocator.destroy(t);
try t.start();
defer t.stop();
const f = try dialed(t, .unix);
defer hangUp(f);
var out: Io.Writer.Allocating = .init(testing.allocator);
defer out.deinit();
try f.ask("echo", "hello\n", &out.writer);
try testing.expectEqualStrings("heard hello\n", out.written());
}
test "fsys: a read the server holds streams three chunks, then its end" {
const t = try testing.allocator.create(TestTree);
defer testing.allocator.destroy(t);
try t.start();
defer t.stop();
const f = try dialed(t, .unix);
defer hangUp(f);
const Feeder = struct {
fn run(tree: *TestTree) anyerror!void {
for ([_][]const u8{ "new 1\n", "del 2\n", "new 3\n", "" }) |chunk| try tree.feed(chunk);
}
};
var feeder = try testing.io.concurrent(Feeder.run, .{t});
defer _ = feeder.cancel(testing.io) catch {};
// Each chunk is flushed as it comes: a writer that marks its flushes.
const Marked = struct {
w: Io.Writer,
marks: [8]usize = undefined,
n: usize = 0,
fn flush(w: *Io.Writer) Io.Writer.Error!void {
const m: *@This() = @fieldParentPtr("w", w);
m.marks[m.n] = w.end;
m.n += 1;
}
};
var store: [64]u8 = undefined;
var marked: Marked = .{ .w = .{ .vtable = &.{ .drain = Io.Writer.fixedDrain, .flush = Marked.flush }, .buffer = &store } };
const out = &marked.w;
try f.ask("event", "follow\n", out);
try testing.expectEqualStrings("new 1\ndel 2\nnew 3\n", out.buffered());
try testing.expectEqualSlices(usize, &.{ 6, 12, 18 }, marked.marks[0..marked.n]);
try feeder.await(testing.io);
}
test "fsys: a server that goes away mid-read is a hangup" {
const t = try testing.allocator.create(TestTree);
defer testing.allocator.destroy(t);
try t.start();
defer t.stop();
const f = try dialed(t, .unix);
defer hangUp(f);
const Feeder = struct {
fn run(tree: *TestTree) anyerror!void {
try tree.feed("first\n");
try tree.feed(null);
}
};
var feeder = try testing.io.concurrent(Feeder.run, .{t});
defer _ = feeder.cancel(testing.io) catch {};
var store: [64]u8 = undefined;
var out: Io.Writer = .fixed(&store);
try testing.expectError(error.Hangup, f.readFile("event", &out));
try testing.expectEqualStrings("first\n", out.buffered());
try feeder.await(testing.io);
}
test "fsys: a server that says nothing is a timeout, not a caller held for ever" {
const t = try testing.allocator.create(TestTree);
defer testing.allocator.destroy(t);
try t.start();
defer t.stop();
const f = try dialed(t, .unix);
defer hangUp(f);
try testing.expectEqual(default_timeout, f.timeout);
// `event`'s reads are held until fed, and nothing feeds this one.
f.timeout = .{ .duration = .{ .raw = .fromMilliseconds(100), .clock = .awake } };
var store: [64]u8 = undefined;
var out: Io.Writer = .fixed(&store);
const began = Io.Clock.Timestamp.now(testing.io, .awake);
try testing.expectError(error.Timeout, f.readFile("event", &out));
const waited = began.durationTo(Io.Clock.Timestamp.now(testing.io, .awake)).raw.toMilliseconds();
// poll(2) counts whole milliseconds, and may wake one early.
try testing.expect(waited >= 90 and waited < 5000);
}
test "fsys: create, write, remove" {
const t = try testing.allocator.create(TestTree);
defer testing.allocator.destroy(t);
try t.start();
defer t.stop();
const f = try dialed(t, .unix);
defer hangUp(f);
const o = try f.create("/new", 0o600, c9.owrite);
try testing.expectEqual(@as(u32, 3), try f.write(o.fid, 0, "abc"));
f.clunk(o.fid);
try testing.expectError(error.Refused, f.create("new", 0o600, c9.owrite));
try testing.expectEqualStrings("File exists", f.said());
try f.remove("new");
try testing.expectError(error.Refused, f.stat("new"));
try testing.expectError(error.Refused, f.remove("README"));
// A name no message holds is not sent, and the walk sent ahead of it
// does not stand in the way of the next call.
const long: [9000]u8 = @splat('x');
try testing.expectError(error.Botch, f.create(&long, 0o600, c9.owrite));
try testing.expectEqualStrings("README", (try f.stat("README")).name);
}
test "fsys: a directory read decodes as entries" {
const t = try testing.allocator.create(TestTree);
defer testing.allocator.destroy(t);
try t.start();
defer t.stop();
const f = try dialed(t, .unix);
defer hangUp(f);
const o = try f.open("", c9.oread);
defer f.clunk(o.fid);
try testing.expect(o.qid.type & wire.qtdir != 0);
var names: [8][]const u8 = undefined;
var n: usize = 0;
var entries: Entries = .{ .bytes = try f.read(o.fid, 0, msize) };
while (try entries.next()) |st| : (n += 1) names[n] = st.name;
try testing.expectEqual(@as(usize, 7), n);
try testing.expectEqualStrings("README", names[0]);
try testing.expectEqualStrings("sub", names[6]);
}
test "fsys: dial by a name posted in the registry, and refuse a name that climbs out of it" {
var dir = testing.tmpDir(.{});
defer dir.cleanup();
var real: [std.fs.max_path_bytes]u8 = undefined;
const len = try dir.dir.realPath(testing.io, &real);
var var_buf: [std.fs.max_path_bytes + 32]u8 = undefined;
const runtime = try std.fmt.bufPrintSentinel(&var_buf, "XDG_RUNTIME_DIR={s}", .{real[0..len]}, 0);
const env = [_:null]?[*:0]const u8{runtime.ptr};
const t = try testing.allocator.create(TestTree);
defer testing.allocator.destroy(t);
try t.start();
defer t.stop();
try t.runner.listenPosted(&env, "c9test", 4);
const f = try testing.allocator.create(Fsys);
defer testing.allocator.destroy(f);
try f.dial(testing.io, &env, "c9test", "glenda");
defer f.close();
try testing.expectEqualStrings("README", (try f.stat("README")).name);
var g: Fsys = undefined;
try testing.expectError(error.BadAddress, g.dial(testing.io, &env, "../c9test", "glenda"));
try testing.expectError(error.NotRuntimeDir, g.dial(testing.io, &no_env, "c9test", "glenda"));
}
|