< prev index next >

src/java.base/unix/classes/sun/nio/fs/UnixFileSystem.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.fs;
  27 
  28 import java.nio.file.*;
  29 import java.nio.file.attribute.*;
  30 import java.nio.file.spi.*;
  31 import java.io.IOException;
  32 import java.util.*;
  33 import java.util.regex.Pattern;
  34 import java.security.AccessController;
  35 import sun.security.action.GetPropertyAction;
  36 
  37 /**
  38  * Base implementation of FileSystem for Unix-like implementations.
  39  */
  40 
  41 abstract class UnixFileSystem
  42     extends FileSystem
  43 {
  44     private final UnixFileSystemProvider provider;
  45     private final byte[] defaultDirectory;
  46     private final boolean needToResolveAgainstDefaultDirectory;
  47     private final UnixPath rootDirectory;
  48 
  49     // package-private
  50     UnixFileSystem(UnixFileSystemProvider provider, String dir) {
  51         this.provider = provider;
  52         this.defaultDirectory = Util.toBytes(UnixPath.normalizeAndCheck(dir));
  53         if (this.defaultDirectory[0] != '/') {
  54             throw new RuntimeException("default directory must be absolute");
  55         }
  56 
  57         // if process-wide chdir is allowed or default directory is not the
  58         // process working directory then paths must be resolved against the
  59         // default directory.
  60         String propValue = AccessController.doPrivileged(
  61             new GetPropertyAction("sun.nio.fs.chdirAllowed", "false"));
  62         boolean chdirAllowed = (propValue.length() == 0) ?
  63             true : Boolean.valueOf(propValue);
  64         if (chdirAllowed) {
  65             this.needToResolveAgainstDefaultDirectory = true;
  66         } else {
  67             byte[] cwd = UnixNativeDispatcher.getcwd();
  68             boolean defaultIsCwd = (cwd.length == defaultDirectory.length);
  69             if (defaultIsCwd) {
  70                 for (int i=0; i<cwd.length; i++) {
  71                     if (cwd[i] != defaultDirectory[i]) {
  72                         defaultIsCwd = false;
  73                         break;
  74                     }
  75                 }
  76             }
  77             this.needToResolveAgainstDefaultDirectory = !defaultIsCwd;
  78         }
  79 
  80         // the root directory
  81         this.rootDirectory = new UnixPath(this, "/");




  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.fs;
  27 
  28 import java.nio.file.*;
  29 import java.nio.file.attribute.*;
  30 import java.nio.file.spi.*;
  31 import java.io.IOException;
  32 import java.util.*;
  33 import java.util.regex.Pattern;

  34 import sun.security.action.GetPropertyAction;
  35 
  36 /**
  37  * Base implementation of FileSystem for Unix-like implementations.
  38  */
  39 
  40 abstract class UnixFileSystem
  41     extends FileSystem
  42 {
  43     private final UnixFileSystemProvider provider;
  44     private final byte[] defaultDirectory;
  45     private final boolean needToResolveAgainstDefaultDirectory;
  46     private final UnixPath rootDirectory;
  47 
  48     // package-private
  49     UnixFileSystem(UnixFileSystemProvider provider, String dir) {
  50         this.provider = provider;
  51         this.defaultDirectory = Util.toBytes(UnixPath.normalizeAndCheck(dir));
  52         if (this.defaultDirectory[0] != '/') {
  53             throw new RuntimeException("default directory must be absolute");
  54         }
  55 
  56         // if process-wide chdir is allowed or default directory is not the
  57         // process working directory then paths must be resolved against the
  58         // default directory.
  59         String propValue = GetPropertyAction
  60                 .getProperty("sun.nio.fs.chdirAllowed", "false");
  61         boolean chdirAllowed = (propValue.length() == 0) ?
  62             true : Boolean.valueOf(propValue);
  63         if (chdirAllowed) {
  64             this.needToResolveAgainstDefaultDirectory = true;
  65         } else {
  66             byte[] cwd = UnixNativeDispatcher.getcwd();
  67             boolean defaultIsCwd = (cwd.length == defaultDirectory.length);
  68             if (defaultIsCwd) {
  69                 for (int i=0; i<cwd.length; i++) {
  70                     if (cwd[i] != defaultDirectory[i]) {
  71                         defaultIsCwd = false;
  72                         break;
  73                     }
  74                 }
  75             }
  76             this.needToResolveAgainstDefaultDirectory = !defaultIsCwd;
  77         }
  78 
  79         // the root directory
  80         this.rootDirectory = new UnixPath(this, "/");


< prev index next >