From c03b6454556ba433f5d8800c0e9f80bac925d07e Mon Sep 17 00:00:00 2001 From: Alexander Golovlev Date: Tue, 22 Oct 2024 16:06:53 -0700 Subject: [PATCH] Partial fix for bug with creating a junction to file instead of symlink This is a partial fix for issue #21747 for the `--windows_enable_symlinks` case. The fix was suggested in discussion of this issue. This resolves the problem with creating a junction if the target path doesn't exist for those who have Windows symlinks enabled, until a complete solution is provided. Closes #24051. PiperOrigin-RevId: 688724224 Change-Id: Ie44f7834af5fd35ab57961e6012b9f336c25d606 --- .../build/lib/windows/WindowsFileSystem.java | 14 +++++++------- src/main/native/windows/file.cc | 3 ++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/windows/WindowsFileSystem.java b/src/main/java/com/google/devtools/build/lib/windows/WindowsFileSystem.java index 7edea00bc173ed..9c351785872e1a 100644 --- a/src/main/java/com/google/devtools/build/lib/windows/WindowsFileSystem.java +++ b/src/main/java/com/google/devtools/build/lib/windows/WindowsFileSystem.java @@ -79,15 +79,15 @@ protected void createSymbolicLink(PathFragment linkPath, PathFragment targetFrag try { java.nio.file.Path link = getIoFile(linkPath).toPath(); java.nio.file.Path target = getIoFile(targetPath).toPath(); - // Still Create a dangling junction if the target doesn't exist. - if (!target.toFile().exists() || target.toFile().isDirectory()) { + if (target.toFile().isDirectory()) { + WindowsFileOperations.createJunction(link.toString(), target.toString()); + } else if (createSymbolicLinks) { + WindowsFileOperations.createSymlink(link.toString(), target.toString()); + } else if (!target.toFile().exists()) { + // Still Create a dangling junction if the target doesn't exist. WindowsFileOperations.createJunction(link.toString(), target.toString()); } else { - if (createSymbolicLinks) { - WindowsFileOperations.createSymlink(link.toString(), target.toString()); - } else { - Files.copy(target, link); - } + Files.copy(target, link); } } catch (java.nio.file.FileAlreadyExistsException e) { throw new IOException(linkPath + ERR_FILE_EXISTS, e); diff --git a/src/main/native/windows/file.cc b/src/main/native/windows/file.cc index f20831e09bcfff..70809bde048900 100644 --- a/src/main/native/windows/file.cc +++ b/src/main/native/windows/file.cc @@ -505,7 +505,8 @@ int CreateSymlink(const wstring& symlink_name, const wstring& symlink_target, const wstring target = AddUncPrefixMaybe(symlink_target); DWORD attrs = GetFileAttributesW(target.c_str()); - if (attrs & FILE_ATTRIBUTE_DIRECTORY) { + if ((attrs != INVALID_FILE_ATTRIBUTES) && + (attrs & FILE_ATTRIBUTE_DIRECTORY)) { // Instead of creating a symlink to a directory use a Junction. return CreateSymlinkResult::kTargetIsDirectory; }