7129029: (fs) Unix file system provider should be buildable on platforms that don't support O_NOFOLLOW

Reviewed-by: alanb
This commit is contained in:
Charles Lee 2012-01-13 13:20:02 +00:00
parent 8b0a1174bd
commit 5c8b083c66
5 changed files with 25 additions and 6 deletions

View File

@ -36,8 +36,6 @@ import sun.nio.ch.SimpleAsynchronousFileChannelImpl;
import sun.misc.SharedSecrets;
import sun.misc.JavaIOFileDescriptorAccess;
import com.sun.nio.file.ExtendedOpenOption;
import static sun.nio.fs.UnixNativeDispatcher.*;
import static sun.nio.fs.UnixConstants.*;
@ -86,13 +84,13 @@ class UnixChannelFactory {
}
continue;
}
if (option == LinkOption.NOFOLLOW_LINKS) {
if (option == LinkOption.NOFOLLOW_LINKS && supportsNoFollowLinks()) {
flags.noFollowLinks = true;
continue;
}
if (option == null)
throw new NullPointerException();
throw new UnsupportedOperationException();
throw new UnsupportedOperationException(option + " not supported");
}
return flags;
}
@ -220,6 +218,15 @@ class UnixChannelFactory {
// follow links by default
boolean followLinks = true;
if (!flags.createNew && (flags.noFollowLinks || flags.deleteOnClose)) {
if (flags.deleteOnClose && !supportsNoFollowLinks()) {
try {
if (UnixFileAttributes.get(path, false).isSymbolicLink())
throw new UnixException("DELETE_ON_CLOSE specified and file is a symbolic link");
} catch (UnixException x) {
if (!flags.create || x.errno() != ENOENT)
throw x;
}
}
followLinks = false;
oflags |= O_NOFOLLOW;
}

View File

@ -395,7 +395,7 @@ public abstract class UnixFileSystemProvider
// can't return SecureDirectoryStream on kernels that don't support
// openat, etc.
if (!supportsAtSysCalls()) {
if (!supportsAtSysCalls() || !supportsNoFollowLinks()) {
try {
long ptr = opendir(dir);
return new UnixDirectoryStream(dir, ptr, filter);

View File

@ -548,6 +548,10 @@ class UnixNativeDispatcher {
return hasAtSysCalls;
}
static boolean supportsNoFollowLinks() {
return UnixConstants.O_NOFOLLOW != 0;
}
// initialize syscalls and fieldIDs
private static native int init();

View File

@ -767,8 +767,11 @@ class UnixPath
// package-private
int openForAttributeAccess(boolean followLinks) throws IOException {
int flags = O_RDONLY;
if (!followLinks)
if (!followLinks) {
if (!supportsNoFollowLinks())
throw new IOException("NOFOLLOW_LINKS is not supported on this platform");
flags |= O_NOFOLLOW;
}
try {
return open(this, flags, 0);
} catch (UnixException x) {

View File

@ -64,7 +64,12 @@ int main(int argc, const char* argv[]) {
DEFX(O_TRUNC);
DEFX(O_SYNC);
DEFX(O_DSYNC);
#ifdef O_NOFOLLOW
DEFX(O_NOFOLLOW);
#else
// not supported (dummy values will not be used at runtime).
emitX("O_NOFOLLOW", 0x0);
#endif
// mode masks
emitX("S_IAMB",