Skip to content

Commit 56c588b

Browse files
author
Brian Burkhalter
committed
8343417: (fs) BasicFileAttributeView.setTimes uses microsecond precision with NOFOLLOW_LINKS
Reviewed-by: alanb
1 parent d3c042f commit 56c588b

File tree

5 files changed

+141
-25
lines changed

5 files changed

+141
-25
lines changed

src/java.base/unix/classes/sun/nio/fs/UnixConstants.java.template

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -139,11 +139,13 @@ class UnixConstants {
139139
#endif
140140

141141
// flags used with openat/unlinkat/etc.
142-
#if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_REMOVEDIR)
142+
#if defined(AT_FDCWD) && defined(AT_SYMLINK_NOFOLLOW) && defined(AT_REMOVEDIR)
143+
static final int PREFIX_AT_FDCWD = AT_FDCWD;
143144
static final int PREFIX_AT_SYMLINK_NOFOLLOW = AT_SYMLINK_NOFOLLOW;
144145
static final int PREFIX_AT_REMOVEDIR = AT_REMOVEDIR;
145146
#else
146147
// not supported (dummy values will not be used at runtime).
148+
static final int PREFIX_AT_FDCWD = 00;
147149
static final int PREFIX_AT_SYMLINK_NOFOLLOW = 00;
148150
static final int PREFIX_AT_REMOVEDIR = 00;
149151
#endif

src/java.base/unix/classes/sun/nio/fs/UnixFileAttributeViews.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -76,13 +76,16 @@ public void setTimes(FileTime lastModifiedTime,
7676
boolean useFutimes = false;
7777
boolean useFutimens = false;
7878
boolean useLutimes = false;
79+
boolean useUtimensat = false;
7980
int fd = -1;
8081
try {
8182
if (!followLinks) {
82-
useLutimes = lutimesSupported() &&
83-
UnixFileAttributes.get(file, false).isSymbolicLink();
83+
// these path-based syscalls also work if following links
84+
if (!(useUtimensat = utimensatSupported())) {
85+
useLutimes = lutimesSupported();
86+
}
8487
}
85-
if (!useLutimes) {
88+
if (!useUtimensat && !useLutimes) {
8689
fd = file.openForAttributeAccess(followLinks);
8790
if (fd != -1) {
8891
haveFd = true;
@@ -92,8 +95,8 @@ public void setTimes(FileTime lastModifiedTime,
9295
}
9396
}
9497
} catch (UnixException x) {
95-
if (!(x.errno() == UnixConstants.ENXIO ||
96-
(x.errno() == UnixConstants.ELOOP && useLutimes))) {
98+
if (!(x.errno() == ENXIO ||
99+
(x.errno() == ELOOP && (useUtimensat || useLutimes)))) {
97100
x.rethrowAsIOException(file);
98101
}
99102
}
@@ -117,7 +120,7 @@ public void setTimes(FileTime lastModifiedTime,
117120
}
118121

119122
// update times
120-
TimeUnit timeUnit = useFutimens ?
123+
TimeUnit timeUnit = (useFutimens || useUtimensat) ?
121124
TimeUnit.NANOSECONDS : TimeUnit.MICROSECONDS;
122125
long modValue = lastModifiedTime.to(timeUnit);
123126
long accessValue= lastAccessTime.to(timeUnit);
@@ -130,13 +133,16 @@ public void setTimes(FileTime lastModifiedTime,
130133
futimes(fd, accessValue, modValue);
131134
} else if (useLutimes) {
132135
lutimes(file, accessValue, modValue);
136+
} else if (useUtimensat) {
137+
utimensat(AT_FDCWD, file, accessValue, modValue,
138+
followLinks ? 0 : AT_SYMLINK_NOFOLLOW);
133139
} else {
134140
utimes(file, accessValue, modValue);
135141
}
136142
} catch (UnixException x) {
137143
// if futimes/utimes fails with EINVAL and one/both of the times is
138144
// negative then we adjust the value to the epoch and retry.
139-
if (x.errno() == UnixConstants.EINVAL &&
145+
if (x.errno() == EINVAL &&
140146
(modValue < 0L || accessValue < 0L)) {
141147
retry = true;
142148
} else {
@@ -153,6 +159,9 @@ public void setTimes(FileTime lastModifiedTime,
153159
futimes(fd, accessValue, modValue);
154160
} else if (useLutimes) {
155161
lutimes(file, accessValue, modValue);
162+
} else if (useUtimensat) {
163+
utimensat(AT_FDCWD, file, accessValue, modValue,
164+
followLinks ? 0 : AT_SYMLINK_NOFOLLOW);
156165
} else {
157166
utimes(file, accessValue, modValue);
158167
}

src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,20 @@ static void lutimes(UnixPath path, long times0, long times1)
391391
private static native void lutimes0(long pathAddress, long times0, long times1)
392392
throws UnixException;
393393

394+
/**
395+
* utimensat(int fd, const char* path,
396+
* const struct timeval times[2], int flags)
397+
*/
398+
static void utimensat(int fd, UnixPath path, long times0, long times1, int flags)
399+
throws UnixException
400+
{
401+
try (NativeBuffer buffer = copyToNativeBuffer(path)) {
402+
utimensat0(fd, buffer.address(), times0, times1, flags);
403+
}
404+
}
405+
private static native void utimensat0(int fd, long pathAddress, long times0, long times1, int flags)
406+
throws UnixException;
407+
394408
/**
395409
* DIR *opendir(const char* dirname)
396410
*/
@@ -557,7 +571,8 @@ static native int flistxattr(int filedes, long listAddress, int size)
557571
private static final int SUPPORTS_FUTIMES = 1 << 2;
558572
private static final int SUPPORTS_FUTIMENS = 1 << 3;
559573
private static final int SUPPORTS_LUTIMES = 1 << 4;
560-
private static final int SUPPORTS_XATTR = 1 << 5;
574+
private static final int SUPPORTS_UTIMENSAT = 1 << 5;
575+
private static final int SUPPORTS_XATTR = 1 << 6;
561576
private static final int SUPPORTS_BIRTHTIME = 1 << 16; // other features
562577
private static final int capabilities;
563578

@@ -589,6 +604,13 @@ static boolean lutimesSupported() {
589604
return (capabilities & SUPPORTS_LUTIMES) != 0;
590605
}
591606

607+
/**
608+
* Supports utimensat
609+
*/
610+
static boolean utimensatSupported() {
611+
return (capabilities & SUPPORTS_UTIMENSAT) != 0;
612+
}
613+
592614
/**
593615
* Supports file birth (creation) time attribute
594616
*/

src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c

+35
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ typedef DIR* fdopendir_func(int);
211211
#if defined(__linux__)
212212
typedef int statx_func(int dirfd, const char *restrict pathname, int flags,
213213
unsigned int mask, struct my_statx *restrict statxbuf);
214+
typedef int utimensat_func(int dirfd, const char *pathname,
215+
const struct timespec[2], int flags);
214216
#endif
215217

216218
static openat_func* my_openat_func = NULL;
@@ -223,6 +225,7 @@ static lutimes_func* my_lutimes_func = NULL;
223225
static fdopendir_func* my_fdopendir_func = NULL;
224226
#if defined(__linux__)
225227
static statx_func* my_statx_func = NULL;
228+
static utimensat_func* my_utimensat_func = NULL;
226229
#endif
227230

228231
/**
@@ -432,6 +435,10 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this)
432435
if (my_statx_func != NULL) {
433436
capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_BIRTHTIME;
434437
}
438+
my_utimensat_func = (utimensat_func*) dlsym(RTLD_DEFAULT, "utimensat");
439+
if (my_utimensat_func != NULL) {
440+
capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_UTIMENSAT;
441+
}
435442
#endif
436443

437444
/* supports extended attributes */
@@ -976,6 +983,34 @@ Java_sun_nio_fs_UnixNativeDispatcher_lutimes0(JNIEnv* env, jclass this,
976983
}
977984
}
978985

986+
JNIEXPORT void JNICALL
987+
Java_sun_nio_fs_UnixNativeDispatcher_utimensat0(JNIEnv* env, jclass this,
988+
jint fd, jlong pathAddress, jlong accessTime, jlong modificationTime, jint flags) {
989+
#if defined(__linux__)
990+
int err;
991+
struct timespec times[2];
992+
const char* path = (const char*)jlong_to_ptr(pathAddress);
993+
994+
times[0].tv_sec = accessTime / 1000000000;
995+
times[0].tv_nsec = accessTime % 1000000000;
996+
997+
times[1].tv_sec = modificationTime / 1000000000;
998+
times[1].tv_nsec = modificationTime % 1000000000;
999+
1000+
if (my_utimensat_func == NULL) {
1001+
JNU_ThrowInternalError(env, "my_utimensat_func is NULL");
1002+
return;
1003+
}
1004+
RESTARTABLE((*my_utimensat_func)(fd, path, &times[0], flags), err);
1005+
1006+
if (err == -1) {
1007+
throwUnixException(env, errno);
1008+
}
1009+
#else
1010+
JNU_ThrowInternalError(env, "should not reach here");
1011+
#endif
1012+
}
1013+
9791014
JNIEXPORT jlong JNICALL
9801015
Java_sun_nio_fs_UnixNativeDispatcher_opendir0(JNIEnv* env, jclass this,
9811016
jlong pathAddress)

test/jdk/java/nio/file/attribute/BasicFileAttributeView/SetTimesNanos.java

+62-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,10 +22,13 @@
2222
*/
2323

2424
/* @test
25-
* @bug 8181493 8231174
25+
* @bug 8181493 8231174 8343417
2626
* @summary Verify that nanosecond precision is maintained for file timestamps
2727
* @requires (os.family == "linux") | (os.family == "mac") | (os.family == "windows")
28+
* @library ../.. /test/lib
29+
* @build jdk.test.lib.Platform
2830
* @modules java.base/sun.nio.fs:+open
31+
* @run main SetTimesNanos
2932
*/
3033

3134
import java.io.IOException;
@@ -36,24 +39,27 @@
3639
import java.nio.file.attribute.BasicFileAttributes;
3740
import java.nio.file.attribute.BasicFileAttributeView;
3841
import java.nio.file.attribute.FileTime;
42+
import java.util.List;
3943
import java.util.Set;
40-
import java.util.concurrent.TimeUnit;
44+
45+
import static java.nio.file.LinkOption.*;
46+
import static java.util.concurrent.TimeUnit.*;
47+
48+
import jdk.test.lib.Platform;
49+
import jtreg.SkippedException;
4150

4251
public class SetTimesNanos {
43-
private static final boolean IS_WINDOWS =
44-
System.getProperty("os.name").startsWith("Windows");
4552

4653
public static void main(String[] args) throws Exception {
47-
if (!IS_WINDOWS) {
54+
if (!Platform.isWindows()) {
4855
// Check whether futimens() system call is supported
4956
Class unixNativeDispatcherClass =
5057
Class.forName("sun.nio.fs.UnixNativeDispatcher");
5158
Method futimensSupported =
5259
unixNativeDispatcherClass.getDeclaredMethod("futimensSupported");
5360
futimensSupported.setAccessible(true);
5461
if (!(boolean)futimensSupported.invoke(null)) {
55-
System.err.println("futimens() not supported; skipping test");
56-
return;
62+
throw new SkippedException("futimens() not supported");
5763
}
5864
}
5965

@@ -63,30 +69,34 @@ public static void main(String[] args) throws Exception {
6369
System.out.format("FileStore: \"%s\" on %s (%s)%n",
6470
dir, store.name(), store.type());
6571

66-
Set<String> testedTypes = IS_WINDOWS ?
72+
Set<String> testedTypes = Platform.isWindows() ?
6773
Set.of("NTFS") : Set.of("apfs", "ext4", "xfs", "zfs");
6874
if (!testedTypes.contains(store.type())) {
69-
System.err.format("%s not in %s; skipping test", store.type(), testedTypes);
70-
return;
75+
throw new SkippedException(store.type() + " not in " + testedTypes);
7176
}
7277

7378
testNanos(dir);
7479

7580
Path file = Files.createFile(dir.resolve("test.dat"));
7681
testNanos(file);
82+
83+
if (Platform.isLinux()) {
84+
testNanosLink(false);
85+
testNanosLink(true);
86+
}
7787
}
7888

7989
private static void testNanos(Path path) throws IOException {
8090
// Set modification and access times
8191
// Time stamp = "2017-01-01 01:01:01.123456789";
8292
long timeNanos = 1_483_261_261L*1_000_000_000L + 123_456_789L;
83-
FileTime pathTime = FileTime.from(timeNanos, TimeUnit.NANOSECONDS);
93+
FileTime pathTime = FileTime.from(timeNanos, NANOSECONDS);
8494
BasicFileAttributeView view =
8595
Files.getFileAttributeView(path, BasicFileAttributeView.class);
8696
view.setTimes(pathTime, pathTime, null);
8797

8898
// Windows file time resolution is 100ns so truncate
89-
if (IS_WINDOWS) {
99+
if (Platform.isWindows()) {
90100
timeNanos = 100L*(timeNanos/100L);
91101
}
92102

@@ -99,12 +109,50 @@ private static void testNanos(Path path) throws IOException {
99109
FileTime[] times = new FileTime[] {attrs.lastModifiedTime(),
100110
attrs.lastAccessTime()};
101111
for (int i = 0; i < timeNames.length; i++) {
102-
long nanos = times[i].to(TimeUnit.NANOSECONDS);
112+
long nanos = times[i].to(NANOSECONDS);
103113
if (nanos != timeNanos) {
104114
throw new RuntimeException("Expected " + timeNames[i] +
105115
" timestamp to be '" + timeNanos + "', but was '" +
106116
nanos + "'");
107117
}
108118
}
109119
}
120+
121+
private static void testNanosLink(boolean absolute) throws IOException {
122+
System.out.println("absolute: " + absolute);
123+
124+
var target = Path.of("target");
125+
var symlink = Path.of("symlink");
126+
if (absolute)
127+
symlink = symlink.toAbsolutePath();
128+
129+
try {
130+
Files.createFile(target);
131+
Files.createSymbolicLink(symlink, target);
132+
133+
var newTime = FileTime.from(1730417633157646106L, NANOSECONDS);
134+
System.out.println("newTime: " + newTime.to(NANOSECONDS));
135+
136+
for (Path p : List.of(target, symlink)) {
137+
System.out.println("p: " + p);
138+
139+
var view = Files.getFileAttributeView(p,
140+
BasicFileAttributeView.class, NOFOLLOW_LINKS);
141+
view.setTimes(newTime, newTime, null);
142+
var attrs = view.readAttributes();
143+
144+
if (!attrs.lastAccessTime().equals(newTime))
145+
throw new RuntimeException("Last access time "
146+
+ attrs.lastAccessTime()
147+
+ " != " + newTime);
148+
if (!attrs.lastAccessTime().equals(newTime))
149+
throw new RuntimeException("Last modified time "
150+
+ attrs.lastModifiedTime()
151+
+ " != " + newTime);
152+
}
153+
} finally {
154+
Files.deleteIfExists(target);
155+
Files.deleteIfExists(symlink);
156+
}
157+
}
110158
}

0 commit comments

Comments
 (0)