ostrya 0.16.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
//! Bucket repositories: repositories in `archive` form in an S3 bucket.
//!
//! A [`BucketRepo`] holds the [`Bucket`] client of one bucket repository.
//! Each file of the repository is one object of the bucket, under the prefix
//! of the address `s3://BUCKET[/PREFIX]`. A pull reads a bucket repository
//! through [`Repo::pull`](crate::Repo::pull). A `BucketRepo` adds the init,
//! the writer lock, the listing of the objects, the fsck, the prune, and the
//! update of the summary. With the `push` feature, `Repo::push_to_bucket`
//! writes commits to a bucket repository.
//!
//! # Init
//!
//! [`BucketRepo::init`] writes the `config` of a new bucket repository under
//! the writer lock, with `If-None-Match: *`. The `config` holds the group
//! `[core]` with `repo_version=1` and `mode=archive-z2`, and
//! `collection-id=ID` when the caller gives an id. A bucket repository with
//! a collection id is the primary server of that collection. When the caller
//! gives a zlib level, the group `[archive]` with `zlib-level=LEVEL` follows.
//! The init refuses a bucket that holds a `config`, so the collection id of a
//! bucket repository never changes. A push needs the `config`.
//!
//! # The push
//!
//! A push writes the objects of its commits, and then the ref files, under
//! the writer lock. It writes no `summary` and no `summary.sig`, and leaves
//! the ones that the bucket holds as they are, until
//! [`BucketRepo::update_summary`] writes them. A commit object goes up only
//! after each object of its tree, so a commit object in the bucket has its
//! whole tree in the bucket, also after a push that failed. The one exception
//! is a commit with a partial-commit marker `state/<checksum>.commitpartial`,
//! which the fsck writes. For this reason, a push walks no tree of a commit
//! that the bucket holds, unless the commit has a marker. The push then
//! uploads the objects of the tree that the bucket lacks, and deletes the
//! marker before it writes the ref files. The refs of the
//! collection id of the bucket, or of a push with no collection, are under
//! `refs/heads/`. The refs of each other collection are under
//! `refs/mirrors/ID/`. The push, the summary update, and the prune write no
//! key under `refs/mirrors/ID/` of the collection id of the bucket. A push builds each `.filez` that it uploads at the
//! `[archive] zlib-level` of the `config` of the bucket, 6 if the `config`
//! has none, clamped to 1 to 9.
//!
//! With the `push` feature, two writers push:
//!
//! - `Repo::push_to_bucket` writes the commits of a local repository.
//! - `BucketRepo::push_tree` writes a local directory as a new commit, which
//!   it makes on the client as the receive side of a tree push makes it.
//!
//! Each of the two states its steps, its namespaces, and its failure
//! states.
//!
//! # The writer lock
//!
//! S3 has no transaction over more than one object. So each writer of a
//! bucket repository holds the writer lock: one object at the key
//! `extensions/ostrya/lock`. [`BucketRepo::lock`] takes it, and
//! [`WriterLock::release`] removes it. A read takes no lock.
//!
//! - Before a request to the lock key, a probe writes the fixed body
//!   `ostrya lock probe\n` to the key `extensions/ostrya/lock-probe` with
//!   `If-None-Match: *`. If the request wrote the key, the probe sends it
//!   again. A server that honors the header refuses one of the two requests
//!   with 412. A server that writes the second request gives no exclusion.
//!   The writer then fails with
//!   [`Error::BucketNoExclusion`](crate::Error::BucketNoExclusion), and it
//!   wrote no lock. `rclone serve s3` 1.75.1 is such a server, and
//!   SeaweedFS 4.36 honors the header. No writer deletes the probe key.
//! - The take writes the lock with `If-None-Match: *`. If the key holds a
//!   lock, the take fails with
//!   [`Error::BucketLocked`](crate::Error::BucketLocked), which names the
//!   holder. A take with no break of the lock then writes only the probe
//!   key.
//! - [`WriterLock::check`] reads the lock and compares its id with the id of
//!   the writer. A writer checks before each change that the lock guards.
//!   A lock with another id, or no lock, gives
//!   [`Error::BucketLockLost`](crate::Error::BucketLockLost).
//! - [`WriterLock::release`] reads the lock, and deletes it with `If-Match`
//!   and the `ETag` of the read if it holds the id of the writer.
//! - With `break_lock`, [`BucketRepo::lock`] deletes the lock that the key
//!   holds after the probe, with `If-Match` and the `ETag` of its read, and
//!   then takes its own. [`WriterLock::broken`] gives the holder of the
//!   deleted lock. If the take after the delete fails, the deleted lock
//!   stays deleted.
//!
//! The lock has no lease. A lock that a writer leaves when it ends with no
//! release stays until a break of the lock removes it. A dropped
//! [`WriterLock`] sends no request, so its lock stays too.
//!
//! The lock has two windows:
//!
//! - A writer that breaks the lock and takes its own after a check, and
//!   before the change that the check guards, does not stop that change.
//! - A server that does not honor `If-Match` deletes the lock whatever its
//!   `ETag` is. A writer that breaks the lock and takes its own between the
//!   read and the delete of a release or of a break then loses its lock.
//!   `rclone serve s3` 1.75.1 is such a server, and SeaweedFS 4.36 honors
//!   the header.
//!
//! # The listing
//!
//! [`BucketRepo::list_objects`] lists the objects of the repository with one
//! list of each of the 256 fan-out prefixes `objects/XX/`. At most
//! `max_outstanding` lists run at a time, as [`BucketRepo::new`] states.
//! The [`ObjectListing`] holds each object with its size, and each
//! stray key under a fan-out prefix. A key outside the fan-out prefixes,
//! for example `objects/abc`, is not in it. The fsck finds such a key with
//! one more list of `objects/` with the delimiter `/`.
//!
//! # The fsck
//!
//! [`BucketRepo::fsck`] checks a bucket repository, and gives a
//! [`BucketFsckReport`]. Each [`BucketFinding`] is an error or a note. The
//! run checks:
//!
//! - The `config`, as a writer reads it.
//! - The ref files under `refs/`: each key is a ref of `refs/heads/` or of
//!   `refs/mirrors/ID/`, each file holds a checksum, and each commit of a
//!   ref is in the bucket. A ref of `refs/mirrors/ID/` of the collection id
//!   of the bucket gets a note, and the run reads no such ref file.
//! - The summary: it parses, and with
//!   [`summary_keys`](BucketFsckOptions::summary_keys), its signature
//!   verifies. A summary ref whose commit is absent, and a summary delta
//!   whose superblock is absent or has another digest, are errors. A summary
//!   that differs from the ref files or from the deltas in other ways, and an
//!   absent summary, are notes: the summary of a bucket repository changes
//!   only with [`BucketRepo::update_summary`].
//! - The keys under `objects/` that are no object keys.
//! - Each commit object of the listing, each dirtree and dirmeta of the tree
//!   of each commit, and each `.filez` of these trees. Each object must be
//!   in the listing, have the checksum of its name, and parse. A `.filez`
//!   must inflate to its declared size with no bytes after its payload.
//! - The attachments in field 2 of each commit that parses, the commits
//!   with a partial-commit marker included. Each entry must have a name that
//!   is not empty and a checksum of 32 bytes. The listing must hold the
//!   commit of each attachment, and the bucket must hold no partial-commit
//!   marker of it. An attachment with an absent or a partial attachment of
//!   its own is partial, at each depth. Each fault is an `Attachment`
//!   finding. With `delete`, the check reads the state after the repair.
//! - The partial-commit markers of the commits whose trees are whole, the
//!   incomplete multipart uploads, and a writer lock at the start or at the
//!   end of the run. These are notes.
//!
//! A run without [`delete`](BucketFsckOptions::delete) takes no lock, so a
//! writer can change the bucket during the run, and the listing of the 256
//! fan-out prefixes is no snapshot. Before the run reports an object that
//! the listing does not hold, it reads the object again, and then the
//! commits of the object. An object that is in the bucket now is a note, and
//! an object whose commits are all gone is no finding. The run also reads
//! again the commit of each attachment that the listing does not hold, and
//! a commit that is in the bucket now makes its finding a note. The run then
//! drops each partial finding that an attachment below it gives, if no
//! `Absent` or `Partial` attachment error stays on that attachment.
//!
//! With `delete`, the run takes the writer lock with the operation `fsck`.
//! It writes the marker `state/<checksum>.commitpartial`, with the one byte
//! `0x66`, for each commit that reaches a missing or a bad object of a tree,
//! deletes the marker of each commit whose tree is whole, and then deletes
//! each bad object. It deletes no commit because of a bad object of its
//! tree, so each ref keeps its commit, unless the object of the commit is
//! bad. A commit object whose own bytes are bad goes, also when a ref names
//! it, the report names the ref, and a push of the ref uploads the commit
//! again. The run checks the attachments against the state after the
//! repair: an attachment whose marker the run deletes is not partial, an
//! attachment that gets a marker is partial, an attachment whose commit
//! object the run deletes is absent, and a commit whose own object the run
//! deletes gets no check. The run writes and deletes no key for an
//! `Attachment` finding. A push of a ref whose commit has a marker uploads
//! the missing objects of its tree. A repair that fails after its first change gives
//! [`Error::BucketRepairIncomplete`](crate::Error::BucketRepairIncomplete),
//! with the report of the changes that completed: run the fsck with
//! `delete` again before a push.
//!
//! # The prune
//!
//! [`BucketRepo::prune`] deletes the objects that no root reaches, and gives
//! a [`BucketPruneReport`]. It takes the [`PruneOptions`](crate::PruneOptions)
//! of [`Repo::prune`](crate::Repo::prune), and refuses each option other than
//! `refs_only`, `depth`, `keep_younger_than`, and `no_prune`. The run takes
//! the writer lock with the operation `prune`, also with `no_prune`, and
//! refuses `break_lock` with `no_prune`.
//!
//! - The roots are the commits of the refs, the refs of `refs/mirrors/ID/`
//!   of the collection id of the bucket included, the commits that the
//!   `summary` names, the attachments in field 2 of each commit that the walk
//!   reaches, and, unless the roots are on the refs alone, each commit of
//!   the listing. The objects that the run keeps are the objects that a
//!   local prune keeps with the same options, refs, and objects, and the
//!   objects of the commits of the summary and of their attachments, under
//!   the bound of the run. The run reads field 2 of each commit
//!   that it reaches, and keeps the attachments as a local prune keeps
//!   them. It reads no detached metadata, so it deletes a supplement that
//!   no other root reaches, which a local prune keeps. Each defective
//!   field 2 entry gives a warning in the report. Unless the roots are on
//!   the refs alone, the run also deletes the detached metadata of a parent
//!   commit that the listing does not hold.
//! - The walk reads each commit and dirtree that it reaches, and checks
//!   each one against its name. It reads the commits one at a time, and at
//!   most `max_outstanding` dirtrees of the tree of one commit at a time,
//!   as [`BucketRepo::new`] gives it. An object that the listing does
//!   not hold is absent, and the walk sends no request for it. A commit or a
//!   dirtree whose bytes do not match its name stops the run before its
//!   first delete. A run of the fsck with `delete` repairs such an object.
//! - The run deletes the commits first, each with its detached metadata,
//!   then the detached metadata whose commits are absent, then each other
//!   object, then the static deltas whose target commits are absent, each
//!   superblock before the other keys of its delta, then the partial-commit
//!   markers whose commits are absent. Then it aborts each incomplete
//!   multipart upload. The run checks the lock before each of these steps
//!   that has a change to make, and after each 1000 deletes or aborts of a
//!   step. The four steps are: the commits with the detached metadata of
//!   absent commits, the other objects, the static deltas with the markers,
//!   and the aborts.
//! - The run deletes no ref, no `.tombstone-commit`, and no key under
//!   `delta-indexes/`, and writes no summary. A summary that names a deleted
//!   static delta stays as it is until [`BucketRepo::update_summary`] writes
//!   the summary again.
//!
//! A run that fails after its first delete leaves a bucket repository that a
//! reader can use, and a later run deletes the rest. An object that only a
//! dirtree that the fsck deleted reaches is unreachable, so the prune
//! deletes it, and a push of the commit of that dirtree uploads it again.
//!
//! # The summary update
//!
//! [`BucketRepo::update_summary`] is the one writer of `summary` and
//! `summary.sig`, and gives a [`BucketSummaryReport`]. The run takes the
//! writer lock with the operation `summary`. It reads the refs, the commit
//! object of each ref, and the superblock of each static delta, and writes
//! the root summary of the bucket repository:
//!
//! - Field 0 holds the refs of `refs/heads/`, and
//!   `ostree.summary.collection-map` holds the refs of each
//!   `refs/mirrors/ID/`. The run skips the keys of `refs/mirrors/ID/` of
//!   the collection id of the bucket.
//! - `ostree.static-deltas` holds each delta with a superblock, with the
//!   SHA-256 of its superblock. Each read of a superblock takes the strict
//!   rule of an absent key, so only a superblock gone with an answer 404
//!   `NoSuchKey` is left out, and each other 404 fails the run.
//! - The other keys come from the `[core]` group of the `config` of the
//!   bucket.
//!
//! The bytes are the bytes of the root summary that a local repository
//! with the same refs, commits, deltas, `config`, and `last-modified` writes,
//! with no `ostree-metadata` anchor commit. The run writes `summary`, and
//! then `summary.sig` when it has a signer. It never deletes `summary.sig`,
//! and a bucket repository that holds a `summary.sig` refuses a run with no
//! signer. Only a run with no signer probes `summary.sig`, by the strict
//! rule of an absent key. Between the two writes, a reader that verifies
//! the summary refuses it, because the old signatures do not cover the new
//! summary. A read after a run that succeeds finds a matching pair. A
//! signed run that can leave a pair that does not match gives
//! [`Error::BucketSummaryIncomplete`](crate::Error::BucketSummaryIncomplete):
//! run the update again with the same signers.
//!
//! # Examples
//!
//! ```no_run
//! use ostrya::bucket::{BucketRepo, LockOperation};
//! use ostrya::remote::s3::{Environment, S3Endpoint, Settings};
//! # async fn run() -> ostrya::Result<()> {
//! let endpoint =
//!     S3Endpoint::resolve("s3://builds/exampleos", &Settings::default(), &Environment::Process)
//!         .await?;
//! let repo = BucketRepo::open(&endpoint).await?;
//! let lock = repo.lock(LockOperation::Prune, false).await?;
//! let listing = repo.list_objects().await?;
//! println!("{} objects", listing.objects.len());
//! lock.check().await?;
//! lock.release().await?;
//! # Ok(()) }
//! ```

use std::future::Future;

use ostrya_core::{ObjectName, RepoMode};

use crate::error::Result;
use crate::pull::drive::Slots;
use crate::remote::PushOutcome;
use crate::remote::s3::{Address, Bucket, PutIfAbsent, S3Endpoint};

mod fsck;
mod listing;
mod lock;
mod prune;
mod state;
mod summary;
mod walk;

pub use fsck::{BucketFinding, BucketFindingKind, BucketFsckOptions, BucketFsckReport};
pub use listing::ObjectListing;
pub use lock::{LockHolder, LockOperation, WriterLock};
pub(crate) use lock::{describe_locked, describe_lost};
pub use prune::{BucketPruneOptions, BucketPruneReport};
#[cfg(test)]
pub(crate) use state::{BucketRefs, summary_refs};
#[cfg_attr(not(feature = "push"), allow(unused_imports))]
pub(crate) use state::{check_prefixes, marker_key};
pub(crate) use summary::describe_summary_incomplete;
pub use summary::{BucketSummaryOptions, BucketSummaryReport};

/// The result of a push to a bucket repository: `Repo::push_to_bucket` or
/// `BucketRepo::push_tree`.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct BucketPushOutcome {
    /// The ref outcomes and the statistics of the push.
    ///
    /// Each ref outcome names the destination of its refspec, or the branch
    /// of a tree push. The namespace of the push decides its key:
    /// `refs/heads/DST` or `refs/mirrors/ID/DST`. The commit is `None` for
    /// `Repo::push_to_bucket`, and the new commit for `BucketRepo::push_tree`.
    pub outcome: PushOutcome,
    /// The holder of the lock that the break of the lock deleted, or `None`
    /// if the push deleted no lock.
    ///
    /// The holder is reported on success alone. A push that breaks the lock
    /// and then fails returns its error, and the holder of the deleted lock
    /// is not in it. [`BucketRepo::lock_holder`] reads the holder before the
    /// push.
    pub broken_lock: Option<LockHolder>,
}

/// The options of a writer of a bucket repository other than its own
/// inputs.
///
/// `BucketRepo::push_tree` takes them. A caller sets the fields of
/// [`default`](Default::default), because the struct is
/// `#[non_exhaustive]`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct BucketWriteOptions {
    /// Remove the writer lock of another writer before the writer takes its
    /// own, as [`BucketRepo::lock`] states.
    pub break_lock: bool,
    /// The part size of a multipart upload, at least 5 MiB, or `None` for
    /// the rule of [`part_size`](crate::remote::s3::part_size). For tests.
    #[doc(hidden)]
    pub part_size: Option<u64>,
}

/// A bucket repository: a repository in `archive` form in an S3 bucket.
///
/// The [module documentation](self) states the writer lock and the listing.
#[derive(Debug)]
pub struct BucketRepo {
    bucket: Bucket,
    /// The bound of the requests in flight of the repository.
    max_outstanding: usize,
}

impl BucketRepo {
    /// Opens the bucket repository of `endpoint`.
    ///
    /// The call builds the bucket client, as [`Bucket::new`] does. It sends
    /// no request and reads no object of the bucket. The
    /// [`max_outstanding`](crate::fetch::FetcherOptions::max_outstanding) of
    /// the HTTP options of `endpoint` bounds the requests in flight of the
    /// repository, as [`new`](BucketRepo::new) states.
    ///
    /// # Errors
    ///
    /// [`Error::Remote`](crate::Error::Remote) with the error of
    /// [`Bucket::new`].
    pub async fn open(endpoint: &S3Endpoint) -> Result<BucketRepo> {
        Ok(BucketRepo::new(
            Bucket::new(endpoint).await?,
            endpoint.http.max_outstanding,
        ))
    }

    /// Returns the bucket repository of the client `bucket`. The call sends
    /// no request.
    ///
    /// `max_outstanding` bounds the requests in flight of one call on the
    /// repository. Calls that run at the same time, such as the listing of
    /// the objects and the reads of the ref files of a push, each have the
    /// bound. The requests are the lists of
    /// [`list_objects`](BucketRepo::list_objects), the reads of the ref files
    /// and of the objects, the uploads and the other writes, and the deletes
    /// and the aborts. A value of zero is one.
    /// The admission limit of the fetcher of `bucket` is the value to give.
    pub fn new(bucket: Bucket, max_outstanding: usize) -> BucketRepo {
        BucketRepo {
            bucket,
            max_outstanding,
        }
    }

    /// Returns the bucket client of the repository.
    pub fn bucket(&self) -> &Bucket {
        &self.bucket
    }

    /// Returns the address of the repository, `s3://BUCKET[/PREFIX]`.
    pub fn address(&self) -> &Address {
        self.bucket.location().address()
    }

    /// Takes the writer lock for `operation`.
    ///
    /// The call first probes that the server honors `If-None-Match`, on the
    /// probe key, and then writes the lock body with `If-None-Match: *`. The
    /// [module documentation](self#the-writer-lock) states the rules. With
    /// `break_lock`, the call reads the lock that the key holds after the
    /// probe, and deletes it with `If-Match` and the `ETag` of the read.
    /// [`WriterLock::broken`] then gives its holder. If the delete gets 412
    /// and a read after it finds no lock, the call goes on to the take, and
    /// [`WriterLock::broken`] gives `None`.
    ///
    /// The probe sends no request to the lock key. It writes the probe body
    /// to the probe key with `If-None-Match: *`, and a 412 shows that the
    /// server honors the header. If the first request wrote the key, or had
    /// an unknown outcome and a read found the probe body, the probe sends a
    /// second request. A server that writes the second request does not
    /// honor the header. If the second request has an unknown outcome and a
    /// read finds the probe body, the call fails, and the probe sends no
    /// third request. A read after a request with an unknown outcome that
    /// finds other bytes counts as a 412. Only a program other than ostrya
    /// writes other bytes to the probe key. The probe runs on each take, and
    /// no writer deletes the probe key.
    ///
    /// A take whose request has an unknown outcome reads the key. If the key
    /// holds the body of this take, the lock is taken: the body holds an id
    /// that no other run makes.
    ///
    /// # Errors
    ///
    /// - [`Error::BucketLocked`](crate::Error::BucketLocked) if the key
    ///   holds a lock. The holder is `None` if the lock was gone at the read
    ///   after the take. With `break_lock`, also if the lock changed between
    ///   the read and the delete, or if another writer took the lock after
    ///   the delete.
    /// - [`Error::BucketNoExclusion`](crate::Error::BucketNoExclusion) if the
    ///   server writes the second request of the probe. The call then sent
    ///   no request to the lock key.
    /// - [`Error::Remote`](crate::Error::Remote) with
    ///   [`Transport`](crate::remote::Error::Transport) if the probe had an
    ///   unknown outcome: the probe key holds the probe body, and no request
    ///   of the probe got 412. The call then sent no request to the lock
    ///   key. Also with `break_lock`, if the read of the lock gives no
    ///   `ETag`. The call then deletes no lock.
    /// - [`Error::Remote`](crate::Error::Remote) for an error of the bucket
    ///   client. An error of the probe comes before a request to the lock
    ///   key.
    pub async fn lock(&self, operation: LockOperation, break_lock: bool) -> Result<WriterLock<'_>> {
        self.take_lock(operation, break_lock).await
    }

    /// Reads the lock that the key holds, with no change to the bucket.
    ///
    /// Returns `None` if the key holds no lock.
    ///
    /// # Errors
    ///
    /// [`Error::Remote`](crate::Error::Remote) for an error of the bucket
    /// client.
    pub async fn lock_holder(&self) -> Result<Option<LockHolder>> {
        self.read_holder().await
    }

    /// Writes the `config` of a new bucket repository.
    ///
    /// The call takes the writer lock for [`LockOperation::Init`], writes
    /// `config` with `If-None-Match: *`, and releases the lock. The `config`
    /// holds the group `[core]` with `repo_version=1` and `mode=archive-z2`,
    /// each on one line, and `collection-id=ID` after them when
    /// `collection_id` is `Some`. The bucket repository is then the primary
    /// server of that collection. When `zlib_level` is `Some`, an empty line
    /// and the group `[archive]` with `zlib-level=LEVEL` follow. Without it,
    /// the `config` has no `[archive]` group, and a writer takes the level 6.
    /// The init writes no summary and no ref.
    ///
    /// With `break_lock`, the call removes the writer lock of another writer
    /// first, as [`lock`](BucketRepo::lock) states. It returns the holder of
    /// the deleted lock, or `None` if it deleted no lock. The holder is
    /// reported on success alone: an init that breaks the lock and then
    /// fails returns its error, and the holder of the deleted lock is not in
    /// it. [`lock_holder`](BucketRepo::lock_holder) reads the holder before
    /// the init.
    ///
    /// A failure before the write of `config` changes nothing but the lock
    /// and the probe key, and the call releases the lock. A failure after
    /// the write leaves the `config`, and the bucket repository is ready for
    /// a push. A crash leaves the lock, as for each writer.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidInput`](crate::Error::InvalidInput) if
    ///   `collection_id` fails [`is_collection_id`](crate::is_collection_id)
    ///   or is longer than 255 bytes, or if `zlib_level` is outside 1 to 9.
    ///   The call then sends no request.
    /// - [`Error::InvalidInput`](crate::Error::InvalidInput) if the bucket
    ///   holds a `config`. The call leaves it unchanged, so the collection id
    ///   of a bucket repository never changes.
    /// - The errors of [`lock`](BucketRepo::lock) and of
    ///   [`WriterLock::release`].
    /// - [`Error::Remote`](crate::Error::Remote) for an error of the bucket
    ///   client. The call releases the lock first, and drops an error of that
    ///   release.
    pub async fn init(
        &self,
        collection_id: Option<&str>,
        zlib_level: Option<u8>,
        break_lock: bool,
    ) -> Result<Option<LockHolder>> {
        if let Some(id) = collection_id
            && !ostrya_core::is_valid_collection_id(id)
        {
            return Err(crate::Error::InvalidInput(format!(
                "the collection id '{}' of a bucket repository fails the collection id rule or \
                 is longer than 255 bytes",
                id.escape_debug()
            )));
        }
        if let Some(level) = zlib_level
            && !(1..=9).contains(&level)
        {
            return Err(crate::Error::InvalidInput(format!(
                "the zlib level {level} of a bucket repository is outside 1 to 9"
            )));
        }
        let lock = self.lock(LockOperation::Init, break_lock).await?;
        let broken = lock.broken().cloned();
        let present = match self
            .bucket
            .put_if_absent("config", config_bytes(collection_id, zlib_level))
            .await
        {
            Ok(PutIfAbsent::Created { .. })
            | Ok(PutIfAbsent::Matches { .. })
            | Ok(PutIfAbsent::Refused { .. }) => false,
            Ok(_) => true,
            Err(e) => {
                let _ = lock.release().await;
                return Err(e.into());
            }
        };
        if present {
            let _ = lock.release().await;
            return Err(crate::Error::InvalidInput(format!(
                "the bucket repository {} holds a config already; its collection id never \
                 changes",
                self.address()
            )));
        }
        lock.release().await?;
        Ok(broken)
    }

    /// Lists the objects of the repository.
    ///
    /// The call sends one list of each fan-out prefix `objects/XX/`. At
    /// most `max_outstanding` lists run at a time, as
    /// [`new`](BucketRepo::new) states. A key outside the fan-out prefixes,
    /// for example `objects/abc` or `objects/zz/x`, is not in the result.
    ///
    /// # Errors
    ///
    /// [`Error::Remote`](crate::Error::Remote) with the error of the first
    /// list that fails, as [`Bucket::list`](crate::remote::s3::Bucket::list)
    /// states. The call then drops the other lists.
    pub async fn list_objects(&self) -> Result<ObjectListing> {
        self.list_fanouts().await
    }

    /// Returns the bound of the requests in flight.
    #[cfg_attr(not(feature = "push"), allow(dead_code))]
    pub(crate) fn max_outstanding(&self) -> usize {
        self.max_outstanding
    }
}

/// Returns the bytes of the `config` of a new bucket repository, with the
/// collection id `collection_id` and the zlib level `zlib_level`.
fn config_bytes(collection_id: Option<&str>, zlib_level: Option<u8>) -> Vec<u8> {
    let mut text = String::from("[core]\nrepo_version=1\nmode=archive-z2\n");
    if let Some(id) = collection_id {
        text.push_str(&format!("collection-id={id}\n"));
    }
    if let Some(level) = zlib_level {
        text.push_str(&format!("\n[archive]\nzlib-level={level}\n"));
    }
    text.into_bytes()
}

/// Returns the bucket key of the object `name`.
pub(crate) fn object_key(name: &ObjectName) -> String {
    format!("objects/{}", name.loose_path(RepoMode::Archive))
}

/// Returns the error of a request of the caller that the bucket repository
/// refuses.
pub(crate) fn invalid(msg: impl Into<String>) -> crate::Error {
    crate::Error::Remote(crate::remote::Error::InvalidInput(msg.into()))
}

/// Returns the error of a writer that failed with `e` while it held `lock`.
///
/// The call releases the lock and drops an error of the release, unless the
/// failure is a lost lock or a lock of another writer.
pub(crate) async fn release_on_error(lock: WriterLock<'_>, e: crate::Error) -> crate::Error {
    if !matches!(
        e,
        crate::Error::BucketLockLost { .. } | crate::Error::BucketLocked { .. }
    ) {
        let _ = lock.release().await;
    }
    e
}

/// Runs each future of `ops`, at most `max` at a time, and gives the output
/// of each one that succeeds to `done`.
///
/// After a failure, the call starts no new future, waits for the futures in
/// flight, and returns the first failure. So `done` sees each change that
/// completed.
async fn each_slot<'a, T, Fut>(
    max: usize,
    ops: impl Iterator<Item = Fut>,
    mut done: impl FnMut(T),
) -> Result<()>
where
    Fut: Future<Output = Result<T>> + Send + 'a,
{
    let mut slots = Slots::new(max);
    let mut ops = ops;
    let mut failure = None;
    loop {
        while failure.is_none()
            && slots.has_room()
            && let Some(op) = ops.next()
        {
            slots.push(op);
        }
        let Some(result) = slots.next_ready().await else {
            break;
        };
        match result {
            Ok(output) => done(output),
            Err(e) => {
                failure.get_or_insert(e);
            }
        }
    }
    failure.map_or(Ok(()), Err)
}

const _: fn() = || {
    fn assert_send_sync<T: Send + Sync>() {}
    assert_send_sync::<BucketRepo>();
    assert_send_sync::<WriterLock<'_>>();
    assert_send_sync::<LockHolder>();
    assert_send_sync::<LockOperation>();
    assert_send_sync::<ObjectListing>();
    assert_send_sync::<BucketPushOutcome>();
    assert_send_sync::<BucketWriteOptions>();
    assert_send_sync::<BucketFsckOptions>();
    assert_send_sync::<BucketFsckReport>();
    assert_send_sync::<BucketFinding>();
    assert_send_sync::<BucketFindingKind>();
    assert_send_sync::<BucketPruneOptions>();
    assert_send_sync::<BucketPruneReport>();
    assert_send_sync::<BucketSummaryOptions>();
    assert_send_sync::<BucketSummaryReport>();
};

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_config_of_a_new_bucket_repository_has_three_or_four_lines() {
        assert_eq!(
            config_bytes(None, None),
            b"[core]\nrepo_version=1\nmode=archive-z2\n"
        );
        assert_eq!(
            config_bytes(Some("org.example.Os"), None),
            b"[core]\nrepo_version=1\nmode=archive-z2\ncollection-id=org.example.Os\n"
        );
        let config = crate::config::RepoConfig::parse(
            std::str::from_utf8(&config_bytes(Some("org.example.Os"), None)).unwrap(),
        )
        .unwrap();
        assert_eq!(config.mode(), ostrya_core::RepoMode::Archive);
        assert_eq!(config.collection_id(), Some("org.example.Os"));
        assert_eq!(config.zlib_level().unwrap(), 6);
    }

    #[test]
    fn a_zlib_level_adds_the_archive_group() {
        let bytes = config_bytes(Some("org.example.Os"), Some(9));
        assert_eq!(
            bytes,
            b"[core]\nrepo_version=1\nmode=archive-z2\ncollection-id=org.example.Os\n\n\
              [archive]\nzlib-level=9\n"
        );
        let config =
            crate::config::RepoConfig::parse(std::str::from_utf8(&bytes).unwrap()).unwrap();
        assert_eq!(config.zlib_level().unwrap(), 9);
        assert_eq!(config.collection_id(), Some("org.example.Os"));
        assert_eq!(
            config_bytes(None, Some(1)),
            b"[core]\nrepo_version=1\nmode=archive-z2\n\n[archive]\nzlib-level=1\n"
        );
    }
}