Skip to content

Commit 4eb6418

Browse files
committed
Avoid UnsupportedOperationException when setting POSIX permissions on non-POSIX filesystems
1 parent c86b378 commit 4eb6418

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

gradle-jdks/src/main/java/com/palantir/gradle/jdks/GradleJdksConfigsUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ public static void writeConfigurationFile(Path pathFile, String content) {
100100
}
101101

102102
public static void setExecuteFilePermissions(Path path) {
103+
if (!path.getFileSystem().supportedFileAttributeViews().contains("posix")) {
104+
return;
105+
}
103106
try {
104107
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(path);
105108
perms.addAll(Set.of(

gradle-jdks/src/test/groovy/com/palantir/gradle/jdks/PosixPermissionsWindowsTest.groovy

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,33 @@ package com.palantir.gradle.jdks
22

33
import spock.lang.Specification
44
import spock.lang.TempDir
5-
65
import java.nio.file.Files
76
import java.nio.file.Path
7+
import java.nio.file.attribute.PosixFilePermission
88

99
class PosixPermissionsWindowsTest extends Specification {
1010

1111
@TempDir Path tempDir
1212

13-
def 'setExecuteFilePermissions crashes on Windows when POSIX is not supported'() {
13+
def 'setExecuteFilePermissions works on both POSIX and non-POSIX filesystems'() {
1414
given:
1515
Path p = Files.createFile(tempDir.resolve("x.sh"))
16+
boolean isPosixSupported = Files.getFileStore(p).supportsFileAttributeView("posix")
1617

1718
when:
1819
GradleJdksConfigsUtils.setExecuteFilePermissions(p)
1920

2021
then:
21-
thrown(RuntimeException)
22+
noExceptionThrown()
23+
24+
and:
25+
if (isPosixSupported) {
26+
def perms = Files.getPosixFilePermissions(p)
27+
assert perms.contains(PosixFilePermission.OWNER_EXECUTE)
28+
assert perms.contains(PosixFilePermission.GROUP_EXECUTE)
29+
assert perms.contains(PosixFilePermission.OTHERS_EXECUTE)
30+
} else {
31+
assert Files.isExecutable(p) || p.toFile().canExecute()
32+
}
2233
}
23-
}
34+
}

0 commit comments

Comments
 (0)