From 6f1e31a9aa4ad80e402b9a441d4129c7f1bd6fb7 Mon Sep 17 00:00:00 2001 From: László Csomor Date: Fri, 27 Jan 2017 11:01:41 +0000 Subject: Bazel client: platform-dependent `strerror` Move `strerror` calls into errors_. We have to get rid of direct `errno` reading too, because it doesn't work on Windows native. Fixes https://github.com/bazelbuild/bazel/issues/2411 -- Change-Id: I69ff502487d698aa9e9147f02fd0bc5253e94e64 Reviewed-on: https://cr.bazel.build/8490 PiperOrigin-RevId: 145777524 MOS_MIGRATED_REVID=145777524 --- src/main/cpp/util/BUILD | 7 +++++- src/main/cpp/util/errors.cc | 7 +++--- src/main/cpp/util/errors.h | 6 +++++ src/main/cpp/util/errors_posix.cc | 33 ++++++++++++++++++++++++++++ src/main/cpp/util/errors_windows.cc | 44 +++++++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 src/main/cpp/util/errors_posix.cc create mode 100644 src/main/cpp/util/errors_windows.cc (limited to 'src/main/cpp/util') diff --git a/src/main/cpp/util/BUILD b/src/main/cpp/util/BUILD index a14034473a..b044d19537 100644 --- a/src/main/cpp/util/BUILD +++ b/src/main/cpp/util/BUILD @@ -61,8 +61,13 @@ cc_library( cc_library( name = "errors", - srcs = ["errors.cc"], + srcs = ["errors.cc"] + select({ + "//src:windows": ["errors_windows.cc"], + "//src:windows_msvc": ["errors_windows.cc"], + "//conditions:default": ["errors_posix.cc"], + }), hdrs = ["errors.h"], + visibility = ["//src/main/cpp:__subpackages__"], deps = [":port"], ) diff --git a/src/main/cpp/util/errors.cc b/src/main/cpp/util/errors.cc index 8497545e0b..112f293bf1 100644 --- a/src/main/cpp/util/errors.cc +++ b/src/main/cpp/util/errors.cc @@ -14,10 +14,9 @@ #include "src/main/cpp/util/errors.h" -#include #include -#include #include +#include #include namespace blaze_util { @@ -37,7 +36,7 @@ void pdie(const int exit_status, const char *format, ...) { va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap); - fprintf(stderr, ": %s\n", strerror(errno)); + fprintf(stderr, ": %s\n", GetLastErrorString().c_str()); exit(exit_status); } @@ -47,7 +46,7 @@ void PrintError(const char *format, ...) { va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap); - fprintf(stderr, ": %s\n", strerror(errno)); + fprintf(stderr, ": %s\n", GetLastErrorString().c_str()); } } // namespace blaze_util diff --git a/src/main/cpp/util/errors.h b/src/main/cpp/util/errors.h index 833a6ee473..e7d93068f4 100644 --- a/src/main/cpp/util/errors.h +++ b/src/main/cpp/util/errors.h @@ -18,6 +18,7 @@ #ifndef BAZEL_SRC_MAIN_CPP_UTIL_ERRORS_H_ #define BAZEL_SRC_MAIN_CPP_UTIL_ERRORS_H_ +#include #include "src/main/cpp/util/port.h" namespace blaze_util { @@ -30,6 +31,11 @@ void pdie(const int exit_status, const char *format, ...) ATTRIBUTE_NORETURN PRINTF_ATTRIBUTE(2, 3); void PrintError(const char *format, ...) PRINTF_ATTRIBUTE(1, 2); +// Returns the last error as a platform-specific error message. +// The string will also contain the platform-specific error code itself +// (which is `errno` on Linux/Darwin, and `GetLastError()` on Windows). +std::string GetLastErrorString(); + } // namespace blaze_util #endif // BAZEL_SRC_MAIN_CPP_UTIL_ERRORS_H_ diff --git a/src/main/cpp/util/errors_posix.cc b/src/main/cpp/util/errors_posix.cc new file mode 100644 index 0000000000..a5e449e424 --- /dev/null +++ b/src/main/cpp/util/errors_posix.cc @@ -0,0 +1,33 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include // strerror +#include +#include +#include "src/main/cpp/util/errors.h" + +namespace blaze_util { + +using std::string; +using std::stringstream; + +string GetLastErrorString() { + int saved_errno = errno; + stringstream result; + result << "(error: " << saved_errno << "): " << strerror(saved_errno); + return result.str(); +} + +} // namespace blaze_util diff --git a/src/main/cpp/util/errors_windows.cc b/src/main/cpp/util/errors_windows.cc new file mode 100644 index 0000000000..b77ae21b26 --- /dev/null +++ b/src/main/cpp/util/errors_windows.cc @@ -0,0 +1,44 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include "src/main/cpp/util/errors.h" + +namespace blaze_util { + +using std::string; +using std::stringstream; + +string GetLastErrorString() { + DWORD last_error = ::GetLastError(); + if (last_error == 0) { + return string(); + } + + char* message_buffer; + size_t size = FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, last_error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPSTR)&message_buffer, 0, NULL); + + stringstream result; + result << "(error: " << last_error << "): " << message_buffer; + LocalFree(message_buffer); + return result.str(); +} + +} // namespace blaze_util -- cgit v1.2.3