diff options
author | 2017-02-15 16:08:45 +0000 | |
---|---|---|
committer | 2017-02-15 19:19:31 +0000 | |
commit | 81f92feeb8ae8e22b62edf90f6b7d44baaec2fbd (patch) | |
tree | 31d0d3d3ce5aa6ceb919d212538b3b5a658f62fc /src/main/java/com/google/devtools/build/lib/vfs | |
parent | 3635539af5cce8c8d3795f745c678c890b45e9b5 (diff) |
Windows: use JNI CreateJunction in Java code
Use the new CreateJunction in the Windows JNI code
every time we need to create junctions. This means
updating WindowsFileOperations and related tests.
Add test for WindowsFileSystem.createSymbolicLink.
See https://github.com/bazelbuild/bazel/issues/2238
--
Change-Id: I5827e2e70e8e147f5f102fabf95fa9a148b3bcdc
Reviewed-on: https://cr.bazel.build/8896
PiperOrigin-RevId: 147598107
MOS_MIGRATED_REVID=147598107
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/vfs')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/vfs/WindowsFileSystem.java | 43 |
1 files changed, 9 insertions, 34 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/WindowsFileSystem.java b/src/main/java/com/google/devtools/build/lib/vfs/WindowsFileSystem.java index 7a144f5c3e..4167d817e4 100644 --- a/src/main/java/com/google/devtools/build/lib/vfs/WindowsFileSystem.java +++ b/src/main/java/com/google/devtools/build/lib/vfs/WindowsFileSystem.java @@ -327,20 +327,17 @@ public class WindowsFileSystem extends JavaIoFileSystem { @Override protected void createSymbolicLink(Path linkPath, PathFragment targetFragment) throws IOException { - // TODO(lberki): Add some JNI to create hard links/junctions instead of calling out to - // cmd.exe - File file = getIoFile(linkPath); + Path targetPath = + targetFragment.isAbsolute() + ? getPath(targetFragment) + : linkPath.getParentDirectory().getRelative(targetFragment); try { - File targetFile = new File(targetFragment.getPathString()); - if (targetFile.isDirectory()) { - createDirectoryJunction(targetFile, file); + java.nio.file.Path link = getIoFile(linkPath).toPath(); + java.nio.file.Path target = getIoFile(targetPath).toPath(); + if (target.toFile().isDirectory()) { + WindowsFileOperations.createJunction(link.toString(), target.toString()); } else { - if (targetFile.isAbsolute()) { - Files.copy(targetFile.toPath(), file.toPath()); - } else { - // When targetFile is a relative path to linkPath, resolve it to an absolute path first. - Files.copy(file.toPath().getParent().resolve(targetFile.toPath()), file.toPath()); - } + Files.copy(target, link); } } catch (java.nio.file.FileAlreadyExistsException e) { throw new IOException(linkPath + ERR_FILE_EXISTS); @@ -361,28 +358,6 @@ public class WindowsFileSystem extends JavaIoFileSystem { return false; } - private void createDirectoryJunction(File sourceDirectory, File targetPath) throws IOException { - StringBuilder cl = new StringBuilder("cmd.exe /c "); - cl.append("mklink /J "); - cl.append('"'); - cl.append(targetPath.getAbsolutePath()); - cl.append('"'); - cl.append(' '); - cl.append('"'); - cl.append(sourceDirectory.getAbsolutePath()); - cl.append('"'); - Process process = Runtime.getRuntime().exec(cl.toString()); - try { - process.waitFor(); - if (process.exitValue() != 0) { - throw new IOException("Command failed " + cl); - } - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new IOException("Command failed ", e); - } - } - @Override protected boolean fileIsSymbolicLink(File file) { try { |