Search in sources :

Example 11 with FileStore

use of java.nio.file.FileStore in project jimfs by google.

the class JimfsUnixLikeFileSystemTest method testFileStore.

@Test
public void testFileStore() throws IOException {
    FileStore fileStore = Iterables.getOnlyElement(fs.getFileStores());
    assertThat(fileStore.name()).isEqualTo("jimfs");
    assertThat(fileStore.type()).isEqualTo("jimfs");
    assertThat(fileStore.isReadOnly()).isFalse();
    // 1 GB
    long totalSpace = 1024 * 1024 * 1024;
    assertThat(fileStore.getTotalSpace()).isEqualTo(totalSpace);
    assertThat(fileStore.getUnallocatedSpace()).isEqualTo(totalSpace);
    assertThat(fileStore.getUsableSpace()).isEqualTo(totalSpace);
    Files.write(fs.getPath("/foo"), new byte[10000]);
    assertThat(fileStore.getTotalSpace()).isEqualTo(totalSpace);
    // We wrote 10000 bytes, but since the file system allocates fixed size blocks, more than 10k
    // bytes may have been allocated. As such, the unallocated space after the write can be at most
    // maxUnallocatedSpace.
    assertThat(fileStore.getUnallocatedSpace() <= totalSpace - 10000).isTrue();
    // Usable space is at most unallocated space. (In this case, it's currently exactly unallocated
    // space, but that's not required.)
    assertThat(fileStore.getUsableSpace() <= fileStore.getUnallocatedSpace()).isTrue();
    Files.delete(fs.getPath("/foo"));
    assertThat(fileStore.getTotalSpace()).isEqualTo(totalSpace);
    assertThat(fileStore.getUnallocatedSpace()).isEqualTo(totalSpace);
    assertThat(fileStore.getUsableSpace()).isEqualTo(totalSpace);
}
Also used : FileStore(java.nio.file.FileStore) Test(org.junit.Test)

Example 12 with FileStore

use of java.nio.file.FileStore in project robovm by robovm.

the class RamDiskTools method setupRamDisk.

/**
     * Checks if a RAM disk is available and prunes it if necessary.
     */
public void setupRamDisk(Config config, File cacheDir, File tmpDir) {
    this.newCacheDir = cacheDir;
    this.newTmpDir = tmpDir;
    if (OS.getDefaultOS() != OS.macosx) {
        return;
    }
    File volume = new File(ROBOVM_RAM_DISK_PATH);
    if (!volume.exists()) {
        try {
            FileStore store = Files.getFileStore(new File(System.getProperty("user.home")).toPath());
            String plist = new Executor(Logger.NULL_LOGGER, "diskutil").args("info", "-plist", store.name()).execCapture();
            NSDictionary dict = (NSDictionary) PropertyListParser.parse(plist.getBytes("UTF-8"));
            NSObject value = dict.objectForKey("SolidState");
            if (value == null || (value instanceof NSNumber && !((NSNumber) value).boolValue())) {
                // @formatter:off
                config.getLogger().warn("RoboVM has detected that you are running on a slow HDD. Please consider mounting a RAM disk.\n" + "To create a 2GB RAM disk, run this in your terminal:\n" + "SIZE=2048 ; diskutil erasevolume HFS+ 'RoboVM RAM Disk' `hdiutil attach -nomount ram://$((SIZE * 2048))`\n" + "See http://docs.robovm.com/ for more info");
            // @formatter:on
            }
        } catch (Throwable t) {
            // nothing to do here, can't decide if we are on a SSD or HDD
            t.printStackTrace();
        }
        return;
    }
    try {
        FileStore store = Files.getFileStore(volume.toPath());
        if (store.getUsableSpace() < MIN_FREE_SPACE) {
            cleanRamDisk(store, volume, config);
            if (store.getUsableSpace() < MIN_FREE_SPACE) {
                config.getLogger().info("Couldn't free enough space on RAM disk, using hard drive");
                return;
            }
        }
        File newCacheDir = new File(volume, "cache");
        if (!newCacheDir.exists() && !newCacheDir.mkdirs()) {
            config.getLogger().info("Couldn't create cache directory on RAM disk, using hard drive");
            return;
        }
        File newTmpDir = new File(volume, "tmp");
        if (!newTmpDir.exists() && !newTmpDir.mkdirs()) {
            config.getLogger().info("Couldn't create tmp directory on RAM disk, using hard drive");
            return;
        }
        newTmpDir = new File(newTmpDir, tmpDir.getAbsolutePath());
        config.getLogger().info("Using RAM disk at %s for cache and tmp directory", ROBOVM_RAM_DISK_PATH);
        this.newCacheDir = newCacheDir;
        this.newTmpDir = newTmpDir;
    } catch (Throwable t) {
        config.getLogger().error("Couldn't setup RAM disk, using hard drive, %s", t.getMessage());
        this.newCacheDir = cacheDir;
        this.newTmpDir = tmpDir;
    }
}
Also used : NSNumber(com.dd.plist.NSNumber) FileStore(java.nio.file.FileStore) NSObject(com.dd.plist.NSObject) Executor(org.robovm.compiler.util.Executor) NSDictionary(com.dd.plist.NSDictionary) File(java.io.File)

Example 13 with FileStore

use of java.nio.file.FileStore in project lucene-solr by apache.

the class IOUtils method getFileStore.

// Files.getFileStore(Path) useless here!
// don't complain, just try it yourself
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 {
                // 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;
    }
}
Also used : FileStore(java.nio.file.FileStore)

Example 14 with FileStore

use of java.nio.file.FileStore in project lucene-solr by apache.

the class TestIOUtils method testSymlinkSSD.

public void testSymlinkSSD() throws Exception {
    assumeFalse("windows is not supported", Constants.WINDOWS);
    Path dir = createTempDir();
    dir = FilterPath.unwrap(dir).toRealPath();
    // fake SSD with a symlink mount (Ubuntu-like):
    Random rnd = random();
    String partitionUUID = new UUID(rnd.nextLong(), rnd.nextLong()).toString();
    FileStore root = new MockFileStore(dir.toString() + " (/dev/disk/by-uuid/" + partitionUUID + ")", "btrfs", "/dev/disk/by-uuid/" + partitionUUID);
    // make a fake /dev/sda1 for it
    Path devdir = dir.resolve("dev");
    Files.createDirectories(devdir);
    Path deviceFile = devdir.resolve("sda1");
    Files.createFile(deviceFile);
    // create a symlink to the above device file
    Path symlinkdir = devdir.resolve("disk").resolve("by-uuid");
    Files.createDirectories(symlinkdir);
    try {
        Files.createSymbolicLink(symlinkdir.resolve(partitionUUID), deviceFile);
    } catch (UnsupportedOperationException | IOException e) {
        assumeNoException("test requires filesystem that supports symbolic links", e);
    }
    // make a fake /sys/block/sda/queue/rotational file for it
    Path sysdir = dir.resolve("sys").resolve("block").resolve("sda").resolve("queue");
    Files.createDirectories(sysdir);
    try (OutputStream o = Files.newOutputStream(sysdir.resolve("rotational"))) {
        o.write("0\n".getBytes(StandardCharsets.US_ASCII));
    }
    Map<String, FileStore> mappings = Collections.singletonMap(dir.toString(), root);
    FileSystem mockLinux = new MockLinuxFileSystemProvider(dir.getFileSystem(), mappings, dir).getFileSystem(null);
    Path mockPath = mockLinux.getPath(dir.toString());
    assertFalse(IOUtils.spinsLinux(mockPath));
}
Also used : FilterPath(org.apache.lucene.mockfile.FilterPath) Path(java.nio.file.Path) OutputStream(java.io.OutputStream) IOException(java.io.IOException) FileStore(java.nio.file.FileStore) Random(java.util.Random) FileSystem(java.nio.file.FileSystem) FilterFileSystem(org.apache.lucene.mockfile.FilterFileSystem) UUID(java.util.UUID)

Example 15 with FileStore

use of java.nio.file.FileStore in project lucene-solr by apache.

the class TestIOUtils method testManyPartitions.

public void testManyPartitions() throws Exception {
    assumeFalse("windows is not supported", Constants.WINDOWS);
    Path dir = createTempDir();
    dir = FilterPath.unwrap(dir).toRealPath();
    // fake ssd
    FileStore root = new MockFileStore(dir.toString() + " (/dev/zzz12)", "zfs", "/dev/zzz12");
    // make a fake /dev/zzz11 for it
    Path devdir = dir.resolve("dev");
    Files.createDirectories(devdir);
    Files.createFile(devdir.resolve("zzz12"));
    // make a fake /sys/block/zzz/queue/rotational file for it
    Path sysdir = dir.resolve("sys").resolve("block").resolve("zzz").resolve("queue");
    Files.createDirectories(sysdir);
    try (OutputStream o = Files.newOutputStream(sysdir.resolve("rotational"))) {
        o.write("0\n".getBytes(StandardCharsets.US_ASCII));
    }
    Map<String, FileStore> mappings = Collections.singletonMap(dir.toString(), root);
    FileSystem mockLinux = new MockLinuxFileSystemProvider(dir.getFileSystem(), mappings, dir).getFileSystem(null);
    Path mockPath = mockLinux.getPath(dir.toString());
    assertFalse(IOUtils.spinsLinux(mockPath));
}
Also used : FilterPath(org.apache.lucene.mockfile.FilterPath) Path(java.nio.file.Path) FileStore(java.nio.file.FileStore) OutputStream(java.io.OutputStream) FileSystem(java.nio.file.FileSystem) FilterFileSystem(org.apache.lucene.mockfile.FilterFileSystem)

Aggregations

FileStore (java.nio.file.FileStore)17 Path (java.nio.file.Path)11 FileSystem (java.nio.file.FileSystem)8 FilterFileSystem (org.apache.lucene.mockfile.FilterFileSystem)8 FilterPath (org.apache.lucene.mockfile.FilterPath)8 OutputStream (java.io.OutputStream)5 IOException (java.io.IOException)4 FileSystemException (java.nio.file.FileSystemException)2 SuppressForbidden (org.elasticsearch.common.SuppressForbidden)2 NSDictionary (com.dd.plist.NSDictionary)1 NSNumber (com.dd.plist.NSNumber)1 NSObject (com.dd.plist.NSObject)1 FileSystemException (io.vertx.core.file.FileSystemException)1 File (java.io.File)1 InputStream (java.io.InputStream)1 BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 UUID (java.util.UUID)1 NodeEnvironment (org.elasticsearch.env.NodeEnvironment)1