Example usage for java.nio.file Files getFileStore

List of usage examples for java.nio.file Files getFileStore

Introduction

In this page you can find the example usage for java.nio.file Files getFileStore.

Prototype

public static FileStore getFileStore(Path path) throws IOException 

Source Link

Document

Returns the FileStore representing the file store where a file is located.

Usage

From source file:org.zanata.ZanataInit.java

/**
 * Returns true if any of the files appear to be stored in NFS (or we can't
 * tell).//from   w  w  w  .ja  va  2 s .  com
 */
private boolean mightUseNFS(File... files) {
    try {
        FileSystem fileSystem = FileSystems.getDefault();
        for (File file : files) {
            Path path = fileSystem.getPath(file.getAbsolutePath());
            String fileStoreType = Files.getFileStore(path).type();
            if (fileStoreType.toLowerCase().contains("nfs")) {
                return true;
            }
        }
        return false;
    } catch (IOException e) {
        log.warn(e.toString(), e);
        // assume the worst case
        return true;
    }
}

From source file:com.gitblit.manager.FilestoreManager.java

@Override
public long getFilestoreAvailableByteCount() {

    try {//from  w ww.  ja  v  a  2s  . c  om
        return Files.getFileStore(getStorageFolder().toPath()).getUsableSpace();
    } catch (IOException e) {
        logger.error(MessageFormat.format("Failed to retrive available space in Filestore {0}", e));
    }

    return UNDEFINED_SIZE;
}

From source file:com.ethercamp.harmony.service.BlockchainInfoService.java

/**
 * Get free space of disk where project located.
 * Verified on multi disk Windows.//from   w  w w.ja  v  a2 s. c om
 * Not tested against sym links
 */
private long getFreeDiskSpace() {
    final File currentDir = new File(".");
    for (Path root : FileSystems.getDefault().getRootDirectories()) {
        //            log.debug(root.toAbsolutePath() + " vs current " + currentDir.getAbsolutePath());
        try {
            final FileStore store = Files.getFileStore(root);

            final boolean isCurrentDirBelongsToRoot = Paths.get(currentDir.getAbsolutePath())
                    .startsWith(root.toAbsolutePath());
            if (isCurrentDirBelongsToRoot) {
                final long usableSpace = store.getUsableSpace();
                //                    log.debug("Disk available:" + readableFileSize(usableSpace)
                //                            + ", total:" + readableFileSize(store.getTotalSpace()));
                return usableSpace;
            }
        } catch (IOException e) {
            log.error("Problem querying space: " + e.toString());
        }
    }
    return 0;
}

From source file:fr.ortolang.diffusion.store.binary.BinaryStoreServiceBean.java

@Override
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public List<BinaryStoreContent> systemBrowse(String name, String prefix) throws BinaryStoreServiceException {
    if (name == null || name.length() == 0) {
        List<BinaryStoreContent> vinfos = new ArrayList<BinaryStoreContent>();
        List<String> vnames = new ArrayList<String>();
        vnames.addAll(BinaryStoreVolumeMapper.listVolumes());
        vnames.add(WORK);/*  www  .  j  av  a  2s .c o  m*/
        for (String vname : vnames) {
            try {
                BinaryStoreContent volume = new BinaryStoreContent();
                Path vpath = Paths.get(base.toString(), vname);
                FileStore vstore = Files.getFileStore(vpath);
                volume.setPath(vname);
                volume.setType(Type.VOLUME);
                volume.setFsName(vstore.name());
                volume.setFsType(vstore.type());
                volume.setFsTotalSize(vstore.getTotalSpace());
                volume.setFsFreeSize(vstore.getUsableSpace());
                volume.setSize(Files.size(vpath));
                volume.setLastModificationDate(Files.getLastModifiedTime(vpath).toMillis());
                vinfos.add(volume);
            } catch (IOException e) {
                LOGGER.log(Level.WARNING,
                        "Unable to retrieve binary store volume information for volume: " + vname);
            }
        }
        return vinfos;
    } else {
        try {
            if (prefix == null) {
                prefix = "";
            }
            Path vpath = Paths.get(base.toString(), name, prefix);
            if (!Files.exists(vpath)) {
                throw new BinaryStoreServiceException(
                        "volume name does not point to an existing file or a directory");
            }
            return Files.list(vpath).map(this::pathToContent).collect(Collectors.toList());
        } catch (IOException e) {
            throw new BinaryStoreServiceException(e);
        }
    }
}

From source file:misc.FileHandler.java

/**
 * Returns a temporary file path that is on the same file store as the given
 * file. The temporary file is created without content, if the given file's
 * file store is identical to the system's default temporary directory file
 * store./*from w  w w.j  a  v a2 s.  c om*/
 * 
 * @param target
 *            the file which determines the file store.
 * @return the path of the temporary file or <code>null</code>, if an error
 *         occurred.
 */
public static Path getTempFile(Path target) {
    Path tempFile = null;
    boolean success = false;

    target = target.normalize();

    try {
        Path targetDirectory = target.toAbsolutePath().getParent();
        tempFile = Files.createTempFile(target.getFileName().toString(), TEMP_FILE_SUFFIX);

        if (!Files.getFileStore(tempFile).equals(Files.getFileStore(targetDirectory))) {
            // the temporary file should be in the target directory.
            Files.delete(tempFile);
            tempFile = Paths.get(targetDirectory.toString(), tempFile.getFileName().toString());
            success = true;
        } else {
            success = true;
        }
    } catch (IOException e) {
        Logger.logError(e);
    } finally {
        if (!success && (tempFile != null)) {
            try {
                Files.deleteIfExists(tempFile);
            } catch (IOException innerE) {
                Logger.logError(innerE);
            }
        }
    }

    return success ? tempFile : null;
}

From source file:de.blizzy.backup.backup.BackupRun.java

private void checkDiskSpaceAndRemoveOldBackups() {
    try {/*from w w w .  j  a  va  2 s.c o  m*/
        FileStore store = Files.getFileStore(new File(settings.getOutputFolder()).toPath());
        long total = store.getTotalSpace();
        if (total > 0) {
            for (;;) {
                long available = store.getUsableSpace();
                if (available <= 0) {
                    break;
                }

                double avail = available * 100d / total;
                if (avail >= (100d - settings.getMaxDiskFillRate())) {
                    break;
                }

                if (!removeOldestBackup()) {
                    break;
                }

                removeUnusedFiles();
            }
        }
    } catch (IOException e) {
        // ignore
    } catch (DataAccessException e) {
        BackupPlugin.getDefault().logError("error removing oldest backup", e); //$NON-NLS-1$
        fireBackupErrorOccurred(e, BackupErrorEvent.Severity.WARNING);
    }
}

From source file:org.apache.cassandra.config.DatabaseDescriptor.java

private static FileStore guessFileStore(String dir) throws IOException {
    Path path = Paths.get(dir);
    while (true) {
        try {// ww w  . j  av  a 2  s  . c  om
            return Files.getFileStore(path);
        } catch (IOException e) {
            if (e instanceof NoSuchFileException)
                path = path.getParent();
            else
                throw e;
        }
    }
}