Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/nio/fs/WindowsFileSystem.java
32288 views
/*1* Copyright (c) 2008, 2011, 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.util.*;31import java.util.regex.Pattern;32import java.io.IOException;33import java.security.AccessController;34import java.security.PrivilegedAction;35import sun.security.action.GetPropertyAction;3637class WindowsFileSystem38extends FileSystem39{40private final WindowsFileSystemProvider provider;4142// default directory (is absolute), and default root43private final String defaultDirectory;44private final String defaultRoot;4546private final boolean supportsLinks;47private final boolean supportsStreamEnumeration;4849// package-private50WindowsFileSystem(WindowsFileSystemProvider provider,51String dir)52{53this.provider = provider;5455// parse default directory and check it is absolute56WindowsPathParser.Result result = WindowsPathParser.parse(dir);5758if ((result.type() != WindowsPathType.ABSOLUTE) &&59(result.type() != WindowsPathType.UNC))60throw new AssertionError("Default directory is not an absolute path");61this.defaultDirectory = result.path();62this.defaultRoot = result.root();6364PrivilegedAction<String> pa = new GetPropertyAction("os.version");65String osversion = AccessController.doPrivileged(pa);66String[] vers = Util.split(osversion, '.');67int major = Integer.parseInt(vers[0]);68int minor = Integer.parseInt(vers[1]);6970// symbolic links available on Vista and newer71supportsLinks = (major >= 6);7273// enumeration of data streams available on Windows Server 2003 and newer74supportsStreamEnumeration = (major >= 6) || (major == 5 && minor >= 2);75}7677// package-private78String defaultDirectory() {79return defaultDirectory;80}8182String defaultRoot() {83return defaultRoot;84}8586boolean supportsLinks() {87return supportsLinks;88}8990boolean supportsStreamEnumeration() {91return supportsStreamEnumeration;92}9394@Override95public FileSystemProvider provider() {96return provider;97}9899@Override100public String getSeparator() {101return "\\";102}103104@Override105public boolean isOpen() {106return true;107}108109@Override110public boolean isReadOnly() {111return false;112}113114@Override115public void close() throws IOException {116throw new UnsupportedOperationException();117}118119@Override120public Iterable<Path> getRootDirectories() {121int drives = 0;122try {123drives = WindowsNativeDispatcher.GetLogicalDrives();124} catch (WindowsException x) {125// shouldn't happen126throw new AssertionError(x.getMessage());127}128129// iterate over roots, ignoring those that the security manager denies130ArrayList<Path> result = new ArrayList<>();131SecurityManager sm = System.getSecurityManager();132for (int i = 0; i <= 25; i++) { // 0->A, 1->B, 2->C...133if ((drives & (1 << i)) != 0) {134StringBuilder sb = new StringBuilder(3);135sb.append((char)('A' + i));136sb.append(":\\");137String root = sb.toString();138if (sm != null) {139try {140sm.checkRead(root);141} catch (SecurityException x) {142continue;143}144}145result.add(WindowsPath.createFromNormalizedPath(this, root));146}147}148return Collections.unmodifiableList(result);149}150151/**152* Iterator returned by getFileStores method.153*/154private class FileStoreIterator implements Iterator<FileStore> {155private final Iterator<Path> roots;156private FileStore next;157158FileStoreIterator() {159this.roots = getRootDirectories().iterator();160}161162private FileStore readNext() {163assert Thread.holdsLock(this);164for (;;) {165if (!roots.hasNext())166return null;167WindowsPath root = (WindowsPath)roots.next();168// ignore if security manager denies access169try {170root.checkRead();171} catch (SecurityException x) {172continue;173}174try {175FileStore fs = WindowsFileStore.create(root.toString(), true);176if (fs != null)177return fs;178} catch (IOException ioe) {179// skip it180}181}182}183184@Override185public synchronized boolean hasNext() {186if (next != null)187return true;188next = readNext();189return next != null;190}191192@Override193public synchronized FileStore next() {194if (next == null)195next = readNext();196if (next == null) {197throw new NoSuchElementException();198} else {199FileStore result = next;200next = null;201return result;202}203}204205@Override206public void remove() {207throw new UnsupportedOperationException();208}209}210211@Override212public Iterable<FileStore> getFileStores() {213SecurityManager sm = System.getSecurityManager();214if (sm != null) {215try {216sm.checkPermission(new RuntimePermission("getFileStoreAttributes"));217} catch (SecurityException se) {218return Collections.emptyList();219}220}221return new Iterable<FileStore>() {222public Iterator<FileStore> iterator() {223return new FileStoreIterator();224}225};226}227228// supported views229private static final Set<String> supportedFileAttributeViews = Collections230.unmodifiableSet(new HashSet<String>(Arrays.asList("basic", "dos", "acl", "owner", "user")));231232@Override233public Set<String> supportedFileAttributeViews() {234return supportedFileAttributeViews;235}236237@Override238public final Path getPath(String first, String... more) {239String path;240if (more.length == 0) {241path = first;242} else {243StringBuilder sb = new StringBuilder();244sb.append(first);245for (String segment: more) {246if (segment.length() > 0) {247if (sb.length() > 0)248sb.append('\\');249sb.append(segment);250}251}252path = sb.toString();253}254return WindowsPath.parse(this, path);255}256257@Override258public UserPrincipalLookupService getUserPrincipalLookupService() {259return LookupService.instance;260}261262private static class LookupService {263static final UserPrincipalLookupService instance =264new UserPrincipalLookupService() {265@Override266public UserPrincipal lookupPrincipalByName(String name)267throws IOException268{269return WindowsUserPrincipals.lookup(name);270}271@Override272public GroupPrincipal lookupPrincipalByGroupName(String group)273throws IOException274{275UserPrincipal user = WindowsUserPrincipals.lookup(group);276if (!(user instanceof GroupPrincipal))277throw new UserPrincipalNotFoundException(group);278return (GroupPrincipal)user;279}280};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.toWindowsRegexPattern(input);294} else {295if (syntax.equals(REGEX_SYNTAX)) {296expr = input;297} else {298throw new UnsupportedOperationException("Syntax '" + syntax +299"' not recognized");300}301}302303// match in unicode_case_insensitive304final Pattern pattern = Pattern.compile(expr,305Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);306307// return matcher308return new PathMatcher() {309@Override310public boolean matches(Path path) {311return pattern.matcher(path.toString()).matches();312}313};314}315private static final String GLOB_SYNTAX = "glob";316private static final String REGEX_SYNTAX = "regex";317318@Override319public WatchService newWatchService()320throws IOException321{322return new WindowsWatchService(this);323}324}325326327