Skip to content

Commit 553574c

Browse files
committed
find: fix day selection with -daystart
-daystart -mtime 1 selected files from two days ago: truncating the age measured from today's midnight counted both today's and yesterday's files as zero days old. Compare signed ages with GNU find's day ranges, keeping its separate boundaries for n, +n and -n. Use i128 so large day arguments cannot overflow. Add matcher tests for the day ranges and fractional-second boundaries, plus an expression test for -mtime, -atime and the position of -daystart. Cross-checked against GNU find with 108 paired comparisons.
1 parent 3b14698 commit 553574c

2 files changed

Lines changed: 136 additions & 7 deletions

File tree

‎src/find/matchers/time.rs‎

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -312,20 +312,36 @@ impl FileTimeMatcher {
312312
e.duration()
313313
}
314314
};
315+
316+
if self.today_start {
317+
// Relative to today's midnight, GNU's n-day interval is
318+
// [(n - 1) days, n days). Keep fractions at the boundaries and use
319+
// i128 so even u64::MAX days cannot overflow.
320+
let age_in_nanos = age.as_nanos() as i128 * if is_negative { -1 } else { 1 };
321+
let nanos_per_day = i128::from(SECONDS_PER_DAY) * 1_000_000_000;
322+
let boundary = |days| i128::from(days) * nanos_per_day;
323+
return Ok(match self.days {
324+
ComparableValue::EqualTo(days) => {
325+
let end = boundary(days);
326+
(end - nanos_per_day..end).contains(&age_in_nanos)
327+
}
328+
ComparableValue::MoreThan(days) => age_in_nanos > boundary(days),
329+
// GNU's -n also accepts the fractional second just before
330+
// the midnight boundary, but not the preceding whole second.
331+
ComparableValue::LessThan(days) => {
332+
age_in_nanos < boundary(days) - nanos_per_day + 1_000_000_000
333+
}
334+
});
335+
}
336+
315337
let age_in_seconds: i64 = age.as_secs() as i64 * if is_negative { -1 } else { 1 };
316338

317339
// rust division truncates towards zero (see
318340
// https://github.com/rust-lang/rust/blob/master/src/libcore/ops.rs#L580 )
319341
// so a simple age_in_seconds / SECONDS_PER_DAY gives the wrong answer
320342
// for negative ages: a file whose age is 1 second in the future needs to
321343
// count as -1 day old, not 0.
322-
// If today_start is true, we should count it as 0 days old.
323-
// because today is 00:00:00, so we need to subtract 1 day.
324-
let negative_offset = if is_negative && !self.today_start {
325-
-1
326-
} else {
327-
0
328-
};
344+
let negative_offset = if is_negative { -1 } else { 0 };
329345

330346
let age_in_days = age_in_seconds / SECONDS_PER_DAY + negative_offset;
331347
Ok(self.days.imatches(age_in_days))
@@ -627,6 +643,61 @@ mod tests {
627643
assert_eq!(midnight.time(), NaiveTime::from_hms_opt(0, 0, 0).unwrap());
628644
}
629645

646+
#[rstest::rstest]
647+
#[case::today(ComparableValue::EqualTo(0), 43_200, 0, true)]
648+
#[case::yesterday_is_not_today(ComparableValue::EqualTo(0), -43_200, 0, false)]
649+
#[case::yesterday(ComparableValue::EqualTo(1), -43_200, 0, true)]
650+
#[case::two_days_ago_is_not_yesterday(ComparableValue::EqualTo(1), -129_600, 0, false)]
651+
#[case::two_days_ago(ComparableValue::EqualTo(2), -129_600, 0, true)]
652+
#[case::midnight_is_not_today(ComparableValue::EqualTo(0), 0, 0, false)]
653+
#[case::after_midnight(ComparableValue::EqualTo(0), 0, 500_000_000, true)]
654+
#[case::next_midnight(ComparableValue::EqualTo(0), 86_400, 0, true)]
655+
#[case::after_next_midnight(ComparableValue::EqualTo(0), 86_400, 500_000_000, false)]
656+
#[case::previous_midnight(ComparableValue::EqualTo(1), -86_400, 0, false)]
657+
#[case::after_previous_midnight(ComparableValue::EqualTo(1), -86_400, 500_000_000, true)]
658+
#[case::midnight_is_yesterday(ComparableValue::EqualTo(1), 0, 0, true)]
659+
#[case::after_midnight_is_not_yesterday(ComparableValue::EqualTo(1), 0, 500_000_000, false)]
660+
#[case::older_than_today(ComparableValue::MoreThan(0), -43_200, 0, true)]
661+
#[case::midnight_is_not_older(ComparableValue::MoreThan(0), 0, 0, false)]
662+
#[case::before_midnight_is_older(ComparableValue::MoreThan(0), -1, 500_000_000, true)]
663+
#[case::previous_midnight_is_not_older(ComparableValue::MoreThan(1), -86_400, 0, false)]
664+
#[case::before_previous_midnight_is_older(ComparableValue::MoreThan(1), -86_401, 500_000_000, true)]
665+
#[case::less_than_one_day(ComparableValue::LessThan(1), 43_200, 0, true)]
666+
#[case::yesterday_is_not_less_than_one_day(ComparableValue::LessThan(1), -43_200, 0, false)]
667+
#[case::last_fractional_second(ComparableValue::LessThan(1), -1, 500_000_000, true)]
668+
#[case::one_second_before_midnight(ComparableValue::LessThan(1), -1, 0, false)]
669+
#[case::previous_last_fractional_second(ComparableValue::LessThan(2), -86_401, 500_000_000, true)]
670+
#[case::before_previous_last_second(ComparableValue::LessThan(2), -86_401, 0, false)]
671+
#[case::huge_equal(ComparableValue::EqualTo(u64::MAX), 0, 0, false)]
672+
#[case::huge_more(ComparableValue::MoreThan(u64::MAX), 0, 0, false)]
673+
#[case::huge_less(ComparableValue::LessThan(u64::MAX), 0, 0, true)]
674+
fn daystart_file_time_ranges(
675+
#[case] days: ComparableValue,
676+
#[case] offset_seconds: i64,
677+
#[case] nanos: u32,
678+
#[case] expected: bool,
679+
#[values(FileTimeType::Accessed, FileTimeType::Modified)] file_time_type: FileTimeType,
680+
) {
681+
const MIDNIGHT: i64 = 1_700_006_400;
682+
let dir = tempfile::tempdir().expect("create fixture directory");
683+
let path = dir.path().join("file");
684+
File::create(&path).expect("create fixture file");
685+
let stamp = filetime::FileTime::from_unix_time(MIDNIGHT + offset_seconds, nanos);
686+
filetime::set_file_times(&path, stamp, stamp).expect("set fixture timestamps");
687+
let entry = get_dir_entry_for(&dir.path().to_string_lossy(), "file");
688+
let midnight = UNIX_EPOCH + Duration::from_secs(MIDNIGHT as u64);
689+
690+
// Expectations include GNU's strict +n boundary and the fractional
691+
// second accepted by -n just before midnight.
692+
let matcher = FileTimeMatcher::new(file_time_type, days, true);
693+
assert_eq!(
694+
matcher
695+
.matches_impl(&entry, midnight)
696+
.expect("match file time"),
697+
expected
698+
);
699+
}
700+
630701
#[test]
631702
fn file_time_matcher_modified_changed_accessed() {
632703
let temp_dir = Builder::new()

‎src/find/mod.rs‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,64 @@ mod tests {
16751675
assert_eq!(rc, 0);
16761676
}
16771677

1678+
#[test]
1679+
fn daystart_selects_calendar_days() {
1680+
use chrono::{Local, TimeZone};
1681+
1682+
// A fixed clock avoids depending on the date or crossing midnight.
1683+
let now: SystemTime = Local
1684+
.with_ymd_and_hms(2025, 1, 15, 18, 0, 0)
1685+
.single()
1686+
.expect("fixed local date is unambiguous")
1687+
.into();
1688+
let dir = tempfile::tempdir().expect("create fixture directory");
1689+
let root = dir.path().to_str().expect("fixture path is UTF-8");
1690+
// Yesterday at 22:00 is less than 24 hours old, so -daystart
1691+
// must change which day it matches.
1692+
for (name, hours) in [
1693+
("today", 6),
1694+
("yesterday", 30),
1695+
("yesterday_late", 20),
1696+
("two_days_ago", 54),
1697+
] {
1698+
let path = dir.path().join(name);
1699+
fs::File::create(&path).expect("create fixture file");
1700+
let stamp =
1701+
filetime::FileTime::from_system_time(now - Duration::from_secs(hours * 3600));
1702+
filetime::set_file_times(&path, stamp, stamp).expect("set fixture timestamps");
1703+
}
1704+
1705+
for predicate in ["-mtime", "-atime"] {
1706+
for (expression, expected) in [
1707+
(vec!["-daystart", predicate, "0"], "today\n"),
1708+
(
1709+
vec!["-daystart", predicate, "1"],
1710+
"yesterday\nyesterday_late\n",
1711+
),
1712+
(vec!["-daystart", predicate, "2"], "two_days_ago\n"),
1713+
(
1714+
vec!["-daystart", predicate, "+0"],
1715+
"two_days_ago\nyesterday\nyesterday_late\n",
1716+
),
1717+
(vec!["-daystart", predicate, "-1"], "today\n"),
1718+
(vec![predicate, "1", "-daystart"], "yesterday\n"),
1719+
(vec![predicate, "1"], "yesterday\n"),
1720+
(
1721+
vec!["-daystart", "(", predicate, "1", ")"],
1722+
"yesterday\nyesterday_late\n",
1723+
),
1724+
] {
1725+
let mut deps = FakeDependencies::new();
1726+
deps.set_time(now);
1727+
let mut args = vec!["find", root, "-sorted", "-type", "f"];
1728+
args.extend_from_slice(&expression);
1729+
args.extend_from_slice(&["-printf", "%f\n"]);
1730+
assert_eq!(find_main(&args, &deps), 0, "{expression:?}");
1731+
assert_eq!(deps.get_output_as_string(), expected, "{expression:?}");
1732+
}
1733+
}
1734+
}
1735+
16781736
#[test]
16791737
fn find_fprinter() {
16801738
let printer = ["fprint", "fprint0"];

0 commit comments

Comments
 (0)