about summary refs log tree commit diff
path: root/src/main.zig
blob: b1be499348d0d1da258a30fa595ef6abc6a91086 (plain) (blame)
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
const std = @import("std");
const Io = std.Io;
const Allocator = std.mem.Allocator;
const linux = std.os.linux;

const Digester = std.crypto.hash.Md5;
pub const Digest = [16]u8;

const log = std.log.scoped(.rbc);

const BUFFER_SIZE = 1024;

// tune log options
pub const std_options = std.Options{
    .log_level = if (@import("builtin").mode == .Debug) .debug else .info,
};

pub fn main(init: std.process.Init) !void {
    try Cli.run(init);
}

// TODO: make this not a global
var FORCE: bool = false;

const Cli = struct {
    fn usage() void {
        std.debug.print("usage: rbc SOURCE TARGET-DIR\n\n", .{});
        std.debug.print("Writes SOURCE into TARGET-DIR, resolving references to other files in ROOT (= parent dir of SOURCE).\n", .{});
        std.debug.print("\n", .{});
    }

    fn run(init: std.process.Init) !void {
        const io = init.io;
        const gpa = init.gpa;

        const args = try init.minimal.args.toSlice(gpa);
        defer gpa.free(args);

        if (init.environ_map.get("RBC_FORCE")) |_| FORCE = true;

        // parse options
        for (args) |arg| {
            if (std.mem.eql(u8, arg, "help")) return Cli.usage();
            if (std.mem.eql(u8, arg, "--help")) return Cli.usage();
            if (std.mem.eql(u8, arg, "-h")) return Cli.usage();
        }

        if (args.len < 3) return Cli.usage();

        // TODO: flag handling

        const source_path = args[1];
        const target_path = args[2];

        log.info("target={s} source={s} ", .{ target_path, source_path });

        // TODO: allow setting custom root path via flags
        const root_path = std.fs.path.dirname(source_path) orelse return error.NoDirname;
        const root_dir = try Io.Dir.cwd().openDir(io, root_path, .{});
        defer root_dir.close(io);

        var source = try File.init(io, root_dir, std.fs.path.basename(source_path), null);
        defer source.close(io);

        // open/create destination dir
        const target_dir = try Io.Dir.cwd().createDirPathOpen(io, target_path, .{});
        defer target_dir.close(io);

        const b = try Builder.init(gpa, root_dir, source, target_dir);
        defer b.deinit(gpa);
        try b.build(io, gpa);
    }
};

const File = struct {
    path: []const u8,
    fd: Io.File,
    mmap: Io.File.MemoryMap,
    mtime: Io.Timestamp,

    fn init(io: Io, dir: Io.Dir, path: []const u8, options: ?Io.Dir.OpenFileOptions) !File {
        const fd = try dir.openFile(io, path, options orelse .{
            .lock = .exclusive,
            .mode = .read_write,
        });
        var mmap = try fd.createMemoryMap(io, .{
            .len = try fd.length(io),
            // .protection = .{ .read = true, .write = false },
        });
        try mmap.read(io);
        const stat = try fd.stat(io);
        return .{
            .path = path,
            .fd = fd,
            .mmap = mmap,
            .mtime = stat.mtime,
        };
    }

    fn digest(self: File) !Digest {
        var buffer: [Digester.digest_length]u8 = undefined;
        var d = Digester.init(.{});
        d.update(self.mmap.memory);
        var buf2: [BUFFER_SIZE]u8 = undefined;
        d.update(try std.fmt.bufPrint(&buf2, "{d}", .{self.mtime}));
        d.final(&buffer);
        return buffer;
    }

    fn close(self: *File, io: Io) void {
        self.mmap.destroy(io);
        self.fd.close(io);
    }
};

const Builder = struct {
    root_dir: Io.Dir,
    source: File,
    target_dir: Io.Dir,
    options: Options,

    // track files which affect build output (paths relative to root_dir)
    parts: std.ArrayList([]const u8),
    // track component digests which should affect digest check
    dgsts: std.ArrayList(Digest),

    pub const Options = struct {
        // TODO: this is a bit of a hack no?
        offset_from_siteroot: ?[]const u8,
    };

    const DELIM_START: []const u8 = "{[";
    const DELIM_END: []const u8 = "]}";

    fn init(gpa: Allocator, root_dir: Io.Dir, source: File, target_dir: Io.Dir) !*Builder {
        const b = try gpa.create(Builder);
        b.* = .{
            .root_dir = root_dir,
            .source = source,
            .target_dir = target_dir,
            .options = .{
                .offset_from_siteroot = "/site",
            },
            .parts = .empty,
            .dgsts = .empty,
        };
        return b;
    }

    fn deinit(self: *Builder, gpa: Allocator) void {
        for (self.parts.items) |p| gpa.free(p);
        self.parts.deinit(gpa);
        self.dgsts.deinit(gpa);
        gpa.destroy(self);
    }

    fn build(self: *Builder, io: Io, gpa: Allocator) !void {
        log.debug("{s}: building", .{self.source.path});

        // track source
        try self.parts.append(gpa, try gpa.dupe(u8, self.source.path));
        try self.dgsts.append(gpa, try self.source.digest());

        const source_stem = std.fs.path.stem(self.source.path);

        // handle meta pages before even creating target_path
        if (std.mem.startsWith(u8, source_stem, "meta.tilde")) {
            return try self.metaTilde(io, gpa);
        }

        // target file
        const target_path = try std.fmt.allocPrint(gpa, "{s}.html", .{source_stem});
        defer gpa.free(target_path);

        // atomic?
        const target_fd = try self.target_dir.createFile(io, target_path, .{
            .lock = .exclusive,
        });
        defer target_fd.close(io);

        // check if we need to do any work
        if (try targetExists(io, gpa, target_fd, self.root_dir)) {
            defer log.debug("{s}: done", .{self.source.path});
            return; // skip any further processing
        }

        log.debug("{s}: writing", .{self.source.path});

        var writer_buf: [BUFFER_SIZE]u8 = undefined;
        var writer = target_fd.writer(io, &writer_buf);

        // determine if this is a fragment and exit if not
        if (try isFullPage(self.source)) {
            // only copy source to target
            try writer.interface.writeAll(self.source.mmap.memory);
        } else {
            // load baseof fragment
            var baseof = try File.init(io, self.root_dir, "meta.baseof.htm", null);
            defer baseof.close(io);

            try self.inject(io, gpa, baseof, &writer.interface);
        }

        try self.finalize(gpa, target_fd);
        defer log.debug("{s}: done", .{self.source.path});
    }

    // finalize computes the digest and attaches it to the target file
    fn finalize(self: *Builder, gpa: Allocator, target_fd: Io.File) !void {
        log.debug("{s}: finalizing", .{self.source.path});

        const composite_dgst = compositeDigest(self.dgsts.items);
        try setXattrSourceDigest(target_fd, composite_dgst);
        try setXattrSourceComponents(gpa, target_fd, self.parts.items);
    }

    // inject interprets source and writes to w
    fn inject(self: *Builder, io: Io, gpa: Allocator, source: File, w: *Io.Writer) !void {
        var iter = std.mem.splitScalar(u8, source.mmap.memory, '\n');
        while (iter.next()) |line| {
            var offset: usize = 0;
            parseLn: while (true) {
                if (std.mem.findPos(u8, line, offset, DELIM_START)) |start| {
                    if (std.mem.findPos(u8, line, offset + start + 1, DELIM_END)) |end| {
                        // write out beginning of chunk
                        try w.writeAll(line[offset..start]);

                        // do templating
                        const ref_name = line[(offset + start + DELIM_START.len)..end];
                        try self.template(io, gpa, ref_name, w);

                        // advance offset
                        offset += end + DELIM_END.len;
                    } else break :parseLn;
                } else break :parseLn;
            }

            // write out remaining chunk
            try w.writeAll(line[offset..]);
            try w.writeByte('\n');
        }
        try w.flush();

        // track injected file
        try self.parts.append(gpa, try gpa.dupe(u8, source.path));
        try self.dgsts.append(gpa, try source.digest());
    }

    fn template(self: *Builder, io: Io, gpa: Allocator, ref_name: []const u8, w: *Io.Writer) anyerror!void {
        log.debug("templating ref={s}", .{ref_name});

        // special case for when we're injecting the source into the baseof
        if (std.mem.eql(u8, ref_name, "builtin.fragment")) {
            try w.writeByte('\n');
            try self.inject(io, gpa, self.source, w);
            try w.writeByte('\n');
            return;
        }
        // builtin variables
        if (std.mem.eql(u8, ref_name, "builtin.title")) {
            const title = try formatName(gpa, std.fs.path.stem(self.source.path));
            defer gpa.free(title);
            try w.writeAll(title);
            return;
        }
        if (std.mem.eql(u8, ref_name, "builtin.path")) {
            try w.writeAll(self.source.path);
            return;
        }
        // TODO: other builtins?

        switch (ref_name[0]) {
            '/' => try self.templatePortal(io, gpa, ref_name[1..], w),
            '$' => return try self.templateLink(gpa, ref_name[1..], w),
            '+' => return try self.templateName(gpa, ref_name[1..], w),
            else => try self.templateRef(io, gpa, ref_name, w),
        }
    }

    fn templatePortal(self: *Builder, io: Io, gpa: Allocator, ref_name: []const u8, w: *Io.Writer) !void {
        log.debug("portaling ref={s}", .{ref_name});

        const ref_path = try std.fmt.allocPrint(gpa, "{s}.htm", .{ref_name});
        defer gpa.free(ref_path);

        var ref = try File.init(io, self.root_dir, ref_path, null);
        defer ref.close(io);

        try w.writeByte('\n');
        try self.inject(io, gpa, ref, w);
        try w.writeByte('\n');
    }

    fn templateRef(self: *Builder, io: Io, gpa: Allocator, ref_str: []const u8, w: *Io.Writer) !void {
        const fragment = std.mem.indexOf(u8, ref_str, "#");
        const ref = if (fragment) |i| ref_str[0..i] else ref_str;

        log.debug("referencing ref={s}", .{ref});

        const ref_path = try std.fmt.allocPrint(gpa, "{s}.html", .{ref});
        defer gpa.free(ref_path);

        // check ref'ed page can exist
        {
            const ref_htm = try std.fmt.allocPrint(gpa, "{s}.htm", .{ref});
            defer gpa.free(ref_htm);

            const possible_paths = [_][]const u8{ ref_path, ref_htm };
            var found = false;
            for (possible_paths) |p|
                found = found or (blk: {
                    self.root_dir.access(io, p, .{}) catch break :blk false;
                    break :blk true;
                });
            if (!found) {
                log.err("{s} not found", .{ref});
                return error.FileNotFound;
            }
        }

        const ref_name = try formatName(gpa, ref);
        defer gpa.free(ref_name);
        const ref_link = try formatLink(gpa, ref, self.options.offset_from_siteroot);
        defer gpa.free(ref_link);

        try w.print(
            \\<a href="{s}{s}">{s}</a>
        , .{
            ref_link,
            if (fragment) |i| ref_str[i..] else "",
            ref_name,
        });
    }

    fn templateLink(self: *Builder, gpa: Allocator, ref_name: []const u8, w: *std.Io.Writer) !void {
        const link = try formatLink(gpa, ref_name, self.options.offset_from_siteroot);
        defer gpa.free(link);
        try w.writeAll(link);
    }

    fn templateName(_: *Builder, gpa: Allocator, ref_name: []const u8, w: *std.Io.Writer) !void {
        const name = try formatName(gpa, ref_name);
        defer gpa.free(name);
        try w.writeAll(name);
    }

    // meta_tilde implements
    fn metaTilde(self: *Builder, io: Io, gpa: Allocator) !void {
        const source_stem = std.fs.path.stem(self.source.path)[11..]; // trim "meta.tilde."
        log.debug("meta.tilde ~{s}", .{source_stem});

        const content = std.mem.trim(u8, self.source.mmap.memory, "\n\t\r");
        const target_name = try std.fmt.allocPrint(gpa, "~{s}", .{source_stem});
        defer gpa.free(target_name);

        const target_fd = try self.target_dir.createFile(io, target_name, .{ .exclusive = true });
        if (try targetExists(io, gpa, target_fd, self.root_dir)) return;

        var buffer: [BUFFER_SIZE]u8 = undefined;
        var writer = target_fd.writer(io, &buffer);

        try writer.interface.print(
            \\<!DOCTYPE html>
            \\<html lang="en">
            \\<head>
            \\<meta charset="utf-8">
            \\<meta http-equiv="refresh" content="0; url={[LINK]s}">
            \\<meta name="fediverse:creator" content="@robertgzr@toot.berlin">
            \\<link rel="stylesheet" type="text/css" href="links/main.css">
            \\</head>
            \\<body>
            \\<noscript>Continue to <a href="{[LINK]s}">{[LINK]s}</a>.</noscript>
            \\<script>
            \\'use strict'
            \\window.location.replace("{[LINK]s}")
            \\</script>
            \\</body>
            \\</html>
            \\
        , .{
            .LINK = content,
        });
        try writer.interface.flush();

        try self.finalize(gpa, target_fd);
        log.debug("{s}: done", .{self.source.path});
    }
};

// helpers

fn formatName(gpa: Allocator, name: []const u8) ![]u8 {
    return try std.mem.replaceOwned(u8, gpa, name, "_", " ");
}

fn formatLink(gpa: Allocator, name: []const u8, offset: ?[]const u8) ![]u8 {
    var link = try std.fmt.allocPrint(gpa, "{s}.html", .{name});
    if (offset) |_| {
        const link2 = try std.fs.path.join(gpa, &.{ offset.?, link });
        gpa.free(link);
        link = link2;
    }
    return link;
}

fn targetExists(io: Io, gpa: Allocator, target_fd: Io.File, root_dir: Io.Dir) !bool {
    if (FORCE) return false;

    const target_parts = getXattrSourceComponents(gpa, target_fd) catch |err|
        switch (err) {
            error.XattrNotFound => return false,
            else => return err,
        };
    defer gpa.free(target_parts);
    defer for (target_parts) |p| gpa.free(p);

    const target_dgst = getXattrSourceDigest(gpa, target_fd) catch |err|
        switch (err) {
            error.XattrNotFound => return false,
            else => return err,
        };

    // create digests for parts
    var actual_dgsts: std.ArrayList(Digest) = .empty;
    defer actual_dgsts.deinit(gpa);
    for (target_parts) |p| {
        var f = try File.init(io, root_dir, p, .{ .mode = .read_write });
        defer f.close(io);
        try actual_dgsts.append(gpa, try f.digest());
    }
    const actual_dgst = compositeDigest(actual_dgsts.items);
    return std.mem.eql(u8, &actual_dgst, &target_dgst);
}

fn isFullPage(source: File) !bool {
    // exit early if extension is .html vs .htm
    const ext = std.fs.path.extension(source.path);
    if (std.mem.eql(u8, ext, ".html")) return true;

    // peek if file starts with <html>
    const buf = source.mmap.memory[0..6];
    return std.mem.eql(u8, buf, "<html>");
}

fn compositeDigest(parts: []Digest) Digest {
    std.sort.block(Digest, parts, {}, comptime struct {
        pub fn inner(_: void, a: Digest, b: Digest) bool {
            return std.mem.lessThan(u8, &a, &b);
        }
    }.inner);

    var buffer: [Digester.digest_length]u8 = undefined;
    var d = Digester.init(.{});
    for (parts) |dgst| d.update(&dgst);
    d.final(&buffer);
    return buffer;
}

// xattr code
// TODO: fallback to cache dir when xattr isnt supported

fn setXattr(fd: Io.File, key: [:0]const u8, val: []const u8) !void {
    const rc = linux.fsetxattr(fd.handle, key.ptr, val.ptr, val.len, 0);
    switch (linux.errno(rc)) {
        .SUCCESS => {},
        .OPNOTSUPP => return error.XattrNotSupported,
        .NODATA => return error.XattrNotFound,
        else => return error.XattrError,
    }
}

fn getXattr(gpa: Allocator, fd: Io.File, key: [:0]const u8) ![]u8 {
    var buffer: [BUFFER_SIZE]u8 = undefined;
    const rc = linux.fgetxattr(fd.handle, key.ptr, &buffer, buffer.len);
    switch (linux.errno(rc)) {
        .SUCCESS => {},
        .OPNOTSUPP => return error.XattrNotSupported,
        .NODATA => return error.XattrNotFound,
        else => {
            log.err("errno: {d}", .{linux.errno(rc)});
            return error.XattrError;
        },
    }
    return try gpa.dupe(u8, buffer[0..@intCast(rc)]);
}

const XATTR_SOURCE_DIGEST = "user.rbc-source-digest";
const XATTR_SOURCE_COMPONENTS = "user.rbc-source-components";

fn setXattrSourceDigest(fd: Io.File, dgst: Digest) !void {
    return try setXattr(fd, XATTR_SOURCE_DIGEST, &dgst);
}

fn getXattrSourceDigest(gpa: Allocator, fd: Io.File) !Digest {
    const val = try getXattr(gpa, fd, XATTR_SOURCE_DIGEST);
    defer gpa.free(val);
    return val[0..16].*;
}

fn setXattrSourceComponents(gpa: Allocator, fd: Io.File, parts: []const []const u8) !void {
    const val = try std.mem.join(gpa, ";", parts);
    defer gpa.free(val);
    return try setXattr(fd, XATTR_SOURCE_COMPONENTS, val);
}

fn getXattrSourceComponents(gpa: Allocator, fd: Io.File) ![][]const u8 {
    const val = try getXattr(gpa, fd, XATTR_SOURCE_COMPONENTS);
    defer gpa.free(val);

    var parts: std.ArrayList([]const u8) = .empty;
    errdefer parts.deinit(gpa);

    var iter = std.mem.splitScalar(u8, val, ';');
    while (iter.next()) |p| try parts.append(gpa, try gpa.dupe(u8, p));
    return try parts.toOwnedSlice(gpa);
}

// tests

pub const Testing = if (@import("builtin").is_test)
    struct {
        pub fn shell(
            io: std.Io,
            command: []const u8,
            opts: struct {
                cwd: std.process.Child.Cwd = .inherit,
                environ_map: ?*const std.process.Environ.Map = null,
                progress_node: std.Progress.Node = std.Progress.Node.none,
            },
        ) !void {
            log.debug("Running Shell <<< {s}", .{command});

            var p = try std.process.spawn(io, .{
                .argv = &.{ "sh", "-c", command },
                .cwd = opts.cwd,
                .stdin = .ignore,
                .stderr = .inherit,
                .stdout = .inherit,
                .environ_map = opts.environ_map,
                .progress_node = opts.progress_node,
            });

            const result = try p.wait(io);
            if (result.exited != 0) return error.NonZeroExitCode;
        }

        pub fn parse_hash(comptime str: []const u8) !Digest {
            var buffer: [BUFFER_SIZE]u8 = undefined;
            var out = try std.fmt.hexToBytes(&buffer, str);
            return out[0..Digester.digest_length].*;
        }
    };

test "xattr" {
    std.testing.log_level = .debug;

    const io = std.testing.io;
    const gpa = std.testing.allocator;

    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();

    const fd = try tmp.dir.createFile(io, &tmp.sub_path, .{});

    // test generic funcs
    {
        try setXattr(fd, "user.foo", "bar");
        const val = try getXattr(gpa, fd, "user.foo");
        defer gpa.free(val);
        try std.testing.expectEqualStrings("bar", val);
    }

    // test digest func
    {
        const dgst = try Testing.parse_hash("accd9547a71463f530ae12a8dd0d7696");
        try setXattrSourceDigest(fd, dgst);
        const val = try getXattrSourceDigest(gpa, fd);
        try std.testing.expectEqualStrings(&dgst, &val);
    }

    // test paths func
    {
        const parts = &[_][]const u8{ "foo.htm", "bar.htm" };
        try setXattrSourceComponents(gpa, fd, parts);
        const val = try getXattrSourceComponents(gpa, fd);
        defer gpa.free(val);
        defer for (val) |v| gpa.free(v);
        try std.testing.expectEqual(parts.len, val.len);
        for (parts, 0..) |p, i| try std.testing.expectEqualStrings(p, val[i]);
    }
}

test "targetExists" {
    std.testing.log_level = .debug;

    const io = std.testing.io;
    const gpa = std.testing.allocator;

    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();

    var dgsts: std.ArrayList(Digest) = .empty;
    defer dgsts.deinit(gpa);

    const source_fd = try tmp.dir.createFile(io, "source.htm", .{});
    try source_fd.writePositionalAll(io, "hello world", 0);
    const source = try File.init(io, tmp.dir, "source.htm", null);
    try dgsts.append(gpa, try source.digest());

    const ref_fd = try tmp.dir.createFile(io, "ref.htm", .{});
    try ref_fd.writePositionalAll(io, "foobar", 0);
    const ref = try File.init(io, tmp.dir, "ref.htm", null);
    try dgsts.append(gpa, try ref.digest());

    const dgst = compositeDigest(dgsts.items);

    const target_fd = try tmp.dir.createFile(io, "target.htm", .{});
    try setXattrSourceDigest(target_fd, dgst);
    try setXattrSourceComponents(gpa, target_fd, &.{
        source.path,
        ref.path,
    });

    const exists = try targetExists(io, gpa, target_fd, tmp.dir);
    try std.testing.expectEqual(true, exists);
}

test "e2e" {
    std.testing.log_level = .debug;

    const io = std.testing.io;
    const gpa = std.testing.allocator;

    const Tc = struct {
        const Self = @This();

        source: File,
        root_dir: Io.Dir,

        expect_path: []const u8,
        expect_dgst: Digest,

        fn init(path: []const u8, expect_path: []const u8, expect_dgst: Digest) !Self {
            const root_dir = try std.Io.Dir.cwd().openDir(io, std.fs.path.dirname(path).?, .{});
            return .{
                .root_dir = root_dir,
                .source = try File.init(io, root_dir, std.fs.path.basename(path), null),
                .expect_path = expect_path,
                .expect_dgst = expect_dgst,
            };
        }

        fn run(tc: *Self, dest: Io.Dir) !void {
            std.debug.print("=== {s} =============================\n", .{tc.source.path});

            defer tc.root_dir.close(io);
            defer tc.source.close(io);

            const b = try Builder.init(gpa, tc.root_dir, tc.source, dest);
            defer b.deinit(gpa);
            try b.build(
                io,
                gpa,
            );

            const fd = try dest.openFile(io, tc.expect_path, .{ .mode = .read_write, .lock = .exclusive });
            defer fd.close(io);
            const xattr_dgst = try getXattrSourceDigest(gpa, fd);

            log.debug("{x} == {x}", .{ tc.expect_dgst, xattr_dgst });
            if (!std.mem.eql(u8, &tc.expect_dgst, &xattr_dgst)) {
                std.debug.print("====== expected this digest: =========\n", .{});
                std.debug.print("{x}\n\n", .{tc.expect_dgst});
                std.debug.print("======== instead found this: =========\n", .{});
                std.debug.print("{x}\n\n", .{xattr_dgst});
                std.debug.print("======================================\n", .{});
                return error.TestExpectedEqual;
            }

            try Testing.shell(io, "tree", .{ .cwd = .{ .dir = dest } });
            const cmd = try std.fmt.allocPrint(gpa, "cat {s}", .{tc.expect_path});
            defer gpa.free(cmd);
            try Testing.shell(io, cmd, .{ .cwd = .{ .dir = dest } });
        }
    };

    var tmp = std.testing.tmpDir(.{ .iterate = true });
    defer tmp.cleanup();

    {
        var tc = try Tc.init(
            "./testdata/only-copy.html",
            "only-copy.html",
            try Testing.parse_hash("eae5ff7df7c71f8bbeda89302f9fe721"),
        );
        try tc.run(tmp.dir);
    }
    {
        var tc = try Tc.init(
            "./testdata/only-copy.html",
            "only-copy.html",
            try Testing.parse_hash("eae5ff7df7c71f8bbeda89302f9fe721"),
        );
        try tc.run(tmp.dir);
    }
    {
        var tc = try Tc.init(
            "./testdata/fragment.htm",
            "fragment.html",
            try Testing.parse_hash("a5c099fac15ea9ea9928712fa6229702"),
        );
        try tc.run(tmp.dir);
    }
    {
        var tc = try Tc.init(
            "./testdata/page.htm",
            "page.html",
            try Testing.parse_hash("5d810c74eb9ba4d03fa49ab920b85e45"),
        );
        try tc.run(tmp.dir);
    }
    {
        var tc = try Tc.init(
            "./testdata/meta.tilde.foo.htm",
            "~foo",
            try Testing.parse_hash("f5bdd49b1a3d691113033ee606e0aa1d"),
        );
        try tc.run(tmp.dir);
    }
}