ostrya 0.11.1

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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
//! A push session as steps, for a transport that carries each step in a
//! request of its own, for example HTTP.

use std::collections::HashMap;
use std::io;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::task::{Context, Poll, Waker};

use futures_io::AsyncRead;
use futures_lite::io::{AsyncReadExt, BufReader};
use ostrya_core::ObjectName;

use super::core::SessionCore;
use super::session::{Failure, ReceiveCommit, STREAM_BUFFER, aborted, next, out_of_order};
use super::{ReceiveHooks, ReceivePolicy};
use crate::error::{Error, Result};
use crate::remote;
use crate::remote::proto::{
    CommitRequest, FrameReader, HaveReply, Hello, HelloReply, MAX_FRAME, Message, ObjectsReply,
};
use crate::repo::Repo;

type Core = SessionCore<Arc<ReceivePolicy>>;

/// A push session as steps, one step for each request of the transport.
///
/// [`hello`](Self::hello) opens the session. [`have`](Self::have),
/// [`objects`](Self::objects), and [`commit`](Self::commit) are its later
/// steps. Each step does what the message of the same name does in
/// [`Repo::receive`](crate::Repo::receive), with the same checks and the same
/// wire codes. The host sends the reply.
///
/// [`hello_with_hooks`](Self::hello_with_hooks) opens a session with the
/// [`ReceiveHooks`] of the host, which [`commit`](Self::commit) calls. The
/// session id, the owner of the session, its idle timeout, and the session
/// limit belong to the host.
///
/// # Concurrency
///
/// Every step takes `&self`, so the host can keep the service in an `Arc` and
/// run steps of one session at the same time:
///
/// - Up to `parallel_uploads` [`objects`](Self::objects) calls run at the
///   same time. They write through the one session transaction. One more
///   call is `limit-exceeded`.
/// - One [`have`](Self::have) runs next to the other steps. It does not count
///   toward `parallel_uploads`. A second `have` while the first is in flight
///   is `limit-exceeded`.
/// - [`commit`](Self::commit) runs only when no other step is in flight. If
///   another step is in flight, `commit` is `protocol`. While `commit` runs,
///   each other step is `protocol`.
///
/// # Metadata budgets
///
/// The detached metadata of the session has one byte cap for the whole
/// session, over every object stream:
/// [`MAX_METADATA_SIZE`](crate::MAX_METADATA_SIZE). The read that takes the
/// session past this cap is `limit-exceeded`.
///
/// The dirtree, dirmeta, and commit objects that the streams read at the same
/// time share a second budget of the same size. Their bytes count from their
/// arrival to their stage step. The read that takes the session past this
/// budget is `limit-exceeded`. The bytes of a detached metadata object count
/// against the first cap alone.
///
/// # End of a session
///
/// A step that fails ends the session with no commit, as one error ends a
/// session over a stream. A refused step also ends the session. An `objects`
/// call in flight then fails at its next read. If a step future is dropped
/// before it completes, the session also ends. If the service is dropped
/// while the session is open, the session ends in the same way.
///
/// When no step holds the session, a task on the blocking pool drops the
/// session transaction. The drop removes the staging directory and releases
/// the repository lock in the background. The call that ends the session does
/// not wait for it. Under the `tokio` backend, a call outside the context of a
/// runtime drops the transaction inline.
///
/// After the end, and after a commit, every step is `protocol`.
///
/// # Failures
///
/// - A failure with a wire code returns as [`Error::Remote`].
/// - A failure on the server side returns as the error it is. The host sends
///   it as `internal`.
/// - If the input of `objects` fails with an I/O error that is not an end of
///   file, the step returns [`Error::Io`].
/// - A step of a session that an earlier failure or [`abort`](Self::abort)
///   ended returns [`remote::Error::Protocol`] with the message
///   `the session was aborted: CAUSE`.
/// - A step can complete after a failure of another step or
///   [`abort`](Self::abort) ended the session. Then the step drops its result
///   and returns the same `protocol` error.
pub struct ReceiveService {
    state: Mutex<State>,
    signal: AbortSignal,
    parallel_uploads: u32,
}

enum State {
    /// The session takes steps. `uploads` counts the `objects` calls in
    /// flight. `haves` counts the `have` calls in flight, 0 or 1. Each step
    /// in flight holds a clone of `core`.
    Open {
        core: Arc<Core>,
        uploads: u32,
        haves: u32,
    },
    /// `commit` runs and owns the session.
    Committing,
    /// The session ended. `reason` is the message of the `protocol` error of
    /// each later step.
    Closed { reason: String },
}

#[derive(Clone, Copy)]
enum Kind {
    Have,
    Objects,
}

fn protocol(message: impl Into<String>) -> Error {
    Error::Remote(remote::Error::Protocol(message.into()))
}

/// Returns the error that a step returns for `failure`.
///
/// An `Abort` of the client and an input that ends before its last message
/// get no reply on a stream. Here they are `protocol`, which the host sends.
fn into_error(failure: Failure) -> Error {
    match failure {
        Failure::Wire(e) => Error::Remote(e),
        Failure::Internal(e) => e,
        Failure::Silent(Error::Remote(remote::Error::Aborted)) => {
            protocol("the client aborted the session")
        }
        Failure::Silent(Error::Io(e)) if e.kind() == io::ErrorKind::UnexpectedEof => {
            protocol("the object stream ends before ObjectsEnd")
        }
        Failure::Silent(e) => e,
    }
}

impl ReceiveService {
    /// Opens a session: answers `Hello` and opens the session transaction.
    ///
    /// `policy` is the policy of the session, which the host selects. It
    /// applies to each ref of `Hello` in the namespace of the ref, as
    /// [`Repo::receive`](crate::Repo::receive) states.
    ///
    /// The session transaction holds the repository lock shared until the
    /// session ends. `parallel_uploads` is the value that `HelloReply`
    /// announces. It is also the number of `objects` calls that the session
    /// runs at the same time. The service sets no upper bound, so the host
    /// keeps the value in a range of its own.
    ///
    /// The session has no hooks. The call is
    /// [`hello_with_hooks`](Self::hello_with_hooks) with `None`.
    ///
    /// # Errors
    ///
    /// A wire code returns inside [`Error::Remote`].
    ///
    /// - [`Error::InvalidInput`] if `parallel_uploads` is 0.
    /// - [`remote::Error::Protocol`] if `Hello` has `one-way` true.
    /// - The errors of [`check_hello`](Self::check_hello).
    /// - [`remote::Error::RefDenied`] if `policy` opens no push session, if
    ///   `Hello` names a remote ref, or if `policy` gives no `r` on a ref of
    ///   `Hello` in the namespace of the ref.
    ///   These checks follow the checks of `check_hello` and do no I/O.
    /// - The errors of [`Repo::transaction`](crate::Repo::transaction) if the
    ///   session transaction cannot open. These include
    ///   [`Error::LockTimeout`] if the wait for the repository lock passes
    ///   `[core] lock-timeout-secs`.
    /// - [`remote::Error::ModeRefused`] if the repository mode is `bare` and the
    ///   process does not run as root.
    /// - [`Error::Core`] or [`Error::InvalidFormat`] if `[core] fsync`,
    ///   `[core] per-object-fsync`, `[ex-integrity] fsverity`,
    ///   `[ex-ostrya] keep-detached-related`, or, in an archive repository,
    ///   `[archive] zlib-level` holds a malformed value.
    /// - An I/O error from the file system, if a read of the staging
    ///   directory or of the refs of `Hello` fails.
    pub async fn hello(
        repo: Repo,
        policy: Arc<ReceivePolicy>,
        parallel_uploads: u32,
        hello: Hello,
    ) -> Result<(ReceiveService, HelloReply)> {
        Self::hello_with_hooks(repo, policy, None, parallel_uploads, hello).await
    }

    /// Opens a session as [`hello`](Self::hello) does, with the hooks of the
    /// host.
    ///
    /// [`commit`](Self::commit) calls [`ReceiveHooks::before_update`] of
    /// `hooks`, or [`ReceiveHooks::before_server_commit`] for a server
    /// commit, before the update lock. It calls
    /// [`ReceiveHooks::after_update`] after the release of the update lock.
    /// `None` gives a session with no hooks.
    ///
    /// # Errors
    ///
    /// The errors of [`hello`](Self::hello).
    pub async fn hello_with_hooks(
        repo: Repo,
        policy: Arc<ReceivePolicy>,
        hooks: Option<Arc<dyn ReceiveHooks>>,
        parallel_uploads: u32,
        hello: Hello,
    ) -> Result<(ReceiveService, HelloReply)> {
        if parallel_uploads == 0 {
            return Err(Error::InvalidInput(
                "parallel_uploads must be at least 1".into(),
            ));
        }
        let (core, reply) =
            SessionCore::open(repo, || Some(policy), hooks, parallel_uploads, hello)
                .await
                .map_err(into_error)?;
        let service = ReceiveService {
            state: Mutex::new(State::Open {
                core: Arc::new(core),
                uploads: 0,
                haves: 0,
            }),
            signal: AbortSignal::default(),
            parallel_uploads,
        };
        Ok((service, reply))
    }

    /// Runs the checks of `hello` that every session runs before it opens.
    ///
    /// The checks run in this order:
    ///
    /// 1. The protocol version (`version-unsupported`).
    /// 2. The mode `bare-split-xattrs` (`mode-refused`).
    /// 3. For a `Hello` with `one-way` false, `[core] locking=false`
    ///    (`locking-disabled`).
    /// 4. Each ref name, which is `NAME`, `REMOTE:NAME`, or `@ID:NAME`, as
    ///    [`Ref`](crate::Ref) reads it (`invalid-ref`).
    /// 5. No two names of `Hello` name one ref (`protocol`). A name sent two
    ///    times names one ref, and so do `NAME` and `@ID:NAME` with `ID` the
    ///    collection id of the repository.
    /// 6. For a `Hello` with `one-way` false, the size of the `HelloReply`
    ///    with a commit for each ref, against [`MAX_FRAME`]
    ///    (`limit-exceeded`).
    ///
    /// The call is sync, does no I/O, and reads no policy.
    /// [`hello`](Self::hello),
    /// [`Repo::receive`](crate::Repo::receive),
    /// [`Repo::receive_stream`](crate::Repo::receive_stream), and
    /// [`Repo::receive_stream_to_commit`](crate::Repo::receive_stream_to_commit)
    /// run the same checks. The call does not refuse a `Hello` with `one-way`
    /// true, which [`hello`](Self::hello) refuses.
    ///
    /// A `Hello` that passes can still fail to open. These steps of the open
    /// come after the checks:
    ///
    /// - The checks of the policy of a two-way session, which
    ///   [`hello`](Self::hello) states.
    /// - The open of the session transaction.
    /// - The reads of `[core] fsync`, `[core] per-object-fsync`,
    ///   `[ex-integrity] fsverity`, `[ex-ostrya] keep-detached-related`, and
    ///   `[archive] zlib-level`.
    /// - The refusal of a `bare` repository if the process does not run as
    ///   root.
    ///
    /// # Errors
    ///
    /// A wire code returns inside [`Error::Remote`].
    ///
    /// - [`remote::Error::VersionUnsupported`] if `hello` names a protocol
    ///   version that the server does not speak.
    /// - [`remote::Error::ModeRefused`] if the repository mode is
    ///   `bare-split-xattrs`.
    /// - [`remote::Error::LockingDisabled`] if `hello` has `one-way` false and
    ///   the repository sets `[core] locking=false`.
    /// - [`remote::Error::InvalidRef`] if a ref name of `hello` is not valid.
    /// - [`remote::Error::Protocol`] if two names of `hello` name one ref.
    /// - [`remote::Error::LimitExceeded`] if `hello` has `one-way` false and
    ///   its `HelloReply` can need a frame over [`MAX_FRAME`].
    /// - [`remote::Error::Internal`] if the encoder refuses the `HelloReply`
    ///   with no ref.
    /// - [`Error::Core`] if `[core] locking` is not a boolean.
    pub fn check_hello(repo: &Repo, hello: &Hello) -> Result<()> {
        super::core::check_hello(repo, hello)
            .map(drop)
            .map_err(into_error)
    }

    /// Answers a `Have`: one bit for each object, set if the object is missing.
    ///
    /// An object is missing if neither the repository nor the session holds
    /// it.
    ///
    /// # Errors
    ///
    /// A wire code returns inside [`Error::Remote`]. Each error ends the
    /// session.
    ///
    /// - [`remote::Error::LimitExceeded`] if another `have` of the session is in
    ///   flight, or if `names` holds more than
    ///   [`MAX_HAVE`](crate::remote::proto::MAX_HAVE) entries.
    /// - [`remote::Error::Protocol`] if the session ended or a
    ///   [`commit`](Self::commit) runs.
    /// - An I/O error from the file system, if the check of an object in the
    ///   object store fails.
    pub async fn have(&self, names: Vec<ObjectName>) -> Result<HaveReply> {
        let step = self.enter(Kind::Have)?;
        let result = step.core().have(names).await;
        step.end(result)
    }

    /// Reads one object stream from `input` and stages its objects.
    ///
    /// The stream is a sequence of frames from `ObjectHeader` or `ObjectsEnd`
    /// to `ObjectsEnd`, and then the end of `input`. The counts of
    /// `ObjectsReply` are the counts of this stream. If two streams of the
    /// session send a content, dirtree, dirmeta, or commit object at the same
    /// time, the object can count in both.
    ///
    /// # Errors
    ///
    /// A wire code returns inside [`Error::Remote`]. Each error ends the
    /// session.
    ///
    /// - [`remote::Error::LimitExceeded`] if `parallel_uploads` calls are in
    ///   flight already, or if a read passes one of the
    ///   [metadata budgets](ReceiveService#metadata-budgets).
    /// - [`remote::Error::LimitExceeded`] if one metadata object is larger than
    ///   [`MAX_METADATA_SIZE`](crate::MAX_METADATA_SIZE), or if the header
    ///   of a content object is larger than its limit.
    /// - [`remote::Error::Protocol`] if a byte follows `ObjectsEnd`, or if
    ///   `input` ends before `ObjectsEnd`.
    /// - [`remote::Error::Protocol`] if the client sends an `Abort` frame, or a
    ///   message out of order, or bytes after the end of an object.
    /// - [`remote::Error::Protocol`] if two detached metadata objects of the
    ///   session name the same commit, or if a detached metadata dict is
    ///   malformed.
    /// - [`remote::Error::Protocol`] if the payload of an object is malformed,
    ///   for example a corrupt DEFLATE stream or a size that does not match
    ///   its header.
    /// - [`remote::Error::Protocol`] if the session ended or a
    ///   [`commit`](Self::commit) runs.
    /// - [`remote::Error::ChecksumMismatch`] if the bytes of an object do not
    ///   hash to its checksum.
    /// - [`remote::Error::ModeRefused`] if an object breaks the content rules of
    ///   the repository mode or of the policy.
    /// - The wire code of the frame decoder, if it refuses a frame.
    /// - [`Error::Io`] if `input` fails with an I/O error that is not an end
    ///   of file.
    /// - An I/O error from the file system, if the stage of an object fails.
    pub async fn objects<R>(&self, input: R) -> Result<ObjectsReply>
    where
        R: AsyncRead + Unpin + Send,
    {
        let step = self.enter(Kind::Objects)?;
        let result = read_objects(step.core(), &self.signal, input).await;
        step.end(result)
    }

    /// Runs `Commit`: the checks of the ref updates, the ref writes, and the
    /// transaction commit.
    ///
    /// The checks and the writes are the ones that
    /// [`Repo::receive`](crate::Repo::receive) runs. The session ends with
    /// the call.
    ///
    /// # Hooks
    ///
    /// In a session with hooks, the call runs [`ReceiveHooks::before_update`]
    /// just before the update lock. For a server commit, it runs
    /// [`ReceiveHooks::before_server_commit`] in its place, before the update
    /// lock. It runs [`ReceiveHooks::after_update`] after the release of the
    /// update lock, also when no ref changes.
    /// [`ReceiveHooks`] states when each hook runs, the entries of the host,
    /// the locks, and the result of an error or a panic in a hook.
    ///
    /// # Partial writes
    ///
    /// The transaction commit is not atomic. These failures can leave the
    /// detached metadata and some refs written:
    ///
    /// - a failure of a detached-metadata write
    /// - a failure of a ref write
    /// - a failure of the `fsync` of a ref directory
    ///
    /// The call then returns the error. In a session with hooks, the carried
    /// value drops, and `after_update` does not run.
    ///
    /// # Reply
    ///
    /// The call returns the report and the outcomes of the reply, each in the
    /// order of the updates. The host sends `CommitReply` with
    /// [`ReceiveCommit::reply`], whose names are the text that the client
    /// sent. The names of the report are canonical. If that send fails, the
    /// host adds a warning of the step
    /// [`ReplyNotDelivered`](super::ReceiveStep::ReplyNotDelivered) to the
    /// report. The report that `after_update` gets never holds this warning.
    ///
    /// # Dropped future
    ///
    /// The call owns the session transaction while it runs. If the `commit`
    /// future is dropped before it completes, the transaction drops inline,
    /// on the thread that drops the future. Each later step is then
    /// `protocol`. A host drops the future only when it drops the commit task,
    /// for example at the shutdown of the runtime.
    ///
    /// The transaction commit writes the detached metadata and the refs on the
    /// blocking pool. If the future is dropped during these writes, the update
    /// lock and the carried value of the hooks drop before the writes end. If
    /// the future is dropped while `after_update` runs, the future of the hook
    /// and the carried value drop, and the refs can be written.
    ///
    /// The host must run each commit to its end, for example in a task that
    /// it joins.
    ///
    /// # Errors
    ///
    /// A wire code returns inside [`Error::Remote`]. Each error ends the
    /// session.
    ///
    /// - [`remote::Error::Protocol`] if another step of the session is in
    ///   flight, if the session ended, or if another `commit` runs.
    /// - [`remote::Error::Protocol`] if the request holds no update and no
    ///   server commit, or names a ref that `Hello` does not name, or names
    ///   one ref twice.
    /// - [`remote::Error::Protocol`] if the request holds a server commit and
    ///   an update or `force`, if the branch or a tag of the server commit is
    ///   not a ref of `Hello`, if one ref occurs two times among them, or if
    ///   a metadata value of the server commit is not a `v` in normal form or
    ///   is too deep for a commit.
    /// - [`remote::Error::Protocol`] if the request holds a server commit and
    ///   the session holds a detached metadata dict or staged a commit object.
    /// - [`remote::Error::Protocol`] if a detached metadata dict belongs to a
    ///   commit that is not a commit of the session, unless the repository
    ///   holds the commit and field 2 of a commit of the session names it.
    ///   The session drops the dict of such a held dependency.
    /// - [`remote::Error::Protocol`] if a staged commit or a staged dirtree does
    ///   not parse.
    /// - [`remote::Error::Protocol`] if an entry of field 2 of a staged commit
    ///   has an empty name or a checksum that is not 32 bytes, or if a
    ///   dependency of a staged commit has dependencies of its own. The
    ///   message is the text of [`Error::InvalidRelated`]. A new commit that
    ///   the repository holds with a partial marker gets the checks of a
    ///   staged commit, here and for `missing-objects`.
    /// - [`remote::Error::Protocol`] if `[ex-ostrya] generate-composefs` of
    ///   the repository is `true`, and `ostree.composefs.digest.v0` of a new
    ///   commit of the session or of the server commit is not a variant of
    ///   type `ay` with 32 bytes.
    /// - [`remote::Error::Protocol`] if the branch and the tags of a server
    ///   commit are not in one namespace.
    /// - [`remote::Error::InvalidRef`] if a ref name is not valid, or if an
    ///   update writes a commit to a local ref of 64 lowercase hex
    ///   characters. This applies to the branch and each tag of a server
    ///   commit too. A bound name of a server commit that is not valid, or
    ///   that holds `:`, also gives this error.
    /// - [`remote::Error::LimitExceeded`] if the `CommitReply` can need a frame
    ///   over [`MAX_FRAME`], or if a merged detached metadata dict is larger
    ///   than [`MAX_METADATA_SIZE`](crate::MAX_METADATA_SIZE).
    /// - [`remote::Error::RefDenied`] if an update writes a commit to a ref
    ///   on which the policy gives no `w` and no `t` in the namespace of the
    ///   ref, or if an update names the collection anchor ref in the local
    ///   refs of a repository with a collection id.
    /// - [`remote::Error::RefDenied`] if the policy gives no `c` on the branch
    ///   of a server commit, or neither `t` nor `w` on a tag, each in the
    ///   namespace of the ref, or if the branch or a tag is the collection
    ///   anchor ref in the local refs of a repository with a collection id.
    /// - [`remote::Error::RefDenied`] if a ref path fails a check under the
    ///   update lock: a ref is an alias, a ref write cannot replace a path, or
    ///   one update names a directory of another.
    /// - [`remote::Error::RefDenied`] or [`remote::Error::Internal`] if
    ///   `before_update` or `before_server_commit` refuses with
    ///   [`HookRefusal::denied`](super::HookRefusal::denied) or
    ///   [`HookRefusal::internal`](super::HookRefusal::internal). A host that
    ///   does not implement `before_server_commit` refuses each server commit
    ///   with `ref-denied`.
    /// - [`remote::Error::MissingObjects`] if a new commit, or an object of the
    ///   tree of a commit of the session, is neither staged nor present.
    /// - [`remote::Error::MissingObjects`] if a dependency of a staged commit
    ///   is neither staged nor present. The error lists each such dependency.
    ///   A dependency that the repository holds and marks partial also gives
    ///   this error, with no object listed and the text of
    ///   [`Error::InvalidRelated`], unless it is a new commit of an update.
    /// - [`remote::Error::BindingMismatch`] if the `ostree.ref-binding` of a
    ///   new commit does not hold the name of one of its refs, without the
    ///   `@ID:` part. A commit with no such key passes, and a commit with an
    ///   empty list fails.
    ///   [`disable_verify_bindings`](super::ReceivePolicy::disable_verify_bindings)
    ///   skips this check.
    /// - [`remote::Error::BindingMismatch`] if the collection binding of a new
    ///   commit does not name the collection of the namespace of one of its
    ///   refs. For a local ref, that collection is the collection id of the
    ///   repository, and in a repository with no collection id each collection
    ///   binding fails. A commit with no collection binding fails for a ref
    ///   `@ID:NAME` of a collection other than the collection id of the
    ///   repository.
    /// - [`remote::Error::SignatureRequired`] if a new commit that the client
    ///   built fails the signature verification of the policy. A commit that
    ///   no update names gets no verification, a staged parent and a
    ///   dependency included. With
    ///   [`allow_unsigned`](super::ReceivePolicy::allow_unsigned), a commit
    ///   with no signature passes, and a commit with a signature that does not
    ///   verify fails.
    /// - [`remote::Error::RefMismatch`] if a ref is not in the state that its
    ///   update expects, or if an update writes a commit to a ref that is
    ///   present and the policy gives `t` and no `w` on it. A tag of a server
    ///   commit that is present also gives this error.
    /// - [`remote::Error::DeleteDenied`] if an update deletes a ref that is
    ///   present and the policy gives no `d` on it.
    /// - [`remote::Error::NonFastForward`] if the new commit does not descend
    ///   from the commit of the ref. If the client sends `force` and the policy
    ///   gives `w` and `d` on the ref, this check does not run. A server commit
    ///   with `orphan` on a branch that is present, where the policy gives no
    ///   `d` on the branch, also gives this error.
    /// - [`remote::Error::ChecksumMismatch`] if `[ex-ostrya] generate-composefs`
    ///   of the repository is `true`, and the composefs image of a new commit of
    ///   the session or of the server commit does not have the fs-verity
    ///   digest that its `ostree.composefs.digest.v0` records.
    /// - [`remote::Error::Internal`] if `after_update` returns an error.
    /// - [`Error::InvalidInput`] if the plan of `before_update` breaks a rule
    ///   that [`ReceiveHooks`] states. The host sends it as `internal`.
    /// - [`Error::InvalidFormat`] if a commit or a dirtree that the repository
    ///   holds does not parse.
    /// - [`Error::Core`] if the field 2 of a dependency that the repository
    ///   holds does not decode.
    /// - [`Error::Core`] or [`Error::InvalidFormat`] if a stored detached
    ///   metadata dict is not an `a{sv}` in normal form.
    /// - [`Error::InvalidFormat`] if a stored dict holds a value that is not an
    ///   `aay` under a signature key that the merge extends.
    /// - The error of the signing engine, for example [`Error::Signature`], if
    ///   a server key fails to sign a commit.
    /// - [`Error::InvalidFormat`] if a server commit takes the time of the
    ///   system clock, and the clock is before the Unix epoch.
    /// - [`Error::LockTimeout`] if the wait for the update lock passes
    ///   `[core] lock-timeout-secs`.
    /// - An I/O error from the file system, if a read of the repository or a
    ///   write of the transaction commit fails.
    pub async fn commit(&self, request: CommitRequest) -> Result<ReceiveCommit> {
        let core = {
            let mut state = self.lock();
            match &*state {
                State::Open {
                    uploads: 0,
                    haves: 0,
                    ..
                } => {}
                State::Open { .. } => {
                    let e = remote::Error::Protocol(
                        "Commit while another request of the session is in flight".into(),
                    );
                    return Err(self.refuse(state, e));
                }
                other => return Err(refusal(other)),
            }
            let State::Open { core, .. } = std::mem::replace(&mut *state, State::Committing) else {
                unreachable!("the state is open")
            };
            core
        };
        // Each step drops its clone of the core before it leaves the count,
        // so with no step counted the state held the last clone.
        let Ok(core) = Arc::try_unwrap(core) else {
            unreachable!("no step of the session holds the core")
        };
        let mut ending = Ending {
            service: self,
            reason: None,
        };
        let result = core.finish(request).await.map_err(into_error);
        ending.reason = Some(match &result {
            Ok(_) => "the session committed".into(),
            Err(e) => format!("the session was aborted: {e}"),
        });
        result
    }

    /// Ends the session with no commit.
    ///
    /// Each `objects` call in flight fails at its next read, and every later
    /// step is `protocol`. The call returns at once. A task on the blocking
    /// pool removes the staging directory in the background. If a `commit`
    /// runs, the call does not stop it and does nothing to it.
    pub fn abort(&self) {
        let core = close_locked(&mut self.lock(), "the host ended the session");
        self.closed(core);
    }

    fn lock(&self) -> MutexGuard<'_, State> {
        self.state.lock().expect("receive service mutex")
    }

    /// Counts one step of `kind` in flight and gives it a clone of the core.
    fn enter(&self, kind: Kind) -> Result<Step<'_>> {
        let mut state = self.lock();
        let (core, uploads, haves) = match &mut *state {
            State::Open {
                core,
                uploads,
                haves,
            } => (core, uploads, haves),
            other => return Err(refusal(other)),
        };
        match kind {
            Kind::Objects if *uploads == self.parallel_uploads => {
                let e = remote::Error::LimitExceeded(format!(
                    "an objects request past parallel-uploads {}",
                    self.parallel_uploads
                ));
                return Err(self.refuse(state, e));
            }
            Kind::Have if *haves == 1 => {
                let e = remote::Error::LimitExceeded(
                    "a Have request while another Have of the session is in flight".into(),
                );
                return Err(self.refuse(state, e));
            }
            Kind::Objects => *uploads += 1,
            Kind::Have => *haves += 1,
        }
        Ok(Step {
            service: self,
            kind,
            core: Some(core.clone()),
            done: false,
        })
    }

    /// Returns the error of a step that failed.
    ///
    /// A session that is open ends with the error. A session that already
    /// ended gives its own reason, because the end is the cause of the
    /// failure of the step.
    fn fail(&self, failure: Failure) -> Error {
        let mut state = self.lock();
        if let State::Closed { reason } = &*state {
            return protocol(reason.clone());
        }
        let error = into_error(failure);
        let core = close_locked(&mut state, &error.to_string());
        drop(state);
        self.closed(core);
        error
    }

    /// Refuses a step with `error` and ends the open session with it, in the
    /// same hold of the lock that `state` holds.
    fn refuse(&self, mut state: MutexGuard<'_, State>, error: remote::Error) -> Error {
        let core = close_locked(&mut state, &error.to_string());
        drop(state);
        self.closed(core);
        Error::Remote(error)
    }

    /// Completes the end of a session after the release of the lock.
    ///
    /// The call fires the signal and releases the reference of the state to
    /// the core. `None`, for a session that did not end here, does nothing.
    fn closed(&self, core: Option<Arc<Core>>) {
        if let Some(core) = core {
            self.signal.fire();
            release(core);
        }
    }
}

/// Ends an open session with `cause`, under the lock of the state.
///
/// The call returns the reference of the state to the core.
/// [`ReceiveService::closed`] releases it after the lock. A session that is
/// not open stays as it is, and the call returns `None`.
fn close_locked(state: &mut State, cause: &str) -> Option<Arc<Core>> {
    if !matches!(state, State::Open { .. }) {
        return None;
    }
    let reason = format!("the session was aborted: {cause}");
    match std::mem::replace(state, State::Closed { reason }) {
        State::Open { core, .. } => Some(core),
        _ => unreachable!("the state is open"),
    }
}

/// Drops a reference to the core.
///
/// The last reference drops the session transaction in a detached task on
/// the blocking pool, because the drop removes the staging directory. The
/// caller does not wait for it. Under the `tokio` backend with no runtime,
/// the drop runs inline.
fn release(core: Arc<Core>) {
    if let Some(core) = Arc::into_inner(core) {
        ostrya_rt::unblock_detached(move || drop(core));
    }
}

impl Drop for ReceiveService {
    /// Ends a session that is still open, with no commit.
    ///
    /// Each step borrows the service, so no step is in flight at the drop. A
    /// task on the blocking pool drops the session transaction.
    fn drop(&mut self) {
        let state = self.state.get_mut().unwrap_or_else(PoisonError::into_inner);
        let closed = State::Closed {
            reason: String::new(),
        };
        if let State::Open { core, .. } = std::mem::replace(state, closed) {
            release(core);
        }
    }
}

/// Returns the `protocol` error of a step on a session that does not take it.
fn refusal(state: &State) -> Error {
    match state {
        State::Open { .. } => unreachable!("an open session takes steps"),
        State::Committing => protocol("the session is committing"),
        State::Closed { reason } => protocol(reason.clone()),
    }
}

/// Reads one object stream of [`ReceiveService::objects`].
async fn read_objects<R: AsyncRead + Unpin>(
    core: &Core,
    signal: &AbortSignal,
    input: R,
) -> std::result::Result<ObjectsReply, Failure> {
    let input = Abortable {
        inner: input,
        signal,
        id: None,
        waker: None,
    };
    let mut reader = FrameReader::new(BufReader::with_capacity(STREAM_BUFFER, input));
    reader.set_limit(MAX_FRAME);
    let first = match next(&mut reader).await? {
        Message::ObjectHeader(header) => Some(header),
        Message::ObjectsEnd => None,
        Message::Abort => return Err(aborted()),
        other => return Err(out_of_order(&other)),
    };
    let mut buf = Vec::new();
    let reply = core.objects(first, &mut reader, &mut buf).await?;
    let mut rest = reader.into_inner();
    let mut byte = [0u8];
    let n = rest
        .read(&mut byte)
        .await
        .map_err(|e| Failure::Silent(Error::Io(e)))?;
    if n != 0 {
        return Err(Failure::Wire(remote::Error::Protocol(
            "bytes follow ObjectsEnd".into(),
        )));
    }
    Ok(reply)
}

/// One step in flight.
///
/// The drop of a step removes it from the count of the session. A step that
/// did not reach [`end`](Self::end) ends the session when it drops.
struct Step<'a> {
    service: &'a ReceiveService,
    kind: Kind,
    core: Option<Arc<Core>>,
    done: bool,
}

impl Step<'_> {
    fn core(&self) -> &Core {
        self.core.as_ref().expect("the step holds the core")
    }

    /// Returns the result of the step.
    ///
    /// A failure ends the session. A step that completes after the session
    /// ended gives the reason of the end.
    fn end<T>(mut self, result: std::result::Result<T, Failure>) -> Result<T> {
        self.done = true;
        match result {
            Ok(value) => match &*self.service.lock() {
                State::Closed { reason } => Err(protocol(reason.clone())),
                _ => Ok(value),
            },
            Err(failure) => Err(self.service.fail(failure)),
        }
    }
}

impl Drop for Step<'_> {
    fn drop(&mut self) {
        // The clone goes before the count, so a commit that counts no step
        // holds the last clone.
        if let Some(core) = self.core.take() {
            release(core);
        }
        // The count and the end of the session change in one hold of the
        // lock, so no commit takes the session between them.
        let core = {
            let mut state = self.service.lock();
            if let State::Open { uploads, haves, .. } = &mut *state {
                match self.kind {
                    Kind::Objects => *uploads -= 1,
                    Kind::Have => *haves -= 1,
                }
            }
            if self.done {
                None
            } else {
                close_locked(
                    &mut state,
                    "a request of the session ended before its reply",
                )
            }
        };
        self.service.closed(core);
    }
}

/// A guard that closes the session when `commit` ends, also when its future
/// is dropped.
struct Ending<'a> {
    service: &'a ReceiveService,
    /// The reason of the end of the session. `None` if the commit did not run
    /// to its end.
    reason: Option<String>,
}

impl Drop for Ending<'_> {
    fn drop(&mut self) {
        let reason = self.reason.take().unwrap_or_else(|| {
            "the session was aborted: the commit ended before its result".into()
        });
        *self.service.lock() = State::Closed { reason };
    }
}

/// A signal that fires once, when the session ends with no commit.
///
/// The signal wakes each object stream that waits for input.
#[derive(Default)]
struct AbortSignal {
    /// The flag of the signal. The signal sets it under the lock of `waiters`
    /// when it fires. Each read reads it with no lock first.
    fired: AtomicBool,
    waiters: Mutex<Waiters>,
}

#[derive(Default)]
struct Waiters {
    next_id: u64,
    /// The waker of each reader that waits, by its id.
    wakers: HashMap<u64, Waker>,
    /// The number of times that a reader stored a waker, for the test of the
    /// reuse.
    #[cfg(test)]
    stores: usize,
}

impl AbortSignal {
    fn fire(&self) {
        let wakers = {
            let mut waiters = self.lock();
            self.fired.store(true, Ordering::Release);
            std::mem::take(&mut waiters.wakers)
        };
        for waker in wakers.into_values() {
            waker.wake();
        }
    }

    fn lock(&self) -> MutexGuard<'_, Waiters> {
        self.waiters.lock().expect("abort signal mutex")
    }
}

/// A reader whose reads fail after the signal fires.
///
/// A read that waits for input wakes when the signal fires. A read that waits
/// again with the waker that the slot holds takes no lock.
struct Abortable<'a, R> {
    inner: R,
    signal: &'a AbortSignal,
    /// The waker slot of the reader, after its first wait.
    id: Option<u64>,
    /// The waker stored in the slot.
    waker: Option<Waker>,
}

fn aborted_read() -> io::Error {
    io::Error::new(io::ErrorKind::ConnectionAborted, "the session was aborted")
}

impl<R: AsyncRead + Unpin> AsyncRead for Abortable<'_, R> {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<io::Result<usize>> {
        let me = self.get_mut();
        if me.signal.fired.load(Ordering::Acquire) {
            return Poll::Ready(Err(aborted_read()));
        }
        if let Poll::Ready(read) = Pin::new(&mut me.inner).poll_read(cx, buf) {
            return Poll::Ready(read);
        }
        // The stored waker stays in the slot until the signal takes it and
        // wakes it. The signal sets the flag before that wake, so the next
        // poll sees the signal.
        if me.waker.as_ref().is_some_and(|w| w.will_wake(cx.waker())) {
            return Poll::Pending;
        }
        // The signal sets the flag under the lock. As a result, a check under
        // the lock sees the flag or stores the waker before the signal takes
        // it.
        let mut waiters = me.signal.lock();
        if me.signal.fired.load(Ordering::Acquire) {
            return Poll::Ready(Err(aborted_read()));
        }
        let id = *me.id.get_or_insert_with(|| {
            let id = waiters.next_id;
            waiters.next_id += 1;
            id
        });
        waiters.wakers.insert(id, cx.waker().clone());
        #[cfg(test)]
        {
            waiters.stores += 1;
        }
        me.waker = Some(cx.waker().clone());
        Poll::Pending
    }
}

impl<R> Drop for Abortable<'_, R> {
    fn drop(&mut self) {
        if let Some(id) = self.id {
            self.signal.lock().wakers.remove(&id);
        }
    }
}

// The futures of the steps are `Send`, so they can run on a thread pool.
const _: fn() = || {
    fn assert_send<T: Send>(_: T) {}
    let _ = |repo: Repo, policy: Arc<ReceivePolicy>, hello: Hello| {
        assert_send(ReceiveService::hello(repo, policy, 1, hello))
    };
    let _ = |repo: Repo,
             policy: Arc<ReceivePolicy>,
             hooks: Option<Arc<dyn ReceiveHooks>>,
             hello: Hello| {
        assert_send(ReceiveService::hello_with_hooks(
            repo, policy, hooks, 1, hello,
        ))
    };
    let _ = |service: &ReceiveService, names: Vec<ObjectName>, request: CommitRequest| {
        assert_send(service.have(names));
        assert_send(service.objects(futures_lite::io::empty()));
        assert_send(service.commit(request));
    };
};

#[cfg(test)]
mod tests {
    use std::sync::atomic::AtomicUsize;
    use std::task::Wake;

    use super::*;

    /// A reader that always waits.
    struct Waits;

    impl AsyncRead for Waits {
        fn poll_read(
            self: Pin<&mut Self>,
            _: &mut Context<'_>,
            _: &mut [u8],
        ) -> Poll<io::Result<usize>> {
            Poll::Pending
        }
    }

    /// Counts the wakes of a waker.
    struct CountWakes(AtomicUsize);

    impl Wake for CountWakes {
        fn wake(self: Arc<Self>) {
            self.0.fetch_add(1, Ordering::Relaxed);
        }
    }

    /// A read that waits again with the waker that the slot holds stores no
    /// waker. A read with another waker replaces it. The signal wakes the
    /// waker that the slot holds and fails the next read.
    #[test]
    fn a_waiting_read_stores_its_waker_once() {
        let signal = AbortSignal::default();
        let mut reader = Abortable {
            inner: Waits,
            signal: &signal,
            id: None,
            waker: None,
        };
        let first = Arc::new(CountWakes(AtomicUsize::new(0)));
        let second = Arc::new(CountWakes(AtomicUsize::new(0)));
        let first_waker = Waker::from(first.clone());
        let second_waker = Waker::from(second.clone());
        let mut buf = [0u8; 8];
        let mut poll = |waker: &Waker| {
            Pin::new(&mut reader).poll_read(&mut Context::from_waker(waker), &mut buf)
        };
        assert!(poll(&first_waker).is_pending());
        assert!(poll(&first_waker).is_pending());
        assert_eq!(signal.lock().stores, 1);
        assert!(poll(&second_waker).is_pending());
        assert_eq!(signal.lock().stores, 2);
        assert_eq!(signal.lock().wakers.len(), 1);
        signal.fire();
        assert_eq!(first.0.load(Ordering::Relaxed), 0);
        assert_eq!(second.0.load(Ordering::Relaxed), 1);
        match poll(&second_waker) {
            Poll::Ready(Err(e)) => assert_eq!(e.kind(), io::ErrorKind::ConnectionAborted),
            other => panic!("the read after the signal: {other:?}"),
        }
    }
}