Skip to content

Commit aef5f5b

Browse files
Copilotslachiewicz
andauthored
Handle IOException when retrieving file ownership on WSL2 network drives (#158)
* Fix WSL2 file ownership IOException handling --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: slachiewicz <6705942+slachiewicz@users.noreply.github.com>
1 parent c582ac0 commit aef5f5b

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/main/java/org/codehaus/plexus/components/io/attributes/FileAttributes.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,16 @@ public FileAttributes(@Nonnull Path path, boolean followLinks) throws IOExceptio
160160
this.lastModifiedTime = (FileTime) attrs.get("lastModifiedTime");
161161
}
162162

163-
private static String getPrincipalName(Path path, String attribute) throws IOException {
164-
Object owner = Files.getAttribute(path, attribute, LinkOption.NOFOLLOW_LINKS);
165-
return ((Principal) owner).getName();
163+
@Nullable
164+
private static String getPrincipalName(Path path, String attribute) {
165+
try {
166+
Object owner = Files.getAttribute(path, attribute, LinkOption.NOFOLLOW_LINKS);
167+
return ((Principal) owner).getName();
168+
} catch (IOException e) {
169+
// Some file systems (e.g., WSL2 mapped network drives) don't provide ownership information
170+
// Return null instead of propagating the exception
171+
return null;
172+
}
166173
}
167174

168175
public FileAttributes(

src/test/java/org/codehaus/plexus/components/io/attributes/FileAttributesTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818

1919
import java.io.File;
20+
import java.io.IOException;
21+
import java.nio.file.Files;
2022

2123
import org.junit.jupiter.api.Test;
2224
import org.junit.jupiter.api.condition.DisabledOnOs;
@@ -35,4 +37,20 @@ void testGetPosixFileAttributes() throws Exception {
3537
PlexusIoResourceAttributes fa = new FileAttributes(file);
3638
assertNotNull(fa);
3739
}
40+
41+
@Test
42+
void testFileAttributesHandlesIOException() throws IOException {
43+
// Test that FileAttributes can be constructed for a regular file
44+
// even if ownership information is not available (e.g., WSL2 mapped network drives)
45+
File tempFile = Files.createTempFile("plexus-io-test", ".tmp").toFile();
46+
try {
47+
// This should not throw even if ownership info is unavailable
48+
PlexusIoResourceAttributes fa = new FileAttributes(tempFile);
49+
assertNotNull(fa);
50+
// The attributes object should be usable even if userName/groupName are null
51+
assertNotNull(fa.toString());
52+
} finally {
53+
tempFile.delete();
54+
}
55+
}
3856
}

0 commit comments

Comments
 (0)