下面列出了java.nio.file.FileStore#supportsFileAttributeView ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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));
}
}
}
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;
}
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");
}
}
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);
}
}
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");
}
}
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);
}
@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));
}
}
@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));
}
}
/**
* @return if the passed {@link FileStore} has support for user extended
* attributes
*/
private static boolean hasUserXattrSupport(FileStore fileStore) {
return fileStore.supportsFileAttributeView(UserDefinedFileAttributeView.class);
}