|
24 | 24 |
|
25 | 25 | package hudson.model; |
26 | 26 |
|
| 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 | + |
27 | 32 | /** |
28 | 33 | * Utility methods for Windows specific details on tests. |
29 | 34 | * |
30 | 35 | * @author Mark Waite |
31 | 36 | */ |
32 | 37 | 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(); |
36 | 74 | } |
37 | 75 | } |
0 commit comments