Repository navigation
Fix some sanity checks for the behavior of utimesat and stat #27896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hoodmane
wants to merge
3
commits into
emscripten-core:main
Choose a base branch
from
hoodmane:fix-time-precision
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Copyright 2026 The Emscripten Authors. All rights reserved. | ||
| * Emscripten is available under two separate licenses, the MIT license and the | ||
| * University of Illinois/NCSA Open Source License. Both these licenses can be | ||
| * found in the LICENSE file. | ||
| */ | ||
|
|
||
| // Creating a file and then touching it must never move the mtime backwards. | ||
| // Run many times in a loop to prevent flakiness. | ||
|
|
||
| #include <assert.h> | ||
| #include <fcntl.h> | ||
| #include <stdio.h> | ||
| #include <sys/stat.h> | ||
| #include <unistd.h> | ||
|
|
||
| static int cmp_timespec(const struct timespec* a, const struct timespec* b) { | ||
| if (a->tv_sec != b->tv_sec) { | ||
| return a->tv_sec < b->tv_sec ? -1 : 1; | ||
| } | ||
| if (a->tv_nsec != b->tv_nsec) { | ||
| return a->tv_nsec < b->tv_nsec ? -1 : 1; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| int main() { | ||
| const char* path = "touchfile"; | ||
| for (int i = 0; i < 1000; i++) { | ||
| // Create the file. Filesystem assigns initial mtime. | ||
| int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0666); | ||
| assert(fd >= 0); | ||
| close(fd); | ||
|
|
||
| // stat 1 | ||
| struct stat before; | ||
| assert(stat(path, &before) == 0); | ||
|
|
||
| // touch | ||
| struct timespec now[2]; | ||
| now[0].tv_nsec = UTIME_NOW; | ||
| now[1].tv_nsec = UTIME_NOW; | ||
| assert(utimensat(AT_FDCWD, path, now, 0) == 0); | ||
|
|
||
| // stat 2 | ||
| struct stat after; | ||
| assert(stat(path, &after) == 0); | ||
|
|
||
| // assert stat 2 >= stat 1 | ||
| if (cmp_timespec(&after.st_mtim, &before.st_mtim) < 0) { | ||
| printf("iteration %d: mtime went backwards: before=%lld.%09ld after=%lld.%09ld\n", | ||
| i, (long long)before.st_mtim.tv_sec, before.st_mtim.tv_nsec, | ||
| (long long)after.st_mtim.tv_sec, after.st_mtim.tv_nsec); | ||
| return 1; | ||
| } | ||
|
|
||
| assert(unlink(path) == 0); | ||
| } | ||
|
|
||
| puts("done"); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright 2026 The Emscripten Authors. All rights reserved. | ||
| * Emscripten is available under two separate licenses, the MIT license and the | ||
| * University of Illinois/NCSA Open Source License. Both these licenses can be | ||
| * found in the LICENSE file. | ||
| */ | ||
|
|
||
| // Reading a file's timestamps with stat() and writing them back with | ||
| // utimensat() must produce the same timestamps when read back again. Run in a | ||
| // loop to exercise different rounding cases. | ||
|
|
||
| #include <assert.h> | ||
| #include <fcntl.h> | ||
| #include <stdio.h> | ||
| #include <sys/stat.h> | ||
| #include <unistd.h> | ||
|
|
||
| #define ITERATIONS 2000 | ||
|
|
||
| static int same_timespec(const struct timespec* a, const struct timespec* b) { | ||
| return a->tv_sec == b->tv_sec && a->tv_nsec == b->tv_nsec; | ||
| } | ||
|
|
||
| static void print_timespec(const char* label, const struct timespec* ts) { | ||
| printf("%s=%lld.%09ld", label, (long long)ts->tv_sec, ts->tv_nsec); | ||
| } | ||
|
|
||
| int main() { | ||
| const char* path = "roundtripfile"; | ||
| for (int i = 0; i < ITERATIONS; i++) { | ||
| // Create the file. Filesystem assigns mtime. | ||
| int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0666); | ||
| assert(fd >= 0); | ||
| close(fd); | ||
|
|
||
| struct stat before; | ||
| assert(stat(path, &before) == 0); | ||
|
|
||
| // Write the timestamps back exactly as we read them. | ||
| struct timespec times[2] = {before.st_atim, before.st_mtim}; | ||
| assert(utimensat(AT_FDCWD, path, times, 0) == 0); | ||
|
|
||
|
|
||
| // Writing back same timestamp shouldn't have changed it. | ||
| struct stat after; | ||
| assert(stat(path, &after) == 0); | ||
|
|
||
| if (!same_timespec(&before.st_atim, &after.st_atim) || | ||
| !same_timespec(&before.st_mtim, &after.st_mtim)) { | ||
| printf("iteration %d: timestamps did not round-trip: ", i); | ||
| print_timespec("atime before", &before.st_atim); | ||
| print_timespec(" after", &after.st_atim); | ||
| print_timespec(" mtime before", &before.st_mtim); | ||
| print_timespec(" after", &after.st_mtim); | ||
| printf("\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| assert(unlink(path) == 0); | ||
| } | ||
|
|
||
| puts("done"); | ||
| return 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if I should revert #27794 instead. Perhaps my attempt if fixing the rounding issues just made things worse?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for reverting #27794.