From 5997fdebe5c91e382148e67197d82e35c0034858 Mon Sep 17 00:00:00 2001 From: Tiago Quelhas Date: Tue, 1 Oct 2024 03:43:12 -0700 Subject: [PATCH] Correctly handle path encoding in DiskCacheLock. The earlier fix in 6922734a7bd6cc504cfc3390f4fa30e9438d1edd is wrong on Linux, where the native encoding is indicated by the sun.jnu.encoding property, and could be something other than UTF-8. Closes #23817. PiperOrigin-RevId: 680947021 Change-Id: Idd52c1615de6edf423f59b62e364f0b7131f2d47 --- .../devtools/build/lib/remote/disk/DiskCacheLock.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheLock.java b/src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheLock.java index ed086122e9255e..5fea966ff44d2c 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheLock.java +++ b/src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheLock.java @@ -13,13 +13,14 @@ // limitations under the License. package com.google.devtools.build.lib.remote.disk; -import static com.google.devtools.build.lib.util.StringUtil.reencodeInternalToExternal; +import static java.nio.charset.StandardCharsets.ISO_8859_1; import com.google.common.annotations.VisibleForTesting; import com.google.devtools.build.lib.vfs.Path; import java.io.IOException; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; +import java.nio.charset.Charset; import java.nio.file.StandardOpenOption; /** Manages shared or exclusive access to the disk cache by concurrent processes. */ @@ -59,7 +60,7 @@ private static DiskCacheLock get(Path path, boolean shared) throws IOException { FileChannel channel = FileChannel.open( // Correctly handle non-ASCII paths by converting from the internal string encoding. - java.nio.file.Path.of(reencodeInternalToExternal(path.getPathString())), + java.nio.file.Path.of(getPathStringForJavaIo(path)), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE); @@ -71,6 +72,12 @@ private static DiskCacheLock get(Path path, boolean shared) throws IOException { return new DiskCacheLock(channel, lock); } + private static String getPathStringForJavaIo(Path path) { + return new String( + path.getPathString().getBytes(ISO_8859_1), + Charset.forName(System.getProperty("sun.jnu.encoding"), ISO_8859_1)); + } + @VisibleForTesting boolean isShared() { return lock.isShared();