Java FileStore spinsLinux(Path path)

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

Description

spins Linux

License

Apache License

Declaration

static boolean spinsLinux(Path path) throws IOException 

Method Source Code

//package com.java2s;
/*//w  w  w .  ja v a2  s  .  c o  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.io.InputStream;

import java.nio.file.DirectoryStream;
import java.nio.file.FileStore;

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

public class Main {
    static boolean spinsLinux(Path path) throws IOException {
        FileStore store = getFileStore(path);

        // if fs type is tmpfs, it doesn't spin.
        // this won't have a corresponding block device
        if ("tmpfs".equals(store.type())) {
            return false;
        }

        // get block device name
        String devName = store.name();

        // not a device (e.g. NFS server)
        if (!devName.startsWith("/")) {
            return true;
        }

        // resolve any symlinks to real block device (e.g. LVM)
        // /dev/sda0 -> sda0
        // /devices/XXX -> sda0
        devName = path.getRoot().resolve(devName).toRealPath().getFileName().toString();

        // now try to find the longest matching device folder in /sys/block
        // (that starts with our dev name):
        Path sysinfo = path.getRoot().resolve("sys").resolve("block");
        Path devsysinfo = null;
        int matchlen = 0;
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(sysinfo)) {
            for (Path device : stream) {
                String name = device.getFileName().toString();
                if (name.length() > matchlen && devName.startsWith(name)) {
                    devsysinfo = device;
                    matchlen = name.length();
                }
            }
        }

        if (devsysinfo == null) {
            return true; // give up
        }

        // read first byte from rotational, it's a 1 if it spins.
        Path rotational = devsysinfo.resolve("queue").resolve("rotational");
        try (InputStream stream = Files.newInputStream(rotational)) {
            return stream.read() == '1';
        }
    }

    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. getPosixAttributes(Path file)
  2. getStorageName(final File file)
  3. getTopResourceInVolume(URI location)
  4. isPosixFileStore(Path path)
  5. isPosixFileSystem()