ignition-core 1.6.1

Core library for ign: config, profiles, gateway client, actions, error taxonomy
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
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
//! The in-memory source and rendered trees, and the render digest.
//!
//! [`SourceTree`] is what render reads: gateway-relative path to raw bytes.
//! [`RenderedTree`] is what render produces: collection path to raw bytes.
//! Both are `BTreeMap`s keyed by [`RelPath`], so iteration is byte-sorted
//! on every platform and never depends on directory order.
//!
//! ## `render_sha256` listing format (documented in the README)
//!
//! For every rendered file, in byte-sorted collection-path order, one entry:
//!
//! ```text
//! <collection path> NUL <lowercase hex sha256 of the file bytes> LF
//! ```
//!
//! The digest is the lowercase hex SHA-256 of the concatenated entries. An
//! empty tree hashes the empty listing ([`EMPTY_RENDER_SHA256`]).

use std::collections::{BTreeMap, BTreeSet};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use super::layout::RelPath;
use super::transport::READ_MAX_FILE_BYTES;
use crate::error::{CoreError, DeployFinding};

/// SHA-256 of the empty listing (the digest of a render that produced no
/// files). Equal to the SHA-256 of zero bytes.
pub const EMPTY_RENDER_SHA256: &str =
    "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";

/// Lowercase hex SHA-256 of `bytes`.
pub fn sha256_hex(bytes: &[u8]) -> String {
    format!("{:x}", Sha256::digest(bytes))
}

/// Source files of one gateway directory, keyed by gateway-relative path
/// (`resources/<module>/<type>/...`).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SourceTree {
    /// Gateway-relative path to raw file bytes.
    pub files: BTreeMap<RelPath, Vec<u8>>,
}

/// The rendered files, keyed by collection path (`<module>/<type>/...`).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RenderedTree {
    /// Collection path to raw file bytes.
    pub files: BTreeMap<RelPath, Vec<u8>>,
}

impl RenderedTree {
    /// Total size of every rendered file in bytes.
    pub fn bytes(&self) -> usize {
        self.files.values().map(Vec::len).sum()
    }

    /// The `render_sha256` digest (see the module doc for the listing).
    pub fn render_sha256(&self) -> String {
        let mut hasher = Sha256::new();
        for (path, bytes) in &self.files {
            hasher.update(path.as_str().as_bytes());
            hasher.update([0u8]);
            hasher.update(sha256_hex(bytes).as_bytes());
            hasher.update(b"\n");
        }
        format!("{:x}", hasher.finalize())
    }
}

impl SourceTree {
    /// Build a tree in memory (tests). Later entries replace earlier ones
    /// with the same path.
    pub fn from_files<I, P, B>(files: I) -> Result<SourceTree, String>
    where
        I: IntoIterator<Item = (P, B)>,
        P: AsRef<str>,
        B: Into<Vec<u8>>,
    {
        let mut map = BTreeMap::new();
        for (path, bytes) in files {
            map.insert(RelPath::new(path.as_ref())?, bytes.into());
        }
        Ok(SourceTree { files: map })
    }

    /// Read `resources/`, `tags/` and `modes/<env>/` of one gateway directory.
    ///
    /// Every entry is inspected with `symlink_metadata` and never followed;
    /// each directory's entries are sorted by name bytes before recursing.
    /// At the gateway root `manifest.yaml` (not collected, the action reads
    /// it), `resources/`, `tags/` (walked), `modes/` and `.gitkeep` (skipped)
    /// are accepted; any other root entry is a finding. Under `modes/` only
    /// the directory named exactly `env` is opened; other entries that are
    /// directories belong to other environments and are skipped unopened, so
    /// their content can never fail or change this render. Under
    /// `modes/<env>/` only `resources/` and `tags/` are accepted. Overlay
    /// files are keyed by their full gateway-relative path
    /// (`modes/<env>/resources/...`); render's overlay stage resolves them.
    /// Symlinks, non-regular files and unusable names are findings. All
    /// findings are raised together as one `deploy_tree_invalid`.
    pub fn read_gateway(gateway_dir: &Path, env: &str) -> Result<SourceTree, CoreError> {
        let gateway = gateway_dir
            .file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_default();
        let mut reader = Reader {
            prefix: format!("gateways/{gateway}"),
            env: env.to_string(),
            files: BTreeMap::new(),
            findings: Vec::new(),
        };
        reader.read_root(gateway_dir);
        if reader.findings.is_empty() {
            Ok(SourceTree {
                files: reader.files,
            })
        } else {
            Err(CoreError::deploy_findings(reader.findings))
        }
    }
}

/// File name of the marker `--out` writes at the root of a rendered tree.
pub const RENDER_MARKER: &str = "render.json";

/// Suffix of the temporary file a write goes through before its rename.
pub(crate) const TMP_SUFFIX: &str = ".ign-render-tmp";

/// `render.json`: ign's own record of a render written with `--out`. It is
/// not a gateway file and is never part of `render_sha256`. Phase 22 reads
/// it (`plan --base`), so its shape is versioned by `schema_version`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RenderMarker {
    /// Marker schema version (1).
    pub schema_version: u32,
    /// Gateway directory name the render came from.
    pub gateway: String,
    /// Environment the render was made for.
    pub env: String,
    /// Deployment mode from the target.
    pub mode: String,
    /// The render's `render_sha256` (equals the envelope's).
    pub render_sha256: String,
    /// Every written file, byte-sorted by path.
    pub files: Vec<MarkerFile>,
}

/// One file entry of [`RenderMarker`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MarkerFile {
    /// Collection path, relative to the `--out` directory.
    pub path: String,
    /// Lowercase hex SHA-256 of the file bytes.
    pub sha256: String,
    /// File size in bytes.
    pub bytes: u64,
}

impl RenderMarker {
    /// The marker for `tree` (files in byte-sorted path order).
    pub fn for_tree(gateway: &str, env: &str, mode: &str, tree: &RenderedTree) -> RenderMarker {
        RenderMarker {
            schema_version: 1,
            gateway: gateway.to_string(),
            env: env.to_string(),
            mode: mode.to_string(),
            render_sha256: tree.render_sha256(),
            files: tree
                .files
                .iter()
                .map(|(path, bytes)| MarkerFile {
                    path: path.as_str().to_string(),
                    sha256: sha256_hex(bytes),
                    bytes: bytes.len() as u64,
                })
                .collect(),
        }
    }

    /// The marker as written: pretty JSON plus one LF.
    pub fn to_bytes(&self) -> Result<Vec<u8>, CoreError> {
        let mut text = serde_json::to_string_pretty(self)
            .map_err(|e| CoreError::Internal(format!("cannot serialize {RENDER_MARKER}: {e}")))?;
        text.push('\n');
        Ok(text.into_bytes())
    }
}

fn refuse(out: &Path, reason: impl std::fmt::Display) -> CoreError {
    CoreError::InvalidInput {
        reason: format!(
            "--out {}: {reason}; remove the directory or choose an empty or absent one",
            out.display()
        ),
    }
}

/// What the check phase found.
enum OutPlan {
    /// The path is absent: create it.
    Create,
    /// An existing empty directory.
    Empty,
    /// A prior render of the same gateway and environment: its listed
    /// files (unchanged) may be replaced.
    Refresh(BTreeSet<String>),
}

/// Inspect `out` without writing anything and decide whether the render may
/// be written there.
fn check_out(tree: &RenderedTree, out: &Path, marker: &RenderMarker) -> Result<OutPlan, CoreError> {
    if tree.files.keys().any(|p| p.as_str() == RENDER_MARKER) {
        return Err(refuse(
            out,
            format!("the render holds a file named {RENDER_MARKER}"),
        ));
    }
    let meta = match std::fs::symlink_metadata(out) {
        Ok(meta) => meta,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(OutPlan::Create),
        Err(e) => return Err(refuse(out, format!("cannot inspect it ({})", e.kind()))),
    };
    if meta.file_type().is_symlink() {
        return Err(refuse(out, "is a symlink; symlinks are refused"));
    }
    if !meta.file_type().is_dir() {
        return Err(refuse(out, "exists and is not a directory"));
    }

    let mut files: BTreeMap<String, PathBuf> = BTreeMap::new();
    let mut dirs: BTreeSet<String> = BTreeSet::new();
    walk_out(out, "", &mut files, &mut dirs).map_err(|why| refuse(out, why))?;
    if files.is_empty() && dirs.is_empty() {
        return Ok(OutPlan::Empty);
    }

    let marker_path = match files.get(RENDER_MARKER) {
        Some(path) => path,
        None => {
            let first = files
                .keys()
                .next()
                .cloned()
                .or_else(|| dirs.iter().next().cloned())
                .unwrap_or_default();
            return Err(refuse(
                out,
                format!("is not empty and holds no {RENDER_MARKER} (first entry: {first})"),
            ));
        }
    };
    let prior: RenderMarker = std::fs::read(marker_path)
        .map_err(|e| refuse(out, format!("cannot read {RENDER_MARKER} ({})", e.kind())))
        .and_then(|bytes| {
            serde_json::from_slice(&bytes)
                .map_err(|_| refuse(out, format!("{RENDER_MARKER} is not a valid render marker")))
        })?;
    if prior.schema_version != 1 {
        return Err(refuse(
            out,
            format!(
                "{RENDER_MARKER} has schema_version {}, expected 1",
                prior.schema_version
            ),
        ));
    }
    if prior.gateway != marker.gateway || prior.env != marker.env {
        return Err(refuse(
            out,
            format!(
                "{RENDER_MARKER} belongs to gateway {} env {}, not gateway {} env {}",
                prior.gateway, prior.env, marker.gateway, marker.env
            ),
        ));
    }

    let mut listed: BTreeSet<String> = BTreeSet::new();
    for entry in &prior.files {
        if !listed.insert(entry.path.clone()) {
            return Err(refuse(
                out,
                format!("{RENDER_MARKER} lists {} twice", entry.path),
            ));
        }
    }
    // every regular file other than the marker is listed with its sha256
    for (rel, path) in &files {
        if rel == RENDER_MARKER {
            continue;
        }
        let Some(entry) = prior.files.iter().find(|f| &f.path == rel) else {
            return Err(refuse(
                out,
                format!("{rel} is not listed in {RENDER_MARKER}"),
            ));
        };
        let bytes = std::fs::read(path)
            .map_err(|e| refuse(out, format!("cannot read {rel} ({})", e.kind())))?;
        if sha256_hex(&bytes) != entry.sha256 {
            return Err(refuse(
                out,
                format!("{rel} was modified since it was rendered"),
            ));
        }
    }
    // every listed file is present, and still produced by the new render
    for rel in &listed {
        if !files.contains_key(rel) {
            return Err(refuse(
                out,
                format!("{rel} is listed in {RENDER_MARKER} but missing"),
            ));
        }
        if !tree.files.keys().any(|p| p.as_str() == rel) {
            return Err(refuse(
                out,
                format!("{rel} was rendered before but this render no longer produces it"),
            ));
        }
    }
    // no directory that holds no listed file
    for dir in &dirs {
        let prefix = format!("{dir}/");
        if !listed.iter().any(|rel| rel.starts_with(&prefix)) {
            return Err(refuse(out, format!("{dir}/ holds no rendered file")));
        }
    }
    // a new path may not collide with an existing file or directory
    for path in tree.files.keys() {
        let rel = path.as_str();
        if listed.contains(rel) {
            continue;
        }
        if files.contains_key(rel) || dirs.contains(rel) {
            return Err(refuse(out, format!("{rel} already exists")));
        }
        let mut at = 0;
        while let Some(i) = rel[at..].find('/') {
            at += i;
            if files.contains_key(&rel[..at]) {
                return Err(refuse(
                    out,
                    format!("{} is a file, not a directory", &rel[..at]),
                ));
            }
            at += 1;
        }
    }
    Ok(OutPlan::Refresh(listed))
}

/// Walk `dir` with `symlink_metadata`, entries sorted by name bytes.
/// Symlinks, special files and non-UTF-8 names are errors naming the path.
/// Shared by `--out` checks and the live-tree readers (plan, pull).
pub(crate) fn walk_out(
    dir: &Path,
    rel: &str,
    files: &mut BTreeMap<String, PathBuf>,
    dirs: &mut BTreeSet<String>,
) -> Result<(), String> {
    walk_capped(dir, rel, files, dirs, usize::MAX)
}

/// [`walk_out`] that stops with an error as soon as `files` and `dirs`
/// together hold more than `max_entries` entries, so a hostile tree cannot
/// be enumerated in full before a cap is checked.
pub(crate) fn walk_capped(
    dir: &Path,
    rel: &str,
    files: &mut BTreeMap<String, PathBuf>,
    dirs: &mut BTreeSet<String>,
    max_entries: usize,
) -> Result<(), String> {
    let read = std::fs::read_dir(dir)
        .map_err(|e| format!("cannot read directory {} ({})", join(rel, ""), e.kind()))?;
    let listing = read.map(|entry| entry.map(|entry| (entry.file_name(), entry.path())));
    let entries = collect_capped(listing, rel, files.len() + dirs.len(), max_entries)?;
    for (name, path) in entries {
        let child = join(rel, &name);
        let meta = std::fs::symlink_metadata(&path)
            .map_err(|e| format!("cannot inspect {child} ({})", e.kind()))?;
        let kind = meta.file_type();
        if kind.is_symlink() {
            return Err(format!("{child} is a symlink; symlinks are refused"));
        } else if kind.is_dir() {
            dirs.insert(child.clone());
            if files.len() + dirs.len() > max_entries {
                return Err(format!("more than {max_entries} entries (at {child})"));
            }
            walk_capped(&path, &child, files, dirs, max_entries)?;
        } else if kind.is_file() {
            files.insert(child.clone(), path);
            if files.len() + dirs.len() > max_entries {
                return Err(format!("more than {max_entries} entries (at {child})"));
            }
        } else {
            return Err(format!("{child} is not a regular file or directory"));
        }
    }
    Ok(())
}

/// Collect one directory's entries, sorted by name bytes, stopping with an
/// error as soon as `taken` (entries the walk already holds) plus the entries
/// collected here exceed `max_entries`, so a flat directory of millions of
/// entries is never enumerated or allocated in full.
fn collect_capped(
    listing: impl Iterator<Item = std::io::Result<(std::ffi::OsString, PathBuf)>>,
    rel: &str,
    taken: usize,
    max_entries: usize,
) -> Result<Vec<(String, PathBuf)>, String> {
    let mut entries: Vec<(String, PathBuf)> = Vec::new();
    for entry in listing {
        let (raw, path) =
            entry.map_err(|e| format!("cannot read a directory entry ({})", e.kind()))?;
        let name = raw.into_string().map_err(|raw| {
            format!(
                "{} has a name that is not valid UTF-8",
                join(rel, &raw.to_string_lossy())
            )
        })?;
        // The sftp transport refuses control characters in a listed name;
        // the local walk does the same so the two transports agree and a
        // name can never carry an escape sequence into a plan or its output.
        if name.chars().any(char::is_control) {
            return Err(format!(
                "{} holds an entry whose name has a control character; refused",
                join(rel, "")
            ));
        }
        if taken.saturating_add(entries.len()) >= max_entries {
            return Err(format!(
                "more than {max_entries} entries (at {})",
                join(rel, &name)
            ));
        }
        entries.push((name, path));
    }
    entries.sort_by(|a, b| a.0.as_bytes().cmp(b.0.as_bytes()));
    Ok(entries)
}

/// Why [`open_regular_file`] refused.
#[derive(Debug)]
pub(crate) enum OpenError {
    /// The open or a metadata call failed.
    Io(std::io::Error),
    /// The opened handle is not the regular file that was inspected: it was
    /// swapped (for a symlink target or another file) after the check.
    Changed,
}

/// Open `rel` beneath the directory `root` and validate the HANDLE against
/// `before`, the `symlink_metadata` taken when the path was inspected.
///
/// On unix the open is descriptor-relative: `root` is opened once as a
/// directory fd, then every component of `rel` is opened with `openat`
/// relative to the previous fd, intermediate directories with
/// `O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC` and the final file with
/// `O_NOFOLLOW | O_CLOEXEC` (read-only). A component that is, or has been
/// swapped for, a symlink fails the open (`ELOOP` / `ENOTDIR`) instead of
/// redirecting it, so no component at any depth can lead outside `root` even
/// when it is replaced after the walk and the metadata check. The handle is
/// then `fstat`ed: it must be a regular file and the same device and inode as
/// `before`. `root` itself is the operator's own directory and is opened
/// through a final symlink (callers that refuse a symlinked root do so first).
///
/// On windows the full path is opened with `FILE_FLAG_OPEN_REPARSE_POINT` and
/// the handle must not be a reparse point. Windows has no descriptor-relative
/// open in std, so an intermediate directory swapped for a link between the
/// caller's check and the open remains a residual race there.
///
/// Returns the file and its handle metadata (take sizes from that).
pub(crate) fn open_regular_file(
    root: &Path,
    rel: &Path,
    before: &std::fs::Metadata,
) -> Result<(std::fs::File, std::fs::Metadata), OpenError> {
    #[cfg(unix)]
    let file = open_beneath(root, rel)?;
    #[cfg(windows)]
    let file = {
        use std::os::windows::fs::OpenOptionsExt;
        let mut options = std::fs::OpenOptions::new();
        // FILE_FLAG_OPEN_REPARSE_POINT: open the link itself, never its target.
        options.read(true).custom_flags(0x0020_0000);
        options.open(root.join(rel)).map_err(OpenError::Io)?
    };
    #[cfg(not(any(unix, windows)))]
    let file = std::fs::File::open(root.join(rel)).map_err(OpenError::Io)?;

    let held = file.metadata().map_err(OpenError::Io)?;
    if held.file_type().is_symlink() || !held.is_file() {
        return Err(OpenError::Changed);
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt;
        if held.dev() != before.dev() || held.ino() != before.ino() {
            return Err(OpenError::Changed);
        }
    }
    #[cfg(not(unix))]
    let _ = before;
    Ok((file, held))
}

/// The descriptor-relative open behind [`open_regular_file`] (unix).
#[cfg(unix)]
fn open_beneath(root: &Path, rel: &Path) -> Result<std::fs::File, OpenError> {
    use rustix::fs::{Mode, OFlags, open, openat};
    use rustix::io::Errno;
    use std::path::Component;

    // A swapped or non-directory component: the tree changed under us.
    let map = |e: Errno| -> OpenError {
        if e == Errno::LOOP || e == Errno::NOTDIR {
            OpenError::Changed
        } else {
            OpenError::Io(e.into())
        }
    };

    let mut parts = Vec::new();
    for component in rel.components() {
        match component {
            Component::Normal(name) => parts.push(name),
            // `.`, `..`, a root or a prefix could leave `root`.
            _ => return Err(OpenError::Changed),
        }
    }
    let Some((last, parents)) = parts.split_last() else {
        return Err(OpenError::Changed);
    };

    let mut dir = open(
        root,
        OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
        Mode::empty(),
    )
    .map_err(map)?;
    let dir_flags = OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::DIRECTORY | OFlags::CLOEXEC;
    for part in parents {
        dir = openat(&dir, *part, dir_flags, Mode::empty()).map_err(map)?;
    }
    // NONBLOCK: a FIFO swapped in after the caller's check must fail the
    // fstat comparison, not block `open(2)` until a writer appears. A regular
    // file is unaffected.
    let fd = openat(
        &dir,
        *last,
        OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::CLOEXEC,
        Mode::empty(),
    )
    .map_err(map)?;
    Ok(std::fs::File::from(fd))
}

/// Descriptor-relative mutation helpers (unix), the write-side twin of
/// [`open_regular_file`]'s read chain.
///
/// Every function takes a directory handle and ONE name, never a path, and
/// every open of a directory uses `O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC`. A
/// component that is, or has been swapped for, a symlink (or any non-directory)
/// fails with [`FdError::Changed`] instead of redirecting the operation, so a
/// mutation can never leave the directory it was rooted in however the tree
/// changes underneath. The root of a chain is the operator's own directory and
/// is opened without following a final symlink.
///
/// Windows has no descriptor-relative primitives in std; its callers stay
/// path-based and carry that as a documented residual.
#[cfg(unix)]
pub(crate) mod dirfd {
    use std::fs::File;
    use std::io::Read;
    use std::os::unix::fs::MetadataExt;
    use std::path::Path;

    use rustix::fs::{
        AtFlags, FileType, Mode, OFlags, mkdirat, openat, renameat, statat, unlinkat,
    };
    use rustix::io::Errno;

    /// Why a descriptor-relative operation was refused.
    #[derive(Debug)]
    pub(crate) enum FdError {
        /// A component is, or became, a symlink or a non-directory (`ELOOP`,
        /// `ENOTDIR`, `ENXIO`), or an opened handle is not what was inspected.
        Changed,
        Missing,
        Exists,
        NotEmpty,
        Io(std::io::Error),
    }

    fn map(e: Errno) -> FdError {
        if e == Errno::LOOP || e == Errno::NOTDIR || e == Errno::NXIO {
            FdError::Changed
        } else if e == Errno::NOENT {
            FdError::Missing
        } else if e == Errno::EXIST {
            FdError::Exists
        } else if e == Errno::NOTEMPTY {
            FdError::NotEmpty
        } else {
            FdError::Io(e.into())
        }
    }

    /// What a name is inside a directory (never followed).
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub(crate) enum Kind {
        Missing,
        Dir,
        File,
        Symlink,
        Other,
    }

    const DIR_FLAGS: OFlags = OFlags::RDONLY
        .union(OFlags::DIRECTORY)
        .union(OFlags::NOFOLLOW)
        .union(OFlags::CLOEXEC);

    /// Open `path` as a directory without following a final symlink.
    pub(crate) fn open_root(path: &Path) -> Result<File, FdError> {
        rustix::fs::open(path, DIR_FLAGS, Mode::empty())
            .map(File::from)
            .map_err(map)
    }

    /// Open the directory `name` inside `dir`.
    pub(crate) fn open_dir(dir: &File, name: &str) -> Result<File, FdError> {
        openat(dir, name, DIR_FLAGS, Mode::empty())
            .map(File::from)
            .map_err(map)
    }

    /// Open `parts` below `dir`, one `openat` per component.
    pub(crate) fn walk(dir: &File, parts: &[&str]) -> Result<File, FdError> {
        let mut current = dir.try_clone().map_err(FdError::Io)?;
        for part in parts {
            current = open_dir(&current, part)?;
        }
        Ok(current)
    }

    /// Open the directory `name` inside `dir`, creating it (`mkdirat`, mode
    /// 0777 under the umask like `create_dir`) when absent.
    pub(crate) fn ensure_dir(dir: &File, name: &str) -> Result<File, FdError> {
        match open_dir(dir, name) {
            Err(FdError::Missing) => {
                match mkdirat(dir, name, Mode::from_raw_mode(0o777)) {
                    Ok(()) | Err(Errno::EXIST) => {}
                    Err(e) => return Err(map(e)),
                }
                open_dir(dir, name)
            }
            other => other,
        }
    }

    /// [`walk`] that creates each missing level.
    pub(crate) fn ensure_walk(dir: &File, parts: &[&str]) -> Result<File, FdError> {
        let mut current = dir.try_clone().map_err(FdError::Io)?;
        for part in parts {
            current = ensure_dir(&current, part)?;
        }
        Ok(current)
    }

    /// What `name` is inside `dir`, without following it.
    pub(crate) fn kind_at(dir: &File, name: &str) -> Result<Kind, FdError> {
        match statat(dir, name, AtFlags::SYMLINK_NOFOLLOW) {
            Ok(stat) => Ok(match FileType::from_raw_mode(stat.st_mode) {
                FileType::Directory => Kind::Dir,
                FileType::RegularFile => Kind::File,
                FileType::Symlink => Kind::Symlink,
                _ => Kind::Other,
            }),
            Err(Errno::NOENT) => Ok(Kind::Missing),
            Err(e) => Err(map(e)),
        }
    }

    /// `(device, inode)` of `name` inside `dir`, `None` when absent.
    #[allow(clippy::unnecessary_cast)] // the raw widths differ per platform
    pub(crate) fn ident_at(dir: &File, name: &str) -> Result<Option<(u64, u64)>, FdError> {
        match statat(dir, name, AtFlags::SYMLINK_NOFOLLOW) {
            Ok(stat) => Ok(Some((stat.st_dev as u64, stat.st_ino as u64))),
            Err(Errno::NOENT) => Ok(None),
            Err(e) => Err(map(e)),
        }
    }

    /// Create `name` in `dir`, which must not exist: `O_CREAT | O_EXCL |
    /// O_NOFOLLOW`, mode 0666 under the umask like `File::create_new`.
    pub(crate) fn create_new(dir: &File, name: &str) -> Result<File, FdError> {
        openat(
            dir,
            name,
            OFlags::WRONLY | OFlags::CREATE | OFlags::EXCL | OFlags::NOFOLLOW | OFlags::CLOEXEC,
            Mode::from_raw_mode(0o666),
        )
        .map(File::from)
        .map_err(map)
    }

    /// Open `name` in `dir` for an in-place overwrite (create when absent,
    /// truncate when present), never following a symlink and never touching a
    /// non-regular file: the handle is checked with `fstat` BEFORE the
    /// truncate.
    pub(crate) fn open_overwrite(dir: &File, name: &str) -> Result<File, FdError> {
        let fd = openat(
            dir,
            name,
            OFlags::WRONLY | OFlags::CREATE | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::CLOEXEC,
            Mode::from_raw_mode(0o666),
        )
        .map_err(map)?;
        let file = File::from(fd);
        if !file.metadata().map_err(FdError::Io)?.is_file() {
            return Err(FdError::Changed);
        }
        file.set_len(0).map_err(FdError::Io)?;
        Ok(file)
    }

    /// Open the regular file `name` in `dir` for reading (no symlink, no
    /// blocking on a FIFO) and read at most `cap + 1` bytes. Returns the
    /// handle's identity and the bytes; a non-regular file is
    /// [`FdError::Changed`].
    pub(crate) fn read_file(
        dir: &File,
        name: &str,
        cap: u64,
    ) -> Result<((u64, u64), Vec<u8>), FdError> {
        let fd = openat(
            dir,
            name,
            OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::CLOEXEC,
            Mode::empty(),
        )
        .map_err(map)?;
        let mut file = File::from(fd);
        let meta = file.metadata().map_err(FdError::Io)?;
        if !meta.is_file() {
            return Err(FdError::Changed);
        }
        let mut bytes = Vec::with_capacity(meta.len().min(cap) as usize);
        (&mut file)
            .take(cap + 1)
            .read_to_end(&mut bytes)
            .map_err(FdError::Io)?;
        Ok(((meta.dev(), meta.ino()), bytes))
    }

    /// `renameat` between directory handles (replaces an existing file).
    pub(crate) fn rename_at(
        from_dir: &File,
        from: &str,
        to_dir: &File,
        to: &str,
    ) -> Result<(), FdError> {
        renameat(from_dir, from, to_dir, to).map_err(map)
    }

    /// `unlinkat` of a non-directory (a symlink is removed, never its target).
    pub(crate) fn unlink_at(dir: &File, name: &str) -> Result<(), FdError> {
        unlinkat(dir, name, AtFlags::empty()).map_err(map)
    }

    /// `unlinkat(AT_REMOVEDIR)`: removes an EMPTY directory only
    /// ([`FdError::NotEmpty`] otherwise).
    pub(crate) fn rmdir_at(dir: &File, name: &str) -> Result<(), FdError> {
        unlinkat(dir, name, AtFlags::REMOVEDIR).map_err(map)
    }
}

/// `symlink_metadata` of `repo/rel`, as a finding on failure.
fn tree_meta(repo: &Path, rel: &str, hint: Option<&str>) -> Result<std::fs::Metadata, CoreError> {
    match std::fs::symlink_metadata(repo.join(rel)) {
        Ok(meta) if meta.file_type().is_symlink() => Err(CoreError::deploy_tree_invalid(
            rel,
            None,
            None,
            "is a symlink; symlinks are refused",
        )),
        Ok(meta) => Ok(meta),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            let message = match hint {
                Some(hint) => format!("does not exist ({hint})"),
                None => "does not exist".to_string(),
            };
            Err(CoreError::deploy_tree_invalid(rel, None, None, message))
        }
        Err(e) => Err(CoreError::deploy_tree_invalid(
            rel,
            None,
            None,
            format!("cannot inspect ({})", e.kind()),
        )),
    }
}

/// Require `repo/rel` to be a real directory (not a symlink, not a file).
pub(crate) fn require_repo_dir(repo: &Path, rel: &str) -> Result<(), CoreError> {
    let meta = tree_meta(repo, rel, None)?;
    if meta.file_type().is_dir() {
        Ok(())
    } else {
        Err(CoreError::deploy_tree_invalid(
            rel,
            None,
            None,
            "must be a directory",
        ))
    }
}

/// Read `repo/rel` as a regular file; a symlink or non-file is refused.
/// Every parent directory is checked, so a symlinked parent at any depth
/// cannot redirect the read. The file is then opened first and the handle
/// validated against the inspected metadata, and at most
/// [`READ_MAX_FILE_BYTES`] are read.
///
/// The open is descriptor-relative on unix (see [`open_regular_file`]): a
/// parent directory swapped for a symlink after the parent check is refused
/// as "changed while it was read" rather than followed. On windows that
/// intermediate-directory race remains (std has no descriptor-relative open);
/// the repo is the operator's own checkout.
pub(crate) fn read_repo_file(
    repo: &Path,
    rel: &str,
    hint: Option<&str>,
) -> Result<Vec<u8>, CoreError> {
    if let Some((parent, _)) = rel.rsplit_once('/') {
        let mut prefix = String::new();
        for part in parent.split('/') {
            if !prefix.is_empty() {
                prefix.push('/');
            }
            prefix.push_str(part);
            match std::fs::symlink_metadata(repo.join(&prefix)) {
                // A missing component is reported by the file check below.
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => break,
                _ => require_repo_dir(repo, &prefix)?,
            }
        }
    }
    let meta = tree_meta(repo, rel, hint)?;
    if !meta.file_type().is_file() {
        return Err(CoreError::deploy_tree_invalid(
            rel,
            None,
            None,
            "is not a regular file",
        ));
    }
    let (file, held) = open_regular_file(repo, Path::new(rel), &meta).map_err(|e| match e {
        OpenError::Changed => CoreError::deploy_tree_invalid(
            rel,
            None,
            None,
            "changed while it was read; symlinks are refused",
        ),
        OpenError::Io(e) => {
            CoreError::deploy_tree_invalid(rel, None, None, format!("cannot read ({})", e.kind()))
        }
    })?;
    if held.len() > READ_MAX_FILE_BYTES {
        return Err(CoreError::deploy_tree_invalid(
            rel,
            None,
            None,
            format!("is over the {READ_MAX_FILE_BYTES} byte file cap"),
        ));
    }
    let mut bytes = Vec::new();
    file.take(READ_MAX_FILE_BYTES + 1)
        .read_to_end(&mut bytes)
        .map_err(|e| {
            CoreError::deploy_tree_invalid(rel, None, None, format!("cannot read ({})", e.kind()))
        })?;
    if bytes.len() as u64 > READ_MAX_FILE_BYTES {
        return Err(CoreError::deploy_tree_invalid(
            rel,
            None,
            None,
            format!("grew past the {READ_MAX_FILE_BYTES} byte file cap while it was read"),
        ));
    }
    Ok(bytes)
}

fn io_failure(what: &str, path: &Path, e: &std::io::Error) -> CoreError {
    if e.kind() == std::io::ErrorKind::AlreadyExists {
        return CoreError::InvalidInput {
            reason: format!(
                "--out: {} already exists (another writer won the race); nothing was replaced",
                path.display()
            ),
        };
    }
    CoreError::Internal(format!("cannot {what} {}: {e}", path.display()))
}

/// Write `bytes` to `<path>.ign-render-tmp` (created new) and rename it
/// over `path`.
fn write_replacing(path: &Path, bytes: &[u8]) -> Result<(), CoreError> {
    let mut tmp = path.as_os_str().to_owned();
    tmp.push(TMP_SUFFIX);
    let tmp = PathBuf::from(tmp);
    let mut file = std::fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(&tmp)
        .map_err(|e| io_failure("create", &tmp, &e))?;
    file.write_all(bytes)
        .map_err(|e| io_failure("write", &tmp, &e))?;
    drop(file);
    std::fs::rename(&tmp, path).map_err(|e| io_failure("replace", path, &e))
}

/// Write `tree` and its `marker` under `out`, in two phases.
///
/// Check phase (no writes): `out` must be absent, an empty directory, or a
/// directory that is exactly a previous render of the same gateway and
/// environment (see the README `--out` section); anything else is
/// `invalid_input` naming the reason and the first offending path. Symlinks
/// and special files anywhere under `out` are refused.
///
/// Write phase: new paths are opened with `create_new`, listed paths are
/// replaced through a temporary file and a rename, and `render.json` goes
/// last the same way. Nothing is ever removed.
pub fn write_out(tree: &RenderedTree, out: &Path, marker: &RenderMarker) -> Result<(), CoreError> {
    let plan = check_out(tree, out, marker)?;
    let marker_bytes = marker.to_bytes()?;

    match &plan {
        OutPlan::Create => {
            if let Some(parent) = out.parent().filter(|p| !p.as_os_str().is_empty()) {
                std::fs::create_dir_all(parent).map_err(|e| io_failure("create", parent, &e))?;
            }
            std::fs::create_dir(out).map_err(|e| io_failure("create", out, &e))?;
        }
        OutPlan::Empty | OutPlan::Refresh(_) => {}
    }
    let listed = match &plan {
        OutPlan::Refresh(listed) => Some(listed),
        _ => None,
    };

    for (path, bytes) in &tree.files {
        let rel = path.as_str();
        let dest = rel.split('/').fold(out.to_path_buf(), |acc, c| acc.join(c));
        if let Some(parent) = dest.parent() {
            std::fs::create_dir_all(parent).map_err(|e| io_failure("create", parent, &e))?;
        }
        if listed.is_some_and(|l| l.contains(rel)) {
            write_replacing(&dest, bytes)?;
        } else {
            let mut file = std::fs::OpenOptions::new()
                .write(true)
                .create_new(true)
                .open(&dest)
                .map_err(|e| io_failure("create", &dest, &e))?;
            file.write_all(bytes)
                .map_err(|e| io_failure("write", &dest, &e))?;
        }
    }
    write_replacing(&out.join(RENDER_MARKER), &marker_bytes)
}

struct Reader {
    /// `gateways/<name>`, the repo-relative prefix used in findings.
    prefix: String,
    /// The environment whose `modes/<env>/` overlay is read.
    env: String,
    files: BTreeMap<RelPath, Vec<u8>>,
    findings: Vec<DeployFinding>,
}

impl Reader {
    fn finding(&mut self, rel: &str, message: impl Into<String>) {
        let file = if rel.is_empty() {
            self.prefix.clone()
        } else {
            format!("{}/{rel}", self.prefix)
        };
        self.findings.push(DeployFinding {
            file,
            line: None,
            column: None,
            message: message.into(),
        });
    }

    /// Entries of `dir` sorted by name bytes; read errors become findings.
    fn sorted_entries(&mut self, dir: &Path, rel: &str) -> Vec<(String, std::path::PathBuf)> {
        let read = match std::fs::read_dir(dir) {
            Ok(read) => read,
            Err(e) => {
                self.finding(rel, format!("cannot read directory ({})", e.kind()));
                return Vec::new();
            }
        };
        let mut entries: Vec<(String, std::path::PathBuf)> = Vec::new();
        for entry in read {
            let entry = match entry {
                Ok(entry) => entry,
                Err(e) => {
                    self.finding(rel, format!("cannot read directory entry ({})", e.kind()));
                    continue;
                }
            };
            match entry.file_name().into_string() {
                Ok(name) => entries.push((name, entry.path())),
                Err(raw) => {
                    let lossy = raw.to_string_lossy();
                    let at = join(rel, &lossy);
                    self.finding(&at, "name is not valid UTF-8");
                }
            }
        }
        entries.sort_by(|a, b| a.0.as_bytes().cmp(b.0.as_bytes()));
        entries
    }

    fn read_root(&mut self, dir: &Path) {
        for (name, path) in self.sorted_entries(dir, "") {
            if self.dot_entry(&path, &name, &name) {
                continue;
            }
            match name.as_str() {
                "manifest.yaml" => {
                    self.check_regular(&path, &name);
                }
                "resources" | "tags" => self.walk(&path, &name),
                "modes" => self.read_modes(&path, &name),
                _ => self.finding(
                    &name,
                    "unexpected entry at the gateway root (expected: manifest.yaml, resources/, tags/, modes/)",
                ),
            }
        }
    }

    /// `modes/`: open only the directory named for this render's
    /// environment.
    fn read_modes(&mut self, dir: &Path, rel: &str) {
        if !self.require_dir(dir, rel) {
            return;
        }
        for (name, path) in self.sorted_entries(dir, rel) {
            let child = join(rel, &name);
            if name == ".gitkeep" {
                self.dot_entry(&path, &child, &name);
                continue;
            }
            if name == self.env {
                self.read_env(&path, &child);
                continue;
            }
            match std::fs::symlink_metadata(&path) {
                // another environment's tree: skipped without being opened
                Ok(meta) if meta.file_type().is_dir() => {}
                Ok(_) => self.finding(
                    &child,
                    "unexpected entry under modes/ (expected one directory per environment)",
                ),
                Err(e) => self.finding(&child, format!("cannot inspect ({})", e.kind())),
            }
        }
    }

    /// `modes/<env>/`: `resources/` and `tags/` only.
    fn read_env(&mut self, dir: &Path, rel: &str) {
        if !self.require_dir(dir, rel) {
            return;
        }
        for (name, path) in self.sorted_entries(dir, rel) {
            let child = join(rel, &name);
            if self.dot_entry(&path, &child, &name) {
                continue;
            }
            match name.as_str() {
                "resources" | "tags" => self.walk(&path, &child),
                _ => self.finding(
                    &child,
                    "unexpected entry under modes/<env>/ (expected: resources/, tags/)",
                ),
            }
        }
    }

    /// Whether `dir` is a real directory (not a symlink); findings otherwise.
    fn require_dir(&mut self, dir: &Path, rel: &str) -> bool {
        match std::fs::symlink_metadata(dir) {
            Ok(meta) if meta.file_type().is_symlink() => {
                self.finding(rel, "is a symlink; symlinks are refused");
                false
            }
            Ok(meta) if meta.file_type().is_dir() => true,
            Ok(_) => {
                self.finding(rel, "must be a directory");
                false
            }
            Err(e) => {
                self.finding(rel, format!("cannot inspect ({})", e.kind()));
                false
            }
        }
    }

    /// Reject a non-regular entry; returns whether `path` is a regular file.
    fn check_regular(&mut self, path: &Path, rel: &str) -> bool {
        match std::fs::symlink_metadata(path) {
            Ok(meta) if meta.file_type().is_symlink() => {
                self.finding(rel, "is a symlink; symlinks are refused");
                false
            }
            Ok(meta) if meta.file_type().is_file() => true,
            Ok(_) => {
                self.finding(rel, "is not a regular file or directory");
                false
            }
            Err(e) => {
                self.finding(rel, format!("cannot inspect ({})", e.kind()));
                false
            }
        }
    }

    /// Handle a dot-named entry: a regular file named `.gitkeep` is skipped
    /// at any depth, everything else that starts with `.` is a finding and is
    /// neither read nor walked. Only the first character decides, so
    /// `com.inductiveautomation.perspective` is not a dot-name. Returns
    /// whether `name` was a dot-name (and so is handled).
    fn dot_entry(&mut self, path: &Path, rel: &str, name: &str) -> bool {
        if !name.starts_with('.') {
            return false;
        }
        match std::fs::symlink_metadata(path) {
            Ok(meta) if name == ".gitkeep" && meta.file_type().is_file() => {}
            Ok(meta) if meta.file_type().is_symlink() => {
                self.finding(rel, "is a symlink; symlinks are refused");
            }
            _ => self.finding(
                rel,
                "name starts with `.`; dot-named entries are refused (only a `.gitkeep` file is skipped)",
            ),
        }
        true
    }

    /// Walk directory `dir` (repo-gateway-relative `rel`), collecting files.
    fn walk(&mut self, dir: &Path, rel: &str) {
        if !self.require_dir(dir, rel) {
            return;
        }
        for (name, path) in self.sorted_entries(dir, rel) {
            let child = join(rel, &name);
            if self.dot_entry(&path, &child, &name) {
                continue;
            }
            let meta = match std::fs::symlink_metadata(&path) {
                Ok(meta) => meta,
                Err(e) => {
                    self.finding(&child, format!("cannot inspect ({})", e.kind()));
                    continue;
                }
            };
            let kind = meta.file_type();
            if kind.is_symlink() {
                self.finding(&child, "is a symlink; symlinks are refused");
            } else if kind.is_dir() {
                self.walk(&path, &child);
            } else if kind.is_file() {
                match RelPath::new(&child) {
                    Err(why) => self.finding(&child, format!("unusable path: {why}")),
                    Ok(key) => match std::fs::read(&path) {
                        Ok(bytes) => {
                            self.files.insert(key, bytes);
                        }
                        Err(e) => self.finding(&child, format!("cannot read file ({})", e.kind())),
                    },
                }
            } else {
                self.finding(&child, "is not a regular file or directory");
            }
        }
    }
}

fn join(rel: &str, name: &str) -> String {
    if rel.is_empty() {
        name.to_string()
    } else {
        format!("{rel}/{name}")
    }
}

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

    #[test]
    fn empty_listing_digest_is_sha256_of_nothing() {
        assert_eq!(RenderedTree::default().render_sha256(), EMPTY_RENDER_SHA256);
        assert_eq!(sha256_hex(b""), EMPTY_RENDER_SHA256);
    }

    #[test]
    fn digest_follows_the_documented_listing() {
        let mut tree = RenderedTree::default();
        tree.files
            .insert(RelPath::new("b/x").unwrap(), b"two".to_vec());
        tree.files
            .insert(RelPath::new("a/x").unwrap(), b"one".to_vec());
        let mut listing = Vec::new();
        for (p, c) in [("a/x", &b"one"[..]), ("b/x", &b"two"[..])] {
            listing.extend_from_slice(p.as_bytes());
            listing.push(0);
            listing.extend_from_slice(sha256_hex(c).as_bytes());
            listing.push(b'\n');
        }
        assert_eq!(tree.render_sha256(), sha256_hex(&listing));
        assert_eq!(tree.bytes(), 6);
    }

    #[cfg(unix)]
    #[test]
    fn walk_capped_refuses_a_control_character_in_a_name() {
        // WR-05: a local file name must not reach the plan renderer raw.
        let dir = tempfile::tempdir().expect("tempdir");
        std::fs::write(dir.path().join("ok.json"), b"{}").expect("write");
        std::fs::write(dir.path().join("evil\u{1b}[31m.json"), b"{}").expect("write");
        let (mut files, mut dirs) = (BTreeMap::new(), BTreeSet::new());
        let why = walk_capped(dir.path(), "root", &mut files, &mut dirs, 10)
            .expect_err("a control character is refused");
        assert!(why.contains("control character"), "{why}");
        assert!(
            !why.contains('\u{1b}'),
            "the message is itself sanitised: {why:?}"
        );
    }

    #[test]
    fn walk_capped_stops_at_the_entry_cap() {
        let dir = tempfile::tempdir().expect("tempdir");
        for name in ["a", "b", "c"] {
            std::fs::write(dir.path().join(name), b"x").expect("write");
        }
        let (mut files, mut dirs) = (BTreeMap::new(), BTreeSet::new());
        walk_capped(dir.path(), "root", &mut files, &mut dirs, 3).expect("three fit");
        assert_eq!(files.len(), 3);

        let (mut files, mut dirs) = (BTreeMap::new(), BTreeSet::new());
        let why = walk_capped(dir.path(), "root", &mut files, &mut dirs, 2)
            .expect_err("three entries exceed a cap of two");
        assert!(why.contains("more than 2 entries"), "{why}");
    }

    #[test]
    fn walk_capped_stops_a_flat_directory_before_enumerating_it() {
        // A lazy listing of far more entries than the cap: the collector must
        // stop pulling as soon as the cap is crossed, not drain the iterator.
        let pulled = std::cell::Cell::new(0_usize);
        let listing = (0..1_000_000_usize).map(|i| {
            pulled.set(pulled.get() + 1);
            Ok((
                std::ffi::OsString::from(format!("e{i}")),
                PathBuf::from("x"),
            ))
        });
        let why = collect_capped(listing, "root", 0, 5)
            .expect_err("a flat directory over the cap must fail");
        assert!(why.contains("more than 5 entries (at root/"), "{why}");
        assert!(pulled.get() <= 6, "pulled {} entries", pulled.get());

        // Entries already held by the walk count against the same cap, and
        // the entries that fit stay sorted by name bytes.
        let fits = [("b", "p"), ("a", "p")]
            .into_iter()
            .map(|(n, p)| Ok((std::ffi::OsString::from(n), PathBuf::from(p))));
        let got = collect_capped(fits, "root", 3, 5).expect("two more fit under five");
        assert_eq!(
            got.iter().map(|e| e.0.as_str()).collect::<Vec<_>>(),
            ["a", "b"]
        );
        let fits = [("b", "p"), ("a", "p")]
            .into_iter()
            .map(|(n, p)| Ok((std::ffi::OsString::from(n), PathBuf::from(p))));
        assert!(collect_capped(fits, "root", 4, 5).is_err());
    }

    #[test]
    fn walk_capped_counts_a_flat_directory_on_disk() {
        let dir = tempfile::tempdir().expect("tempdir");
        for i in 0..10 {
            std::fs::write(dir.path().join(format!("f{i}")), b"x").expect("write");
        }
        let (mut files, mut dirs) = (BTreeMap::new(), BTreeSet::new());
        let why = walk_capped(dir.path(), "root", &mut files, &mut dirs, 4)
            .expect_err("ten entries exceed a cap of four");
        assert!(why.contains("more than 4 entries"), "{why}");
    }

    #[cfg(unix)]
    #[test]
    fn open_regular_file_rejects_a_swapped_inode() {
        let dir = tempfile::tempdir().expect("tempdir");
        let outside = tempfile::tempdir().expect("tempdir");
        let inside = dir.path().join("f");
        std::fs::write(&inside, b"inside").expect("write");
        let secret = outside.path().join("secret");
        std::fs::write(&secret, b"outside").expect("write");

        // The check-time metadata is of the real file.
        let before = std::fs::symlink_metadata(&inside).expect("meta");
        let (_, held) =
            open_regular_file(dir.path(), Path::new("f"), &before).expect("unswapped file opens");
        assert_eq!(held.len(), 6);

        // Swap the file for a symlink to an outside file after the check.
        // Rename the original aside rather than delete it: a freed inode
        // number can be handed to the next file created (Linux does), which
        // would give the replacement below the checked file's (dev, ino).
        std::fs::rename(&inside, dir.path().join("original")).expect("rename");
        std::os::unix::fs::symlink(&secret, &inside).expect("symlink");
        assert!(matches!(
            open_regular_file(dir.path(), Path::new("f"), &before),
            Err(OpenError::Changed)
        ));

        // Swap in a different regular file: also a changed inode.
        std::fs::remove_file(&inside).expect("remove");
        std::fs::write(&inside, b"replaced").expect("write");
        assert!(matches!(
            open_regular_file(dir.path(), Path::new("f"), &before),
            Err(OpenError::Changed)
        ));
    }

    #[cfg(unix)]
    #[test]
    fn open_regular_file_does_not_block_on_a_swapped_in_fifo() {
        // WR-04: a regular file replaced by a FIFO after the check must be
        // refused, not waited on until a writer appears.
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("f");
        std::fs::write(&path, b"regular").expect("write");
        let before = std::fs::symlink_metadata(&path).expect("meta");
        std::fs::remove_file(&path).expect("remove");
        let made = std::process::Command::new("mkfifo")
            .arg(&path)
            .status()
            .expect("mkfifo runs");
        assert!(made.success(), "mkfifo");

        let root = dir.path().to_path_buf();
        let (tx, rx) = std::sync::mpsc::channel();
        std::thread::spawn(move || {
            let outcome = open_regular_file(&root, Path::new("f"), &before).map(|_| ());
            let _ = tx.send(outcome);
        });
        let outcome = rx
            .recv_timeout(std::time::Duration::from_secs(5))
            .expect("opening a FIFO must not block");
        assert!(matches!(outcome, Err(OpenError::Changed)), "{outcome:?}");
    }

    #[cfg(unix)]
    #[test]
    fn open_regular_file_does_not_follow_a_swapped_intermediate_directory() {
        let root = tempfile::tempdir().expect("tempdir");
        let outside = tempfile::tempdir().expect("tempdir");
        // `a/b/f` resolves outside when `a/b` is swapped (outside/f) and when
        // `a` is swapped (outside/b/f).
        std::fs::create_dir(outside.path().join("b")).expect("mkdir");
        std::fs::write(outside.path().join("f"), b"outside-secret").expect("write");
        std::fs::write(outside.path().join("b/f"), b"outside-secret").expect("write");
        std::fs::create_dir_all(root.path().join("a/b")).expect("mkdir");
        std::fs::write(root.path().join("a/b/f"), b"inside").expect("write");
        let rel = Path::new("a/b/f");

        let before = std::fs::symlink_metadata(root.path().join(rel)).expect("meta");
        let (mut file, held) = open_regular_file(root.path(), rel, &before).expect("plain open");
        assert_eq!(held.len(), 6);
        let mut text = String::new();
        file.read_to_string(&mut text).expect("read");
        assert_eq!(text, "inside");

        // Swap the deepest directory, then a shallower one, for a symlink to
        // an outside directory holding a same-named regular file. The stale
        // metadata is what a fresh `symlink_metadata(path)` would return
        // after the swap: the outside file, since it follows parents.
        for swapped in ["a/b", "a"] {
            let target = root.path().join(swapped);
            let moved = root.path().join("moved");
            std::fs::rename(&target, &moved).expect("move aside");
            std::os::unix::fs::symlink(outside.path(), &target).expect("symlink");
            let after = std::fs::symlink_metadata(root.path().join(rel)).expect("meta");
            assert!(after.is_file(), "the full path still resolves to a file");
            assert!(
                matches!(
                    open_regular_file(root.path(), rel, &after),
                    Err(OpenError::Changed)
                ),
                "a symlinked `{swapped}` must be refused even with fresh metadata"
            );
            std::fs::remove_file(&target).expect("unlink");
            std::fs::rename(&moved, &target).expect("restore");
        }

        // A symlinked final component and path escapes are refused too.
        std::fs::remove_file(root.path().join(rel)).expect("remove");
        std::os::unix::fs::symlink(outside.path().join("f"), root.path().join(rel))
            .expect("symlink");
        let after = std::fs::symlink_metadata(root.path().join(rel)).expect("meta");
        assert!(matches!(
            open_regular_file(root.path(), rel, &after),
            Err(OpenError::Changed)
        ));
        for escape in ["../x", "/etc/passwd", "a/../f", ""] {
            assert!(
                matches!(
                    open_regular_file(root.path(), Path::new(escape), &before),
                    Err(OpenError::Changed)
                ),
                "{escape:?}"
            );
        }
        // An absent file is an I/O error, not a swap.
        assert!(matches!(
            open_regular_file(root.path(), Path::new("a/b/missing"), &before),
            Err(OpenError::Io(e)) if e.kind() == std::io::ErrorKind::NotFound
        ));
    }

    #[cfg(unix)]
    #[test]
    fn read_repo_file_refuses_a_symlinked_deep_parent_and_caps_bytes() {
        let repo = tempfile::tempdir().expect("tempdir");
        let outside = tempfile::tempdir().expect("tempdir");
        std::fs::write(outside.path().join("m.json"), b"{}").expect("write");
        std::fs::create_dir_all(repo.path().join("a")).expect("mkdir");
        std::os::unix::fs::symlink(outside.path(), repo.path().join("a/b")).expect("symlink");
        let err = read_repo_file(repo.path(), "a/b/m.json", None)
            .expect_err("a symlinked second component must be refused");
        assert!(err.to_string().contains("symlink"), "{err}");

        std::fs::write(repo.path().join("a/ok.json"), b"{}").expect("write");
        assert_eq!(
            read_repo_file(repo.path(), "a/ok.json", None).expect("plain read"),
            b"{}"
        );
    }

    #[test]
    fn from_files_rejects_hostile_paths() {
        assert!(SourceTree::from_files([("../x", "a")]).is_err());
        assert!(SourceTree::from_files([("resources/a/b/c", "a")]).is_ok());
    }
}