ostrya 0.9.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
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
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
//! The prune of a bucket repository.

use std::collections::{BTreeMap, BTreeSet, HashSet};

use futures_lite::future::zip;
use ostrya_core::{Checksum, ObjectName, ObjectType, RepoMode};

use super::lock::{LockHolder, LockOperation, WriterLock};
use super::state::{BucketRefs, marker_key};
use super::{BucketRepo, ObjectListing, each_slot, invalid, object_key, release_on_error};
use crate::delta::parse_delta_dir;
use crate::deltagen::delta_name;
use crate::error::{Error, Result};
use crate::object::MAX_METADATA_SIZE;
use crate::prune::{PruneOptions, PruneStats, counted};
use crate::related::RelatedWarning;
use crate::remote::s3::{Bucket, Upload, is_repository_key};
use crate::summary::{SUMMARY_READ_CAP, Summary};
use crate::traverse::{GcRoots, MetadataSource, ParentBound, walk_reachable_gc};

/// The most deletes or aborts that a phase sends after one check of the
/// lock.
const CHECK_INTERVAL: usize = 1000;

/// The size above which the walk hashes a metadata object on the blocking
/// pool: 1 MiB.
pub(super) const UNBLOCK_SIZE: usize = 1024 * 1024;

/// The options of [`BucketRepo::prune`] other than the [`PruneOptions`].
///
/// A caller sets the fields of [`default`](Default::default), because the
/// struct is `#[non_exhaustive]`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct BucketPruneOptions {
    /// Remove the writer lock of another writer before the run takes its
    /// own, as [`BucketRepo::lock`] states. The run refuses this option with
    /// [`no_prune`](PruneOptions::no_prune).
    pub break_lock: bool,
}

/// The result of [`BucketRepo::prune`].
///
/// With [`no_prune`](PruneOptions::no_prune), each list names what a run
/// without `no_prune` deletes or aborts, and the run changes nothing but the
/// probe key of the writer lock.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct BucketPruneReport {
    /// The counts of the run, as the counts of
    /// [`Repo::prune`](crate::Repo::prune) state them.
    ///
    /// The sizes come from the listing of the objects. A delete of a key
    /// that holds no object counts too.
    /// [`deleted_refs`](PruneStats::deleted_refs) and
    /// [`deleted_collection_refs`](PruneStats::deleted_collection_refs) are
    /// always empty, because the run deletes no ref.
    /// [`warnings`](PruneStats::warnings) holds one warning for each
    /// defective field 2 entry that the walk read. It holds no warning about
    /// `ostrya.related`, because the run reads no detached metadata.
    pub stats: PruneStats,
    /// The name of each static delta whose keys the run deleted, sorted.
    ///
    /// The name is the hex of the target commit for a delta from scratch,
    /// and `FROM-TO` for other deltas.
    pub deleted_deltas: Vec<String>,
    /// Each commit whose partial-commit marker the run deleted, sorted.
    pub deleted_markers: Vec<Checksum>,
    /// Each incomplete multipart upload that the run aborted, sorted by key,
    /// then by upload id.
    pub aborted_uploads: Vec<Upload>,
    /// The holder of the lock that the break of the lock deleted, or `None`
    /// if the run deleted no lock.
    ///
    /// The holder is reported on success alone. A run that breaks the lock
    /// and then fails returns its error, and the holder of the deleted lock
    /// is not in it.
    pub broken_lock: Option<LockHolder>,
}

/// The objects that the walk of a prune reads: the objects that the listing
/// of the run holds.
///
/// The run holds the writer lock, so an object that the listing does not
/// hold is absent, and the source sends no request for it. A read takes at
/// most the size of the object in the listing. The source checks the
/// SHA-256 of each commit and dirtree that it reads against its name. The
/// walk of one tree has at most `max_outstanding` reads in flight. Each read
/// holds the bytes of its object in memory, so the reads in flight hold at
/// most `max_outstanding` objects, each at most its size in the listing and
/// at most [`MAX_METADATA_SIZE`].
struct ListedObjects<'a> {
    repo: &'a BucketRepo,
    listing: &'a ObjectListing,
}

impl MetadataSource for ListedObjects<'_> {
    async fn load_metadata(&self, ty: ObjectType, checksum: Checksum) -> Result<Option<Vec<u8>>> {
        let name = ObjectName::new(checksum, ty);
        let Some(&listed) = self.listing.objects.get(&name) else {
            return Ok(None);
        };
        let key = object_key(&name);
        if listed > MAX_METADATA_SIZE {
            return Err(self.repo.malformed(format!(
                "the object {key} is larger than {MAX_METADATA_SIZE} bytes"
            )));
        }
        let bytes = match self.repo.bucket.get_strict(&key, listed).await {
            Ok(Some(object)) => object.bytes,
            Ok(None) => {
                return Err(self.repo.malformed(format!(
                    "the object {key} was in the listing and is gone at its read"
                )));
            }
            Err(crate::remote::Error::LimitExceeded(_)) => {
                return Err(self.repo.malformed(format!(
                    "the object {key} is larger than its size in the listing, {listed} bytes"
                )));
            }
            Err(e) => return Err(e.into()),
        };
        if !matches!(ty, ObjectType::Commit | ObjectType::DirTree) {
            return Ok(Some(bytes));
        }
        let hash = move || (Checksum::sha256(&bytes), bytes);
        let (actual, bytes) = if listed > UNBLOCK_SIZE as u64 {
            ostrya_rt::unblock(hash).await
        } else {
            hash()
        };
        if actual != checksum {
            return Err(self.repo.malformed(format!(
                "the object {key} has the checksum {actual}, which is not the checksum of its \
                 name; the prune deletes nothing until `ostrya remote fsck --delete` repairs \
                 the repository"
            )));
        }
        Ok(Some(bytes))
    }

    fn max_reads(&self) -> usize {
        self.repo.max_outstanding
    }
}

/// The changes of one run, in the order of the run.
#[derive(Debug, PartialEq, Eq)]
struct Plan {
    /// The key of each commit object to delete, each with the keys of its
    /// detached metadata and of its composefs image when these go too,
    /// sorted by commit.
    commits: Vec<Vec<String>>,
    /// Each detached metadata and each composefs image to delete whose
    /// commit object the listing does not hold, in the order of the keys.
    orphans: Vec<ObjectName>,
    /// Each other object to delete, in the order of the keys.
    content: Vec<ObjectName>,
    /// The superblock key of each static delta to delete, sorted.
    superblocks: Vec<Vec<String>>,
    /// The other keys of each static delta to delete, sorted.
    parts: Vec<Vec<String>>,
    /// The commits whose partial-commit markers go, sorted.
    markers: Vec<Checksum>,
    /// The name of each static delta to delete, sorted.
    deltas: Vec<String>,
    /// The counts of the run.
    stats: PruneStats,
}

impl BucketRepo {
    /// Deletes the objects of the bucket repository that no root reaches,
    /// with the static deltas and the partial-commit markers of the commits
    /// that are not in the bucket after the run, and aborts each incomplete
    /// multipart upload.
    ///
    /// The run keeps the objects that [`Repo::prune`](crate::Repo::prune)
    /// keeps with the same options in a local repository with the same
    /// objects and refs, and the commits that the summary names, with their
    /// dependencies under the bound of the run. A kept commit keeps its
    /// detached metadata and its composefs image. Two differences apply:
    ///
    /// - The run reads field 2 of each commit that the walk reaches, and keeps
    ///   the dependencies as [`Repo::prune`](crate::Repo::prune) keeps them
    ///   ("Related commits"). It reads no detached metadata, so it reads no
    ///   build source. Thus it deletes a build source that no other root
    ///   reaches, and the objects that only that commit reaches. It also
    ///   deletes the detached metadata and the composefs image of a build
    ///   source that the listing does not hold. `Repo::prune` keeps these.
    /// - Unless the roots are on the refs alone, the run also deletes the
    ///   detached metadata and the composefs image of a parent commit that
    ///   the listing does not hold. `Repo::prune` keeps these when its bound
    ///   reaches that parent.
    ///
    /// The run deletes no ref, writes no summary, and leaves the `summary`, the
    /// `summary.sig`, the `delta-indexes/`, and each `.tombstone-commit` as
    /// they are. The bucket repository module states the run under "The
    /// prune".
    ///
    /// # Options
    ///
    /// The run takes [`refs_only`](PruneOptions::refs_only),
    /// [`depth`](PruneOptions::depth),
    /// [`keep_younger_than`](PruneOptions::keep_younger_than), and
    /// [`no_prune`](PruneOptions::no_prune), as [`Repo::prune`](crate::Repo::prune)
    /// takes them. Each other option must keep its default value. With
    /// `no_prune`, [`break_lock`](BucketPruneOptions::break_lock) must be
    /// false.
    ///
    /// # Roots
    ///
    /// The commit of each ref is a root under the bound of the run. The
    /// commit of each ref of the summary, field 0 and
    /// `ostree.summary.collection-map`, is a root that keeps no parent.
    /// Unless the roots are on the refs alone, each commit of the listing is
    /// a root too. In that case, each root keeps no parent, so the walk reads
    /// each commit of the listing once: each commit of the listing is a root
    /// of its own. With `keep_younger_than`, the walk reads a commit a second
    /// time when the time bound rejected the commit as a parent and the walk
    /// then reaches the commit as a root, for example as a dependency. The
    /// walk reads each other commit once.
    ///
    /// The commit of each field 2 entry of a commit that the walk reaches is
    /// a root too: the dependencies. With `refs_only` or `keep_younger_than`,
    /// a dependency is a root under the bound of the run, also a dependency
    /// of a commit of the summary. Otherwise a dependency keeps no parent, as
    /// each other root, so the walk still reads each commit once. The walk
    /// reads field 2 from the bytes of the commit object, and sends no other
    /// request for it.
    ///
    /// - A defective field 2 entry, with an empty name or a checksum that is
    ///   not 32 bytes, roots nothing and gives one warning in
    ///   [`PruneStats::warnings`]. The other entries of the field root as
    ///   usual.
    /// - The walk roots on entries that are not sorted, on two entries with
    ///   one name, and on the field 2 entries of a dependency. These are not
    ///   defective.
    /// - An entry that names a commit that the listing does not hold roots
    ///   nothing and gives no warning. The walk keeps the name of that commit,
    ///   so the run keeps its detached metadata and its composefs image.
    ///
    /// The walk reads each commit and dirtree that it reaches with one `GET`,
    /// and checks its SHA-256 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 [`new`](BucketRepo::new) gives it. If more than
    /// one read fails, the error is the error of the first read that fails
    /// in time. The walk sends no request for an object that the listing
    /// does not hold, and reads such an object as absent. An absent commit
    /// or dirtree keeps its own name and nothing below it.
    ///
    /// # Steps
    ///
    /// 1. The refusal of the options, before any request.
    /// 2. The read of the `config`, by the strict rule of an absent key, and
    ///    then the take of the writer lock for [`LockOperation::Prune`] with
    ///    [`break_lock`](BucketPruneOptions::break_lock). A run with
    ///    `no_prune` takes the lock too.
    /// 3. At the same time: the refs, the listing of the objects, the list of
    ///    `deltas/`, the partial-commit markers, the list of the incomplete
    ///    multipart uploads, and the read of the `summary`.
    /// 4. The walk. With `no_prune`, the run then releases the lock and
    ///    returns the report.
    /// 5. The commits: each commit object, and then its detached metadata
    ///    and its composefs image, one slot for each commit. Then each
    ///    detached metadata and each composefs image whose commit object the
    ///    listing does not hold.
    /// 6. Each other object.
    /// 7. The superblock of each static delta whose target commit is not in
    ///    the bucket after the run, and then each other key of these
    ///    deltas. A delta directory with no superblock goes too. A key under
    ///    `deltas/` that fails [`is_repository_key`], and a directory whose
    ///    name does not decode as a delta, stay. Then the partial-commit
    ///    marker of each commit that is not in the bucket after the run.
    /// 8. The abort of each incomplete multipart upload.
    /// 9. The release of the lock.
    ///
    /// The run checks the lock before each of the steps 5 to 8 that has a
    /// change to make, and again after each 1000 deletes or aborts of a step.
    /// At most `max_outstanding` deletes or aborts run at a time, as
    /// [`new`](BucketRepo::new) states. Each delete is one `DELETE` of one
    /// key.
    ///
    /// A run that fails after its first delete leaves a bucket repository
    /// that a reader can use: each commit object in the bucket has the
    /// objects of its tree, because the commits go first. A later run
    /// deletes the rest.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidInput`] if an option other than `refs_only`,
    ///   `depth`, `keep_younger_than`, and `no_prune` is not at its default
    ///   value. The message names the first such field, in the order of the
    ///   fields. The run then sends no request.
    /// - [`Error::InvalidInput`] if `no_prune` and
    ///   [`break_lock`](BucketPruneOptions::break_lock) are both set. The run
    ///   then sends no request.
    /// - [`Error::Remote`] with
    ///   [`InvalidInput`](crate::remote::Error::InvalidInput) if the bucket
    ///   repository has no `config`, or if its `config` sets
    ///   `[core] tombstone-commits` to `true`. The run then takes no lock.
    /// - [`Error::InvalidFormat`] if the `config` breaks a rule of a bucket
    ///   repository, if a key under `refs/` is no ref key, if a ref file is
    ///   malformed or is gone at its read, if the summary does not parse or
    ///   is larger than its cap, or if a commit or a dirtree that the walk
    ///   reads is larger than its cap or than its size in the listing, is
    ///   gone at its read, or does not have the checksum of its name. The run
    ///   then deletes nothing. Run `ostrya remote fsck --delete` to repair
    ///   an object whose checksum is wrong.
    /// - [`Error::Core`] if a commit or a dirtree that the walk reads does
    ///   not parse. The run then deletes nothing.
    /// - The errors of [`lock`](BucketRepo::lock). A lock of another writer
    ///   fails the run before it reads an object.
    /// - [`Error::BucketLockLost`] if a check of the lock or the release
    ///   fails. The run then sends no more deletes and releases no lock.
    /// - [`Error::Remote`] for an error of the bucket client. The run then
    ///   releases its lock and drops an error of that release. A failure
    ///   after the first delete leaves the deletes that completed.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use ostrya::PruneOptions;
    /// use ostrya::bucket::{BucketPruneOptions, BucketRepo};
    /// 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 mut opts = PruneOptions::default();
    /// opts.refs_only = true;
    /// opts.depth = 0;
    /// let report = repo.prune(&opts, &BucketPruneOptions::default()).await?;
    /// for warning in &report.stats.warnings {
    ///     eprintln!("warning: {warning}");
    /// }
    /// println!(
    ///     "deleted {} of {} objects",
    ///     report.stats.pruned_objects, report.stats.total_objects
    /// );
    /// # Ok(()) }
    /// ```
    pub async fn prune(
        &self,
        opts: &PruneOptions,
        bucket_opts: &BucketPruneOptions,
    ) -> Result<BucketPruneReport> {
        refuse_options(opts)?;
        if opts.no_prune && bucket_opts.break_lock {
            return Err(Error::InvalidInput(
                "the prune of a bucket repository takes no break_lock with no_prune; a run with \
                 no_prune never removes the writer lock of another writer"
                    .to_owned(),
            ));
        }
        let Some(config) = self.read_config_strict().await? else {
            return Err(invalid(format!(
                "the bucket repository {} has no config; init it first",
                self.address()
            )));
        };
        if config.tombstone_commits()? {
            return Err(invalid(format!(
                "the config of the bucket repository {} sets [core] tombstone-commits to true, \
                 and the prune of a bucket repository writes no tombstone",
                self.address()
            )));
        }
        let own = config.collection_id();
        let lock = self
            .lock(LockOperation::Prune, bucket_opts.break_lock)
            .await?;
        let broken_lock = lock.broken().cloned();
        let (plan, uploads) = match self.prune_plan(opts, own).await {
            Ok(planned) => planned,
            Err(e) => return Err(release_on_error(lock, e).await),
        };
        if !opts.no_prune
            && let Err(e) = self.prune_apply(&plan, &uploads, &lock).await
        {
            return Err(release_on_error(lock, e).await);
        }
        lock.release().await?;
        Ok(BucketPruneReport {
            stats: plan.stats,
            deleted_deltas: plan.deltas,
            deleted_markers: plan.markers,
            aborted_uploads: uploads,
            broken_lock,
        })
    }

    /// Reads the state of the repository, walks it, and returns the plan of
    /// the run and the incomplete multipart uploads, sorted.
    async fn prune_plan(
        &self,
        opts: &PruneOptions,
        own: Option<&str>,
    ) -> Result<(Plan, Vec<Upload>)> {
        let (refs, (listing, (deltas, (partial, (uploads, summary))))) = zip(
            self.read_refs(own),
            zip(
                self.list_objects(),
                zip(
                    self.bucket.list("deltas/"),
                    zip(
                        self.partial_commits(),
                        zip(self.bucket.list_uploads(), self.summary_commits()),
                    ),
                ),
            ),
        )
        .await;
        let (refs, listing, deltas) = (refs?, listing?, deltas?);
        let (partial, mut uploads, summary) = (partial?, uploads?, summary?);
        uploads.sort_by(|a, b| {
            a.key
                .cmp(&b.key)
                .then_with(|| a.upload_id.cmp(&b.upload_id))
        });
        let (keep, warnings) = self.prune_walk(opts, &refs, &summary, &listing).await?;
        let deltas: Vec<&str> = deltas
            .objects
            .iter()
            .map(|listed| listed.key.as_str())
            .collect();
        let mut plan = plan(&listing, &keep, &deltas, &partial);
        plan.stats.warnings = warnings;
        Ok((plan, uploads))
    }

    /// Returns the name of each object that the roots of the run reach, and
    /// a warning for each defective field 2 entry that the walk read.
    async fn prune_walk(
        &self,
        opts: &PruneOptions,
        refs: &BucketRefs,
        summary: &BTreeSet<Checksum>,
        listing: &ObjectListing,
    ) -> Result<(HashSet<ObjectName>, Vec<RelatedWarning>)> {
        let targets: HashSet<Checksum> = refs
            .heads
            .values()
            .chain(refs.mirrors.values().flat_map(BTreeMap::values))
            .copied()
            .collect();
        let global = opts.global_bound();
        let mut roots: Vec<(Checksum, ParentBound)> = Vec::new();
        // A dependency takes the bound of the run when the roots are on the
        // refs alone, as in a local prune. Otherwise each commit of the
        // listing is a root that keeps no parent, so a dependency keeps no
        // parent either: a parent bound adds no commit of the listing.
        let related_bound = if opts.roots_on_refs_alone() {
            global
        } else {
            ParentBound::Depth(0)
        };
        if opts.roots_on_refs_alone() {
            // The root of a ref target already keeps the commit and its
            // tree, so a second root for it at no parent adds nothing.
            // The walk takes the roots from the end, so it expands the
            // commits of the summary last. An earlier expansion of such a
            // commit, as a dependency or as a parent, covers its root.
            roots.extend(
                summary
                    .iter()
                    .filter(|commit| !targets.contains(commit))
                    .map(|commit| (*commit, ParentBound::Depth(0))),
            );
            roots.extend(targets.iter().map(|commit| (*commit, global)));
        } else {
            // Each commit of the listing is a root, so a root that keeps no
            // parent keeps the same objects of the listing, and the walk
            // reads each commit once. The name of a parent commit that the
            // listing does not hold is then not kept, so the plan deletes
            // the detached metadata and the composefs image of that commit.
            roots.extend(
                targets
                    .iter()
                    .chain(summary)
                    .copied()
                    .chain(
                        listing
                            .objects
                            .keys()
                            .filter(|name| name.ty == ObjectType::Commit)
                            .map(|name| name.checksum),
                    )
                    .map(|commit| (commit, ParentBound::Depth(0))),
            );
        }
        // The walk reads field 2 from the bytes of each commit that it reads,
        // and reads no detached metadata, so it follows no build source.
        let gc = GcRoots {
            dependencies: true,
            build_sources: false,
            traverse_parent: true,
            traverse_tree: true,
            weak_roots: Default::default(),
        };
        let source = ListedObjects {
            repo: self,
            listing,
        };
        walk_reachable_gc(&source, roots, related_bound, &gc, &targets, None).await
    }

    /// Returns each commit that the `summary` of the repository names, in
    /// field 0 and in `ostree.summary.collection-map`, or an empty set if
    /// the key holds no object.
    ///
    /// The read takes the strict rule of an absent key, up to the read cap
    /// of a summary. The parse runs on the blocking pool.
    async fn summary_commits(&self) -> Result<BTreeSet<Checksum>> {
        let bytes = match self.bucket.get_strict("summary", SUMMARY_READ_CAP).await {
            Ok(Some(object)) => object.bytes,
            Ok(None) => return Ok(BTreeSet::new()),
            Err(crate::remote::Error::LimitExceeded(_)) => {
                return Err(self.malformed(format!(
                    "its summary is larger than {SUMMARY_READ_CAP} bytes"
                )));
            }
            Err(e) => return Err(e.into()),
        };
        let parsed = ostrya_rt::unblock(move || -> Result<BTreeSet<Checksum>> {
            let summary = Summary::parse(&bytes)?;
            let mut commits: BTreeSet<Checksum> =
                summary.refs.iter().map(|entry| entry.commit).collect();
            for (_, entries) in summary.collection_map()? {
                commits.extend(entries.iter().map(|entry| entry.commit));
            }
            Ok(commits)
        })
        .await;
        parsed.map_err(|e| self.malformed(format!("its summary does not parse: {e}")))
    }

    /// Sends the deletes and the aborts of `plan` and `uploads` under
    /// `lock`, in the order of the run.
    async fn prune_apply(
        &self,
        plan: &Plan,
        uploads: &[Upload],
        lock: &WriterLock<'_>,
    ) -> Result<()> {
        let bucket = &self.bucket;
        let one = |key: String| async move {
            bucket.delete(&key).await?;
            Ok(())
        };
        // The commits, with the sidecars of the absent commits.
        let mut sent = None;
        self.run_each(lock, &mut sent, &plan.commits, Vec::len, |keys| {
            delete_in_order(bucket, keys)
        })
        .await?;
        self.run_each(
            lock,
            &mut sent,
            &plan.orphans,
            |_| 1,
            |name| one(object_key(name)),
        )
        .await?;
        // The other objects.
        let mut sent = None;
        self.run_each(
            lock,
            &mut sent,
            &plan.content,
            |_| 1,
            |name| one(object_key(name)),
        )
        .await?;
        // The static deltas, each superblock first, and the markers.
        let mut sent = None;
        self.run_each(lock, &mut sent, &plan.superblocks, Vec::len, |keys| {
            delete_in_order(bucket, keys)
        })
        .await?;
        self.run_each(lock, &mut sent, &plan.parts, Vec::len, |keys| {
            delete_in_order(bucket, keys)
        })
        .await?;
        self.run_each(
            lock,
            &mut sent,
            &plan.markers,
            |_| 1,
            |commit| one(marker_key(commit)),
        )
        .await?;
        // The incomplete multipart uploads.
        let mut sent = None;
        self.run_each(
            lock,
            &mut sent,
            uploads,
            |_| 1,
            |upload| async move {
                bucket.abort_upload(&upload.key, &upload.upload_id).await?;
                Ok(())
            },
        )
        .await
    }

    /// Runs `op` on each item of `items`, at most `max_outstanding` at a
    /// time, and waits for each one before it returns.
    ///
    /// `sent` is the weight of the items of the step that ran since its last
    /// check of the lock, or `None` before its first check. The call checks
    /// the lock before the groups that [`groups`] gives, and updates `sent`.
    async fn run_each<'i, T, Fut>(
        &self,
        lock: &WriterLock<'_>,
        sent: &mut Option<usize>,
        items: &'i [T],
        weight: impl Fn(&T) -> usize,
        op: impl Fn(&'i T) -> Fut,
    ) -> Result<()>
    where
        Fut: Future<Output = Result<()>> + Send + 'i,
    {
        for (check, group) in groups(items, weight, sent) {
            if check {
                lock.check().await?;
            }
            each_slot(self.max_outstanding, group.iter().map(&op), |()| {}).await?;
        }
        Ok(())
    }
}

/// Refuses each option of `opts` that the prune of a bucket repository does
/// not take, in the order of the fields.
fn refuse_options(opts: &PruneOptions) -> Result<()> {
    let refused = [
        ("delete_commit", opts.delete_commit.is_some()),
        ("only_branch", !opts.only_branch.is_empty()),
        ("retain_branch_depth", !opts.retain_branch_depth.is_empty()),
        ("commit_only", opts.commit_only),
        ("static_deltas_only", opts.static_deltas_only),
        ("traverse_parent", !opts.traverse_parent),
        ("weak_ref_filter", opts.weak_ref_filter.is_set()),
    ];
    match refused.iter().find(|(_, set)| *set) {
        Some((field, _)) => Err(Error::InvalidInput(format!(
            "the prune of a bucket repository takes no option {field}; the option must keep its \
             default value"
        ))),
        None => Ok(()),
    }
}

/// Returns the plan of a run: the objects of `listing` that `keep` does not
/// hold, the static deltas of the keys `deltas` under `deltas/` and the
/// markers of `partial` whose commits are not in the bucket after the run,
/// and the counts.
fn plan(
    listing: &ObjectListing,
    keep: &HashSet<ObjectName>,
    deltas: &[&str],
    partial: &BTreeSet<Checksum>,
) -> Plan {
    let kept = |name: &ObjectName| {
        keep.contains(name)
            || (name.ty.is_commit_sidecar()
                && keep.contains(&ObjectName::new(name.checksum, ObjectType::Commit)))
    };
    // The objects of the listing that the run deletes.
    let doomed = |name: &ObjectName| {
        listing.objects.contains_key(name) && name.ty != ObjectType::TombstoneCommit && !kept(name)
    };
    let mut out = Plan {
        commits: Vec::new(),
        orphans: Vec::new(),
        content: Vec::new(),
        superblocks: Vec::new(),
        parts: Vec::new(),
        markers: Vec::new(),
        deltas: Vec::new(),
        stats: PruneStats {
            total_objects: listing
                .objects
                .keys()
                .filter(|name| counted(name.ty, false))
                .count(),
            pruned_objects: 0,
            freed_bytes: 0,
            deleted_refs: Vec::new(),
            deleted_collection_refs: Vec::new(),
            warnings: Vec::new(),
        },
    };
    let mut candidates: Vec<ObjectName> = Vec::new();
    for (name, size) in &listing.objects {
        if name.ty == ObjectType::TombstoneCommit || kept(name) {
            continue;
        }
        if counted(name.ty, false) {
            out.stats.pruned_objects += 1;
            out.stats.freed_bytes += size;
        }
        candidates.push(*name);
    }
    // The order of the checksum bytes, then of the extensions, is the order
    // of the keys, because the keys hold the checksums in lowercase hex.
    candidates.sort_unstable_by(|a, b| {
        a.checksum
            .as_bytes()
            .cmp(b.checksum.as_bytes())
            .then_with(|| {
                a.ty.extension(RepoMode::Archive)
                    .cmp(b.ty.extension(RepoMode::Archive))
            })
    });
    for name in &candidates {
        match name.ty {
            ObjectType::Commit => {
                let mut keys = vec![object_key(name)];
                for ty in [ObjectType::CommitMeta, ObjectType::ComposefsImage] {
                    let sidecar = ObjectName::new(name.checksum, ty);
                    if doomed(&sidecar) {
                        keys.push(object_key(&sidecar));
                    }
                }
                out.commits.push(keys);
            }
            ty if ty.is_commit_sidecar() => {
                let commit = ObjectName::new(name.checksum, ObjectType::Commit);
                if !listing.objects.contains_key(&commit) {
                    out.orphans.push(*name);
                }
            }
            _ => {}
        }
    }
    candidates.retain(|name| name.ty != ObjectType::Commit && !name.ty.is_commit_sidecar());
    out.content = candidates;
    // The commits that the bucket holds after the run.
    let present = |commit: &Checksum| {
        let name = ObjectName::new(*commit, ObjectType::Commit);
        listing.objects.contains_key(&name) && !doomed(&name)
    };
    out.markers = partial
        .iter()
        .filter(|commit| !present(commit))
        .copied()
        .collect();
    // The keys of each delta directory whose name decodes, by directory.
    let mut dirs: BTreeMap<(&str, &str), (String, Vec<&str>)> = BTreeMap::new();
    for &key in deltas {
        let mut parts = key.splitn(4, '/');
        let (Some("deltas"), Some(fanout), Some(leaf), Some(_)) =
            (parts.next(), parts.next(), parts.next(), parts.next())
        else {
            continue;
        };
        let Ok((from, to)) = parse_delta_dir(fanout, leaf) else {
            continue;
        };
        if present(&to) || !is_repository_key(key) {
            continue;
        }
        dirs.entry((fanout, leaf))
            .or_insert_with(|| (delta_name(from.as_ref(), &to), Vec::new()))
            .1
            .push(key);
    }
    for ((fanout, leaf), (name, keys)) in dirs {
        let superblock = format!("deltas/{fanout}/{leaf}/superblock");
        for key in keys {
            if key == superblock {
                out.superblocks.push(vec![superblock.clone()]);
            } else {
                out.parts.push(vec![key.to_owned()]);
            }
        }
        out.deltas.push(name);
    }
    out.deltas.sort();
    out.superblocks.sort();
    out.parts.sort();
    out
}

/// Deletes `keys` in order.
async fn delete_in_order(bucket: &Bucket, keys: &[String]) -> Result<()> {
    for key in keys {
        bucket.delete(key).await?;
    }
    Ok(())
}

/// Splits `items` into groups in order, each with `true` if the lock is
/// checked before it.
///
/// `sent` is the weight of the items of the step that ran since its last
/// check of the lock, or `None` before its first check. The first item of a
/// step comes after a check. Each other item comes after a check if the
/// weight since the last check goes above [`CHECK_INTERVAL`] with it. So
/// after a check, a group holds items of at most `CHECK_INTERVAL` in all by
/// `weight`, or one item that weighs more. The call sets `sent` to the
/// weight since the last check after the last item.
fn groups<'i, T>(
    items: &'i [T],
    weight: impl Fn(&T) -> usize,
    sent: &mut Option<usize>,
) -> Vec<(bool, &'i [T])> {
    let mut out = Vec::new();
    let mut start = 0;
    let mut check = false;
    for (at, item) in items.iter().enumerate() {
        let w = weight(item);
        let total = match *sent {
            Some(total) if total == 0 || total + w <= CHECK_INTERVAL => total,
            _ => {
                if at > start {
                    out.push((check, &items[start..at]));
                }
                start = at;
                check = true;
                0
            }
        };
        *sent = Some(total + w);
    }
    if start < items.len() {
        out.push((check, &items[start..]));
    }
    out
}

/// A compile-time check that the future of a prune is `Send`.
const _: fn() = || {
    fn assert_send<T: Send>(_: &T) {}
    let _ = |repo: &BucketRepo, opts: &PruneOptions, bucket: &BucketPruneOptions| {
        assert_send(&repo.prune(opts, bucket))
    };
};

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

    fn csum(byte: u8) -> Checksum {
        Checksum::from_bytes([byte; 32])
    }

    fn name(byte: u8, ty: ObjectType) -> ObjectName {
        ObjectName::new(csum(byte), ty)
    }

    fn listing(names: &[(ObjectName, u64)]) -> ObjectListing {
        let mut listing = ObjectListing::default();
        listing.objects.extend(names.iter().copied());
        listing
    }

    #[test]
    fn a_doomed_commit_takes_its_sidecars_and_the_counts_leave_them_out() {
        let commit = name(1, ObjectType::Commit);
        let meta = name(1, ObjectType::CommitMeta);
        let image = name(1, ObjectType::ComposefsImage);
        let tombstone = name(1, ObjectType::TombstoneCommit);
        let orphan = name(2, ObjectType::CommitMeta);
        let tree = name(3, ObjectType::DirTree);
        let file = name(4, ObjectType::File);
        let kept = name(5, ObjectType::Commit);
        let kept_meta = name(5, ObjectType::CommitMeta);
        let listing = listing(&[
            (commit, 10),
            (meta, 20),
            (image, 30),
            (tombstone, 40),
            (orphan, 50),
            (tree, 60),
            (file, 70),
            (kept, 80),
            (kept_meta, 90),
        ]);
        let keep: HashSet<ObjectName> = [kept].into_iter().collect();
        let plan = plan(&listing, &keep, &[], &BTreeSet::new());
        assert_eq!(
            plan.commits,
            [vec![
                object_key(&commit),
                object_key(&meta),
                object_key(&image)
            ]]
        );
        assert_eq!(plan.orphans, [orphan]);
        assert_eq!(plan.content, [tree, file]);
        assert_eq!(plan.stats.total_objects, 4);
        assert_eq!(plan.stats.pruned_objects, 3);
        assert_eq!(plan.stats.freed_bytes, 10 + 60 + 70);
        assert!(plan.stats.deleted_refs.is_empty());
    }

    #[test]
    fn the_objects_to_delete_are_in_the_order_of_their_keys() {
        let types = [
            ObjectType::File,
            ObjectType::DirTree,
            ObjectType::DirMeta,
            ObjectType::PayloadLink,
            ObjectType::FileXattrs,
            ObjectType::FileXattrsLink,
        ];
        let mut names = Vec::new();
        for byte in [0x00, 0x0f, 0x10, 0x9a, 0xa0, 0xff] {
            for ty in types {
                names.push((name(byte, ty), 1));
            }
        }
        let plan = plan(&listing(&names), &HashSet::new(), &[], &BTreeSet::new());
        let keys: Vec<String> = plan.content.iter().map(object_key).collect();
        let mut sorted = keys.clone();
        sorted.sort();
        assert_eq!(keys, sorted);
        assert_eq!(keys.len(), names.len());
    }

    #[test]
    fn the_deltas_and_the_markers_of_absent_commits_go() {
        let kept = csum(1);
        let doomed = csum(2);
        let absent = csum(3);
        let listing = listing(&[
            (ObjectName::new(kept, ObjectType::Commit), 1),
            (ObjectName::new(doomed, ObjectType::Commit), 1),
        ]);
        let keep: HashSet<ObjectName> = [ObjectName::new(kept, ObjectType::Commit)]
            .into_iter()
            .collect();
        let dir = |from: Option<&Checksum>, to: &Checksum| {
            let to = to.to_base64_modified();
            match from {
                Some(from) => {
                    let from = from.to_base64_modified();
                    format!("deltas/{}/{}-{to}", &from[..2], &from[2..])
                }
                None => format!("deltas/{}/{}", &to[..2], &to[2..]),
            }
        };
        let doomed_dir = dir(None, &doomed);
        let no_superblock = dir(Some(&kept), &absent);
        let kept_dir = dir(Some(&doomed), &kept);
        let deltas = [
            format!("{doomed_dir}/0"),
            format!("{doomed_dir}/superblock"),
            format!("{doomed_dir}/x/y"),
            format!("{no_superblock}/0"),
            format!("{kept_dir}/superblock"),
            "deltas/!!/bad/superblock".to_owned(),
            "deltas/short".to_owned(),
        ];
        let deltas: Vec<&str> = deltas.iter().map(String::as_str).collect();
        let partial: BTreeSet<Checksum> = [kept, doomed, absent].into_iter().collect();
        let plan = plan(&listing, &keep, &deltas, &partial);
        assert_eq!(plan.superblocks, [vec![format!("{doomed_dir}/superblock")]]);
        let mut parts = vec![
            vec![format!("{doomed_dir}/0")],
            vec![format!("{no_superblock}/0")],
        ];
        parts.sort();
        assert_eq!(plan.parts, parts);
        let mut names = vec![
            doomed.to_hex(),
            format!("{}-{}", kept.to_hex(), absent.to_hex()),
        ];
        names.sort();
        assert_eq!(plan.deltas, names);
        let mut markers = vec![doomed, absent];
        markers.sort();
        assert_eq!(plan.markers, markers);
    }

    #[test]
    fn a_group_holds_at_most_the_interval() {
        let items: Vec<usize> = vec![600, 400, 1, 2000, 3, 999, 1];
        let mut sent = None;
        let split = groups(&items, |w| *w, &mut sent);
        assert_eq!(
            split,
            [
                (true, &[600, 400][..]),
                (true, &[1][..]),
                (true, &[2000][..]),
                (true, &[3][..]),
                (true, &[999, 1][..])
            ]
        );
        assert_eq!(sent, Some(1000));
        let mut sent = None;
        assert!(groups(&[] as &[usize], |w| *w, &mut sent).is_empty());
        assert_eq!(sent, None);
        let ones = vec![1usize; 2500];
        let sizes: Vec<(bool, usize)> = groups(&ones, |_| 1, &mut sent)
            .iter()
            .map(|(check, group)| (*check, group.len()))
            .collect();
        assert_eq!(sizes, [(true, 1000), (true, 1000), (true, 500)]);
        assert_eq!(sent, Some(500));
    }

    #[test]
    fn the_next_list_of_a_step_carries_the_count_since_the_check() {
        let mut sent = Some(998);
        let split = groups(&[1usize, 1, 1, 5][..], |w| *w, &mut sent);
        assert_eq!(split, [(false, &[1, 1][..]), (true, &[1, 5][..])]);
        assert_eq!(sent, Some(6));
        // A list that fits after the last check gets no check.
        let split = groups(&[3usize][..], |w| *w, &mut sent);
        assert_eq!(split, [(false, &[3][..])]);
        assert_eq!(sent, Some(9));
        // An item that weighs more than the interval comes after a check.
        let split = groups(&[2000usize][..], |w| *w, &mut sent);
        assert_eq!(split, [(true, &[2000][..])]);
        assert_eq!(sent, Some(2000));
    }

    #[test]
    fn each_refused_option_is_named_in_field_order() {
        let mut opts = PruneOptions {
            refs_only: true,
            depth: 0,
            no_prune: true,
            keep_younger_than: Some(1),
            ..PruneOptions::default()
        };
        refuse_options(&opts).unwrap();
        opts.traverse_parent = false;
        opts.commit_only = true;
        match refuse_options(&opts) {
            Err(Error::InvalidInput(message)) => {
                assert!(message.contains("option commit_only;"), "{message}");
            }
            other => panic!("{other:?}"),
        }
        opts.commit_only = false;
        match refuse_options(&opts) {
            Err(Error::InvalidInput(message)) => {
                assert!(message.contains("option traverse_parent;"), "{message}");
            }
            other => panic!("{other:?}"),
        }
    }
}