iso-parser 0.17.0

Boot-entry discovery from ISO installation media (Arch, Debian/Ubuntu, Fedora/RHEL, Alpine, NixOS, Mint)
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
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
// SPDX-License-Identifier: MIT OR Apache-2.0

// Phase 6 of #286 — README.md becomes the rustdoc landing page
// alongside the Rust-specific module docs below. docs.rs + local
// `cargo doc --open` visitors see the operator-level overview
// first; the Rust-API detail (Safety, Supported Distributions,
// Usage) stays inline.
//
// `clippy::doc_markdown = allow` at module scope because the README
// is prose for a general operator audience — strict auto-backticking
// of distro names / tool names / product names (clippy::doc_markdown
// wants `Arch Linux` → `` `Arch Linux` ``) is noise without signal
// for the README's readers. The module-level `//!` API docs still
// get the full lint benefit below.
#![allow(clippy::doc_markdown)]
#![doc = include_str!("../README.md")]
//!
//! ---
//!
//! # Safety
//!
//! `forbid(unsafe_code)` at the crate level — `iso-parser` ships to crates.io
//! per [#51](https://github.com/aegis-boot/aegis-boot/issues/51) and a
//! library that parses untrusted ISO content has no business calling raw
//! syscalls. The kexec syscall lives in `kexec-loader`, the only crate in the
//! workspace that's exempt from this constraint.
//!
//! # Supported Distributions
//! - **Arch Linux**: `/boot/` contains `vmlinuz` and `initrd.img`
//! - **Debian/Ubuntu**: `/install/` or `/casper/` contains `vmlinuz` and `initrd.gz`
//! - **Fedora**: `/images/pxeboot/` contains `vmlinuz` and `initrd.img`
//!
//! # Usage
//! ```text
//! // Illustrative only — OsIsoEnvironment doesn't exist in this
//! // crate (real callers supply their own IsoEnvironment impl).
//! // `text` fence so this doesn't compile under `cargo test --
//! // --ignored` either.
//! use iso_parser::{IsoParser, OsIsoEnvironment};
//! use std::path::Path;
//!
//! async fn example() {
//!     let parser = IsoParser::new(OsIsoEnvironment::new());
//!     let entries = parser.scan_directory(Path::new("/media/isos")).await?;
//!     for entry in entries {
//!         println!("Found: {} ({:?})", entry.label, entry.distribution);
//!     }
//! }
//! ```

#![forbid(unsafe_code)]

use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use thiserror::Error;
use tracing::{debug, instrument};

#[cfg(test)]
#[path = "detection_tests.rs"]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::too_many_lines,
    clippy::missing_panics_doc
)]
mod detection_tests;

/// Errors that can occur during ISO parsing
#[derive(Debug, Error)]
pub enum IsoError {
    /// Underlying I/O failure — path read, file stat, or directory
    /// listing. Wraps [`std::io::Error`] transparently.
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// Scan completed but no recognized boot entries were found inside
    /// the ISO. The inner string names the ISO path for context.
    #[error("No boot entries found in ISO: {0}")]
    NoBootEntries(String),

    /// `mount` (or the configured `IsoEnvironment`'s `mount_iso`) failed
    /// — inner string is the mounter's stderr or a descriptive message.
    #[error("Mount failed: {0}")]
    MountFailed(String),

    /// Requested path escaped the expected base directory (contains
    /// `..` components or doesn't live under the mount root). Inner
    /// string is the offending path.
    #[error("Path traversal attempt blocked: {0}")]
    PathTraversal(String),
}

/// Result of a directory scan — successful boot entries plus any
/// per-file failures that the caller should surface to the user.
///
/// Returned by [`IsoParser::scan_directory_with_failures`]. Unlike the
/// legacy [`IsoParser::scan_directory`] which silently drops failed
/// ISOs, this shape preserves the full on-disk inventory so a UI
/// (e.g. rescue-tui) can render a descriptive row for each broken
/// ISO instead of hiding it behind a "skipped" count. (#456)
#[derive(Debug, Clone)]
pub struct ScanReport {
    /// ISOs that were mounted, parsed, and yielded at least one boot
    /// entry.
    pub entries: Vec<BootEntry>,
    /// ISOs that were found on disk but could not be processed.
    /// `reason` is human-readable; `kind` is structured for tier
    /// decisions downstream.
    pub failures: Vec<ScanFailure>,
}

/// A single ISO file that failed to yield boot entries during a
/// directory scan.
#[derive(Debug, Clone)]
pub struct ScanFailure {
    /// Absolute path to the `.iso` file that failed.
    pub iso_path: PathBuf,
    /// Human-readable reason, rendered safely in TUIs (no control
    /// characters, source-error `Display` already applied).
    pub reason: String,
    /// Structured classification for downstream tier mapping.
    pub kind: ScanFailureKind,
}

/// Structured classification of why an ISO failed to yield boot
/// entries. A 1-to-1 map from the per-file variants of [`IsoError`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScanFailureKind {
    /// Filesystem error reading the ISO or its mount point.
    IoError,
    /// Loop-mounting the ISO failed (wrong format, permission denied,
    /// no loop device available).
    MountFailed,
    /// Mount succeeded but no recognized boot entries were found on
    /// the ISO's filesystem.
    NoBootEntries,
}

impl ScanFailureKind {
    fn from_iso_error(e: &IsoError) -> Self {
        match e {
            IsoError::MountFailed(_) => Self::MountFailed,
            IsoError::NoBootEntries(_) => Self::NoBootEntries,
            // Io and PathTraversal both map to IoError. PathTraversal
            // is a caller-supplied error that should never surface at
            // this layer (path validation runs before the per-ISO
            // loop); defensively funneled here so a future regression
            // surfaces as a generic IoError rather than a panic.
            IsoError::Io(_) | IsoError::PathTraversal(_) => Self::IoError,
        }
    }
}

/// Maximum length (in bytes) of a [`ScanFailure::reason`] string.
/// Long enough to include the original error's meaningful prefix
/// (mount errors typically fit in ~120 chars) while keeping TUI
/// rendering bounded.
const MAX_REASON_LEN: usize = 256;

/// Produce a TUI-safe version of an error string: control characters
/// replaced with spaces, trimmed, truncated to [`MAX_REASON_LEN`].
/// Non-ASCII is preserved (UTF-8 safe).
fn sanitize_reason(raw: &str) -> String {
    let cleaned: String = raw
        .chars()
        .map(|c| {
            // Allow printable + space; replace any other control char
            // with a single space so the TUI's line-layout math doesn't
            // break. Tab is also dropped (would shift columns).
            if c.is_control() { ' ' } else { c }
        })
        .collect();
    let trimmed = cleaned.trim();
    if trimmed.len() <= MAX_REASON_LEN {
        return trimmed.to_string();
    }
    // Truncate on a char boundary so we never split a multibyte char.
    let mut end = MAX_REASON_LEN;
    while !trimmed.is_char_boundary(end) {
        end -= 1;
    }
    format!("{}", &trimmed[..end])
}

/// Represents a discovered boot entry from an ISO
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct BootEntry {
    /// Label for the boot menu (e.g., "Arch Linux `x86_64`")
    pub label: String,
    /// Path to kernel (relative to ISO mount point)
    pub kernel: PathBuf,
    /// Path to initrd (relative to ISO mount point)
    pub initrd: Option<PathBuf>,
    /// Kernel command line parameters
    pub kernel_args: Option<String>,
    /// Distribution identifier
    pub distribution: Distribution,
    /// ISO filename (for reference)
    pub source_iso: String,
    /// Full distro name with version, extracted from `/etc/os-release`
    /// (`PRETTY_NAME`) or fallback files on the mounted ISO. Populated
    /// by `scan_directory`; `None` when none of the probe paths exist
    /// (older installers or unfamiliar layouts). Surfaced as the
    /// primary label in downstream UI when present so operators see
    /// "Ubuntu 24.04.2 LTS (Noble Numbat)" instead of just "Ubuntu".
    /// (#119)
    #[serde(default)]
    pub pretty_name: Option<String>,
}

/// Supported distribution families.
///
/// Ordering of detection matters: more specific matches (Alpine's
/// `boot/vmlinuz-lts`, `NixOS`'s `boot/bzImage`, RHEL-family's `images/pxeboot`)
/// must run before the broader ones (Arch's generic `boot/` heuristic).
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum Distribution {
    /// Arch Linux install media (`arch/boot/x86_64/vmlinuz-linux`).
    Arch,
    /// Debian and Ubuntu live/install media (`casper/`, `install.amd/`, `live/`).
    Debian,
    /// Fedora install media (`images/pxeboot/`).
    Fedora,
    /// RHEL / Rocky / `AlmaLinux` — same `images/pxeboot` layout as Fedora
    /// but a distinct signing CA and stricter lockdown kexec policy.
    RedHat,
    /// Alpine Linux (`boot/vmlinuz-lts`).
    Alpine,
    /// `NixOS` install media (`boot/bzImage`).
    NixOS,
    /// Windows installer media. Recognized by `bootmgr`, `sources/boot.wim`,
    /// or `efi/microsoft/boot/`. **Not kexec-bootable**: Windows uses a
    /// fundamentally different boot protocol (NT loader, not Linux kernel).
    /// Surfaced so the TUI can give a specific diagnostic rather than fail
    /// silently.
    Windows,
    /// Layout not recognized.
    Unknown,
}

impl Distribution {
    /// Detect distribution from a kernel path observed inside an ISO.
    #[must_use]
    pub fn from_paths(kernel_path: &std::path::Path) -> Self {
        let path_str = kernel_path.to_string_lossy().to_lowercase();

        // Specific signals first — RHEL/Rocky/Alma carry distinctive markers in
        // their ISO volume labels and filenames, but at this path-only layer
        // we can't disambiguate from Fedora. Keep them separate variants; the
        // caller can upgrade detection once volume-label sniffing is added.
        if path_str.contains("bootmgr")
            || path_str.contains("sources/boot.wim")
            || path_str.contains("efi/microsoft")
            || path_str.contains("windows")
        {
            Distribution::Windows
        } else if path_str.contains("nixos") || path_str.ends_with("bzimage") {
            Distribution::NixOS
        } else if path_str.contains("alpine")
            // Alpine's kernel filename suffix is the authoritative
            // signal — `vmlinuz-lts` (Standard) and `vmlinuz-virt`
            // (Virt edition). Kept case-insensitive; path_str is
            // already lowercased. (#116)
            || path_str.contains("vmlinuz-lts")
            || path_str.contains("vmlinuz-virt")
            || path_str.contains("initramfs-lts")
            || path_str.contains("initramfs-virt")
        {
            Distribution::Alpine
        } else if path_str.contains("rhel")
            || path_str.contains("rocky")
            || path_str.contains("almalinux")
            || path_str.contains("centos")
        {
            Distribution::RedHat
        } else if path_str.contains("fedora")
            || path_str.contains("images")
            || path_str.contains("pxeboot")
        {
            Distribution::Fedora
        } else if path_str.contains("debian")
            || path_str.contains("ubuntu")
            || path_str.contains("casper")
        {
            Distribution::Debian
        } else if path_str.contains("arch")
            || (path_str.contains("boot")
                && !path_str.contains("efi")
                && !path_str.contains("images"))
        {
            Distribution::Arch
        } else {
            Distribution::Unknown
        }
    }
}

/// Environment abstraction for file system and OS operations
///
/// This trait enables unit testing without actual mounts by providing
/// a mockable interface for filesystem access and process execution.
pub trait IsoEnvironment: Send + Sync {
    /// List files in a directory.
    ///
    /// # Errors
    ///
    /// Returns [`std::io::Error`] on any read failure (missing path,
    /// permission denied, I/O error mid-read).
    fn list_dir(&self, path: &std::path::Path) -> std::io::Result<Vec<std::path::PathBuf>>;

    /// Check if a file exists.
    fn exists(&self, path: &std::path::Path) -> bool;

    /// Read file metadata.
    ///
    /// # Errors
    ///
    /// Returns [`std::io::Error`] when the path can't be stat'd
    /// (missing, permission denied, I/O error).
    fn metadata(&self, path: &std::path::Path) -> std::io::Result<std::fs::Metadata>;

    /// Mount an ISO file and return the mount point.
    ///
    /// # Errors
    ///
    /// Returns [`IsoError::MountFailed`] if the underlying mount
    /// command (or mock handler) returned non-zero, or
    /// [`IsoError::Io`] if a required helper (mkdir, losetup, mount)
    /// couldn't be spawned.
    fn mount_iso(&self, iso_path: &std::path::Path) -> Result<PathBuf, IsoError>;

    /// Unmount a previously mounted ISO.
    ///
    /// # Errors
    ///
    /// Returns [`IsoError::MountFailed`] if `umount` returned non-zero
    /// (busy mount, stale mount point), or [`IsoError::Io`] if the
    /// unmount helper couldn't be spawned.
    fn unmount(&self, mount_point: &std::path::Path) -> Result<(), IsoError>;

    /// Validate that `path` is rooted under `base` and contains no
    /// parent-directory escapes.
    ///
    /// Returns [`IsoError::PathTraversal`] when:
    ///   * any path component is `..` (could escape on normalization), OR
    ///   * `path` does not lie under `base` (absolute paths to elsewhere).
    ///
    /// Symlinks are NOT resolved — callers that mount untrusted media must
    /// constrain symlink-following at the mount layer (e.g. `nosymfollow`),
    /// not rely on this check.
    ///
    /// Previous implementation silently returned `Ok(path)` when
    /// `strip_prefix(base)` failed, meaning paths outside `base` were
    /// accepted. Fixed in #56.
    ///
    /// # Errors
    ///
    /// Returns [`IsoError::PathTraversal`] on either of the two
    /// traversal conditions above.
    fn validate_path(
        &self,
        base: &std::path::Path,
        path: &std::path::Path,
    ) -> Result<PathBuf, IsoError> {
        if path
            .components()
            .any(|c| matches!(c, std::path::Component::ParentDir))
        {
            return Err(IsoError::PathTraversal(path.display().to_string()));
        }
        if !path.starts_with(base) {
            return Err(IsoError::PathTraversal(path.display().to_string()));
        }
        Ok(path.to_path_buf())
    }
}

/// OS-specific implementation of `IsoEnvironment`
pub struct OsIsoEnvironment {
    mount_base: PathBuf,
}

impl OsIsoEnvironment {
    /// Construct a default `OsIsoEnvironment` with mount points under
    /// `/tmp/iso-parser-mounts`. Callers that need a different base
    /// path should construct the struct directly.
    #[must_use]
    pub fn new() -> Self {
        Self {
            mount_base: PathBuf::from("/tmp/iso-parser-mounts"),
        }
    }

    /// Find a free loop device and attach `iso_path` to it. Tries
    /// util-linux semantics (`losetup -f --show -r`) first, then falls
    /// back to busybox semantics (scan `/dev/loop*` manually and attach
    /// via `losetup <dev> <iso>`). Returns the allocated device path on
    /// success.
    fn allocate_loop_device(iso_path: &std::path::Path) -> Option<String> {
        use std::process::Command;

        // Attempt A: util-linux `-f --show -r`.
        match Command::new("losetup")
            .args(["-f", "--show", "-r", &iso_path.to_string_lossy()])
            .output()
        {
            Ok(out) if out.status.success() => {
                let dev = String::from_utf8_lossy(&out.stdout).trim().to_string();
                if !dev.is_empty() && dev.starts_with("/dev/") {
                    return Some(dev);
                }
                // Success exit but stdout didn't name a loop device —
                // surface so operators see why "no ISOs found" when
                // losetup is present. (#138)
                tracing::warn!(
                    iso = %iso_path.display(),
                    stdout = %String::from_utf8_lossy(&out.stdout),
                    "iso-parser: util-linux losetup succeeded but returned no /dev/loop* device"
                );
            }
            Ok(out) => {
                tracing::warn!(
                    iso = %iso_path.display(),
                    exit = ?out.status.code(),
                    stderr = %String::from_utf8_lossy(&out.stderr),
                    "iso-parser: util-linux losetup -f --show failed; falling back to busybox scan"
                );
            }
            Err(e) => {
                tracing::warn!(
                    iso = %iso_path.display(),
                    error = %e,
                    "iso-parser: losetup exec failed (not on PATH?); falling back to busybox scan"
                );
            }
        }

        // Attempt B: busybox fallback. Find a free loop device manually
        // (one that's not currently bound — busybox `losetup LOOPDEV`
        // without args prints its binding or errors).
        for n in 0..16 {
            let dev = format!("/dev/loop{n}");
            if !std::path::Path::new(&dev).exists() {
                continue;
            }
            // Query — if it returns non-zero, device is free.
            let query = match Command::new("losetup").arg(&dev).output() {
                Ok(q) => q,
                Err(e) => {
                    tracing::warn!(
                        dev = %dev,
                        error = %e,
                        "iso-parser: losetup query exec failed; skipping device"
                    );
                    continue;
                }
            };
            if query.status.success() {
                continue; // already bound
            }
            // Try to attach.
            match Command::new("losetup")
                .args(["-r", &dev, &iso_path.to_string_lossy()])
                .output()
            {
                Ok(attach) if attach.status.success() => return Some(dev),
                Ok(attach) => {
                    tracing::warn!(
                        dev = %dev,
                        iso = %iso_path.display(),
                        exit = ?attach.status.code(),
                        stderr = %String::from_utf8_lossy(&attach.stderr),
                        "iso-parser: losetup attach failed; trying next device"
                    );
                }
                Err(e) => {
                    tracing::warn!(
                        dev = %dev,
                        iso = %iso_path.display(),
                        error = %e,
                        "iso-parser: losetup attach exec failed; giving up"
                    );
                    return None;
                }
            }
        }
        tracing::warn!(
            iso = %iso_path.display(),
            "iso-parser: exhausted /dev/loop0..15 without a free device; cannot mount ISO"
        );
        None
    }
}

impl Default for OsIsoEnvironment {
    fn default() -> Self {
        Self::new()
    }
}

impl IsoEnvironment for OsIsoEnvironment {
    fn list_dir(&self, path: &std::path::Path) -> std::io::Result<Vec<PathBuf>> {
        let mut entries = std::fs::read_dir(path)?
            .map(|e| e.map(|entry| entry.path()))
            .collect::<Result<Vec<_>, _>>()?;
        entries.sort();
        Ok(entries)
    }

    fn exists(&self, path: &std::path::Path) -> bool {
        path.exists()
    }

    fn metadata(&self, path: &std::path::Path) -> std::io::Result<std::fs::Metadata> {
        std::fs::metadata(path)
    }

    fn mount_iso(&self, iso_path: &std::path::Path) -> Result<PathBuf, IsoError> {
        use std::process::Command;

        // Generate unique mount point
        let iso_name = iso_path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("iso");

        let mount_point = self.mount_base.join(format!("mount_{iso_name}"));
        std::fs::create_dir_all(&mount_point)?;

        // Attempt 1: `mount -o loop,ro`. Works with util-linux; may not
        // work with some busybox builds where the `loop` option is a
        // no-op (it mounts the file as if it were a raw block device,
        // which then fails). Try it first because it's one syscall on
        // util-linux-based systems.
        let output = Command::new("mount")
            .args([
                "-o",
                "loop,ro",
                // Windows install ISOs are UDF-primary with a tiny
                // iso9660 fallback volume that contains only a readme.txt
                // shim. Mount tries types left-to-right — UDF first so
                // we get the real filesystem on Windows ISOs, then iso9660
                // as the fallback for pure-iso9660 media (Alpine, Ubuntu,
                // Fedora install/live ISOs).
                "-t",
                "udf,iso9660",
                &iso_path.to_string_lossy(),
                &mount_point.to_string_lossy(),
            ])
            .output();

        // If that fails AND we have `losetup` available, fall through to
        // the explicit loop-setup path below. Verify by checking if the
        // mount point now contains anything (mount silently succeeds with
        // nothing mounted under certain busybox builds — test by listing).
        let loop_attempt_ok = match &output {
            Ok(out) if out.status.success() => {
                // Verify the mount actually took by checking for directory
                // entries. An empty dir after a "successful" mount means
                // busybox loop-mode didn't work.
                std::fs::read_dir(&mount_point)
                    .ok()
                    .and_then(|mut entries| entries.next())
                    .is_some()
            }
            _ => false,
        };

        if !loop_attempt_ok {
            // Attempt 2: explicit losetup + mount. Handles both
            // util-linux (`losetup -f --show`) and busybox (`losetup -f`
            // prints the allocated device on stdout as a side effect;
            // `--show` is a util-linux long option that busybox doesn't
            // accept). Try util-linux form first; fall back to querying
            // /dev/loop* after a bare `losetup -f` attach.
            let loop_dev = Self::allocate_loop_device(iso_path);
            if let Some(loop_dev) = loop_dev {
                let mount_out = Command::new("mount")
                    .args([
                        "-r",
                        "-t",
                        "udf,iso9660",
                        &loop_dev,
                        &mount_point.to_string_lossy(),
                    ])
                    .output();
                if let Ok(mo) = mount_out
                    && mo.status.success()
                {
                    debug!(
                        "Mounted {} via losetup {} -> {:?}",
                        iso_path.display(),
                        loop_dev,
                        mount_point
                    );
                    return Ok(mount_point);
                }
                // losetup succeeded but mount failed — detach.
                let _ = Command::new("losetup").args(["-d", &loop_dev]).output();
            }
        }

        // Terminal dispatch. Attempt 1 may have reported status=success
        // but left the mount_point empty (busybox loop-mode silently
        // no-ops, or the filesystem type list didn't match the ISO's
        // actual layout). In that case we previously returned
        // Ok(empty mount_point) — callers then saw NoBootEntries
        // instead of the real "mount didn't take" diagnostic. Re-verify
        // the mount point has entries before accepting status.success.
        let mount_point_populated = || {
            std::fs::read_dir(&mount_point)
                .ok()
                .and_then(|mut entries| entries.next())
                .is_some()
        };
        match output {
            Ok(out) if out.status.success() && mount_point_populated() => {
                debug!("Mounted {} to {:?}", iso_path.display(), mount_point);
                Ok(mount_point)
            }
            Ok(out) => {
                let stderr = String::from_utf8_lossy(&out.stderr);
                // Explicit hint when mount claimed success but wrote
                // nothing: typically a filesystem-type mismatch
                // (Windows/macOS ISOs against older mount defaults).
                let reason = if out.status.success() {
                    format!(
                        "mount claimed success but {} is empty — \
                         filesystem type likely not auto-detected \
                         (stderr: {})",
                        mount_point.display(),
                        stderr.trim()
                    )
                } else {
                    stderr.to_string()
                };
                // Try fallback with fuseiso
                let fuse_output = Command::new("fuseiso")
                    .arg(iso_path.to_string_lossy().as_ref())
                    .arg(mount_point.to_string_lossy().as_ref())
                    .output();

                match fuse_output {
                    Ok(fuse_out) if fuse_out.status.success() && mount_point_populated() => {
                        debug!("Mounted {} via fuseiso", iso_path.display());
                        Ok(mount_point)
                    }
                    _ => {
                        // Cleanup mount point on failure.
                        let _ = std::fs::remove_dir(&mount_point);
                        Err(IsoError::MountFailed(reason))
                    }
                }
            }
            Err(e) => Err(IsoError::Io(e)),
        }
    }

    fn unmount(&self, mount_point: &std::path::Path) -> Result<(), IsoError> {
        use std::process::Command;

        // Try umount first, then fusermount
        let umount_result = Command::new("umount").arg(mount_point).output();

        match umount_result {
            Ok(out) if out.status.success() => {
                let _ = std::fs::remove_dir(mount_point);
                Ok(())
            }
            _ => {
                // Try fusermount as fallback
                let fusermount = Command::new("fusermount")
                    .arg("-u")
                    .arg(mount_point)
                    .output();
                match fusermount {
                    Ok(out) if out.status.success() => {
                        let _ = std::fs::remove_dir(mount_point);
                        Ok(())
                    }
                    _ => Err(IsoError::MountFailed(format!(
                        "Failed to unmount {}",
                        mount_point.display()
                    ))),
                }
            }
        }
    }
}

/// ISO Parser - main entry point for boot discovery
///
/// Generic over environment to allow testing without actual filesystem/mounts.
pub struct IsoParser<E: IsoEnvironment> {
    env: E,
}

impl<E: IsoEnvironment> IsoParser<E> {
    /// Construct a parser bound to the given [`IsoEnvironment`].
    /// Typically [`OsIsoEnvironment`] in production; a mock in tests.
    pub fn new(env: E) -> Self {
        Self { env }
    }

    /// Scan a directory for ISO files and extract boot entries.
    ///
    /// The `async` signature is retained for backwards source-compat
    /// with callers that `.await` it; the function itself performs no
    /// async work today.
    ///
    /// This is the legacy entry point — it discards per-ISO failures.
    /// Prefer [`IsoParser::scan_directory_with_failures`] for new
    /// callers that need to surface broken ISOs to the user (#456).
    ///
    /// # Errors
    ///
    /// Returns [`IsoError::PathTraversal`] if `path` escapes
    /// `/` (degenerate), [`IsoError::Io`] on a filesystem read failure
    /// during the ISO-file discovery walk, or [`IsoError::NoBootEntries`]
    /// when every discovered ISO failed to yield entries (legacy
    /// behavior — preserved for callers that still rely on it).
    #[instrument(skip(self))]
    pub async fn scan_directory(&self, path: &std::path::Path) -> Result<Vec<BootEntry>, IsoError> {
        let report = self.scan_directory_with_failures(path).await?;
        if report.entries.is_empty() {
            // Preserve the legacy contract: "no usable entries" is a
            // NoBootEntries error even when .iso files were found but
            // all failed to parse.
            return Err(IsoError::NoBootEntries(path.to_string_lossy().to_string()));
        }
        Ok(report.entries)
    }

    /// Scan a directory for `.iso` files, mount + parse each one, and
    /// return a [`ScanReport`] with both successful entries and
    /// per-file failures.
    ///
    /// Unlike [`IsoParser::scan_directory`], this does NOT return
    /// [`IsoError::NoBootEntries`] when every on-disk ISO failed to
    /// parse — instead it returns `Ok(ScanReport { entries: [],
    /// failures: […] })`. `NoBootEntries` is reserved for the stricter
    /// case "the walk found zero `.iso` files", which lets the caller
    /// distinguish an empty stick from a stick full of broken ISOs.
    /// (#456)
    ///
    /// # Errors
    ///
    /// Returns [`IsoError::PathTraversal`] if `path` escapes `/`,
    /// [`IsoError::Io`] on a filesystem read failure during the walk,
    /// or [`IsoError::NoBootEntries`] when zero `.iso` files were
    /// found under `path`.
    #[instrument(skip(self))]
    #[allow(clippy::unused_async)]
    pub async fn scan_directory_with_failures(
        &self,
        path: &std::path::Path,
    ) -> Result<ScanReport, IsoError> {
        let validated_path = self.env.validate_path(std::path::Path::new("/"), path)?;

        debug!("Scanning directory: {:?}", validated_path);

        let iso_files = self.find_iso_files(&validated_path)?;
        let attempted = iso_files.len();

        if attempted == 0 {
            // Walk found zero `.iso` files — this is the only case we
            // treat as "no ISOs". A directory that had files but they
            // all failed to parse returns Ok with populated failures.
            return Err(IsoError::NoBootEntries(
                validated_path.to_string_lossy().to_string(),
            ));
        }

        let mut entries = Vec::new();
        let mut failures = Vec::new();

        for iso_path in iso_files {
            debug!("Processing ISO: {:?}", iso_path);

            match self.process_iso(&iso_path).await {
                Ok(mut iso_entries) => entries.append(&mut iso_entries),
                Err(e) => {
                    // Warn-level so silent-skip failures surface on the
                    // serial console without operators needing debug
                    // tracing. (#68) The failure is ALSO captured in
                    // the ScanReport so TUIs can render a descriptive
                    // row. (#456)
                    tracing::warn!(
                        iso = %iso_path.display(),
                        error = %e,
                        "iso-parser: skipped ISO (mount/parse failed)"
                    );
                    failures.push(ScanFailure {
                        iso_path: iso_path.clone(),
                        reason: sanitize_reason(&e.to_string()),
                        kind: ScanFailureKind::from_iso_error(&e),
                    });
                }
            }
        }

        tracing::info!(
            root = %validated_path.display(),
            found_isos = attempted,
            extracted_entries = entries.len(),
            skipped_isos = failures.len(),
            "iso-parser: scan summary"
        );

        Ok(ScanReport { entries, failures })
    }

    /// Find all ISO files in a directory recursively
    fn find_iso_files(&self, path: &std::path::Path) -> Result<Vec<PathBuf>, IsoError> {
        let mut isos = Vec::new();

        for entry in self.env.list_dir(path)? {
            let entry_path = &entry;

            // Recurse into subdirectories
            if entry.is_dir() {
                // Skip certain directories
                let name = entry.file_name().and_then(|n| n.to_str()).unwrap_or("");

                if !name.starts_with('.')
                    && name != "proc"
                    && name != "sys"
                    && name != "dev"
                    && let Ok(mut sub_isos) = self.find_iso_files(entry_path)
                {
                    isos.append(&mut sub_isos);
                }
            } else if let Some(ext) = entry.extension().and_then(|s| s.to_str())
                && ext.eq_ignore_ascii_case("iso")
            {
                isos.push(entry.clone());
            }
        }

        Ok(isos)
    }

    /// Process a single ISO: mount, extract boot entries, unmount
    async fn process_iso(&self, iso_path: &Path) -> Result<Vec<BootEntry>, IsoError> {
        let mount_point = self.env.mount_iso(iso_path)?;

        let result = self.extract_boot_entries(&mount_point, iso_path).await;

        // Always attempt unmount
        let _ = self.env.unmount(&mount_point);

        result
    }

    /// Extract boot entries from a mounted ISO.
    #[allow(clippy::unused_async)]
    async fn extract_boot_entries(
        &self,
        mount_point: &Path,
        source_iso: &Path,
    ) -> Result<Vec<BootEntry>, IsoError> {
        let mut entries = Vec::new();

        // Try each distribution pattern
        entries.extend(self.try_arch_layout(mount_point, source_iso)?);
        entries.extend(self.try_debian_layout(mount_point, source_iso)?);
        entries.extend(self.try_fedora_layout(mount_point, source_iso)?);
        entries.extend(self.try_windows_layout(mount_point, source_iso)?);

        // Populate pretty_name from the mounted ISO's release files
        // before the caller unmounts. Best-effort — if none of the
        // known paths resolve, the field stays None and downstream UI
        // falls back to the distribution-family label. (#119)
        let pretty = read_pretty_name(mount_point);
        if pretty.is_some() {
            for entry in &mut entries {
                entry.pretty_name.clone_from(&pretty);
            }
        }

        Ok(entries)
    }

    /// Try Arch Linux layout: /boot/{vmlinuz,initrd.img}
    fn try_arch_layout(
        &self,
        mount_point: &Path,
        source_iso: &Path,
    ) -> Result<Vec<BootEntry>, IsoError> {
        let boot_dir = mount_point.join("boot");

        if !self.env.exists(&boot_dir) {
            return Ok(Vec::new());
        }

        let mut entries = Vec::new();

        // Find kernel files (vmlinuz*)
        for entry in self.env.list_dir(&boot_dir)? {
            let name = entry.file_name().and_then(|n| n.to_str()).unwrap_or("");

            if name.starts_with("vmlinuz") {
                let kernel = entry.clone();
                let mut initrd = boot_dir.join(format!(
                    "initrd.img{}",
                    name.strip_prefix("vmlinuz").unwrap_or("")
                ));

                // Try common initrd names
                if !self.env.exists(&initrd) {
                    initrd = boot_dir.join("initrd.img");
                }
                if !self.env.exists(&initrd) {
                    initrd = boot_dir.join(format!(
                        "initrd{}",
                        name.strip_prefix("vmlinuz").unwrap_or("")
                    ));
                }

                let has_initrd = self.env.exists(&initrd);

                // Classify from the actual kernel filename — `boot/vmlinuz-lts`
                // and `boot/vmlinuz-virt` are Alpine, not Arch, etc. This
                // layout matches multiple distros that share the
                // `/boot/vmlinuz*` convention; use the path classifier
                // rather than a hardcoded `Distribution::Arch`. (#116)
                let rel_kernel = kernel
                    .strip_prefix(mount_point)
                    .map(std::path::Path::to_path_buf)
                    .map_err(|_| {
                        IsoError::Io(std::io::Error::new(
                            std::io::ErrorKind::InvalidData,
                            "Kernel path escape",
                        ))
                    })?;
                let distribution = Distribution::from_paths(&rel_kernel);
                let label = match distribution {
                    Distribution::Alpine => format!(
                        "Alpine {}",
                        name.strip_prefix("vmlinuz-").unwrap_or("").trim()
                    ),
                    Distribution::Arch => format!(
                        "Arch Linux {}",
                        name.strip_prefix("vmlinuz").unwrap_or("").trim()
                    ),
                    _ => format!(
                        "Linux {}",
                        name.strip_prefix("vmlinuz").unwrap_or("").trim()
                    ),
                };
                // Kernel args: only set for actual Arch; leave empty for
                // Alpine/unknown so the ISO's own boot config wins.
                let kernel_args = if distribution == Distribution::Arch {
                    Some(
                        "archisobasedir=arch archiso_http_server=https://mirror.archlinux.org"
                            .to_string(),
                    )
                } else {
                    None
                };

                entries.push(BootEntry {
                    label,
                    kernel: rel_kernel,
                    initrd: if has_initrd { Some(initrd) } else { None },
                    kernel_args,
                    distribution,
                    source_iso: source_iso
                        .file_name()
                        .and_then(|n| n.to_str())
                        .unwrap_or("unknown")
                        .to_string(),
                    pretty_name: None,
                });
            }
        }

        Ok(entries)
    }

    /// Try Debian/Ubuntu layout: /install/vmlinuz, /casper/initrd.lz
    fn try_debian_layout(
        &self,
        mount_point: &Path,
        source_iso: &Path,
    ) -> Result<Vec<BootEntry>, IsoError> {
        let mut entries = Vec::new();

        // Debian-family ISOs have one or more of: /install (debian-
        // installer), /casper (ubuntu live), /.disk/info (both), or
        // /pool (package pool). Gate on at least one of those being
        // present — without the gate, try_debian_layout also matches
        // Alpine's /boot/vmlinuz-lts and produces spurious
        // "Debian/Ubuntu" entries. (#122)
        let debian_markers = [
            mount_point.join("install"),
            mount_point.join("casper"),
            mount_point.join(".disk"),
            mount_point.join("pool"),
            mount_point.join("dists"),
        ];
        if !debian_markers.iter().any(|p| self.env.exists(p)) {
            return Ok(entries);
        }

        // Try multiple potential locations
        let search_paths = [
            mount_point.join("install"),
            mount_point.join("casper"),
            mount_point.join("boot"),
        ];

        for search_dir in &search_paths {
            if !self.env.exists(search_dir) {
                continue;
            }

            // Find vmlinuz
            for entry in self.env.list_dir(search_dir)? {
                let name = entry.file_name().and_then(|n| n.to_str()).unwrap_or("");

                if name.starts_with("vmlinuz") {
                    let kernel = entry.clone();

                    // Look for initrd in same directory or common locations
                    let initrd_names = ["initrd.lz", "initrd.gz", "initrd.img", "initrd"];
                    let mut found_initrd = None;

                    for initrd_name in initrd_names {
                        let initrd_path = search_dir.join(initrd_name);
                        if self.env.exists(&initrd_path) {
                            found_initrd = Some(initrd_path);
                            break;
                        }
                    }

                    // Also check casper filesystem.squashfs for live boot
                    let kernel_args = if search_dir == &mount_point.join("casper") {
                        Some(
                            "boot=casper locale=en_US.UTF-8 keyboard-configuration/layoutcode=us"
                                .to_string(),
                        )
                    } else {
                        None
                    };

                    // Both casper and non-casper paths result in Debian family
                    entries.push(BootEntry {
                        label: format!(
                            "Debian/Ubuntu {}",
                            name.strip_prefix("vmlinuz").unwrap_or("").trim()
                        ),
                        kernel: kernel
                            .strip_prefix(mount_point)
                            .map(std::path::Path::to_path_buf)
                            .map_err(|_| {
                                IsoError::Io(std::io::Error::new(
                                    std::io::ErrorKind::InvalidData,
                                    "Kernel path escape",
                                ))
                            })?,
                        initrd: found_initrd
                            .map(|p| {
                                p.strip_prefix(mount_point)
                                    .map(std::path::Path::to_path_buf)
                                    .map_err(|_| {
                                        IsoError::Io(std::io::Error::new(
                                            std::io::ErrorKind::InvalidData,
                                            "Initrd path escape",
                                        ))
                                    })
                            })
                            .transpose()?,
                        kernel_args,
                        distribution: Distribution::Debian,
                        source_iso: source_iso
                            .file_name()
                            .and_then(|n| n.to_str())
                            .unwrap_or("unknown")
                            .to_string(),
                        pretty_name: None,
                    });
                }
            }
        }

        Ok(entries)
    }

    /// Try Fedora layout: /images/pxeboot/vmlinuz, /images/pxeboot/initrd.img
    fn try_fedora_layout(
        &self,
        mount_point: &Path,
        source_iso: &Path,
    ) -> Result<Vec<BootEntry>, IsoError> {
        let images_dir = mount_point.join("images").join("pxeboot");

        if !self.env.exists(&images_dir) {
            // Try alternate: /isolinux/ (common Fedora live media)
            let alt_dir = mount_point.join("isolinux");
            if !self.env.exists(&alt_dir) {
                return Ok(Vec::new());
            }
            return self.process_fedora_isolinux(&alt_dir, mount_point, source_iso);
        }

        let mut entries = Vec::new();

        // Find kernel
        for entry in self.env.list_dir(&images_dir)? {
            let name = entry.file_name().and_then(|n| n.to_str()).unwrap_or("");

            if name.starts_with("vmlinuz") {
                let kernel = entry.clone();

                // Find matching initrd
                let version = name.strip_prefix("vmlinuz").unwrap_or("");
                let initrd_names = [
                    format!("initrd{version}.img"),
                    "initrd.img".to_string(),
                    format!("initrd{}.img", version.trim_end_matches('-')),
                ];

                let mut found_initrd = None;
                for initrd_name in &initrd_names {
                    let initrd_path = images_dir.join(initrd_name);
                    if self.env.exists(&initrd_path) {
                        found_initrd = Some(initrd_path);
                        break;
                    }
                }

                entries.push(BootEntry {
                    label: format!("Fedora {}", version.trim()),
                    kernel: kernel
                        .strip_prefix(mount_point)
                        .map(std::path::Path::to_path_buf)
                        .map_err(|_| {
                            IsoError::Io(std::io::Error::new(
                                std::io::ErrorKind::InvalidData,
                                "Kernel path escape",
                            ))
                        })?,
                    initrd: found_initrd
                        .map(|p| {
                            p.strip_prefix(mount_point)
                                .map(std::path::Path::to_path_buf)
                                .map_err(|_| {
                                    IsoError::Io(std::io::Error::new(
                                        std::io::ErrorKind::InvalidData,
                                        "Initrd path escape",
                                    ))
                                })
                        })
                        .transpose()?,
                    kernel_args: Some("inst.stage2=hd:LABEL=Fedora-39-x86_64".to_string()),
                    distribution: Distribution::Fedora,
                    source_iso: source_iso
                        .file_name()
                        .and_then(|n| n.to_str())
                        .unwrap_or("unknown")
                        .to_string(),
                    pretty_name: None,
                });
            }
        }

        Ok(entries)
    }

    fn process_fedora_isolinux(
        &self,
        isolinux_dir: &Path,
        mount_point: &Path,
        source_iso: &Path,
    ) -> Result<Vec<BootEntry>, IsoError> {
        let mut entries = Vec::new();

        for entry in self.env.list_dir(isolinux_dir)? {
            let name = entry.file_name().and_then(|n| n.to_str()).unwrap_or("");

            if name.starts_with("vmlinuz") {
                let kernel = entry.clone();

                // Look for initrd in images directory
                let images_dir = mount_point.join("images");
                let initrd_path = images_dir.join("initrd.img");

                entries.push(BootEntry {
                    label: format!(
                        "Fedora (isolinux) {}",
                        name.strip_prefix("vmlinuz").unwrap_or("").trim()
                    ),
                    kernel: kernel
                        .strip_prefix(mount_point)
                        .map(std::path::Path::to_path_buf)
                        .map_err(|_| {
                            IsoError::Io(std::io::Error::new(
                                std::io::ErrorKind::InvalidData,
                                "Kernel path escape",
                            ))
                        })?,
                    initrd: if self.env.exists(&initrd_path) {
                        Some(
                            initrd_path
                                .strip_prefix(mount_point)
                                .map(std::path::Path::to_path_buf)
                                .map_err(|_| {
                                    IsoError::Io(std::io::Error::new(
                                        std::io::ErrorKind::InvalidData,
                                        "Initrd path escape",
                                    ))
                                })?,
                        )
                    } else {
                        None
                    },
                    kernel_args: Some("inst.stage2=hd:LABEL=Fedora".to_string()),
                    distribution: Distribution::Fedora,
                    source_iso: source_iso
                        .file_name()
                        .and_then(|n| n.to_str())
                        .unwrap_or("unknown")
                        .to_string(),
                    pretty_name: None,
                });
            }
        }

        Ok(entries)
    }

    /// Detect Windows installer ISOs (Win10, Win11, Server). Emits a
    /// synthesized `BootEntry` so the ISO surfaces in rescue-tui's list
    /// with `Distribution::Windows` and the `NotKexecBootable` quirk —
    /// replaces the current behavior where Windows ISOs got silently
    /// skipped as `NoBootEntries`, which mismatched the `docs/
    /// compatibility/iso-matrix.md` + `iso-probe`'s explicit "not a
    /// kexec target" classification.
    ///
    /// Detection uses three independent markers (ANY match suffices):
    ///
    /// 1. `/bootmgr` — Windows NT loader, present since Vista on
    ///    installer and recovery media.
    /// 2. `/sources/boot.wim` — Windows PE boot image, the signature
    ///    of a Microsoft-shipped install ISO.
    /// 3. `/efi/microsoft/boot/` — UEFI boot directory with the
    ///    signed `bootmgfw.efi`.
    ///
    /// The synthesized `kernel` field points at `bootmgr` (or the
    /// EFI equivalent when `bootmgr` is absent). It's never passed to
    /// kexec — downstream code gates on the `NotKexecBootable` quirk
    /// surfaced by `iso-probe::lookup_quirks(Distribution::Windows)`.
    /// Using `bootmgr` as the semantic "kernel" makes the rendered
    /// evidence line ("kernel: bootmgr") self-explanatory.
    // `Result` parallels `try_arch_layout` / `try_debian_layout` / etc.
    // even though Windows detection uses only `env.exists()` (infallible
    // in this crate's IsoEnvironment shape) — keeps the caller site in
    // `extract_boot_entries` uniformly `?`-chained across all layouts.
    #[allow(clippy::unnecessary_wraps)]
    fn try_windows_layout(
        &self,
        mount_point: &Path,
        source_iso: &Path,
    ) -> Result<Vec<BootEntry>, IsoError> {
        let bootmgr = mount_point.join("bootmgr");
        let boot_wim = mount_point.join("sources/boot.wim");
        let efi_ms_boot = mount_point.join("efi/microsoft/boot");
        let bootmgfw_efi = mount_point.join("efi/boot/bootx64.efi");

        let has_any_marker = self.env.exists(&bootmgr)
            || self.env.exists(&boot_wim)
            || self.env.exists(&efi_ms_boot);
        if !has_any_marker {
            return Ok(Vec::new());
        }

        // Prefer `bootmgr` (the classic NT loader) as the synthetic
        // "kernel" path. Fall back to bootmgfw.efi / a synthetic marker
        // if a stripped-down ISO is missing bootmgr but still carries
        // sources/boot.wim (unusual but seen on some Windows PE rebuilds).
        let kernel_path = if self.env.exists(&bootmgr) {
            PathBuf::from("bootmgr")
        } else if self.env.exists(&bootmgfw_efi) {
            PathBuf::from("efi/boot/bootx64.efi")
        } else {
            PathBuf::from("sources/boot.wim")
        };

        let label = "Windows (not kexec-bootable)".to_string();

        Ok(vec![BootEntry {
            label,
            kernel: kernel_path,
            // Windows PE uses `boot.wim` as its "initrd equivalent" but
            // that's not something kexec could use — leave None.
            initrd: None,
            kernel_args: None,
            distribution: Distribution::Windows,
            source_iso: source_iso
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("unknown")
                .to_string(),
            pretty_name: None,
        }])
    }
}

/// Best-effort "friendly" distro name for a mounted ISO.
///
/// Reads the first file in this priority order and returns the first
/// useful value found:
///
/// 1. `/etc/os-release` `PRETTY_NAME` — systemd convention; all
///    modern distros ship this (Ubuntu, Fedora, Rocky, Alma, Debian 12+,
///    openSUSE, Arch, `NixOS` 22+, Alpine 3.17+).
/// 2. `/lib/os-release` `PRETTY_NAME` — symlink target on some distros;
///    handled independently in case the `/etc` copy is missing.
/// 3. `/.disk/info` — single line of free text, Ubuntu + Debian live/install
///    media tradition since circa Debian 6. Form: "Ubuntu 24.04.2 LTS ...".
/// 4. `/etc/alpine-release` — single version string (e.g. "3.20.3") on
///    Alpine. We prepend "Alpine " so the returned value is self-contained.
///
/// Returns `None` if none of the paths exist or all attempts produce an
/// empty string. This is advisory — every caller must tolerate `None`
/// and fall back to the `Distribution`-family label.
#[must_use]
pub fn read_pretty_name(mount_point: &Path) -> Option<String> {
    for rel in ["etc/os-release", "lib/os-release", "usr/lib/os-release"] {
        if let Some(name) = read_os_release(&mount_point.join(rel)) {
            return Some(name);
        }
    }
    if let Some(first_line) = read_first_nonempty_line(&mount_point.join(".disk/info")) {
        return Some(first_line);
    }
    if let Some(version) = read_first_nonempty_line(&mount_point.join("etc/alpine-release")) {
        return Some(format!("Alpine Linux {version}"));
    }
    None
}

/// Parse a systemd-style `os-release` file for the value of `PRETTY_NAME`.
/// Strips surrounding double quotes if present. Returns `None` on any
/// read error or if the key is missing / empty.
fn read_os_release(path: &Path) -> Option<String> {
    let content = std::fs::read_to_string(path).ok()?;
    parse_os_release_pretty_name(&content)
}

/// Pure-string version of the `os-release` parser — split out so we can
/// unit-test without touching the filesystem.
#[must_use]
pub(crate) fn parse_os_release_pretty_name(content: &str) -> Option<String> {
    for line in content.lines() {
        let Some(rest) = line.strip_prefix("PRETTY_NAME=") else {
            continue;
        };
        // Strip surrounding " or ' (systemd spec allows either, and we
        // want to be forgiving of wild-in-the-field variants).
        let trimmed = rest
            .trim()
            .trim_matches(|c| c == '"' || c == '\'')
            .to_string();
        if trimmed.is_empty() {
            return None;
        }
        return Some(trimmed);
    }
    None
}

/// Read the first non-empty trimmed line of a file. Used for free-text
/// release files (`/.disk/info`, `/etc/alpine-release`) that don't
/// follow the `KEY=VALUE` shape.
fn read_first_nonempty_line(path: &Path) -> Option<String> {
    let content = std::fs::read_to_string(path).ok()?;
    for line in content.lines() {
        let trimmed = line.trim();
        if !trimmed.is_empty() {
            return Some(trimmed.to_string());
        }
    }
    None
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::too_many_lines,
    clippy::missing_panics_doc,
    clippy::match_same_arms
)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use std::sync::Mutex;

    /// Mock environment for testing without actual filesystem
    struct MockIsoEnvironment {
        files: HashMap<PathBuf, MockEntry>,
        mount_points: Mutex<Vec<PathBuf>>,
        /// Per-ISO mount failure injection for exercising the
        /// failure-surfacing path in [`IsoParser::scan_directory_with_failures`].
        /// Key = absolute ISO path, value = `MountFailed` reason string.
        mount_failures: HashMap<PathBuf, String>,
    }

    #[derive(Debug, Clone)]
    enum MockEntry {
        File,
        Directory(Vec<PathBuf>),
    }

    impl MockIsoEnvironment {
        fn new() -> Self {
            Self {
                files: HashMap::new(),
                mount_points: Mutex::new(Vec::new()),
                mount_failures: HashMap::new(),
            }
        }

        /// Register an ISO path whose [`IsoEnvironment::mount_iso`] call
        /// should fail with [`IsoError::MountFailed`] carrying `reason`.
        /// Used by the scan-failure surfacing tests.
        fn with_failing_mount(mut self, iso_path: &Path, reason: &str) -> Self {
            self.mount_failures
                .insert(iso_path.to_path_buf(), reason.to_string());
            self
        }

        fn with_iso(distribution: Distribution) -> Self {
            let mut env = Self::new();

            let mount_base = PathBuf::from("/mock_mount");

            match distribution {
                Distribution::Arch => {
                    // Arch: /boot/vmlinuz, /boot/initrd.img
                    env.files.insert(
                        mount_base.join("boot"),
                        MockEntry::Directory(vec![
                            mount_base.join("boot/vmlinuz"),
                            mount_base.join("boot/initrd.img"),
                        ]),
                    );
                    env.files
                        .insert(mount_base.join("boot/vmlinuz"), MockEntry::File);
                    env.files
                        .insert(mount_base.join("boot/initrd.img"), MockEntry::File);
                }
                Distribution::Debian => {
                    // Debian: /install/vmlinuz, /casper/initrd.lz
                    env.files.insert(
                        mount_base.join("install"),
                        MockEntry::Directory(vec![mount_base.join("install/vmlinuz")]),
                    );
                    env.files
                        .insert(mount_base.join("install/vmlinuz"), MockEntry::File);
                    env.files.insert(
                        mount_base.join("casper"),
                        MockEntry::Directory(vec![
                            mount_base.join("casper/initrd.lz"),
                            mount_base.join("casper/filesystem.squashfs"),
                        ]),
                    );
                    env.files
                        .insert(mount_base.join("casper/initrd.lz"), MockEntry::File);
                    env.files.insert(
                        mount_base.join("casper/filesystem.squashfs"),
                        MockEntry::File,
                    );
                }
                Distribution::Fedora => {
                    // Fedora: /images/pxeboot/vmlinuz, /images/pxeboot/initrd.img
                    env.files.insert(
                        mount_base.join("images"),
                        MockEntry::Directory(vec![mount_base.join("images/pxeboot")]),
                    );
                    env.files.insert(
                        mount_base.join("images/pxeboot"),
                        MockEntry::Directory(vec![
                            mount_base.join("images/pxeboot/vmlinuz"),
                            mount_base.join("images/pxeboot/initrd.img"),
                        ]),
                    );
                    env.files
                        .insert(mount_base.join("images/pxeboot/vmlinuz"), MockEntry::File);
                    env.files.insert(
                        mount_base.join("images/pxeboot/initrd.img"),
                        MockEntry::File,
                    );
                }
                // New variants reuse existing mock fixtures by analogue
                // (Alpine + NixOS behave like Arch at the path layer; RedHat
                // like Fedora). The scan_directory tests only care about the
                // 3 original categories, so nothing new to stage here.
                Distribution::RedHat | Distribution::Alpine | Distribution::NixOS => {}
                Distribution::Windows => {
                    // Windows installer: /bootmgr + /sources/boot.wim +
                    // /efi/microsoft/boot/. We stage all three canonical
                    // markers so try_windows_layout's any-marker detection
                    // logic gets exercised from multiple angles.
                    env.files
                        .insert(mount_base.join("bootmgr"), MockEntry::File);
                    env.files.insert(
                        mount_base.join("sources"),
                        MockEntry::Directory(vec![mount_base.join("sources/boot.wim")]),
                    );
                    env.files
                        .insert(mount_base.join("sources/boot.wim"), MockEntry::File);
                    env.files.insert(
                        mount_base.join("efi"),
                        MockEntry::Directory(vec![mount_base.join("efi/microsoft")]),
                    );
                    env.files.insert(
                        mount_base.join("efi/microsoft"),
                        MockEntry::Directory(vec![mount_base.join("efi/microsoft/boot")]),
                    );
                    env.files.insert(
                        mount_base.join("efi/microsoft/boot"),
                        MockEntry::Directory(vec![]),
                    );
                }
                Distribution::Unknown => {}
            }

            // Add ISO file in parent directory
            env.files.insert(
                PathBuf::from("/isos"),
                MockEntry::Directory(vec![PathBuf::from("/isos/test.iso")]),
            );
            env.files
                .insert(PathBuf::from("/isos/test.iso"), MockEntry::File);

            env
        }
    }

    impl IsoEnvironment for MockIsoEnvironment {
        fn list_dir(&self, path: &std::path::Path) -> std::io::Result<Vec<PathBuf>> {
            match self.files.get(path) {
                Some(MockEntry::Directory(entries)) => Ok(entries.clone()),
                Some(MockEntry::File) => Err(std::io::Error::new(
                    std::io::ErrorKind::NotFound,
                    "Not a directory",
                )),
                None => Ok(Vec::new()), // Empty for non-existent
            }
        }

        fn exists(&self, path: &std::path::Path) -> bool {
            self.files.contains_key(path)
        }

        fn metadata(&self, _path: &std::path::Path) -> std::io::Result<std::fs::Metadata> {
            // Fail closed: the previous implementation returned the real
            // metadata of `std::env::temp_dir()` for any path that existed
            // in the mock — which silently made size/mtime assertions pass
            // on fake data (they'd read /tmp's values, not the mock's).
            //
            // Since no caller in the workspace uses IsoEnvironment::metadata
            // today (the trait method is currently unused, per #138 audit),
            // and std::fs::Metadata has no public constructor, there is no
            // safe way to return a synthesized value from pure mock data.
            //
            // If a future caller needs this method, the correct fix is to
            // add real size/mtime fields to MockEntry and return them via a
            // wrapper type — not to paper over the hazard with /tmp values.
            Err(std::io::Error::new(
                std::io::ErrorKind::Unsupported,
                "MockIsoEnvironment::metadata is not implemented — see #138 for the design note",
            ))
        }

        fn mount_iso(&self, iso_path: &std::path::Path) -> Result<PathBuf, IsoError> {
            if let Some(reason) = self.mount_failures.get(iso_path) {
                return Err(IsoError::MountFailed(reason.clone()));
            }
            let mount_point = PathBuf::from(format!(
                "/mock_mount/{}",
                iso_path
                    .file_stem()
                    .and_then(|s| s.to_str())
                    .unwrap_or("iso")
            ));

            // Poison-safe lock: if a prior test panicked while holding the
            // mutex, `.lock()` returns `Err(PoisonError)`. `into_inner()`
            // recovers the guarded value so we don't cascade-fail every
            // subsequent test that happens to hit this path. Mock state is
            // append-or-trim only, so partial updates from a poisoned
            // critical section are safe to observe.
            self.mount_points
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .push(mount_point.clone());
            Ok(mount_point)
        }

        fn unmount(&self, mount_point: &std::path::Path) -> Result<(), IsoError> {
            let mut points = self
                .mount_points
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            points.retain(|p| p != mount_point);
            Ok(())
        }
    }

    #[test]
    fn test_path_traversal_blocked() {
        let env = MockIsoEnvironment::new();
        let result = env.validate_path(
            PathBuf::from("/safe").as_path(),
            PathBuf::from("/safe/../../../etc/passwd").as_path(),
        );

        assert!(result.is_err());
        match result {
            Err(IsoError::PathTraversal(_)) => {}
            _ => panic!("Expected PathTraversal error"),
        }
    }

    #[test]
    fn test_path_allowed() {
        let env = MockIsoEnvironment::new();
        let result = env.validate_path(
            PathBuf::from("/safe").as_path(),
            PathBuf::from("/safe/subdir/file").as_path(),
        );

        assert!(result.is_ok());
    }

    #[test]
    fn test_path_outside_base_rejected() {
        // Regression for #56: validate_path used to silently return Ok
        // when strip_prefix(base) failed, accepting absolute paths to
        // anywhere on the filesystem.
        let env = MockIsoEnvironment::new();
        let result = env.validate_path(
            PathBuf::from("/mnt/iso").as_path(),
            PathBuf::from("/etc/passwd").as_path(),
        );
        assert!(matches!(result, Err(IsoError::PathTraversal(_))));
    }

    #[test]
    fn test_path_sibling_of_base_rejected() {
        // /safe2 starts with the string "/safe" but is NOT under /safe —
        // Path::starts_with respects component boundaries, not prefix match.
        let env = MockIsoEnvironment::new();
        let result = env.validate_path(
            PathBuf::from("/safe").as_path(),
            PathBuf::from("/safe2/file").as_path(),
        );
        assert!(matches!(result, Err(IsoError::PathTraversal(_))));
    }

    #[test]
    fn test_dots_embedded_in_filename_are_not_traversal() {
        // Regression for the nightly-fuzz panic on 2026-04-19..23:
        // filenames like `..\x03|.` or `foo..bar` contain `..` as a
        // substring but are single legitimate path components (no `/`
        // separator around the dots). validate_path correctly accepts
        // them because `Path::components()` reports them as Normal,
        // not ParentDir. Real ISOs in the wild can carry such
        // filenames; rejecting them would block legitimate extraction.
        let env = MockIsoEnvironment::new();

        for weird_name in [
            "foo..bar",
            "..\x03|.",
            "..hidden",
            "trailing..",
            "..".repeat(4).as_str(),
        ] {
            let candidate = PathBuf::from(format!("/safe/{weird_name}"));
            let result = env.validate_path(PathBuf::from("/safe").as_path(), candidate.as_path());
            // filenames that are LITERALLY ".." are ParentDir and
            // should reject; anything else with embedded dots is a
            // Normal component and should pass through.
            if weird_name == ".." {
                assert!(
                    matches!(result, Err(IsoError::PathTraversal(_))),
                    "literal `..` must reject, got {result:?} for {weird_name:?}"
                );
            } else {
                assert!(
                    result.is_ok(),
                    "`..`-substring but not a ParentDir component must pass: {weird_name:?} got {result:?}"
                );
            }
        }
    }

    #[test]
    fn test_parent_dir_in_middle_of_path_rejected() {
        // Genuine traversal: `..` as a path component between
        // `/` boundaries. validate_path catches this via
        // `Component::ParentDir` detection.
        let env = MockIsoEnvironment::new();
        let result = env.validate_path(
            PathBuf::from("/safe").as_path(),
            PathBuf::from("/safe/a/../b").as_path(),
        );
        assert!(matches!(result, Err(IsoError::PathTraversal(_))));
    }

    #[tokio::test]
    async fn test_arch_detection() {
        let mock = MockIsoEnvironment::with_iso(Distribution::Arch);
        let parser = IsoParser::new(mock);

        let mount_base = PathBuf::from("/mock_mount");
        let entries = parser
            .extract_boot_entries(&mount_base, &PathBuf::from("test.iso"))
            .await
            .unwrap();

        // Should find at least the Arch entry (might also find via other layouts that scan /boot)
        assert!(!entries.is_empty());
        assert!(entries.iter().any(|e| e.distribution == Distribution::Arch));
        assert!(
            entries
                .iter()
                .any(|e| e.kernel.to_string_lossy().contains("vmlinuz"))
        );
    }

    #[tokio::test]
    async fn test_debian_detection() {
        let mock = MockIsoEnvironment::with_iso(Distribution::Debian);
        let parser = IsoParser::new(mock);

        let mount_base = PathBuf::from("/mock_mount");
        let entries = parser
            .extract_boot_entries(&mount_base, &PathBuf::from("test.iso"))
            .await
            .unwrap();

        assert!(!entries.is_empty());
        assert!(
            entries
                .iter()
                .any(|e| e.distribution == Distribution::Debian)
        );
    }

    #[tokio::test]
    async fn test_fedora_detection() {
        let mock = MockIsoEnvironment::with_iso(Distribution::Fedora);
        let parser = IsoParser::new(mock);

        let mount_base = PathBuf::from("/mock_mount");
        let entries = parser
            .extract_boot_entries(&mount_base, &PathBuf::from("test.iso"))
            .await
            .unwrap();

        assert!(!entries.is_empty());
        assert!(
            entries
                .iter()
                .any(|e| e.distribution == Distribution::Fedora)
        );
    }

    #[test]
    fn test_distribution_from_paths() {
        assert_eq!(
            Distribution::from_paths(PathBuf::from("/boot/vmlinuz").as_path()),
            Distribution::Arch
        );
        assert_eq!(
            Distribution::from_paths(PathBuf::from("/casper/vmlinuz").as_path()),
            Distribution::Debian
        );
        assert_eq!(
            Distribution::from_paths(PathBuf::from("/images/pxeboot/vmlinuz").as_path()),
            Distribution::Fedora
        );
    }

    #[test]
    fn test_boot_entry_serialization() {
        let entry = BootEntry {
            label: "Test Linux".to_string(),
            kernel: PathBuf::from("boot/vmlinuz"),
            initrd: Some(PathBuf::from("boot/initrd.img")),
            kernel_args: Some("quiet".to_string()),
            distribution: Distribution::Arch,
            source_iso: "test.iso".to_string(),
            pretty_name: None,
        };

        let json = serde_json::to_string(&entry).unwrap();
        let decoded: BootEntry = serde_json::from_str(&json).unwrap();

        assert_eq!(decoded.label, "Test Linux");
        assert_eq!(decoded.distribution, Distribution::Arch);
    }

    // ---- #119: pretty-name detection --------------------------------

    #[test]
    fn parse_pretty_name_systemd_shape() {
        let content = r#"
NAME="Ubuntu"
VERSION_ID="24.04"
PRETTY_NAME="Ubuntu 24.04.2 LTS (Noble Numbat)"
ID=ubuntu
"#;
        assert_eq!(
            parse_os_release_pretty_name(content).as_deref(),
            Some("Ubuntu 24.04.2 LTS (Noble Numbat)"),
        );
    }

    #[test]
    fn parse_pretty_name_strips_single_quotes() {
        let content = "PRETTY_NAME='Alpine Linux v3.20'";
        assert_eq!(
            parse_os_release_pretty_name(content).as_deref(),
            Some("Alpine Linux v3.20"),
        );
    }

    #[test]
    fn parse_pretty_name_unquoted_value() {
        // Some distros omit the quotes; spec allows either.
        let content = "PRETTY_NAME=Arch Linux";
        assert_eq!(
            parse_os_release_pretty_name(content).as_deref(),
            Some("Arch Linux"),
        );
    }

    #[test]
    fn parse_pretty_name_empty_returns_none() {
        assert!(parse_os_release_pretty_name("PRETTY_NAME=\"\"").is_none());
        assert!(parse_os_release_pretty_name("").is_none());
    }

    #[test]
    fn parse_pretty_name_missing_returns_none() {
        let content = "NAME=\"Ubuntu\"\nID=ubuntu";
        assert!(parse_os_release_pretty_name(content).is_none());
    }

    #[test]
    fn parse_pretty_name_first_match_wins() {
        // Defensive: if a file has two PRETTY_NAME lines, take the first.
        let content = "PRETTY_NAME=\"First\"\nPRETTY_NAME=\"Second\"";
        assert_eq!(
            parse_os_release_pretty_name(content).as_deref(),
            Some("First"),
        );
    }

    #[test]
    fn read_pretty_name_finds_etc_os_release() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(tmp.path().join("etc")).unwrap();
        std::fs::write(
            tmp.path().join("etc/os-release"),
            "PRETTY_NAME=\"Rocky Linux 9.3 (Blue Onyx)\"\n",
        )
        .unwrap();
        assert_eq!(
            read_pretty_name(tmp.path()).as_deref(),
            Some("Rocky Linux 9.3 (Blue Onyx)"),
        );
    }

    #[test]
    fn read_pretty_name_falls_back_to_disk_info() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(tmp.path().join(".disk")).unwrap();
        std::fs::write(
            tmp.path().join(".disk/info"),
            "Ubuntu 24.04.2 LTS \"Noble Numbat\" - Release amd64 (20250215)\n",
        )
        .unwrap();
        assert_eq!(
            read_pretty_name(tmp.path()).as_deref(),
            Some("Ubuntu 24.04.2 LTS \"Noble Numbat\" - Release amd64 (20250215)"),
        );
    }

    #[test]
    fn read_pretty_name_alpine_release_prepends_alpine_linux() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(tmp.path().join("etc")).unwrap();
        std::fs::write(tmp.path().join("etc/alpine-release"), "3.20.3\n").unwrap();
        assert_eq!(
            read_pretty_name(tmp.path()).as_deref(),
            Some("Alpine Linux 3.20.3"),
        );
    }

    #[test]
    fn read_pretty_name_prefers_etc_over_lib() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(tmp.path().join("etc")).unwrap();
        std::fs::create_dir_all(tmp.path().join("usr/lib")).unwrap();
        std::fs::write(
            tmp.path().join("etc/os-release"),
            "PRETTY_NAME=\"Etc Wins\"\n",
        )
        .unwrap();
        std::fs::write(
            tmp.path().join("usr/lib/os-release"),
            "PRETTY_NAME=\"Lib Loses\"\n",
        )
        .unwrap();
        assert_eq!(read_pretty_name(tmp.path()).as_deref(), Some("Etc Wins"),);
    }

    #[test]
    fn read_pretty_name_returns_none_for_empty_mount() {
        let tmp = tempfile::tempdir().unwrap();
        assert!(read_pretty_name(tmp.path()).is_none());
    }

    #[test]
    fn read_pretty_name_skips_empty_disk_info_line() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(tmp.path().join(".disk")).unwrap();
        std::fs::write(tmp.path().join(".disk/info"), "\n\n   \nDebian 12.8\n").unwrap();
        assert_eq!(read_pretty_name(tmp.path()).as_deref(), Some("Debian 12.8"),);
    }

    /// `MockIsoEnvironment::metadata` must fail closed — previously it
    /// returned the real metadata of `std::env::temp_dir()` for any path
    /// the mock knew about, which silently validated size/mtime assertions
    /// against `/tmp` values instead of mock data. Regression from #138.
    #[test]
    fn mock_metadata_fails_closed() {
        let env = MockIsoEnvironment::new();
        let err = env
            .metadata(std::path::Path::new("/mock_mount/boot/vmlinuz"))
            .expect_err("mock metadata() must surface an error");
        assert_eq!(err.kind(), std::io::ErrorKind::Unsupported);
    }

    /// Poisoned mount-points mutex must not cascade. Simulate a poisoning
    /// by panicking inside a lock-holding scope and confirm subsequent
    /// `mount_iso` / `unmount` calls still succeed. Regression from #138.
    #[test]
    fn mock_mount_lock_recovers_from_poison() {
        use std::sync::Arc;
        let env = Arc::new(MockIsoEnvironment::new());
        // Force a poisoned lock by panicking inside a critical section on
        // a scoped thread. The spawned thread's join result is expected
        // to be Err (the panic); that's what poisons the Mutex.
        let env_for_thread = env.clone();
        let join = std::thread::spawn(move || {
            let _guard = env_for_thread.mount_points.lock().unwrap();
            panic!("deliberately poisoning the mutex for this test");
        })
        .join();
        assert!(join.is_err(), "helper thread must have panicked");

        // Now verify the mock still functions — mount + unmount should
        // succeed without panicking via lock recovery.
        let iso = std::path::Path::new("/isos/test.iso");
        let mount = env
            .mount_iso(iso)
            .expect("mount_iso must recover from poison");
        env.unmount(&mount)
            .expect("unmount must recover from poison");
    }

    // ---- Windows layout detection (was silently skipped before) ----

    #[tokio::test]
    async fn extract_boot_entries_detects_windows_installer() {
        let mock = MockIsoEnvironment::with_iso(Distribution::Windows);
        let parser = IsoParser::new(mock);

        let mount_base = PathBuf::from("/mock_mount");
        let entries = parser
            .extract_boot_entries(&mount_base, &PathBuf::from("Win11_25H2.iso"))
            .await
            .expect("Windows ISO should now produce a BootEntry instead of empty");

        assert!(
            !entries.is_empty(),
            "Windows ISO must produce at least one entry"
        );
        let win = entries
            .iter()
            .find(|e| e.distribution == Distribution::Windows)
            .expect("one of the entries must be Distribution::Windows");
        assert_eq!(win.kernel.to_string_lossy(), "bootmgr");
        assert!(win.initrd.is_none());
        assert_eq!(win.kernel_args, None);
        assert!(win.label.contains("Windows"));
        assert!(win.source_iso.contains("Win11"));
    }

    #[tokio::test]
    async fn try_windows_layout_declines_on_linux_layouts() {
        // Arch mock has no Windows markers; try_windows_layout must
        // decline (return empty) rather than synthesize an entry.
        let mock = MockIsoEnvironment::with_iso(Distribution::Arch);
        let parser = IsoParser::new(mock);

        let mount_base = PathBuf::from("/mock_mount");
        let entries = parser
            .extract_boot_entries(&mount_base, &PathBuf::from("arch.iso"))
            .await
            .expect("Arch ISO must produce entries");

        // No Windows-tagged entries should sneak in.
        assert!(
            !entries
                .iter()
                .any(|e| e.distribution == Distribution::Windows),
            "Windows detector must not fire on Arch fixture"
        );
    }

    #[test]
    fn windows_boot_entry_has_not_kexec_bootable_quirk_in_iso_probe() {
        // Contract between iso-parser and iso-probe: when iso-parser emits
        // Distribution::Windows, iso-probe's lookup_quirks returns
        // NotKexecBootable. This test lives here (iso-parser side) so
        // the pairing is guarded end-to-end even if iso-probe internals
        // change — the public Distribution::Windows arm is stable.
        //
        // We don't depend on iso-probe from this crate (cyclic), so this
        // test asserts the metadata iso-parser produces is the shape
        // iso-probe's mapping expects (a Windows enum variant an
        // external crate can pattern-match on).
        let iso_distro = Distribution::Windows;
        assert!(matches!(iso_distro, Distribution::Windows));
    }

    // ---- #456 — ScanReport / ScanFailure surfacing ----

    #[test]
    fn sanitize_reason_trims_whitespace() {
        assert_eq!(sanitize_reason("  hello  "), "hello");
    }

    #[test]
    fn sanitize_reason_replaces_control_chars_with_spaces() {
        // Newlines, tabs, and C0 controls all become single spaces so
        // the TUI's line-layout math doesn't break on multi-line error
        // strings (common from mount's stderr).
        let input = "mount failed:\nwrong fs type\tor bad\x01option";
        let out = sanitize_reason(input);
        assert!(!out.contains('\n'));
        assert!(!out.contains('\t'));
        assert!(!out.contains('\x01'));
        assert!(out.contains("mount failed"));
        assert!(out.contains("wrong fs type"));
    }

    #[test]
    fn sanitize_reason_preserves_utf8() {
        let out = sanitize_reason("données non prises en charge — système Win32 ≠ ext4");
        assert!(out.contains("données"));
        assert!(out.contains(''));
    }

    #[test]
    fn sanitize_reason_truncates_at_char_boundary() {
        // Long string with multibyte chars near the truncation point
        // must not split a char.
        let long = "é".repeat(200); // 400 bytes, well over MAX_REASON_LEN
        let out = sanitize_reason(&long);
        // Must end with the ellipsis we appended.
        assert!(
            out.ends_with(''),
            "truncated output must end with …, got {out}"
        );
        // Must be valid UTF-8 (implicit — Rust guarantees this for String).
        assert!(out.chars().all(|c| c == 'é' || c == ''));
    }

    #[test]
    fn scan_failure_kind_maps_from_iso_error() {
        assert_eq!(
            ScanFailureKind::from_iso_error(&IsoError::MountFailed("x".into())),
            ScanFailureKind::MountFailed
        );
        assert_eq!(
            ScanFailureKind::from_iso_error(&IsoError::NoBootEntries("x".into())),
            ScanFailureKind::NoBootEntries
        );
        assert_eq!(
            ScanFailureKind::from_iso_error(&IsoError::Io(std::io::Error::other("io"))),
            ScanFailureKind::IoError
        );
        // PathTraversal is not a per-file error; map defensively to IoError.
        assert_eq!(
            ScanFailureKind::from_iso_error(&IsoError::PathTraversal("x".into())),
            ScanFailureKind::IoError
        );
    }

    /// Build a MockIsoEnvironment with `/isos/` containing `a.iso` and
    /// `b.iso` — `a.iso` mounts successfully with an Arch layout;
    /// `b.iso` can be configured to fail via `with_failing_mount`.
    ///
    /// The `/mock_mount/a` subtree is populated with an Arch-style
    /// layout so `a.iso` parses; `b.iso` mounts to `/mock_mount/b`
    /// which is intentionally empty (so even if b.iso mounts, it
    /// produces no entries — callers that want a mount-failure
    /// specifically must call `with_failing_mount`).
    fn mock_with_two_isos() -> MockIsoEnvironment {
        let mut env = MockIsoEnvironment::new();
        // Register the top-level /isos directory.
        env.files.insert(
            PathBuf::from("/isos"),
            MockEntry::Directory(vec![
                PathBuf::from("/isos/a.iso"),
                PathBuf::from("/isos/b.iso"),
            ]),
        );
        env.files
            .insert(PathBuf::from("/isos/a.iso"), MockEntry::File);
        env.files
            .insert(PathBuf::from("/isos/b.iso"), MockEntry::File);

        // Arch layout under /mock_mount/a (matches mount_iso's
        // filename-based mount-point derivation).
        let a_root = PathBuf::from("/mock_mount/a");
        env.files.insert(
            a_root.clone(),
            MockEntry::Directory(vec![a_root.join("boot")]),
        );
        env.files.insert(
            a_root.join("boot"),
            MockEntry::Directory(vec![
                a_root.join("boot/vmlinuz"),
                a_root.join("boot/initrd.img"),
            ]),
        );
        env.files
            .insert(a_root.join("boot/vmlinuz"), MockEntry::File);
        env.files
            .insert(a_root.join("boot/initrd.img"), MockEntry::File);

        env
    }

    #[tokio::test]
    async fn scan_directory_with_failures_empty_dir_errors_no_boot_entries() {
        // Walk found zero .iso files — still an error so callers can
        // distinguish empty-stick from stick-with-broken-ISOs.
        let mut env = MockIsoEnvironment::new();
        env.files
            .insert(PathBuf::from("/isos"), MockEntry::Directory(Vec::new()));
        let parser = IsoParser::new(env);
        let err = parser
            .scan_directory_with_failures(Path::new("/isos"))
            .await
            .expect_err("empty dir must error");
        assert!(matches!(err, IsoError::NoBootEntries(_)));
    }

    #[tokio::test]
    async fn scan_directory_with_failures_all_failed_returns_ok_with_failures() {
        // Directory has ISOs but every mount fails — we return Ok with
        // empty entries + populated failures so rescue-tui can show a
        // descriptive row per broken ISO instead of hiding them.
        let env = mock_with_two_isos()
            .with_failing_mount(
                Path::new("/isos/a.iso"),
                "mount: wrong fs type, bad option, bad superblock",
            )
            .with_failing_mount(Path::new("/isos/b.iso"), "mount: no loop device available");
        let parser = IsoParser::new(env);

        let report = parser
            .scan_directory_with_failures(Path::new("/isos"))
            .await
            .expect("all-failed is Ok, not an error");
        assert!(report.entries.is_empty(), "no ISOs should parse");
        assert_eq!(report.failures.len(), 2);
        // Failures must carry path + sanitized reason + kind.
        let by_path: HashMap<_, _> = report
            .failures
            .iter()
            .map(|f| (f.iso_path.clone(), f.clone()))
            .collect();
        let a = &by_path[&PathBuf::from("/isos/a.iso")];
        assert_eq!(a.kind, ScanFailureKind::MountFailed);
        assert!(a.reason.contains("wrong fs type"));
        let b = &by_path[&PathBuf::from("/isos/b.iso")];
        assert_eq!(b.kind, ScanFailureKind::MountFailed);
        assert!(b.reason.contains("no loop device"));
    }

    #[tokio::test]
    async fn scan_directory_with_failures_mixed_returns_entries_and_failures() {
        // a.iso mounts (Arch), b.iso fails — report carries both.
        let env = mock_with_two_isos()
            .with_failing_mount(Path::new("/isos/b.iso"), "mount: input/output error");
        let parser = IsoParser::new(env);

        let report = parser
            .scan_directory_with_failures(Path::new("/isos"))
            .await
            .expect("mixed is Ok");
        assert!(
            !report.entries.is_empty(),
            "a.iso should produce at least one entry"
        );
        assert!(
            report.entries.iter().any(|e| e.source_iso == "a.iso"),
            "entries must include a.iso"
        );
        assert_eq!(report.failures.len(), 1);
        assert_eq!(report.failures[0].iso_path, PathBuf::from("/isos/b.iso"));
        assert!(report.failures[0].reason.contains("input/output"));
    }

    #[tokio::test]
    async fn scan_directory_legacy_preserves_no_boot_entries_on_all_failed() {
        // The old scan_directory contract: when every on-disk ISO
        // fails to parse, the overall result is NoBootEntries. This
        // preserves the callsite behavior of any pre-#456 consumer
        // that pattern-matches on that error.
        let env = mock_with_two_isos()
            .with_failing_mount(Path::new("/isos/a.iso"), "mount fail a")
            .with_failing_mount(Path::new("/isos/b.iso"), "mount fail b");
        let parser = IsoParser::new(env);

        let err = parser
            .scan_directory(Path::new("/isos"))
            .await
            .expect_err("legacy wrapper must error when all failed");
        assert!(matches!(err, IsoError::NoBootEntries(_)));
    }
}