893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Apackage sun.nio.fs;
893N/A
893N/Aimport java.nio.file.*;
893N/Aimport java.nio.file.attribute.*;
893N/Aimport java.nio.channels.*;
893N/Aimport java.util.*;
893N/Aimport java.io.IOException;
893N/Aimport java.security.AccessController;
893N/Aimport java.security.PrivilegedAction;
893N/A
893N/A/**
893N/A * Base implementation of FileStore for Unix/like implementations.
893N/A */
893N/A
893N/Aabstract class UnixFileStore
893N/A extends FileStore
893N/A{
893N/A // original path of file that identified file system
893N/A private final UnixPath file;
893N/A
893N/A // device ID
893N/A private final long dev;
893N/A
893N/A // entry in the mount tab
893N/A private final UnixMountEntry entry;
893N/A
893N/A // return the device ID where the given file resides
893N/A private static long devFor(UnixPath file) throws IOException {
893N/A try {
893N/A return UnixFileAttributes.get(file, true).dev();
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(file);
893N/A return 0L; // keep compiler happy
893N/A }
893N/A }
893N/A
893N/A UnixFileStore(UnixPath file) throws IOException {
893N/A this.file = file;
893N/A this.dev = devFor(file);
893N/A this.entry = findMountEntry();
893N/A }
893N/A
893N/A UnixFileStore(UnixFileSystem fs, UnixMountEntry entry) throws IOException {
893N/A this.file = new UnixPath(fs, entry.dir());
893N/A this.dev = (entry.dev() == 0L) ? devFor(this.file) : entry.dev();
893N/A this.entry = entry;
893N/A }
893N/A
893N/A /**
893N/A * Find the mount entry for the file store
893N/A */
893N/A abstract UnixMountEntry findMountEntry() throws IOException;
893N/A
893N/A UnixPath file() {
893N/A return file;
893N/A }
893N/A
893N/A long dev() {
893N/A return dev;
893N/A }
893N/A
893N/A UnixMountEntry entry() {
893N/A return entry;
893N/A }
893N/A
893N/A @Override
893N/A public String name() {
893N/A return entry.name();
893N/A }
893N/A
893N/A @Override
893N/A public String type() {
893N/A return entry.fstype();
893N/A }
893N/A
893N/A @Override
893N/A public boolean isReadOnly() {
893N/A return entry.isReadOnly();
893N/A }
893N/A
3471N/A // uses statvfs to read the file system information
3471N/A private UnixFileStoreAttributes readAttributes() throws IOException {
3471N/A try {
3471N/A return UnixFileStoreAttributes.get(file);
3471N/A } catch (UnixException x) {
3471N/A x.rethrowAsIOException(file);
3471N/A return null; // keep compile happy
3471N/A }
3471N/A }
3471N/A
893N/A @Override
3471N/A public long getTotalSpace() throws IOException {
3471N/A UnixFileStoreAttributes attrs = readAttributes();
3471N/A return attrs.blockSize() * attrs.totalBlocks();
3471N/A }
3471N/A
3471N/A @Override
3471N/A public long getUsableSpace() throws IOException {
3471N/A UnixFileStoreAttributes attrs = readAttributes();
3471N/A return attrs.blockSize() * attrs.availableBlocks();
3471N/A }
3471N/A
3471N/A @Override
3471N/A public long getUnallocatedSpace() throws IOException {
3471N/A UnixFileStoreAttributes attrs = readAttributes();
3471N/A return attrs.blockSize() * attrs.freeBlocks();
3471N/A }
3471N/A
3471N/A @Override
1319N/A public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> view)
893N/A {
1319N/A if (view == null)
1319N/A throw new NullPointerException();
893N/A return (V) null;
893N/A }
893N/A
893N/A @Override
1319N/A public Object getAttribute(String attribute) throws IOException {
3471N/A if (attribute.equals("totalSpace"))
3471N/A return getTotalSpace();
3471N/A if (attribute.equals("usableSpace"))
3471N/A return getUsableSpace();
3471N/A if (attribute.equals("unallocatedSpace"))
3471N/A return getUnallocatedSpace();
1319N/A throw new UnsupportedOperationException("'" + attribute + "' not recognized");
893N/A }
893N/A
893N/A @Override
893N/A public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {
1319N/A if (type == null)
1319N/A throw new NullPointerException();
893N/A if (type == BasicFileAttributeView.class)
893N/A return true;
893N/A if (type == PosixFileAttributeView.class ||
893N/A type == FileOwnerAttributeView.class)
893N/A {
893N/A // lookup fstypes.properties
893N/A FeatureStatus status = checkIfFeaturePresent("posix");
1609N/A // assume supported if UNKNOWN
1609N/A return (status != FeatureStatus.NOT_PRESENT);
893N/A }
893N/A return false;
893N/A }
893N/A
893N/A @Override
893N/A public boolean supportsFileAttributeView(String name) {
893N/A if (name.equals("basic") || name.equals("unix"))
893N/A return true;
893N/A if (name.equals("posix"))
893N/A return supportsFileAttributeView(PosixFileAttributeView.class);
893N/A if (name.equals("owner"))
893N/A return supportsFileAttributeView(FileOwnerAttributeView.class);
893N/A return false;
893N/A }
893N/A
893N/A @Override
893N/A public boolean equals(Object ob) {
893N/A if (ob == this)
893N/A return true;
893N/A if (!(ob instanceof UnixFileStore))
893N/A return false;
893N/A UnixFileStore other = (UnixFileStore)ob;
2824N/A return (this.dev == other.dev) &&
2824N/A Arrays.equals(this.entry.dir(), other.entry.dir());
893N/A }
893N/A
893N/A @Override
893N/A public int hashCode() {
2824N/A return (int)(dev ^ (dev >>> 32)) ^ Arrays.hashCode(entry.dir());
893N/A }
893N/A
893N/A @Override
893N/A public String toString() {
893N/A StringBuilder sb = new StringBuilder(new String(entry.dir()));
893N/A sb.append(" (");
893N/A sb.append(entry.name());
893N/A sb.append(")");
893N/A return sb.toString();
893N/A }
893N/A
893N/A // -- fstypes.properties --
893N/A
893N/A private static final Object loadLock = new Object();
893N/A private static volatile Properties props;
893N/A
893N/A enum FeatureStatus {
893N/A PRESENT,
893N/A NOT_PRESENT,
893N/A UNKNOWN;
893N/A }
893N/A
893N/A /**
893N/A * Returns status to indicate if file system supports a given feature
893N/A */
893N/A FeatureStatus checkIfFeaturePresent(String feature) {
893N/A if (props == null) {
893N/A synchronized (loadLock) {
893N/A if (props == null) {
893N/A props = AccessController.doPrivileged(
893N/A new PrivilegedAction<Properties>() {
893N/A @Override
893N/A public Properties run() {
893N/A return loadProperties();
893N/A }});
893N/A }
893N/A }
893N/A }
893N/A
893N/A String value = props.getProperty(type());
893N/A if (value != null) {
893N/A String[] values = value.split("\\s");
893N/A for (String s: values) {
893N/A s = s.trim().toLowerCase();
893N/A if (s.equals(feature)) {
893N/A return FeatureStatus.PRESENT;
893N/A }
893N/A if (s.startsWith("no")) {
893N/A s = s.substring(2);
893N/A if (s.equals(feature)) {
893N/A return FeatureStatus.NOT_PRESENT;
893N/A }
893N/A }
893N/A }
893N/A }
893N/A return FeatureStatus.UNKNOWN;
893N/A }
893N/A
893N/A private static Properties loadProperties() {
893N/A Properties result = new Properties();
893N/A String fstypes = System.getProperty("java.home") + "/lib/fstypes.properties";
1319N/A Path file = Paths.get(fstypes);
893N/A try {
3471N/A try (ReadableByteChannel rbc = Files.newByteChannel(file)) {
893N/A result.load(Channels.newReader(rbc, "UTF-8"));
893N/A }
893N/A } catch (IOException x) {
893N/A }
893N/A return result;
893N/A }
893N/A}