java.nio.file.FileStore Java Examples

The following examples show how to use java.nio.file.FileStore. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MCRIFSFileSystem.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Iterable<FileStore> getFileStores() {
    return MCRStoreCenter.instance().getCurrentStores(org.mycore.datamodel.ifs2.MCRFileStore.class)
        .filter(s -> s.getID().startsWith(MCRFileSystemUtils.STORE_ID_PREFIX))
        .sorted(Comparator.comparing(MCRStore::getID))
        .map(MCRStore::getID)
        .distinct()
        .map(storeId -> {
            try {
                return MCRFileStore.getInstance(storeId);
            } catch (IOException e) {
                LogManager.getLogger().error("Error while iterating FileStores.", e);
                return null;
            }
        })
        .filter(Objects::nonNull)
        .map(FileStore.class::cast)::iterator;
}
 
Example #2
Source File: TestIOUtils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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));
}
 
Example #3
Source File: InformationController.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
private void fillFromStorage ( final Map<String, Object> model )
{
    if ( this.manager != null )
    {
        final Path base = this.manager.getContext ().getBasePath ();
        try
        {
            final FileStore store = Files.getFileStore ( base );
            model.put ( "storageTotal", store.getTotalSpace () );
            model.put ( "storageFree", store.getUsableSpace () );
            model.put ( "storageUsed", store.getTotalSpace () - store.getUsableSpace () );
            model.put ( "storageName", store.name () );
        }
        catch ( final Exception e )
        {
            logger.warn ( "Failed to check storage space", e );
            // ignore
        }
    }
}
 
Example #4
Source File: TestIOUtils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testSSD() 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/zzz1)", "btrfs", "/dev/zzz1");
  // make a fake /dev/zzz1 for it
  Path devdir = dir.resolve("dev");
  Files.createDirectories(devdir);
  Files.createFile(devdir.resolve("zzz1"));
  // 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));
}
 
Example #5
Source File: FileBlobStore.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean isStorageAvailable() {
  try {
    FileStore fileStore = Files.getFileStore(contentDir);
    long usableSpace = fileStore.getUsableSpace();
    boolean readOnly = fileStore.isReadOnly();
    boolean result = !readOnly && usableSpace > 0;
    if (!result) {
      log.warn("File blob store '{}' is not writable. Read only: {}. Usable space: {}",
          getBlobStoreConfiguration().getName(), readOnly, usableSpace);
    }
    return result;
  }
  catch (IOException e) {
    log.warn("File blob store '{}' is not writable.", getBlobStoreConfiguration().getName(), e);
    return false;
  }
}
 
Example #6
Source File: TestIOUtils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public MockLinuxFileSystemProvider(FileSystem delegateInstance, final Map<String,FileStore> filesToStore, Path root) {
  super("mocklinux://", delegateInstance);
  if (mockedPath(root)) {
    throw new AssumptionViolatedException("can't mock /sys and /dev inside of /sys or /dev!");
  }
  final Collection<FileStore> allStores = new HashSet<>(filesToStore.values());
  this.fileSystem = new FilterFileSystem(this, delegateInstance) {
    @Override
    public Iterable<FileStore> getFileStores() {
      return allStores;
    }

    @Override
    public Path getPath(String first, String... more) {
      return new MockLinuxPath(delegateInstance.getPath(first, more), this);
    }
  };
  this.filesToStore = filesToStore;
  this.root = new MockLinuxPath(root, this.fileSystem);
}
 
Example #7
Source File: AddDVDActionHandler.java    From Quelea with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Get the location of the DVD, or null if no DVD can be found.
 * <p>
 * @return the DVD location.
 */
private String getLocation() {
    FileSystem fs = FileSystems.getDefault();
    for(Path rootPath : fs.getRootDirectories()) {
        try {
            FileStore store = Files.getFileStore(rootPath);
            if(store.type().toLowerCase().contains("udf")) {
                if(store.getTotalSpace()>10000000000L) { //Blu-ray
                    return "bluray:///" + rootPath.toString();
                }
                else {
                    return "dvdsimple:///" + rootPath.toString(); //DVD
                }
            }
        }
        catch(IOException ex) {
            //Never mind
        }
    }
    return null;
}
 
Example #8
Source File: TestFileStore.java    From jsr203-hadoop with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileStore() throws URISyntaxException, IOException {
  URI uri = clusterUri.resolve("/tmp/testFileStore");
  Path path = Paths.get(uri);
  if (Files.exists(path))
    Files.delete(path);
  assertFalse(Files.exists(path));
  Files.createFile(path);
  assertTrue(Files.exists(path));
  FileStore st = Files.getFileStore(path);
  assertNotNull(st);
  Assert.assertNotNull(st.name());
  Assert.assertNotNull(st.type());

  Assert.assertFalse(st.isReadOnly());

  Assert.assertNotEquals(0, st.getTotalSpace());
  Assert.assertNotEquals(0, st.getUnallocatedSpace());
  Assert.assertNotEquals(0, st.getUsableSpace());

  Assert
      .assertTrue(st.supportsFileAttributeView(BasicFileAttributeView.class));
  Assert.assertTrue(st.supportsFileAttributeView("basic"));

  st.getAttribute("test");
}
 
Example #9
Source File: FilterFileSystem.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<FileStore> getFileStores() {
  final Iterable<FileStore> fileStores = delegate.getFileStores();
  return () -> {
    final Iterator<FileStore> iterator = fileStores.iterator();
    return new Iterator<FileStore>() {
      @Override
      public boolean hasNext() {
        return iterator.hasNext();
      }

      @Override
      public FileStore next() {
        return new FilterFileStore(iterator.next(), parent.getScheme()) {};
      }
      
      @Override
      public void remove() {
        iterator.remove();
      }
    };
  };
}
 
Example #10
Source File: IndexFetcher.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static Long getUsableSpace(String dir) {
  try {
    File file = new File(dir);
    if (!file.exists()) {
      file = file.getParentFile();
      if (!file.exists()) {//this is not a disk directory . so just pretend that there is enough space
        return Long.MAX_VALUE;
      }
    }
    FileStore fileStore = Files.getFileStore(file.toPath());
    return fileStore.getUsableSpace();
  } catch (IOException e) {
    throw new SolrException(ErrorCode.SERVER_ERROR, "Could not free disk space", e);
  }
}
 
Example #11
Source File: FaultyFileSystem.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Iterable<FileStore> getFileStores() {
    FileStore store;
    try {
        store = Files.getFileStore(root);
    } catch (IOException ioe) {
        store = null;
    }
    return SoleIterable(store);
}
 
Example #12
Source File: FilePersistenceUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static boolean supportsFileOwnerAttributeView(Path path, Class<? extends FileOwnerAttributeView> view) {
    FileStore store;
    try {
        store = Files.getFileStore(path);
    } catch (IOException e) {
        return false;
    }
    return store.supportsFileAttributeView(view);
}
 
Example #13
Source File: ESFileStore.java    From crate with Apache License 2.0 5 votes vote down vote up
private static String getMountPointLinux(final FileStore store) {
    String desc = store.toString();
    int index = desc.lastIndexOf(" (");
    if (index != -1) {
        return desc.substring(0, index);
    } else {
        return desc;
    }
}
 
Example #14
Source File: TestIOUtils.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testNVME() 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/nvme0n1p1)", "btrfs", "/dev/nvme0n1p1");
  // make a fake /dev/nvme0n1p1 for it
  Path devdir = dir.resolve("dev");
  Files.createDirectories(devdir);
  Files.createFile(devdir.resolve("nvme0n1p1"));
  // make a fake /sys/block/nvme0n1/queue/rotational file for it
  Path sysdir = dir.resolve("sys").resolve("block").resolve("nvme0n1").resolve("queue");
  Files.createDirectories(sysdir);
  try (OutputStream o = Files.newOutputStream(sysdir.resolve("rotational"))) {
    o.write("0\n".getBytes(StandardCharsets.US_ASCII));
  }
  // As test for the longest path match, add some other devices (that have no queue/rotational), too:
  Files.createFile(dir.resolve("sys").resolve("block").resolve("nvme0"));
  Files.createFile(dir.resolve("sys").resolve("block").resolve("dummy"));
  Files.createFile(dir.resolve("sys").resolve("block").resolve("nvm"));
  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));
}
 
Example #15
Source File: TestIOUtils.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testGetFileStore() throws Exception {
  Path dir = createTempDir();
  dir = FilterPath.unwrap(dir).toRealPath();
  
  // now we can create some fake mount points:
  FileStore root = new MockFileStore(dir.toString() + " (/dev/sda1)", "ntfs", "/dev/sda1");
  FileStore usr = new MockFileStore(dir.resolve("usr").toString() + " (/dev/sda2)", "xfs", "/dev/sda2");

  // associate some preset files to these
  Map<String,FileStore> mappings = new HashMap<>();
  mappings.put(dir.toString(), root);
  mappings.put(dir.resolve("foo.txt").toString(), root);
  mappings.put(dir.resolve("usr").toString(), usr);
  mappings.put(dir.resolve("usr/bar.txt").toString(), usr);
  
  FileSystem mockLinux = new MockLinuxFileSystemProvider(dir.getFileSystem(), mappings, dir).getFileSystem(null);
  Path mockPath = mockLinux.getPath(dir.toString());
  
  // sanity check our mock:
  assertSame(usr, Files.getFileStore(mockPath.resolve("usr")));
  assertSame(usr, Files.getFileStore(mockPath.resolve("usr/bar.txt")));
  // for root filesystem we get a crappy one
  assertNotSame(root, Files.getFileStore(mockPath));
  assertNotSame(usr, Files.getFileStore(mockPath));
  assertNotSame(root, Files.getFileStore(mockPath.resolve("foo.txt")));
  assertNotSame(usr, Files.getFileStore(mockPath.resolve("foo.txt")));
  
  // now test our method:
  assertSame(usr, IOUtils.getFileStore(mockPath.resolve("usr")));
  assertSame(usr, IOUtils.getFileStore(mockPath.resolve("usr/bar.txt")));
  assertSame(root, IOUtils.getFileStore(mockPath));
  assertSame(root, IOUtils.getFileStore(mockPath.resolve("foo.txt")));
}
 
Example #16
Source File: TestIOUtils.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public FileStore getFileStore(Path path) throws IOException {
  FileStore ret = filesToStore.get(path.toString());
  if (ret == null) {
    throw new IllegalArgumentException("this mock doesnt know wtf to do with: " + path);
  }
  // act like the linux fs provider here, return a crazy rootfs one
  if (ret.toString().startsWith(root + " (")) {
    return new MockFileStore(root + " (rootfs)", "rootfs", "rootfs");
  }

  return ret;
}
 
Example #17
Source File: FileStoresMeterBinder.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void bindTo(MeterRegistry registry) {
  for (FileStore fileStore : FileSystems.getDefault().getFileStores()) {
    LOG.debug(
        "Add gauge metric for {}, isReadOnly {}, type {}",
        fileStore.name(),
        fileStore.isReadOnly(),
        fileStore.type());
    Iterable<Tag> tagsWithPath = Tags.concat(Tags.empty(), "path", fileStore.toString());

    Gauge.builder("disk.free", fileStore, exceptionToNonWrapper(FileStore::getUnallocatedSpace))
        .tags(tagsWithPath)
        .description("Unallocated space for file storage")
        .baseUnit("bytes")
        .strongReference(true)
        .register(registry);
    Gauge.builder("disk.total", fileStore, exceptionToNonWrapper(FileStore::getTotalSpace))
        .tags(tagsWithPath)
        .description("Total space for file storage")
        .baseUnit("bytes")
        .strongReference(true)
        .register(registry);
    Gauge.builder("disk.usable", fileStore, exceptionToNonWrapper(FileStore::getUsableSpace))
        .tags(tagsWithPath)
        .description("Usable space for file storage")
        .baseUnit("bytes")
        .strongReference(true)
        .register(registry);
  }
}
 
Example #18
Source File: FaultyFileSystem.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Iterable<FileStore> getFileStores() {
    FileStore store;
    try {
        store = Files.getFileStore(root);
    } catch (IOException ioe) {
        store = null;
    }
    return SoleIterable(store);
}
 
Example #19
Source File: MCRIFSFileSystem.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Iterable<FileStore> getFileStores() {
    return MCRContentStoreFactory
        .getAvailableStores()
        .keySet()
        .stream()
        .map(MCRIFSFileSystem::getFileStore)::iterator;
}
 
Example #20
Source File: FileSystemConfiguration.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public long unallocatedBytes() throws IOException {
    Iterable<FileStore> fileStores = localFileSystem.getFileStores();
    long totalUsableSpace = 0l;
    for(FileStore fs:fileStores){
        totalUsableSpace+=fs.getUnallocatedSpace();
    }
    return totalUsableSpace;
}
 
Example #21
Source File: IOUtils.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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;    
  }
}
 
Example #22
Source File: MCRPath.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public Path toPhysicalPath() throws IOException {
    if (isAbsolute()) {
        for (FileStore fs : getFileSystem().getFileStores()) {
            if (fs instanceof MCRAbstractFileStore) {
                Path physicalPath = ((MCRAbstractFileStore) fs).getPhysicalPath(this);
                if (physicalPath != null) {
                    return physicalPath;
                }
            }
        }
        return null;
    }
    throw new IOException("Cannot get real path from relative path.");
}
 
Example #23
Source File: MCRIFSFileSystem.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private static FileStore getFileStore(String id) {
    try {
        return MCRFileStore.getInstance(id);
    } catch (IOException e) {
        throw new MCRException(e);
    }
}
 
Example #24
Source File: FaultyFileSystem.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Iterable<FileStore> getFileStores() {
    FileStore store;
    try {
        store = Files.getFileStore(root);
    } catch (IOException ioe) {
        store = null;
    }
    return SoleIterable(store);
}
 
Example #25
Source File: TestFileSystem.java    From jsr203-hadoop with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileStore() throws URISyntaxException, IOException {
  URI uri = clusterUri.resolve("/tmp/testFileStore");
  Path path = Paths.get(uri);
  if (Files.exists(path))
    Files.delete(path);
  assertFalse(Files.exists(path));
  Files.createFile(path);
  assertTrue(Files.exists(path));
  FileStore st = Files.getFileStore(path);
  assertNotNull(st);
}
 
Example #26
Source File: EvenMoreFiles.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
public static boolean isReadOnlyExecutable(Path path) throws IOException {
  FileStore fileStore = Files.getFileStore(path);
  if (fileStore.supportsFileAttributeView("posix")) {
    Set<PosixFilePermission> perms = Files.getPosixFilePermissions(path);
    if (perms.contains(PosixFilePermission.OWNER_EXECUTE)
        && !perms.contains(PosixFilePermission.GROUP_EXECUTE)
        && !perms.contains(PosixFilePermission.OTHERS_EXECUTE)) {
      setReadOnlyPerms(path, true);
    }
    return perms.contains(PosixFilePermission.OWNER_EXECUTE);
  } else {
    return Files.isExecutable(path);
  }
}
 
Example #27
Source File: FileSingleStreamSpillerFactory.java    From presto with Apache License 2.0 5 votes vote down vote up
private boolean hasEnoughDiskSpace(Path path)
{
    try {
        FileStore fileStore = getFileStore(path);
        return fileStore.getUsableSpace() > fileStore.getTotalSpace() * (1.0 - maxUsedSpaceThreshold);
    }
    catch (IOException e) {
        throw new PrestoException(OUT_OF_SPILL_SPACE, "Cannot determine free space for spill", e);
    }
}
 
Example #28
Source File: LocalFileSystem.java    From simple-nfs with Apache License 2.0 5 votes vote down vote up
@Override
public FsStat getFsStat() throws IOException {
    FileStore store = Files.getFileStore(_root);
    long total = store.getTotalSpace();
    long free = store.getUsableSpace();
    return new FsStat(total, Long.MAX_VALUE, total-free, pathToInode.size());
}
 
Example #29
Source File: FileSystemConfiguration.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public long freeDiskBytes() throws IOException {
    Iterable<FileStore> fileStores = localFileSystem.getFileStores();
    long totalUsableSpace = 0l;
    for(FileStore fs:fileStores){
        totalUsableSpace+=fs.getUsableSpace();
    }
    return totalUsableSpace;
}
 
Example #30
Source File: Basic.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void checkFileStores(FileSystem fs) throws IOException {
    // sanity check method
    if (FileUtils.areFileSystemsAccessible()) {
        System.out.println("\n--- Begin FileStores ---");
        for (FileStore store: fs.getFileStores()) {
            System.out.println(store);
        }
        System.out.println("--- EndFileStores ---\n");
    } else {
        System.err.println
            ("Skipping FileStore check due to file system access failure");
    }
}