Skip to content

Commit

Permalink
Correctly handle path encoding in DiskCacheLock.
Browse files Browse the repository at this point in the history
The earlier fix in 6922734 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
  • Loading branch information
tjgq authored and copybara-github committed Oct 1, 2024
1 parent 509f79f commit 5997fde
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down

0 comments on commit 5997fde

Please sign in to comment.