Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/nio/fs/UnixFileSystem.java
32288 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.fs;2627import java.nio.file.*;28import java.nio.file.attribute.*;29import java.nio.file.spi.*;30import java.io.IOException;31import java.util.*;32import java.util.regex.Pattern;33import java.security.AccessController;34import sun.security.action.GetPropertyAction;3536/**37* Base implementation of FileSystem for Unix-like implementations.38*/3940abstract class UnixFileSystem41extends FileSystem42{43private final UnixFileSystemProvider provider;44private final byte[] defaultDirectory;45private final boolean needToResolveAgainstDefaultDirectory;46private final UnixPath rootDirectory;4748// package-private49UnixFileSystem(UnixFileSystemProvider provider, String dir) {50this.provider = provider;51this.defaultDirectory = Util.toBytes(UnixPath.normalizeAndCheck(dir));52if (this.defaultDirectory[0] != '/') {53throw new RuntimeException("default directory must be absolute");54}5556// if process-wide chdir is allowed or default directory is not the57// process working directory then paths must be resolved against the58// default directory.59String propValue = AccessController.doPrivileged(60new GetPropertyAction("sun.nio.fs.chdirAllowed", "false"));61boolean chdirAllowed = (propValue.length() == 0) ?62true : Boolean.valueOf(propValue);63if (chdirAllowed) {64this.needToResolveAgainstDefaultDirectory = true;65} else {66byte[] cwd = UnixNativeDispatcher.getcwd();67boolean defaultIsCwd = (cwd.length == defaultDirectory.length);68if (defaultIsCwd) {69for (int i=0; i<cwd.length; i++) {70if (cwd[i] != defaultDirectory[i]) {71defaultIsCwd = false;72break;73}74}75}76this.needToResolveAgainstDefaultDirectory = !defaultIsCwd;77}7879// the root directory80this.rootDirectory = new UnixPath(this, "/");81}8283// package-private84byte[] defaultDirectory() {85return defaultDirectory;86}8788boolean needToResolveAgainstDefaultDirectory() {89return needToResolveAgainstDefaultDirectory;90}9192UnixPath rootDirectory() {93return rootDirectory;94}9596boolean isSolaris() {97return false;98}99100static List<String> standardFileAttributeViews() {101return Arrays.asList("basic", "posix", "unix", "owner");102}103104@Override105public final FileSystemProvider provider() {106return provider;107}108109@Override110public final String getSeparator() {111return "/";112}113114@Override115public final boolean isOpen() {116return true;117}118119@Override120public final boolean isReadOnly() {121return false;122}123124@Override125public final void close() throws IOException {126throw new UnsupportedOperationException();127}128129/**130* Copies non-POSIX attributes from the source to target file.131*132* Copying a file preserving attributes, or moving a file, will preserve133* the file owner/group/permissions/timestamps but it does not preserve134* other non-POSIX attributes. This method is invoked by the135* copy or move operation to preserve these attributes. It should copy136* extended attributes, ACLs, or other attributes.137*138* @param sfd139* Open file descriptor to source file140* @param tfd141* Open file descriptor to target file142*/143void copyNonPosixAttributes(int sfd, int tfd) {144// no-op by default145}146147/**148* Unix systems only have a single root directory (/)149*/150@Override151public final Iterable<Path> getRootDirectories() {152final List<Path> allowedList =153Collections.unmodifiableList(Arrays.asList((Path)rootDirectory));154return new Iterable<Path>() {155public Iterator<Path> iterator() {156try {157SecurityManager sm = System.getSecurityManager();158if (sm != null)159sm.checkRead(rootDirectory.toString());160return allowedList.iterator();161} catch (SecurityException x) {162List<Path> disallowed = Collections.emptyList();163return disallowed.iterator();164}165}166};167}168169/**170* Returns object to iterate over entries in mounttab or equivalent171*/172abstract Iterable<UnixMountEntry> getMountEntries();173174/**175* Returns a FileStore to represent the file system for the given mount176* mount.177*/178abstract FileStore getFileStore(UnixMountEntry entry) throws IOException;179180/**181* Iterator returned by getFileStores method.182*/183private class FileStoreIterator implements Iterator<FileStore> {184private final Iterator<UnixMountEntry> entries;185private FileStore next;186187FileStoreIterator() {188this.entries = getMountEntries().iterator();189}190191private FileStore readNext() {192assert Thread.holdsLock(this);193for (;;) {194if (!entries.hasNext())195return null;196UnixMountEntry entry = entries.next();197198// skip entries with the "ignore" option199if (entry.isIgnored())200continue;201202// check permission to read mount point203SecurityManager sm = System.getSecurityManager();204if (sm != null) {205try {206sm.checkRead(Util.toString(entry.dir()));207} catch (SecurityException x) {208continue;209}210}211try {212return getFileStore(entry);213} catch (IOException ignore) {214// ignore as per spec215}216}217}218219@Override220public synchronized boolean hasNext() {221if (next != null)222return true;223next = readNext();224return next != null;225}226227@Override228public synchronized FileStore next() {229if (next == null)230next = readNext();231if (next == null) {232throw new NoSuchElementException();233} else {234FileStore result = next;235next = null;236return result;237}238}239240@Override241public void remove() {242throw new UnsupportedOperationException();243}244}245246@Override247public final Iterable<FileStore> getFileStores() {248SecurityManager sm = System.getSecurityManager();249if (sm != null) {250try {251sm.checkPermission(new RuntimePermission("getFileStoreAttributes"));252} catch (SecurityException se) {253return Collections.emptyList();254}255}256return new Iterable<FileStore>() {257public Iterator<FileStore> iterator() {258return new FileStoreIterator();259}260};261}262263@Override264public final Path getPath(String first, String... more) {265String path;266if (more.length == 0) {267path = first;268} else {269StringBuilder sb = new StringBuilder();270sb.append(first);271for (String segment: more) {272if (segment.length() > 0) {273if (sb.length() > 0)274sb.append('/');275sb.append(segment);276}277}278path = sb.toString();279}280return new UnixPath(this, path);281}282283@Override284public PathMatcher getPathMatcher(String syntaxAndInput) {285int pos = syntaxAndInput.indexOf(':');286if (pos <= 0 || pos == syntaxAndInput.length())287throw new IllegalArgumentException();288String syntax = syntaxAndInput.substring(0, pos);289String input = syntaxAndInput.substring(pos+1);290291String expr;292if (syntax.equals(GLOB_SYNTAX)) {293expr = Globs.toUnixRegexPattern(input);294} else {295if (syntax.equals(REGEX_SYNTAX)) {296expr = input;297} else {298throw new UnsupportedOperationException("Syntax '" + syntax +299"' not recognized");300}301}302303// return matcher304final Pattern pattern = compilePathMatchPattern(expr);305306return new PathMatcher() {307@Override308public boolean matches(Path path) {309return pattern.matcher(path.toString()).matches();310}311};312}313314private static final String GLOB_SYNTAX = "glob";315private static final String REGEX_SYNTAX = "regex";316317@Override318public final UserPrincipalLookupService getUserPrincipalLookupService() {319return LookupService.instance;320}321322private static class LookupService {323static final UserPrincipalLookupService instance =324new UserPrincipalLookupService() {325@Override326public UserPrincipal lookupPrincipalByName(String name)327throws IOException328{329return UnixUserPrincipals.lookupUser(name);330}331332@Override333public GroupPrincipal lookupPrincipalByGroupName(String group)334throws IOException335{336return UnixUserPrincipals.lookupGroup(group);337}338};339}340341// Override if the platform has different path match requrement, such as342// case insensitive or Unicode canonical equal on MacOSX343Pattern compilePathMatchPattern(String expr) {344return Pattern.compile(expr);345}346347// Override if the platform uses different Unicode normalization form348// for native file path. For example on MacOSX, the native path is stored349// in Unicode NFD form.350char[] normalizeNativePath(char[] path) {351return path;352}353354// Override if the native file path use non-NFC form. For example on MacOSX,355// the native path is stored in Unicode NFD form, the path need to be356// normalized back to NFC before passed back to Java level.357String normalizeJavaPath(String path) {358return path;359}360}361362363