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 aac6917 Add PathUtils.isPosix(Path, LinkOption...). (#290) aac6917 is described below commit aac69176c55014d765cedc3949b87fddc4f12383 Author: Gary Gregory <garydgreg...@users.noreply.github.com> AuthorDate: Sat Oct 30 11:50:30 2021 -0400 Add PathUtils.isPosix(Path, LinkOption...). (#290) Add PathUtils.readAttributes(Path, Class<A>, LinkOption...). Co-authored-by: Gary Gregory <ggreg...@rocketsoftware.com> --- .../java/org/apache/commons/io/file/PathUtils.java | 35 ++++++++++++++++++++++ .../org/apache/commons/io/file/PathUtilsTest.java | 28 +++++++++++++++++ 2 files changed, 63 insertions(+) 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 fa6ab88..e5d08db 100644 --- a/src/main/java/org/apache/commons/io/file/PathUtils.java +++ b/src/main/java/org/apache/commons/io/file/PathUtils.java @@ -981,6 +981,41 @@ public final class PathUtils { } /** + * Tests whether the given path is on a POSIX file system. + * + * @param test The Path to test. + * @param options options indicating how to handle symbolic links. + * @return true if test is on a POSIX file system. + * @since 2.12.0 + */ + public static boolean isPosix(final Path test, final LinkOption... options) { + return readAttributes(test, PosixFileAttributes.class, options) != null; + } + + /** + * Calls {@link Files#readAttributes(Path, Class, LinkOption...)} but returns null instead of throwing + * {@link UnsupportedOperationException} or {@link IOException}. + * + * @param <A> The {@code BasicFileAttributes} type + * @param test The Path to test. + * @param type the {@code Class} of the file attributes required to read. + * @param options options indicating how to handle symbolic links. + * @return the file attributes. + * @since 2.12.0 + */ + public static <A extends BasicFileAttributes> A readAttributes(final Path test, final Class<A> type, final LinkOption... options) { + try { + return Files.readAttributes(test, type, options); + } catch (final UnsupportedOperationException e) { + // For example, on Windows. + return null; + } catch (final IOException e) { + // For example, when the path does not exist. + return null; + } + } + + /** * 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)}. * 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 7e3ed69..98ce81b 100644 --- a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java +++ b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java @@ -20,6 +20,7 @@ package org.apache.commons.io.file; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeFalse; @@ -35,6 +36,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.DosFileAttributeView; import java.nio.file.attribute.PosixFileAttributeView; +import java.nio.file.attribute.PosixFileAttributes; import java.nio.file.attribute.PosixFilePermission; import java.util.HashMap; import java.util.Iterator; @@ -187,6 +189,19 @@ public class PathUtilsTest extends TestArguments { } @Test + public void testIsPosix() throws IOException { + boolean isPosix; + try { + Files.getPosixFilePermissions(PathUtils.current()); + isPosix = true; + } catch (UnsupportedOperationException e) { + isPosix = false; + } + assertEquals(isPosix, PathUtils.isPosix(PathUtils.current())); + assertEquals(false, PathUtils.isPosix(Paths.get("does not.exist"))); + } + + @Test public void testIsRegularFile() throws IOException { assertFalse(PathUtils.isRegularFile(null)); @@ -238,6 +253,19 @@ public class PathUtilsTest extends TestArguments { } @Test + public void testReadAttributesPosix() throws IOException { + boolean isPosix; + try { + Files.getPosixFilePermissions(PathUtils.current()); + isPosix = true; + } catch (UnsupportedOperationException e) { + isPosix = false; + } + assertEquals(isPosix, PathUtils.readAttributes(PathUtils.current(), PosixFileAttributes.class) != null); + assertNull(PathUtils.readAttributes(Paths.get("does not.exist"), PosixFileAttributes.class)); + } + + @Test public void testReadStringEmptyFile() throws IOException { final Path path = Paths.get("src/test/resources/org/apache/commons/io/test-file-empty.bin"); assertEquals(StringUtils.EMPTY, PathUtils.readString(path, StandardCharsets.UTF_8));