You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/09/28 18:36:31 UTC
[commons-io] branch master updated: Add
PathUtiFiles.getFileAttributeView() shorthands.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push:
new 2f6284a Add PathUtiFiles.getFileAttributeView() shorthands.
2f6284a is described below
commit 2f6284a654caec50a227b9dfa6d87af6913b77fe
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Sep 28 14:36:29 2021 -0400
Add PathUtiFiles.getFileAttributeView() shorthands.
- PathUtils.getAclFileAttributeView(Path, LinkOption...)
- PathUtils.getDosFileAttributeView(Path, LinkOption...)
- PathUtils.getPosixFileAttributeView(Path, LinkOption...)
---
src/changes/changes.xml | 6 +++
.../java/org/apache/commons/io/file/PathUtils.java | 44 ++++++++++++++++++++--
.../org/apache/commons/io/file/PathUtilsTest.java | 4 +-
3 files changed, 48 insertions(+), 6 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5fbe961..49c8fc6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -244,6 +244,12 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add PathUtils.writeString(Path, CharSequence, Charset, OpenOption...).
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add PathUtiFiles.getFileAttributeView() shorthands:
+ - PathUtils.getAclFileAttributeView(Path, LinkOption...)
+ - PathUtils.getDosFileAttributeView(Path, LinkOption...)
+ - PathUtils.getPosixFileAttributeView(Path, LinkOption...)
+ </action>
<!-- UPDATE -->
<action dev="ggregory" type="add" due-to="Gary Gregory">
Update FileEntry to use FileTime instead of long for file time stamps.
diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java b/src/main/java/org/apache/commons/io/file/PathUtils.java
index 0f1e01f..fa6ab88 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -729,15 +729,51 @@ public final class PathUtils {
* @since 2.8.0
*/
public static List<AclEntry> getAclEntryList(final Path sourcePath) throws IOException {
- final AclFileAttributeView fileAttributeView = Files.getFileAttributeView(sourcePath, AclFileAttributeView.class);
+ final AclFileAttributeView fileAttributeView = getAclFileAttributeView(sourcePath);
return fileAttributeView == null ? null : fileAttributeView.getAcl();
}
+ /**
+ * Shorthand for {@code Files.getFileAttributeView(path, AclFileAttributeView.class)}.
+ *
+ * @param path the path to the file.
+ * @param options how to handle symbolic links.
+ * @return a AclFileAttributeView, or {@code null} if the attribute view type is not available.
+ * @since 2.12.0
+ */
+ public static AclFileAttributeView getAclFileAttributeView(final Path path, final LinkOption... options) {
+ return Files.getFileAttributeView(path, AclFileAttributeView.class, options);
+ }
+
+ /**
+ * Shorthand for {@code Files.getFileAttributeView(path, DosFileAttributeView.class)}.
+ *
+ * @param path the path to the file.
+ * @param options how to handle symbolic links.
+ * @return a DosFileAttributeView, or {@code null} if the attribute view type is not available.
+ * @since 2.12.0
+ */
+ public static DosFileAttributeView getDosFileAttributeView(final Path path, final LinkOption... options) {
+ return Files.getFileAttributeView(path, DosFileAttributeView.class, options);
+ }
+
private static FileTime getLastModifiedTime(final Path path, final LinkOption... options) throws IOException {
return Files.getLastModifiedTime(Objects.requireNonNull(path, "path"), options);
}
/**
+ * Shorthand for {@code Files.getFileAttributeView(path, PosixFileAttributeView.class)}.
+ *
+ * @param path the path to the file.
+ * @param options how to handle symbolic links.
+ * @return a PosixFileAttributeView, or {@code null} if the attribute view type is not available.
+ * @since 2.12.0
+ */
+ public static PosixFileAttributeView getPosixFileAttributeView(final Path path, final LinkOption... options) {
+ return Files.getFileAttributeView(path, PosixFileAttributeView.class, options);
+ }
+
+ /**
* Gets a {@link Path} representing the system temporary directory.
*
* @return the system temporary directory.
@@ -915,6 +951,7 @@ public final class PathUtils {
return isOlder(file, FileTime.from(instant), options);
}
+
/**
* Tests if the given {@code Path} is older than the given time reference.
*
@@ -943,7 +980,6 @@ public final class PathUtils {
return isOlder(file, getLastModifiedTime(reference));
}
-
/**
* Tests whether the given {@code Path} is a regular file or not. Implemented as a null-safe delegate to
* {@code Files.isRegularFile(Path path, LinkOption... options)}.
@@ -1152,7 +1188,7 @@ public final class PathUtils {
*/
public static Path setReadOnly(final Path path, final boolean readOnly, final LinkOption... linkOptions) throws IOException {
final List<Exception> causeList = new ArrayList<>(2);
- final DosFileAttributeView fileAttributeView = Files.getFileAttributeView(path, DosFileAttributeView.class, linkOptions);
+ final DosFileAttributeView fileAttributeView = getDosFileAttributeView(path, linkOptions);
if (fileAttributeView != null) {
// Windows 10
try {
@@ -1163,7 +1199,7 @@ public final class PathUtils {
causeList.add(e);
}
}
- final PosixFileAttributeView posixFileAttributeView = Files.getFileAttributeView(path, PosixFileAttributeView.class, linkOptions);
+ final PosixFileAttributeView posixFileAttributeView = getPosixFileAttributeView(path, linkOptions);
if (posixFileAttributeView != null) {
// Not Windows 10
final PosixFileAttributes readAttributes = posixFileAttributeView.readAttributes();
diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
index a56736b..6e9f3dc 100644
--- a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
@@ -283,11 +283,11 @@ public class PathUtilsTest extends TestArguments {
PathUtils.setReadOnly(resolved, true);
assertEquals(true, Files.isReadable(resolved));
assertEquals(false, Files.isWritable(resolved));
- final DosFileAttributeView dosFileAttributeView = Files.getFileAttributeView(resolved, DosFileAttributeView.class);
+ final DosFileAttributeView dosFileAttributeView = PathUtils.getDosFileAttributeView(resolved);
if (dosFileAttributeView != null) {
assertTrue(dosFileAttributeView.readAttributes().isReadOnly());
}
- final PosixFileAttributeView posixFileAttributeView = Files.getFileAttributeView(resolved, PosixFileAttributeView.class);
+ final PosixFileAttributeView posixFileAttributeView = PathUtils.getPosixFileAttributeView(resolved);
if (posixFileAttributeView != null) {
// Not Windows
final Set<PosixFilePermission> permissions = posixFileAttributeView.readAttributes().permissions();