ostrya 0.3.0

Pure-Rust async reimplementation of the ostree repository library
Documentation
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
//! Integration tests of the object writers.
//!
//! The tests write objects into real repositories and check these items:
//!
//! - archive and bare-user loose objects against the checked-in fixtures,
//!   byte for byte, and the bare-user-shared objects that follow from
//!   bare-user
//! - bare objects against the objects of the `ostree` command
//! - a second write of an object, which is a no-op (dedup)
//! - the free-space guard
//! - concurrent writers on one `&Transaction`
//! - the read-back through `load_file`
//! - the canonical header that bare-user-only stores and names its objects
//!   for

mod common;

use std::path::Path;
use std::process::Command;

use common::{TmpDir, fixture_repo, ostree_available};
use futures_lite::AsyncReadExt;
use futures_lite::io::Cursor;
use ostrya::{
    Checksum, CreateOptions, Error, FileKind, FileMeta, FsckOptions, Repo, RepoMode, Transaction,
    TransactionStats,
};
use ostrya_core::{ObjectType, loose_path};
use ostrya_rt::block_on;

// The fixture tree of the golden repositories (owner 0:0, mode 0644), and the
// object checksums that the `ostree` command gave to it.
const HELLO: &[u8] = b"hello ostree\n";
const NESTED: &[u8] = b"nested\n";
const EMPTY: &[u8] = b"";
const HELLO_TXT: &str = common::HELLO_TXT;
const EMPTY_TXT: &str = common::EMPTY_TXT;
const NESTED_TXT: &str = common::NESTED_TXT;
const LINK: &str = common::LINK;

fn csum(hex: &str) -> Checksum {
    Checksum::from_hex(hex).unwrap()
}

/// Returns a regular-file `FileMeta` with owner 0:0 and mode 0644, as in the
/// fixtures.
fn reg() -> FileMeta {
    FileMeta::regular(0, 0, 0o644)
}

/// Writes the four fixture content objects into `txn` through ostrya.
///
/// The writes use the inline, streaming, and symlink writers. The function
/// asserts that each computed checksum is equal to the fixture checksum of the
/// `ostree` command.
async fn write_fixture_tree(txn: &Transaction) {
    assert_eq!(
        txn.write_regfile_inline(Some(&csum(HELLO_TXT)), &reg(), HELLO)
            .await
            .unwrap(),
        csum(HELLO_TXT),
        "hello.txt identity"
    );
    // The streaming writer must give the same checksum as the inline writer.
    assert_eq!(
        txn.write_content(None, &reg(), Cursor::new(NESTED.to_vec()))
            .await
            .unwrap(),
        csum(NESTED_TXT),
        "nested.txt identity"
    );
    assert_eq!(
        txn.write_regfile_inline(None, &reg(), EMPTY).await.unwrap(),
        csum(EMPTY_TXT),
        "empty.txt identity"
    );
    assert_eq!(
        txn.write_symlink("hello.txt", &FileMeta::regular(0, 0, 0), Some(&csum(LINK)))
            .await
            .unwrap(),
        csum(LINK),
        "link identity"
    );
}

/// Returns the on-disk bytes of a loose object in the repository at `root`.
fn object_bytes(root: &Path, hex: &str, ty: ObjectType, mode: RepoMode) -> Vec<u8> {
    std::fs::read(root.join("objects").join(loose_path(&csum(hex), ty, mode))).unwrap()
}

/// Returns the bytes of a loose object in the checked-in fixture repository.
fn fixture_bytes(mode_dir: &str, hex: &str, ty: ObjectType, mode: RepoMode) -> Vec<u8> {
    std::fs::read(
        fixture_repo(mode_dir)
            .join("objects")
            .join(loose_path(&csum(hex), ty, mode)),
    )
    .unwrap()
}

/// Returns the `user.ostreemeta` xattr of a loose object.
fn ostreemeta(root: &Path, hex: &str, mode: RepoMode) -> Vec<u8> {
    let path = root
        .join("objects")
        .join(loose_path(&csum(hex), ObjectType::File, mode));
    let mut buf = vec![0u8; 256];
    let n = rustix::fs::getxattr(&path, "user.ostreemeta", &mut buf).unwrap();
    buf.truncate(n);
    buf
}

fn inode_perm(root: &Path, hex: &str, ty: ObjectType, mode: RepoMode) -> u32 {
    let path = root.join("objects").join(loose_path(&csum(hex), ty, mode));
    let stat = rustix::fs::stat(&path).unwrap();
    stat.st_mode & 0o7777
}

#[test]
fn archive_objects_are_byte_identical_to_the_fixture() {
    let tmp = TmpDir::new("write-archive");
    let root = tmp.path().join("repo");
    block_on(async {
        let repo = Repo::create(&root, CreateOptions::new(RepoMode::Archive))
            .await
            .unwrap();
        let txn = repo.transaction().await.unwrap();
        write_fixture_tree(&txn).await;
        let stats = txn.commit().await.unwrap();
        assert_eq!(stats.content_written, 4);

        // Each stored `.filez` object (regular files and the symlink) is byte
        // for byte the same as the object that the `ostree` command wrote.
        for hex in [HELLO_TXT, EMPTY_TXT, NESTED_TXT, LINK] {
            assert_eq!(
                object_bytes(&root, hex, ObjectType::File, RepoMode::Archive),
                fixture_bytes("archive", hex, ObjectType::File, RepoMode::Archive),
                "archive object {hex}"
            );
            assert_eq!(
                inode_perm(&root, hex, ObjectType::File, RepoMode::Archive),
                0o644
            );
        }
    });
}

#[test]
fn bare_user_objects_match_the_fixture() {
    let tmp = TmpDir::new("write-bare-user");
    let root = tmp.path().join("repo");
    block_on(async {
        let repo = Repo::create(&root, CreateOptions::new(RepoMode::BareUser))
            .await
            .unwrap();
        let txn = repo.transaction().await.unwrap();
        write_fixture_tree(&txn).await;
        txn.commit().await.unwrap();

        // Regular files: the raw payload is on disk, and the logical metadata
        // is in the xattr.
        for (hex, payload) in [(HELLO_TXT, HELLO), (EMPTY_TXT, EMPTY), (NESTED_TXT, NESTED)] {
            assert_eq!(
                object_bytes(&root, hex, ObjectType::File, RepoMode::BareUser),
                payload,
                "bare-user payload {hex}"
            );
            assert_eq!(
                ostreemeta(&root, hex, RepoMode::BareUser),
                fixture_ostreemeta("bare-user", hex),
                "bare-user user.ostreemeta {hex}"
            );
            assert_eq!(
                inode_perm(&root, hex, ObjectType::File, RepoMode::BareUser),
                0o644
            );
        }
        // bare-user stores the symlink as a regular file: the target and then
        // a NUL.
        assert_eq!(
            object_bytes(&root, LINK, ObjectType::File, RepoMode::BareUser),
            b"hello.txt\0"
        );
        assert_eq!(
            ostreemeta(&root, LINK, RepoMode::BareUser),
            fixture_ostreemeta("bare-user", LINK),
        );
    });
}

/// Returns the `user.ostreemeta` xattr of a fixture object.
fn fixture_ostreemeta(mode_dir: &str, hex: &str) -> Vec<u8> {
    let path = fixture_repo(mode_dir).join("objects").join(loose_path(
        &csum(hex),
        ObjectType::File,
        RepoMode::BareUser,
    ));
    let mut buf = vec![0u8; 256];
    let n = rustix::fs::getxattr(&path, "user.ostreemeta", &mut buf).unwrap();
    buf.truncate(n);
    buf
}

#[test]
fn bare_user_shared_shares_bare_user_identity_with_fixed_mode() {
    let tmp = TmpDir::new("write-shared");
    let root = tmp.path().join("repo");
    block_on(async {
        let repo = Repo::create(&root, CreateOptions::new(RepoMode::BareUserShared))
            .await
            .unwrap();
        let txn = repo.transaction().await.unwrap();
        // The checksums are the same as in bare-user (`write_fixture_tree`
        // asserts them).
        write_fixture_tree(&txn).await;
        txn.commit().await.unwrap();

        // The payload and `user.ostreemeta` are byte for byte the same as in
        // bare-user. The inode mode is always 0644, for each logical mode.
        for (hex, payload) in [(HELLO_TXT, HELLO), (NESTED_TXT, NESTED)] {
            assert_eq!(
                object_bytes(&root, hex, ObjectType::File, RepoMode::BareUserShared),
                payload
            );
            assert_eq!(
                ostreemeta(&root, hex, RepoMode::BareUserShared),
                fixture_ostreemeta("bare-user", hex),
            );
            assert_eq!(
                inode_perm(&root, hex, ObjectType::File, RepoMode::BareUserShared),
                0o644
            );
        }
    });
}

#[test]
fn write_metadata_stages_a_metadata_object() {
    let tmp = TmpDir::new("write-meta");
    let root = tmp.path().join("repo");
    block_on(async {
        let repo = Repo::create(&root, CreateOptions::new(RepoMode::BareUser))
            .await
            .unwrap();
        // A directory metadata object: uid 0, gid 0, mode 040755, no xattrs.
        let dirmeta = ostrya_core::DirMeta {
            uid: 0,
            gid: 0,
            mode: 0o040755,
            xattrs: ostrya_core::Xattrs::empty(),
        };
        let bytes = dirmeta.serialize().unwrap();
        let expected = Checksum::sha256(&bytes);

        let txn = repo.transaction().await.unwrap();
        let c = txn
            .write_metadata(ObjectType::DirMeta, Some(&expected), &bytes)
            .await
            .unwrap();
        assert_eq!(
            c, expected,
            "identity is the sha256 of the normal-form bytes"
        );
        let stats = txn.commit().await.unwrap();
        assert_eq!(stats.metadata_written, 1);
        assert_eq!(stats.content_written, 0);

        // The staged object is at its loose path, with the fixed inode mode
        // 0644 and the same bytes. The repository reads it back.
        assert_eq!(
            object_bytes(&root, &c.to_hex(), ObjectType::DirMeta, RepoMode::BareUser),
            bytes
        );
        assert_eq!(
            inode_perm(&root, &c.to_hex(), ObjectType::DirMeta, RepoMode::BareUser),
            0o644
        );
        let repo = Repo::open(&root).await.unwrap();
        assert_eq!(repo.load_dirmeta(&c).await.unwrap(), dirmeta);
    });
}

#[test]
fn write_metadata_rejects_bare_split_xattrs() {
    let tmp = TmpDir::new("write-meta-split");
    let root = tmp.path().join("repo");
    block_on(async {
        let repo = Repo::create(&root, CreateOptions::new(RepoMode::BareSplitXattrs))
            .await
            .unwrap();
        let dirmeta = ostrya_core::DirMeta {
            uid: 0,
            gid: 0,
            mode: 0o040755,
            xattrs: ostrya_core::Xattrs::empty(),
        };
        let bytes = dirmeta.serialize().unwrap();
        let txn = repo.transaction().await.unwrap();
        // All writers treat bare-split-xattrs as read-only. The content,
        // symlink, and metadata writers refuse the mode.
        let err = txn
            .write_metadata(ObjectType::DirMeta, None, &bytes)
            .await
            .unwrap_err();
        assert!(
            matches!(err, Error::Unsupported(_)),
            "bare-split-xattrs is read-only, got {err:?}"
        );
        txn.abort().await.unwrap();
    });
}

#[test]
fn bare_content_applies_inode_xattrs() {
    let tmp = TmpDir::new("write-bare-xattr");
    // Bare writes the logical ownership to the inode, so the test uses ids
    // that the process owns. It sets only `user.*` names. A process without
    // privileges can apply both.
    let owned = rustix::fs::stat(tmp.path()).unwrap();
    let uid = owned.st_uid;
    let gid = owned.st_gid;
    let root = tmp.path().join("repo");
    block_on(async {
        let repo = Repo::create(&root, CreateOptions::new(RepoMode::Bare))
            .await
            .unwrap();
        let txn = repo.transaction().await.unwrap();
        // Stored names end in a NUL. The writer removes the NUL before the
        // `setxattr` syscall.
        let xattrs = ostrya_core::Xattrs::new([
            (b"user.one\0".to_vec(), b"first".to_vec()),
            (b"user.two\0".to_vec(), b"second".to_vec()),
        ])
        .unwrap();
        let mut meta = FileMeta::regular(uid, gid, 0o644);
        meta.xattrs = xattrs;
        let checksum = txn.write_regfile_inline(None, &meta, HELLO).await.unwrap();
        txn.commit().await.unwrap();

        // Bare stores the raw payload and puts the logical xattrs on the inode.
        let hex = checksum.to_hex();
        assert_eq!(
            object_bytes(&root, &hex, ObjectType::File, RepoMode::Bare),
            HELLO
        );
        assert_eq!(inode_xattr(&root, &hex, "user.one"), b"first");
        assert_eq!(inode_xattr(&root, &hex, "user.two"), b"second");
    });
}

/// Returns the value of a named xattr on the inode of a bare loose object.
fn inode_xattr(root: &Path, hex: &str, name: &str) -> Vec<u8> {
    let path = root
        .join("objects")
        .join(loose_path(&csum(hex), ObjectType::File, RepoMode::Bare));
    let mut buf = vec![0u8; 256];
    let n = rustix::fs::getxattr(&path, name, &mut buf).unwrap();
    buf.truncate(n);
    buf
}

#[test]
fn rewriting_an_object_is_a_dedup_noop() {
    let tmp = TmpDir::new("write-dedup");
    let root = tmp.path().join("repo");
    block_on(async {
        let repo = Repo::create(&root, CreateOptions::new(RepoMode::BareUser))
            .await
            .unwrap();
        let txn = repo.transaction().await.unwrap();
        let a = txn.write_regfile_inline(None, &reg(), HELLO).await.unwrap();
        let b = txn.write_regfile_inline(None, &reg(), HELLO).await.unwrap();
        assert_eq!(a, b, "same content, same identity");
        let stats: TransactionStats = txn.commit().await.unwrap();
        assert_eq!(
            stats.content_written, 1,
            "the second write is a dedup no-op"
        );

        // A new transaction finds the object in `objects/` and does not write
        // it again (dedup).
        let txn = repo.transaction().await.unwrap();
        assert_eq!(
            txn.write_regfile_inline(None, &reg(), HELLO).await.unwrap(),
            a
        );
        let stats = txn.commit().await.unwrap();
        assert_eq!(
            stats.content_written, 0,
            "already published, so no new object"
        );
    });
}

#[test]
fn free_space_guard_trips_on_an_exhausted_budget() {
    let tmp = TmpDir::new("write-space");
    let root = tmp.path().join("repo");
    block_on(async {
        Repo::create(&root, CreateOptions::new(RepoMode::BareUser))
            .await
            .unwrap();
        // A reservation of 100% of the file system gives a write budget of 0.
        let config = root.join("config");
        let mut text = std::fs::read_to_string(&config).unwrap();
        text.push_str("min-free-space-percent=100\n");
        std::fs::write(&config, text).unwrap();
        let repo = Repo::open(&root).await.unwrap();

        let txn = repo.transaction().await.unwrap();
        let err = txn
            .write_regfile_inline(None, &reg(), HELLO)
            .await
            .unwrap_err();
        assert!(
            matches!(err, Error::InsufficientFreeSpace { shortfall } if shortfall > 0),
            "expected a free-space error, got {err:?}"
        );
        txn.abort().await.unwrap();
    });
}

/// Checks that each write path refuses a `[core] fsync` value that the reader
/// does not accept.
///
/// The test runs with no [`Transaction::set_fsync`] override and with an
/// override of each polarity. An override replaces the configured policy. The
/// reader parses the configured value in all cases, so the bad value stops the
/// write.
#[test]
fn a_bad_configured_fsync_is_refused_under_every_override() {
    let tmp = TmpDir::new("write-fsync-bad-config");
    let root = tmp.path().join("repo");
    block_on(async {
        Repo::create(&root, CreateOptions::new(RepoMode::BareUser))
            .await
            .unwrap();
        let config = root.join("config");
        let mut text = std::fs::read_to_string(&config).unwrap();
        text.push_str("fsync=bogus\n");
        std::fs::write(&config, text).unwrap();
        let repo = Repo::open(&root).await.unwrap();

        for override_value in [None, Some(true), Some(false)] {
            let mut txn = repo.transaction().await.unwrap();
            if let Some(enabled) = override_value {
                txn.set_fsync(enabled);
            }
            let err = txn
                .write_regfile_inline(None, &reg(), HELLO)
                .await
                .unwrap_err();
            assert!(
                matches!(
                    &err,
                    Error::Core(ostrya_core::Error::KeyFile(text))
                        if text.contains("core.fsync")
                ),
                "override {override_value:?} gave {err:?} in place of the config refusal",
            );
            txn.abort().await.unwrap();
        }
    });
}

/// Checks that each write path refuses a `[core] per-object-fsync` value that
/// the reader does not accept.
///
/// The test runs with no [`Transaction::set_per_object_fsync`] override, with
/// an override of each polarity, and with fsync turned off by
/// [`Transaction::set_fsync`]. An override replaces the configured setting. The
/// reader parses the configured value in all cases, so the bad value stops the
/// write.
#[test]
fn a_bad_configured_per_object_fsync_is_refused_under_every_override() {
    let tmp = TmpDir::new("write-per-object-fsync-bad-config");
    let root = tmp.path().join("repo");
    block_on(async {
        Repo::create(&root, CreateOptions::new(RepoMode::BareUser))
            .await
            .unwrap();
        let config = root.join("config");
        let mut text = std::fs::read_to_string(&config).unwrap();
        text.push_str("per-object-fsync=bogus\n");
        std::fs::write(&config, text).unwrap();
        let repo = Repo::open(&root).await.unwrap();

        // (per-object override, fsync override)
        let rows = [
            (None, None),
            (Some(true), None),
            (Some(false), None),
            (None, Some(false)),
            (Some(true), Some(false)),
        ];
        for (per_object, fsync) in rows {
            let mut txn = repo.transaction().await.unwrap();
            if let Some(enabled) = per_object {
                txn.set_per_object_fsync(enabled);
            }
            if let Some(enabled) = fsync {
                txn.set_fsync(enabled);
            }
            let err = txn
                .write_regfile_inline(None, &reg(), HELLO)
                .await
                .unwrap_err();
            assert!(
                matches!(
                    &err,
                    Error::Core(ostrya_core::Error::KeyFile(text))
                        if text.contains("core.per-object-fsync")
                ),
                "overrides {per_object:?}/{fsync:?} gave {err:?} in place of the config refusal",
            );
            txn.abort().await.unwrap();
        }
    });
}

#[test]
fn concurrent_writers_share_one_transaction() {
    let tmp = TmpDir::new("write-concurrent");
    let root = tmp.path().join("repo");
    let repo = block_on(Repo::create(&root, CreateOptions::new(RepoMode::BareUser))).unwrap();
    let txn = block_on(repo.transaction()).unwrap();

    const N: usize = 8;
    let payloads: Vec<Vec<u8>> = (0..N)
        .map(|i| format!("payload number {i}\n").into_bytes())
        .collect();

    std::thread::scope(|scope| {
        for payload in &payloads {
            let txn = &txn;
            scope.spawn(move || {
                block_on(async {
                    txn.write_content(None, &reg(), Cursor::new(payload.clone()))
                        .await
                        .unwrap();
                });
            });
        }
    });

    let stats = block_on(txn.commit()).unwrap();
    assert_eq!(
        stats.content_written as usize, N,
        "each writer staged one object"
    );

    // Every object is present and reads back to its payload.
    block_on(async {
        let repo = Repo::open(&root).await.unwrap();
        for payload in &payloads {
            let checksum = object_checksum_of(&repo, &reg(), payload).await;
            let file = repo.load_file(&checksum).await.unwrap();
            let mut got = Vec::new();
            file.reader()
                .await
                .unwrap()
                .read_to_end(&mut got)
                .await
                .unwrap();
            assert_eq!(&got, payload);
        }
    });
}

/// Returns the checksum of a payload.
///
/// The function writes the payload in a temporary transaction and then aborts
/// that transaction.
async fn object_checksum_of(repo: &Repo, meta: &FileMeta, payload: &[u8]) -> Checksum {
    let txn = repo.transaction().await.unwrap();
    let c = txn.write_regfile_inline(None, meta, payload).await.unwrap();
    txn.abort().await.unwrap();
    c
}

#[test]
fn content_reads_back_through_load_file() {
    let tmp = TmpDir::new("write-roundtrip");
    let root = tmp.path().join("repo");
    block_on(async {
        let repo = Repo::create(&root, CreateOptions::new(RepoMode::Archive))
            .await
            .unwrap();
        let txn = repo.transaction().await.unwrap();
        write_fixture_tree(&txn).await;
        txn.commit().await.unwrap();

        let repo = Repo::open(&root).await.unwrap();
        let hello = repo.load_file(&csum(HELLO_TXT)).await.unwrap();
        assert_eq!(
            hello.kind,
            FileKind::Regular {
                size: HELLO.len() as u64
            }
        );
        assert_eq!((hello.uid, hello.gid, hello.mode), (0, 0, 0o100644));
        let mut got = Vec::new();
        hello
            .reader()
            .await
            .unwrap()
            .read_to_end(&mut got)
            .await
            .unwrap();
        assert_eq!(got, HELLO);

        let link = repo.load_file(&csum(LINK)).await.unwrap();
        assert_eq!(
            link.kind,
            FileKind::Symlink {
                target: "hello.txt".to_owned()
            }
        );
    });
}

#[test]
fn a_read_only_mode_is_stored_in_bare_user() {
    // A logical mode with no owner-write bit is usual in a system tree, for
    // example 0444. bare-user can store such a mode. In bare-user the logical
    // metadata is in a `user.ostreemeta` xattr. The kernel checks this xattr
    // against the write permission of the inode. The test uses both content
    // writers, because each writer stages its own temporary file before it
    // applies the inode policy.
    let tmp = TmpDir::new("write-readonly");
    let root = tmp.path().join("repo");
    block_on(async {
        let repo = Repo::create(&root, CreateOptions::new(RepoMode::BareUser))
            .await
            .unwrap();
        let txn = repo.transaction().await.unwrap();
        let meta = FileMeta::regular(0, 0, 0o444);
        let inline = txn.write_regfile_inline(None, &meta, HELLO).await.unwrap();
        let streamed = txn
            .write_content(None, &meta, Cursor::new(NESTED.to_vec()))
            .await
            .unwrap();
        txn.commit().await.unwrap();

        for checksum in [inline, streamed] {
            // In bare-user the canonical inode mode of a 0444 file is 0444. The
            // logical mode reads back from the xattr.
            let path = root.join("objects").join(loose_path(
                &checksum,
                ObjectType::File,
                RepoMode::BareUser,
            ));
            let stored = rustix::fs::stat(&path).unwrap();
            assert_eq!(stored.st_mode & 0o7777, 0o444, "stored mode of {checksum}");
            let file = repo.load_file(&checksum).await.unwrap();
            assert_eq!((file.uid, file.gid, file.mode), (0, 0, 0o100444));
        }
    });
}

#[test]
fn a_read_only_mode_with_an_xattr_is_stored_in_bare() {
    // Bare puts the logical xattrs of a content object on the inode. The
    // kernel checks a `user.*` name against the write permission of the inode.
    // Bare can store a logical mode with no owner-write bit together with such
    // an xattr. The test uses both content writers, because each writer stages
    // its own temporary file before it applies the inode policy.
    let tmp = TmpDir::new("write-bare-readonly");
    // Bare writes the logical ownership to the inode, so the test uses ids
    // that the process owns. It sets only `user.*` names. A process without
    // privileges can apply both.
    let owned = rustix::fs::stat(tmp.path()).unwrap();
    let uid = owned.st_uid;
    let gid = owned.st_gid;
    let root = tmp.path().join("repo");
    block_on(async {
        let repo = Repo::create(&root, CreateOptions::new(RepoMode::Bare))
            .await
            .unwrap();
        let txn = repo.transaction().await.unwrap();
        let mut meta = FileMeta::regular(uid, gid, 0o444);
        meta.xattrs =
            ostrya_core::Xattrs::new([(b"user.demo\0".to_vec(), b"value".to_vec())]).unwrap();
        let inline = txn.write_regfile_inline(None, &meta, HELLO).await.unwrap();
        let streamed = txn
            .write_content(None, &meta, Cursor::new(NESTED.to_vec()))
            .await
            .unwrap();
        txn.commit().await.unwrap();

        for checksum in [inline, streamed] {
            let hex = checksum.to_hex();
            assert_eq!(inode_xattr(&root, &hex, "user.demo"), b"value");
            let file = repo.load_file(&checksum).await.unwrap();
            assert_eq!((file.uid, file.gid, file.mode), (uid, gid, 0o100444));
        }
    });
}

#[test]
fn bare_objects_match_the_tool() {
    if !ostree_available() {
        eprintln!("skipping bare_objects_match_the_tool: the ostree tool is unavailable");
        return;
    }
    // Bare stores the logical ownership on the inode, so a faithful write
    // needs ids that the process can apply. The test takes them from a
    // directory that this process owns. ostrya and the `ostree` command then
    // use the same ids, so their objects match.
    let tmp = TmpDir::new("write-bare");
    let owned = rustix::fs::stat(tmp.path()).unwrap();
    let uid = owned.st_uid;
    let gid = owned.st_gid;

    // Build a bare repository with ostrya, with that ownership.
    let port_root = tmp.path().join("port");
    block_on(async {
        let repo = Repo::create(&port_root, CreateOptions::new(RepoMode::Bare))
            .await
            .unwrap();
        let txn = repo.transaction().await.unwrap();
        let meta = FileMeta::regular(uid, gid, 0o644);
        txn.write_regfile_inline(None, &meta, HELLO).await.unwrap();
        txn.write_regfile_inline(None, &meta, NESTED).await.unwrap();
        txn.write_regfile_inline(None, &meta, EMPTY).await.unwrap();
        txn.write_symlink("hello.txt", &FileMeta::regular(uid, gid, 0), None)
            .await
            .unwrap();
        txn.commit().await.unwrap();
    });

    // Build the same tree with the `ostree` command in a bare repository.
    let tool_root = tmp.path().join("tool");
    let src = tmp.path().join("src");
    std::fs::create_dir_all(&src).unwrap();
    std::fs::write(src.join("hello.txt"), HELLO).unwrap();
    std::fs::write(src.join("nested.txt"), NESTED).unwrap();
    std::fs::write(src.join("empty.txt"), EMPTY).unwrap();
    std::os::unix::fs::symlink("hello.txt", src.join("link")).unwrap();
    for f in ["hello.txt", "nested.txt", "empty.txt"] {
        std::fs::set_permissions(
            src.join(f),
            std::os::unix::fs::PermissionsExt::from_mode(0o644),
        )
        .unwrap();
    }
    let repo_arg = format!("--repo={}", tool_root.display());
    run_ostree(&[&repo_arg, "init", "--mode=bare"]);
    run_ostree(&[
        &repo_arg,
        "commit",
        "--branch=t",
        "--subject=x",
        &format!("--owner-uid={uid}"),
        &format!("--owner-gid={gid}"),
        "--no-xattrs",
        "--timestamp=@1700000000",
        src.to_str().unwrap(),
    ]);

    // Each content object that the `ostree` command wrote is in the ostrya
    // repository, with the same bytes, inode mode, and ownership. The ostrya
    // side writes only content objects, so the test does not compare the tree
    // and commit metadata objects of the `ostree` command.
    for entry in walk_objects(&tool_root.join("objects")) {
        if entry.extension().and_then(|e| e.to_str()) != Some("file") {
            continue;
        }
        let rel = entry.strip_prefix(tool_root.join("objects")).unwrap();
        let ours = port_root.join("objects").join(rel);
        // The test uses `symlink_metadata`. The relative target of a symlink
        // object does not resolve inside `objects/`. `exists()` follows the
        // link, so it returns `false`.
        let our_meta = std::fs::symlink_metadata(&ours)
            .unwrap_or_else(|_| panic!("port is missing object {rel:?}"));
        let tool_meta = std::fs::symlink_metadata(&entry).unwrap();
        use std::os::unix::fs::MetadataExt;
        assert_eq!(tool_meta.mode(), our_meta.mode(), "mode of {rel:?}");
        assert_eq!(tool_meta.uid(), our_meta.uid(), "uid of {rel:?}");
        assert_eq!(tool_meta.gid(), our_meta.gid(), "gid of {rel:?}");
        if tool_meta.file_type().is_symlink() {
            assert_eq!(
                std::fs::read_link(&entry).unwrap(),
                std::fs::read_link(&ours).unwrap(),
                "symlink target of {rel:?}"
            );
        } else {
            assert_eq!(
                std::fs::read(&entry).unwrap(),
                std::fs::read(&ours).unwrap(),
                "content of {rel:?}"
            );
        }
    }
}

#[test]
fn bare_user_only_hashes_the_canonical_header() {
    // bare-user-only stores no ownership and no xattrs. It reduces the
    // permission bits of a regular file to `perm & 0o755`. The checksum of an
    // object covers that reduced header, so the name of the object matches
    // what the mode stores. As a result, the object reads back as named and
    // passes fsck.
    //
    // The checksum is equal to the checksum of the same canonical entry in
    // each other mode. The test compares against that checksum. The test
    // `commit::bare_user_only_commit_matches_the_tool` pins this behavior to
    // the `ostree` command.
    let tmp = TmpDir::new("write-buo-canon");
    let buo_root = tmp.path().join("buo");
    let bu_root = tmp.path().join("bu");
    block_on(async {
        // A non-canonical entry: ids that the mode discards, a mode with the
        // group-write and other-write bits set, and one xattr.
        let mut meta = FileMeta::regular(4242, 4242, 0o777);
        meta.xattrs =
            ostrya_core::Xattrs::new([(b"user.demo\0".to_vec(), b"value".to_vec())]).unwrap();

        let repo = Repo::create(&buo_root, CreateOptions::new(RepoMode::BareUserOnly))
            .await
            .unwrap();
        let txn = repo.transaction().await.unwrap();
        let inline = txn.write_regfile_inline(None, &meta, HELLO).await.unwrap();
        let streamed = txn
            .write_content(None, &meta, Cursor::new(NESTED.to_vec()))
            .await
            .unwrap();
        let link = txn.write_symlink("hello.txt", &meta, None).await.unwrap();
        txn.commit().await.unwrap();

        // The checksum of the canonical form of each entry, from bare-user. This
        // mode stores the header that the writer gets.
        let canon = FileMeta::regular(0, 0, 0o755);
        let bu = Repo::create(&bu_root, CreateOptions::new(RepoMode::BareUser))
            .await
            .unwrap();
        let txn = bu.transaction().await.unwrap();
        let canon_inline = txn.write_regfile_inline(None, &canon, HELLO).await.unwrap();
        let canon_streamed = txn
            .write_content(None, &canon, Cursor::new(NESTED.to_vec()))
            .await
            .unwrap();
        let canon_link = txn.write_symlink("hello.txt", &canon, None).await.unwrap();
        txn.commit().await.unwrap();

        assert_eq!(inline, canon_inline, "inline regular-file identity");
        assert_eq!(streamed, canon_streamed, "streamed regular-file identity");
        assert_eq!(link, canon_link, "symlink identity");

        // Each object reads back with the header that its checksum covers.
        for checksum in [inline, streamed] {
            let file = repo.load_file(&checksum).await.unwrap();
            assert_eq!((file.uid, file.gid, file.mode), (0, 0, 0o100755));
            assert!(file.xattrs.is_empty(), "xattrs of {checksum}");
        }
        let file = repo.load_file(&link).await.unwrap();
        assert_eq!((file.uid, file.gid), (0, 0));

        let report = repo.fsck(&FsckOptions::default()).await.unwrap();
        assert!(report.is_ok(), "fsck reported {:?}", report.errors);
    });
}

fn run_ostree(args: &[&str]) {
    let status = Command::new("ostree")
        .args(args)
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .expect("run ostree");
    assert!(status.success(), "ostree {args:?} failed");
}

/// Returns each regular file and symlink under an `objects/` directory.
fn walk_objects(dir: &Path) -> Vec<std::path::PathBuf> {
    let mut out = Vec::new();
    for fanout in std::fs::read_dir(dir).unwrap().flatten() {
        if fanout.file_type().unwrap().is_dir() {
            for obj in std::fs::read_dir(fanout.path()).unwrap().flatten() {
                out.push(obj.path());
            }
        }
    }
    out
}