Skip to content

Commit 2f6284a

Browse files
author
Gary Gregory
committed
Add PathUtiFiles.getFileAttributeView() shorthands.
- PathUtils.getAclFileAttributeView(Path, LinkOption...) - PathUtils.getDosFileAttributeView(Path, LinkOption...) - PathUtils.getPosixFileAttributeView(Path, LinkOption...)
1 parent 0c00862 commit 2f6284a

3 files changed

Lines changed: 48 additions & 6 deletions

File tree

src/changes/changes.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ The <action> type attribute can be add,update,fix,remove.
244244
<action dev="ggregory" type="add" due-to="Gary Gregory">
245245
Add PathUtils.writeString(Path, CharSequence, Charset, OpenOption...).
246246
</action>
247+
<action dev="ggregory" type="add" due-to="Gary Gregory">
248+
Add PathUtiFiles.getFileAttributeView() shorthands:
249+
- PathUtils.getAclFileAttributeView(Path, LinkOption...)
250+
- PathUtils.getDosFileAttributeView(Path, LinkOption...)
251+
- PathUtils.getPosixFileAttributeView(Path, LinkOption...)
252+
</action>
247253
<!-- UPDATE -->
248254
<action dev="ggregory" type="add" due-to="Gary Gregory">
249255
Update FileEntry to use FileTime instead of long for file time stamps.

src/main/java/org/apache/commons/io/file/PathUtils.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -729,14 +729,50 @@ private static <R, A> R filterPaths(final PathFilter filter, final Stream<Path>
729729
* @since 2.8.0
730730
*/
731731
public static List<AclEntry> getAclEntryList(final Path sourcePath) throws IOException {
732-
final AclFileAttributeView fileAttributeView = Files.getFileAttributeView(sourcePath, AclFileAttributeView.class);
732+
final AclFileAttributeView fileAttributeView = getAclFileAttributeView(sourcePath);
733733
return fileAttributeView == null ? null : fileAttributeView.getAcl();
734734
}
735735

736+
/**
737+
* Shorthand for {@code Files.getFileAttributeView(path, AclFileAttributeView.class)}.
738+
*
739+
* @param path the path to the file.
740+
* @param options how to handle symbolic links.
741+
* @return a AclFileAttributeView, or {@code null} if the attribute view type is not available.
742+
* @since 2.12.0
743+
*/
744+
public static AclFileAttributeView getAclFileAttributeView(final Path path, final LinkOption... options) {
745+
return Files.getFileAttributeView(path, AclFileAttributeView.class, options);
746+
}
747+
748+
/**
749+
* Shorthand for {@code Files.getFileAttributeView(path, DosFileAttributeView.class)}.
750+
*
751+
* @param path the path to the file.
752+
* @param options how to handle symbolic links.
753+
* @return a DosFileAttributeView, or {@code null} if the attribute view type is not available.
754+
* @since 2.12.0
755+
*/
756+
public static DosFileAttributeView getDosFileAttributeView(final Path path, final LinkOption... options) {
757+
return Files.getFileAttributeView(path, DosFileAttributeView.class, options);
758+
}
759+
736760
private static FileTime getLastModifiedTime(final Path path, final LinkOption... options) throws IOException {
737761
return Files.getLastModifiedTime(Objects.requireNonNull(path, "path"), options);
738762
}
739763

764+
/**
765+
* Shorthand for {@code Files.getFileAttributeView(path, PosixFileAttributeView.class)}.
766+
*
767+
* @param path the path to the file.
768+
* @param options how to handle symbolic links.
769+
* @return a PosixFileAttributeView, or {@code null} if the attribute view type is not available.
770+
* @since 2.12.0
771+
*/
772+
public static PosixFileAttributeView getPosixFileAttributeView(final Path path, final LinkOption... options) {
773+
return Files.getFileAttributeView(path, PosixFileAttributeView.class, options);
774+
}
775+
740776
/**
741777
* Gets a {@link Path} representing the system temporary directory.
742778
*
@@ -915,6 +951,7 @@ public static boolean isOlder(final Path file, final Instant instant, final Link
915951
return isOlder(file, FileTime.from(instant), options);
916952
}
917953

954+
918955
/**
919956
* Tests if the given {@code Path} is older than the given time reference.
920957
*
@@ -943,7 +980,6 @@ public static boolean isOlder(final Path file, final Path reference) throws IOEx
943980
return isOlder(file, getLastModifiedTime(reference));
944981
}
945982

946-
947983
/**
948984
* Tests whether the given {@code Path} is a regular file or not. Implemented as a null-safe delegate to
949985
* {@code Files.isRegularFile(Path path, LinkOption... options)}.
@@ -1152,7 +1188,7 @@ public static void setLastModifiedTime(final Path sourceFile, final Path targetF
11521188
*/
11531189
public static Path setReadOnly(final Path path, final boolean readOnly, final LinkOption... linkOptions) throws IOException {
11541190
final List<Exception> causeList = new ArrayList<>(2);
1155-
final DosFileAttributeView fileAttributeView = Files.getFileAttributeView(path, DosFileAttributeView.class, linkOptions);
1191+
final DosFileAttributeView fileAttributeView = getDosFileAttributeView(path, linkOptions);
11561192
if (fileAttributeView != null) {
11571193
// Windows 10
11581194
try {
@@ -1163,7 +1199,7 @@ public static Path setReadOnly(final Path path, final boolean readOnly, final Li
11631199
causeList.add(e);
11641200
}
11651201
}
1166-
final PosixFileAttributeView posixFileAttributeView = Files.getFileAttributeView(path, PosixFileAttributeView.class, linkOptions);
1202+
final PosixFileAttributeView posixFileAttributeView = getPosixFileAttributeView(path, linkOptions);
11671203
if (posixFileAttributeView != null) {
11681204
// Not Windows 10
11691205
final PosixFileAttributes readAttributes = posixFileAttributeView.readAttributes();

src/test/java/org/apache/commons/io/file/PathUtilsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,11 @@ public void testSetReadOnlyFile() throws IOException {
283283
PathUtils.setReadOnly(resolved, true);
284284
assertEquals(true, Files.isReadable(resolved));
285285
assertEquals(false, Files.isWritable(resolved));
286-
final DosFileAttributeView dosFileAttributeView = Files.getFileAttributeView(resolved, DosFileAttributeView.class);
286+
final DosFileAttributeView dosFileAttributeView = PathUtils.getDosFileAttributeView(resolved);
287287
if (dosFileAttributeView != null) {
288288
assertTrue(dosFileAttributeView.readAttributes().isReadOnly());
289289
}
290-
final PosixFileAttributeView posixFileAttributeView = Files.getFileAttributeView(resolved, PosixFileAttributeView.class);
290+
final PosixFileAttributeView posixFileAttributeView = PathUtils.getPosixFileAttributeView(resolved);
291291
if (posixFileAttributeView != null) {
292292
// Not Windows
293293
final Set<PosixFilePermission> permissions = posixFileAttributeView.readAttributes().permissions();

0 commit comments

Comments
 (0)