Skip to content

Commit

Permalink
8338884: java/nio/file/attribute/BasicFileAttributeView/CreationTime.…
Browse files Browse the repository at this point in the history
…java#tmp fails on alinux3

Reviewed-by: sgehwolf, bpb
  • Loading branch information
SendaoYan committed Oct 10, 2024
1 parent 475f8f9 commit 9d621d3
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 18 deletions.
2 changes: 2 additions & 0 deletions make/test/JtregNativeJdk.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ ifeq ($(call isTargetOs, linux), true)
# stripping during the test libraries' build.
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libFib := -g
BUILD_JDK_JTREG_LIBRARIES_STRIP_SYMBOLS_libFib := false
# nio tests' libCreationTimeHelper native needs -ldl linker flag
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libCreationTimeHelper := -ldl
endif

ifeq ($(ASAN_ENABLED), true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024 Alibaba Group Holding Limited. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,18 +26,18 @@
* @bug 8011536 8151430 8316304 8334339
* @summary Basic test for creationTime attribute on platforms/file systems
* that support it, tests using /tmp directory.
* @library ../.. /test/lib
* @build jdk.test.lib.Platform
* @run main CreationTime
* @library ../.. /test/lib /java/foreign
* @build jdk.test.lib.Platform NativeTestHelper
* @run main/othervm/native --enable-native-access=ALL-UNNAMED CreationTime
*/

/* @test id=cwd
* @summary Basic test for creationTime attribute on platforms/file systems
* that support it, tests using the test scratch directory, the test
* scratch directory maybe at diff disk partition to /tmp on linux.
* @library ../.. /test/lib
* @build jdk.test.lib.Platform
* @run main CreationTime .
* @library ../.. /test/lib /java/foreign
* @build jdk.test.lib.Platform NativeTestHelper
* @run main/othervm/native --enable-native-access=ALL-UNNAMED CreationTime .
*/

import java.lang.foreign.Linker;
Expand All @@ -51,8 +52,6 @@

public class CreationTime {

private static final java.io.PrintStream err = System.err;

/**
* Reads the creationTime attribute
*/
Expand All @@ -78,14 +77,9 @@ static void test(Path top) throws IOException {
FileTime creationTime = creationTime(file);
Instant now = Instant.now();
if (Math.abs(creationTime.toMillis()-now.toEpochMilli()) > 10000L) {
System.out.println("creationTime.toMillis() == " + creationTime.toMillis());
// If the file system doesn't support birth time, then skip this test
if (creationTime.toMillis() == 0) {
throw new SkippedException("birth time not support for: " + file);
} else {
err.println("File creation time reported as: " + creationTime);
throw new RuntimeException("Expected to be close to: " + now);
}
System.err.println("creationTime.toMillis() == " + creationTime.toMillis());
System.err.println("File creation time reported as: " + creationTime);
throw new RuntimeException("Expected to be close to: " + now);
}

/**
Expand All @@ -107,7 +101,12 @@ static void test(Path top) throws IOException {
}
} else if (Platform.isLinux()) {
// Creation time read depends on statx system call support
supportsCreationTimeRead = Linker.nativeLinker().defaultLookup().find("statx").isPresent();
try {
supportsCreationTimeRead = CreationTimeHelper.
linuxIsCreationTimeSupported(file.toAbsolutePath().toString());
} catch (Throwable e) {
supportsCreationTimeRead = false;
}
// Creation time updates are not supported on Linux
supportsCreationTimeWrite = false;
}
Expand All @@ -122,8 +121,11 @@ static void test(Path top) throws IOException {
Instant plusHour = Instant.now().plusSeconds(60L * 60L);
Files.setLastModifiedTime(file, FileTime.from(plusHour));
FileTime current = creationTime(file);
if (!current.equals(creationTime))
if (!current.equals(creationTime)) {
System.err.println("current = " + current);
System.err.println("creationTime = " + creationTime);
throw new RuntimeException("Creation time should not have changed");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2024 Alibaba Group Holding Limited. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.Linker;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.SymbolLookup;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;

public class CreationTimeHelper extends NativeTestHelper {

static {
System.loadLibrary("CreationTimeHelper");
}

final static Linker abi = Linker.nativeLinker();
static final SymbolLookup lookup = SymbolLookup.loaderLookup();
final static MethodHandle methodHandle = abi.
downcallHandle(lookup.findOrThrow("linuxIsCreationTimeSupported"),
FunctionDescriptor.of(C_BOOL, C_POINTER));

// Helper so as to determine birth time support or not on Linux.
// Support is determined in a two-step process:
// 1. Determine if `statx` system call is available. If available proceed,
// otherwise return false.
// 2. Perform an actual `statx` call on the given file and check for birth
// time support in the mask returned from the call. This is needed,
// since some file systems, like nfs/tmpfs etc., don't support birth
// time even though the `statx` system call is available.
static boolean linuxIsCreationTimeSupported(String file) throws Throwable {
if (!abi.defaultLookup().find("statx").isPresent()) {
return false;
}
try (var arena = Arena.ofConfined()) {
MemorySegment s = arena.allocateFrom(file);
return (boolean)methodHandle.invokeExact(s);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (c) 2024 Alibaba Group Holding Limited. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include "export.h"
#include <stdbool.h>
#if defined(__linux__)
#include <linux/fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <bits/types.h>
#include <dlfcn.h>
#ifndef STATX_BASIC_STATS
#define STATX_BASIC_STATS 0x000007ffU
#endif
#ifndef STATX_BTIME
#define STATX_BTIME 0x00000800U
#endif
#ifndef RTLD_DEFAULT
#define RTLD_DEFAULT RTLD_LOCAL
#endif

/*
* Timestamp structure for the timestamps in struct statx.
*/
struct my_statx_timestamp {
__int64_t tv_sec;
__uint32_t tv_nsec;
__int32_t __reserved;
};

/*
* struct statx used by statx system call on >= glibc 2.28
* systems
*/
struct my_statx
{
__uint32_t stx_mask;
__uint32_t stx_blksize;
__uint64_t stx_attributes;
__uint32_t stx_nlink;
__uint32_t stx_uid;
__uint32_t stx_gid;
__uint16_t stx_mode;
__uint16_t __statx_pad1[1];
__uint64_t stx_ino;
__uint64_t stx_size;
__uint64_t stx_blocks;
__uint64_t stx_attributes_mask;
struct my_statx_timestamp stx_atime;
struct my_statx_timestamp stx_btime;
struct my_statx_timestamp stx_ctime;
struct my_statx_timestamp stx_mtime;
__uint32_t stx_rdev_major;
__uint32_t stx_rdev_minor;
__uint32_t stx_dev_major;
__uint32_t stx_dev_minor;
__uint64_t __statx_pad2[14];
};

typedef int statx_func(int dirfd, const char *restrict pathname, int flags,
unsigned int mask, struct my_statx *restrict statxbuf);

static statx_func* my_statx_func = NULL;
#endif //#defined(__linux__)

// static boolean linuxIsCreationTimeSupported(char* file)
EXPORT bool linuxIsCreationTimeSupported(char* file) {
#if defined(__linux__)
struct my_statx stx = {0};
int ret, atflag = AT_SYMLINK_NOFOLLOW;
unsigned int mask = STATX_BASIC_STATS | STATX_BTIME;

my_statx_func = (statx_func*) dlsym(RTLD_DEFAULT, "statx");
if (my_statx_func == NULL) {
return false;
}

if (file == NULL) {
printf("input file error!\n");
return false;
}

ret = my_statx_func(AT_FDCWD, file, atflag, mask, &stx);
if (ret != 0) {
return false;
}
// On some systems where statx is available but birth time might still not
// be supported as it's file system specific. The only reliable way to
// check for supported or not is looking at the filled in STATX_BTIME bit
// in the returned statx buffer mask.
if ((stx.stx_mask & STATX_BTIME) != 0)
return true;
return false;
#else
return false;
#endif
}

3 comments on commit 9d621d3

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@sendaoYan
Copy link
Member

Choose a reason for hiding this comment

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

/backport jdk23u

@openjdk
Copy link

@openjdk openjdk bot commented on 9d621d3 Oct 10, 2024

Choose a reason for hiding this comment

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

@sendaoYan the backport was successfully created on the branch backport-sendaoYan-9d621d39-master in my personal fork of openjdk/jdk23u. To create a pull request with this backport targeting openjdk/jdk23u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 9d621d39 from the openjdk/jdk repository.

The commit being backported was authored by SendaoYan on 10 Oct 2024 and was reviewed by Severin Gehwolf and Brian Burkhalter.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk23u:

$ git fetch https://github.com/openjdk-bots/jdk23u.git backport-sendaoYan-9d621d39-master:backport-sendaoYan-9d621d39-master
$ git checkout backport-sendaoYan-9d621d39-master
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk23u.git backport-sendaoYan-9d621d39-master

Please sign in to comment.