Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/nio/fs/UnixFileStore.java
32288 views
/*1* Copyright (c) 2008, 2015, 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.channels.*;30import java.util.*;31import java.io.IOException;32import java.security.AccessController;33import java.security.PrivilegedAction;3435/**36* Base implementation of FileStore for Unix/like implementations.37*/3839abstract class UnixFileStore40extends FileStore41{42// original path of file that identified file system43private final UnixPath file;4445// device ID46private final long dev;4748// entry in the mount tab49private final UnixMountEntry entry;5051// return the device ID where the given file resides52private static long devFor(UnixPath file) throws IOException {53try {54return UnixFileAttributes.get(file, true).dev();55} catch (UnixException x) {56x.rethrowAsIOException(file);57return 0L; // keep compiler happy58}59}6061UnixFileStore(UnixPath file) throws IOException {62this.file = file;63this.dev = devFor(file);64this.entry = findMountEntry();65}6667UnixFileStore(UnixFileSystem fs, UnixMountEntry entry) throws IOException {68this.file = new UnixPath(fs, entry.dir());69this.dev = (entry.dev() == 0L) ? devFor(this.file) : entry.dev();70this.entry = entry;71}7273/**74* Find the mount entry for the file store75*/76abstract UnixMountEntry findMountEntry() throws IOException;7778UnixPath file() {79return file;80}8182long dev() {83return dev;84}8586UnixMountEntry entry() {87return entry;88}8990@Override91public String name() {92return entry.name();93}9495@Override96public String type() {97return entry.fstype();98}99100@Override101public boolean isReadOnly() {102return entry.isReadOnly();103}104105// uses statvfs to read the file system information106private UnixFileStoreAttributes readAttributes() throws IOException {107try {108return UnixFileStoreAttributes.get(file);109} catch (UnixException x) {110x.rethrowAsIOException(file);111return null; // keep compile happy112}113}114115@Override116public long getTotalSpace() throws IOException {117UnixFileStoreAttributes attrs = readAttributes();118return attrs.blockSize() * attrs.totalBlocks();119}120121@Override122public long getUsableSpace() throws IOException {123UnixFileStoreAttributes attrs = readAttributes();124return attrs.blockSize() * attrs.availableBlocks();125}126127@Override128public long getUnallocatedSpace() throws IOException {129UnixFileStoreAttributes attrs = readAttributes();130return attrs.blockSize() * attrs.freeBlocks();131}132133@Override134public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> view)135{136if (view == null)137throw new NullPointerException();138return (V) null;139}140141@Override142public Object getAttribute(String attribute) throws IOException {143if (attribute.equals("totalSpace"))144return getTotalSpace();145if (attribute.equals("usableSpace"))146return getUsableSpace();147if (attribute.equals("unallocatedSpace"))148return getUnallocatedSpace();149throw new UnsupportedOperationException("'" + attribute + "' not recognized");150}151152@Override153public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {154if (type == null)155throw new NullPointerException();156if (type == BasicFileAttributeView.class)157return true;158if (type == PosixFileAttributeView.class ||159type == FileOwnerAttributeView.class)160{161// lookup fstypes.properties162FeatureStatus status = checkIfFeaturePresent("posix");163// assume supported if UNKNOWN164return (status != FeatureStatus.NOT_PRESENT);165}166return false;167}168169@Override170public boolean supportsFileAttributeView(String name) {171if (name.equals("basic") || name.equals("unix"))172return true;173if (name.equals("posix"))174return supportsFileAttributeView(PosixFileAttributeView.class);175if (name.equals("owner"))176return supportsFileAttributeView(FileOwnerAttributeView.class);177return false;178}179180@Override181public boolean equals(Object ob) {182if (ob == this)183return true;184if (!(ob instanceof UnixFileStore))185return false;186UnixFileStore other = (UnixFileStore)ob;187return (this.dev == other.dev) &&188Arrays.equals(this.entry.dir(), other.entry.dir()) &&189this.entry.name().equals(other.entry.name());190}191192@Override193public int hashCode() {194return (int)(dev ^ (dev >>> 32)) ^ Arrays.hashCode(entry.dir());195}196197@Override198public String toString() {199StringBuilder sb = new StringBuilder(Util.toString(entry.dir()));200sb.append(" (");201sb.append(entry.name());202sb.append(")");203return sb.toString();204}205206// -- fstypes.properties --207208private static final Object loadLock = new Object();209private static volatile Properties props;210211enum FeatureStatus {212PRESENT,213NOT_PRESENT,214UNKNOWN;215}216217/**218* Returns status to indicate if file system supports a given feature219*/220FeatureStatus checkIfFeaturePresent(String feature) {221if (props == null) {222synchronized (loadLock) {223if (props == null) {224props = AccessController.doPrivileged(225new PrivilegedAction<Properties>() {226@Override227public Properties run() {228return loadProperties();229}});230}231}232}233234String value = props.getProperty(type());235if (value != null) {236String[] values = value.split("\\s");237for (String s: values) {238s = s.trim().toLowerCase();239if (s.equals(feature)) {240return FeatureStatus.PRESENT;241}242if (s.startsWith("no")) {243s = s.substring(2);244if (s.equals(feature)) {245return FeatureStatus.NOT_PRESENT;246}247}248}249}250return FeatureStatus.UNKNOWN;251}252253private static Properties loadProperties() {254Properties result = new Properties();255String fstypes = System.getProperty("java.home") + "/lib/fstypes.properties";256Path file = Paths.get(fstypes);257try {258try (ReadableByteChannel rbc = Files.newByteChannel(file)) {259result.load(Channels.newReader(rbc, "UTF-8"));260}261} catch (IOException x) {262}263return result;264}265}266267268