Skip to content

fix(find): sample directory times before the enumeration open (#1402) - #1418

Merged
caomengxuan666 merged 1 commit into
mainfrom
fix/iss-find-used-atime
Oct 27, 2026
Merged

caomengxuan666 merged 1 commit into
mainfrom
fix/iss-find-used-atime

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #1402

Problem

find -used 1 on a root directory operand (.) could include . itself where GNU does not. The traversal opened/enumerated a directory before sampling its atime/ctime for the -used/-atime family, so under relatime-style semantics the verdict was judged on the atime bump the traversal's own enumeration open had just caused. GNU findutils' fts layer stats each start point before opendir, which naturally avoids this. Issue evidence: a fixture where . had atime−ctime = −5.0 ms before the run (negative gaps must never match -used) still matched, and the gap measured +302 ms after the run.

Fix

Sample a directory's own timestamps before any open the traversal performs on it, and feed that pre-open snapshot to every time-family evaluation for the entry:

  • EntryTimes snapshot captured once per entry via capture_pre_open_times(), covering only the timestamp kinds the active predicates read (cfg.time_sample_mask, computed in build_config from the direct time filters plus an AST walk expression_time_sample_mask() over the expression tree — non-time queries stay probe-free).
  • The snapshot is threaded through entry_matches / evaluate_expression and consumed by file_age_seconds, file_age_daystart_seconds, used_gap_ticks, and the -newerXY/-anewer/-cnewer entry-time reads, in all three traversals:
    • scan_depth_first / scan_delete_depth_first (-depth / -delete): captured right after the stat, before the loop-detection identity probe and the directory enumeration — previously the directory's own verdict was evaluated last, after its whole subtree had been walked.
    • scan_one_root: captured for the starting point before the recursive iterator is constructed, and per-entry before entry_matches in the iteration loop.
  • A side benefit: verdicts are now independent of evaluation order inside entry_matches — -empty enumerates the directory before the time family runs, which under relatime had the same contamination shape.

i18n delta

None. No user-visible text changed (no diagnostics, help, or option text touched).

Tests

  • find.issue1402_root_operand_negative_used_gap_excludes_directory: a negative-gap root operand is excluded from -used +100, -used 1, and -used -1; a future-atime child directory and file still match -used +100.
  • find.issue1402_depth_scan_samples_directory_times_before_enumeration: under -depth the directory's own verdict comes from pre-open times; default and -depth traversals agree on the same fixture.
  • find.issue1402_expression_time_nodes_use_pre_open_times_for_directories: the expression path (-name d -used +100) consumes the same snapshot for a directory operand.
  • Fixtures are re-stamped before every run: each traversal bumps directory atimes on volumes with last-access updates enabled, so sharing a fixture across runs would contaminate them (the issue's stage-1 'known atime trap').

On this Windows dev box the volume has last-access updates enabled (system-managed policy on large volumes), so the regression tests were verified to fail on the pre-fix traversal — with the old order the root dropped out of -used +100 and the subtree directory entered -used 1, exactly the issue mechanism — and to pass with the snapshot. They remain meaningful pins for the Wine harness where the issue evidence was gathered.

Verification

  • Full first build (scripts/build-with-vs.ps1) clean.
  • find.*: 156/157 pass; the single failure is find.issue_1309_ls_suppresses_default_print_and_has_all_fields, the known environment baseline failure called out in the issue notes.
  • Full gtest suite minus the known hangs/environment failures (xargs.xargs_interactive_*, xargs.xargs_max_chars_splits_batches, xargs.xargs_delimiter_does_not_force_exit_if_exceeded, xargs.xargs_verbose_quotes_special_args_1322, *.issue_1288_*, find.issue_1309_*): 3343 passed, 0 failed.
  • New code introduces no clang-format violations (checked against the changed line ranges; the pre-existing violations in these files are untouched).

The -used/-atime family sampled a directory's atime/ctime after the
traversal had already opened it for enumeration. Under relatime-style
semantics that enumeration open raises the directory atime (the Wine
harness measured a root operand at atime-ctime = -5 ms before a run and
+302 ms after), so a directory could match -used on the bump its own
traversal caused, while GNU's fts layer stats each start point before
opendir and never sees it.

Capture the entry's own timestamps in one pre-open probe and feed that
snapshot to every -atime/-amin/-ctime/-cmin/-mtime/-mmin/-used and
-newerXY/-anewer/-cnewer evaluation for the entry, in all three
traversals (scan_one_root, scan_depth_first, scan_delete_depth_first).
A bitmask computed from the active predicates keeps non-time queries
probe-free, and the snapshot also makes verdicts independent of
evaluation order inside entry_matches (-empty enumerates the directory
before the time family runs).

The issue1402 regression tests drive -used against directory operands
with controlled access times; on volumes with last-access updates
enabled they fail on the pre-fix traversal (root drops out of -used
+100, subtree directories enter -used 1) and pass with the snapshot.
@caomengxuan666
caomengxuan666 merged commit 8cd7277 into main Oct 27, 2026
3 of 5 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/iss-find-used-atime branch October 27, 2026 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

find: -used/-atime family samples directory times after an atime-bumping enumeration open

1 participant