Skip to content

Commit e7aef80

Browse files
tjgqcopybara-github
authored andcommitted
Actually fix setting the mtime on a shared disk cache.
The earlier attempt in e9a18ce was not the correct fix, due to the requirements spelled out in the utimensat(2) man page: if the current user has write permission but is not the owner of the file, it may set both atime and mtime to UTIME_NOW, but any other combination is forbidden. Although in theory we could still leave the atime alone in other cases, it's not worth the complexity: atime is often unreliable and Bazel doesn't care about it, anyway. Unfortunately, it's difficult to write an integration test for this, because we can't arrange for the existence of multiple users in our CI environment. Fixes #23512. PiperOrigin-RevId: 729042375 Change-Id: I7ca5e8bc0858a9796f448fca554d0c9465579e19
1 parent a3eb351 commit e7aef80

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/main/native/unix_jni.cc

+7-5
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,17 @@ extern "C" JNIEXPORT void JNICALL
411411
Java_com_google_devtools_build_lib_unix_NativePosixFiles_utimensat(
412412
JNIEnv *env, jclass clazz, jstring path, jboolean now, jlong millis) {
413413
const char *path_chars = GetStringLatin1Chars(env, path);
414+
// If `now` is true use the current time, otherwise use `millis`.
415+
// On Linux, if the current user has write permission but isn't the owner of
416+
// the file, atime and mtime may be simultaneously set to UTIME_NOW, but any
417+
// other combination is forbidden. For simplicity, always set both.
414418
int64_t sec = millis / 1000;
415419
int32_t nsec = (millis % 1000) * 1000000;
416-
struct timespec spec[2] = {
417-
// Do not set atime.
418-
{0, UTIME_OMIT},
419-
// Set mtime to now if `now` is true, otherwise to the specified time.
420+
struct timespec times[2] = {
421+
{sec, now ? UTIME_NOW : nsec},
420422
{sec, now ? UTIME_NOW : nsec},
421423
};
422-
if (::utimensat(AT_FDCWD, path_chars, spec, 0) == -1) {
424+
if (::utimensat(AT_FDCWD, path_chars, times, 0) == -1) {
423425
PostException(env, errno, path_chars);
424426
}
425427
ReleaseStringLatin1Chars(path_chars);

0 commit comments

Comments
 (0)