Search in sources :

Example 31 with PosixFileAttributes

use of java.nio.file.attribute.PosixFileAttributes in project java-chassis by ServiceComb.

the class FilePerm method setFilePerm.

/**
 * 设置文件权限。前提:必须支持PosixFileAttributeView.
 */
public static void setFilePerm(File file, String perm) {
    if (filePermSupported()) {
        try {
            Set<PosixFilePermission> perms = PosixFilePermissions.fromString(perm);
            PosixFileAttributes attr = Files.readAttributes(file.toPath(), PosixFileAttributes.class);
            attr.permissions().clear();
            Files.setPosixFilePermissions(file.toPath(), perms);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes)

Example 32 with PosixFileAttributes

use of java.nio.file.attribute.PosixFileAttributes in project alluxio by Alluxio.

the class SimpleFileManagerTest method addMultipleFileSameName.

@Test
public void addMultipleFileSameName() throws Exception {
    assertTrue(mFileManager.addFile("test", "644", "test".getBytes()));
    List<String> files = mFileManager.listFile();
    assertEquals(1, files.size());
    assertEquals("test", files.get(0));
    Path file = Paths.get(mFileManager.getNextFilePath("test"));
    PosixFileAttributes attrs = Files.getFileAttributeView(file, PosixFileAttributeView.class).readAttributes();
    assertEquals(mUser, attrs.owner().getName());
    assertEquals(mGroup, attrs.group().getName());
    verifyPerms(attrs.permissions(), "644");
    // Create a new file with the same name, but different content and permissions
    assertTrue(mFileManager.addFile("test", "777", "test 2".getBytes()));
    files = mFileManager.listFile();
    assertEquals(1, files.size());
    assertEquals("test", files.get(0));
    file = Paths.get(mFileManager.getNextFilePath("test"));
    attrs = Files.getFileAttributeView(file, PosixFileAttributeView.class).readAttributes();
    assertEquals(mUser, attrs.owner().getName());
    assertEquals(mGroup, attrs.group().getName());
    verifyPerms(attrs.permissions(), "777");
}
Also used : Path(java.nio.file.Path) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) Test(org.junit.Test)

Example 33 with PosixFileAttributes

use of java.nio.file.attribute.PosixFileAttributes in project alluxio by Alluxio.

the class LocalUnderFileSystem method getStatus.

@Override
public UfsStatus getStatus(String path) throws IOException {
    String tpath = stripPath(path);
    File file = new File(tpath);
    try {
        PosixFileAttributes attr = Files.readAttributes(Paths.get(file.getPath()), PosixFileAttributes.class);
        if (file.isFile()) {
            // Return file status.
            String contentHash = UnderFileSystemUtils.approximateContentHash(file.length(), file.lastModified());
            return new UfsFileStatus(path, contentHash, file.length(), file.lastModified(), attr.owner().getName(), attr.group().getName(), FileUtils.translatePosixPermissionToMode(attr.permissions()), mUfsConf.getBytes(PropertyKey.USER_BLOCK_SIZE_BYTES_DEFAULT));
        }
        // Return directory status.
        return new UfsDirectoryStatus(path, attr.owner().getName(), attr.group().getName(), FileUtils.translatePosixPermissionToMode(attr.permissions()), file.lastModified());
    } catch (FileSystemException e) {
        throw new FileNotFoundException(e.getMessage());
    }
}
Also used : UfsFileStatus(alluxio.underfs.UfsFileStatus) FileSystemException(java.nio.file.FileSystemException) FileNotFoundException(java.io.FileNotFoundException) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) File(java.io.File) UfsDirectoryStatus(alluxio.underfs.UfsDirectoryStatus)

Example 34 with PosixFileAttributes

use of java.nio.file.attribute.PosixFileAttributes in project alluxio by Alluxio.

the class LocalUnderFileSystem method getFileStatus.

@Override
public UfsFileStatus getFileStatus(String path) throws IOException {
    String tpath = stripPath(path);
    File file = new File(tpath);
    try {
        PosixFileAttributes attr = Files.readAttributes(Paths.get(file.getPath()), PosixFileAttributes.class);
        if (attr.isDirectory()) {
            throw new IOException(String.format("path %s is not a file", path));
        }
        String contentHash = UnderFileSystemUtils.approximateContentHash(file.length(), file.lastModified());
        return new UfsFileStatus(path, contentHash, file.length(), file.lastModified(), attr.owner().getName(), attr.group().getName(), FileUtils.translatePosixPermissionToMode(attr.permissions()), mUfsConf.getBytes(PropertyKey.USER_BLOCK_SIZE_BYTES_DEFAULT));
    } catch (FileSystemException e) {
        throw new FileNotFoundException(e.getMessage());
    }
}
Also used : UfsFileStatus(alluxio.underfs.UfsFileStatus) FileSystemException(java.nio.file.FileSystemException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) File(java.io.File)

Example 35 with PosixFileAttributes

use of java.nio.file.attribute.PosixFileAttributes in project alluxio by Alluxio.

the class LocalUnderFileSystem method getDirectoryStatus.

@Override
public UfsDirectoryStatus getDirectoryStatus(String path) throws IOException {
    String tpath = stripPath(path);
    File file = new File(tpath);
    try {
        PosixFileAttributes attr = Files.readAttributes(Paths.get(file.getPath()), PosixFileAttributes.class);
        if (!attr.isDirectory()) {
            throw new IOException(String.format("path %s is not directory", path));
        }
        return new UfsDirectoryStatus(path, attr.owner().getName(), attr.group().getName(), FileUtils.translatePosixPermissionToMode(attr.permissions()), file.lastModified());
    } catch (FileSystemException e) {
        throw new FileNotFoundException(e.getMessage());
    }
}
Also used : FileSystemException(java.nio.file.FileSystemException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) File(java.io.File) UfsDirectoryStatus(alluxio.underfs.UfsDirectoryStatus)

Aggregations

PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)37 Path (java.nio.file.Path)17 IOException (java.io.IOException)13 Test (org.junit.Test)13 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)11 File (java.io.File)8 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)8 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)6 FileTime (java.nio.file.attribute.FileTime)5 HashMap (java.util.HashMap)5 InputStream (java.io.InputStream)4 OutputStream (java.io.OutputStream)4 DosFileAttributes (java.nio.file.attribute.DosFileAttributes)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Set (java.util.Set)4 UfsDirectoryStatus (alluxio.underfs.UfsDirectoryStatus)3 UfsFileStatus (alluxio.underfs.UfsFileStatus)3 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableMap (com.google.common.collect.ImmutableMap)3