diff --git a/src/main/java/org/codehaus/plexus/components/io/attributes/FileAttributes.java b/src/main/java/org/codehaus/plexus/components/io/attributes/FileAttributes.java index d530790..214387c 100644 --- a/src/main/java/org/codehaus/plexus/components/io/attributes/FileAttributes.java +++ b/src/main/java/org/codehaus/plexus/components/io/attributes/FileAttributes.java @@ -160,9 +160,16 @@ public FileAttributes(@Nonnull Path path, boolean followLinks) throws IOExceptio this.lastModifiedTime = (FileTime) attrs.get("lastModifiedTime"); } - private static String getPrincipalName(Path path, String attribute) throws IOException { - Object owner = Files.getAttribute(path, attribute, LinkOption.NOFOLLOW_LINKS); - return ((Principal) owner).getName(); + @Nullable + private static String getPrincipalName(Path path, String attribute) { + try { + Object owner = Files.getAttribute(path, attribute, LinkOption.NOFOLLOW_LINKS); + return ((Principal) owner).getName(); + } catch (IOException e) { + // Some file systems (e.g., WSL2 mapped network drives) don't provide ownership information + // Return null instead of propagating the exception + return null; + } } public FileAttributes( diff --git a/src/test/java/org/codehaus/plexus/components/io/attributes/FileAttributesTest.java b/src/test/java/org/codehaus/plexus/components/io/attributes/FileAttributesTest.java index 3449f76..c77e09c 100644 --- a/src/test/java/org/codehaus/plexus/components/io/attributes/FileAttributesTest.java +++ b/src/test/java/org/codehaus/plexus/components/io/attributes/FileAttributesTest.java @@ -17,6 +17,8 @@ */ import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledOnOs; @@ -35,4 +37,20 @@ void testGetPosixFileAttributes() throws Exception { PlexusIoResourceAttributes fa = new FileAttributes(file); assertNotNull(fa); } + + @Test + void testFileAttributesHandlesIOException() throws IOException { + // Test that FileAttributes can be constructed for a regular file + // even if ownership information is not available (e.g., WSL2 mapped network drives) + File tempFile = Files.createTempFile("plexus-io-test", ".tmp").toFile(); + try { + // This should not throw even if ownership info is unavailable + PlexusIoResourceAttributes fa = new FileAttributes(tempFile); + assertNotNull(fa); + // The attributes object should be usable even if userName/groupName are null + assertNotNull(fa.toString()); + } finally { + tempFile.delete(); + } + } }