type-bridge-workspace 2.2.2

Validated programmatic workspace configuration for type-bridge
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
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
//! Retained filesystem authority for one physical workspace root.

use std::collections::{BTreeMap, BTreeSet};
use std::ffi::{OsStr, OsString};
use std::io::{self, Read as _, Seek as _, Write as _};
use std::path::{Component, Path, PathBuf};
use std::time::UNIX_EPOCH;

use cap_fs_ext::DirExt as _;
use cap_std::ambient_authority;
use cap_std::fs::{Dir, Metadata};
use sha2::{Digest as _, Sha256};
use type_bridge_schema::{
    SchemaSourceCapture, SchemaSourceIdentity, SchemaSourceKind, SchemaSourceObservation,
    SchemaSourceRevision, SchemaSourceService, SchemaSourceServiceError,
};
use type_bridge_schema_migration::validate_portable_direct_child;

use crate::{
    WorkspaceConfigError, WorkspaceConfigErrorCode, WorkspaceRoot, WorkspaceServiceError,
    portable_path_collision_key, workspace_paths_overlap,
};

/// A retained, no-follow capability for one canonical workspace root.
///
/// Schema discovery and all later output or migration operations derive from
/// this descriptor. Renaming or replacing the ambient pathname after it opens
/// cannot split reads and writes across different physical roots.
pub struct WorkspaceDirectoryAuthority {
    directory: Dir,
    root: WorkspaceRoot,
}

/// A retained, confined directory used for generated workspace artifacts.
pub struct WorkspaceOutputDirectory {
    directory: Dir,
    display_path: PathBuf,
}

impl std::fmt::Debug for WorkspaceOutputDirectory {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("WorkspaceOutputDirectory")
            .field("display_path", &self.display_path)
            .finish_non_exhaustive()
    }
}

impl std::fmt::Debug for WorkspaceDirectoryAuthority {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("WorkspaceDirectoryAuthority")
            .field("root", &self.root)
            .finish_non_exhaustive()
    }
}

impl WorkspaceDirectoryAuthority {
    /// Open a canonical root component-by-component without following links.
    pub fn open(root: WorkspaceRoot) -> Result<Self, WorkspaceConfigError> {
        let directory = retain_canonical_root(root.as_path()).map_err(|_| {
            WorkspaceConfigError::new(
                WorkspaceConfigErrorCode::WorkspaceRootCanonicalizationFailed,
                "workspace root cannot be retained as directory authority",
            )
            .with_detail("workspace_root_authority_unavailable")
        })?;
        Ok(Self { directory, root })
    }

    /// Return the canonical display spelling bound to this authority.
    #[must_use]
    pub const fn root(&self) -> &WorkspaceRoot {
        &self.root
    }

    /// Return the retained root descriptor for handle-relative consumers.
    #[must_use]
    pub(crate) const fn directory(&self) -> &Dir {
        &self.directory
    }

    /// Clone the retained root into a generated-output authority.
    pub fn output_root(&self) -> Result<WorkspaceOutputDirectory, String> {
        WorkspaceOutputDirectory::from_root(&self.directory, self.root.as_path())
    }

    /// Capture one confined file with the schema loader's bounded semantics.
    pub fn capture_relative_file(
        &self,
        relative: &Path,
        maximum_bytes: usize,
    ) -> Result<SchemaSourceCapture, WorkspaceServiceError> {
        let absolute = self.root.as_path().join(relative);
        let capture = self
            .capture_file(&absolute, maximum_bytes)
            .map_err(|_| WorkspaceServiceError::new("workspace_file_read_failed"))?;
        if capture.before().kind() != SchemaSourceKind::File || capture.before() != capture.after()
        {
            return Err(WorkspaceServiceError::new(
                "workspace_file_changed_during_capture",
            ));
        }
        Ok(capture)
    }

    fn relative_path<'a>(&self, path: &'a Path) -> Result<&'a Path, SchemaSourceServiceError> {
        let relative = path
            .strip_prefix(self.root.as_path())
            .map_err(|_| SchemaSourceServiceError)?;
        if relative
            .components()
            .any(|component| !matches!(component, Component::Normal(_)))
            && !relative.as_os_str().is_empty()
        {
            return Err(SchemaSourceServiceError);
        }
        Ok(if relative.as_os_str().is_empty() {
            Path::new(".")
        } else {
            relative
        })
    }
}

impl WorkspaceOutputDirectory {
    pub(crate) fn from_root(directory: &Dir, display_path: &Path) -> Result<Self, String> {
        Ok(Self {
            directory: directory
                .try_clone()
                .map_err(|error| format!("cannot retain output root: {error}"))?,
            display_path: display_path.to_path_buf(),
        })
    }

    /// Open or create a real directory path beneath this retained authority.
    pub fn open_beneath(&self, relative: &Path) -> Result<Self, String> {
        let mut directory = self
            .directory
            .try_clone()
            .map_err(|error| format!("cannot retain output directory: {error}"))?;
        let mut display_path = self.display_path.clone();
        for component in relative.components() {
            let Component::Normal(name) = component else {
                return Err(format!(
                    "output directory {} is not confined beneath {}",
                    relative.display(),
                    self.display_path.display()
                ));
            };
            display_path.push(name);
            match directory.symlink_metadata(name) {
                Ok(metadata) => {
                    if metadata.file_type().is_symlink() || !metadata.is_dir() {
                        return Err(format!(
                            "output component {} must be a real directory, not a link",
                            display_path.display()
                        ));
                    }
                }
                Err(error) if error.kind() == io::ErrorKind::NotFound => {
                    match directory.create_dir(name) {
                        Ok(()) => {}
                        Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {}
                        Err(error) => {
                            return Err(format!(
                                "cannot create {}: {error}",
                                display_path.display()
                            ));
                        }
                    }
                }
                Err(error) => {
                    return Err(format!(
                        "cannot inspect {}: {error}",
                        display_path.display()
                    ));
                }
            }
            directory = directory.open_dir_nofollow(name).map_err(|error| {
                format!(
                    "output component {} must be a real directory, not a link: {error}",
                    display_path.display()
                )
            })?;
        }
        Ok(Self {
            directory,
            display_path,
        })
    }

    /// Return the configured path for diagnostics and operator output only.
    #[must_use]
    pub fn display_path(&self) -> &Path {
        self.display_path.as_path()
    }

    /// Transactionally replace one direct regular output through this authority.
    pub fn write_atomic(&self, file_name: &std::ffi::OsStr, bytes: &[u8]) -> Result<(), String> {
        let file_name = validate_portable_direct_child(file_name).map_err(|error| {
            format!(
                "generated output name is not a portable direct child beneath {}: {error}",
                self.display_path.display()
            )
        })?;
        self.write_atomic_batch([(Path::new(file_name), bytes)])
    }

    /// Atomically replace a generation of confined regular output files.
    ///
    /// Paths are relative to this retained directory and every component must
    /// be a portable direct-child name. The input order is the publication
    /// order. Every destination is validated and every same-directory
    /// temporary is written and flushed before the first destination changes.
    /// If ordinary publication or directory-flush work fails, all previously
    /// accepted files are restored byte-for-byte and newly introduced files
    /// are removed. This is generation-atomic within one running process; it
    /// does not claim crash-atomic publication across filesystems.
    pub fn write_atomic_batch<I, P, B>(&self, outputs: I) -> Result<(), String>
    where
        I: IntoIterator<Item = (P, B)>,
        P: AsRef<Path>,
        B: AsRef<[u8]>,
    {
        let outputs = outputs
            .into_iter()
            .map(|(path, bytes)| (path.as_ref().to_path_buf(), bytes))
            .collect();
        self.write_atomic_batch_with(outputs, |_| Ok(()))
    }

    fn write_atomic_batch_with<B, F>(
        &self,
        outputs: Vec<(PathBuf, B)>,
        mut after_destination_displaced: F,
    ) -> Result<(), String>
    where
        B: AsRef<[u8]>,
        F: FnMut(usize) -> Result<(), String>,
    {
        let plans = validate_batch_plans(self, outputs)?;
        if plans.is_empty() {
            return Ok(());
        }

        let mut entries = Vec::with_capacity(plans.len());
        let mut payloads = Vec::with_capacity(plans.len());
        for plan in plans {
            let parent_path = plan.relative_path.parent().unwrap_or_else(|| Path::new(""));
            let parent = self.open_beneath(parent_path)?;
            let file_name = plan
                .relative_path
                .file_name()
                .expect("validated output path has a final component")
                .to_os_string();
            let had_original = open_regular_output(&parent, &file_name)?.is_some();
            let replacement = bytes_identity(plan.bytes.as_ref())?;
            entries.push(PreparedBatchEntry {
                parent,
                file_name,
                had_original,
                original: None,
                replacement,
                temporary_name: None,
                temporary_live: false,
                backup_name: None,
                backup_live: false,
                backup_file: None,
                destination_state: DestinationState::Untouched,
            });
            payloads.push(plan.bytes);
        }

        if let Err(error) = prepare_batch_sidecars(&mut entries, &payloads)
            .and_then(|()| sync_batch_parents(&entries))
        {
            return Err(clean_up_unpublished_batch(&mut entries, error));
        }

        let publication = (|| -> Result<(), String> {
            for (index, entry) in entries.iter_mut().enumerate() {
                require_unchanged_destination(entry)?;
                if entry.had_original {
                    entry
                        .parent
                        .directory
                        .remove_file(&entry.file_name)
                        .map_err(|error| {
                            format!(
                                "cannot displace existing output {} before publication: {error}",
                                entry.parent.display_path.join(&entry.file_name).display()
                            )
                        })?;
                    entry.destination_state = DestinationState::VacantForPublication;
                }
                after_destination_displaced(index)?;
                let temporary = entry
                    .temporary_name
                    .as_ref()
                    .expect("prepared output has a temporary name")
                    .clone();
                entry
                    .parent
                    .directory
                    .hard_link(&temporary, &entry.parent.directory, &entry.file_name)
                    .map_err(|error| {
                        format!(
                            "cannot publish {} without replacing a concurrent destination: {error}",
                            entry.parent.display_path.join(&entry.file_name).display()
                        )
                    })?;
                entry.destination_state = DestinationState::ReplacementPublished;
                entry
                    .parent
                    .directory
                    .remove_file(&temporary)
                    .map_err(|error| {
                        format!(
                            "cannot retire published generation temporary {}: {error}",
                            entry.parent.display_path.join(temporary).display()
                        )
                    })?;
                entry.temporary_live = false;
            }
            sync_batch_parents(&entries)?;
            clean_up_published_backups(&mut entries)?;
            sync_batch_parents(&entries)?;
            require_no_batch_sidecars(&entries)
        })();

        match publication {
            Ok(()) => Ok(()),
            Err(error) => Err(rollback_batch(&mut entries, error)),
        }
    }

    fn sync_all(&self) -> Result<(), String> {
        #[cfg(unix)]
        {
            use cap_fs_ext::{
                FollowSymlinks, OpenOptionsFollowExt as _, OpenOptionsMaybeDirExt as _,
            };
            let mut options = cap_std::fs::OpenOptions::new();
            options
                .read(true)
                .maybe_dir(true)
                .follow(FollowSymlinks::No);
            self.directory
                .open_with(".", &options)
                .and_then(|directory| directory.into_std().sync_all())
                .map_err(|error| {
                    format!(
                        "cannot flush directory {}: {error}",
                        self.display_path.display()
                    )
                })
        }
        #[cfg(not(unix))]
        {
            Ok(())
        }
    }
}

struct BatchPlan<B> {
    relative_path: PathBuf,
    bytes: B,
}

struct PreparedBatchEntry {
    parent: WorkspaceOutputDirectory,
    file_name: OsString,
    had_original: bool,
    original: Option<OutputIdentity>,
    replacement: OutputIdentity,
    temporary_name: Option<OsString>,
    temporary_live: bool,
    backup_name: Option<OsString>,
    backup_live: bool,
    backup_file: Option<std::fs::File>,
    destination_state: DestinationState,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum DestinationState {
    Untouched,
    VacantForPublication,
    ReplacementPublished,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct OutputIdentity {
    length: u64,
    sha256: [u8; 32],
}

fn bytes_identity(bytes: &[u8]) -> Result<OutputIdentity, String> {
    let length =
        u64::try_from(bytes.len()).map_err(|_| "generated output length exceeds u64".to_owned())?;
    let mut sha256 = [0_u8; 32];
    sha256.copy_from_slice(&Sha256::digest(bytes));
    Ok(OutputIdentity { length, sha256 })
}

fn validate_batch_plans<B>(
    root: &WorkspaceOutputDirectory,
    outputs: Vec<(PathBuf, B)>,
) -> Result<Vec<BatchPlan<B>>, String> {
    let mut plans = Vec::with_capacity(outputs.len());
    let mut paths = BTreeMap::new();
    for (relative_path, bytes) in outputs {
        validate_generated_output_path(root, &relative_path)?;
        let collision_key = portable_path_collision_key(&relative_path)
            .expect("validated generated output paths are portable UTF-8");
        if let Some(first) = paths.insert(collision_key, relative_path.clone()) {
            return Err(format!(
                "generated outputs {} and {} collide in one atomic batch after portable path normalization",
                root.display_path.join(first).display(),
                root.display_path.join(relative_path).display(),
            ));
        }
        plans.push(BatchPlan {
            relative_path,
            bytes,
        });
    }
    let paths = paths.into_values().collect::<Vec<_>>();
    for (left_index, left) in paths.iter().enumerate() {
        for right in paths.iter().skip(left_index + 1) {
            if workspace_paths_overlap(left, right) {
                return Err(format!(
                    "generated outputs {} and {} cannot be equal or nested after portable path normalization",
                    root.display_path.join(left).display(),
                    root.display_path.join(right).display(),
                ));
            }
        }
    }

    // Inspect every existing path before open_beneath is allowed to create a
    // missing parent for any target. Later hostile targets therefore cannot
    // leave earlier outputs partially prepared or published.
    for plan in &plans {
        prevalidate_existing_target(root, &plan.relative_path)?;
    }
    Ok(plans)
}

fn validate_generated_output_path(
    root: &WorkspaceOutputDirectory,
    relative_path: &Path,
) -> Result<(), String> {
    if relative_path.as_os_str().is_empty() {
        return Err(format!(
            "generated output path beneath {} is empty",
            root.display_path.display()
        ));
    }
    for component in relative_path.components() {
        let Component::Normal(name) = component else {
            return Err(format!(
                "generated output {} is not confined beneath {}",
                relative_path.display(),
                root.display_path.display()
            ));
        };
        validate_portable_direct_child(name).map_err(|error| {
            format!(
                "generated output component {:?} beneath {} is not portable: {error}",
                name,
                root.display_path.display()
            )
        })?;
    }
    Ok(())
}

fn prevalidate_existing_target(
    root: &WorkspaceOutputDirectory,
    relative_path: &Path,
) -> Result<(), String> {
    let mut directory = root
        .directory
        .try_clone()
        .map_err(|error| format!("cannot retain output root: {error}"))?;
    let mut display_path = root.display_path.clone();
    let parent = relative_path.parent().unwrap_or_else(|| Path::new(""));
    for component in parent.components() {
        let Component::Normal(name) = component else {
            unreachable!("generated output path was already validated")
        };
        display_path.push(name);
        match directory.symlink_metadata(name) {
            Ok(metadata) if metadata.file_type().is_symlink() || !metadata.is_dir() => {
                return Err(format!(
                    "output component {} must be a real directory, not a link",
                    display_path.display()
                ));
            }
            Ok(_) => {}
            Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()),
            Err(error) => {
                return Err(format!(
                    "cannot inspect {}: {error}",
                    display_path.display()
                ));
            }
        }
        directory = directory.open_dir_nofollow(name).map_err(|error| {
            format!(
                "output component {} must be a real directory, not a link: {error}",
                display_path.display()
            )
        })?;
    }

    let file_name = relative_path
        .file_name()
        .expect("validated output path has a final component");
    let destination = display_path.join(file_name);
    match directory.symlink_metadata(file_name) {
        Ok(metadata) if metadata.file_type().is_symlink() || !metadata.is_file() => Err(format!(
            "generated output {} must be a regular file, not a link or special entry",
            destination.display()
        )),
        Ok(_) => Ok(()),
        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(format!("cannot inspect {}: {error}", destination.display())),
    }
}

fn open_regular_output(
    parent: &WorkspaceOutputDirectory,
    file_name: &OsStr,
) -> Result<Option<std::fs::File>, String> {
    use cap_fs_ext::{FollowSymlinks, OpenOptionsFollowExt as _};

    let destination = parent.display_path.join(file_name);
    match parent.directory.symlink_metadata(file_name) {
        Ok(metadata) if metadata.file_type().is_symlink() || !metadata.is_file() => {
            return Err(format!(
                "generated output {} must be a regular file, not a link or special entry",
                destination.display()
            ));
        }
        Ok(_) => {}
        Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(None),
        Err(error) => {
            return Err(format!("cannot inspect {}: {error}", destination.display()));
        }
    }
    let mut options = cap_std::fs::OpenOptions::new();
    options.read(true).follow(FollowSymlinks::No);
    let file = parent
        .directory
        .open_with(file_name, &options)
        .map_err(|error| format!("cannot open {}: {error}", destination.display()))?
        .into_std();
    if !file
        .metadata()
        .map_err(|error| format!("cannot inspect {}: {error}", destination.display()))?
        .is_file()
    {
        return Err(format!(
            "generated output {} must be a regular file, not a link or special entry",
            destination.display()
        ));
    }
    Ok(Some(file))
}

fn regular_output_identity(
    parent: &WorkspaceOutputDirectory,
    file_name: &OsStr,
) -> Result<Option<OutputIdentity>, String> {
    let Some(mut file) = open_regular_output(parent, file_name)? else {
        return Ok(None);
    };
    stream_identity(&mut file).map(Some).map_err(|error| {
        format!(
            "cannot fingerprint {}: {error}",
            parent.display_path.join(file_name).display()
        )
    })
}

fn stream_identity(reader: &mut impl io::Read) -> io::Result<OutputIdentity> {
    let mut length = 0_u64;
    let mut hasher = Sha256::new();
    let mut buffer = [0_u8; 64 * 1024];
    loop {
        let read = reader.read(&mut buffer)?;
        if read == 0 {
            break;
        }
        length = length
            .checked_add(u64::try_from(read).expect("buffer length fits u64"))
            .ok_or_else(|| io::Error::other("generated output length exceeds u64"))?;
        hasher.update(&buffer[..read]);
    }
    let mut sha256 = [0_u8; 32];
    sha256.copy_from_slice(&hasher.finalize());
    Ok(OutputIdentity { length, sha256 })
}

fn prepare_batch_sidecars<B: AsRef<[u8]>>(
    entries: &mut [PreparedBatchEntry],
    payloads: &[B],
) -> Result<(), String> {
    for (entry, bytes) in entries.iter_mut().zip(payloads) {
        let (temporary, mut temporary_file) =
            create_unique_sidecar(&entry.parent, "typebridge-tmp")?;
        entry.temporary_name = Some(temporary);
        entry.temporary_live = true;
        temporary_file
            .write_all(bytes.as_ref())
            .and_then(|()| temporary_file.sync_all())
            .map_err(|error| {
                format!(
                    "cannot write {}: {error}",
                    entry
                        .parent
                        .display_path
                        .join(
                            entry
                                .temporary_name
                                .as_ref()
                                .expect("live temporary has a name")
                        )
                        .display()
                )
            })?;

        if entry.had_original {
            let mut original =
                open_regular_output(&entry.parent, &entry.file_name)?.ok_or_else(|| {
                    format!(
                        "generated output {} disappeared while its atomic batch was prepared",
                        entry.parent.display_path.join(&entry.file_name).display()
                    )
                })?;
            let (backup, mut backup_file) =
                create_unique_sidecar(&entry.parent, "typebridge-backup")?;
            entry.backup_name = Some(backup);
            entry.backup_live = true;
            let identity = stream_copy_identity(&mut original, &mut backup_file)
                .and_then(|identity| {
                    backup_file.sync_all()?;
                    backup_file.seek(io::SeekFrom::Start(0))?;
                    Ok(identity)
                })
                .map_err(|error| {
                    format!(
                        "cannot prepare rollback backup {}: {error}",
                        entry
                            .parent
                            .display_path
                            .join(entry.backup_name.as_ref().expect("live backup has a name"))
                            .display()
                    )
                })?;
            entry.original = Some(identity);
            entry.backup_file = Some(backup_file);
        }
    }
    Ok(())
}

fn create_unique_sidecar(
    parent: &WorkspaceOutputDirectory,
    kind: &str,
) -> Result<(OsString, std::fs::File), String> {
    use cap_fs_ext::{FollowSymlinks, OpenOptionsFollowExt as _};

    for attempt in 0..128_u64 {
        let candidate = unique_output_sidecar_name(kind, attempt);
        let mut options = cap_std::fs::OpenOptions::new();
        options
            .read(true)
            .write(true)
            .create_new(true)
            .follow(FollowSymlinks::No);
        match parent.directory.open_with(&candidate, &options) {
            Ok(file) => return Ok((candidate.into(), file.into_std())),
            Err(error) if error.kind() == io::ErrorKind::AlreadyExists => continue,
            Err(error) => {
                return Err(format!(
                    "cannot create {}: {error}",
                    parent.display_path.join(&candidate).display()
                ));
            }
        }
    }
    Err(format!(
        "cannot allocate a unique {kind} file beneath {}",
        parent.display_path.display()
    ))
}

fn stream_copy_identity(
    reader: &mut impl io::Read,
    writer: &mut impl io::Write,
) -> io::Result<OutputIdentity> {
    let mut length = 0_u64;
    let mut hasher = Sha256::new();
    let mut buffer = [0_u8; 64 * 1024];
    loop {
        let read = reader.read(&mut buffer)?;
        if read == 0 {
            break;
        }
        writer.write_all(&buffer[..read])?;
        length = length
            .checked_add(u64::try_from(read).expect("buffer length fits u64"))
            .ok_or_else(|| io::Error::other("generated output length exceeds u64"))?;
        hasher.update(&buffer[..read]);
    }
    let mut sha256 = [0_u8; 32];
    sha256.copy_from_slice(&hasher.finalize());
    Ok(OutputIdentity { length, sha256 })
}

fn require_unchanged_destination(entry: &PreparedBatchEntry) -> Result<(), String> {
    let current = regular_output_identity(&entry.parent, &entry.file_name)?;
    if current == entry.original {
        Ok(())
    } else {
        Err(format!(
            "generated output {} changed while its atomic batch was prepared",
            entry.parent.display_path.join(&entry.file_name).display()
        ))
    }
}

fn sync_batch_parents(entries: &[PreparedBatchEntry]) -> Result<(), String> {
    let mut synced = BTreeSet::new();
    for entry in entries {
        if synced.insert(entry.parent.display_path.clone()) {
            entry.parent.sync_all()?;
        }
    }
    Ok(())
}

fn clean_up_published_backups(entries: &mut [PreparedBatchEntry]) -> Result<(), String> {
    for entry in entries {
        if entry.backup_live {
            let backup = entry.backup_name.as_ref().expect("live backup has a name");
            entry
                .parent
                .directory
                .remove_file(backup)
                .map_err(|error| {
                    format!(
                        "cannot remove completed generation backup {}: {error}",
                        entry.parent.display_path.join(backup).display()
                    )
                })?;
            entry.backup_live = false;
        }
    }
    Ok(())
}

fn clean_up_unpublished_batch(entries: &mut [PreparedBatchEntry], primary: String) -> String {
    let mut errors = Vec::new();
    clean_up_batch_sidecars(entries, &mut errors);
    if let Err(error) = sync_batch_parents(entries) {
        errors.push(error);
    }
    if let Err(error) = require_no_batch_sidecars(entries) {
        errors.push(error);
    }
    append_rollback_errors(primary, errors)
}

fn rollback_batch(entries: &mut [PreparedBatchEntry], primary: String) -> String {
    let mut errors = Vec::new();
    for entry in entries.iter_mut().rev() {
        if entry.destination_state == DestinationState::Untouched {
            continue;
        }
        if entry.destination_state == DestinationState::ReplacementPublished {
            match regular_output_identity(&entry.parent, &entry.file_name) {
                Ok(Some(current)) if current == entry.replacement => {
                    match entry.parent.directory.remove_file(&entry.file_name) {
                        Ok(()) => {
                            entry.destination_state = DestinationState::VacantForPublication;
                        }
                        Err(error) => {
                            errors.push(format!(
                                "cannot remove newly published output {} before rollback: {error}",
                                entry.parent.display_path.join(&entry.file_name).display()
                            ));
                            continue;
                        }
                    }
                }
                Ok(current) => {
                    errors.push(format!(
                        "cannot roll back {} because the published destination changed: expected {:?}, found {current:?}",
                        entry.parent.display_path.join(&entry.file_name).display(),
                        entry.replacement
                    ));
                    continue;
                }
                Err(error) => {
                    errors.push(error);
                    continue;
                }
            }
        }
        if !require_vacant_rollback_destination(entry, &mut errors) {
            continue;
        }
        match entry.original {
            Some(_) => {
                let restored_from_backup = if entry.backup_live {
                    let backup = entry.backup_name.as_ref().expect("live backup has a name");
                    match entry.parent.directory.rename(
                        backup,
                        &entry.parent.directory,
                        &entry.file_name,
                    ) {
                        Ok(()) => {
                            entry.backup_live = false;
                            true
                        }
                        Err(error) => {
                            errors.push(format!(
                                "cannot restore {} from its generation backup: {error}",
                                entry.parent.display_path.join(&entry.file_name).display()
                            ));
                            false
                        }
                    }
                } else {
                    false
                };
                let restored = if restored_from_backup {
                    true
                } else {
                    match restore_original_from_retained_backup(entry) {
                        Ok(()) => true,
                        Err(error) => {
                            errors.push(error);
                            false
                        }
                    }
                };
                if restored {
                    entry.destination_state = DestinationState::Untouched;
                }
            }
            None => entry.destination_state = DestinationState::Untouched,
        }
    }
    clean_up_batch_sidecars(entries, &mut errors);
    if let Err(error) = sync_batch_parents(entries) {
        errors.push(error);
    }
    for entry in entries.iter() {
        match regular_output_identity(&entry.parent, &entry.file_name) {
            Ok(current) if current == entry.original => {}
            Ok(_) => errors.push(format!(
                "rollback did not restore byte-identical output {}",
                entry.parent.display_path.join(&entry.file_name).display()
            )),
            Err(error) => errors.push(error),
        }
    }
    if let Err(error) = require_no_batch_sidecars(entries) {
        errors.push(error);
    }
    append_rollback_errors(primary, errors)
}

fn require_vacant_rollback_destination(
    entry: &PreparedBatchEntry,
    errors: &mut Vec<String>,
) -> bool {
    match entry.parent.directory.symlink_metadata(&entry.file_name) {
        Err(error) if error.kind() == io::ErrorKind::NotFound => true,
        Ok(_) => {
            errors.push(format!(
                "cannot restore {} because its destination reappeared during publication",
                entry.parent.display_path.join(&entry.file_name).display()
            ));
            false
        }
        Err(error) => {
            errors.push(format!(
                "cannot inspect {} before rollback: {error}",
                entry.parent.display_path.join(&entry.file_name).display()
            ));
            false
        }
    }
}

fn restore_original_from_retained_backup(entry: &mut PreparedBatchEntry) -> Result<(), String> {
    let expected = entry
        .original
        .expect("published pre-existing output has an identity");
    let backup_file = entry
        .backup_file
        .as_mut()
        .expect("published pre-existing output retains its backup handle");
    backup_file
        .seek(io::SeekFrom::Start(0))
        .map_err(|error| format!("cannot rewind retained generation backup: {error}"))?;
    let (restore, mut restore_file) = create_unique_sidecar(&entry.parent, "typebridge-rollback")?;
    let restoration = (|| -> Result<(), String> {
        let restored = stream_copy_identity(backup_file, &mut restore_file)
            .and_then(|identity| {
                restore_file.sync_all()?;
                Ok(identity)
            })
            .map_err(|error| format!("cannot copy retained generation backup: {error}"))?;
        if restored != expected {
            return Err("retained generation backup identity changed before rollback".to_owned());
        }
        entry
            .parent
            .directory
            .rename(&restore, &entry.parent.directory, &entry.file_name)
            .map_err(|error| {
                format!(
                    "cannot restore original output {}: {error}",
                    entry.parent.display_path.join(&entry.file_name).display()
                )
            })
    })();
    if let Err(primary) = restoration {
        return match entry.parent.directory.remove_file(&restore) {
            Ok(()) => Err(primary),
            Err(error) if error.kind() == io::ErrorKind::NotFound => Err(primary),
            Err(error) => Err(format!(
                "{primary}; cannot remove rollback sidecar {}: {error}",
                entry.parent.display_path.join(&restore).display()
            )),
        };
    }
    Ok(())
}

fn clean_up_batch_sidecars(entries: &mut [PreparedBatchEntry], errors: &mut Vec<String>) {
    for entry in entries {
        let mut sidecars = vec![(&entry.temporary_name, &mut entry.temporary_live)];
        if entry.destination_state == DestinationState::Untouched {
            sidecars.push((&entry.backup_name, &mut entry.backup_live));
        }
        for (name, live) in sidecars {
            if !*live {
                continue;
            }
            let name = name.as_ref().expect("live sidecar has a name");
            match entry.parent.directory.remove_file(name) {
                Ok(()) => *live = false,
                Err(error) if error.kind() == io::ErrorKind::NotFound => *live = false,
                Err(error) => errors.push(format!(
                    "cannot remove generation sidecar {}: {error}",
                    entry.parent.display_path.join(name).display()
                )),
            }
        }
    }
}

fn require_no_batch_sidecars(entries: &[PreparedBatchEntry]) -> Result<(), String> {
    for entry in entries {
        for name in [&entry.temporary_name, &entry.backup_name]
            .into_iter()
            .flatten()
        {
            match entry.parent.directory.symlink_metadata(name) {
                Ok(_) => {
                    return Err(format!(
                        "generation sidecar {} was not removed",
                        entry.parent.display_path.join(name).display()
                    ));
                }
                Err(error) if error.kind() == io::ErrorKind::NotFound => {}
                Err(error) => {
                    return Err(format!(
                        "cannot verify generation sidecar cleanup for {}: {error}",
                        entry.parent.display_path.join(name).display()
                    ));
                }
            }
        }
    }
    Ok(())
}

fn append_rollback_errors(primary: String, errors: Vec<String>) -> String {
    if errors.is_empty() {
        primary
    } else {
        format!(
            "{primary}; atomic generation rollback failed: {}",
            errors.join("; ")
        )
    }
}

fn unique_output_sidecar_name(kind: &str, attempt: u64) -> String {
    use std::sync::atomic::{AtomicU64, Ordering};
    static NEXT_OUTPUT_TEMPORARY: AtomicU64 = AtomicU64::new(1);
    let nonce = NEXT_OUTPUT_TEMPORARY.fetch_add(1, Ordering::Relaxed);
    format!(".{kind}.{}.{}.{}", std::process::id(), nonce, attempt)
}

impl SchemaSourceService for WorkspaceDirectoryAuthority {
    fn canonicalize(&self, path: &Path) -> Result<PathBuf, SchemaSourceServiceError> {
        let relative = self.relative_path(path)?;
        let canonical = self
            .directory
            .canonicalize(relative)
            .map_err(|_| SchemaSourceServiceError)?;
        if canonical.is_absolute()
            || canonical
                .components()
                .any(|component| matches!(component, Component::ParentDir | Component::Prefix(_)))
        {
            return Err(SchemaSourceServiceError);
        }
        Ok(self.root.as_path().join(canonical))
    }

    fn metadata(&self, path: &Path) -> Result<SchemaSourceObservation, SchemaSourceServiceError> {
        let relative = self.relative_path(path)?;
        let metadata = self
            .directory
            .metadata(relative)
            .map_err(|_| SchemaSourceServiceError)?;
        observation(&metadata, path)
    }

    fn symlink_metadata(
        &self,
        path: &Path,
    ) -> Result<SchemaSourceObservation, SchemaSourceServiceError> {
        let relative = self.relative_path(path)?;
        let metadata = self
            .directory
            .symlink_metadata(relative)
            .map_err(|_| SchemaSourceServiceError)?;
        observation(&metadata, path)
    }

    fn read_directory_names(&self, path: &Path) -> Result<Vec<OsString>, SchemaSourceServiceError> {
        let relative = self.relative_path(path)?;
        let mut names = self
            .directory
            .read_dir(relative)
            .map_err(|_| SchemaSourceServiceError)?
            .map(|entry| {
                entry
                    .map(|entry| entry.file_name())
                    .map_err(|_| SchemaSourceServiceError)
            })
            .collect::<Result<Vec<_>, _>>()?;
        names.sort();
        Ok(names)
    }

    fn capture_file(
        &self,
        path: &Path,
        maximum_bytes: usize,
    ) -> Result<SchemaSourceCapture, SchemaSourceServiceError> {
        let relative = self.relative_path(path)?;
        let mut file = self
            .directory
            .open(relative)
            .map_err(|_| SchemaSourceServiceError)?;
        let before = observation(
            &file.metadata().map_err(|_| SchemaSourceServiceError)?,
            path,
        )?;
        let read_limit = u64::try_from(maximum_bytes)
            .unwrap_or(u64::MAX)
            .saturating_add(1);
        let mut bytes = Vec::new();
        (&mut file)
            .take(read_limit)
            .read_to_end(&mut bytes)
            .map_err(|_| SchemaSourceServiceError)?;
        let after = observation(
            &file.metadata().map_err(|_| SchemaSourceServiceError)?,
            path,
        )?;
        Ok(SchemaSourceCapture::new(bytes, before, after))
    }
}

pub(crate) fn retain_canonical_root(root: &Path) -> io::Result<Dir> {
    let mut anchor = PathBuf::new();
    let mut names = Vec::new();
    let mut reached_name = false;

    for component in root.components() {
        match component {
            Component::Prefix(prefix) if !reached_name => anchor.push(prefix.as_os_str()),
            Component::RootDir if !reached_name => anchor.push(component.as_os_str()),
            Component::Normal(name) => {
                reached_name = true;
                names.push(name.to_owned());
            }
            Component::Prefix(_)
            | Component::RootDir
            | Component::CurDir
            | Component::ParentDir => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "workspace root is not a canonical absolute directory",
                ));
            }
        }
    }
    if anchor.as_os_str().is_empty() {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "workspace root is not absolute",
        ));
    }

    let mut directory = Dir::open_ambient_dir(anchor, ambient_authority())?;
    for name in names {
        directory = directory.open_dir_nofollow(name)?;
    }
    Ok(directory)
}

fn observation(
    metadata: &Metadata,
    path: &Path,
) -> Result<SchemaSourceObservation, SchemaSourceServiceError> {
    let modified = metadata
        .modified()
        .map_err(|_| SchemaSourceServiceError)?
        .into_std();
    let revision = match modified.duration_since(UNIX_EPOCH) {
        Ok(duration) => format!("after:{}:{}", duration.as_secs(), duration.subsec_nanos()),
        Err(error) => {
            let duration = error.duration();
            format!("before:{}:{}", duration.as_secs(), duration.subsec_nanos())
        }
    };
    let kind = if metadata.is_file() {
        SchemaSourceKind::File
    } else if metadata.is_dir() {
        SchemaSourceKind::Directory
    } else if metadata.is_symlink() {
        SchemaSourceKind::Symlink
    } else {
        SchemaSourceKind::Other
    };
    Ok(SchemaSourceObservation::new(
        source_identity(metadata, path)?,
        SchemaSourceRevision::new(revision)?,
        metadata.len(),
        kind,
    ))
}

#[cfg(unix)]
fn source_identity(
    metadata: &Metadata,
    _path: &Path,
) -> Result<SchemaSourceIdentity, SchemaSourceServiceError> {
    use cap_std::fs::MetadataExt as _;
    SchemaSourceIdentity::new(format!("unix:{}:{}", metadata.dev(), metadata.ino()))
}

#[cfg(not(unix))]
fn source_identity(
    _metadata: &Metadata,
    path: &Path,
) -> Result<SchemaSourceIdentity, SchemaSourceServiceError> {
    SchemaSourceIdentity::new(path.to_string_lossy())
}

#[cfg(test)]
#[path = "authority_atomic_batch_tests.rs"]
mod atomic_batch_tests;