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:Main.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("");
    FileStore fs = Files.getFileStore(path);

    // Check if POSIX file attribute is supported by the file store
    boolean supported = fs.supportsFileAttributeView(PosixFileAttributeView.class);
    if (supported) {
        System.out.println("POSIX file attribute view  is supported.");
    } else {//ww w.java  2s  . c o m
        System.out.println("POSIX file attribute view  is not  supported.");
    }

}

From source file:Main.java

public static void main(String[] args) {
    Path path = Paths.get("C:");

    try {/*  w ww  . j av a 2s.c o  m*/
        FileStore fs = Files.getFileStore(path);
        printDetails(fs, AclFileAttributeView.class);
        printDetails(fs, BasicFileAttributeView.class);
        printDetails(fs, DosFileAttributeView.class);
        printDetails(fs, FileOwnerAttributeView.class);
        printDetails(fs, PosixFileAttributeView.class);
        printDetails(fs, UserDefinedFileAttributeView.class);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:/home/docs/users.txt");
    FileStore fileStore = Files.getFileStore(path);
    System.out.println(/*ww  w . j a v  a2  s.c o m*/
            "FileAttributeView supported: " + fileStore.supportsFileAttributeView(FileAttributeView.class));
    System.out.println("BasicFileAttributeView supported: "
            + fileStore.supportsFileAttributeView(BasicFileAttributeView.class));
    System.out.println("FileOwnerAttributeView supported: "
            + fileStore.supportsFileAttributeView(FileOwnerAttributeView.class));
    System.out.println("AclFileAttributeView supported: "
            + fileStore.supportsFileAttributeView(AclFileAttributeView.class));
    System.out.println("PosixFileAttributeView supported: "
            + fileStore.supportsFileAttributeView(PosixFileAttributeView.class));
    System.out.println("UserDefinedFileAttributeView supported: "
            + fileStore.supportsFileAttributeView(UserDefinedFileAttributeView.class));
    System.out.println("DosFileAttributeView supported: "
            + fileStore.supportsFileAttributeView(DosFileAttributeView.class));

    System.out.println("FileAttributeView supported: " + fileStore.supportsFileAttributeView("file"));
    System.out.println("BasicFileAttributeView supported: " + fileStore.supportsFileAttributeView("basic"));
    System.out.println("FileOwnerAttributeView supported: " + fileStore.supportsFileAttributeView("owner"));
    System.out.println("AclFileAttributeView supported: " + fileStore.supportsFileAttributeView("acl"));
    System.out.println("PosixFileAttributeView supported: " + fileStore.supportsFileAttributeView("posix"));
    System.out
            .println("UserDefinedFileAttributeView supported: " + fileStore.supportsFileAttributeView("user"));
    System.out.println("DosFileAttributeView supported: " + fileStore.supportsFileAttributeView("dos"));
}

From source file:uk.co.flax.harahachibu.services.impl.SolrDiskSpaceChecker.java

@Override
public void configure(Map<String, Object> configuration) throws DiskSpaceCheckerException {
    final String dataPath = (String) configuration.get(DATA_DIR_CONFIG_OPTION);
    if (StringUtils.isBlank(dataPath)) {
        throw new DiskSpaceCheckerException("No data directory given for Solr disk space checker");
    } else {/*from   ww w  .  jav a2  s  .c om*/
        try {
            // Set up the FileStore
            Path path = FileSystems.getDefault().getPath(dataPath);
            fs = Files.getFileStore(path);
        } catch (IOException e) {
            LOGGER.error("IO Exception initialising file store: {}", e.getMessage());
            throw new DiskSpaceCheckerException(e);
        }
    }
}

From source file:RestoreService.java

public static void setAdvancedAttributes(final VOBackupFile voBackupFile, final File file) throws IOException {
    // advanced attributes
    // owner/*w  ww.  jav  a 2s.com*/
    if (StringUtils.isNotBlank(voBackupFile.getOwner())) {
        try {
            UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
            UserPrincipal userPrincipal = lookupService.lookupPrincipalByName(voBackupFile.getOwner());
            Files.setOwner(file.toPath(), userPrincipal);
        } catch (UserPrincipalNotFoundException e) {
            logger.warn("Cannot set owner {}", voBackupFile.getOwner());
        }
    }
    if (Files.getFileStore(file.toPath()).supportsFileAttributeView(DosFileAttributeView.class)
            && BooleanUtils.isTrue(voBackupFile.getDosAttr())) {
        Files.setAttribute(file.toPath(), "dos:hidden", BooleanUtils.isTrue(voBackupFile.getDosHidden()));
        Files.setAttribute(file.toPath(), "dos:archive", BooleanUtils.isTrue(voBackupFile.getDosArchive()));
        Files.setAttribute(file.toPath(), "dos:readonly", BooleanUtils.isTrue(voBackupFile.getDosReadOnly()));
        Files.setAttribute(file.toPath(), "dos:system", BooleanUtils.isTrue(voBackupFile.getDosSystem()));
    }

    if (Files.getFileStore(file.toPath()).supportsFileAttributeView(PosixFileAttributeView.class)
            && BooleanUtils.isTrue(voBackupFile.getPosixAttr())) {
        try {
            UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
            GroupPrincipal groupPrincipal = lookupService
                    .lookupPrincipalByGroupName(voBackupFile.getPosixGroup());
            Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS)
                    .setGroup(groupPrincipal);
        } catch (UserPrincipalNotFoundException e) {
            logger.warn("Cannot set group {}", voBackupFile.getOwner());
        }

        if (StringUtils.isNotBlank(voBackupFile.getPosixPermitions())) {
            Set<PosixFilePermission> perms = PosixFilePermissions.fromString(voBackupFile.getPosixPermitions());
            Files.setPosixFilePermissions(file.toPath(), perms);
        }
    }
}

From source file:VOBackupFile.java

/**
 * Wrapper for metadata of backuped file.
 *
 * @param file backuped file//from   w  ww  .  j a va  2 s .c  om
 */
public VOBackupFile(File file) {
    this.path = file.getAbsolutePath();
    this.directory = file.isDirectory();
    this.symLink = Files.isSymbolicLink(file.toPath());

    this.size = ((file.isDirectory() || symLink) ? 0 : file.length());
    this.modify = new Date(file.lastModified());

    if (symLink) {
        try {
            symlinkTarget = Files.readSymbolicLink(file.toPath()).toString();
        } catch (IOException e) {
            e.printStackTrace(); //TODO Lebeda - oetit do logu
        }
    } else {
        // advanced attributes

        try {

            owner = Files.getOwner(file.toPath(), LinkOption.NOFOLLOW_LINKS).getName();

            if (Files.getFileStore(file.toPath()).supportsFileAttributeView(DosFileAttributeView.class)) {
                dosAttr = Boolean.TRUE;
                final DosFileAttributes dosFileAttributes = Files.readAttributes(file.toPath(),
                        DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);

                dosArchive = dosFileAttributes.isArchive();
                dosHidden = dosFileAttributes.isHidden();
                dosSystem = dosFileAttributes.isSystem();
                dosReadOnly = dosFileAttributes.isReadOnly();
            } else {
                dosAttr = Boolean.FALSE;
            }

            if (Files.getFileStore(file.toPath()).supportsFileAttributeView(PosixFileAttributeView.class)) {
                posixAttr = Boolean.TRUE;
                final PosixFileAttributes posixFileAttributes = Files.readAttributes(file.toPath(),
                        PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
                posixGroup = posixFileAttributes.group().getName();
                posixPermitions = PosixFilePermissions.toString(posixFileAttributes.permissions());
            } else {
                posixAttr = Boolean.FALSE;
            }

        } catch (IOException e) {
            e.printStackTrace(); //Todo implementovat
        }
    }

}

From source file:com.googlecode.fascinator.storage.jclouds.BlobStoreClient.java

/**
 * Establish a connection to the BlobStore, then return the instantiated
 * BlobStore client used to connect.//from www .j  a v a2  s  .  com
 *
 * @return BlobStore: The client used to connect to the API
 * @throws StorageException
 *             if there was an error
 */
private static BlobStore blobStoreConnect() throws StorageException {
    if (blobStore != null && connectCount < 100) {
        return blobStore;
    }
    connectCount = 0;
    ContextBuilder contextBuilder = ContextBuilder.newBuilder(provider);
    // If we're using filesystem, set local directory to write objects to
    if ("filesystem".equals(provider)) {
        if (supportsUserMetadataSetting != null) {
            supportsUserMetadata = supportsUserMetadataSetting;
        } else {
            File storageDir = new File(fileSystemLocation);
            if (!storageDir.exists()) {
                try {
                    FileUtils.forceMkdir(storageDir);
                    // Java doesn't support extended attributes in some file
                    // systems like FAT32 and HFS. As JClouds use them to
                    // store
                    // user metadata we'll need to store them differently on
                    // these file systems.
                    if (!Files.getFileStore(storageDir.toPath())
                            .supportsFileAttributeView(UserDefinedFileAttributeView.class)) {
                        supportsUserMetadata = false;
                    }
                } catch (IOException e) {
                    throw new StorageException("Failed to create storage directory", e);
                }
            }
        }
        Properties properties = new Properties();
        properties.setProperty(FilesystemConstants.PROPERTY_BASEDIR, fileSystemLocation);
        contextBuilder.overrides(properties);
    } else if ("gridfs".equals(provider)) {
        Properties properties = new Properties();
        properties.setProperty(Constants.PROPERTY_ENDPOINT, gridFsConnectionString);
        contextBuilder.overrides(properties);

    }
    context = contextBuilder.credentials(identity, credential)
            .endpoint("https://keystone.rc.nectar.org.au:5000/v2.0").buildView(BlobStoreContext.class);

    blobStore = context.getBlobStore();

    Location loc = null;
    if (StringUtils.isNotEmpty(location)) {
        for (Location assignableLoc : blobStore.listAssignableLocations()) {
            if (assignableLoc.getId().equalsIgnoreCase(location)) {
                loc = assignableLoc;
                break;
            }

        }
        if (loc == null) {
            throw new StorageException(location + " location not found in Blobstore");
        }
    }
    blobStore.createContainerInLocation(loc, containerName);

    return blobStore;
}

From source file:alluxio.cli.validation.StorageSpaceValidationTask.java

private boolean addDirectoryInfo(String path, long quota, Map<String, MountedStorage> storageMap)
        throws IOException {
    File file = new File(path);
    if (!file.exists()) {
        System.err.format("Path %s does not exist.%n", path);
        return false;
    }//w  w  w  .j  a v a2  s  .  co m

    if (!file.isDirectory()) {
        System.err.format("Path %s is not a valid directory.%n", path);
        return false;
    }

    long directorySize = FileUtils.sizeOfDirectory(file);

    // gets mounted FileStore that backs the directory of the given path
    FileStore store = Files.getFileStore(Paths.get(path));

    MountedStorage storage = storageMap.get(store.name());
    if (storage == null) {
        storage = new MountedStorage(store);
        storageMap.put(store.name(), storage);
    }

    storage.addDirectoryInfo(path, quota, directorySize);
    return true;
}

From source file:org.cryptomator.frontend.webdav.servlet.DavFolder.java

@Override
public DavProperty<?> getProperty(DavPropertyName name) {
    if (PROPERTY_QUOTA_AVAILABLE.equals(name)) {
        try {//from  w w  w.j  av a 2  s  .c o  m
            long availableBytes = Files.getFileStore(path).getTotalSpace();
            return new DefaultDavProperty<Long>(name, availableBytes);
        } catch (IOException e) {
            return null;
        }
    } else if (PROPERTY_QUOTA_USED.equals(name)) {
        try {
            long availableBytes = Files.getFileStore(path).getTotalSpace();
            long usedBytes = Files.getFileStore(path).getUsableSpace();
            long freeBytes = availableBytes - usedBytes;
            return new DefaultDavProperty<Long>(name, freeBytes);
        } catch (IOException e) {
            return null;
        }
    } else {
        return super.getProperty(name);
    }
}

From source file:com.commander4j.util.JUtility.java

public static String diskFree() {
    String result = "";
    long free = 0;
    File f = new File(System.getProperty("user.dir"));
    String root = "";

    while (f.getParent() != null) {
        root = f.getParent();/*from  w w w .j  a  va2  s .  c o m*/
        f = new File(root);
    }

    try {
        URI rootURI = new URI("file:///");
        Path rootPath = Paths.get(rootURI);
        Path dirPath = rootPath.resolve(root);
        FileStore dirFileStore = Files.getFileStore(dirPath);

        free = dirFileStore.getUsableSpace() / 1048576;
        result = String.valueOf(free) + " mb on " + root;

    } catch (Exception e) {
        result = "Error trying to determine free disk space " + e.getLocalizedMessage();
    }

    return result;
}