Skip to content

Commit 082fc42

Browse files
committed
Create a symbolic link on Windows to confirm they are supported
1 parent 0750dd8 commit 082fc42

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

test/src/test/java/hudson/model/WindowsUtil.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,52 @@
2424

2525
package hudson.model;
2626

27+
import java.io.IOException;
28+
import java.nio.file.Files;
29+
import java.nio.file.Path;
30+
import java.util.concurrent.atomic.AtomicReference;
31+
2732
/**
2833
* Utility methods for Windows specific details on tests.
2934
*
3035
* @author Mark Waite
3136
*/
3237
public class WindowsUtil {
33-
public static boolean isWindowsSymlinkSupported() {
34-
// TODO: Replace with more accurate check for symlink support
35-
return false;
38+
39+
private static final AtomicReference<Boolean> symlinkSupported = new AtomicReference<>();
40+
41+
/**
42+
* Returns true if Windows allows a symbolic link to be created.
43+
*
44+
* @return true if Windows allows a symbolic link to be created.
45+
* @throws IOException if creation or removal of temporary files fails
46+
*/
47+
public static boolean isWindowsSymlinkSupported() throws IOException {
48+
// Fast path, don't acquire unnecessary lock
49+
Boolean supported = symlinkSupported.get();
50+
if (supported != null) {
51+
return supported;
52+
}
53+
synchronized (WindowsUtil.class) {
54+
supported = symlinkSupported.get();
55+
if (supported != null) {
56+
return supported;
57+
}
58+
Path tempDir = Files.createTempDirectory("symlink-check");
59+
Path target = Files.createFile(tempDir.resolve("target.txt"));
60+
Path link = tempDir.resolve("link.txt");
61+
62+
try {
63+
Files.createSymbolicLink(link, target);
64+
Files.delete(link);
65+
symlinkSupported.set(true);
66+
} catch (IOException | UnsupportedOperationException uoe) {
67+
symlinkSupported.set(false);
68+
} finally {
69+
Files.delete(target);
70+
Files.delete(tempDir);
71+
}
72+
}
73+
return symlinkSupported.get();
3674
}
3775
}

0 commit comments

Comments
 (0)