Java Code Examples for java.nio.file.FileStore#supportsFileAttributeView()

The following examples show how to use java.nio.file.FileStore#supportsFileAttributeView() . 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: ScopedTemporaryDirectory.java    From bazel with Apache License 2.0 6 votes vote down vote up
private void makeWritable(Path file) throws IOException {
  FileStore fileStore = Files.getFileStore(file);
  if (IS_WINDOWS && fileStore.supportsFileAttributeView(DosFileAttributeView.class)) {
    DosFileAttributeView dosAttribs =
        Files.getFileAttributeView(file, DosFileAttributeView.class);
    if (dosAttribs != null) {
      dosAttribs.setReadOnly(false);
    }
  } else if (fileStore.supportsFileAttributeView(PosixFileAttributeView.class)) {
    PosixFileAttributeView posixAttribs =
        Files.getFileAttributeView(file, PosixFileAttributeView.class);
    if (posixAttribs != null) {
      posixAttribs.setPermissions(EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE));
    }
  }
}
 
Example 2
Source File: IOUtils.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
public static List<NamedFileKey> listDirentSorted(Path path, FileStore fileStore)
    throws IOException {
  final List<NamedFileKey> dirents;
  if (fileStore.supportsFileAttributeView("posix")) {
    dirents = ffiReaddir(libc.get(), runtime(), path);
  } else {
    dirents = listNIOdirentSorted(path);
  }
  dirents.sort(Comparator.comparing(NamedFileKey::getName));
  return dirents;
}
 
Example 3
Source File: EvenMoreFiles.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
public static void setReadOnlyPerms(Path path, boolean executable) throws IOException {
  FileStore fileStore = Files.getFileStore(path);
  if (fileStore.supportsFileAttributeView("posix")) {
    if (executable) {
      Files.setPosixFilePermissions(path, readOnlyExecPerms);
    } else {
      Files.setPosixFilePermissions(path, readOnlyPerms);
    }
  } else if (fileStore.supportsFileAttributeView("acl")) {
    // windows, we hope
    UserPrincipal authenticatedUsers =
        path.getFileSystem()
            .getUserPrincipalLookupService()
            .lookupPrincipalByName("Authenticated Users");
    AclEntry denyWriteEntry =
        AclEntry.newBuilder()
            .setType(AclEntryType.DENY)
            .setPrincipal(authenticatedUsers)
            .setPermissions(AclEntryPermission.APPEND_DATA, AclEntryPermission.WRITE_DATA)
            .build();
    AclEntry execEntry =
        AclEntry.newBuilder()
            .setType(executable ? AclEntryType.ALLOW : AclEntryType.DENY)
            .setPrincipal(authenticatedUsers)
            .setPermissions(AclEntryPermission.EXECUTE)
            .build();

    AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class);
    List<AclEntry> acl = view.getAcl();
    acl.add(0, execEntry);
    acl.add(0, denyWriteEntry);
    view.setAcl(acl);
  } else {
    throw new UnsupportedOperationException("no recognized attribute view");
  }
}
 
Example 4
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 5
Source File: Directories.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
private static void makeWritable(Path dir, boolean writable) throws IOException {
  FileStore fileStore = Files.getFileStore(dir);
  if (fileStore.supportsFileAttributeView("posix")) {
    if (writable) {
      Files.setPosixFilePermissions(dir, writablePerms);
    } else {
      Files.setPosixFilePermissions(dir, nonWritablePerms);
    }
  } else if (fileStore.supportsFileAttributeView("acl")) {
    // windows, we hope
    UserPrincipal authenticatedUsers =
        dir.getFileSystem()
            .getUserPrincipalLookupService()
            .lookupPrincipalByName("Authenticated Users");
    AclEntry entry =
        AclEntry.newBuilder()
            .setType(writable ? AclEntryType.ALLOW : AclEntryType.DENY)
            .setPrincipal(authenticatedUsers)
            .setPermissions(
                AclEntryPermission.DELETE,
                AclEntryPermission.DELETE_CHILD,
                AclEntryPermission.ADD_FILE,
                AclEntryPermission.ADD_SUBDIRECTORY)
            .build();

    AclFileAttributeView view = Files.getFileAttributeView(dir, AclFileAttributeView.class);
    List<AclEntry> acl = view.getAcl();
    acl.add(0, entry);
    view.setAcl(acl);
  } else {
    throw new UnsupportedOperationException("no recognized attribute view");
  }
}
 
Example 6
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 7
Source File: TestListFile.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testAttributesSet() throws Exception {
    // create temp file and time constant
    final File file1 = new File(TESTDIR + "/file1.txt");
    assertTrue(file1.createNewFile());
    FileOutputStream fos = new FileOutputStream(file1);
    fos.write(new byte[1234]);
    fos.close();
    assertTrue(file1.setLastModified(time3millis));
    Long time3rounded = time3millis - time3millis % 1000;
    String userName = System.getProperty("user.name");

    // validate the file transferred
    runner.setProperty(ListFile.DIRECTORY, testDir.getAbsolutePath());
    runNext();
    runner.assertAllFlowFilesTransferred(ListFile.REL_SUCCESS);
    final List<MockFlowFile> successFiles1 = runner.getFlowFilesForRelationship(ListFile.REL_SUCCESS);
    assertEquals(1, successFiles1.size());

    // get attribute check values
    final Path file1Path = file1.toPath();
    final Path directoryPath = new File(TESTDIR).toPath();
    final Path relativePath = directoryPath.relativize(file1.toPath().getParent());
    String relativePathString = relativePath.toString();
    relativePathString = relativePathString.isEmpty() ? "." + File.separator : relativePathString + File.separator;
    final Path absolutePath = file1.toPath().toAbsolutePath();
    final String absolutePathString = absolutePath.getParent().toString() + File.separator;
    final FileStore store = Files.getFileStore(file1Path);
    final DateFormat formatter = new SimpleDateFormat(ListFile.FILE_MODIFY_DATE_ATTR_FORMAT, Locale.US);
    final String time3Formatted = formatter.format(time3rounded);

    // check standard attributes
    MockFlowFile mock1 = successFiles1.get(0);
    assertEquals(relativePathString, mock1.getAttribute(CoreAttributes.PATH.key()));
    assertEquals("file1.txt", mock1.getAttribute(CoreAttributes.FILENAME.key()));
    assertEquals(absolutePathString, mock1.getAttribute(CoreAttributes.ABSOLUTE_PATH.key()));
    assertEquals("1234", mock1.getAttribute(ListFile.FILE_SIZE_ATTRIBUTE));

    // check attributes dependent on views supported
    if (store.supportsFileAttributeView("basic")) {
        assertEquals(time3Formatted, mock1.getAttribute(ListFile.FILE_LAST_MODIFY_TIME_ATTRIBUTE));
        assertNotNull(mock1.getAttribute(ListFile.FILE_CREATION_TIME_ATTRIBUTE));
        assertNotNull(mock1.getAttribute(ListFile.FILE_LAST_ACCESS_TIME_ATTRIBUTE));
    }
    if (store.supportsFileAttributeView("owner")) {
        // look for username containment to handle Windows domains as well as Unix user names
        // org.junit.ComparisonFailure: expected:<[]username> but was:<[DOMAIN\]username>
        assertTrue(mock1.getAttribute(ListFile.FILE_OWNER_ATTRIBUTE).contains(userName));
    }
    if (store.supportsFileAttributeView("posix")) {
        assertNotNull("Group name should be set", mock1.getAttribute(ListFile.FILE_GROUP_ATTRIBUTE));
        assertNotNull("File permissions should be set", mock1.getAttribute(ListFile.FILE_PERMISSIONS_ATTRIBUTE));
    }
}
 
Example 8
Source File: TestListFile.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testAttributesSet() throws Exception {
    // create temp file and time constant
    final File file1 = new File(TESTDIR + "/file1.txt");
    assertTrue(file1.createNewFile());
    FileOutputStream fos = new FileOutputStream(file1);
    fos.write(new byte[1234]);
    fos.close();
    assertTrue(file1.setLastModified(time3millis));
    Long time3rounded = time3millis - time3millis % 1000;
    String userName = System.getProperty("user.name");

    // validate the file transferred
    runner.setProperty(ListFile.DIRECTORY, testDir.getAbsolutePath());
    runNext();
    runner.assertAllFlowFilesTransferred(ListFile.REL_SUCCESS);
    final List<MockFlowFile> successFiles1 = runner.getFlowFilesForRelationship(ListFile.REL_SUCCESS);
    assertEquals(1, successFiles1.size());

    // get attribute check values
    final Path file1Path = file1.toPath();
    final Path directoryPath = new File(TESTDIR).toPath();
    final Path relativePath = directoryPath.relativize(file1.toPath().getParent());
    String relativePathString = relativePath.toString();
    relativePathString = relativePathString.isEmpty() ? "." + File.separator : relativePathString + File.separator;
    final Path absolutePath = file1.toPath().toAbsolutePath();
    final String absolutePathString = absolutePath.getParent().toString() + File.separator;
    final FileStore store = Files.getFileStore(file1Path);
    final DateFormat formatter = new SimpleDateFormat(ListFile.FILE_MODIFY_DATE_ATTR_FORMAT, Locale.US);
    final String time3Formatted = formatter.format(time3rounded);

    // check standard attributes
    MockFlowFile mock1 = successFiles1.get(0);
    assertEquals(relativePathString, mock1.getAttribute(CoreAttributes.PATH.key()));
    assertEquals("file1.txt", mock1.getAttribute(CoreAttributes.FILENAME.key()));
    assertEquals(absolutePathString, mock1.getAttribute(CoreAttributes.ABSOLUTE_PATH.key()));
    assertEquals("1234", mock1.getAttribute(ListFile.FILE_SIZE_ATTRIBUTE));

    // check attributes dependent on views supported
    if (store.supportsFileAttributeView("basic")) {
        assertEquals(time3Formatted, mock1.getAttribute(ListFile.FILE_LAST_MODIFY_TIME_ATTRIBUTE));
        assertNotNull(mock1.getAttribute(ListFile.FILE_CREATION_TIME_ATTRIBUTE));
        assertNotNull(mock1.getAttribute(ListFile.FILE_LAST_ACCESS_TIME_ATTRIBUTE));
    }
    if (store.supportsFileAttributeView("owner")) {
        // look for username containment to handle Windows domains as well as Unix user names
        // org.junit.ComparisonFailure: expected:<[]username> but was:<[DOMAIN\]username>
        assertTrue(mock1.getAttribute(ListFile.FILE_OWNER_ATTRIBUTE).contains(userName));
    }
    if (store.supportsFileAttributeView("posix")) {
        assertNotNull("Group name should be set", mock1.getAttribute(ListFile.FILE_GROUP_ATTRIBUTE));
        assertNotNull("File permissions should be set", mock1.getAttribute(ListFile.FILE_PERMISSIONS_ATTRIBUTE));
    }
}
 
Example 9
Source File: UserExtendedAttributes.java    From yfs with Apache License 2.0 2 votes vote down vote up
/**
 * @return if the passed {@link FileStore} has support for user extended
 * attributes
 */
private static boolean hasUserXattrSupport(FileStore fileStore) {
    return fileStore.supportsFileAttributeView(UserDefinedFileAttributeView.class);
}