UnixFileStore.java 8.9 KB
Newer Older
1
/*
2
 * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
 */

package sun.nio.fs;

import java.nio.file.*;
import java.nio.file.attribute.*;
import java.nio.channels.*;
import java.util.*;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;

/**
 * Base implementation of FileStore for Unix/like implementations.
 */

abstract class UnixFileStore
    extends FileStore
{
    // original path of file that identified file system
    private final UnixPath file;

    // device ID
    private final long dev;

    // entry in the mount tab
    private final UnixMountEntry entry;

    // return the device ID where the given file resides
    private static long devFor(UnixPath file) throws IOException {
        try {
            return UnixFileAttributes.get(file, true).dev();
        } catch (UnixException x) {
            x.rethrowAsIOException(file);
            return 0L;  // keep compiler happy
        }
    }

    UnixFileStore(UnixPath file) throws IOException {
        this.file = file;
        this.dev = devFor(file);
        this.entry = findMountEntry();
    }

    UnixFileStore(UnixFileSystem fs, UnixMountEntry entry) throws IOException {
        this.file = new UnixPath(fs, entry.dir());
        this.dev = (entry.dev() == 0L) ? devFor(this.file) : entry.dev();
        this.entry = entry;
    }

    /**
     * Find the mount entry for the file store
     */
    abstract UnixMountEntry findMountEntry() throws IOException;

    UnixPath file() {
        return file;
    }

    long dev() {
        return dev;
    }

    UnixMountEntry entry() {
        return entry;
    }

    @Override
    public String name() {
        return entry.name();
    }

    @Override
    public String type() {
        return entry.fstype();
    }

    @Override
    public boolean isReadOnly() {
        return entry.isReadOnly();
    }

    @Override
    @SuppressWarnings("unchecked")
108
    public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> view)
109
    {
110 111 112
        if (view == null)
            throw new NullPointerException();
        if (view == FileStoreSpaceAttributeView.class)
113 114 115 116 117
            return (V) new UnixFileStoreSpaceAttributeView(this);
        return (V) null;
    }

    @Override
118 119 120 121 122 123 124 125 126 127 128
    public Object getAttribute(String attribute) throws IOException {
        if (attribute.equals("space:totalSpace"))
            return new UnixFileStoreSpaceAttributeView(this)
                .readAttributes().totalSpace();
        if (attribute.equals("space:usableSpace"))
            return new UnixFileStoreSpaceAttributeView(this)
                 .readAttributes().usableSpace();
        if (attribute.equals("space:unallocatedSpace"))
            return new UnixFileStoreSpaceAttributeView(this)
                 .readAttributes().unallocatedSpace();
        throw new UnsupportedOperationException("'" + attribute + "' not recognized");
129 130 131 132
    }

    @Override
    public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {
133 134
        if (type == null)
            throw new NullPointerException();
135 136 137 138 139 140 141
        if (type == BasicFileAttributeView.class)
            return true;
        if (type == PosixFileAttributeView.class ||
            type == FileOwnerAttributeView.class)
        {
            // lookup fstypes.properties
            FeatureStatus status = checkIfFeaturePresent("posix");
142 143
            // assume supported if UNKNOWN
            return (status != FeatureStatus.NOT_PRESENT);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        }
        return false;
    }

    @Override
    public boolean supportsFileAttributeView(String name) {
        if (name.equals("basic") || name.equals("unix"))
            return true;
        if (name.equals("posix"))
            return supportsFileAttributeView(PosixFileAttributeView.class);
        if (name.equals("owner"))
            return supportsFileAttributeView(FileOwnerAttributeView.class);
        return false;
    }

    @Override
    public boolean equals(Object ob) {
        if (ob == this)
            return true;
        if (!(ob instanceof UnixFileStore))
            return false;
        UnixFileStore other = (UnixFileStore)ob;
166 167
        return (this.dev == other.dev) &&
               Arrays.equals(this.entry.dir(), other.entry.dir());
168 169 170 171
    }

    @Override
    public int hashCode() {
172
        return (int)(dev ^ (dev >>> 32)) ^ Arrays.hashCode(entry.dir());
173 174 175 176 177 178 179 180 181 182 183 184
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(new String(entry.dir()));
        sb.append(" (");
        sb.append(entry.name());
        sb.append(")");
        return sb.toString();
    }

    private static class UnixFileStoreSpaceAttributeView
185
        implements FileStoreSpaceAttributeView
186 187 188 189 190 191 192
    {
        private final UnixFileStore fs;

        UnixFileStoreSpaceAttributeView(UnixFileStore fs) {
            this.fs = fs;
        }

193 194 195 196 197
        @Override
        public String name() {
            return "space";
        }

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
        @Override
        public FileStoreSpaceAttributes readAttributes()
            throws IOException
        {
            UnixPath file = fs.file();
            final UnixFileStoreAttributes attrs;
            try {
                attrs = UnixFileStoreAttributes.get(file);
            } catch (UnixException x) {
                x.rethrowAsIOException(file);
                return null;    // keep compile happy
            }

            return new FileStoreSpaceAttributes() {
                @Override
                public long totalSpace() {
                    return attrs.blockSize() * attrs.totalBlocks();
                }
                @Override
                public long usableSpace() {
                    return attrs.blockSize() * attrs.availableBlocks();
                }
                @Override
                public long unallocatedSpace() {
                    return attrs.blockSize() * attrs.freeBlocks();
                }
            };
        }
    }

    // -- fstypes.properties --

    private static final Object loadLock = new Object();
    private static volatile Properties props;

    enum FeatureStatus {
        PRESENT,
        NOT_PRESENT,
        UNKNOWN;
    }

    /**
     * Returns status to indicate if file system supports a given feature
     */
    FeatureStatus checkIfFeaturePresent(String feature) {
        if (props == null) {
            synchronized (loadLock) {
                if (props == null) {
                    props = AccessController.doPrivileged(
                        new PrivilegedAction<Properties>() {
                            @Override
                            public Properties run() {
                                return loadProperties();
                            }});
                }
            }
        }

        String value = props.getProperty(type());
        if (value != null) {
            String[] values = value.split("\\s");
            for (String s: values) {
                s = s.trim().toLowerCase();
                if (s.equals(feature)) {
                    return FeatureStatus.PRESENT;
                }
                if (s.startsWith("no")) {
                    s = s.substring(2);
                    if (s.equals(feature)) {
                        return FeatureStatus.NOT_PRESENT;
                    }
                }
            }
        }
        return FeatureStatus.UNKNOWN;
    }

    private static Properties loadProperties() {
        Properties result = new Properties();
        String fstypes = System.getProperty("java.home") + "/lib/fstypes.properties";
278
        Path file = Paths.get(fstypes);
279 280 281 282 283 284 285 286 287 288 289 290
        try {
            ReadableByteChannel rbc = file.newByteChannel();
            try {
                result.load(Channels.newReader(rbc, "UTF-8"));
            } finally {
                rbc.close();
            }
        } catch (IOException x) {
        }
        return result;
    }
}