Java FileStore getFileStore(Path path)

Here you can find the source of getFileStore(Path path)

Description

get File Store

License

Apache License

Declaration

static FileStore getFileStore(Path path) throws IOException 

Method Source Code

//package com.java2s;
/*/*from  www  . j  ava 2s.co  m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;

import java.nio.file.FileStore;

import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    static FileStore getFileStore(Path path) throws IOException {
        FileStore store = Files.getFileStore(path);
        String mount = getMountPoint(store);

        // find the "matching" FileStore from system list, it's the one we want, but only return
        // that if it's unambiguous (only one matching):
        FileStore sameMountPoint = null;
        for (FileStore fs : path.getFileSystem().getFileStores()) {
            if (mount.equals(getMountPoint(fs))) {
                if (sameMountPoint == null) {
                    sameMountPoint = fs;
                } else {
                    // more than one filesystem has the same mount point; something is wrong!
                    // fall back to crappy one we got from Files.getFileStore
                    return store;
                }
            }
        }

        if (sameMountPoint != null) {
            // ok, we found only one, use it:
            return sameMountPoint;
        } else {
            // fall back to crappy one we got from Files.getFileStore
            return store;
        }
    }

    static String getMountPoint(FileStore store) {
        String desc = store.toString();
        int index = desc.lastIndexOf(" (");
        if (index != -1) {
            return desc.substring(0, index);
        } else {
            return desc;
        }
    }
}

Related

  1. createTempFileWithAttributes(Path tempFilePath, File fileName)
  2. diskFree()
  3. ensureExists(Path folder)
  4. findTopResourceInVolume(FileStore fstore, Path path)
  5. getAclAttributes(Path file)
  6. getFileStore(URI uri)
  7. getMountPoint(FileStore store)
  8. getPosixAttributes(Path file)
  9. getStorageName(final File file)