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
use anyhow::{Context, anyhow};
use async_recursion::async_recursion;
use std::os::unix::fs::PermissionsExt;
use tracing::instrument;
use crate::filter::TimeFilter;
use crate::progress;
use crate::walk::{self, EntryKind};
/// Error type for remove operations. See [`crate::error::OperationError`] for
/// logging conventions and rationale.
pub type Error = crate::error::OperationError<Summary>;
#[derive(Debug, Clone)]
pub struct Settings {
pub fail_early: bool,
/// filter settings for include/exclude patterns
pub filter: Option<crate::filter::FilterSettings>,
/// time-based filter (mtime/btime); applied to each entry individually (files,
/// symlinks, and directories). This is an entry filter, not a subtree gate:
/// directories are always traversed, and the filter only decides whether each
/// entry — including the directory itself, after its children are processed — is
/// eligible for removal. A directory whose own timestamps are too recent is left
/// intact even when its children have been removed; a non-empty leftover directory
/// is logged at info and not treated as an error.
pub time_filter: Option<TimeFilter>,
/// dry-run mode for previewing operations
pub dry_run: Option<crate::config::DryRunMode>,
}
/// Returns true when `err`'s chain contains an `io::Error` with `ErrorKind::Unsupported`.
/// Used to downgrade time-filter eval failures on filesystems / entry types that don't
/// report btime (e.g. many symlinks) from `error!` to `warn!` so they don't flood logs
/// on otherwise-successful runs.
fn is_unsupported_io_error(err: &anyhow::Error) -> bool {
err.chain().any(|cause| {
cause
.downcast_ref::<std::io::Error>()
.is_some_and(|io_err| io_err.kind() == std::io::ErrorKind::Unsupported)
})
}
/// Summary with the appropriate `*_skipped` counter set to 1 for the given entry kind.
/// Special files count as `files_skipped` to match the historical mapping used
/// when filters skip an entry.
fn skipped_summary_for(kind: EntryKind) -> Summary {
match kind {
EntryKind::Dir => Summary {
directories_skipped: 1,
..Default::default()
},
EntryKind::Symlink => Summary {
symlinks_skipped: 1,
..Default::default()
},
EntryKind::File | EntryKind::Special => Summary {
files_skipped: 1,
..Default::default()
},
}
}
#[derive(Copy, Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct Summary {
pub bytes_removed: u64,
pub files_removed: usize,
pub symlinks_removed: usize,
pub directories_removed: usize,
pub files_skipped: usize,
pub symlinks_skipped: usize,
pub directories_skipped: usize,
}
impl std::ops::Add for Summary {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
bytes_removed: self.bytes_removed + other.bytes_removed,
files_removed: self.files_removed + other.files_removed,
symlinks_removed: self.symlinks_removed + other.symlinks_removed,
directories_removed: self.directories_removed + other.directories_removed,
files_skipped: self.files_skipped + other.files_skipped,
symlinks_skipped: self.symlinks_skipped + other.symlinks_skipped,
directories_skipped: self.directories_skipped + other.directories_skipped,
}
}
}
impl std::fmt::Display for Summary {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"bytes removed: {}\n\
files removed: {}\n\
symlinks removed: {}\n\
directories removed: {}\n\
files skipped: {}\n\
symlinks skipped: {}\n\
directories skipped: {}\n",
bytesize::ByteSize(self.bytes_removed),
self.files_removed,
self.symlinks_removed,
self.directories_removed,
self.files_skipped,
self.symlinks_skipped,
self.directories_skipped
)
}
}
/// RAII guard that restores a directory's original mode on drop.
///
/// `rm_internal` chmod-relaxes a read-only directory to `0o777` to clear its contents. If the
/// directory is retained (filter-protected children, time-filter skip, ENOTEMPTY) or any error
/// occurs after the relax, the original mode must be restored — otherwise a `--delete`/`rrm`
/// run leaves a protected tree world-writable. Drop fires on every exit path (retain, error,
/// panic-unwind) without callers having to remember it; the success-remove path calls
/// [`Self::defuse`] because the directory no longer exists.
///
/// Drop runs synchronously (it can't be async), so it uses `std::fs::set_permissions`: one
/// chmod syscall — negligible cost, no need to round-trip through the tokio blocking pool just
/// for cleanup. Best-effort: a restore failure is logged, not fatal.
struct RelaxedDirGuard<'a> {
path: &'a std::path::Path,
/// Original mode to restore on drop. `None` means defused (no restore).
mode: Option<u32>,
}
impl<'a> RelaxedDirGuard<'a> {
fn new(path: &'a std::path::Path) -> Self {
Self { path, mode: None }
}
/// Record the original mode so it's restored on drop.
fn arm(&mut self, original_mode: u32) {
self.mode = Some(original_mode);
}
/// Cancel the pending restore (call after successfully removing the directory).
fn defuse(&mut self) {
self.mode = None;
}
}
impl Drop for RelaxedDirGuard<'_> {
fn drop(&mut self) {
if let Some(mode) = self.mode.take()
&& let Err(err) =
std::fs::set_permissions(self.path, std::fs::Permissions::from_mode(mode))
{
tracing::warn!(
"failed to restore original permissions on retained directory {:?}: {:#}",
self.path,
err
);
}
}
}
/// Public entry point for remove operations.
/// Internally delegates to rm_internal with source_root tracking for proper filter matching.
#[instrument(skip(prog_track, settings))]
pub async fn rm(
prog_track: &'static progress::Progress,
path: &std::path::Path,
settings: &Settings,
) -> Result<Summary, Error> {
// check filter for top-level path (files, directories, and symlinks)
if let Some(ref filter) = settings.filter {
let path_name = path.file_name().map(std::path::Path::new);
if let Some(name) = path_name {
let path_metadata = crate::walk::run_metadata_probed(
congestion::Side::Source,
congestion::MetadataOp::Stat,
tokio::fs::symlink_metadata(path),
)
.await
.with_context(|| format!("failed reading metadata from {:?}", &path))
.map_err(|err| Error::new(err, Default::default()))?;
let is_dir = path_metadata.is_dir();
let result = filter.should_include_root_item(name, is_dir);
match result {
crate::filter::FilterResult::Included => {}
result => {
let kind = EntryKind::from_metadata(&path_metadata);
if let Some(mode) = settings.dry_run {
crate::dry_run::report_skip(path, &result, mode, kind.label_long());
}
kind.inc_skipped(prog_track);
return Ok(skipped_summary_for(kind));
}
}
}
}
// note: the time filter (applied to files, symlinks, and directories) is handled
// inside rm_internal, so we don't duplicate the check here.
rm_internal(prog_track, path, path, settings).await
}
/// Like [`rm`], but evaluates the include/exclude filter relative to `filter_root` instead of
/// `path`. Used by `--delete` pruning: when removing an extraneous destination subtree,
/// descendant excludes must be matched against their full destination-root-relative paths
/// (which mirror the source-relative paths the filter targets), so path/anchored patterns
/// like `cache/*.log` protect the right entries. The caller is responsible for the top-level
/// filter decision on `path`.
pub async fn rm_with_filter_root(
prog_track: &'static progress::Progress,
path: &std::path::Path,
filter_root: &std::path::Path,
settings: &Settings,
) -> Result<Summary, Error> {
rm_internal(prog_track, path, filter_root, settings).await
}
#[instrument(skip(prog_track, settings))]
#[async_recursion]
async fn rm_internal(
prog_track: &'static progress::Progress,
path: &std::path::Path,
source_root: &std::path::Path,
settings: &Settings,
) -> Result<Summary, Error> {
let _ops_guard = prog_track.ops.guard();
tracing::debug!("read path metadata");
let src_metadata = crate::walk::run_metadata_probed(
congestion::Side::Source,
congestion::MetadataOp::Stat,
tokio::fs::symlink_metadata(path),
)
.await
.with_context(|| format!("failed reading metadata from {:?}", &path))
.map_err(|err| Error::new(err, Default::default()))?;
if !src_metadata.is_dir() {
tracing::debug!("not a directory, just remove");
let is_symlink = src_metadata.file_type().is_symlink();
let file_size = if is_symlink { 0 } else { src_metadata.len() };
// apply time filter before removing (files/symlinks only)
if let Some(ref time_filter) = settings.time_filter {
let entry_type = if is_symlink { "symlink" } else { "file" };
let make_skipped_summary = || {
tracing::debug!("skipping {:?} due to time filter", &path);
if is_symlink {
prog_track.symlinks_skipped.inc();
Summary {
symlinks_skipped: 1,
..Default::default()
}
} else {
prog_track.files_skipped.inc();
Summary {
files_skipped: 1,
..Default::default()
}
}
};
match time_filter.matches(&src_metadata) {
Ok(result) => {
if let Some(skip_reason) = result.as_skip_reason() {
if let Some(mode) = settings.dry_run {
crate::dry_run::report_time_skip(path, skip_reason, mode, entry_type);
}
return Ok(make_skipped_summary());
}
}
Err(err) => {
let err = err.context(format!("failed evaluating time filter on {:?}", &path));
if settings.fail_early {
return Err(Error::new(err, Default::default()));
}
// log and skip — never delete an entry whose age we cannot verify.
// btime being unsupported (common for symlinks) is expected noise, so
// downgrade to warn; anything else is unexpected and stays at error.
if is_unsupported_io_error(&err) {
tracing::warn!(
"time filter evaluation unsupported for {} {:?}, skipping: {:#}",
entry_type,
&path,
&err
);
} else {
tracing::error!(
"time filter evaluation failed for {} {:?}, skipping: {:#}",
entry_type,
&path,
&err
);
}
return Ok(make_skipped_summary());
}
}
}
// handle dry-run mode for files/symlinks
if settings.dry_run.is_some() {
let entry_type = if is_symlink { "symlink" } else { "file" };
crate::dry_run::report_action("remove", path, None, entry_type);
return Ok(Summary {
bytes_removed: file_size,
files_removed: if is_symlink { 0 } else { 1 },
symlinks_removed: if is_symlink { 1 } else { 0 },
..Default::default()
});
}
if let Err(err) = crate::walk::run_metadata_probed(
congestion::Side::Destination,
congestion::MetadataOp::Unlink,
tokio::fs::remove_file(path),
)
.await
.with_context(|| format!("failed removing {:?}", &path))
{
return Err(Error::new(err, Default::default()));
}
if is_symlink {
prog_track.symlinks_removed.inc();
return Ok(Summary {
symlinks_removed: 1,
..Default::default()
});
}
prog_track.files_removed.inc();
prog_track.bytes_removed.add(file_size);
return Ok(Summary {
bytes_removed: file_size,
files_removed: 1,
..Default::default()
});
}
tracing::debug!("remove contents of the directory first");
// When the directory is read-only we relax it to 0o777 so its contents can be cleared.
// `relaxed_guard` records the original mode and restores it on Drop — covering every
// retain branch (filter-protected children, time-filter skip, ENOTEMPTY) AND every error
// path that returns before the directory is removed. The success-remove path calls
// `defuse()` because the directory no longer exists. Dry-run skips the relax entirely,
// so the guard stays unarmed.
let mut relaxed_guard = RelaxedDirGuard::new(path);
if settings.dry_run.is_none() && src_metadata.permissions().readonly() {
tracing::debug!("directory is read-only - change the permissions");
let original_mode = src_metadata.permissions().mode() & 0o7777;
tokio::fs::set_permissions(path, std::fs::Permissions::from_mode(0o777))
.await
.with_context(|| {
format!(
"failed to make '{:?}' directory readable and writeable",
&path
)
})
.map_err(|err| Error::new(err, Default::default()))?;
relaxed_guard.arm(original_mode);
}
let mut entries = match tokio::fs::read_dir(path).await {
Ok(entries) => entries,
Err(err) => {
return Err(Error::new(
anyhow::Error::new(err).context(format!("failed reading directory {:?}", &path)),
Default::default(),
));
}
};
let mut join_set = tokio::task::JoinSet::new();
let errors = crate::error_collector::ErrorCollector::default();
let mut skipped_files = 0;
let mut skipped_symlinks = 0;
let mut skipped_dirs = 0;
loop {
let next_entry =
crate::walk::next_entry_probed(&mut entries, congestion::Side::Source, || {
format!("failed traversing directory {:?}", &path)
})
.await;
let Some((entry, entry_file_type)) = (match next_entry {
Ok(opt) => opt,
Err(err) => {
return Err(Error::new(err, Default::default()));
}
}) else {
break;
};
let entry_path = entry.path();
let entry_kind = EntryKind::from_file_type(entry_file_type.as_ref());
let entry_is_dir = entry_kind == EntryKind::Dir;
// compute relative path from source_root for filter matching
let relative_path = walk::relative_to_root(&entry_path, source_root);
// apply filter if configured
if let Some(skip_result) =
walk::should_skip_entry(&settings.filter, relative_path, entry_is_dir)
{
if let Some(mode) = settings.dry_run {
crate::dry_run::report_skip(&entry_path, &skip_result, mode, entry_kind.label());
}
tracing::debug!("skipping {:?} due to filter", &entry_path);
// increment skipped counters - will be added to rm_summary below
match entry_kind {
EntryKind::Dir => skipped_dirs += 1,
EntryKind::Symlink => skipped_symlinks += 1,
EntryKind::File | EntryKind::Special => skipped_files += 1,
}
entry_kind.inc_skipped(prog_track);
continue;
}
let settings = settings.clone();
let source_root = source_root.to_owned();
// for positively-known leaf entries (files, symlinks, special),
// acquire the pending-meta permit BEFORE spawning so we don't create
// unbounded tasks. We deliberately skip pre-acquire when
// `entry_file_type` is None (file_type() lookup failed): the entry
// could actually be a directory, and a chain of such unknown-typed
// directories holding permits while recursing would deadlock the
// pending-meta pool. Directories also skip pre-acquire for the same
// reason. We use the pending-meta semaphore (not open-files) because
// rm operations don't hold fds — and rm is reachable from copy_file's
// overwrite path, which already holds an open-files permit; using a
// distinct semaphore avoids that cross-pool deadlock.
let known_leaf = entry_file_type.as_ref().is_some_and(|ft| !ft.is_dir());
let pending_guard = if known_leaf {
Some(throttle::pending_meta_permit().await)
} else {
None
};
let do_rm = || async move {
let _pending_guard = pending_guard;
rm_internal(prog_track, &entry_path, &source_root, &settings).await
};
join_set.spawn(do_rm());
}
// unfortunately ReadDir is opening file-descriptors and there's not a good way to limit this,
// one thing we CAN do however is to drop it as soon as we're done with it
drop(entries);
let mut rm_summary = Summary {
directories_removed: 0,
files_skipped: skipped_files,
symlinks_skipped: skipped_symlinks,
directories_skipped: skipped_dirs,
..Default::default()
};
while let Some(res) = join_set.join_next().await {
match res {
Ok(result) => match result {
Ok(summary) => rm_summary = rm_summary + summary,
Err(error) => {
tracing::error!("remove: {:?} failed with: {:#}", path, &error);
rm_summary = rm_summary + error.summary;
errors.push(error.source);
if settings.fail_early {
break;
}
}
},
Err(error) => {
errors.push(error.into());
if settings.fail_early {
break;
}
}
}
}
if errors.has_errors() {
// unwrap is safe: has_errors() guarantees into_error() returns Some
return Err(Error::new(errors.into_error().unwrap(), rm_summary));
}
tracing::debug!("finally remove the empty directory");
let anything_removed = rm_summary.files_removed > 0
|| rm_summary.symlinks_removed > 0
|| rm_summary.directories_removed > 0;
let anything_skipped = rm_summary.files_skipped > 0
|| rm_summary.symlinks_skipped > 0
|| rm_summary.directories_skipped > 0;
// a directory is "traversed only" when include filters are active, nothing was removed
// from it, and the directory itself doesn't directly match an include pattern. such
// directories were only entered to search for matching content inside and should be
// left intact. directories that directly match an include pattern (e.g. --include target/)
// should be removed even if empty. exclude-only filters never produce traversed-only
// directories because directly_matches_include returns true when no includes exist.
let relative_path = walk::relative_to_root(path, source_root);
let traversed_only = !anything_removed
&& settings
.filter
.as_ref()
.is_some_and(|f| f.has_includes() && !f.directly_matches_include(relative_path, true));
// evaluate the directory's own time filter to decide whether to remove it.
// the time filter is an entry filter, not a subtree gate: children are already handled
// by their own recursive calls, so this decision only controls the final remove_dir.
// returns Ok(true) = proceed, Ok(false) = skip (too new), Err propagates a fail-early.
// the src_metadata captured at entry is used so rrm's own mutations during traversal
// don't change the answer.
let dir_passes_time_filter: bool = if let Some(ref time_filter) = settings.time_filter {
match time_filter.matches(&src_metadata) {
Ok(result) => match result.as_skip_reason() {
Some(reason) => {
if let Some(mode) = settings.dry_run {
crate::dry_run::report_time_skip(path, reason, mode, "dir");
}
false
}
None => true,
},
Err(err) => {
let err = err.context(format!("failed evaluating time filter on {:?}", &path));
if settings.fail_early {
return Err(Error::new(err, rm_summary));
}
// log and skip — never remove a directory whose age we cannot verify.
// btime being unsupported on the filesystem is expected noise; downgrade
// to warn. anything else is unexpected and stays at error.
if is_unsupported_io_error(&err) {
tracing::warn!(
"time filter evaluation unsupported for dir {:?}, leaving it intact: {:#}",
&path,
&err
);
} else {
tracing::error!(
"time filter evaluation failed for dir {:?}, leaving it intact: {:#}",
&path,
&err
);
}
false
}
}
} else {
true
};
// handle dry-run mode for directories.
// `traversed_only` catches dirs only entered to search for include pattern matches.
// `anything_skipped` catches dirs that would still have content after partial removal.
// `!dir_passes_time_filter` catches dirs whose own timestamps disqualify removal.
// the real-mode path below only needs `traversed_only` and `!dir_passes_time_filter`
// because the subsequent `remove_dir` call handles the non-empty case via ENOTEMPTY.
if settings.dry_run.is_some() {
if traversed_only || anything_skipped || !dir_passes_time_filter {
tracing::debug!(
"dry-run: directory {:?} would not be removed (removed={}, skipped={}, time_ok={})",
&path,
anything_removed,
anything_skipped,
dir_passes_time_filter
);
if !dir_passes_time_filter {
prog_track.directories_skipped.inc();
rm_summary.directories_skipped += 1;
}
} else {
crate::dry_run::report_action("remove", path, None, "dir");
rm_summary.directories_removed += 1;
}
return Ok(rm_summary);
}
// skip directories that were only traversed to look for include matches.
// not needed for exclude-only filters or directly-matched directories.
// non-empty directories are handled by the ENOTEMPTY check below.
if traversed_only {
tracing::debug!(
"directory {:?} had nothing removed, leaving it intact",
&path
);
return Ok(rm_summary);
}
// skip directories whose own timestamps don't satisfy the time filter.
// children have already been processed; this only gates the dir's own removal.
if !dir_passes_time_filter {
tracing::debug!(
"directory {:?} skipped by time filter, leaving it intact",
&path
);
prog_track.directories_skipped.inc();
rm_summary.directories_skipped += 1;
return Ok(rm_summary);
}
// when filtering is active, directories may not be empty because we only removed
// matching files (includes) or skipped excluded files; use remove_dir (not remove_dir_all)
// so non-empty directories fail gracefully with ENOTEMPTY
let any_filter_active = settings.filter.is_some() || settings.time_filter.is_some();
match crate::walk::run_metadata_probed(
congestion::Side::Destination,
congestion::MetadataOp::RmDir,
tokio::fs::remove_dir(path),
)
.await
{
Ok(()) => {
prog_track.directories_removed.inc();
rm_summary.directories_removed += 1;
// the directory is gone, so there's nothing to restore on drop.
relaxed_guard.defuse();
}
Err(err) if any_filter_active => {
// with filtering, it's expected that directories may not be empty because we only
// removed matching files; raw_os_error 39 is ENOTEMPTY on Linux. this is not an
// error — surface it at info so users can see which directories survived.
if err.kind() == std::io::ErrorKind::DirectoryNotEmpty || err.raw_os_error() == Some(39)
{
tracing::info!(
"directory {:?} not empty after filtering, leaving it intact",
&path
);
} else {
return Err(Error::new(
anyhow!(err).context(format!("failed removing directory {:?}", &path)),
rm_summary,
));
}
}
Err(err) => {
return Err(Error::new(
anyhow!(err).context(format!("failed removing directory {:?}", &path)),
rm_summary,
));
}
}
Ok(rm_summary)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::DryRunMode;
use crate::testutils;
use tracing_test::traced_test;
static PROGRESS: std::sync::LazyLock<progress::Progress> =
std::sync::LazyLock::new(progress::Progress::new);
#[tokio::test]
#[traced_test]
async fn no_write_permission() -> Result<(), anyhow::Error> {
let tmp_dir = testutils::setup_test_dir().await?;
let test_path = tmp_dir.as_path();
let filepaths = vec![
test_path.join("foo").join("0.txt"),
test_path.join("foo").join("bar").join("2.txt"),
test_path.join("foo").join("baz").join("4.txt"),
test_path.join("foo").join("baz"),
];
for fpath in &filepaths {
// change file permissions to not readable and not writable
tokio::fs::set_permissions(&fpath, std::fs::Permissions::from_mode(0o555)).await?;
}
let summary = rm(
&PROGRESS,
&test_path.join("foo"),
&Settings {
fail_early: false,
filter: None,
dry_run: None,
time_filter: None,
},
)
.await?;
assert!(!test_path.join("foo").exists());
assert_eq!(summary.files_removed, 5);
assert_eq!(summary.symlinks_removed, 2);
assert_eq!(summary.directories_removed, 3);
Ok(())
}
#[tokio::test]
#[traced_test]
async fn relaxed_dir_mode_restored_on_error_exit() -> Result<(), anyhow::Error> {
// Regression: when rm_internal chmod-relaxes a read-only directory to 0o777 to clear
// its contents, it must restore the original mode on ERROR paths too — not just on the
// retain paths (traversed-only, time-filtered skip, ENOTEMPTY under filter). Without
// that, a partial failure leaves the directory world-writable.
//
// We trigger the remove_dir error path by making the dst's PARENT non-writable: rm can
// chmod-relax dst and successfully unlink its contents (write on dst is granted by the
// relax), but the final remove_dir(dst) needs write permission on the parent, which it
// doesn't have → EACCES. The fix's inline restore at that error site must put dst back
// to 0o555 before propagating.
let tmp = tempfile::tempdir()?;
let parent = tmp.path().join("parent");
let dst = parent.join("dst");
tokio::fs::create_dir(&parent).await?;
tokio::fs::create_dir(&dst).await?;
tokio::fs::write(dst.join("inside.txt"), b"x").await?;
// dst is read-only → rm relaxes it.
tokio::fs::set_permissions(&dst, std::fs::Permissions::from_mode(0o555)).await?;
// parent is read-only (still traversable via the execute bit) → remove_dir(dst) fails.
tokio::fs::set_permissions(&parent, std::fs::Permissions::from_mode(0o555)).await?;
let result = rm(
&PROGRESS,
&dst,
&Settings {
fail_early: false,
filter: None,
dry_run: None,
time_filter: None,
},
)
.await;
// restore parent writability so we can stat dst and clean up regardless of the assertion.
tokio::fs::set_permissions(&parent, std::fs::Permissions::from_mode(0o755)).await?;
assert!(
result.is_err(),
"rm must fail when its dst can be emptied but the parent dir blocks remove_dir"
);
let mode = tokio::fs::metadata(&dst).await?.permissions().mode() & 0o7777;
assert_eq!(
mode, 0o555,
"relaxed-then-erroring directory must be restored to its original mode (got {mode:o}o); leaving it 0o777 leaks permissions on partial failure"
);
// cleanup
tokio::fs::set_permissions(&dst, std::fs::Permissions::from_mode(0o755)).await?;
Ok(())
}
#[tokio::test]
#[traced_test]
async fn parent_dir_no_write_permission() -> Result<(), anyhow::Error> {
let tmp_dir = testutils::setup_test_dir().await?;
let test_path = tmp_dir.as_path();
// make parent directory read-only (no write permission)
tokio::fs::set_permissions(
&test_path.join("foo").join("bar"),
std::fs::Permissions::from_mode(0o555),
)
.await?;
let result = rm(
&PROGRESS,
&test_path.join("foo").join("bar").join("2.txt"),
&Settings {
fail_early: true,
filter: None,
dry_run: None,
time_filter: None,
},
)
.await;
// should fail with permission denied error
assert!(result.is_err());
let err = result.unwrap_err();
let err_string = format!("{:#}", err);
// verify the error chain includes "Permission denied"
assert!(
err_string.contains("Permission denied") || err_string.contains("permission denied"),
"Error should contain 'Permission denied' but got: {}",
err_string
);
Ok(())
}
mod filter_tests {
use super::*;
use crate::filter::FilterSettings;
/// Test that path-based patterns (with /) work correctly with nested paths.
#[tokio::test]
#[traced_test]
async fn test_path_pattern_matches_nested_files() -> Result<(), anyhow::Error> {
let tmp_dir = testutils::setup_test_dir().await?;
let test_path = tmp_dir.as_path();
// create filter that should only remove files in bar/ directory
let mut filter = FilterSettings::new();
filter.add_include("bar/*.txt").unwrap();
let summary = rm(
&PROGRESS,
&test_path.join("foo"),
&Settings {
fail_early: false,
filter: Some(filter),
dry_run: None,
time_filter: None,
},
)
.await?;
// should only remove files matching bar/*.txt pattern (bar/1.txt, bar/2.txt, bar/3.txt)
assert_eq!(
summary.files_removed, 3,
"should remove 3 files matching bar/*.txt"
);
// each file is 1 byte ("1", "2", "3")
assert_eq!(summary.bytes_removed, 3, "should report 3 bytes removed");
// verify the right files were removed
assert!(
!test_path.join("foo/bar/1.txt").exists(),
"bar/1.txt should be removed"
);
assert!(
!test_path.join("foo/bar/2.txt").exists(),
"bar/2.txt should be removed"
);
assert!(
!test_path.join("foo/bar/3.txt").exists(),
"bar/3.txt should be removed"
);
// verify files outside the pattern still exist
assert!(
test_path.join("foo/0.txt").exists(),
"0.txt should still exist"
);
Ok(())
}
/// Test that filters are applied to top-level file arguments.
#[tokio::test]
#[traced_test]
async fn test_filter_applies_to_single_file_source() -> Result<(), anyhow::Error> {
let tmp_dir = testutils::setup_test_dir().await?;
let test_path = tmp_dir.as_path();
// create filter that excludes .txt files
let mut filter = FilterSettings::new();
filter.add_exclude("*.txt").unwrap();
let summary = rm(
&PROGRESS,
&test_path.join("foo/0.txt"), // single file source
&Settings {
fail_early: false,
filter: Some(filter),
dry_run: None,
time_filter: None,
},
)
.await?;
// the file should NOT be removed because it matches the exclude pattern
assert_eq!(
summary.files_removed, 0,
"file matching exclude pattern should not be removed"
);
assert!(
test_path.join("foo/0.txt").exists(),
"excluded file should still exist"
);
Ok(())
}
/// Test that filters apply to root directories with simple exclude patterns.
#[tokio::test]
#[traced_test]
async fn test_filter_applies_to_root_directory() -> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
// create a directory that should be excluded
tokio::fs::create_dir_all(test_path.join("excluded_dir")).await?;
tokio::fs::write(test_path.join("excluded_dir/file.txt"), "content").await?;
// create filter that excludes *_dir/ directories
let mut filter = FilterSettings::new();
filter.add_exclude("*_dir/").unwrap();
let result = rm(
&PROGRESS,
&test_path.join("excluded_dir"),
&Settings {
fail_early: false,
filter: Some(filter),
dry_run: None,
time_filter: None,
},
)
.await?;
// directory should NOT be removed because it matches exclude pattern
assert_eq!(
result.directories_removed, 0,
"root directory matching exclude should not be removed"
);
assert!(
test_path.join("excluded_dir").exists(),
"excluded root directory should still exist"
);
Ok(())
}
/// Test that filters apply to root symlinks with simple exclude patterns.
#[tokio::test]
#[traced_test]
async fn test_filter_applies_to_root_symlink() -> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
// create a target file and a symlink to it
tokio::fs::write(test_path.join("target.txt"), "content").await?;
tokio::fs::symlink(
test_path.join("target.txt"),
test_path.join("excluded_link"),
)
.await?;
// create filter that excludes *_link
let mut filter = FilterSettings::new();
filter.add_exclude("*_link").unwrap();
let result = rm(
&PROGRESS,
&test_path.join("excluded_link"),
&Settings {
fail_early: false,
filter: Some(filter),
dry_run: None,
time_filter: None,
},
)
.await?;
// symlink should NOT be removed because it matches exclude pattern
assert_eq!(
result.symlinks_removed, 0,
"root symlink matching exclude should not be removed"
);
assert!(
test_path.join("excluded_link").exists(),
"excluded root symlink should still exist"
);
Ok(())
}
/// Test combined include and exclude patterns (exclude takes precedence).
#[tokio::test]
#[traced_test]
async fn test_combined_include_exclude_patterns() -> Result<(), anyhow::Error> {
let tmp_dir = testutils::setup_test_dir().await?;
let test_path = tmp_dir.as_path();
// test structure from setup_test_dir:
// foo/
// 0.txt
// bar/ (1.txt, 2.txt, 3.txt)
// baz/ (4.txt, 5.txt symlink, 6.txt symlink)
// include all .txt files in bar/, but exclude 2.txt specifically
let mut filter = FilterSettings::new();
filter.add_include("bar/*.txt").unwrap();
filter.add_exclude("bar/2.txt").unwrap();
let summary = rm(
&PROGRESS,
&test_path.join("foo"),
&Settings {
fail_early: false,
filter: Some(filter),
dry_run: None,
time_filter: None,
},
)
.await?;
// should remove: bar/1.txt, bar/3.txt = 2 files
// should skip: bar/2.txt (excluded by pattern), 0.txt (excluded by default - no match) = 2 files
assert_eq!(summary.files_removed, 2, "should remove 2 files");
assert_eq!(
summary.files_skipped, 2,
"should skip 2 files (bar/2.txt excluded, 0.txt no match)"
);
// verify
assert!(
!test_path.join("foo/bar/1.txt").exists(),
"bar/1.txt should be removed"
);
assert!(
test_path.join("foo/bar/2.txt").exists(),
"bar/2.txt should be excluded"
);
assert!(
!test_path.join("foo/bar/3.txt").exists(),
"bar/3.txt should be removed"
);
Ok(())
}
/// Test that skipped counts accurately reflect what was filtered.
#[tokio::test]
#[traced_test]
async fn test_skipped_counts_comprehensive() -> Result<(), anyhow::Error> {
let tmp_dir = testutils::setup_test_dir().await?;
let test_path = tmp_dir.as_path();
// test structure from setup_test_dir:
// foo/
// 0.txt
// bar/ (1.txt, 2.txt, 3.txt)
// baz/ (4.txt, 5.txt symlink, 6.txt symlink)
// exclude bar/ directory entirely
let mut filter = FilterSettings::new();
filter.add_exclude("bar/").unwrap();
let summary = rm(
&PROGRESS,
&test_path.join("foo"),
&Settings {
fail_early: false,
filter: Some(filter),
dry_run: None,
time_filter: None,
},
)
.await?;
// removed: 0.txt, baz/4.txt = 2 files
// removed: baz/5.txt symlink, baz/6.txt symlink = 2 symlinks
// removed: baz = 1 directory (foo cannot be removed because bar still exists)
// skipped: bar directory (1 dir) - contents not counted since whole dir skipped
assert_eq!(summary.files_removed, 2, "should remove 2 files");
assert_eq!(summary.symlinks_removed, 2, "should remove 2 symlinks");
assert_eq!(
summary.directories_removed, 1,
"should remove 1 directory (baz only, foo not empty)"
);
assert_eq!(
summary.directories_skipped, 1,
"should skip 1 directory (bar)"
);
// bar should still exist
assert!(
test_path.join("foo/bar").exists(),
"bar directory should still exist"
);
// foo should still exist (not empty because bar is still there)
assert!(
test_path.join("foo").exists(),
"foo directory should still exist (contains bar)"
);
Ok(())
}
/// Test that empty directories are not removed when they were only traversed to look
/// for matches (regression test for bug where --include='foo' would remove empty dir baz).
#[tokio::test]
#[traced_test]
async fn test_empty_dir_not_removed_when_only_traversed() -> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
// create structure:
// test/
// foo (file)
// bar (file)
// baz/ (empty directory)
tokio::fs::write(test_path.join("foo"), "content").await?;
tokio::fs::write(test_path.join("bar"), "content").await?;
tokio::fs::create_dir(test_path.join("baz")).await?;
// include only 'foo' file
let mut filter = FilterSettings::new();
filter.add_include("foo").unwrap();
let summary = rm(
&PROGRESS,
&test_path,
&Settings {
fail_early: false,
filter: Some(filter),
dry_run: None,
time_filter: None,
},
)
.await?;
// only 'foo' should be removed
assert_eq!(summary.files_removed, 1, "should remove only 'foo' file");
assert_eq!(
summary.directories_removed, 0,
"should NOT remove empty 'baz' directory"
);
// verify foo was removed
assert!(!test_path.join("foo").exists(), "foo should be removed");
// verify bar still exists (not matching include pattern)
assert!(test_path.join("bar").exists(), "bar should still exist");
// verify empty baz directory still exists
assert!(
test_path.join("baz").exists(),
"empty baz directory should NOT be removed"
);
Ok(())
}
/// Test that empty directories ARE removed with exclude-only filters.
/// Unlike include filters (where empty dirs are only traversed for matches),
/// exclude-only filters should not prevent removal of empty directories.
#[tokio::test]
#[traced_test]
async fn test_exclude_only_removes_empty_directory() -> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
// create structure:
// test/
// foo (file)
// bar.log (file)
// baz/ (empty directory)
tokio::fs::write(test_path.join("foo"), "content").await?;
tokio::fs::write(test_path.join("bar.log"), "content").await?;
tokio::fs::create_dir(test_path.join("baz")).await?;
// exclude only .log files
let mut filter = FilterSettings::new();
filter.add_exclude("*.log").unwrap();
let summary = rm(
&PROGRESS,
&test_path,
&Settings {
fail_early: false,
filter: Some(filter),
dry_run: None,
time_filter: None,
},
)
.await?;
// foo should be removed, bar.log should be skipped, baz/ should be removed
assert_eq!(summary.files_removed, 1, "should remove 'foo'");
assert_eq!(summary.files_skipped, 1, "should skip 'bar.log'");
assert_eq!(
summary.directories_removed, 1,
"should remove empty 'baz' directory"
);
assert!(!test_path.join("foo").exists(), "foo should be removed");
assert!(
test_path.join("bar.log").exists(),
"bar.log should still exist"
);
assert!(
!test_path.join("baz").exists(),
"empty baz directory should be removed"
);
Ok(())
}
/// Test that empty directories are not removed in dry-run mode when only traversed.
#[tokio::test]
#[traced_test]
async fn test_dry_run_empty_dir_not_reported_as_removed() -> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
// create structure:
// test/
// foo (file)
// bar (file)
// baz/ (empty directory)
tokio::fs::write(test_path.join("foo"), "content").await?;
tokio::fs::write(test_path.join("bar"), "content").await?;
tokio::fs::create_dir(test_path.join("baz")).await?;
// include only 'foo' file
let mut filter = FilterSettings::new();
filter.add_include("foo").unwrap();
let summary = rm(
&PROGRESS,
&test_path,
&Settings {
fail_early: false,
filter: Some(filter),
dry_run: Some(DryRunMode::Explain),
time_filter: None,
},
)
.await?;
// only 'foo' should be reported as would-be-removed
assert_eq!(
summary.files_removed, 1,
"should report only 'foo' would be removed"
);
assert_eq!(
summary.directories_removed, 0,
"should NOT report empty 'baz' would be removed"
);
// verify nothing was actually removed (dry-run mode)
assert!(test_path.join("foo").exists(), "foo should still exist");
assert!(test_path.join("bar").exists(), "bar should still exist");
assert!(test_path.join("baz").exists(), "baz should still exist");
Ok(())
}
/// Test that an empty directory directly matching an include pattern IS removed.
/// Unlike traversed-only directories, directly matched ones are explicit targets.
#[tokio::test]
#[traced_test]
async fn test_include_directly_matched_empty_dir_is_removed() -> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
// create structure:
// test/
// foo (file)
// baz/ (empty directory)
tokio::fs::write(test_path.join("foo"), "content").await?;
tokio::fs::create_dir(test_path.join("baz")).await?;
// include pattern that directly matches the directory
let mut filter = FilterSettings::new();
filter.add_include("baz/").unwrap();
let summary = rm(
&PROGRESS,
&test_path,
&Settings {
fail_early: false,
filter: Some(filter),
dry_run: None,
time_filter: None,
},
)
.await?;
assert_eq!(
summary.directories_removed, 1,
"should remove directly matched empty 'baz' directory"
);
assert_eq!(summary.files_removed, 0, "should not remove 'foo'");
assert!(test_path.join("foo").exists(), "foo should still exist");
assert!(
!test_path.join("baz").exists(),
"directly matched empty baz directory should be removed"
);
Ok(())
}
}
mod dry_run_tests {
use super::*;
use crate::filter::FilterSettings;
/// Test that dry-run mode doesn't modify permissions on read-only directories.
#[tokio::test]
#[traced_test]
async fn test_dry_run_preserves_readonly_permissions() -> Result<(), anyhow::Error> {
let tmp_dir = testutils::setup_test_dir().await?;
let test_path = tmp_dir.as_path();
let readonly_dir = test_path.join("foo/bar");
// make the directory read-only
tokio::fs::set_permissions(&readonly_dir, std::fs::Permissions::from_mode(0o555))
.await?;
// verify it's read-only
let before_mode = tokio::fs::metadata(&readonly_dir)
.await?
.permissions()
.mode()
& 0o777;
assert_eq!(
before_mode, 0o555,
"directory should be read-only before dry-run"
);
let summary = rm(
&PROGRESS,
&readonly_dir,
&Settings {
fail_early: false,
filter: None,
dry_run: Some(DryRunMode::Brief),
time_filter: None,
},
)
.await?;
// verify the directory still exists (dry-run shouldn't remove it)
assert!(
readonly_dir.exists(),
"directory should still exist after dry-run"
);
// verify permissions weren't changed
let after_mode = tokio::fs::metadata(&readonly_dir)
.await?
.permissions()
.mode()
& 0o777;
assert_eq!(
after_mode, 0o555,
"dry-run should not modify directory permissions"
);
// verify summary shows what would be removed
assert!(
summary.directories_removed > 0 || summary.files_removed > 0,
"dry-run should report what would be removed"
);
Ok(())
}
/// Test that dry-run mode with filtering correctly handles directories that
/// wouldn't be empty after filtering.
#[tokio::test]
#[traced_test]
async fn test_dry_run_with_filter_non_empty_directory() -> Result<(), anyhow::Error> {
let tmp_dir = testutils::setup_test_dir().await?;
let test_path = tmp_dir.as_path();
// test structure from setup_test_dir:
// foo/
// 0.txt
// bar/ (1.txt, 2.txt, 3.txt)
// baz/ (4.txt, 5.txt symlink, 6.txt symlink)
// exclude bar/ - so foo would not be empty after removing (bar still there)
let mut filter = crate::filter::FilterSettings::new();
filter.add_exclude("bar/").unwrap();
let summary = rm(
&PROGRESS,
&test_path.join("foo"),
&Settings {
fail_early: false,
filter: Some(filter),
dry_run: Some(DryRunMode::Brief),
time_filter: None,
},
)
.await?;
// dry-run shouldn't actually remove anything
assert!(
test_path.join("foo").exists(),
"foo should still exist after dry-run"
);
// verify summary reflects what WOULD happen:
// - files: 0.txt, baz/4.txt would be removed = 2
// - symlinks: baz/5.txt, baz/6.txt would be removed = 2
// - directories: baz would be removed, but NOT foo (bar is skipped, so foo not empty)
// - skipped: bar directory = 1
assert_eq!(
summary.files_removed, 2,
"should report 2 files would be removed"
);
assert_eq!(
summary.symlinks_removed, 2,
"should report 2 symlinks would be removed"
);
assert_eq!(
summary.directories_removed, 1,
"should report only baz (not foo) would be removed"
);
assert_eq!(
summary.directories_skipped, 1,
"should report bar directory skipped"
);
Ok(())
}
/// Test that dry-run with exclude-only filter correctly reports empty directories
/// as would-be-removed (unlike include filters where empty dirs are only traversed).
#[tokio::test]
#[traced_test]
async fn test_dry_run_exclude_only_reports_empty_dir_removed() -> Result<(), anyhow::Error>
{
let test_path = testutils::create_temp_dir().await?;
// create structure:
// test/
// foo (file)
// bar.log (file)
// baz/ (empty directory)
tokio::fs::write(test_path.join("foo"), "content").await?;
tokio::fs::write(test_path.join("bar.log"), "content").await?;
tokio::fs::create_dir(test_path.join("baz")).await?;
// exclude only .log files
let mut filter = FilterSettings::new();
filter.add_exclude("*.log").unwrap();
let summary = rm(
&PROGRESS,
&test_path,
&Settings {
fail_early: false,
filter: Some(filter),
dry_run: Some(DryRunMode::Explain),
time_filter: None,
},
)
.await?;
// foo should be reported as would-be-removed, bar.log skipped, baz/ removed
assert_eq!(
summary.files_removed, 1,
"should report 'foo' would be removed"
);
assert_eq!(
summary.files_skipped, 1,
"should report 'bar.log' would be skipped"
);
assert_eq!(
summary.directories_removed, 1,
"should report empty 'baz' directory would be removed"
);
// verify nothing was actually removed (dry-run mode)
assert!(test_path.join("foo").exists(), "foo should still exist");
assert!(
test_path.join("bar.log").exists(),
"bar.log should still exist"
);
assert!(test_path.join("baz").exists(), "baz should still exist");
Ok(())
}
/// Test that dry-run correctly reports removal of an empty directory that directly
/// matches an include pattern (not merely traversed).
#[tokio::test]
#[traced_test]
async fn test_dry_run_include_directly_matched_empty_dir_reported()
-> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
// create structure:
// test/
// foo (file)
// baz/ (empty directory)
tokio::fs::write(test_path.join("foo"), "content").await?;
tokio::fs::create_dir(test_path.join("baz")).await?;
// include pattern that directly matches the directory
let mut filter = FilterSettings::new();
filter.add_include("baz/").unwrap();
let summary = rm(
&PROGRESS,
&test_path,
&Settings {
fail_early: false,
filter: Some(filter),
dry_run: Some(DryRunMode::Explain),
time_filter: None,
},
)
.await?;
assert_eq!(
summary.directories_removed, 1,
"should report directly matched empty 'baz' would be removed"
);
assert_eq!(summary.files_removed, 0, "should not report 'foo'");
// verify nothing was actually removed (dry-run mode)
assert!(test_path.join("foo").exists(), "foo should still exist");
assert!(test_path.join("baz").exists(), "baz should still exist");
Ok(())
}
}
mod time_filter_tests {
use super::*;
use crate::filter::TimeFilter;
fn set_mtime_age(path: &std::path::Path, age: std::time::Duration) -> anyhow::Result<()> {
let past = filetime::FileTime::from_system_time(std::time::SystemTime::now() - age);
filetime::set_file_mtime(path, past)?;
Ok(())
}
/// File with mtime older than threshold is removed.
#[tokio::test]
#[traced_test]
async fn removes_files_older_than_modified_before() -> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
let file = test_path.join("old.txt");
tokio::fs::write(&file, "x").await?;
set_mtime_age(&file, std::time::Duration::from_secs(7200))?;
// age test_path so the root dir passes its own time filter check
set_mtime_age(&test_path, std::time::Duration::from_secs(7200))?;
let summary = rm(
&PROGRESS,
&test_path,
&Settings {
fail_early: false,
filter: None,
time_filter: Some(TimeFilter {
modified_before: Some(std::time::Duration::from_secs(3600)),
created_before: None,
}),
dry_run: None,
},
)
.await?;
assert_eq!(summary.files_removed, 1, "old file should be removed");
assert_eq!(summary.files_skipped, 0);
assert!(!file.exists(), "old.txt should be removed");
Ok(())
}
/// File with mtime newer than threshold is skipped.
#[tokio::test]
#[traced_test]
async fn keeps_files_newer_than_modified_before() -> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
let file = test_path.join("new.txt");
tokio::fs::write(&file, "x").await?;
set_mtime_age(&file, std::time::Duration::from_secs(60))?;
set_mtime_age(&test_path, std::time::Duration::from_secs(7200))?;
let summary = rm(
&PROGRESS,
&test_path,
&Settings {
fail_early: false,
filter: None,
time_filter: Some(TimeFilter {
modified_before: Some(std::time::Duration::from_secs(3600)),
created_before: None,
}),
dry_run: None,
},
)
.await?;
assert_eq!(summary.files_removed, 0, "new file should not be removed");
assert_eq!(summary.files_skipped, 1, "new file should be skipped");
assert!(file.exists(), "new.txt should still exist");
Ok(())
}
/// A fresh subdirectory is descended into (children are handled individually),
/// but the fresh_dir itself is not removed because its own mtime is too recent.
#[tokio::test]
#[traced_test]
async fn fresh_subdirectory_is_descended_but_not_removed() -> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
let old_file = test_path.join("old.txt");
let fresh_dir = test_path.join("fresh_dir");
let fresh_child = fresh_dir.join("fresh_child.txt");
let old_child = fresh_dir.join("old_child.txt");
tokio::fs::write(&old_file, "x").await?;
tokio::fs::create_dir(&fresh_dir).await?;
tokio::fs::write(&fresh_child, "x").await?;
tokio::fs::write(&old_child, "x").await?;
set_mtime_age(&old_file, std::time::Duration::from_secs(7200))?;
set_mtime_age(&old_child, std::time::Duration::from_secs(7200))?;
// fresh_child keeps its recent mtime; so does fresh_dir (we took the mtime
// snapshot before remove_file mutates it)
set_mtime_age(&test_path, std::time::Duration::from_secs(7200))?;
let summary = rm(
&PROGRESS,
&test_path,
&Settings {
fail_early: false,
filter: None,
time_filter: Some(TimeFilter {
modified_before: Some(std::time::Duration::from_secs(3600)),
created_before: None,
}),
dry_run: None,
},
)
.await?;
// we descend into fresh_dir: old_child removed, fresh_child skipped
assert_eq!(summary.files_removed, 2, "old.txt and old_child removed");
assert_eq!(
summary.files_skipped, 1,
"fresh_child skipped inside fresh_dir"
);
assert_eq!(
summary.directories_skipped, 1,
"fresh_dir itself is skipped at removal time"
);
assert_eq!(
summary.directories_removed, 0,
"root survives because fresh_dir is still inside it"
);
assert!(!old_file.exists());
assert!(!old_child.exists(), "old_child inside fresh_dir removed");
assert!(
fresh_dir.exists(),
"fresh_dir kept despite its old child being removed"
);
assert!(fresh_child.exists(), "fresh_child inside fresh_dir kept");
Ok(())
}
/// An old directory that still holds a new (skipped) file survives as non-empty.
/// The leftover-dir case is not treated as an error.
#[tokio::test]
#[traced_test]
async fn old_dir_with_new_file_leaves_non_empty_dir_without_error()
-> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
let old_dir = test_path.join("old_dir");
tokio::fs::create_dir(&old_dir).await?;
let new_file = old_dir.join("new.txt");
tokio::fs::write(&new_file, "x").await?;
set_mtime_age(&new_file, std::time::Duration::from_secs(60))?;
set_mtime_age(&old_dir, std::time::Duration::from_secs(7200))?;
set_mtime_age(&test_path, std::time::Duration::from_secs(7200))?;
let result = rm(
&PROGRESS,
&test_path,
&Settings {
fail_early: false,
filter: None,
time_filter: Some(TimeFilter {
modified_before: Some(std::time::Duration::from_secs(3600)),
created_before: None,
}),
dry_run: None,
},
)
.await;
let summary = result.expect("ENOTEMPTY should not surface as an error");
assert_eq!(summary.files_skipped, 1, "new file should be skipped");
assert_eq!(
summary.directories_removed, 0,
"old_dir cannot be removed while new.txt remains"
);
assert!(old_dir.exists(), "old_dir should still exist");
assert!(new_file.exists(), "new.txt should still exist");
// the 'left intact' message is logged at info level
assert!(
logs_contain("not empty after filtering, leaving it intact"),
"should log ENOTEMPTY case at info"
);
Ok(())
}
/// An old, already-empty directory is removed by the time filter run.
#[tokio::test]
#[traced_test]
async fn old_empty_directory_is_removed() -> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
let old_empty = test_path.join("old_empty");
tokio::fs::create_dir(&old_empty).await?;
set_mtime_age(&old_empty, std::time::Duration::from_secs(7200))?;
set_mtime_age(&test_path, std::time::Duration::from_secs(7200))?;
let summary = rm(
&PROGRESS,
&test_path,
&Settings {
fail_early: false,
filter: None,
time_filter: Some(TimeFilter {
modified_before: Some(std::time::Duration::from_secs(3600)),
created_before: None,
}),
dry_run: None,
},
)
.await?;
// both old_empty and test_path itself are removed
assert_eq!(summary.directories_removed, 2);
assert!(!old_empty.exists());
assert!(!test_path.exists());
Ok(())
}
/// Time filter combines with glob exclude — both must pass for removal.
#[tokio::test]
#[traced_test]
async fn time_filter_combines_with_glob_exclude() -> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
let old_keep = test_path.join("keep.log");
let old_drop = test_path.join("drop.txt");
let new_drop = test_path.join("recent.txt");
tokio::fs::write(&old_keep, "x").await?;
tokio::fs::write(&old_drop, "x").await?;
tokio::fs::write(&new_drop, "x").await?;
set_mtime_age(&old_keep, std::time::Duration::from_secs(7200))?;
set_mtime_age(&old_drop, std::time::Duration::from_secs(7200))?;
set_mtime_age(&new_drop, std::time::Duration::from_secs(60))?;
set_mtime_age(&test_path, std::time::Duration::from_secs(7200))?;
let mut filter = crate::filter::FilterSettings::new();
filter.add_exclude("*.log").unwrap();
let summary = rm(
&PROGRESS,
&test_path,
&Settings {
fail_early: false,
filter: Some(filter),
time_filter: Some(TimeFilter {
modified_before: Some(std::time::Duration::from_secs(3600)),
created_before: None,
}),
dry_run: None,
},
)
.await?;
// only old_drop passes both filters
assert_eq!(summary.files_removed, 1, "only old_drop should be removed");
assert_eq!(
summary.files_skipped, 2,
"old_keep and recent_drop should be skipped"
);
assert!(
old_keep.exists(),
"keep.log excluded by glob, should remain"
);
assert!(!old_drop.exists(), "drop.txt should be removed");
assert!(new_drop.exists(), "recent.txt should remain (too new)");
Ok(())
}
/// Dry-run with time filter previews removal without modifying files.
#[tokio::test]
#[traced_test]
async fn time_filter_with_dry_run() -> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
let old_file = test_path.join("old.txt");
let new_file = test_path.join("new.txt");
tokio::fs::write(&old_file, "x").await?;
tokio::fs::write(&new_file, "x").await?;
set_mtime_age(&old_file, std::time::Duration::from_secs(7200))?;
set_mtime_age(&new_file, std::time::Duration::from_secs(60))?;
set_mtime_age(&test_path, std::time::Duration::from_secs(7200))?;
let summary = rm(
&PROGRESS,
&test_path,
&Settings {
fail_early: false,
filter: None,
time_filter: Some(TimeFilter {
modified_before: Some(std::time::Duration::from_secs(3600)),
created_before: None,
}),
dry_run: Some(DryRunMode::Explain),
},
)
.await?;
assert_eq!(
summary.files_removed, 1,
"should report old file would be removed"
);
assert_eq!(
summary.files_skipped, 1,
"should report new file would be skipped"
);
assert!(old_file.exists(), "old.txt should still exist (dry-run)");
assert!(new_file.exists(), "new.txt should still exist (dry-run)");
Ok(())
}
/// A fresh top-level directory is traversed (its old children are removed),
/// but the root itself is not removed because its own mtime is too recent.
#[tokio::test]
#[traced_test]
async fn fresh_top_level_directory_is_traversed_but_not_removed()
-> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
let old_inside = test_path.join("old.txt");
tokio::fs::write(&old_inside, "x").await?;
set_mtime_age(&old_inside, std::time::Duration::from_secs(7200))?;
// test_path itself is left fresh (recent mtime)
let summary = rm(
&PROGRESS,
&test_path,
&Settings {
fail_early: false,
filter: None,
time_filter: Some(TimeFilter {
modified_before: Some(std::time::Duration::from_secs(3600)),
created_before: None,
}),
dry_run: None,
},
)
.await?;
assert_eq!(
summary.files_removed, 1,
"old child should be removed despite fresh parent"
);
assert_eq!(
summary.directories_skipped, 1,
"fresh root itself is skipped at removal time"
);
assert_eq!(
summary.directories_removed, 0,
"fresh root must not be removed"
);
assert!(test_path.exists(), "fresh root should still exist");
assert!(!old_inside.exists(), "old child should be gone");
Ok(())
}
/// Time filter on a single-file root argument increments skip when too new.
#[tokio::test]
#[traced_test]
async fn time_filter_on_root_file_argument() -> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
let new_file = test_path.join("new.txt");
tokio::fs::write(&new_file, "x").await?;
set_mtime_age(&new_file, std::time::Duration::from_secs(60))?;
let summary = rm(
&PROGRESS,
&new_file,
&Settings {
fail_early: false,
filter: None,
time_filter: Some(TimeFilter {
modified_before: Some(std::time::Duration::from_secs(3600)),
created_before: None,
}),
dry_run: None,
},
)
.await?;
assert_eq!(summary.files_removed, 0);
assert_eq!(
summary.files_skipped, 1,
"root file too new should be skipped"
);
assert!(new_file.exists(), "root file should still exist");
Ok(())
}
}
/// Stress tests exercising max-open-files saturation during rm.
mod max_open_files_tests {
use super::*;
/// wide rm: many files with a very low open-files limit.
/// verifies all files are removed correctly under permit saturation.
#[tokio::test]
#[traced_test]
async fn wide_rm_under_open_files_saturation() -> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
let file_count = 200;
for i in 0..file_count {
tokio::fs::write(
test_path.join(format!("{}.txt", i)),
format!("content-{}", i),
)
.await?;
}
// set a very low limit to force permit contention
throttle::set_max_open_files(4);
let summary = rm(
&PROGRESS,
&test_path,
&Settings {
fail_early: true,
filter: None,
dry_run: None,
time_filter: None,
},
)
.await?;
assert_eq!(summary.files_removed, file_count);
assert_eq!(summary.directories_removed, 1);
assert!(!test_path.exists());
Ok(())
}
/// deep + wide rm: directory tree deeper than the open-files limit, with files
/// at every level. verifies no deadlock occurs (directories don't consume permits).
#[tokio::test]
#[traced_test]
async fn deep_tree_no_deadlock_under_open_files_saturation() -> Result<(), anyhow::Error> {
let test_path = testutils::create_temp_dir().await?;
let depth = 20;
let files_per_level = 5;
let limit = 4;
// create a directory chain deeper than the permit limit, with files at each level
let mut dir = test_path.clone();
for level in 0..depth {
tokio::fs::create_dir_all(&dir).await?;
for f in 0..files_per_level {
tokio::fs::write(
dir.join(format!("f{}_{}.txt", level, f)),
format!("L{}F{}", level, f),
)
.await?;
}
dir = dir.join(format!("d{}", level));
}
throttle::set_max_open_files(limit);
let summary = tokio::time::timeout(
std::time::Duration::from_secs(30),
rm(
&PROGRESS,
&test_path,
&Settings {
fail_early: true,
filter: None,
dry_run: None,
time_filter: None,
},
),
)
.await
.context("rm timed out — possible deadlock")?
.context("rm failed")?;
assert_eq!(summary.files_removed, depth * files_per_level);
assert_eq!(summary.directories_removed, depth);
assert!(!test_path.exists());
Ok(())
}
/// Locks down the boolean used at the rm spawn site to decide whether
/// to pre-acquire a pending-meta permit. A naive `entry_is_dir = false
/// ⇒ pre-acquire` policy treats unknown-typed entries (when
/// `DirEntry::file_type()` fails) as leaves, so the spawned task
/// holds the permit even if the entry is actually a directory and
/// recurses. A chain of such entries can deadlock the pool. The
/// safer pattern — `pre-acquire iff positively-known-not-directory`
/// — keeps the predicate `false` for unknown types.
#[test]
fn pre_acquire_skips_unknown_filetype() -> Result<(), anyhow::Error> {
let tmp = std::env::temp_dir().join(format!(
"rcp_pre_acquire_test_{}_{}",
std::process::id(),
rand::random::<u64>()
));
std::fs::create_dir(&tmp)?;
let dir_path = tmp.join("d");
std::fs::create_dir(&dir_path)?;
let file_path = tmp.join("f");
std::fs::write(&file_path, "x")?;
let dir_ft = std::fs::metadata(&dir_path)?.file_type();
let file_ft = std::fs::metadata(&file_path)?.file_type();
// The exact predicate used in the rm spawn site:
let known_leaf =
|ft: Option<std::fs::FileType>| ft.as_ref().is_some_and(|t| !t.is_dir());
assert!(!known_leaf(None), "unknown filetype must skip pre-acquire");
assert!(!known_leaf(Some(dir_ft)), "directory must skip pre-acquire");
assert!(known_leaf(Some(file_ft)), "regular file must pre-acquire");
std::fs::remove_dir_all(&tmp).ok();
Ok(())
}
}
}