undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
|
|
@ -0,0 +1,157 @@
|
|||
/*
|
||||
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. 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.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 4313887 6838333 8364277
|
||||
* @summary Unit test for java.nio.file.attribute.BasicFileAttributeView
|
||||
* @library ../.. /test/lib
|
||||
* @build jdk.test.lib.Platform
|
||||
* jdk.test.lib.util.FileUtils
|
||||
* @run main/othervm --enable-native-access=ALL-UNNAMED Basic
|
||||
*/
|
||||
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.attribute.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.io.*;
|
||||
|
||||
import jdk.test.lib.Platform;
|
||||
import jdk.test.lib.util.FileUtils;
|
||||
|
||||
public class Basic {
|
||||
|
||||
static void check(boolean okay, String msg) {
|
||||
if (!okay)
|
||||
throw new RuntimeException(msg);
|
||||
}
|
||||
|
||||
static void checkAttributesOfDirectory(Path dir)
|
||||
throws IOException
|
||||
{
|
||||
BasicFileAttributes attrs = Files.readAttributes(dir, BasicFileAttributes.class);
|
||||
check(attrs.isDirectory(), "is a directory");
|
||||
check(!attrs.isRegularFile(), "is not a regular file");
|
||||
check(!attrs.isSymbolicLink(), "is not a link");
|
||||
check(!attrs.isOther(), "is not other");
|
||||
|
||||
// last-modified-time should match java.io.File in seconds
|
||||
File f = new File(dir.toString());
|
||||
check(f.lastModified()/1000 == attrs.lastModifiedTime().to(TimeUnit.SECONDS),
|
||||
"last-modified time should be the same");
|
||||
}
|
||||
|
||||
static void checkAttributesOfFile(Path dir, Path file)
|
||||
throws IOException
|
||||
{
|
||||
BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
|
||||
check(attrs.isRegularFile(), "is a regular file");
|
||||
check(!attrs.isDirectory(), "is not a directory");
|
||||
check(!attrs.isSymbolicLink(), "is not a link");
|
||||
check(!attrs.isOther(), "is not other");
|
||||
|
||||
// size and last-modified-time should match java.io.File in seconds
|
||||
File f = new File(file.toString());
|
||||
check(f.length() == attrs.size(), "size should be the same");
|
||||
check(f.lastModified()/1000 == attrs.lastModifiedTime().to(TimeUnit.SECONDS),
|
||||
"last-modified time should be the same");
|
||||
|
||||
// copy last-modified time from directory to file,
|
||||
// re-read attribtues, and check they match
|
||||
BasicFileAttributeView view =
|
||||
Files.getFileAttributeView(file, BasicFileAttributeView.class);
|
||||
BasicFileAttributes dirAttrs = Files.readAttributes(dir, BasicFileAttributes.class);
|
||||
view.setTimes(dirAttrs.lastModifiedTime(), null, null);
|
||||
|
||||
attrs = view.readAttributes();
|
||||
check(attrs.lastModifiedTime().equals(dirAttrs.lastModifiedTime()),
|
||||
"last-modified time should be equal");
|
||||
|
||||
// security tests
|
||||
check (!(attrs instanceof PosixFileAttributes),
|
||||
"should not be able to cast to PosixFileAttributes");
|
||||
}
|
||||
|
||||
static void checkAttributesOfLink(Path link)
|
||||
throws IOException
|
||||
{
|
||||
BasicFileAttributes attrs =
|
||||
Files.readAttributes(link, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
|
||||
check(attrs.isSymbolicLink(), "is a link");
|
||||
check(!attrs.isDirectory(), "is a directory");
|
||||
check(!attrs.isRegularFile(), "is not a regular file");
|
||||
check(!attrs.isOther(), "is not other");
|
||||
}
|
||||
|
||||
static void checkAttributesOfJunction(Path junction)
|
||||
throws IOException
|
||||
{
|
||||
BasicFileAttributes attrs =
|
||||
Files.readAttributes(junction, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
|
||||
check(!attrs.isSymbolicLink(), "is a link");
|
||||
check(!attrs.isDirectory(), "is a directory");
|
||||
check(!attrs.isRegularFile(), "is not a regular file");
|
||||
check(attrs.isOther(), "is other");
|
||||
}
|
||||
|
||||
static void attributeReadWriteTests(Path dir)
|
||||
throws IOException
|
||||
{
|
||||
// create file
|
||||
Path file = dir.resolve("foo");
|
||||
try (OutputStream out = Files.newOutputStream(file)) {
|
||||
out.write("this is not an empty file".getBytes("UTF-8"));
|
||||
}
|
||||
|
||||
// check attributes of directory and file
|
||||
checkAttributesOfDirectory(dir);
|
||||
checkAttributesOfFile(dir, file);
|
||||
|
||||
// symbolic links may be supported
|
||||
Path link = dir.resolve("link");
|
||||
try {
|
||||
Files.createSymbolicLink(link, file);
|
||||
checkAttributesOfLink(link);
|
||||
} catch (IOException | UnsupportedOperationException x) {
|
||||
if (!Platform.isWindows())
|
||||
return;
|
||||
}
|
||||
|
||||
// NTFS junctions are Windows-only
|
||||
if (Platform.isWindows()) {
|
||||
Path junction = dir.resolve("junction");
|
||||
FileUtils.createWinDirectoryJunction(junction, dir);
|
||||
checkAttributesOfJunction(junction);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
// create temporary directory to run tests
|
||||
Path dir = TestUtil.createTemporaryDirectory();
|
||||
try {
|
||||
attributeReadWriteTests(dir);
|
||||
} finally {
|
||||
TestUtil.removeAll(dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,160 @@
|
|||
/*
|
||||
* 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
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/* @test id=tmp
|
||||
* @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 /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 /java/foreign
|
||||
* @build jdk.test.lib.Platform NativeTestHelper
|
||||
* @run main/othervm/native --enable-native-access=ALL-UNNAMED CreationTime .
|
||||
*/
|
||||
|
||||
import java.lang.foreign.Linker;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.attribute.*;
|
||||
import java.time.Instant;
|
||||
import java.io.IOException;
|
||||
|
||||
import jdk.test.lib.Platform;
|
||||
import jtreg.SkippedException;
|
||||
|
||||
public class CreationTime {
|
||||
|
||||
/**
|
||||
* Reads the creationTime attribute
|
||||
*/
|
||||
private static FileTime creationTime(Path file) throws IOException {
|
||||
return Files.readAttributes(file, BasicFileAttributes.class).creationTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the creationTime attribute
|
||||
*/
|
||||
private static void setCreationTime(Path file, FileTime time) throws IOException {
|
||||
BasicFileAttributeView view =
|
||||
Files.getFileAttributeView(file, BasicFileAttributeView.class);
|
||||
view.setTimes(null, null, time);
|
||||
}
|
||||
|
||||
static void test(Path top) throws IOException {
|
||||
Path file = Files.createFile(top.resolve("foo"));
|
||||
|
||||
/**
|
||||
* Check that creationTime reported
|
||||
*/
|
||||
FileTime creationTime = creationTime(file);
|
||||
Instant now = Instant.now();
|
||||
if (Math.abs(creationTime.toMillis()-now.toEpochMilli()) > 10000L) {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the creationTime attribute supported here?
|
||||
*/
|
||||
boolean supportsCreationTimeRead = false;
|
||||
boolean supportsCreationTimeWrite = false;
|
||||
if (Platform.isOSX()) {
|
||||
String type = Files.getFileStore(file).type();
|
||||
if (type.equals("apfs") || type.equals("hfs")) {
|
||||
supportsCreationTimeRead = true;
|
||||
supportsCreationTimeWrite = true;
|
||||
}
|
||||
} else if (Platform.isWindows()) {
|
||||
String type = Files.getFileStore(file).type();
|
||||
if (type.equals("NTFS") || type.equals("FAT")) {
|
||||
supportsCreationTimeRead = true;
|
||||
supportsCreationTimeWrite = true;
|
||||
}
|
||||
} else if (Platform.isLinux()) {
|
||||
// Creation time read depends on statx system call support
|
||||
try {
|
||||
supportsCreationTimeRead = CreationTimeHelper.
|
||||
linuxIsCreationTimeSupported(file.toAbsolutePath().toString());
|
||||
} catch (Throwable e) {
|
||||
supportsCreationTimeRead = false;
|
||||
}
|
||||
// Creation time updates are not supported on Linux
|
||||
supportsCreationTimeWrite = false;
|
||||
}
|
||||
System.out.println(top + " supportsCreationTimeRead == " + supportsCreationTimeRead);
|
||||
|
||||
/**
|
||||
* If the creation-time attribute is supported then change the file's
|
||||
* last modified and check that it doesn't change the creation-time.
|
||||
*/
|
||||
if (supportsCreationTimeRead) {
|
||||
// change modified time by +1 hour
|
||||
Instant plusHour = Instant.now().plusSeconds(60L * 60L);
|
||||
Files.setLastModifiedTime(file, FileTime.from(plusHour));
|
||||
FileTime current = creationTime(file);
|
||||
if (!current.equals(creationTime)) {
|
||||
System.err.println("current = " + current);
|
||||
System.err.println("creationTime = " + creationTime);
|
||||
throw new RuntimeException("Creation time should not have changed");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If the creation-time attribute is supported and can be changed then
|
||||
* check that the change is effective.
|
||||
*/
|
||||
if (supportsCreationTimeWrite) {
|
||||
// change creation time by -1 hour
|
||||
Instant minusHour = Instant.now().minusSeconds(60L * 60L);
|
||||
creationTime = FileTime.from(minusHour);
|
||||
setCreationTime(file, creationTime);
|
||||
FileTime current = creationTime(file);
|
||||
if (Math.abs(creationTime.toMillis()-current.toMillis()) > 1000L)
|
||||
throw new RuntimeException("Creation time not changed");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
// create temporary directory to run tests
|
||||
Path dir;
|
||||
if (args.length == 0) {
|
||||
dir = TestUtil.createTemporaryDirectory();
|
||||
} else {
|
||||
dir = TestUtil.createTemporaryDirectory(args[0]);
|
||||
}
|
||||
try {
|
||||
test(dir);
|
||||
} finally {
|
||||
TestUtil.removeAll(dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,152 @@
|
|||
/*
|
||||
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. 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.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 8181493 8231174 8343417
|
||||
* @summary Verify that nanosecond precision is maintained for file timestamps
|
||||
* @library ../.. /test/lib
|
||||
* @build jdk.test.lib.Platform
|
||||
* @modules java.base/sun.nio.fs:+open
|
||||
* @run main SetTimesNanos
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.FileStore;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.nio.file.attribute.BasicFileAttributeView;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static java.nio.file.LinkOption.*;
|
||||
import static java.util.concurrent.TimeUnit.*;
|
||||
|
||||
import jdk.test.lib.Platform;
|
||||
import jtreg.SkippedException;
|
||||
|
||||
public class SetTimesNanos {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Path dirPath = Path.of("test");
|
||||
Path dir = Files.createDirectory(dirPath);
|
||||
FileStore store = Files.getFileStore(dir);
|
||||
System.out.format("FileStore: \"%s\" on %s (%s)%n",
|
||||
dir, store.name(), store.type());
|
||||
|
||||
Set<String> testedTypes = Platform.isWindows() ?
|
||||
Set.of("NTFS") : Set.of("apfs", "ext4", "xfs", "zfs");
|
||||
if (!testedTypes.contains(store.type())) {
|
||||
throw new SkippedException(store.type() + " not in " + testedTypes);
|
||||
}
|
||||
|
||||
testNanos(dir);
|
||||
|
||||
Path file = Files.createFile(dir.resolve("test.dat"));
|
||||
testNanos(file);
|
||||
|
||||
if (TestUtil.supportsSymbolicLinks(Path.of(""))) {
|
||||
testNanosLink(false);
|
||||
testNanosLink(true);
|
||||
}
|
||||
}
|
||||
|
||||
private static void testNanos(Path path) throws IOException {
|
||||
// Set modification and access times
|
||||
// Time stamp = "2017-01-01 01:01:01.123456789";
|
||||
long timeNanos = 1_483_261_261L*1_000_000_000L + 123_456_789L;
|
||||
FileTime pathTime = FileTime.from(timeNanos, NANOSECONDS);
|
||||
BasicFileAttributeView view =
|
||||
Files.getFileAttributeView(path, BasicFileAttributeView.class);
|
||||
view.setTimes(pathTime, pathTime, null);
|
||||
|
||||
// Windows file time resolution is 100ns so truncate
|
||||
if (Platform.isWindows()) {
|
||||
timeNanos = 100L*(timeNanos/100L);
|
||||
}
|
||||
|
||||
// Read attributes
|
||||
BasicFileAttributes attrs =
|
||||
Files.readAttributes(path, BasicFileAttributes.class);
|
||||
|
||||
// Check timestamps
|
||||
String[] timeNames = new String[] {"modification", "access"};
|
||||
FileTime[] times = new FileTime[] {attrs.lastModifiedTime(),
|
||||
attrs.lastAccessTime()};
|
||||
for (int i = 0; i < timeNames.length; i++) {
|
||||
long nanos = times[i].to(NANOSECONDS);
|
||||
if (nanos != timeNanos) {
|
||||
throw new RuntimeException("Expected " + timeNames[i] +
|
||||
" timestamp to be '" + timeNanos + "', but was '" +
|
||||
nanos + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void testNanosLink(boolean absolute) throws IOException {
|
||||
System.out.println("absolute: " + absolute);
|
||||
|
||||
var target = Path.of("target");
|
||||
var symlink = Path.of("symlink");
|
||||
if (absolute)
|
||||
symlink = symlink.toAbsolutePath();
|
||||
|
||||
try {
|
||||
Files.createFile(target);
|
||||
Files.createSymbolicLink(symlink, target);
|
||||
|
||||
long timeNanos = 1730417633157646106L;
|
||||
|
||||
// Windows file time resolution is 100ns so truncate
|
||||
if (Platform.isWindows()) {
|
||||
timeNanos = 100L*(timeNanos/100L);
|
||||
}
|
||||
|
||||
var newTime = FileTime.from(timeNanos, NANOSECONDS);
|
||||
System.out.println("newTime: " + newTime.to(NANOSECONDS));
|
||||
|
||||
for (Path p : List.of(target, symlink)) {
|
||||
System.out.println("p: " + p);
|
||||
|
||||
var view = Files.getFileAttributeView(p,
|
||||
BasicFileAttributeView.class, NOFOLLOW_LINKS);
|
||||
view.setTimes(newTime, newTime, null);
|
||||
var attrs = view.readAttributes();
|
||||
|
||||
if (!attrs.lastAccessTime().equals(newTime))
|
||||
throw new RuntimeException("Last access time "
|
||||
+ attrs.lastAccessTime()
|
||||
+ " != " + newTime);
|
||||
if (!attrs.lastAccessTime().equals(newTime))
|
||||
throw new RuntimeException("Last modified time "
|
||||
+ attrs.lastModifiedTime()
|
||||
+ " != " + newTime);
|
||||
}
|
||||
} finally {
|
||||
Files.deleteIfExists(target);
|
||||
Files.deleteIfExists(symlink);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,152 @@
|
|||
/*
|
||||
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. 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.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 8139133
|
||||
* @summary Verify ability to set time attributes of socket files with no device
|
||||
* @requires os.family == "linux"
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardWatchEventKinds;
|
||||
import java.nio.file.WatchKey;
|
||||
import java.nio.file.WatchService;
|
||||
import java.nio.file.attribute.BasicFileAttributeView;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
|
||||
public class UnixSocketFile {
|
||||
private static final String TEST_SUB_DIR = "UnixSocketFile";
|
||||
private static final String SOCKET_FILE_NAME = "mysocket";
|
||||
private static final String CMD_BASE = "nc -lU";
|
||||
|
||||
public static void main(String[] args)
|
||||
throws InterruptedException, IOException {
|
||||
|
||||
// Use 'which' to verify that 'nc' is available and skip the test
|
||||
// if it is not.
|
||||
Process proc = Runtime.getRuntime().exec("which nc");
|
||||
InputStream stdout = proc.getInputStream();
|
||||
int b = stdout.read();
|
||||
proc.destroy();
|
||||
if (b == -1) {
|
||||
System.err.println("Netcat command unavailable; skipping test.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Verify that 'nc' accepts '-U' for Unix domain sockets.
|
||||
// Skip the test if it is not.
|
||||
Process procHelp = Runtime.getRuntime().exec(CMD_BASE + " -h");
|
||||
if (procHelp.waitFor() != 0) {
|
||||
System.err.println("Netcat does not accept required options; skipping test.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Create a new sub-directory of the nominal test directory in which
|
||||
// 'nc' will create the socket file.
|
||||
String testSubDir = System.getProperty("test.dir", ".")
|
||||
+ File.separator + TEST_SUB_DIR;
|
||||
Path socketTestDir = Paths.get(testSubDir);
|
||||
Files.createDirectory(socketTestDir);
|
||||
|
||||
// Set the path of the socket file.
|
||||
String socketFilePath = testSubDir + File.separator
|
||||
+ SOCKET_FILE_NAME;
|
||||
|
||||
// Create a process which executes the nc (netcat) utility to create
|
||||
// a socket file at the indicated location.
|
||||
FileSystem fs = FileSystems.getDefault();
|
||||
try (WatchService ws = fs.newWatchService()) {
|
||||
// Watch the test sub-directory to receive notification when an
|
||||
// entry, i.e., the socket file, is added to the sub-directory.
|
||||
WatchKey wk = socketTestDir.register(ws,
|
||||
StandardWatchEventKinds.ENTRY_CREATE);
|
||||
|
||||
// Execute the 'nc' command.
|
||||
proc = Runtime.getRuntime().exec(CMD_BASE + " " + socketFilePath);
|
||||
|
||||
// Wait until the socket file is created.
|
||||
WatchKey key = ws.take();
|
||||
if (key != wk) {
|
||||
throw new RuntimeException("Unknown entry created - expected: "
|
||||
+ wk.watchable() + ", actual: " + key.watchable());
|
||||
}
|
||||
wk.cancel();
|
||||
}
|
||||
|
||||
// Verify that the socket file in fact exists.
|
||||
Path socketPath = fs.getPath(socketFilePath);
|
||||
if (!Files.exists(socketPath)) {
|
||||
throw new RuntimeException("Socket file " + socketFilePath
|
||||
+ " was not created by \"nc\" command.");
|
||||
}
|
||||
|
||||
// Retrieve the most recent access and modification times of the
|
||||
// socket file; print the values.
|
||||
BasicFileAttributeView attributeView = Files.getFileAttributeView(
|
||||
socketPath, BasicFileAttributeView.class);
|
||||
BasicFileAttributes oldAttributes = attributeView.readAttributes();
|
||||
FileTime oldAccessTime = oldAttributes.lastAccessTime();
|
||||
FileTime oldModifiedTime = oldAttributes.lastModifiedTime();
|
||||
System.out.println("Old times: " + oldAccessTime
|
||||
+ " " + oldModifiedTime);
|
||||
|
||||
// Calculate the time to which the access and modification times of the
|
||||
// socket file will be changed.
|
||||
FileTime newFileTime =
|
||||
FileTime.fromMillis(oldAccessTime.toMillis() + 1066);
|
||||
|
||||
try {
|
||||
// Set the access and modification times of the socket file.
|
||||
attributeView.setTimes(newFileTime, newFileTime, null);
|
||||
|
||||
// Retrieve the updated access and modification times of the
|
||||
// socket file; print the values.
|
||||
FileTime newAccessTime = null;
|
||||
FileTime newModifiedTime = null;
|
||||
BasicFileAttributes newAttributes = attributeView.readAttributes();
|
||||
newAccessTime = newAttributes.lastAccessTime();
|
||||
newModifiedTime = newAttributes.lastModifiedTime();
|
||||
System.out.println("New times: " + newAccessTime + " "
|
||||
+ newModifiedTime);
|
||||
|
||||
// Verify that the updated times have the expected values.
|
||||
if ((newAccessTime != null && !newAccessTime.equals(newFileTime))
|
||||
|| (newModifiedTime != null
|
||||
&& !newModifiedTime.equals(newFileTime))) {
|
||||
throw new RuntimeException("Failed to set correct times.");
|
||||
}
|
||||
} finally {
|
||||
// Destry the process running netcat and delete the socket file.
|
||||
proc.destroy();
|
||||
Files.delete(socketPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,124 @@
|
|||
/*
|
||||
* 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 <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.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
|
||||
#ifndef AT_SYMLINK_NOFOLLOW
|
||||
#define AT_SYMLINK_NOFOLLOW 0x100
|
||||
#endif
|
||||
#ifndef AT_FDCWD
|
||||
#define AT_FDCWD -100
|
||||
#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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue