findutils 0.8.0

Rust implementation of GNU findutils
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
// Copyright 2017 Google Inc.
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

mod access;
mod delete;
mod empty;
mod entry;
pub mod exec;
pub mod fs;
mod glob;
mod group;
mod lname;
mod logical_matchers;
mod ls;
mod name;
mod path;
mod perm;
mod printer;
mod printf;
mod prune;
mod quit;
mod regex;
mod samefile;
mod size;
#[cfg(unix)]
mod stat;
pub mod time;
mod type_matcher;
mod user;

use self::access::AccessMatcher;
use self::delete::DeleteMatcher;
use self::empty::EmptyMatcher;
use self::exec::{MultiExecMatcher, SingleExecMatcher};
use self::group::{GroupMatcher, NoGroupMatcher};
use self::lname::LinkNameMatcher;
use self::logical_matchers::{
    AndMatcherBuilder, FalseMatcher, ListMatcherBuilder, NotMatcher, TrueMatcher,
};
use self::name::NameMatcher;
use self::path::PathMatcher;
use self::perm::PermMatcher;
use self::printer::{PrintDelimiter, Printer};
use self::printf::Printf;
use self::prune::PruneMatcher;
use self::quit::QuitMatcher;
use self::regex::RegexMatcher;
use self::samefile::SameFileMatcher;
use self::size::SizeMatcher;
#[cfg(unix)]
use self::stat::{InodeMatcher, LinksMatcher};
use self::time::{
    FileAgeRangeMatcher, FileTimeMatcher, FileTimeType, NewerMatcher, NewerOptionMatcher,
    NewerOptionType, NewerTimeMatcher,
};
use self::type_matcher::{TypeMatcher, XtypeMatcher};
use self::user::{NoUserMatcher, UserMatcher};
use ::regex::Regex;
use chrono::{DateTime, Datelike, NaiveDateTime, Utc};
use fs::FileSystemMatcher;
use ls::Ls;
use std::{
    error::Error,
    fs::{File, Metadata},
    io::Read,
    path::Path,
    str::FromStr,
    time::SystemTime,
};

use super::{Config, Dependencies};

pub use entry::{FileType, WalkEntry, WalkError};

/// Symlink following mode.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Follow {
    /// Never follow symlinks (-P; default).
    Never,
    /// Follow symlinks on root paths only (-H).
    Roots,
    /// Always follow symlinks (-L).
    Always,
}

impl Follow {
    /// Check whether to follow a path of the given depth.
    pub fn follow_at_depth(self, depth: usize) -> bool {
        match self {
            Self::Never => false,
            Self::Roots => depth == 0,
            Self::Always => true,
        }
    }

    /// Get metadata for a [WalkEntry].
    pub fn metadata(self, entry: &WalkEntry) -> Result<Metadata, WalkError> {
        if self.follow_at_depth(entry.depth()) == entry.follow() {
            // Same follow flag, re-use cached metadata
            entry.metadata().cloned()
        } else if !entry.follow() && !entry.file_type().is_symlink() {
            // Not a symlink, re-use cached metadata
            entry.metadata().cloned()
        } else if entry.follow() && entry.file_type().is_symlink() {
            // Broken symlink, re-use cached metadata
            entry.metadata().cloned()
        } else {
            self.metadata_at_depth(entry.path(), entry.depth())
        }
    }

    /// Get metadata for a path from the command line.
    pub fn root_metadata(self, path: impl AsRef<Path>) -> Result<Metadata, WalkError> {
        self.metadata_at_depth(path, 0)
    }

    /// Get metadata for a path, following symlinks as necessary.
    pub fn metadata_at_depth(
        self,
        path: impl AsRef<Path>,
        depth: usize,
    ) -> Result<Metadata, WalkError> {
        let path = path.as_ref();

        if self.follow_at_depth(depth) {
            match path.metadata().map_err(WalkError::from) {
                Ok(meta) => return Ok(meta),
                Err(e) if !e.is_not_found() => return Err(e),
                _ => {}
            }
        }

        Ok(path.symlink_metadata()?)
    }
}

/// Struct holding references to outputs and any inputs that can't be derived
/// from the file/directory info.
pub struct MatcherIO<'a> {
    should_skip_dir: bool,
    exit_code: i32,
    quit: bool,
    deps: &'a dyn Dependencies,
}

impl MatcherIO<'_> {
    pub fn new(deps: &dyn Dependencies) -> MatcherIO<'_> {
        MatcherIO {
            should_skip_dir: false,
            exit_code: 0,
            quit: false,
            deps,
        }
    }

    pub fn mark_current_dir_to_be_skipped(&mut self) {
        self.should_skip_dir = true;
    }

    #[must_use]
    pub fn should_skip_current_dir(&self) -> bool {
        self.should_skip_dir
    }

    pub fn set_exit_code(&mut self, code: i32) {
        self.exit_code = code;
    }

    #[must_use]
    pub fn exit_code(&self) -> i32 {
        self.exit_code
    }

    pub fn quit(&mut self) {
        self.quit = true;
    }

    #[must_use]
    pub fn should_quit(&self) -> bool {
        self.quit
    }

    #[must_use]
    pub fn now(&self) -> SystemTime {
        self.deps.now()
    }
}

/// A basic interface that can be used to determine whether a directory entry
/// is what's being searched for. To a first order approximation, find consists
/// of building a chain of Matcher objects, and then walking a directory tree,
/// passing each entry to the chain of Matchers.
pub trait Matcher: 'static {
    /// Boxes this matcher as a trait object.
    fn into_box(self) -> Box<dyn Matcher>
    where
        Self: Sized,
    {
        Box::new(self)
    }

    /// Returns whether the given file matches the object's predicate.
    fn matches(&self, entry: &WalkEntry, matcher_io: &mut MatcherIO) -> bool;

    /// Returns whether the matcher has any side-effects (e.g. executing a
    /// command, deleting a file). Iff no such matcher exists in the chain, then
    /// the filename will be printed to stdout. While this is a compile-time
    /// fact for most matchers, it's run-time for matchers that contain a
    /// collection of sub-Matchers.
    fn has_side_effects(&self) -> bool {
        // most matchers don't have side-effects, so supply a default implementation.
        false
    }

    /// Notification that find is leaving a given directory.
    fn finished_dir(&self, _finished_directory: &Path, _matcher_io: &mut MatcherIO) {}

    /// Notification that find has finished processing all directories -
    /// allowing for any cleanup that isn't suitable for destructors (e.g.
    /// blocking calls, I/O etc.)
    fn finished(&self, _matcher_io: &mut MatcherIO) {}
}

impl Matcher for Box<dyn Matcher> {
    fn into_box(self) -> Box<dyn Matcher> {
        self
    }

    fn matches(&self, entry: &WalkEntry, matcher_io: &mut MatcherIO) -> bool {
        (**self).matches(entry, matcher_io)
    }

    fn has_side_effects(&self) -> bool {
        (**self).has_side_effects()
    }

    fn finished_dir(&self, finished_directory: &Path, matcher_io: &mut MatcherIO) {
        (**self).finished_dir(finished_directory, matcher_io);
    }

    fn finished(&self, matcher_io: &mut MatcherIO) {
        (**self).finished(matcher_io);
    }
}

#[derive(Debug, Eq, PartialEq)]
pub enum ComparableValue {
    MoreThan(u64),
    EqualTo(u64),
    LessThan(u64),
}

impl ComparableValue {
    fn matches(&self, value: u64) -> bool {
        match *self {
            Self::MoreThan(limit) => value > limit,
            Self::EqualTo(limit) => value == limit,
            Self::LessThan(limit) => value < limit,
        }
    }

    /// same as matches, but takes a signed value
    fn imatches(&self, value: i64) -> bool {
        match *self {
            Self::MoreThan(limit) => value >= 0 && (value as u64) > limit,
            Self::EqualTo(limit) => value >= 0 && (value as u64) == limit,
            Self::LessThan(limit) => value < 0 || (value as u64) < limit,
        }
    }
}

/// Builds a single `AndMatcher` containing the Matcher objects corresponding
/// to the passed in predicate arguments.
pub fn build_top_level_matcher(
    args: &[&str],
    config: &mut Config,
) -> Result<Box<dyn Matcher>, Box<dyn Error>> {
    let (_, top_level_matcher) = (build_matcher_tree(args, config, 0, false))?;

    // if the matcher doesn't have any side-effects, then we default to printing
    if !top_level_matcher.has_side_effects() {
        let mut new_and_matcher = AndMatcherBuilder::new();
        new_and_matcher.new_and_condition(top_level_matcher);
        new_and_matcher.new_and_condition(Printer::new(PrintDelimiter::Newline, None));
        return Ok(new_and_matcher.build());
    }
    Ok(top_level_matcher)
}

/// Helper function for `build_matcher_tree`.
fn are_more_expressions(args: &[&str], index: usize) -> bool {
    (index < args.len() - 1) && args[index + 1] != ")"
}

fn convert_arg_to_number(
    option_name: &str,
    value_as_string: &str,
) -> Result<usize, Box<dyn Error>> {
    match value_as_string.parse::<usize>() {
        Ok(val) => Ok(val),
        _ => Err(From::from(format!(
            "Expected a positive decimal integer argument to {option_name}, but got \
             `{value_as_string}'"
        ))),
    }
}

fn convert_arg_to_comparable_value(
    option_name: &str,
    value_as_string: &str,
) -> Result<ComparableValue, Box<dyn Error>> {
    let re = Regex::new(r"^([-+]?)[-+]?(\d+)$")?;
    if let Some(groups) = re.captures(value_as_string) {
        if let Ok(val) = groups[2].parse::<u64>() {
            return Ok(match &groups[1] {
                "+" => ComparableValue::MoreThan(val),
                "-" => ComparableValue::LessThan(val),
                _ => ComparableValue::EqualTo(val),
            });
        }
    }
    Err(From::from(format!(
        "Expected a decimal integer (with optional + or - prefix) argument \
         to {option_name}, but got `{value_as_string}'"
    )))
}

fn convert_arg_to_comparable_value_and_suffix(
    option_name: &str,
    value_as_string: &str,
) -> Result<(ComparableValue, String), Box<dyn Error>> {
    let re = Regex::new(r"([-+]?)[-+]?(\d+)(.*)$")?;
    if let Some(groups) = re.captures(value_as_string) {
        if let Ok(val) = groups[2].parse::<u64>() {
            return Ok((
                match &groups[1] {
                    "+" => ComparableValue::MoreThan(val),
                    "-" => ComparableValue::LessThan(val),
                    _ => ComparableValue::EqualTo(val),
                },
                groups[3].to_string(),
            ));
        }
    }
    Err(From::from(format!(
        "Expected a decimal integer (with optional + or - prefix) and \
         (optional suffix) argument to {option_name}, but got `{value_as_string}'"
    )))
}

/// This is a function that converts a specific string format into a timestamp.
/// It allows converting a time string of
/// "(week abbreviation) (date), (year) (time)" to a Unix timestamp.
/// such as: "jan 01, 2025 00:00:01" -> 1735689601000
/// When (time) is not provided, it will be automatically filled in as 00:00:00
/// such as: "jan 01, 2025" = "jan 01, 2025 00:00:00" -> 1735689600000
fn parse_date_str_to_timestamps(date_str: &str) -> Option<i64> {
    let regex_pattern =
        r"^(?P<month_day>\w{3} \d{2})?(?:, (?P<year>\d{4}))?(?: (?P<time>\d{2}:\d{2}:\d{2}))?$";
    let re = Regex::new(regex_pattern);

    if let Some(captures) = re.ok()?.captures(date_str) {
        let now = Utc::now();
        let month_day = captures
            .get(1)
            .map_or(format!("{} {}", now.format("%b"), now.format("%d")), |m| {
                m.as_str().to_string()
            });
        // If no year input.
        let year = captures
            .get(2)
            .map_or(now.year(), |m| m.as_str().parse().unwrap());
        // If the user does not enter a specific time, it will be filled with 0
        let time_str = captures.get(3).map_or("00:00:00", |m| m.as_str());
        let date_time_str = format!("{month_day}, {year} {time_str}");
        let datetime = NaiveDateTime::parse_from_str(&date_time_str, "%b %d, %Y %H:%M:%S").ok()?;
        let utc_datetime = DateTime::<Utc>::from_naive_utc_and_offset(datetime, Utc);
        Some(utc_datetime.timestamp_millis())
    } else {
        None
    }
}

/// This function implements the function of matching substrings of
/// X and Y from the -newerXY string.
/// X and Y are constrained to a/B/c/m and t.
/// such as: "-neweraB" -> Some(a, B) "-neweraD" -> None
///
/// Additionally, there is support for the -anewer and -cnewer short arguments. as follows:
/// 1. -anewer is equivalent to -neweram
/// 2. -cnewer is equivalent to - newercm
///
/// If -newer is used it will be resolved to -newermm.
fn parse_str_to_newer_args(input: &str) -> Option<(String, String)> {
    if input.is_empty() {
        return None;
    }

    if input == "-newer" {
        return Some(("m".to_string(), "m".to_string()));
    }

    if input == "-anewer" {
        return Some(("a".to_string(), "m".to_string()));
    }

    if input == "-cnewer" {
        return Some(("c".to_string(), "m".to_string()));
    }

    let re = Regex::new(r"-newer([aBcm])([aBcmt])").unwrap();
    if let Some(captures) = re.captures(input) {
        let x = captures.get(1)?.as_str().to_string();
        let y = captures.get(2)?.as_str().to_string();
        Some((x, y))
    } else {
        None
    }
}

/// Creates a file if it doesn't exist.
/// If it does exist, it will be overwritten.
fn get_or_create_file(path: &str) -> Result<File, Box<dyn Error>> {
    let file = File::create(path)?;
    Ok(file)
}

/// The main "translate command-line args into a matcher" function. Will call
/// itself recursively if it encounters an opening bracket. A successful return
/// consists of a tuple containing the new index into the args array to use (if
/// called recursively) and the resulting matcher.
fn build_matcher_tree(
    args: &[&str],
    config: &mut Config,
    arg_index: usize,
    mut expecting_bracket: bool,
) -> Result<(usize, Box<dyn Matcher>), Box<dyn Error>> {
    let mut top_level_matcher = ListMatcherBuilder::new();

    let mut regex_type = regex::RegexType::default();

    // can't use getopts for a variety or reasons:
    // order of arguments is important
    // arguments can start with + as well as -
    // multiple-character flags don't start with a double dash
    let mut i = arg_index;
    let mut invert_next_matcher = false;
    while i < args.len() {
        let possible_submatcher = match args[i] {
            "-print" => Some(Printer::new(PrintDelimiter::Newline, None).into_box()),
            "-print0" => Some(Printer::new(PrintDelimiter::Null, None).into_box()),
            "-printf" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                i += 1;
                Some(Printf::new(args[i], None)?.into_box())
            }
            "-fprint" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                i += 1;

                let file = get_or_create_file(args[i])?;
                Some(Printer::new(PrintDelimiter::Newline, Some(file)).into_box())
            }
            "-fprintf" => {
                if i >= args.len() - 2 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }

                // Action: -fprintf file format
                // Args + 1: output file path
                // Args + 2: format string
                i += 1;
                let file = get_or_create_file(args[i])?;
                i += 1;
                Some(Printf::new(args[i], Some(file))?.into_box())
            }
            "-fprint0" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                i += 1;

                let file = get_or_create_file(args[i])?;
                Some(Printer::new(PrintDelimiter::Null, Some(file)).into_box())
            }
            "-ls" => Some(Ls::new(None).into_box()),
            "-fls" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                i += 1;

                let file = get_or_create_file(args[i])?;
                Some(Ls::new(Some(file)).into_box())
            }
            "-true" => Some(TrueMatcher.into_box()),
            "-false" => Some(FalseMatcher.into_box()),
            "-lname" | "-ilname" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                i += 1;
                Some(LinkNameMatcher::new(args[i], args[i - 1].starts_with("-i")).into_box())
            }
            "-name" | "-iname" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                i += 1;
                Some(NameMatcher::new(args[i], args[i - 1].starts_with("-i")).into_box())
            }
            "-path" | "-ipath" | "-wholename" | "-iwholename" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                i += 1;
                Some(PathMatcher::new(args[i], args[i - 1].starts_with("-i")).into_box())
            }
            "-readable" => Some(AccessMatcher::Readable.into_box()),
            "-regextype" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                i += 1;
                regex_type = regex::RegexType::from_str(args[i])?;
                Some(TrueMatcher.into_box())
            }
            "-regex" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                i += 1;
                Some(RegexMatcher::new(regex_type, args[i], false)?.into_box())
            }
            "-iregex" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                i += 1;
                Some(RegexMatcher::new(regex_type, args[i], true)?.into_box())
            }
            "-type" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                i += 1;
                Some(TypeMatcher::new(args[i])?.into_box())
            }
            "-xtype" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                i += 1;
                Some(XtypeMatcher::new(args[i])?.into_box())
            }
            "-fstype" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                i += 1;
                Some(FileSystemMatcher::new(args[i].to_string()).into_box())
            }
            "-delete" => {
                // -delete implicitly requires -depth
                config.depth_first = true;
                Some(DeleteMatcher::new().into_box())
            }
            "-newer" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                i += 1;
                Some(NewerMatcher::new(args[i], config.follow)?.into_box())
            }
            "-mtime" | "-atime" | "-ctime" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                let file_time_type = match args[i] {
                    "-atime" => FileTimeType::Accessed,
                    "-ctime" => FileTimeType::Changed,
                    "-mtime" => FileTimeType::Modified,
                    // This shouldn't be possible. We've already checked the value
                    // is one of those three values.
                    _ => unreachable!("Encountered unexpected value {}", args[i]),
                };
                let days = convert_arg_to_comparable_value(args[i], args[i + 1])?;
                i += 1;
                Some(FileTimeMatcher::new(file_time_type, days, config.today_start).into_box())
            }
            "-amin" | "-cmin" | "-mmin" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                let file_time_type = match args[i] {
                    "-amin" => FileTimeType::Accessed,
                    "-cmin" => FileTimeType::Changed,
                    "-mmin" => FileTimeType::Modified,
                    _ => unreachable!("Encountered unexpected value {}", args[i]),
                };
                let minutes = convert_arg_to_comparable_value(args[i], args[i + 1])?;
                i += 1;
                Some(
                    FileAgeRangeMatcher::new(file_time_type, minutes, config.today_start)
                        .into_box(),
                )
            }
            "-size" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                let (size, unit) =
                    convert_arg_to_comparable_value_and_suffix(args[i], args[i + 1])?;
                i += 1;
                Some(SizeMatcher::new(size, &unit)?.into_box())
            }
            "-empty" => Some(EmptyMatcher::new().into_box()),
            "-exec" | "-execdir" => {
                let mut arg_index = i + 1;
                while arg_index < args.len()
                    && args[arg_index] != ";"
                    && (args[arg_index - 1] != "{}" || args[arg_index] != "+")
                {
                    arg_index += 1;
                }
                let required_arg = if arg_index < args.len() && args[arg_index] == "+" {
                    3
                } else {
                    2
                };
                if arg_index < i + required_arg || arg_index == args.len() {
                    // at the minimum we need the executable and the ';'
                    // or the executable and the '{} +'
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                let expression = args[i];
                let executable = args[i + 1];
                let exec_args = &args[i + 2..arg_index];
                i = arg_index;
                match args[arg_index] {
                    ";" => Some(
                        SingleExecMatcher::new(executable, exec_args, expression == "-execdir")?
                            .into_box(),
                    ),
                    "+" => {
                        if exec_args.iter().filter(|x| matches!(**x, "{}")).count() == 1 {
                            Some(
                                MultiExecMatcher::new(
                                    executable,
                                    &exec_args[0..exec_args.len() - 1],
                                    expression == "-execdir",
                                )?
                                .into_box(),
                            )
                        } else {
                            return Err(From::from(
                                "Only one instance of {} is supported with -execdir ... +",
                            ));
                        }
                    }
                    _ => unreachable!("Encountered unexpected value {}", args[arg_index]),
                }
            }
            #[cfg(unix)]
            "-inum" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                let inum = convert_arg_to_comparable_value(args[i], args[i + 1])?;
                i += 1;
                Some(InodeMatcher::new(inum).into_box())
            }
            #[cfg(not(unix))]
            "-inum" => {
                return Err(From::from(
                    "Inode numbers are not available on this platform",
                ));
            }
            #[cfg(unix)]
            "-links" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                let inum = convert_arg_to_comparable_value(args[i], args[i + 1])?;
                i += 1;
                Some(LinksMatcher::new(inum).into_box())
            }
            #[cfg(not(unix))]
            "-links" => {
                return Err(From::from("Link counts are not available on this platform"));
            }
            "-samefile" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                i += 1;
                let path = args[i];
                let matcher = SameFileMatcher::new(path, config.follow)
                    .map_err(|e| format!("{path}: {e}"))?;
                Some(matcher.into_box())
            }
            "-user" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }

                let user = args[i + 1];

                if user.is_empty() {
                    return Err(From::from("The argument to -user should not be empty"));
                }

                i += 1;
                let matcher = UserMatcher::from_user_name(user)
                    .or_else(|| Some(UserMatcher::from_uid(user.parse::<u32>().ok()?)))
                    .ok_or_else(|| format!("{user} is not the name of a known user"))?;
                Some(matcher.into_box())
            }
            "-nouser" => Some(NoUserMatcher {}.into_box()),
            "-uid" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                // check if the argument is a number
                let uid = convert_arg_to_comparable_value(args[i], args[i + 1])?;
                i += 1;
                Some(UserMatcher::from_comparable(uid).into_box())
            }
            "-group" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }

                let group = args[i + 1];

                if group.is_empty() {
                    return Err(From::from(
                        "Argument to -group is empty, but should be a group name",
                    ));
                }

                i += 1;
                let matcher = GroupMatcher::from_group_name(group)
                    .or_else(|| Some(GroupMatcher::from_gid(group.parse::<u32>().ok()?)))
                    .ok_or_else(|| format!("{group} is not the name of an existing group"))?;
                Some(matcher.into_box())
            }
            "-nogroup" => Some(NoGroupMatcher {}.into_box()),
            "-gid" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                // check if the argument is a number
                let gid = convert_arg_to_comparable_value(args[i], args[i + 1])?;
                i += 1;
                Some(GroupMatcher::from_comparable(gid).into_box())
            }
            "-executable" => Some(AccessMatcher::Executable.into_box()),
            "-perm" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                i += 1;
                Some(PermMatcher::new(args[i])?.into_box())
            }
            "-prune" => Some(PruneMatcher::new().into_box()),
            "-quit" => Some(QuitMatcher.into_box()),
            "-writable" => Some(AccessMatcher::Writable.into_box()),
            "-not" | "!" => {
                if !are_more_expressions(args, i) {
                    return Err(From::from(format!(
                        "expected an expression after {}",
                        args[i]
                    )));
                }
                invert_next_matcher = !invert_next_matcher;
                None
            }
            "-and" | "-a" => {
                if !are_more_expressions(args, i) {
                    return Err(From::from(format!(
                        "expected an expression after {}",
                        args[i]
                    )));
                }
                top_level_matcher.check_new_and_condition()?;
                None
            }
            "-or" | "-o" => {
                if !are_more_expressions(args, i) {
                    return Err(From::from(format!(
                        "expected an expression after {}",
                        args[i]
                    )));
                }
                top_level_matcher.new_or_condition(args[i])?;
                None
            }
            "," => {
                if !are_more_expressions(args, i) {
                    return Err(From::from(format!(
                        "expected an expression after {}",
                        args[i]
                    )));
                }
                top_level_matcher.new_list_condition()?;
                None
            }
            "(" => {
                let (new_arg_index, sub_matcher) = build_matcher_tree(args, config, i + 1, true)?;
                i = new_arg_index;
                Some(sub_matcher)
            }
            ")" => {
                if !expecting_bracket {
                    return Err(From::from(
                        "invalid expression: expected expression before closing parentheses ')'.",
                    ));
                }

                let bracket = args[i - 1];
                if bracket == "(" {
                    return Err(From::from(
                        "invalid expression; empty parentheses are not allowed.",
                    ));
                }

                return Ok((i, top_level_matcher.build()));
            }
            "-follow" => {
                // This option affects multiple matchers.
                // 1. It will use noleaf by default. (but -noleaf No change of behavior)
                // Unless -L or -H is specified:
                // 2. changes the behaviour of the -newer predicate.
                // 3. consideration applies to -newerXY, -anewer and -cnewer
                // 4. -type predicate will always match against the type of
                //    the file that a symbolic link points to rather than the link itself.
                //
                // 5. causes the -lname and -ilname predicates always to return false.
                //    (unless they happen to match broken symbolic links)
                config.follow = Follow::Always;
                config.no_leaf_dirs = true;
                Some(TrueMatcher.into_box())
            }
            "-daystart" => {
                config.today_start = true;
                Some(TrueMatcher.into_box())
            }
            "-noleaf" => {
                // No change of behavior
                config.no_leaf_dirs = true;
                Some(TrueMatcher.into_box())
            }
            "-d" | "-depth" => {
                // TODO add warning if it appears after actual testing criterion
                config.depth_first = true;
                Some(TrueMatcher.into_box())
            }
            "-mount" | "-xdev" => {
                // TODO add warning if it appears after actual testing criterion
                config.same_file_system = true;
                Some(TrueMatcher.into_box())
            }
            "-sorted" => {
                // TODO add warning if it appears after actual testing criterion
                config.sorted_output = true;
                Some(TrueMatcher.into_box())
            }
            "-maxdepth" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                config.max_depth = convert_arg_to_number(args[i], args[i + 1])?;
                i += 1;
                Some(TrueMatcher.into_box())
            }
            "-mindepth" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                config.min_depth = convert_arg_to_number(args[i], args[i + 1])?;
                i += 1;
                Some(TrueMatcher.into_box())
            }
            "-help" | "--help" => {
                config.help_requested = true;
                None
            }
            "-version" | "--version" => {
                config.version_requested = true;
                None
            }
            "-files0-from" => {
                if i >= args.len() - 1 {
                    return Err(From::from(format!("missing argument to {}", args[i])));
                }
                let _ = config.files0_argument.insert(args[i + 1].to_string());
                i += 1;
                Some(TrueMatcher.into_box())
            }

            _ => {
                match parse_str_to_newer_args(args[i]) {
                    Some((x_option, y_option)) => {
                        if i >= args.len() - 1 {
                            return Err(From::from(format!("missing argument to {}", args[i])));
                        }
                        #[cfg(target_os = "linux")]
                        if x_option == "B" {
                            return Err(From::from("find: This system does not provide a way to find the birth time of a file."));
                        }
                        if y_option == "t" {
                            let time = args[i + 1];
                            let newer_time_type = NewerOptionType::from_str(x_option.as_str());
                            // Convert args to unix timestamps. (expressed in numeric types)
                            let Some(comparable_time) = parse_date_str_to_timestamps(time) else {
                                return Err(From::from(format!(
                                    "find: I cannot figure out how to interpret ‘{}’ as a date or time",
                                    args[i + 1]
                                )));
                            };
                            i += 1;
                            Some(NewerTimeMatcher::new(newer_time_type, comparable_time).into_box())
                        } else {
                            let file_path = args[i + 1];
                            i += 1;
                            Some(
                                NewerOptionMatcher::new(&x_option, &y_option, file_path)?
                                    .into_box(),
                            )
                        }
                    }
                    None => return Err(From::from(format!("Unrecognized flag: '{}'", args[i]))),
                }
            }
        };
        i += 1;
        if config.help_requested || config.version_requested {
            // Ignore anything, even invalid expressions, after -help/-version
            expecting_bracket = false;
            break;
        }
        if let Some(submatcher) = possible_submatcher {
            if invert_next_matcher {
                top_level_matcher.new_and_condition(NotMatcher::new(submatcher));
                invert_next_matcher = false;
            } else {
                top_level_matcher.new_and_condition(submatcher);
            }
        }
    }
    if expecting_bracket {
        return Err(From::from(
            "invalid expression; I was expecting to find a ')' somewhere but \
             did not see one.",
        ));
    }
    if config.files0_argument.is_some() {
        parse_files0_args(config)?;
    }
    Ok((i, top_level_matcher.build()))
}

// https://www.gnu.org/software/findutils/manual/html_node/find_html/Starting-points.html
// This allows users to take the entry point for find from stdin (eg. pipe) or from a text file.
// eg. dummy | find -files0-from -
// eg. find -files0-from rust.txt -name "cargo"
fn parse_files0_args(config: &mut Config) -> Result<(), Box<dyn Error>> {
    let mode = config.files0_argument.as_ref().unwrap();
    let mut buffer = Vec::new();
    let new_paths = config.new_paths.insert(Vec::new());

    if mode == "-" {
        std::io::stdin().read_to_end(&mut buffer)?;
    } else {
        let mut file =
            File::open(mode).map_err(|e| format!("cannot open '{}' for reading: {}", mode, e))?;
        file.read_to_end(&mut buffer)?;
    }

    let mut buffer_split: Vec<&[u8]> = buffer.split(|&b| b == 0).collect();
    // if the pipe/file ends with ASCII NULL
    if buffer_split.last().is_some_and(|s| s.is_empty()) {
        buffer_split.remove(buffer_split.len() - 1);
    }

    let mut string_segments: Vec<String> = buffer_split
        .iter()
        .filter_map(|s| std::str::from_utf8(s).ok())
        .map(|s| s.to_string())
        .collect();
    // empty starting point checker
    if string_segments.iter().any(|s| s.is_empty()) {
        eprintln!("find: invalid zero-length file name");
        // remove the empty ones so as to avoid file not found error
        string_segments.retain(|s| !s.is_empty());
    }

    new_paths.extend(string_segments);
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::find::tests::fix_up_slashes;
    use crate::find::tests::FakeDependencies;

    /// Helper function for tests to get a [WalkEntry] object. root should
    /// probably be a string starting with `test_data/` (cargo's tests run with
    /// a working directory set to the root findutils folder).
    pub fn get_dir_entry_for(root: &str, path: &str) -> WalkEntry {
        get_dir_entry_follow(root, path, Follow::Never)
    }

    /// Get a [WalkEntry] with an explicit [Follow] flag.
    pub fn get_dir_entry_follow(root: &str, path: &str, follow: Follow) -> WalkEntry {
        let root = fix_up_slashes(root);
        let root = Path::new(&root);

        let path = fix_up_slashes(path);
        let path = if path.is_empty() {
            root.to_owned()
        } else {
            root.join(path)
        };

        let depth = path.components().count() - root.components().count();
        WalkEntry::new(path, depth, follow)
    }

    #[test]
    fn build_top_level_matcher_name() {
        let abbbc_lower = get_dir_entry_for("./test_data/simple", "abbbc");
        let abbbc_upper = get_dir_entry_for("./test_data/simple/subdir", "ABBBC");
        let mut config = Config::default();
        let deps = FakeDependencies::new();

        let matcher = build_top_level_matcher(&["-name", "a*c"], &mut config).unwrap();

        assert!(matcher.matches(&abbbc_lower, &mut deps.new_matcher_io()));
        assert!(!matcher.matches(&abbbc_upper, &mut deps.new_matcher_io()));
        assert_eq!(
            deps.get_output_as_string(),
            fix_up_slashes("./test_data/simple/abbbc\n")
        );
    }

    #[test]
    fn build_top_level_matcher_iname() {
        let abbbc_lower = get_dir_entry_for("./test_data/simple", "abbbc");
        let abbbc_upper = get_dir_entry_for("./test_data/simple/subdir", "ABBBC");
        let mut config = Config::default();
        let deps = FakeDependencies::new();

        let matcher = build_top_level_matcher(&["-iname", "a*c"], &mut config).unwrap();

        assert!(matcher.matches(&abbbc_lower, &mut deps.new_matcher_io()));
        assert!(matcher.matches(&abbbc_upper, &mut deps.new_matcher_io()));
        assert_eq!(
            deps.get_output_as_string(),
            fix_up_slashes("./test_data/simple/abbbc\n./test_data/simple/subdir/ABBBC\n")
        );
    }

    #[test]
    fn build_top_level_matcher_not() {
        for arg in &["-not", "!"] {
            let abbbc_lower = get_dir_entry_for("./test_data/simple", "abbbc");
            let mut config = Config::default();
            let deps = FakeDependencies::new();

            let matcher =
                build_top_level_matcher(&[arg, "-name", "does_not_exist"], &mut config).unwrap();

            assert!(matcher.matches(&abbbc_lower, &mut deps.new_matcher_io()));
            assert_eq!(
                deps.get_output_as_string(),
                fix_up_slashes("./test_data/simple/abbbc\n")
            );
        }
    }

    #[test]
    fn build_top_level_matcher_not_needs_expression() {
        for arg in &["-not", "!"] {
            let mut config = Config::default();

            if let Err(e) = build_top_level_matcher(&[arg], &mut config) {
                assert!(e.to_string().contains("expected an expression"));
            } else {
                panic!("parsing argument lists that end in -not should fail");
            }
        }
    }

    #[test]
    fn build_top_level_matcher_not_double_negation() {
        for arg in &["-not", "!"] {
            let abbbc_lower = get_dir_entry_for("./test_data/simple", "abbbc");
            let mut config = Config::default();
            let deps = FakeDependencies::new();

            let matcher =
                build_top_level_matcher(&[arg, arg, "-name", "abbbc"], &mut config).unwrap();

            assert!(matcher.matches(&abbbc_lower, &mut deps.new_matcher_io()));
            assert_eq!(
                deps.get_output_as_string(),
                fix_up_slashes("./test_data/simple/abbbc\n")
            );

            config = Config::default();
            let matcher =
                build_top_level_matcher(&[arg, arg, "-name", "does_not_exist"], &mut config)
                    .unwrap();

            assert!(!matcher.matches(&abbbc_lower, &mut deps.new_matcher_io()));
        }
    }

    #[test]
    fn build_top_level_matcher_missing_args() {
        for arg in &["-iname", "-name", "-type"] {
            let mut config = Config::default();

            if let Err(e) = build_top_level_matcher(&[arg], &mut config) {
                assert!(e.to_string().contains("missing argument to"));
                assert!(e.to_string().contains(arg));
            } else {
                panic!("parsing argument lists that end in -not should fail");
            }
        }
    }

    #[test]
    fn build_top_level_matcher_or_without_expr1() {
        for arg in &["-or", "-o"] {
            let mut config = Config::default();

            if let Err(e) = build_top_level_matcher(&[arg, "-true"], &mut config) {
                assert!(e.to_string().contains("you have used a binary operator"));
            } else {
                panic!("parsing argument list that begins with -or should fail");
            }
        }
    }

    #[test]
    fn build_top_level_matcher_or_without_expr2() {
        for arg in &["-or", "-o"] {
            let mut config = Config::default();

            if let Err(e) = build_top_level_matcher(&["-true", arg], &mut config) {
                assert!(e.to_string().contains("expected an expression"));
            } else {
                panic!("parsing argument list that ends with -or should fail");
            }
        }
    }

    #[test]
    fn build_top_level_matcher_and_without_expr1() {
        let mut config = Config::default();

        if let Err(e) = build_top_level_matcher(&["-a", "-true"], &mut config) {
            assert!(e.to_string().contains("you have used a binary operator"));
        } else {
            panic!("parsing argument list that begins with -a should fail");
        }
    }

    #[test]
    fn build_top_level_matcher_and_without_expr2() {
        let mut config = Config::default();

        if let Err(e) = build_top_level_matcher(&["-true", "-a"], &mut config) {
            assert!(e.to_string().contains("expected an expression"));
        } else {
            panic!("parsing argument list that ends with -or should fail");
        }
    }

    #[test]
    fn build_top_level_matcher_dash_a_works() {
        for arg in &["-a", "-and"] {
            let abbbc = get_dir_entry_for("./test_data/simple", "abbbc");
            let mut config = Config::default();
            let deps = FakeDependencies::new();

            // build a matcher using an explicit -a argument
            let matcher = build_top_level_matcher(&["-true", arg, "-true"], &mut config).unwrap();
            assert!(matcher.matches(&abbbc, &mut deps.new_matcher_io()));
            assert_eq!(
                deps.get_output_as_string(),
                fix_up_slashes("./test_data/simple/abbbc\n")
            );
        }
    }

    #[test]
    fn build_top_level_matcher_or_works() {
        let abbbc = get_dir_entry_for("./test_data/simple", "abbbc");
        for args in &[
            ["-true", "-o", "-false"],
            ["-false", "-o", "-true"],
            ["-true", "-o", "-true"],
        ] {
            let mut config = Config::default();
            let deps = FakeDependencies::new();

            let matcher = build_top_level_matcher(args, &mut config).unwrap();

            assert!(matcher.matches(&abbbc, &mut deps.new_matcher_io()));
            assert_eq!(
                deps.get_output_as_string(),
                fix_up_slashes("./test_data/simple/abbbc\n")
            );
        }

        let mut config = Config::default();
        let deps = FakeDependencies::new();

        let matcher = build_top_level_matcher(&["-false", "-o", "-false"], &mut config).unwrap();

        assert!(!matcher.matches(&abbbc, &mut deps.new_matcher_io()));
        assert_eq!(deps.get_output_as_string(), "");
    }

    #[test]
    fn build_top_level_matcher_and_works() {
        let abbbc = get_dir_entry_for("./test_data/simple", "abbbc");
        for args in &[
            ["-true", "-false"],
            ["-false", "-true"],
            ["-false", "-false"],
        ] {
            let mut config = Config::default();
            let deps = FakeDependencies::new();

            let matcher = build_top_level_matcher(args, &mut config).unwrap();

            assert!(!matcher.matches(&abbbc, &mut deps.new_matcher_io()));
            assert_eq!(deps.get_output_as_string(), "");
        }

        let mut config = Config::default();
        let deps = FakeDependencies::new();

        let matcher = build_top_level_matcher(&["-true", "-true"], &mut config).unwrap();

        assert!(matcher.matches(&abbbc, &mut deps.new_matcher_io()));
        assert_eq!(
            deps.get_output_as_string(),
            fix_up_slashes("./test_data/simple/abbbc\n")
        );
    }

    #[test]
    fn build_top_level_matcher_list_works() {
        let abbbc = get_dir_entry_for("./test_data/simple", "abbbc");
        let args = ["-true", "-print", "-false", ",", "-print", "-false"];
        let mut config = Config::default();
        let deps = FakeDependencies::new();

        let matcher = build_top_level_matcher(&args, &mut config).unwrap();

        // final matcher returns false, so list matcher should too
        assert!(!matcher.matches(&abbbc, &mut deps.new_matcher_io()));
        // two print matchers means doubled output
        assert_eq!(
            deps.get_output_as_string(),
            fix_up_slashes("./test_data/simple/abbbc\n./test_data/simple/abbbc\n")
        );
    }

    #[test]
    fn build_top_level_matcher_list_without_expr1() {
        let mut config = Config::default();

        if let Err(e) = build_top_level_matcher(&[",", "-true"], &mut config) {
            assert!(e.to_string().contains("you have used a binary operator"));
        } else {
            panic!("parsing argument list that begins with , should fail");
        }

        if let Err(e) = build_top_level_matcher(&["-true", "-o", ",", "-true"], &mut config) {
            assert!(e.to_string().contains("you have used a binary operator"));
        } else {
            panic!("parsing argument list that contains '-o  ,' should fail");
        }
    }

    #[test]
    fn build_top_level_matcher_list_without_expr2() {
        let mut config = Config::default();

        if let Err(e) = build_top_level_matcher(&["-true", ","], &mut config) {
            assert!(e.to_string().contains("expected an expression"));
        } else {
            panic!("parsing argument list that ends with , should fail");
        }
    }

    #[test]
    fn build_top_level_matcher_not_enough_brackets() {
        let mut config = Config::default();

        if let Err(e) = build_top_level_matcher(&["-true", "("], &mut config) {
            assert!(e.to_string().contains("I was expecting to find a ')'"));
        } else {
            panic!("parsing argument list with not enough closing brackets should fail");
        }
    }

    #[test]
    fn build_top_level_matcher_too_many_brackets() {
        let mut config = Config::default();

        if let Err(e) = build_top_level_matcher(
            &["-type", "f", "(", "-name", "*.txt", ")", ")"],
            &mut config,
        ) {
            assert!(e
                .to_string()
                .contains("expected expression before closing parentheses ')'"));
        } else {
            panic!("parsing argument list with too many closing brackets should fail");
        }
    }

    #[test]
    fn build_top_level_matcher_can_use_bracket_as_arg() {
        let mut config = Config::default();
        // make sure that if we use a bracket as an argument (e.g. to -name)
        // then it isn't viewed as a bracket
        build_top_level_matcher(&["-name", "("], &mut config).unwrap();
        build_top_level_matcher(&["-name", ")"], &mut config).unwrap();
    }

    #[test]
    fn build_top_level_matcher_brackets_work() {
        let abbbc = get_dir_entry_for("./test_data/simple", "abbbc");
        // same as true | ( false & false) = true
        let args_without = ["-true", "-o", "-false", "-false"];
        // same as (true | false) & false = false
        let args_with = ["(", "-true", "-o", "-false", ")", "-false"];
        let mut config = Config::default();
        let deps = FakeDependencies::new();

        {
            let matcher = build_top_level_matcher(&args_without, &mut config).unwrap();
            assert!(matcher.matches(&abbbc, &mut deps.new_matcher_io()));
        }
        {
            let matcher = build_top_level_matcher(&args_with, &mut config).unwrap();
            assert!(!matcher.matches(&abbbc, &mut deps.new_matcher_io()));
        }
    }

    #[test]
    fn build_top_level_matcher_not_and_brackets_work() {
        let abbbc = get_dir_entry_for("./test_data/simple", "abbbc");
        // same as (true & !(false)) | true = true
        let args_without = ["-true", "-not", "-false", "-o", "-true"];
        // same as true & !(false | true) = false
        let args_with = ["-true", "-not", "(", "-false", "-o", "-true", ")"];
        let mut config = Config::default();
        let deps = FakeDependencies::new();

        {
            let matcher = build_top_level_matcher(&args_without, &mut config).unwrap();
            assert!(matcher.matches(&abbbc, &mut deps.new_matcher_io()));
        }
        {
            let matcher = build_top_level_matcher(&args_with, &mut config).unwrap();
            assert!(!matcher.matches(&abbbc, &mut deps.new_matcher_io()));
        }
    }

    #[test]
    fn build_top_level_matcher_expression_empty_parentheses() {
        let mut config = Config::default();

        if let Err(e) = build_top_level_matcher(&["-true", "(", ")"], &mut config) {
            assert!(e.to_string().contains("empty parentheses are not allowed"));
        } else {
            panic!("parsing argument list with empty parentheses in an expression should fail");
        }
    }

    #[test]
    fn build_top_level_matcher_follow_config() {
        let mut config = Config::default();

        build_top_level_matcher(&["-follow"], &mut config).unwrap();

        assert_eq!(config.follow, Follow::Always);
    }

    #[test]
    fn comparable_value_matches() {
        assert!(
            !ComparableValue::LessThan(0).matches(0),
            "0 should not be less than 0"
        );
        assert!(
            ComparableValue::LessThan(u64::MAX).matches(0),
            "0 should be less than max_value"
        );
        assert!(
            !ComparableValue::LessThan(0).matches(u64::MAX),
            "max_value should not be less than 0"
        );
        assert!(
            !ComparableValue::LessThan(u64::MAX).matches(u64::MAX),
            "max_value should not be less than max_value"
        );

        assert!(
            ComparableValue::EqualTo(0).matches(0),
            "0 should be equal to 0"
        );
        assert!(
            !ComparableValue::EqualTo(u64::MAX).matches(0),
            "0 should not be equal to max_value"
        );
        assert!(
            !ComparableValue::EqualTo(0).matches(u64::MAX),
            "max_value should not be equal to 0"
        );
        assert!(
            ComparableValue::EqualTo(u64::MAX).matches(u64::MAX),
            "max_value should be equal to max_value"
        );

        assert!(
            !ComparableValue::MoreThan(0).matches(0),
            "0 should not be more than 0"
        );
        assert!(
            !ComparableValue::MoreThan(u64::MAX).matches(0),
            "0 should not be more than max_value"
        );
        assert!(
            ComparableValue::MoreThan(0).matches(u64::MAX),
            "max_value should be more than 0"
        );
        assert!(
            !ComparableValue::MoreThan(u64::MAX).matches(u64::MAX),
            "max_value should not be more than max_value"
        );
    }

    #[test]
    fn comparable_value_imatches() {
        assert!(
            !ComparableValue::LessThan(0).imatches(0),
            "0 should not be less than 0"
        );
        assert!(
            ComparableValue::LessThan(u64::MAX).imatches(0),
            "0 should be less than max_value"
        );
        assert!(
            !ComparableValue::LessThan(0).imatches(i64::MAX),
            "max_value should not be less than 0"
        );
        assert!(
            ComparableValue::LessThan(u64::MAX).imatches(i64::MAX),
            "max_value should be less than max_value"
        );
        assert!(
            ComparableValue::LessThan(0).imatches(i64::MIN),
            "min_value should be less than 0"
        );
        assert!(
            ComparableValue::LessThan(u64::MAX).imatches(i64::MIN),
            "min_value should be less than max_value"
        );

        assert!(
            ComparableValue::EqualTo(0).imatches(0),
            "0 should be equal to 0"
        );
        assert!(
            !ComparableValue::EqualTo(u64::MAX).imatches(0),
            "0 should not be equal to max_value"
        );
        assert!(
            !ComparableValue::EqualTo(0).imatches(i64::MAX),
            "max_value should not be equal to 0"
        );
        assert!(
            !ComparableValue::EqualTo(u64::MAX).imatches(i64::MAX),
            "max_value should not be equal to i64::max_value"
        );
        assert!(
            ComparableValue::EqualTo(i64::MAX as u64).imatches(i64::MAX),
            "i64::max_value should be equal to i64::max_value"
        );
        assert!(
            !ComparableValue::EqualTo(0).imatches(i64::MIN),
            "min_value should not be equal to 0"
        );
        assert!(
            !ComparableValue::EqualTo(u64::MAX).imatches(i64::MIN),
            "min_value should not be equal to max_value"
        );

        assert!(
            !ComparableValue::MoreThan(0).imatches(0),
            "0 should not be more than 0"
        );
        assert!(
            !ComparableValue::MoreThan(u64::MAX).imatches(0),
            "0 should not be more than max_value"
        );
        assert!(
            ComparableValue::MoreThan(0).imatches(i64::MAX),
            "max_value should be more than 0"
        );
        assert!(
            !ComparableValue::MoreThan(u64::MAX).imatches(i64::MAX),
            "max_value should not be more than max_value"
        );
        assert!(
            !ComparableValue::MoreThan(0).imatches(i64::MIN),
            "min_value should not be more than 0"
        );
        assert!(
            !ComparableValue::MoreThan(u64::MAX).imatches(i64::MIN),
            "min_value should not be more than max_value"
        );
    }

    #[test]
    fn build_top_level_matcher_bad_ctime_value() {
        let mut config = Config::default();

        if let Err(e) = build_top_level_matcher(&["-ctime", "-123."], &mut config) {
            assert!(
                e.to_string().contains("Expected a decimal integer"),
                "bad description: {e}"
            );
        } else {
            panic!("parsing a bad ctime value should fail");
        }
    }

    #[test]
    fn build_top_level_exec_not_enough_args() {
        let mut config = Config::default();

        if let Err(e) = build_top_level_matcher(&["-exec"], &mut config) {
            assert!(e.to_string().contains("missing argument"));
        } else {
            panic!("parsing argument list with exec and no executable or semi-colon should fail");
        }

        if let Err(e) = build_top_level_matcher(&["-exec", ";"], &mut config) {
            assert!(e.to_string().contains("missing argument"));
        } else {
            panic!("parsing argument list with exec and no executable should fail");
        }

        if let Err(e) = build_top_level_matcher(&["-exec", "foo"], &mut config) {
            assert!(e.to_string().contains("missing argument"));
        } else {
            panic!("parsing argument list with exec and no executable should fail");
        }

        if let Err(e) = build_top_level_matcher(&["-exec", "+"], &mut config) {
            assert!(e.to_string().contains("missing argument"));
        } else {
            panic!("parsing argument list with exec and no executable should fail");
        }

        if let Err(e) = build_top_level_matcher(&["-exec", "foo", "+"], &mut config) {
            assert!(e.to_string().contains("missing argument"));
        } else {
            panic!("parsing argument list with exec and no brackets should fail");
        }

        if let Err(e) = build_top_level_matcher(&["-exec", "{}", "+"], &mut config) {
            assert!(e.to_string().contains("missing argument"));
        } else {
            panic!("parsing argument list with exec and no executable should fail");
        }

        if let Err(e) = build_top_level_matcher(&["-exec", "foo", "{}", "foo", "+"], &mut config) {
            assert!(e.to_string().contains("missing argument"));
        } else {
            panic!("parsing argument list with exec and + not following {{}} should fail");
        }
    }

    #[test]
    fn build_top_level_exec_should_eat_args() {
        let mut config = Config::default();
        build_top_level_matcher(&["-exec", "foo", "-o", "(", ";"], &mut config)
            .expect("parsing argument list with exec that takes brackets and -os should work");
    }

    #[test]
    fn build_top_level_exec_plus_semicolon() {
        let mut config = Config::default();
        build_top_level_matcher(&["-exec", "foo", "{}", "foo", "+", ";"], &mut config)
            .expect("only {} + should be considered a multi-exec");
    }

    #[test]
    fn build_top_level_multi_exec_too_many_holders() {
        let mut config = Config::default();
        if let Err(e) =
            build_top_level_matcher(&["-exec", "foo", "{}", "foo", "{}", "+", ";"], &mut config)
        {
            assert!(e.to_string().contains("Only one instance of {}"));
        } else {
            panic!("parsing argument list with more than one {{}} for + should fail");
        }
    }

    #[test]
    #[cfg(unix)]
    fn build_top_level_matcher_perm() {
        let abbbc = get_dir_entry_for("./test_data/simple", "abbbc");
        let mut config = Config::default();

        // this should match: abbbc is readable
        let matcher_readable = build_top_level_matcher(&["-perm", "-u+r"], &mut config).unwrap();
        // this shouldn't match: abbbc isn't executable
        let matcher_executable = build_top_level_matcher(&["-perm", "-u+x"], &mut config).unwrap();

        let deps = FakeDependencies::new();
        assert!(matcher_readable.matches(&abbbc, &mut deps.new_matcher_io()));
        assert_eq!(deps.get_output_as_string(), "./test_data/simple/abbbc\n");

        let deps = FakeDependencies::new();
        assert!(!matcher_executable.matches(&abbbc, &mut deps.new_matcher_io()));
        assert_eq!(deps.get_output_as_string(), "");
    }

    #[test]
    #[cfg(unix)]
    fn build_top_level_matcher_perm_bad() {
        let mut config = Config::default();
        if let Err(e) = build_top_level_matcher(&["-perm", "foo"], &mut config) {
            assert!(e.to_string().contains("invalid operator"));
        } else {
            panic!("-perm with bad mode pattern should fail");
        }

        if let Err(e) = build_top_level_matcher(&["-perm"], &mut config) {
            assert!(e.to_string().contains("missing argument"));
        } else {
            panic!("-perm with no mode pattern should fail");
        }
    }

    #[test]
    #[cfg(not(unix))]
    fn build_top_level_matcher_perm_not_unix() {
        let mut config = Config::default();
        if let Err(e) = build_top_level_matcher(&["-perm", "444"], &mut config) {
            assert!(e.to_string().contains("not available"));
        } else {
            panic!("-perm on non-unix systems shouldn't be available");
        }

        if let Err(e) = build_top_level_matcher(&["-perm"], &mut config) {
            assert!(e.to_string().contains("missing argument"));
        } else {
            panic!("-perm with no mode pattern should fail");
        }
    }

    #[test]
    fn convert_arg_to_comparable_value_test() {
        assert_eq!(
            convert_arg_to_comparable_value("test", "10").unwrap(),
            ComparableValue::EqualTo(10),
        );
        assert_eq!(
            convert_arg_to_comparable_value("test", "+10").unwrap(),
            ComparableValue::MoreThan(10),
        );
        assert_eq!(
            convert_arg_to_comparable_value("test", "-10").unwrap(),
            ComparableValue::LessThan(10),
        );

        assert_eq!(
            convert_arg_to_comparable_value("test", "-+10").unwrap(),
            ComparableValue::LessThan(10),
        );
        assert_eq!(
            convert_arg_to_comparable_value("test", "++10").unwrap(),
            ComparableValue::MoreThan(10),
        );
    }

    #[test]
    fn convert_exception_arg_to_comparable_value_test() {
        let exception_args = ["1%2", "1%2%3", "1a2", "1%2a", "abc", "-", "+", "%"];

        for arg in exception_args {
            let comparable = convert_arg_to_comparable_value("test", arg);
            assert!(
                comparable.is_err(),
                "{} should be parse to Comparable correctly",
                arg
            );
        }
    }

    #[test]
    fn parse_date_str_to_timestamps_test() {
        let full_date_timestamps = parse_date_str_to_timestamps("jan 01, 2025 00:00:01").unwrap();
        assert!(full_date_timestamps.to_string().contains("1735689601000"));

        let not_include_time_date_timestamps =
            parse_date_str_to_timestamps("jan 01, 2025").unwrap();
        assert!(not_include_time_date_timestamps
            .to_string()
            .contains("1735689600000"));

        // pass if return current time.
        let none_date_timestamps = parse_date_str_to_timestamps("");
        let now_but_zero_hour_min_sec = Utc::now()
            .date_naive()
            .and_hms_opt(0, 0, 0)
            .unwrap()
            .and_utc()
            .timestamp_millis();
        assert_eq!(none_date_timestamps, Some(now_but_zero_hour_min_sec));
    }

    #[test]
    fn parse_str_to_newer_args_test() {
        // test for error case
        let arg = parse_str_to_newer_args("");
        assert!(arg.is_none());

        // test for short options
        // -newer equivalent to -newermm
        let arg = parse_str_to_newer_args("-newer").unwrap();
        assert_eq!(("m".to_string(), "m".to_string()), arg);

        // -anewer equivalent to -neweram
        let arg = parse_str_to_newer_args("-anewer").unwrap();
        assert_eq!(("a".to_string(), "m".to_string()), arg);

        // -cnewer equivalent to - newercm
        let arg = parse_str_to_newer_args("-cnewer").unwrap();
        assert_eq!(("c".to_string(), "m".to_string()), arg);

        let x_options = ["a", "B", "c", "m"];
        let y_options = ["a", "B", "c", "m", "t"];

        for &x in &x_options {
            for &y in &y_options {
                let eq: (String, String) = (String::from(x), String::from(y));
                let arg = parse_str_to_newer_args(&format!("-newer{x}{y}").to_string()).unwrap();
                assert_eq!(eq, arg);
            }
        }
    }

    #[test]
    fn build_top_level_matcher_option_logical() {
        let mut config = Config::default();
        build_top_level_matcher(&["-maxdepth", "0", "-a", "-print"], &mut config)
            .expect("logical operators like -a should work with options");
        assert_eq!(config.max_depth, 0);
    }

    #[test]
    fn build_top_level_matcher_help_invalid() {
        let mut config = Config::default();
        build_top_level_matcher(&["(", "-help", "-a"], &mut config)
            .expect("-help should stop parsing");
        assert!(config.help_requested);
    }

    #[test]
    fn build_top_level_matcher_version_invalid() {
        let mut config = Config::default();
        build_top_level_matcher(&["(", "-version", "-o", ")", ")"], &mut config)
            .expect("-version should stop parsing");
        assert!(config.version_requested);
    }

    #[test]
    fn get_or_create_file_test() {
        use std::fs;

        // remove file if hard link file exist.
        // But you can't delete a file that doesn't exist,
        // so ignore the error returned here.
        let _ = fs::remove_file("test_data/get_or_create_file_test");

        // test create file
        let file = get_or_create_file("test_data/get_or_create_file_test");
        assert!(file.is_ok());

        let file = get_or_create_file("test_data/get_or_create_file_test");
        assert!(file.is_ok());

        // test error when file no permission
        #[cfg(unix)]
        {
            let result = get_or_create_file("/etc/shadow");
            assert!(result.is_err());
        }

        let _ = fs::remove_file("test_data/get_or_create_file_test");
    }
}