Skip to content

Commit 6e80cbe

Browse files
committed
Address comments
1 parent cc08b2e commit 6e80cbe

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/main/java/com/google/devtools/build/lib/vfs/AbstractFileSystem.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
@ThreadSafe
4141
public abstract class AbstractFileSystem extends FileSystem {
4242

43-
protected static final String ERR_PERMISSION_DENIED = " (Permission denied)";
4443
protected static final Profiler profiler = Profiler.instance();
4544

4645
public AbstractFileSystem(DigestHashFunction digestFunction) {

src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static java.nio.charset.StandardCharsets.ISO_8859_1;
2020

2121
import com.google.common.base.Preconditions;
22+
import com.google.common.collect.ImmutableMap;
2223
import com.google.common.collect.Lists;
2324
import com.google.common.io.ByteSource;
2425
import com.google.common.io.CharStreams;
@@ -41,6 +42,7 @@
4142
import java.util.Collection;
4243
import java.util.Iterator;
4344
import java.util.List;
45+
import java.util.Map;
4446
import javax.annotation.Nullable;
4547

4648
/** This interface models a file system. */
@@ -812,6 +814,25 @@ public java.nio.file.Path getNioPath(PathFragment path) {
812814
"getNioPath() not supported for " + getClass().getName());
813815
}
814816

817+
// Mapping from FileSystemException reason strings on various platforms to the corresponding Unix
818+
// error message that Bazel's own filesystem implementations produce. Bazel forces the root locale
819+
// for the JVM, so the error messages should be stable per OS.
820+
// This map is best-effort and almost certainly incomplete, especially on Windows.
821+
private static final Map<String, String> reasonToUnixError =
822+
ImmutableMap.of(
823+
// Unix
824+
"Is a directory",
825+
ERR_IS_DIRECTORY,
826+
"Not a directory",
827+
ERR_NOT_A_DIRECTORY,
828+
"Directory not empty",
829+
ERR_DIRECTORY_NOT_EMPTY,
830+
// Windows
831+
// https://github.com/bazelbuild/bazel/pull/27458#discussion_r2478544279
832+
// https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-
833+
"The directory is not empty.",
834+
ERR_DIRECTORY_NOT_EMPTY);
835+
815836
/**
816837
* Translates common java.nio.file IOExceptions into the equivalent java.io IOExceptions with
817838
* consistent error messages.
@@ -845,18 +866,12 @@ protected static IOException translateNioToIoException(PathFragment path, IOExce
845866
case NotDirectoryException unused -> new IOException(prefix + ERR_NOT_A_DIRECTORY, e);
846867
case NotLinkException unused -> new NotASymlinkException(path, e);
847868
// Rewrite exception messages to be identical to the ones produced by the native Unix
848-
// filesystem implementation. Bazel forces the root locale for the JVM, so the error messages
849-
// should be stable. Some of the exceptions caught above can also appear as untyped
850-
// FileSystemExceptions, so we need to catch those as well.
851-
case FileSystemException unused
852-
when fileSystemException.getReason().equals("Is a directory") ->
853-
new IOException(prefix + ERR_IS_DIRECTORY, e);
854-
case FileSystemException unused
855-
when fileSystemException.getReason().equals("Not a directory") ->
856-
new IOException(prefix + ERR_NOT_A_DIRECTORY, e);
857-
case FileSystemException unused
858-
when fileSystemException.getReason().equals("Directory not empty") ->
859-
new IOException(prefix + ERR_DIRECTORY_NOT_EMPTY, e);
869+
// filesystem implementation. Some of the exceptions caught above can also appear as untyped
870+
// FileSystemExceptions and are thus included in reasonToUnixError.
871+
case FileSystemException fse -> {
872+
String unixError = reasonToUnixError.get(fse.getReason());
873+
yield unixError != null ? new IOException(prefix + unixError, e) : fileSystemException;
874+
}
860875
default -> fileSystemException;
861876
};
862877
}

0 commit comments

Comments
 (0)