Skip to content

Commit

Permalink
8316304: (fs) Add support for BasicFileAttributes.creationTime() for …
Browse files Browse the repository at this point in the history
…Linux
  • Loading branch information
jerboaa committed Sep 21, 2023
1 parent d75d977 commit 3ead43a
Show file tree
Hide file tree
Showing 13 changed files with 698 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2023, Red Hat, Inc.
* 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/

package sun.nio.fs;

import java.io.IOException;
import java.nio.file.attribute.BasicFileAttributes;

import sun.nio.fs.UnixFileAttributeViews.Basic;

class LinuxBasicAttributesView extends Basic {

LinuxBasicAttributesView(UnixPath file, boolean followLinks) {
super(file, followLinks);
}

@Override
public BasicFileAttributes readAttributes() throws IOException {
file.checkRead();
try {
LinuxFileAttributes attrs =
LinuxFileAttributes.get(file, followLinks);
return attrs.asBasicFileAttributes();
} catch (UnixException x) {
x.rethrowAsIOException(file);
return null; // keep compiler happy
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@

package sun.nio.fs;

import java.nio.file.attribute.*;
import static sun.nio.fs.UnixConstants.ENODATA;
import static sun.nio.fs.UnixNativeDispatcher.close;

import java.io.IOException;
import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.DosFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.Map;
import java.util.Set;
import java.io.IOException;
import jdk.internal.misc.Unsafe;

import static sun.nio.fs.UnixNativeDispatcher.*;
import static sun.nio.fs.UnixConstants.*;
import jdk.internal.misc.Unsafe;

/**
* Linux implementation of DosFileAttributeView for use on file systems such
Expand Down Expand Up @@ -120,7 +123,7 @@ public DosFileAttributes readAttributes() throws IOException {
int fd = -1;
try {
fd = file.openForAttributeAccess(followLinks);
final UnixFileAttributes attrs = UnixFileAttributes.get(fd);
final LinuxFileAttributes attrs = LinuxFileAttributes.get(fd);
final int dosAttribute = getDosAttribute(fd);

return new DosFileAttributes() {
Expand Down
81 changes: 81 additions & 0 deletions src/java.base/linux/classes/sun/nio/fs/LinuxFileAttributes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2023, Red Hat, Inc.
* 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/

package sun.nio.fs;

import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.PosixFileAttributes;

class LinuxFileAttributes extends UnixFileAttributes implements PosixFileAttributes {

private final boolean isStatxSupported;
private long stx_birthtime_nsec;

private LinuxFileAttributes(boolean isStatxSupported) {
this.isStatxSupported = isStatxSupported;
}

// get the Linux file attributes for a given file
static LinuxFileAttributes get(UnixPath path, boolean followLinks) throws UnixException {
boolean statxSupport = LinuxNativeDispatcher.isStatxSupported();
LinuxFileAttributes attrs = new LinuxFileAttributes(statxSupport);
if (statxSupport) {
LinuxNativeDispatcher.statx(path, attrs, followLinks);
} else {
// this sets attribute values in attrs
UnixFileAttributes.get(path, followLinks, attrs);
}
return attrs;
}

// get the LinuxFileAttributes for a given file descriptor
static LinuxFileAttributes get(int fd) throws UnixException {
boolean statxSupport = LinuxNativeDispatcher.isStatxSupported();
LinuxFileAttributes attrs = new LinuxFileAttributes(statxSupport);
if (statxSupport) {
LinuxNativeDispatcher.statxfd(fd, attrs);
} else {
// this sets attribute values in attrs
UnixFileAttributes.get(fd, attrs);
}
return attrs;
}

// wrap this object with BasicFileAttributes object to prevent leaking of
// user information
BasicFileAttributes asBasicFileAttributes() {
return UnixAsBasicFileAttributes.wrap(this);
}

@Override
public FileTime creationTime() {
if (isStatxSupported) {
return UnixFileAttributes.toFileTime(st_birthtime_sec, stx_birthtime_nsec);
} else {
return super.creationTime();
}
}
}
4 changes: 2 additions & 2 deletions src/java.base/linux/classes/sun/nio/fs/LinuxFileStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ UnixMountEntry findMountEntry() throws IOException {
fs.getMountEntries("/proc/mounts");
UnixPath parent = path.getParent();
while (parent != null) {
UnixFileAttributes attrs = null;
LinuxFileAttributes attrs = null;
try {
attrs = UnixFileAttributes.get(parent, true);
attrs = LinuxFileAttributes.get(parent, true);
} catch (UnixException x) {
x.rethrowAsIOException(parent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@

package sun.nio.fs;

import java.nio.file.*;
import java.nio.file.attribute.*;
import java.nio.file.spi.FileTypeDetector;
import java.io.IOException;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.DosFileAttributes;
import java.nio.file.attribute.FileAttributeView;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.nio.file.spi.FileTypeDetector;

import jdk.internal.util.StaticProperty;
import sun.nio.fs.UnixFileAttributeViews.Posix;

/**
* Linux implementation of FileSystemProvider
Expand Down Expand Up @@ -65,6 +75,20 @@ public <V extends FileAttributeView> V getFileAttributeView(Path obj,
return (V) new LinuxUserDefinedFileAttributeView(UnixPath.toUnixPath(obj),
Util.followLinks(options));
}
if (type == BasicFileAttributeView.class) {
return (V) new LinuxBasicAttributesView(UnixPath.toUnixPath(obj),
Util.followLinks(options));
}
if (type == PosixFileAttributeView.class) {
return (V) new LinuxPosixAttributesView(UnixPath.toUnixPath(obj),
Util.followLinks(options));
}
if (type == FileOwnerAttributeView.class) {
Posix posixView = new LinuxPosixAttributesView(UnixPath.toUnixPath(obj),
Util.followLinks(options));
return (V) UnixFileAttributeViews.createOwnerView(posixView);
}

return super.getFileAttributeView(obj, type, options);
}

Expand All @@ -81,6 +105,19 @@ public DynamicFileAttributeView getFileAttributeView(Path obj,
return new LinuxUserDefinedFileAttributeView(UnixPath.toUnixPath(obj),
Util.followLinks(options));
}
if (name.equals("basic")) {
return (DynamicFileAttributeView)getFileAttributeView(obj, BasicFileAttributeView.class, options);
}
if (name.equals("posix")) {
return (DynamicFileAttributeView)getFileAttributeView(obj, PosixFileAttributeView.class, options);
}
if (name.equals("owner")) {
return (DynamicFileAttributeView)getFileAttributeView(obj, FileOwnerAttributeView.class, options);
}
if (name.equals("unix")) {
return new LinuxUnixAttributesView(UnixPath.toUnixPath(obj),
Util.followLinks(options));
}
return super.getFileAttributeView(obj, name, options);
}

Expand All @@ -95,6 +132,14 @@ public <A extends BasicFileAttributes> A readAttributes(Path file,
DosFileAttributeView view =
getFileAttributeView(file, DosFileAttributeView.class, options);
return (A) view.readAttributes();
} else if (type == BasicFileAttributes.class) {
BasicFileAttributeView view =
getFileAttributeView(file, BasicFileAttributeView.class, options);
return (A) view.readAttributes();
} else if (type == PosixFileAttributes.class) {
PosixFileAttributeView view =
getFileAttributeView(file, PosixFileAttributeView.class, options);
return (A) view.readAttributes();
} else {
return super.readAttributes(file, type, options);
}
Expand Down
53 changes: 53 additions & 0 deletions src/java.base/linux/classes/sun/nio/fs/LinuxNativeDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@

package sun.nio.fs;

import jdk.internal.misc.Blocker;

/**
* Linux specific system calls.
*/

class LinuxNativeDispatcher extends UnixNativeDispatcher {
private LinuxNativeDispatcher() { }

// set by JNI code based on glibc support
private static volatile boolean supports_statx = false;

/**
* FILE *setmntent(const char *filename, const char *type);
*/
Expand Down Expand Up @@ -68,6 +73,50 @@ static native int getmntent0(long fp, UnixMountEntry entry, long buffer, int buf
static native int posix_fadvise(int fd, long offset, long len, int advice)
throws UnixException;

static void statx(UnixPath path, LinuxFileAttributes attrs, boolean followLinks)
throws UnixException {
try (NativeBuffer buffer = copyToNativeBuffer(path)) {
long comp = Blocker.begin();
try {
int errno = statx0(buffer.address(), attrs, followLinks);
if (errno != 0) {
throw new UnixException(errno);
}
} finally {
Blocker.end(comp);
}
}
}

static void statxfd(int fd, LinuxFileAttributes attrs)
throws UnixException {
long comp = Blocker.begin();
try {
int errno = statxfd0(fd, attrs);
if (errno != 0) {
throw new UnixException(errno);
}
} finally {
Blocker.end(comp);
}
}

/**
* int statx(int dirfd, const char *restrict pathname, int flags,
* unsigned int mask, struct statx *restrict statxbuf);
*
* statx supports lookups with a file descriptor if path name is empty
* and AT_EMPTY_PATH flag is set in flags. In that case the fd passed in by
* dirfd will be used.
*/
static native int statxfd0(int fd, LinuxFileAttributes attrs);

/**
* int statx(int dirfd, const char *restrict pathname, int flags,
* unsigned int mask, struct statx *restrict statxbuf);
*/
static native int statx0(long address, LinuxFileAttributes attrs, boolean followLinks);

/**
* Copies data between file descriptors {@code src} and {@code dst} using
* a platform-specific function or system call possibly having kernel
Expand All @@ -86,6 +135,10 @@ static native int posix_fadvise(int fd, long offset, long len, int advice)
static native int directCopy0(int dst, int src, long addressToPollForCancel)
throws UnixException;

static boolean isStatxSupported() {
return supports_statx;
}

// initialize
private static native void init();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2023, Red Hat, Inc.
* 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/

package sun.nio.fs;

import java.io.IOException;

import sun.nio.fs.UnixFileAttributeViews.Posix;

class LinuxPosixAttributesView extends Posix {

LinuxPosixAttributesView(UnixPath file, boolean followLinks) {
super(file, followLinks);
}

@Override
public UnixFileAttributes readAttributes() throws IOException {
checkReadExtended();
try {
LinuxFileAttributes attrs =
LinuxFileAttributes.get(file, followLinks);
return attrs;
} catch (UnixException x) {
x.rethrowAsIOException(file);
return null; // keep compiler happy
}
}
}
Loading

0 comments on commit 3ead43a

Please sign in to comment.