rch 2.1.20

Remote Compilation Helper - PreToolUse hook CLI
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
//! Binary installation and rollback.

use super::download::DownloadedRelease;
use super::lock::UpdateLock;
use super::types::{BackupEntry, MAX_BACKUPS, UpdateError};
use crate::commands::configured_socket_path;
use crate::ui::OutputContext;
use flate2::read::GzDecoder;
use std::fs;
use std::io::{self, Read};
use std::path::{Component, Path, PathBuf};
use std::process::Command;

#[cfg(unix)]
mod daemon_shutdown;

#[cfg(not(windows))]
const UPDATE_BINARIES: &[&str] = &["rch", "rchd", "rch-wkr"];
#[cfg(windows)]
const UPDATE_BINARIES: &[&str] = &["rch.exe", "rchd.exe", "rch-wkr.exe"];

#[cfg(not(windows))]
const REQUIRED_UPDATE_BINARY: &str = "rch";
#[cfg(windows)]
const REQUIRED_UPDATE_BINARY: &str = "rch.exe";

/// Result of installation.
#[allow(dead_code)]
pub struct InstallResult {
    pub backup_path: PathBuf,
    pub installed_version: String,
    pub daemon_restarted: bool,
}

/// Install downloaded update.
pub async fn install_update(
    ctx: &OutputContext,
    download: &DownloadedRelease,
    restart_daemon: bool,
    drain_timeout: u64,
) -> Result<InstallResult, UpdateError> {
    // Acquire update lock
    let _lock = UpdateLock::acquire()?;

    if !ctx.is_json() {
        println!("Installing update...");
    }

    // Get installation paths
    let install_dir = get_install_dir()?;

    // Reject malformed, incomplete or unusable releases while the running
    // installation is still untouched. Stage on the destination filesystem:
    // copying out of an archive (and executing from a noexec temp directory)
    // must not be deferred until after the daemon has been stopped.
    let prepared = prepare_release(
        &download.archive_path,
        &install_dir,
        &download.version,
        std::time::Duration::from_secs(10),
    )
    .await?;

    // Get current version for backup
    let current_version = env!("CARGO_PKG_VERSION");

    // Create backup with metadata
    if !ctx.is_json() {
        println!("Backing up current installation (v{})...", current_version);
    }
    let backup_entry = create_backup(&install_dir, current_version)?;
    let backup_dir = backup_entry.backup_path;

    // Both release preparation and backup must succeed before any shutdown.
    let daemon_was_running = if restart_daemon {
        stop_daemon_gracefully(drain_timeout).await?
    } else {
        false
    };

    // Publish exactly the staged payloads that passed the version probes.
    // Each rename is atomic on Unix; this is not a crash-atomic multi-file
    // transaction. Retain the backup if publication or restart fails.
    if !ctx.is_json() {
        println!("Installing new binaries...");
    }
    let _installed_binaries = replace_binaries(prepared)?;

    // Check the actual installed siblings with the same bounded version probe.
    super::inspect_installation(
        &install_dir.join(REQUIRED_UPDATE_BINARY),
        &download.version,
        std::time::Duration::from_secs(10),
    )
    .await
    .map_err(|error| {
        UpdateError::InstallFailed(format!("Installed version check failed: {error:#}"))
    })?;

    // Restart daemon if it was running
    let daemon_restarted = if restart_daemon && daemon_was_running {
        if !ctx.is_json() {
            println!("Restarting daemon...");
        }
        start_daemon(&install_dir, Some(&download.version)).await?;
        true
    } else {
        false
    };

    Ok(InstallResult {
        backup_path: backup_dir,
        installed_version: download.version.clone(),
        daemon_restarted,
    })
}

/// Rollback to previous version.
///
/// If `target_version` is None, rolls back to the most recent backup.
/// If `target_version` is Some, rolls back to the specified version.
pub async fn rollback(
    ctx: &OutputContext,
    dry_run: bool,
    target_version: Option<&str>,
) -> Result<(), UpdateError> {
    // A real rollback chooses its backup under the same ownership lock as
    // updates, so another update cannot prune the selected backup mid-read.
    let _lock = if dry_run {
        None
    } else {
        Some(UpdateLock::acquire()?)
    };
    let backup_dir = if let Some(version) = target_version {
        find_backup_by_version(version)?
    } else {
        find_latest_backup()?
    };

    // Try to get version info from backup metadata
    let version_info = {
        let metadata_path = backup_dir.join("backup.json");
        if metadata_path.exists() {
            fs::read_to_string(&metadata_path)
                .ok()
                .and_then(|s| serde_json::from_str::<BackupEntry>(&s).ok())
                .map(|e| e.version)
        } else {
            None
        }
    };

    if !ctx.is_json() {
        if let Some(ref version) = version_info {
            println!("Rolling back to version {}...", version);
        } else {
            println!("Rolling back to backup at {}...", backup_dir.display());
        }
    }

    if dry_run {
        if !ctx.is_json() {
            println!("Dry run: would restore from {}", backup_dir.display());
        }
        return Ok(());
    }

    let install_dir = get_install_dir()?;
    // A missing required client, unreadable companion or staging failure must
    // leave the current daemon and all installed binaries alone.
    let prepared = StagedBinaries::new(&backup_dir, &install_dir)?;
    if let Some(version) = &version_info {
        prepared
            .verify(version, std::time::Duration::from_secs(10))
            .await?;
    }

    let daemon_was_running = stop_daemon_gracefully(30).await?;

    replace_binaries(prepared)?;

    // Restart daemon if it was running
    if daemon_was_running {
        start_daemon(&install_dir, version_info.as_deref()).await?;
    }

    if !ctx.is_json() {
        if let Some(version) = version_info {
            println!("Rollback to version {} complete.", version);
        } else {
            println!("Rollback complete.");
        }
    }

    Ok(())
}

/// Find a backup by version string.
fn find_backup_by_version(version: &str) -> Result<PathBuf, UpdateError> {
    let backups = list_backups()?;
    let version_clean = version.strip_prefix('v').unwrap_or(version);

    for backup in backups {
        if backup.version == version_clean || backup.version == version {
            return Ok(backup.backup_path);
        }
    }

    Err(UpdateError::NoBackupAvailable)
}

/// Get the installation directory.
fn get_install_dir() -> Result<PathBuf, UpdateError> {
    // Try to determine where rch is installed
    if let Ok(exe) = std::env::current_exe()
        && let Some(parent) = exe.parent()
    {
        return Ok(parent.to_path_buf());
    }

    // Default to ~/.local/bin
    let home = dirs::home_dir().ok_or_else(|| {
        UpdateError::InstallFailed("Could not determine home directory".to_string())
    })?;

    Ok(home.join(".local/bin"))
}

/// Get the backup directory for a version.
fn get_backup_dir(version: &str) -> Result<PathBuf, UpdateError> {
    let data_dir = dirs::data_dir().ok_or_else(|| {
        UpdateError::InstallFailed("Could not determine data directory".to_string())
    })?;

    let backup_base = data_dir.join("rch/backups");
    std::fs::create_dir_all(&backup_base)
        .map_err(|e| UpdateError::InstallFailed(format!("Failed to create backup dir: {}", e)))?;

    // Include a nonce so rapid same-version backups cannot alias each other.
    let timestamp = chrono::Utc::now().format("%Y%m%d_%H%M%S_%f");
    Ok(backup_base.join(format!(
        "v{}-{}-{}",
        version,
        timestamp,
        uuid::Uuid::new_v4()
    )))
}

/// Find the latest backup.
///
/// Previously sorted directory entries by filename, which made `v1.0.10-...`
/// appear *older* than `v1.0.9-...` (lexicographic `0` < `9`). Worse, any
/// version with ten or more digits in any segment would land in the wrong
/// bucket entirely. We now delegate to `list_backups`, which reads each
/// backup's `backup.json` metadata and sorts by the recorded creation time,
/// so "latest" really means "most recently created" regardless of version
/// formatting.
fn find_latest_backup() -> Result<PathBuf, UpdateError> {
    let backups = list_backups()?;
    backups
        .into_iter()
        .next()
        .map(|b| b.backup_path)
        .ok_or(UpdateError::NoBackupAvailable)
}

/// Backup current installation.
fn backup_current_installation(
    install_dir: &std::path::Path,
    backup_dir: &std::path::Path,
) -> Result<(), UpdateError> {
    std::fs::create_dir_all(backup_dir)
        .map_err(|e| UpdateError::InstallFailed(format!("Failed to create backup dir: {}", e)))?;

    for binary in UPDATE_BINARIES {
        let src = install_dir.join(binary);
        if binary_path_exists(&src, binary, "installed binary")? {
            let dst = backup_dir.join(binary);
            copy_regular_binary_payload(&src, &dst, binary, "backup")?;
        }
    }

    Ok(())
}

/// Create a backup with JSON metadata file.
pub fn create_backup(
    install_dir: &std::path::Path,
    version: &str,
) -> Result<BackupEntry, UpdateError> {
    let backup_dir = get_backup_dir(version)?;

    // Backup the binaries
    backup_current_installation(install_dir, &backup_dir)?;

    // Create metadata
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);

    let entry = BackupEntry {
        version: version.to_string(),
        created_at: now,
        original_path: install_dir.to_path_buf(),
        backup_path: backup_dir.clone(),
    };

    // Write metadata JSON
    let metadata_path = backup_dir.join("backup.json");
    let json = serde_json::to_string_pretty(&entry).map_err(|e| {
        UpdateError::InstallFailed(format!("Failed to serialize backup metadata: {}", e))
    })?;
    fs::write(&metadata_path, json).map_err(|e| {
        UpdateError::InstallFailed(format!("Failed to write backup metadata: {}", e))
    })?;

    // Prune old backups
    prune_old_backups()?;

    Ok(entry)
}

/// List all available backups with metadata.
pub fn list_backups() -> Result<Vec<BackupEntry>, UpdateError> {
    let data_dir = dirs::data_dir().ok_or_else(|| {
        UpdateError::InstallFailed("Could not determine data directory".to_string())
    })?;

    let backup_base = data_dir.join("rch/backups");

    if !backup_base.exists() {
        return Ok(Vec::new());
    }

    let mut backups = Vec::new();

    let entries = fs::read_dir(&backup_base)
        .map_err(|e| UpdateError::InstallFailed(format!("Failed to read backup dir: {}", e)))?;

    for entry in entries.filter_map(|e| e.ok()) {
        if !entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false) {
            continue;
        }

        let backup_path = entry.path();
        let metadata_path = backup_path.join("backup.json");

        if metadata_path.exists() {
            // Read metadata from JSON
            if let Ok(content) = fs::read_to_string(&metadata_path)
                && let Ok(mut backup_entry) = serde_json::from_str::<BackupEntry>(&content)
            {
                backup_entry.backup_path = backup_path;
                backups.push(backup_entry);
            }
        } else {
            // Legacy backup without metadata - extract info from directory name
            let dir_name = entry.file_name().to_string_lossy().to_string();
            if let Some(version) = dir_name.strip_prefix('v') {
                // Format: v{version}-{timestamp}
                let version_part = version.split('-').next().unwrap_or(version);
                let created_at = fs::metadata(&backup_path)
                    .and_then(|m| m.modified())
                    .ok()
                    .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
                    .map(|d| d.as_secs())
                    .unwrap_or(0);

                backups.push(BackupEntry {
                    version: version_part.to_string(),
                    created_at,
                    original_path: PathBuf::new(),
                    backup_path,
                });
            }
        }
    }

    // Sort by creation time, newest first. Use the path as a deterministic
    // tie-breaker because metadata stores seconds, while backup names carry
    // subsecond time and a nonce.
    backups.sort_by(|left, right| {
        right
            .created_at
            .cmp(&left.created_at)
            .then_with(|| right.backup_path.cmp(&left.backup_path))
    });

    Ok(backups)
}

/// Prune old backups, keeping only MAX_BACKUPS.
pub fn prune_old_backups() -> Result<(), UpdateError> {
    let backups = list_backups()?;

    if backups.len() <= MAX_BACKUPS {
        return Ok(());
    }

    // Remove oldest backups (list is sorted newest-first)
    for backup in backups.iter().skip(MAX_BACKUPS) {
        if backup.backup_path.exists() {
            let _ = fs::remove_dir_all(&backup.backup_path);
        }
    }

    Ok(())
}

fn new_update_extract_dir() -> PathBuf {
    std::env::temp_dir().join(format!("rch-extract-{}", uuid::Uuid::new_v4()))
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum UpdateArchiveFormat {
    TarGz,
    Zip,
}

struct UpdateExtractDir {
    path: PathBuf,
}

impl UpdateExtractDir {
    fn new() -> Self {
        Self {
            path: new_update_extract_dir(),
        }
    }

    fn path(&self) -> &Path {
        &self.path
    }
}

impl Drop for UpdateExtractDir {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.path);
    }
}

/// Extract archive to destination.
fn extract_archive(archive: &std::path::Path, dest: &std::path::Path) -> Result<(), UpdateError> {
    let format = update_archive_format(archive)?;

    std::fs::create_dir(dest)
        .map_err(|e| UpdateError::InstallFailed(format!("Failed to create extract dir: {}", e)))?;

    let archive_file = fs::File::open(archive).map_err(|e| {
        UpdateError::InstallFailed(format!(
            "Failed to open update archive {}: {}",
            archive.display(),
            e
        ))
    })?;

    match format {
        UpdateArchiveFormat::TarGz => extract_tar_gz_archive(archive_file, dest),
        UpdateArchiveFormat::Zip => extract_zip_archive(archive_file, dest),
    }
}

fn update_archive_format(archive: &Path) -> Result<UpdateArchiveFormat, UpdateError> {
    let Some(file_name) = archive.file_name().and_then(|name| name.to_str()) else {
        return Err(UpdateError::InstallFailed(format!(
            "Unsupported update archive path: {}",
            archive.display()
        )));
    };

    if file_name.ends_with(".tar.gz") || file_name.ends_with(".tgz") {
        Ok(UpdateArchiveFormat::TarGz)
    } else if file_name.ends_with(".zip") {
        Ok(UpdateArchiveFormat::Zip)
    } else {
        Err(UpdateError::InstallFailed(format!(
            "Unsupported update archive format: {}",
            archive.display()
        )))
    }
}

fn extract_tar_gz_archive(archive_file: fs::File, dest: &Path) -> Result<(), UpdateError> {
    let decoder = GzDecoder::new(archive_file);
    let mut tar = tar::Archive::new(decoder);
    let entries = tar
        .entries()
        .map_err(|e| UpdateError::InstallFailed(format!("Failed to read archive: {}", e)))?;

    for entry in entries {
        let mut entry = entry
            .map_err(|e| UpdateError::InstallFailed(format!("Invalid archive entry: {}", e)))?;
        let entry_type = entry.header().entry_type();
        let entry_path = entry
            .path()
            .map_err(|e| UpdateError::InstallFailed(format!("Invalid archive path: {}", e)))?;
        let relative_path = safe_archive_entry_path(&entry_path)?;
        let destination = dest.join(&relative_path);

        if entry_type.is_dir() {
            fs::create_dir_all(&destination).map_err(|e| {
                UpdateError::InstallFailed(format!(
                    "Failed to create extracted directory {}: {}",
                    relative_path.display(),
                    e
                ))
            })?;
        } else if entry_type.is_file() {
            unpack_regular_file(&mut entry, &relative_path, &destination)?;
        } else {
            return Err(UpdateError::InstallFailed(format!(
                "Unsupported update archive entry type for {}",
                relative_path.display()
            )));
        }
    }

    Ok(())
}

fn extract_zip_archive(archive_file: fs::File, dest: &Path) -> Result<(), UpdateError> {
    let mut zip = zip::ZipArchive::new(archive_file)
        .map_err(|e| UpdateError::InstallFailed(format!("Failed to read zip archive: {}", e)))?;

    for index in 0..zip.len() {
        let mut entry = zip
            .by_index(index)
            .map_err(|e| UpdateError::InstallFailed(format!("Invalid zip archive entry: {}", e)))?;
        let entry_name = entry.name().to_string();
        let relative_path = safe_archive_entry_path(Path::new(&entry_name))?;
        let destination = dest.join(&relative_path);

        if entry.is_dir() {
            fs::create_dir_all(&destination).map_err(|e| {
                UpdateError::InstallFailed(format!(
                    "Failed to create extracted directory {}: {}",
                    relative_path.display(),
                    e
                ))
            })?;
        } else if entry.is_file() {
            unpack_regular_file(&mut entry, &relative_path, &destination)?;
        } else {
            return Err(UpdateError::InstallFailed(format!(
                "Unsupported update zip entry type for {}",
                relative_path.display()
            )));
        }
    }

    Ok(())
}

fn safe_archive_entry_path(path: &Path) -> Result<PathBuf, UpdateError> {
    let mut safe_path = PathBuf::new();

    for component in path.components() {
        match component {
            Component::Normal(part) => {
                let Some(part) = part.to_str() else {
                    return Err(UpdateError::InstallFailed(format!(
                        "Archive entry path is not UTF-8: {}",
                        path.display()
                    )));
                };
                if part.contains('\\') || part.contains('\0') {
                    return Err(UpdateError::InstallFailed(format!(
                        "Unsafe archive entry path: {}",
                        path.display()
                    )));
                }
                safe_path.push(part);
            }
            Component::CurDir => {}
            Component::ParentDir | Component::RootDir | Component::Prefix(_) => {
                return Err(UpdateError::InstallFailed(format!(
                    "Unsafe archive entry path: {}",
                    path.display()
                )));
            }
        }
    }

    if safe_path.as_os_str().is_empty() {
        return Err(UpdateError::InstallFailed(
            "Archive entry path is empty".to_string(),
        ));
    }

    Ok(safe_path)
}

fn unpack_regular_file<R: Read>(
    entry: &mut R,
    relative_path: &Path,
    destination: &Path,
) -> Result<(), UpdateError> {
    if let Some(parent) = destination.parent() {
        fs::create_dir_all(parent).map_err(|e| {
            UpdateError::InstallFailed(format!(
                "Failed to create extracted parent for {}: {}",
                relative_path.display(),
                e
            ))
        })?;
    }

    let mut output = fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(destination)
        .map_err(|e| {
            UpdateError::InstallFailed(format!(
                "Failed to create extracted file {}: {}",
                relative_path.display(),
                e
            ))
        })?;
    io::copy(entry, &mut output).map_err(|e| {
        UpdateError::InstallFailed(format!(
            "Failed to extract file {}: {}",
            relative_path.display(),
            e
        ))
    })?;

    Ok(())
}

fn discover_extracted_update_binaries(
    src_dir: &std::path::Path,
) -> Result<Vec<&'static str>, UpdateError> {
    let mut found = Vec::new();
    for binary in UPDATE_BINARIES {
        let src = src_dir.join(binary);
        match std::fs::symlink_metadata(&src) {
            Ok(metadata) if metadata.file_type().is_file() => found.push(*binary),
            Ok(_) => {
                return Err(UpdateError::InstallFailed(format!(
                    "Extracted update entry '{}' is not a regular file",
                    src.display()
                )));
            }
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
            Err(e) => {
                return Err(UpdateError::InstallFailed(format!(
                    "Failed to inspect extracted {}: {}",
                    binary, e
                )));
            }
        }
    }

    if !found.contains(&REQUIRED_UPDATE_BINARY) {
        return Err(UpdateError::InstallFailed(format!(
            "Extracted update archive did not contain required '{}' binary in {}",
            REQUIRED_UPDATE_BINARY,
            src_dir.display()
        )));
    }

    Ok(found)
}

/// Owned, destination-local payloads. Every copy and permission change finishes
/// before the first installed name changes. The source archive/backup is never
/// read again during publication, and failed preparation cleans only this stage.
struct StagedBinaries {
    directory: tempfile::TempDir,
    install_dir: PathBuf,
    binaries: Vec<&'static str>,
}

impl StagedBinaries {
    fn new(src_dir: &Path, install_dir: &Path) -> Result<Self, UpdateError> {
        let binaries = discover_extracted_update_binaries(src_dir)?;
        for binary in &binaries {
            let destination = install_dir.join(binary);
            match fs::symlink_metadata(&destination) {
                Ok(metadata) if metadata.is_file() => {}
                Err(error) if error.kind() == io::ErrorKind::NotFound => {}
                Ok(_) => {
                    return Err(UpdateError::InstallFailed(format!(
                        "Refusing to replace non-regular installed entry {}",
                        destination.display()
                    )));
                }
                Err(error) => {
                    return Err(UpdateError::InstallFailed(format!(
                        "Cannot inspect installed entry {}: {error}",
                        destination.display()
                    )));
                }
            }
        }
        fs::create_dir_all(install_dir).map_err(|error| {
            UpdateError::InstallFailed(format!("Failed to create install dir: {error}"))
        })?;
        let directory = tempfile::Builder::new()
            .prefix(".rch-update-stage-")
            .tempdir_in(install_dir)
            .map_err(|error| {
                UpdateError::InstallFailed(format!("Failed to create update stage: {error}"))
            })?;
        for binary in &binaries {
            let staged = directory.path().join(binary);
            copy_regular_binary_payload(&src_dir.join(binary), &staged, binary, "stage")?;
            set_update_binary_permissions(&staged)?;
            fs::OpenOptions::new()
                .read(true)
                .write(true)
                .open(&staged)
                .and_then(|file| file.sync_all())
                .map_err(|error| {
                    UpdateError::InstallFailed(format!("Failed to sync staged {binary}: {error}"))
                })?;
        }
        Ok(Self {
            directory,
            install_dir: install_dir.to_owned(),
            binaries,
        })
    }

    async fn verify(&self, version: &str, timeout: std::time::Duration) -> Result<(), UpdateError> {
        for binary in UPDATE_BINARIES {
            let path = if self.binaries.contains(binary) {
                self.directory.path().join(binary)
            } else {
                let path = self.install_dir.join(binary);
                if !binary_path_exists(&path, binary, "retained companion")? {
                    continue;
                }
                path
            };
            // Optional companions can be absent, not silently kept at another
            // release version. No PATH lookup or unbounded blocking child.
            let name = binary.strip_suffix(".exe").unwrap_or(binary);
            super::probe_installed_version(&path, name, version, timeout)
                .await
                .map_err(|error| {
                    UpdateError::InstallFailed(format!(
                        "Update preflight for {binary} failed: {error:#}"
                    ))
                })?;
        }
        Ok(())
    }
}

async fn prepare_release(
    archive: &Path,
    install_dir: &Path,
    version: &str,
    timeout: std::time::Duration,
) -> Result<StagedBinaries, UpdateError> {
    let extracted = UpdateExtractDir::new();
    extract_archive(archive, extracted.path())?;
    let staged = StagedBinaries::new(extracted.path(), install_dir)?;
    staged.verify(version, timeout).await?;
    Ok(staged)
}

/// Publish the already prepared set without any further source copies.
fn replace_binaries(prepared: StagedBinaries) -> Result<Vec<&'static str>, UpdateError> {
    for binary in &prepared.binaries {
        let src = prepared.directory.path().join(binary);
        let dst = prepared.install_dir.join(binary);
        replace_staged_binary(&src, &dst, binary)?;
    }
    Ok(prepared.binaries)
}

#[cfg(unix)]
fn replace_staged_binary(staged: &Path, dst: &Path, binary: &str) -> Result<(), UpdateError> {
    std::fs::rename(staged, dst).map_err(|e| {
        let _ = std::fs::remove_file(staged);
        UpdateError::InstallFailed(format!("Failed to install {}: {}", binary, e))
    })
}

#[cfg(windows)]
fn replace_staged_binary(staged: &Path, dst: &Path, binary: &str) -> Result<(), UpdateError> {
    if dst.exists() {
        std::fs::remove_file(dst).map_err(|e| {
            let _ = std::fs::remove_file(staged);
            UpdateError::InstallFailed(format!("Failed to remove old {}: {}", binary, e))
        })?;
    }

    std::fs::rename(staged, dst).map_err(|e| {
        let _ = std::fs::remove_file(staged);
        UpdateError::InstallFailed(format!("Failed to install {}: {}", binary, e))
    })
}

#[cfg(unix)]
fn set_update_binary_permissions(path: &Path) -> Result<(), UpdateError> {
    use std::os::unix::fs::PermissionsExt;
    let mut perms = std::fs::metadata(path)
        .map_err(|e| UpdateError::InstallFailed(format!("Failed to get permissions: {}", e)))?
        .permissions();
    perms.set_mode(0o755);
    std::fs::set_permissions(path, perms)
        .map_err(|e| UpdateError::InstallFailed(format!("Failed to set permissions: {}", e)))
}

#[cfg(windows)]
fn set_update_binary_permissions(_path: &Path) -> Result<(), UpdateError> {
    Ok(())
}

fn binary_path_exists(path: &Path, binary: &str, source_name: &str) -> Result<bool, UpdateError> {
    match std::fs::symlink_metadata(path) {
        Ok(_) => Ok(true),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false),
        Err(e) => Err(UpdateError::InstallFailed(format!(
            "Failed to inspect {} {} at {}: {}",
            source_name,
            binary,
            path.display(),
            e
        ))),
    }
}

fn copy_regular_binary_payload(
    src: &Path,
    dst: &Path,
    binary: &str,
    action: &str,
) -> Result<(), UpdateError> {
    let metadata = std::fs::symlink_metadata(src).map_err(|e| {
        UpdateError::InstallFailed(format!(
            "Failed to inspect {} at {} before {}: {}",
            binary,
            src.display(),
            action,
            e
        ))
    })?;

    if !metadata.file_type().is_file() {
        return Err(UpdateError::InstallFailed(format!(
            "Refusing to {} {} from non-regular file {}",
            action,
            binary,
            src.display()
        )));
    }

    std::fs::copy(src, dst).map_err(|e| {
        UpdateError::InstallFailed(format!("Failed to {} {}: {}", action, binary, e))
    })?;

    Ok(())
}

/// Stop daemon gracefully, waiting for builds to complete.
#[cfg(not(unix))]
async fn stop_daemon_gracefully(_timeout_secs: u64) -> Result<bool, UpdateError> {
    Ok(false)
}

/// Stop daemon gracefully, waiting for builds to complete.
#[cfg(unix)]
async fn stop_daemon_gracefully(timeout_secs: u64) -> Result<bool, UpdateError> {
    let socket_path = configured_update_socket_path()?;
    // Both installation and rollback call this before modifying any binaries.
    // The interactive stop path can force a process-level stop; an update
    // never has that authority, even when its drain timeout has elapsed.
    daemon_shutdown::stop(&socket_path, std::time::Duration::from_secs(timeout_secs))
        .await
        .map_err(UpdateError::InstallFailed)
}

/// Restart only the installed sibling and require an operational API. Spawning
/// a launcher (which may delegate to a service manager) is not readiness.
async fn start_daemon(
    install_dir: &Path,
    expected_version: Option<&str>,
) -> Result<(), UpdateError> {
    let socket_path = configured_update_socket_path()?;
    let mut command = daemon_start_command(install_dir);

    // Preserve custom socket configuration across update and rollback restarts.
    command.args(daemon_start_args(&socket_path));
    #[cfg(unix)]
    {
        daemon_shutdown::start(command, &socket_path, expected_version)
            .await
            .map_err(UpdateError::InstallFailed)
    }
    #[cfg(not(unix))]
    {
        let _ = (command, expected_version);
        Err(UpdateError::InstallFailed(
            "Confirmed daemon restart is unsupported on this platform".into(),
        ))
    }
}

#[cfg(not(windows))]
fn daemon_start_command(install_dir: &Path) -> Command {
    Command::new(install_dir.join("rchd"))
}

#[cfg(windows)]
fn daemon_start_command(install_dir: &Path) -> Command {
    Command::new(install_dir.join("rchd.exe"))
}

fn configured_update_socket_path() -> Result<PathBuf, UpdateError> {
    configured_socket_path()
        .map(PathBuf::from)
        .map_err(|e| UpdateError::InstallFailed(format!("Failed to resolve daemon socket: {e}")))
}

fn daemon_start_args(socket_path: &Path) -> [&std::ffi::OsStr; 2] {
    [std::ffi::OsStr::new("--socket"), socket_path.as_os_str()]
}

#[cfg(test)]
mod tests {
    use super::*;
    use flate2::Compression;
    use flate2::write::GzEncoder;
    use std::io::Cursor;
    use std::io::Write;
    use tempfile::TempDir;

    fn create_tar_gz_with_entries(
        archive_path: &Path,
        entries: &[(&str, &[u8])],
    ) -> Result<(), Box<dyn std::error::Error>> {
        let file = fs::File::create(archive_path)?;
        let encoder = GzEncoder::new(file, Compression::default());
        let mut builder = tar::Builder::new(encoder);

        for (path, content) in entries {
            let mut header = tar::Header::new_gnu();
            header.set_path(path)?;
            header.set_size(u64::try_from(content.len())?);
            header.set_cksum();
            builder.append(&header, Cursor::new(*content))?;
        }

        builder.finish()?;
        let encoder = builder.into_inner()?;
        encoder.finish()?;
        Ok(())
    }

    fn create_zip_with_entries(
        archive_path: &Path,
        entries: &[(&str, &[u8])],
    ) -> Result<(), Box<dyn std::error::Error>> {
        let file = fs::File::create(archive_path)?;
        let mut writer = zip::ZipWriter::new(file);

        for (path, content) in entries {
            let options = zip::write::SimpleFileOptions::default()
                .compression_method(zip::CompressionMethod::Deflated);
            writer.start_file(*path, options)?;
            writer.write_all(content)?;
        }

        writer.finish()?;
        Ok(())
    }

    fn optional_update_binary() -> &'static str {
        UPDATE_BINARIES
            .iter()
            .copied()
            .find(|binary| *binary != REQUIRED_UPDATE_BINARY)
            .expect("update binary list should include at least one optional binary")
    }

    fn stage_and_replace(src: &Path, dst: &Path) -> Result<Vec<&'static str>, UpdateError> {
        replace_binaries(StagedBinaries::new(src, dst)?)
    }

    #[tokio::test]
    async fn update_preflight_refuses_bad_archives_without_changing_installation() {
        let temp = TempDir::new().unwrap();
        let install = temp.path().join("install");
        fs::create_dir(&install).unwrap();
        fs::write(
            install.join(REQUIRED_UPDATE_BINARY),
            b"working installation",
        )
        .unwrap();
        let archive = temp.path().join("rch.tar.gz");
        fs::write(&archive, b"not a gzip archive").unwrap();
        assert!(
            prepare_release(
                &archive,
                &install,
                "9.1.2",
                std::time::Duration::from_secs(1)
            )
            .await
            .is_err()
        );
        create_tar_gz_with_entries(&archive, &[(optional_update_binary(), b"no client")]).unwrap();
        assert!(
            prepare_release(
                &archive,
                &install,
                "9.1.2",
                std::time::Duration::from_secs(1)
            )
            .await
            .is_err()
        );
        assert_eq!(
            fs::read(install.join(REQUIRED_UPDATE_BINARY)).unwrap(),
            b"working installation"
        );
        assert_eq!(fs::read_dir(&install).unwrap().count(), 1);
    }

    #[test]
    fn update_preflight_rejects_late_invalid_destination_before_first_replacement() {
        let temp = TempDir::new().unwrap();
        let source = temp.path().join("source");
        let install = temp.path().join("install");
        fs::create_dir(&source).unwrap();
        fs::create_dir_all(install.join(optional_update_binary())).unwrap();
        fs::write(source.join(REQUIRED_UPDATE_BINARY), b"new client").unwrap();
        fs::write(source.join(optional_update_binary()), b"new companion").unwrap();
        fs::write(install.join(REQUIRED_UPDATE_BINARY), b"old client").unwrap();
        assert!(stage_and_replace(&source, &install).is_err());
        assert_eq!(
            fs::read(install.join(REQUIRED_UPDATE_BINARY)).unwrap(),
            b"old client"
        );
        assert!(install.join(optional_update_binary()).is_dir());
    }

    #[test]
    fn update_preflight_publishes_staged_bytes_not_reopened_source_files() {
        let temp = TempDir::new().unwrap();
        let source = temp.path().join("source");
        let install = temp.path().join("install");
        fs::create_dir(&source).unwrap();
        for binary in UPDATE_BINARIES {
            fs::write(source.join(binary), format!("prepared {binary}")).unwrap();
        }
        let staged = StagedBinaries::new(&source, &install).unwrap();
        let stage_path = staged.directory.path().to_owned();
        for binary in UPDATE_BINARIES {
            assert!(stage_path.join(binary).is_file());
            assert!(!install.join(binary).exists());
            fs::write(source.join(binary), b"changed after staging").unwrap();
        }
        assert_eq!(replace_binaries(staged).unwrap(), UPDATE_BINARIES);
        for binary in UPDATE_BINARIES {
            assert_eq!(
                fs::read(install.join(binary)).unwrap(),
                format!("prepared {binary}").as_bytes()
            );
        }
        assert!(!stage_path.exists());
    }

    #[test]
    fn update_preflight_empty_rollback_payload_does_not_report_success() {
        let temp = TempDir::new().unwrap();
        let source = temp.path().join("backup");
        let install = temp.path().join("install");
        fs::create_dir(&source).unwrap();
        fs::create_dir(&install).unwrap();
        fs::write(
            install.join(REQUIRED_UPDATE_BINARY),
            b"working installation",
        )
        .unwrap();
        assert!(stage_and_replace(&source, &install).is_err());
        assert_eq!(
            fs::read(install.join(REQUIRED_UPDATE_BINARY)).unwrap(),
            b"working installation"
        );
    }

    #[cfg(unix)]
    fn version_payload(name: &str, version: &str) -> Vec<u8> {
        format!("#!/bin/sh\n[ \"$1\" = --version ] || exit 91\nprintf '%s\\n' '{name} {version}'\n")
            .into_bytes()
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn update_preflight_verifies_every_staged_component_before_publication() {
        let temp = TempDir::new().unwrap();
        let archive = temp.path().join("rch.tar.gz");
        let install = temp.path().join("install");
        fs::create_dir(&install).unwrap();
        let payloads: Vec<_> = UPDATE_BINARIES
            .iter()
            .map(|name| (*name, version_payload(name, "9.1.2")))
            .collect();
        let entries: Vec<_> = payloads
            .iter()
            .map(|(name, bytes)| (*name, bytes.as_slice()))
            .collect();
        create_tar_gz_with_entries(&archive, &entries).unwrap();
        for binary in UPDATE_BINARIES {
            fs::write(install.join(binary), b"old binary").unwrap();
        }
        let staged = prepare_release(
            &archive,
            &install,
            "9.1.2",
            std::time::Duration::from_secs(1),
        )
        .await
        .unwrap();
        for binary in UPDATE_BINARIES {
            assert_eq!(fs::read(install.join(binary)).unwrap(), b"old binary");
        }
        replace_binaries(staged).unwrap();
        super::super::inspect_installation(
            &install.join(REQUIRED_UPDATE_BINARY),
            "9.1.2",
            std::time::Duration::from_secs(1),
        )
        .await
        .unwrap();
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn update_preflight_refuses_wrong_failed_noisy_and_hanging_companions() {
        let cases = [
            version_payload("rchd", "9.1.1"),
            version_payload("unrelated", "9.1.2"),
            b"#!/bin/sh\nexit 0\n".to_vec(),
            b"#!/bin/sh\nprintf 'rchd 9.1.2\\n'\nexit 7\n".to_vec(),
            b"#!/bin/sh\nprintf 'rchd 9.1.2\\n'\nprintf 'broken\\n' >&2\n".to_vec(),
            b"#!/bin/sh\nexec sleep 5\n".to_vec(),
        ];
        for (index, companion) in cases.iter().enumerate() {
            let temp = TempDir::new().unwrap();
            let archive = temp.path().join("rch.tar.gz");
            let install = temp.path().join("install");
            fs::create_dir(&install).unwrap();
            fs::write(install.join("rch"), b"old client").unwrap();
            fs::write(install.join("rchd"), b"old daemon").unwrap();
            create_tar_gz_with_entries(
                &archive,
                &[
                    ("rch", &version_payload("rch", "9.1.2")),
                    ("rchd", companion),
                ],
            )
            .unwrap();
            let result = prepare_release(
                &archive,
                &install,
                "9.1.2",
                std::time::Duration::from_secs(1),
            )
            .await;
            let error = result.err().expect("invalid companion must be refused");
            assert!(
                error.to_string().contains("preflight for rchd failed"),
                "case {index} failed before reaching the companion probe: {error}"
            );
            assert_eq!(fs::read(install.join("rch")).unwrap(), b"old client");
            assert_eq!(fs::read(install.join("rchd")).unwrap(), b"old daemon");
            assert_eq!(fs::read_dir(&install).unwrap().count(), 2);
        }
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn update_preflight_retains_only_matching_optional_companions() {
        let temp = TempDir::new().unwrap();
        let archive = temp.path().join("rch.tar.gz");
        let install = temp.path().join("install");
        fs::create_dir(&install).unwrap();
        fs::write(install.join("rch"), b"old client").unwrap();
        create_tar_gz_with_entries(&archive, &[("rch", &version_payload("rch", "9.1.2"))]).unwrap();
        // An actually absent companion is supported, as before.
        drop(
            prepare_release(
                &archive,
                &install,
                "9.1.2",
                std::time::Duration::from_secs(1),
            )
            .await
            .unwrap(),
        );
        let companion = install.join("rchd");
        fs::write(&companion, version_payload("rchd", "9.1.1")).unwrap();
        set_update_binary_permissions(&companion).unwrap();
        assert!(
            prepare_release(
                &archive,
                &install,
                "9.1.2",
                std::time::Duration::from_secs(1)
            )
            .await
            .is_err()
        );
        assert_eq!(fs::read(install.join("rch")).unwrap(), b"old client");
        fs::write(&companion, version_payload("rchd", "9.1.2")).unwrap();
        let staged = prepare_release(
            &archive,
            &install,
            "9.1.2",
            std::time::Duration::from_secs(1),
        )
        .await
        .unwrap();
        assert_eq!(staged.binaries, vec!["rch"]);
    }

    fn create_tar_gz_with_raw_path(
        archive_path: &Path,
        raw_path: &[u8],
        content: &[u8],
    ) -> Result<(), Box<dyn std::error::Error>> {
        let file = fs::File::create(archive_path)?;
        let encoder = GzEncoder::new(file, Compression::default());
        let mut builder = tar::Builder::new(encoder);
        let mut header = tar::Header::new_gnu();
        header.set_entry_type(tar::EntryType::Regular);
        header.set_size(u64::try_from(content.len())?);
        header.set_mode(0o755);
        let path_field = header
            .as_mut_bytes()
            .get_mut(0..raw_path.len())
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "raw tar path too long"))?;
        path_field.copy_from_slice(raw_path);
        header.set_cksum();
        builder.append(&header, Cursor::new(content))?;
        builder.finish()?;
        let encoder = builder.into_inner()?;
        encoder.finish()?;
        Ok(())
    }

    fn create_tar_gz_with_symlink(
        archive_path: &Path,
        link_path: &str,
        target_path: &str,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let file = fs::File::create(archive_path)?;
        let encoder = GzEncoder::new(file, Compression::default());
        let mut builder = tar::Builder::new(encoder);
        let mut header = tar::Header::new_gnu();
        header.set_entry_type(tar::EntryType::Symlink);
        header.set_path(link_path)?;
        header.set_link_name(target_path)?;
        header.set_size(0);
        header.set_cksum();
        builder.append(&header, io::empty())?;
        builder.finish()?;
        let encoder = builder.into_inner()?;
        encoder.finish()?;
        Ok(())
    }

    #[test]
    fn test_get_install_dir() {
        let dir = get_install_dir().unwrap();
        assert!(dir.is_absolute());
    }

    #[test]
    fn test_backup_dir_has_timestamp() {
        let dir = get_backup_dir("0.1.0").unwrap();
        let name = dir.file_name().unwrap().to_str().unwrap();
        assert!(name.starts_with("v0.1.0-"));
        assert!(name.len() > "v0.1.0-".len()); // Has timestamp
    }

    #[test]
    fn test_backup_dir_is_unique_for_same_version() {
        let first = get_backup_dir("0.1.0").unwrap();
        let second = get_backup_dir("0.1.0").unwrap();

        assert_ne!(
            first, second,
            "same-version backups created in rapid succession must not share a directory"
        );
    }

    #[test]
    fn test_backup_and_restore() {
        let temp = TempDir::new().unwrap();
        let install_dir = temp.path().join("install");
        let backup_dir = temp.path().join("backup");

        std::fs::create_dir_all(&install_dir).unwrap();
        std::fs::write(install_dir.join(REQUIRED_UPDATE_BINARY), "test binary").unwrap();

        backup_current_installation(&install_dir, &backup_dir).unwrap();
        assert!(backup_dir.join(REQUIRED_UPDATE_BINARY).exists());

        // Modify the original
        std::fs::write(install_dir.join(REQUIRED_UPDATE_BINARY), "modified").unwrap();

        // Restore
        stage_and_replace(&backup_dir, &install_dir).unwrap();

        let content = std::fs::read_to_string(install_dir.join(REQUIRED_UPDATE_BINARY)).unwrap();
        assert_eq!(content, "test binary");
    }

    #[test]
    fn test_backup_creates_directory() {
        let temp = TempDir::new().unwrap();
        let install_dir = temp.path().join("install");
        let backup_dir = temp.path().join("backup/nested/deep");

        std::fs::create_dir_all(&install_dir).unwrap();
        std::fs::write(install_dir.join(REQUIRED_UPDATE_BINARY), "test").unwrap();

        // Backup directory doesn't exist yet
        assert!(!backup_dir.exists());

        backup_current_installation(&install_dir, &backup_dir).unwrap();

        // Should have created the directory
        assert!(backup_dir.exists());
        assert!(backup_dir.join(REQUIRED_UPDATE_BINARY).exists());
    }

    #[test]
    fn test_backup_skips_missing_binaries() {
        let temp = TempDir::new().unwrap();
        let install_dir = temp.path().join("install");
        let backup_dir = temp.path().join("backup");

        std::fs::create_dir_all(&install_dir).unwrap();
        // Only create the required binary, not the optional companions.
        std::fs::write(install_dir.join(REQUIRED_UPDATE_BINARY), "test").unwrap();

        backup_current_installation(&install_dir, &backup_dir).unwrap();

        // Only rch should be backed up
        assert!(backup_dir.join(REQUIRED_UPDATE_BINARY).exists());
        for binary in UPDATE_BINARIES
            .iter()
            .copied()
            .filter(|binary| *binary != REQUIRED_UPDATE_BINARY)
        {
            assert!(!backup_dir.join(binary).exists());
        }
    }

    #[cfg(unix)]
    #[test]
    fn test_backup_rejects_symlinked_installed_binary() {
        use std::os::unix::fs::symlink;

        let temp = TempDir::new().unwrap();
        let install_dir = temp.path().join("install");
        let backup_dir = temp.path().join("backup");
        let external_binary = temp.path().join("external-rch");

        std::fs::create_dir_all(&install_dir).unwrap();
        std::fs::write(&external_binary, "external binary").unwrap();
        symlink(&external_binary, install_dir.join(REQUIRED_UPDATE_BINARY)).unwrap();

        let result = backup_current_installation(&install_dir, &backup_dir);

        assert!(matches!(result, Err(UpdateError::InstallFailed(_))));
        assert!(
            !backup_dir.join(REQUIRED_UPDATE_BINARY).exists(),
            "backup must not follow installed binary symlinks"
        );
    }

    #[test]
    fn test_backup_all_binaries() {
        let temp = TempDir::new().unwrap();
        let install_dir = temp.path().join("install");
        let backup_dir = temp.path().join("backup");

        std::fs::create_dir_all(&install_dir).unwrap();
        for binary in UPDATE_BINARIES {
            std::fs::write(install_dir.join(binary), format!("content for {binary}")).unwrap();
        }

        backup_current_installation(&install_dir, &backup_dir).unwrap();

        // All three should be backed up
        for binary in UPDATE_BINARIES {
            assert!(backup_dir.join(binary).exists());
        }

        // Verify content
        assert_eq!(
            std::fs::read_to_string(backup_dir.join(REQUIRED_UPDATE_BINARY)).unwrap(),
            format!("content for {REQUIRED_UPDATE_BINARY}")
        );
    }

    #[test]
    fn test_replace_binaries_creates_install_dir() {
        let temp = TempDir::new().unwrap();
        let src_dir = temp.path().join("src");
        let install_dir = temp.path().join("install/nested");

        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::write(src_dir.join(REQUIRED_UPDATE_BINARY), "new binary").unwrap();

        // Install directory doesn't exist
        assert!(!install_dir.exists());

        let installed = stage_and_replace(&src_dir, &install_dir).unwrap();

        // Should have created it
        assert_eq!(installed, vec![REQUIRED_UPDATE_BINARY]);
        assert!(install_dir.join(REQUIRED_UPDATE_BINARY).exists());
    }

    #[test]
    fn test_replace_binaries_requires_rch_payload() {
        let temp = TempDir::new().unwrap();
        let src_dir = temp.path().join("src");
        let install_dir = temp.path().join("install");

        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::create_dir_all(&install_dir).unwrap();
        let optional_binary = optional_update_binary();
        std::fs::write(src_dir.join(optional_binary), "new optional binary").unwrap();
        std::fs::write(
            install_dir.join(REQUIRED_UPDATE_BINARY),
            "old required binary",
        )
        .unwrap();

        let result = stage_and_replace(&src_dir, &install_dir);
        assert!(matches!(result, Err(UpdateError::InstallFailed(_))));
        assert_eq!(
            std::fs::read_to_string(install_dir.join(REQUIRED_UPDATE_BINARY)).unwrap(),
            "old required binary",
            "missing required payload must fail before touching installed binaries"
        );
        assert!(
            !install_dir.join(optional_binary).exists(),
            "preflight failure must not partially install optional binaries"
        );
    }

    #[test]
    fn test_replace_binaries_replaces_existing_binary_after_staging() {
        let temp = TempDir::new().unwrap();
        let src_dir = temp.path().join("src");
        let install_dir = temp.path().join("install");

        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::create_dir_all(&install_dir).unwrap();
        std::fs::write(src_dir.join(REQUIRED_UPDATE_BINARY), "new binary").unwrap();
        std::fs::write(install_dir.join(REQUIRED_UPDATE_BINARY), "old binary").unwrap();

        let installed = stage_and_replace(&src_dir, &install_dir).unwrap();

        assert_eq!(installed, vec![REQUIRED_UPDATE_BINARY]);
        assert_eq!(
            std::fs::read_to_string(install_dir.join(REQUIRED_UPDATE_BINARY)).unwrap(),
            "new binary"
        );
        assert!(
            std::fs::read_dir(&install_dir)
                .unwrap()
                .filter_map(Result::ok)
                .all(|entry| !entry.file_name().to_string_lossy().contains(".rch-update-")),
            "successful replacement should not leave staged update files"
        );
    }

    #[test]
    fn test_replace_binaries_rejects_non_file_payload() {
        let temp = TempDir::new().unwrap();
        let src_dir = temp.path().join("src");
        let install_dir = temp.path().join("install");

        std::fs::create_dir_all(src_dir.join(REQUIRED_UPDATE_BINARY)).unwrap();

        let result = stage_and_replace(&src_dir, &install_dir);
        assert!(matches!(result, Err(UpdateError::InstallFailed(_))));
        assert!(
            !install_dir.exists(),
            "invalid payload must fail before creating the install dir"
        );
    }

    #[test]
    fn test_new_update_extract_dir_is_unique() {
        let first = new_update_extract_dir();
        let second = new_update_extract_dir();
        assert_ne!(first, second);
        assert!(
            first
                .file_name()
                .unwrap()
                .to_string_lossy()
                .starts_with("rch-extract-")
        );
    }

    #[test]
    fn test_update_extract_dir_cleans_on_drop() {
        let path = {
            let dir = UpdateExtractDir::new();
            fs::create_dir(dir.path()).unwrap();
            fs::write(dir.path().join("partial"), "partial extraction").unwrap();
            dir.path().to_path_buf()
        };

        assert!(
            !path.exists(),
            "temporary extraction directory should be removed on drop"
        );
    }

    #[test]
    fn test_update_archive_format_accepts_release_formats() {
        assert_eq!(
            update_archive_format(Path::new("rch-v1.0.0-x86_64-unknown-linux-musl.tar.gz"))
                .unwrap(),
            UpdateArchiveFormat::TarGz
        );
        assert_eq!(
            update_archive_format(Path::new("rch-v1.0.0-x86_64-pc-windows-msvc.zip")).unwrap(),
            UpdateArchiveFormat::Zip
        );
        assert!(matches!(
            update_archive_format(Path::new("rch-v1.0.0.txt")),
            Err(UpdateError::InstallFailed(_))
        ));
    }

    #[test]
    fn test_extract_archive_extracts_tar_gz_regular_files() {
        let temp = TempDir::new().unwrap();
        let archive_path = temp.path().join("rch.tar.gz");
        let dest = temp.path().join("extract");
        create_tar_gz_with_entries(
            &archive_path,
            &[("rch", b"hook binary"), ("nested/rchd", b"daemon binary")],
        )
        .unwrap();

        extract_archive(&archive_path, &dest).unwrap();

        assert_eq!(
            std::fs::read_to_string(dest.join("rch")).unwrap(),
            "hook binary"
        );
        assert_eq!(
            std::fs::read_to_string(dest.join("nested/rchd")).unwrap(),
            "daemon binary"
        );
    }

    #[test]
    fn test_extract_archive_extracts_zip_regular_files() {
        let temp = TempDir::new().unwrap();
        let archive_path = temp.path().join("rch.zip");
        let dest = temp.path().join("extract");
        create_zip_with_entries(
            &archive_path,
            &[
                (REQUIRED_UPDATE_BINARY, b"hook binary"),
                (optional_update_binary(), b"optional binary"),
            ],
        )
        .unwrap();

        extract_archive(&archive_path, &dest).unwrap();

        assert_eq!(
            std::fs::read_to_string(dest.join(REQUIRED_UPDATE_BINARY)).unwrap(),
            "hook binary"
        );
        assert_eq!(
            std::fs::read_to_string(dest.join(optional_update_binary())).unwrap(),
            "optional binary"
        );
    }

    #[test]
    fn test_extract_archive_rejects_parent_traversal() {
        let temp = TempDir::new().unwrap();
        let archive_path = temp.path().join("rch.tar.gz");
        let dest = temp.path().join("extract");
        let outside = temp.path().join("outside-rch");
        create_tar_gz_with_raw_path(&archive_path, b"../outside-rch", b"escape").unwrap();

        let result = extract_archive(&archive_path, &dest);

        assert!(matches!(result, Err(UpdateError::InstallFailed(_))));
        assert!(
            !outside.exists(),
            "traversal entry must not write outside extract root"
        );
    }

    #[test]
    fn test_extract_archive_rejects_absolute_paths() {
        let temp = TempDir::new().unwrap();
        let archive_path = temp.path().join("rch.tar.gz");
        let dest = temp.path().join("extract");
        create_tar_gz_with_raw_path(&archive_path, b"/tmp/rch-escape", b"escape").unwrap();

        let result = extract_archive(&archive_path, &dest);

        assert!(matches!(result, Err(UpdateError::InstallFailed(_))));
    }

    #[test]
    fn test_extract_archive_rejects_backslash_paths() {
        let temp = TempDir::new().unwrap();
        let archive_path = temp.path().join("rch.tar.gz");
        let dest = temp.path().join("extract");
        create_tar_gz_with_entries(&archive_path, &[("nested\\rch", b"binary")]).unwrap();

        let result = extract_archive(&archive_path, &dest);

        assert!(matches!(result, Err(UpdateError::InstallFailed(_))));
    }

    #[test]
    fn test_extract_archive_rejects_symlink_entries() {
        let temp = TempDir::new().unwrap();
        let archive_path = temp.path().join("rch.tar.gz");
        let dest = temp.path().join("extract");
        create_tar_gz_with_symlink(&archive_path, "rch", "/tmp/rch-target").unwrap();

        let result = extract_archive(&archive_path, &dest);

        assert!(matches!(result, Err(UpdateError::InstallFailed(_))));
        assert!(!dest.join("rch").exists());
    }

    #[test]
    fn test_extract_archive_rejects_preexisting_destination() {
        let temp = TempDir::new().unwrap();
        let archive_path = temp.path().join("rch.tar.gz");
        let dest = temp.path().join("extract");
        create_tar_gz_with_entries(&archive_path, &[("rch", b"binary")]).unwrap();
        fs::create_dir(&dest).unwrap();

        let result = extract_archive(&archive_path, &dest);

        assert!(matches!(result, Err(UpdateError::InstallFailed(_))));
        assert!(
            !dest.join("rch").exists(),
            "existing extraction root must not be reused"
        );
    }

    #[test]
    fn test_restore_preserves_content() {
        let temp = TempDir::new().unwrap();
        let backup_dir = temp.path().join("backup");
        let install_dir = temp.path().join("install");

        std::fs::create_dir_all(&backup_dir).unwrap();
        std::fs::create_dir_all(&install_dir).unwrap();
        let optional_binary = optional_update_binary();

        // Create backup with specific content
        std::fs::write(backup_dir.join(REQUIRED_UPDATE_BINARY), "backup v1.0").unwrap();
        std::fs::write(backup_dir.join(optional_binary), "backup optional").unwrap();

        // Create current with different content
        std::fs::write(install_dir.join(REQUIRED_UPDATE_BINARY), "current v2.0").unwrap();
        std::fs::write(install_dir.join(optional_binary), "current optional").unwrap();

        stage_and_replace(&backup_dir, &install_dir).unwrap();

        assert_eq!(
            std::fs::read_to_string(install_dir.join(REQUIRED_UPDATE_BINARY)).unwrap(),
            "backup v1.0"
        );
        assert_eq!(
            std::fs::read_to_string(install_dir.join(optional_binary)).unwrap(),
            "backup optional"
        );
    }

    #[cfg(unix)]
    #[test]
    fn test_restore_rejects_symlink_backup_payload() {
        use std::os::unix::fs::symlink;

        let temp = TempDir::new().unwrap();
        let backup_dir = temp.path().join("backup");
        let install_dir = temp.path().join("install");
        let external_binary = temp.path().join("external-rch");

        std::fs::create_dir_all(&backup_dir).unwrap();
        std::fs::create_dir_all(&install_dir).unwrap();
        std::fs::write(&external_binary, "external backup binary").unwrap();
        symlink(&external_binary, backup_dir.join(REQUIRED_UPDATE_BINARY)).unwrap();
        std::fs::write(install_dir.join(REQUIRED_UPDATE_BINARY), "current binary").unwrap();

        let result = stage_and_replace(&backup_dir, &install_dir);

        assert!(matches!(result, Err(UpdateError::InstallFailed(_))));
        assert_eq!(
            std::fs::read_to_string(install_dir.join(REQUIRED_UPDATE_BINARY)).unwrap(),
            "current binary",
            "rollback must not follow symlinked backup payloads"
        );
    }

    #[test]
    fn test_restore_failure_preserves_existing_binary() {
        let temp = TempDir::new().unwrap();
        let backup_dir = temp.path().join("backup");
        let install_dir = temp.path().join("install");

        std::fs::create_dir_all(backup_dir.join(REQUIRED_UPDATE_BINARY)).unwrap();
        std::fs::create_dir_all(&install_dir).unwrap();
        std::fs::write(install_dir.join(REQUIRED_UPDATE_BINARY), "current binary").unwrap();

        let result = stage_and_replace(&backup_dir, &install_dir);

        assert!(matches!(result, Err(UpdateError::InstallFailed(_))));
        assert_eq!(
            std::fs::read_to_string(install_dir.join(REQUIRED_UPDATE_BINARY)).unwrap(),
            "current binary",
            "rollback must stage backup payload before replacing the installed binary"
        );
    }

    #[test]
    fn daemon_start_args_pin_configured_socket_path() {
        let socket_path = Path::new("/tmp/rch-update-custom.sock");
        let args = daemon_start_args(socket_path);

        assert_eq!(args[0], std::ffi::OsStr::new("--socket"));
        assert_eq!(args[1], socket_path.as_os_str());
    }

    #[test]
    fn update_start_selects_the_installed_sibling_not_path() {
        let directory = TempDir::new().unwrap();
        let install_dir = directory.path().join("installation : with spaces");
        let command = daemon_start_command(&install_dir);
        assert_eq!(
            command.get_program(),
            install_dir
                .join(format!("rchd{}", std::env::consts::EXE_SUFFIX))
                .as_os_str()
        );
    }
}