Skip to content

touch: use write-open + futimens on Unix to trigger IN_CLOSE_WRITE#9813

Open
mattsu2020 wants to merge 1 commit intouutils:mainfrom
mattsu2020:bug_analystic
Open

touch: use write-open + futimens on Unix to trigger IN_CLOSE_WRITE#9813
mattsu2020 wants to merge 1 commit intouutils:mainfrom
mattsu2020:bug_analystic

Conversation

@mattsu2020
Copy link
Contributor

Summary

Make touch open regular files for write and call futimens on the fd (Unix only). This matches GNU touch behavior and ensures inotify emits IN_CLOSE_WRITE when timestamps are updated.

Motivation

Some inotify-based reloaders watch only IN_CLOSE_WRITE. With the current path-based timestamp update, touch emits only IN_ATTRIB, so those watchers miss the change. This caused infinite loops in downstream tests (e.g., 0 A.D. hotload tests on Ubuntu Questing).

Changes

  • On Unix, attempt OpenOptions::new().write(true) and call futimens on the fd for regular files.
  • Fallback to the existing set_file_times path-based update when open/futimens fails or the target is not a regular file.
  • Keep existing symlink handling intact.

Testing

  • cargo test -p uu_touch

Notes

  • Behavior is Unix-only and preserves current semantics on non-Unix platforms.

related
#9812

@sylvestre
Copy link
Contributor

is it possible to add tests ? thanks

@mattsu2020
Copy link
Contributor Author

is it possible to add tests ? thanks

add

@sylvestre
Copy link
Contributor

sorry, it needs to be rebased

@mattsu2020 mattsu2020 requested a review from sylvestre January 5, 2026 10:16
@github-actions
Copy link

github-actions bot commented Jan 5, 2026

GNU testsuite comparison:

Skipping an intermittent issue tests/timeout/timeout (passes in this run but fails in the 'main' branch)

@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/stty/bad-speed is now passing!

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 19, 2026

Merging this PR will not alter performance

✅ 288 untouched benchmarks
⏩ 38 skipped benchmarks1


Comparing mattsu2020:bug_analystic (8e88196) with main (289d701)

Open in CodSpeed

Footnotes

  1. 38 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@github-actions
Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/shuf/shuf-reservoir (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/sort/sort-stale-thread-mem (passes in this run but fails in the 'main' branch)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

he conversion from nix::Error to std::io::Error is incorrect

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

#[cfg(unix)]
{
// Open write-only and use futimens to trigger IN_CLOSE_WRITE on Linux.
if !is_stdout && try_futimens_via_write_fd(path, atime, mtime).is_ok() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silent error swallowing with is_ok() loses information about why futimens failed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary metadata check before open. Opens file twice (metadata + open). Should check metadata after opening via file.metadata() to avoid TOCTOU and reduce syscalls.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use translate!()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsafe type cast 'as _' for nanoseconds. Should explicitly cast to i32: atime.nanoseconds() as i32

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/pr/bounded-memory is no longer failing!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant import - std::fs is already imported unconditionally at line 26

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

@github-actions
Copy link

GNU testsuite comparison:

GNU test failed: tests/touch/not-owner. tests/touch/not-owner is passing on 'main'. Maybe you have to rebase?

@github-actions
Copy link

GNU testsuite comparison:

GNU test failed: tests/factor/t10. tests/factor/t10 is passing on 'main'. Maybe you have to rebase?
GNU test failed: tests/factor/t34. tests/factor/t34 is passing on 'main'. Maybe you have to rebase?
GNU test failed: tests/touch/not-owner. tests/touch/not-owner is passing on 'main'. Maybe you have to rebase?
Skipping an intermittent issue tests/tail/follow-name (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tty/tty-eof (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/cut/cut-huge-range is now passing!
Congrats! The gnu test tests/rm/many-dir-entries-vs-OOM is now passing!

@github-actions
Copy link

GNU testsuite comparison:

GNU test failed: tests/rm/isatty. tests/rm/isatty is passing on 'main'. Maybe you have to rebase?
GNU test failed: tests/touch/not-owner. tests/touch/not-owner is passing on 'main'. Maybe you have to rebase?
Congrats! The gnu test tests/cut/bounded-memory is no longer failing!
Note: The gnu test tests/cp/link-heap is now being skipped but was previously passing.

On Unix, try futimens through a write-opened file descriptor to trigger IN_CLOSE_WRITE semantics, and fall back to set_file_times when needed. Also adds a unit test for the futimens path and wires unix-only dependencies.
@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/cut/bounded-memory is no longer failing!
Note: The gnu test tests/rm/many-dir-entries-vs-OOM is now being skipped but was previously passing.
Note: The gnu test tests/unexpand/bounded-memory is now being skipped but was previously passing.

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.

2 participants

Comments