8305664: [BACKOUT] (fs) Remove FileSystem support for resolving against a default directory (chdir configuration)

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2023-04-05 20:17:07 +00:00
parent 39f12a88e7
commit 507c49a3ab
15 changed files with 104 additions and 44 deletions

View file

@ -52,7 +52,6 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import jdk.internal.misc.Blocker;
import jdk.internal.util.StaticProperty;
import sun.nio.ch.DirectBuffer;
import sun.nio.ch.IOStatus;
import sun.security.action.GetPropertyAction;
@ -74,17 +73,39 @@ abstract class UnixFileSystem
private final UnixFileSystemProvider provider;
private final byte[] defaultDirectory;
private final boolean needToResolveAgainstDefaultDirectory;
private final UnixPath rootDirectory;
// package-private
UnixFileSystem(UnixFileSystemProvider provider) {
String dir = StaticProperty.userDir();
UnixFileSystem(UnixFileSystemProvider provider, String dir) {
this.provider = provider;
this.defaultDirectory = Util.toBytes(UnixPath.normalizeAndCheck(dir));
if (this.defaultDirectory[0] != '/') {
throw new RuntimeException("default directory must be absolute");
}
// if process-wide chdir is allowed or default directory is not the
// process working directory then paths must be resolved against the
// default directory.
String propValue = GetPropertyAction
.privilegedGetProperty("sun.nio.fs.chdirAllowed", "false");
boolean chdirAllowed = propValue.isEmpty() ? true : Boolean.parseBoolean(propValue);
if (chdirAllowed) {
this.needToResolveAgainstDefaultDirectory = true;
} else {
byte[] cwd = UnixNativeDispatcher.getcwd();
boolean defaultIsCwd = (cwd.length == defaultDirectory.length);
if (defaultIsCwd) {
for (int i=0; i<cwd.length; i++) {
if (cwd[i] != defaultDirectory[i]) {
defaultIsCwd = false;
break;
}
}
}
this.needToResolveAgainstDefaultDirectory = !defaultIsCwd;
}
// the root directory
this.rootDirectory = new UnixPath(this, "/");
}
@ -94,6 +115,10 @@ abstract class UnixFileSystem
return defaultDirectory;
}
boolean needToResolveAgainstDefaultDirectory() {
return needToResolveAgainstDefaultDirectory;
}
boolean isCaseInsensitiveAndPreserving() {
return false;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2022, 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
@ -75,7 +75,7 @@ public abstract class UnixFileSystemProvider
private final UnixFileSystem theFileSystem;
public UnixFileSystemProvider() {
theFileSystem = newFileSystem();
theFileSystem = newFileSystem(StaticProperty.userDir());
}
UnixFileSystem theFileSystem() {
@ -83,9 +83,9 @@ public abstract class UnixFileSystemProvider
}
/**
* Constructs a new file system.
* Constructs a new file system using the given default directory.
*/
abstract UnixFileSystem newFileSystem();
abstract UnixFileSystem newFileSystem(String dir);
@Override
public final String getScheme() {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2022, 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
@ -52,6 +52,11 @@ class UnixNativeDispatcher {
return buffer;
}
/**
* char *getcwd(char *buf, size_t size);
*/
static native byte[] getcwd();
/**
* int dup(int filedes)
*/

View file

@ -138,12 +138,18 @@ class UnixPath implements Path {
// use this path when making system/library calls
byte[] getByteArrayForSysCalls() {
if (!isEmpty()) {
return path;
// resolve against default directory if required (chdir allowed or
// file system default directory is not working directory)
if (getFileSystem().needToResolveAgainstDefaultDirectory()) {
return resolve(getFileSystem().defaultDirectory(), path);
} else {
// empty path case will access current directory
byte[] here = { '.' };
return here;
if (!isEmpty()) {
return path;
} else {
// empty path case will access current directory
byte[] here = { '.' };
return here;
}
}
}
@ -154,7 +160,11 @@ class UnixPath implements Path {
// use this path for permission checks
String getPathForPermissionCheck() {
return toString();
if (getFileSystem().needToResolveAgainstDefaultDirectory()) {
return Util.toString(getByteArrayForSysCalls());
} else {
return toString();
}
}
// Checks that the given file is a UnixPath