Remove pdie.
pdie and die are pretty similar, pdie just adds the errno string or equivalent from GetLastErrorString(). Make this explicit. This makes message formatting more clear in preparation for moving these all to BAZEL_LOG.
RELNOTES: None.
PiperOrigin-RevId: 190957255
diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc
index eb555de..9c72fb5 100644
--- a/src/main/cpp/blaze.cc
+++ b/src/main/cpp/blaze.cc
@@ -71,7 +71,7 @@
#include "src/main/protobuf/command_server.grpc.pb.h"
using blaze_util::die;
-using blaze_util::pdie;
+using blaze_util::GetLastErrorString;
namespace blaze {
@@ -363,18 +363,17 @@
devtools_ijar::ZipExtractor::Create(self_path.c_str(), &processor));
if (extractor.get() == NULL) {
die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "\nFailed to open %s as a zip file: %s",
- globals->options->product_name.c_str(),
- blaze_util::GetLastErrorString().c_str());
+ "Failed to open %s as a zip file: %s",
+ globals->options->product_name.c_str(), GetLastErrorString().c_str());
}
if (extractor->ProcessAll() < 0) {
die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "\nFailed to extract install_base_key: %s", extractor->GetError());
+ "Failed to extract install_base_key: %s", extractor->GetError());
}
if (globals->install_md5.empty()) {
die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "\nFailed to find install_base_key's in zip file");
+ "Failed to find install_base_key's in zip file");
}
}
@@ -635,8 +634,9 @@
static void GoToWorkspace(const WorkspaceLayout *workspace_layout) {
if (workspace_layout->InWorkspace(globals->workspace) &&
!blaze_util::ChangeDirectory(globals->workspace)) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "changing directory into %s failed",
- globals->workspace.c_str());
+ die(blaze_exit_code::INTERNAL_ERROR,
+ "changing directory into %s failed: %s", globals->workspace.c_str(),
+ GetLastErrorString().c_str());
}
}
@@ -717,21 +717,23 @@
string exe =
globals->options->GetExe(globals->jvm_path, globals->ServerJarPath());
ExecuteProgram(exe, jvm_args_vector);
- pdie(blaze_exit_code::INTERNAL_ERROR, "execv of '%s' failed", exe.c_str());
+ die(blaze_exit_code::INTERNAL_ERROR, "execv of '%s' failed: %s", exe.c_str(),
+ GetLastErrorString().c_str());
}
static void WriteFileToStderrOrDie(const char *file_name) {
FILE *fp = fopen(file_name, "r");
if (fp == NULL) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "opening %s failed",
- file_name);
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "opening %s failed: %s",
+ file_name, GetLastErrorString().c_str());
}
char buffer[255];
int num_read;
while ((num_read = fread(buffer, 1, sizeof buffer, fp)) > 0) {
if (ferror(fp)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "failed to read from '%s'", file_name);
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "failed to read from '%s': %s", file_name,
+ GetLastErrorString().c_str());
}
fwrite(buffer, 1, num_read, stderr);
}
@@ -769,8 +771,9 @@
// The server dir has the socket, so we don't allow access by other
// users.
if (!blaze_util::MakeDirectories(server_dir, 0700)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "server directory '%s' could not be created", server_dir.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "server directory '%s' could not be created: %s", server_dir.c_str(),
+ GetLastErrorString().c_str());
}
// If we couldn't connect to the server check if there is still a PID file
@@ -829,10 +832,10 @@
if (globals->jvm_log_file_append) {
// Don't dump the log if we were appending - the user should know where
// to find it, and who knows how much content they may have accumulated.
- BAZEL_LOG(USER) << "\nServer crashed during startup. See "
+ BAZEL_LOG(USER) << "Server crashed during startup. See "
<< globals->jvm_log_file;
} else {
- BAZEL_LOG(USER) << "\nServer crashed during startup. Now printing "
+ BAZEL_LOG(USER) << "Server crashed during startup. Now printing "
<< globals->jvm_log_file;
WriteFileToStderrOrDie(globals->jvm_log_file.c_str());
}
@@ -840,8 +843,7 @@
}
}
die(blaze_exit_code::INTERNAL_ERROR,
- "\nError: couldn't connect to server (%d) after 120 seconds.",
- server_pid);
+ "couldn't connect to server (%d) after 120 seconds.", server_pid);
}
// A PureZipExtractorProcessor to extract the files from the blaze zip.
@@ -863,14 +865,14 @@
const devtools_ijar::u1 *data, const size_t size) override {
string path = blaze_util::JoinPath(embedded_binaries_, filename);
if (!blaze_util::MakeDirectories(blaze_util::Dirname(path), 0777)) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "couldn't create '%s'",
- path.c_str());
+ die(blaze_exit_code::INTERNAL_ERROR, "couldn't create '%s': %s",
+ path.c_str(), GetLastErrorString().c_str());
}
if (!blaze_util::WriteFile(data, size, path, 0755)) {
die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "\nFailed to write zipped file \"%s\": %s", path.c_str(),
- blaze_util::GetLastErrorString().c_str());
+ "Failed to write zipped file \"%s\": %s", path.c_str(),
+ GetLastErrorString().c_str());
}
}
@@ -888,8 +890,8 @@
CompoundZipProcessor processor({&extract_blaze_processor,
&install_key_processor});
if (!blaze_util::MakeDirectories(embedded_binaries, 0777)) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "couldn't create '%s'",
- embedded_binaries.c_str());
+ die(blaze_exit_code::INTERNAL_ERROR, "couldn't create '%s': %s",
+ embedded_binaries.c_str(), GetLastErrorString().c_str());
}
BAZEL_LOG(USER) << "Extracting " << globals->options->product_name
@@ -899,13 +901,12 @@
devtools_ijar::ZipExtractor::Create(argv0.c_str(), &processor));
if (extractor.get() == NULL) {
die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "\nFailed to open %s as a zip file: %s",
- globals->options->product_name.c_str(),
- blaze_util::GetLastErrorString().c_str());
+ "Failed to open %s as a zip file: %s",
+ globals->options->product_name.c_str(), GetLastErrorString().c_str());
}
if (extractor->ProcessAll() < 0) {
die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "\nFailed to extract %s as a zip file: %s",
+ "Failed to extract %s as a zip file: %s",
globals->options->product_name.c_str(), extractor->GetError());
}
@@ -944,8 +945,9 @@
// changed. This is essential for the correctness of actions that use
// embedded binaries as artifacts.
if (!mtime.get()->SetToDistantFuture(it)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "failed to set timestamp on '%s'", extracted_path);
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "failed to set timestamp on '%s': %s", extracted_path,
+ GetLastErrorString().c_str());
}
blaze_util::SyncFile(it);
@@ -1015,14 +1017,15 @@
// Give up renaming after 120 failed attempts / 2 minutes.
if (attempts == 120) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "install base directory '%s' could not be renamed into place",
- tmp_install.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "install base directory '%s' could not be renamed into place: "
+ "%s",
+ tmp_install.c_str(), GetLastErrorString().c_str());
}
} else {
if (!blaze_util::IsDirectory(globals->options->install_base)) {
die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "Error: Install base directory '%s' could not be created. "
+ "Install base directory '%s' could not be created. "
"It exists but is not a directory.",
globals->options->install_base.c_str());
}
@@ -1039,7 +1042,7 @@
}
if (!blaze_util::CanReadFile(path)) {
die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "Error: corrupt installation: file '%s' missing."
+ "corrupt installation: file '%s' missing."
" Please remove '%s' and try again.",
path.c_str(), globals->options->install_base.c_str());
}
@@ -1159,9 +1162,9 @@
blaze_util::UnlinkPath(installation_path);
if (!SymlinkDirectories(globals->options->install_base,
installation_path)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "failed to create installation symlink '%s'",
- installation_path.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "failed to create installation symlink '%s': %s",
+ installation_path.c_str(), GetLastErrorString().c_str());
}
// Update the mtime of the install base so that cleanup tools can
@@ -1169,9 +1172,9 @@
std::unique_ptr<blaze_util::IFileMtime> mtime(
blaze_util::CreateFileMtime());
if (!mtime.get()->SetToNow(globals->options->install_base)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "failed to set timestamp on '%s'",
- globals->options->install_base.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "failed to set timestamp on '%s': %s",
+ globals->options->install_base.c_str(), GetLastErrorString().c_str());
}
}
}
@@ -1245,9 +1248,9 @@
static void ComputeWorkspace(const WorkspaceLayout *workspace_layout) {
globals->cwd = blaze_util::MakeCanonical(blaze_util::GetCwd().c_str());
if (globals->cwd.empty()) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "blaze_util::MakeCanonical('%s') failed",
- blaze_util::GetCwd().c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "blaze_util::MakeCanonical('%s') failed: %s",
+ blaze_util::GetCwd().c_str(), GetLastErrorString().c_str());
}
globals->workspace = workspace_layout->GetWorkspace(globals->cwd);
}
@@ -1287,28 +1290,30 @@
const char *output_base = globals->options->output_base.c_str();
if (!blaze_util::PathExists(globals->options->output_base)) {
if (!blaze_util::MakeDirectories(globals->options->output_base, 0777)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "Output base directory '%s' could not be created", output_base);
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "Output base directory '%s' could not be created: %s", output_base,
+ GetLastErrorString().c_str());
}
} else {
if (!blaze_util::IsDirectory(globals->options->output_base)) {
die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "Error: Output base directory '%s' could not be created. "
+ "Output base directory '%s' could not be created. "
"It exists but is not a directory.",
output_base);
}
}
if (!blaze_util::CanAccessDirectory(globals->options->output_base)) {
die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "Error: Output base directory '%s' must be readable and writable.",
+ "Output base directory '%s' must be readable and writable.",
output_base);
}
ExcludePathFromBackup(output_base);
globals->options->output_base = blaze_util::MakeCanonical(output_base);
if (globals->options->output_base.empty()) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "blaze_util::MakeCanonical('%s') failed", output_base);
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "blaze_util::MakeCanonical('%s') failed: %s", output_base,
+ GetLastErrorString().c_str());
}
globals->lockfile =
@@ -1485,7 +1490,8 @@
pipe_ = blaze_util::CreatePipe();
if (pipe_ == NULL) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "Couldn't create pipe");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "Couldn't create pipe: %s",
+ GetLastErrorString().c_str());
}
}
@@ -1614,8 +1620,9 @@
if (bytes_read < 0 && error == blaze_util::IPipe::INTERRUPTED) {
continue;
} else if (bytes_read != 1) {
- pdie(blaze_exit_code::INTERNAL_ERROR,
- "Cannot communicate with cancel thread");
+ die(blaze_exit_code::INTERNAL_ERROR,
+ "Cannot communicate with cancel thread: %s",
+ GetLastErrorString().c_str());
}
switch (buf) {
@@ -1842,8 +1849,9 @@
}
if (!blaze_util::ChangeDirectory(request.working_directory())) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "changing directory into %s failed",
- request.working_directory().c_str());
+ die(blaze_exit_code::INTERNAL_ERROR,
+ "changing directory into %s failed: %s",
+ request.working_directory().c_str(), GetLastErrorString().c_str());
}
ExecuteProgram(request.argv(0), argv);
}
diff --git a/src/main/cpp/blaze_util_darwin.cc b/src/main/cpp/blaze_util_darwin.cc
index 008b721..76427f4 100644
--- a/src/main/cpp/blaze_util_darwin.cc
+++ b/src/main/cpp/blaze_util_darwin.cc
@@ -40,7 +40,7 @@
namespace blaze {
using blaze_util::die;
-using blaze_util::pdie;
+using blaze_util::GetLastErrorString;
using std::string;
using std::vector;
@@ -127,7 +127,8 @@
char pathbuf[PROC_PIDPATHINFO_MAXSIZE] = {};
int len = proc_pidpath(getpid(), pathbuf, sizeof(pathbuf));
if (len == 0) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "error calling proc_pidpath");
+ die(blaze_exit_code::INTERNAL_ERROR, "error calling proc_pidpath: %s",
+ GetLastErrorString().c_str());
}
return string(pathbuf, len);
}
@@ -135,7 +136,8 @@
uint64_t GetMillisecondsMonotonic() {
struct timeval ts = {};
if (gettimeofday(&ts, NULL) < 0) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "error calling gettimeofday");
+ die(blaze_exit_code::INTERNAL_ERROR, "error calling gettimeofday: %s",
+ GetLastErrorString().c_str());
}
return ts.tv_sec * 1000LL + ts.tv_usec / 1000LL;
}
@@ -169,8 +171,9 @@
FILE *output = popen("/usr/libexec/java_home -v 1.7+", "r");
if (output == NULL) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "Could not run /usr/libexec/java_home");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "Could not run /usr/libexec/java_home: %s",
+ GetLastErrorString().c_str());
}
char buf[512];
diff --git a/src/main/cpp/blaze_util_freebsd.cc b/src/main/cpp/blaze_util_freebsd.cc
index 5c51678..e105e37 100644
--- a/src/main/cpp/blaze_util_freebsd.cc
+++ b/src/main/cpp/blaze_util_freebsd.cc
@@ -39,7 +39,7 @@
namespace blaze {
using blaze_util::die;
-using blaze_util::pdie;
+using blaze_util::GetLastErrorString;
using std::string;
string GetOutputRoot() {
@@ -79,12 +79,14 @@
auto p = procstat_getprocs(procstat, KERN_PROC_PID, pid, &n);
if (p) {
if (n != 1) {
- pdie(blaze_exit_code::INTERNAL_ERROR,
- "expected exactly one process from procstat_getprocs, got %d", n);
+ die(blaze_exit_code::INTERNAL_ERROR,
+ "expected exactly one process from procstat_getprocs, got %d: %s", n,
+ GetLastErrorString().c_str());
}
auto r = procstat_getpathname(procstat, p, buffer, PATH_MAX);
if (r != 0) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "error procstat_getpathname");
+ die(blaze_exit_code::INTERNAL_ERROR, "procstat_getpathname failed: %s",
+ GetLastErrorString().c_str());
}
procstat_freeprocs(procstat, p);
}
@@ -116,8 +118,9 @@
string cwd;
if (p) {
if (n != 1) {
- pdie(blaze_exit_code::INTERNAL_ERROR,
- "expected exactly one process from procstat_getprocs, got %d", n);
+ die(blaze_exit_code::INTERNAL_ERROR,
+ "expected exactly one process from procstat_getprocs, got %d: %s", n,
+ GetLastErrorString().c_str());
}
auto files = procstat_getfiles(procstat, p, false);
filestat *entry;
diff --git a/src/main/cpp/blaze_util_linux.cc b/src/main/cpp/blaze_util_linux.cc
index 82c3b99..1682a65 100644
--- a/src/main/cpp/blaze_util_linux.cc
+++ b/src/main/cpp/blaze_util_linux.cc
@@ -38,7 +38,7 @@
namespace blaze {
using blaze_util::die;
-using blaze_util::pdie;
+using blaze_util::GetLastErrorString;
using std::string;
using std::vector;
@@ -89,7 +89,8 @@
errno = ENAMETOOLONG;
}
if (bytes == -1) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "error reading /proc/self/exe");
+ die(blaze_exit_code::INTERNAL_ERROR, "error reading /proc/self/exe: %s",
+ GetLastErrorString().c_str());
}
buffer[bytes] = '\0'; // readlink does not NUL-terminate
return string(buffer);
@@ -112,8 +113,9 @@
sched_param param = {};
param.sched_priority = 0;
if (sched_setscheduler(0, SCHED_BATCH, ¶m)) {
- pdie(blaze_exit_code::INTERNAL_ERROR,
- "sched_setscheduler(SCHED_BATCH) failed");
+ die(blaze_exit_code::INTERNAL_ERROR,
+ "sched_setscheduler(SCHED_BATCH) failed: %s",
+ GetLastErrorString().c_str());
}
}
@@ -121,9 +123,9 @@
if (blaze_util::sys_ioprio_set(
IOPRIO_WHO_PROCESS, getpid(),
IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, io_nice_level)) < 0) {
- pdie(blaze_exit_code::INTERNAL_ERROR,
- "ioprio_set() with class %d and level %d failed",
- IOPRIO_CLASS_BE, io_nice_level);
+ die(blaze_exit_code::INTERNAL_ERROR,
+ "ioprio_set() with class %d and level %d failed: %s", IOPRIO_CLASS_BE,
+ io_nice_level, GetLastErrorString().c_str());
}
}
}
@@ -183,8 +185,8 @@
// Resolve all symlinks.
char resolved_path[PATH_MAX];
if (realpath(javac_dir.c_str(), resolved_path) == NULL) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "Could not resolve javac directory");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "Could not resolve javac directory: %s", GetLastErrorString().c_str());
}
javac_dir = resolved_path;
@@ -203,8 +205,9 @@
vector<string> stat_entries = blaze_util::Split(statline, ' ');
if (stat_entries.size() < 22) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "Format of stat file at %s is unknown", statfile.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "Format of stat file at %s is unknown: %s", statfile.c_str(),
+ GetLastErrorString().c_str());
}
// Start time since startup in jiffies. This combined with the PID should be
@@ -219,14 +222,16 @@
string start_time;
if (!GetStartTime(pid_string, &start_time)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "Cannot get start time of process %s", pid_string.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "Cannot get start time of process %s: %s", pid_string.c_str(),
+ GetLastErrorString().c_str());
}
string start_time_file = blaze_util::JoinPath(server_dir, "server.starttime");
if (!blaze_util::WriteFile(start_time, start_time_file)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "Cannot write start time in server dir %s", server_dir.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "Cannot write start time in server dir %s: %s", server_dir.c_str(),
+ GetLastErrorString().c_str());
}
}
diff --git a/src/main/cpp/blaze_util_posix.cc b/src/main/cpp/blaze_util_posix.cc
index eca9fdb..2a3fba2 100644
--- a/src/main/cpp/blaze_util_posix.cc
+++ b/src/main/cpp/blaze_util_posix.cc
@@ -48,9 +48,9 @@
namespace blaze {
-using blaze_util::die;
-using blaze_util::pdie;
using blaze_exit_code::INTERNAL_ERROR;
+using blaze_util::die;
+using blaze_util::GetLastErrorString;
using std::string;
using std::vector;
@@ -340,7 +340,8 @@
int fds[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds)) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "socket creation failed");
+ die(blaze_exit_code::INTERNAL_ERROR, "socket creation failed: %s",
+ GetLastErrorString().c_str());
}
const char* daemon_output_chars = daemon_output.c_str();
@@ -349,7 +350,8 @@
int child = fork();
if (child == -1) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "fork() failed");
+ die(blaze_exit_code::INTERNAL_ERROR, "fork() failed: %s",
+ GetLastErrorString().c_str());
return -1;
} else if (child > 0) {
// Parent process (i.e. the client)
@@ -361,7 +363,8 @@
"cannot read server PID from server");
string pid_file = blaze_util::JoinPath(server_dir, kServerPidFile);
if (!blaze_util::WriteFile(ToString(server_pid), pid_file)) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "cannot write PID file");
+ die(blaze_exit_code::INTERNAL_ERROR, "cannot write PID file: %s",
+ GetLastErrorString().c_str());
return -1;
}
@@ -402,7 +405,8 @@
const std::vector<string>& args_vector) {
int fds[2];
if (pipe(fds)) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "pipe creation failed");
+ die(blaze_exit_code::INTERNAL_ERROR, "pipe creation failed: %s",
+ GetLastErrorString().c_str());
}
int recv_socket = fds[0];
int send_socket = fds[1];
@@ -412,14 +416,16 @@
int child = fork();
if (child == -1) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "fork() failed");
+ die(blaze_exit_code::INTERNAL_ERROR, "fork() failed: %s",
+ GetLastErrorString().c_str());
} else if (child > 0) { // we're the parent
close(send_socket); // parent keeps only the reading side
string result;
bool success = blaze_util::ReadFrom(recv_socket, &result);
close(recv_socket);
if (!success) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "Cannot read subprocess output");
+ die(blaze_exit_code::INTERNAL_ERROR, "Cannot read subprocess output: %s",
+ GetLastErrorString().c_str());
}
return result;
} else { // We're the child
@@ -454,14 +460,16 @@
struct stat fileinfo = {};
if (!blaze_util::MakeDirectories(root, 0755)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "mkdir('%s')", root);
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "mkdir('%s'): %s", root,
+ GetLastErrorString().c_str());
}
// The path already exists.
// Check ownership and mode, and verify that it is a directory.
if (lstat(root, &fileinfo) < 0) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "lstat('%s')", root);
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "lstat('%s'): %s", root,
+ GetLastErrorString().c_str());
}
if (fileinfo.st_uid != geteuid()) {
@@ -479,7 +487,8 @@
}
if (stat(root, &fileinfo) < 0) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "stat('%s')", root);
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "stat('%s'): %s", root,
+ GetLastErrorString().c_str());
}
if (!S_ISDIR(fileinfo.st_mode)) {
@@ -559,8 +568,9 @@
if (fcntl(fd, F_OFD_SETLK, lock) == 0) return 0;
if (errno != EINVAL) {
if (errno != EACCES && errno != EAGAIN) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "unexpected result from F_OFD_SETLK");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "unexpected result from F_OFD_SETLK: %s",
+ GetLastErrorString().c_str());
}
return -1;
}
@@ -569,8 +579,8 @@
#endif
if (fcntl(fd, F_SETLK, lock) == 0) return 0;
if (errno != EACCES && errno != EAGAIN) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "unexpected result from F_SETLK");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "unexpected result from F_SETLK: %s", GetLastErrorString().c_str());
}
return -1;
}
@@ -581,15 +591,17 @@
int lockfd = open(lockfile.c_str(), O_CREAT|O_RDWR, 0644);
if (lockfd < 0) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "cannot open lockfile '%s' for writing", lockfile.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "cannot open lockfile '%s' for writing: %s", lockfile.c_str(),
+ GetLastErrorString().c_str());
}
// Keep server from inheriting a useless fd if we are not in batch mode
if (!batch_mode) {
if (fcntl(lockfd, F_SETFD, FD_CLOEXEC) == -1) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "fcntl(F_SETFD) failed for lockfile");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "fcntl(F_SETFD) failed for lockfile: %s",
+ GetLastErrorString().c_str());
}
}
@@ -699,8 +711,9 @@
errno = 0;
passwd *pwent = getpwuid(getuid()); // NOLINT (single-threaded)
if (pwent == NULL || pwent->pw_name == NULL) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "$USER is not set, and unable to look up name of current user");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "$USER is not set, and unable to look up name of current user: %s",
+ GetLastErrorString().c_str());
}
return pwent->pw_name;
}
diff --git a/src/main/cpp/blaze_util_windows.cc b/src/main/cpp/blaze_util_windows.cc
index 449fd32..8097413 100644
--- a/src/main/cpp/blaze_util_windows.cc
+++ b/src/main/cpp/blaze_util_windows.cc
@@ -62,12 +62,11 @@
using bazel::windows::AutoHandle;
using bazel::windows::CreateJunction;
-// TODO(bazel-team): get rid of die/pdie, handle errors on the caller side.
-// die/pdie are exit points in the code and they make it difficult to follow the
-// control flow, plus it's not clear whether they call destructors on local
-// variables in the call stack.
+// TODO(bazel-team): stop using die, handle errors on the caller side.
+// die calls exit(exitcode), which makes it difficult to follow the control flow
+// and does not call destructors on local variables on the call stack.
using blaze_util::die;
-using blaze_util::pdie;
+using blaze_util::GetLastErrorString;
using std::string;
using std::unique_ptr;
@@ -196,8 +195,8 @@
string GetSelfPath() {
WCHAR buffer[kWindowsPathBufferSize] = {0};
if (!GetModuleFileNameW(0, buffer, kWindowsPathBufferSize)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "GetSelfPath: GetModuleFileNameW");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "GetSelfPath: GetModuleFileNameW: %s", GetLastErrorString().c_str());
}
return string(blaze_util::WstringToCstring(buffer).get());
}
@@ -212,8 +211,8 @@
WCHAR buffer[kWindowsPathBufferSize] = {0};
if (!::GetTempPathW(kWindowsPathBufferSize, buffer)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "GetOutputRoot: GetTempPathW");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "GetOutputRoot: GetTempPathW: %s", GetLastErrorString().c_str());
}
return string(blaze_util::WstringToCstring(buffer).get());
}
@@ -285,8 +284,9 @@
std::ostringstream cmdline;
string short_exe;
if (!blaze_util::AsShortWindowsPath(exe, &short_exe)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "CreateCommandLine: AsShortWindowsPath(%s)", exe.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "CreateCommandLine: AsShortWindowsPath(%s): %s", exe.c_str(),
+ GetLastErrorString().c_str());
}
bool first = true;
for (const auto& s : args_vector) {
@@ -337,8 +337,8 @@
string cmdline_str = cmdline.str();
if (cmdline_str.size() >= MAX_CMDLINE_LENGTH) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "Command line too long (%d > %d): %s",
- cmdline_str.size(), MAX_CMDLINE_LENGTH, cmdline_str.c_str());
+ die(blaze_exit_code::INTERNAL_ERROR, "Command line too long (%d > %d): %s",
+ cmdline_str.size(), MAX_CMDLINE_LENGTH, cmdline_str.c_str());
}
// Copy command line into a mutable buffer.
@@ -364,16 +364,17 @@
static void WriteProcessStartupTime(const string& server_dir, HANDLE process) {
uint64_t start_time = 0;
if (!GetProcessStartupTime(process, &start_time)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "WriteProcessStartupTime(%s): GetProcessStartupTime",
- server_dir.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "WriteProcessStartupTime(%s): GetProcessStartupTime failed: %s",
+ server_dir.c_str(), GetLastErrorString().c_str());
}
string start_time_file = blaze_util::JoinPath(server_dir, "server.starttime");
if (!blaze_util::WriteFile(ToString(start_time), start_time_file)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "WriteProcessStartupTime(%s): WriteFile(%s)", server_dir.c_str(),
- start_time_file.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "WriteProcessStartupTime(%s): WriteFile(%s) failed: %s",
+ server_dir.c_str(), start_time_file.c_str(),
+ GetLastErrorString().c_str());
}
}
@@ -444,9 +445,9 @@
BlazeServerStartup** server_startup) {
wstring wdaemon_output;
if (!blaze_util::AsAbsoluteWindowsPath(daemon_output, &wdaemon_output)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "ExecuteDaemon(%s): AsAbsoluteWindowsPath(%s)", exe.c_str(),
- daemon_output.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "ExecuteDaemon(%s): AsAbsoluteWindowsPath(%s) failed: %s", exe.c_str(),
+ daemon_output.c_str(), GetLastErrorString().c_str());
}
SECURITY_ATTRIBUTES sa;
@@ -460,16 +461,17 @@
AutoHandle devnull(::CreateFileA("NUL", GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
if (!devnull.IsValid()) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "ExecuteDaemon(%s): CreateFileA(NUL)", exe.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "ExecuteDaemon(%s): CreateFileA(NUL) failed: %s", exe.c_str(),
+ GetLastErrorString().c_str());
}
AutoHandle stdout_file(CreateJvmOutputFile(wdaemon_output.c_str(), &sa,
daemon_out_append));
if (!stdout_file.IsValid()) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "ExecuteDaemon(%s): CreateJvmOutputFile(%ls)", exe.c_str(),
- wdaemon_output.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "ExecuteDaemon(%s): CreateJvmOutputFile(%ls) failed: %s", exe.c_str(),
+ wdaemon_output.c_str(), GetLastErrorString().c_str());
}
HANDLE stderr_handle;
// We must duplicate the handle to stdout, otherwise "bazel clean --expunge"
@@ -484,9 +486,9 @@
/* dwDesiredAccess */ 0,
/* bInheritHandle */ TRUE,
/* dwOptions */ DUPLICATE_SAME_ACCESS)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "ExecuteDaemon(%s): DuplicateHandle(%ls)", exe.c_str(),
- wdaemon_output.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "ExecuteDaemon(%s): DuplicateHandle(%ls) failed: %s", exe.c_str(),
+ wdaemon_output.c_str(), GetLastErrorString().c_str());
}
AutoHandle stderr_file(stderr_handle);
@@ -497,8 +499,9 @@
if (!UpdateProcThreadAttribute(
lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
handlesToInherit, 2 * sizeof(HANDLE), NULL, NULL)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "ExecuteDaemon(%s): UpdateProcThreadAttribute", exe.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "ExecuteDaemon(%s): UpdateProcThreadAttribute failed: %s", exe.c_str(),
+ GetLastErrorString().c_str());
}
PROCESS_INFORMATION processInfo = {0};
@@ -528,8 +531,9 @@
/* lpProcessInformation */ &processInfo);
if (!ok) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "ExecuteDaemon(%s): CreateProcess(%s)", exe.c_str(), cmdline.cmdline);
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "ExecuteDaemon(%s): CreateProcess(%s) failed: %s", exe.c_str(),
+ cmdline.cmdline, GetLastErrorString().c_str());
}
WriteProcessStartupTime(server_dir, processInfo.hProcess);
@@ -573,8 +577,9 @@
if (NestedJobsSupported()) {
job = CreateJobObject(NULL, NULL);
if (job == NULL) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "ExecuteProgram(%s): CreateJobObject", exe.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "ExecuteProgram(%s): CreateJobObject failed: %s", exe.c_str(),
+ GetLastErrorString().c_str());
}
JOBOBJECT_EXTENDED_LIMIT_INFORMATION job_info = {0};
@@ -583,8 +588,9 @@
if (!SetInformationJobObject(job, JobObjectExtendedLimitInformation,
&job_info, sizeof(job_info))) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "ExecuteProgram(%s): SetInformationJobObject", exe.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "ExecuteProgram(%s): SetInformationJobObject failed: %s", exe.c_str(),
+ GetLastErrorString().c_str());
}
}
@@ -601,8 +607,9 @@
/* lpProcessInformation */ &processInfo);
if (!success) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "ExecuteProgram(%s): CreateProcess(%s)", exe.c_str(), cmdline.cmdline);
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "ExecuteProgram(%s): CreateProcess(%s) failed: %s", exe.c_str(),
+ cmdline.cmdline, GetLastErrorString().c_str());
}
// On Windows versions that support nested jobs (Windows 8 and above), we
@@ -618,15 +625,17 @@
// subprocesses via the JNI library.
if (job != INVALID_HANDLE_VALUE) {
if (!AssignProcessToJobObject(job, processInfo.hProcess)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "ExecuteProgram(%s): AssignProcessToJobObject", exe.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "ExecuteProgram(%s): AssignProcessToJobObject failed: %s",
+ exe.c_str(), GetLastErrorString().c_str());
}
}
// Now that we potentially put the process into a new job object, we can start
// running it.
if (ResumeThread(processInfo.hThread) == -1) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "ExecuteProgram(%s): ResumeThread", exe.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "ExecuteProgram(%s): ResumeThread failed: %s", exe.c_str(),
+ GetLastErrorString().c_str());
}
WaitForSingleObject(processInfo.hProcess, INFINITE);
@@ -642,8 +651,9 @@
string PathAsJvmFlag(const string& path) {
string spath;
if (!blaze_util::AsShortWindowsPath(path, &spath)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "PathAsJvmFlag(%s): AsShortWindowsPath", path.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "PathAsJvmFlag(%s): AsShortWindowsPath failed: %s", path.c_str(),
+ GetLastErrorString().c_str());
}
// Convert backslashes to forward slashes, in order to avoid the JVM parsing
// Windows paths as if they contained escaped characters.
@@ -656,8 +666,9 @@
// The path may not be Windows-style and may not be normalized, so convert it.
wstring wpath;
if (!blaze_util::AsAbsoluteWindowsPath(path, &wpath)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "ConvertPath(%s): AsAbsoluteWindowsPath", path.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "ConvertPath(%s): AsAbsoluteWindowsPath failed: %s", path.c_str(),
+ GetLastErrorString().c_str());
}
std::transform(wpath.begin(), wpath.end(), wpath.begin(), ::towlower);
return string(blaze_util::WstringToCstring(
@@ -669,15 +680,17 @@
wstring name;
wstring target;
if (!blaze_util::AsAbsoluteWindowsPath(posix_name, &name)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "SymlinkDirectories(%s, %s): AsAbsoluteWindowsPath(%s)",
- posix_target.c_str(), posix_name.c_str(), posix_target.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "SymlinkDirectories(%s, %s): AsAbsoluteWindowsPath(%s) failed: %s",
+ posix_target.c_str(), posix_name.c_str(), posix_target.c_str(),
+ GetLastErrorString().c_str());
return false;
}
if (!blaze_util::AsAbsoluteWindowsPath(posix_target, &target)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "SymlinkDirectories(%s, %s): AsAbsoluteWindowsPath(%s)",
- posix_target.c_str(), posix_name.c_str(), posix_name.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "SymlinkDirectories(%s, %s): AsAbsoluteWindowsPath(%s) failed: %s",
+ posix_target.c_str(), posix_name.c_str(), posix_name.c_str(),
+ GetLastErrorString().c_str());
return false;
}
wstring werror(CreateJunction(name, target));
@@ -742,9 +755,9 @@
BOOL result = TerminateProcess(process, /*uExitCode*/ 0);
if (!result || !AwaitServerProcessTermination(pid, output_base,
kPostKillGracePeriodSeconds)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "Cannot terminate server process with PID %d, output_base=(%s)", pid,
- output_base.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "Cannot terminate server process with PID %d, output_base=(%s): %s",
+ pid, output_base.c_str(), GetLastErrorString().c_str());
}
return result;
}
@@ -785,8 +798,8 @@
// implementation does.
const char* root = path.c_str();
if (!blaze_util::MakeDirectories(path, 0755)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "MakeDirectories(%s) failed", root);
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "MakeDirectories(%s) failed: %s", root, GetLastErrorString().c_str());
}
if (!blaze_util::IsDirectory(path)) {
@@ -878,17 +891,18 @@
LARGE_INTEGER WindowsClock::GetFrequency() {
LARGE_INTEGER result;
if (!QueryPerformanceFrequency(&result)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "WindowsClock::GetFrequency: QueryPerformanceFrequency");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "WindowsClock::GetFrequency: QueryPerformanceFrequency failed: %s",
+ GetLastErrorString().c_str());
}
// On ancient Windows versions (pre-XP) and specific hardware the result may
// be 0. Since this is pre-XP, we don't handle that, just error out.
if (result.QuadPart <= 0) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "WindowsClock::GetFrequency: QueryPerformanceFrequency returned "
- "invalid result (%llu)\n",
- result.QuadPart);
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "WindowsClock::GetFrequency: QueryPerformanceFrequency returned "
+ "invalid result (%llu): %s",
+ result.QuadPart, GetLastErrorString().c_str());
}
return result;
@@ -898,8 +912,10 @@
const LARGE_INTEGER& freq) {
LARGE_INTEGER counter;
if (!QueryPerformanceCounter(&counter)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "WindowsClock::GetMillisecondsAsLargeInt: QueryPerformanceCounter");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "WindowsClock::GetMillisecondsAsLargeInt: QueryPerformanceCounter "
+ "failed: %s",
+ GetLastErrorString().c_str());
}
LARGE_INTEGER result;
@@ -931,9 +947,9 @@
string lockfile = blaze_util::JoinPath(output_base, "lock");
wstring wlockfile;
if (!blaze_util::AsAbsoluteWindowsPath(lockfile, &wlockfile)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "AcquireLock(%s): AsAbsoluteWindowsPath(%s)", output_base.c_str(),
- lockfile.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "AcquireLock(%s): AsAbsoluteWindowsPath(%s) failed: %s",
+ output_base.c_str(), lockfile.c_str(), GetLastErrorString().c_str());
}
blaze_lock->handle = INVALID_HANDLE_VALUE;
@@ -966,9 +982,9 @@
}
Sleep(/* dwMilliseconds */ 200);
} else {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "AcquireLock(%s): CreateFileW(%ls)", lockfile.c_str(),
- wlockfile.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "AcquireLock(%s): CreateFileW(%ls) failed: %s", lockfile.c_str(),
+ wlockfile.c_str(), GetLastErrorString().c_str());
}
}
uint64_t wait_time = GetMillisecondsMonotonic() - st;
@@ -982,9 +998,9 @@
/* nNumberOfBytesToLockLow */ 1,
/* nNumberOfBytesToLockHigh */ 0,
/* lpOverlapped */ &overlapped)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "AcquireLock(%s): LockFileEx(%ls)", lockfile.c_str(),
- wlockfile.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "AcquireLock(%s): LockFileEx(%ls) failed: %s", lockfile.c_str(),
+ wlockfile.c_str(), GetLastErrorString().c_str());
}
// On other platforms we write some info about this process into the lock file
// such as the server PID. On Windows we don't do that because the file is
@@ -1010,7 +1026,8 @@
WCHAR buffer[UNLEN + 1];
DWORD len = UNLEN + 1;
if (!::GetUserNameW(buffer, &len)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "GetUserNameW");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "GetUserNameW failed: %s",
+ GetLastErrorString().c_str());
}
return string(blaze_util::WstringToCstring(buffer).get());
}
diff --git a/src/main/cpp/util/errors.cc b/src/main/cpp/util/errors.cc
index b337809..ec25de3 100644
--- a/src/main/cpp/util/errors.cc
+++ b/src/main/cpp/util/errors.cc
@@ -25,6 +25,7 @@
// TODO(b/32967056) This should be a FATAL log statement
void die(const int exit_status, const char *format, ...) {
+ fprintf(stderr, "\nError: ");
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
@@ -33,16 +34,4 @@
exit(exit_status);
}
-// TODO(b/32967056) This should be a FATAL log statement
-void pdie(const int exit_status, const char *format, ...) {
- const char *errormsg = GetLastErrorString().c_str();
- fprintf(stderr, "Error: ");
- va_list ap;
- va_start(ap, format);
- vfprintf(stderr, format, ap);
- va_end(ap);
- fprintf(stderr, ": %s\n", errormsg);
- exit(exit_status);
-}
-
} // namespace blaze_util
diff --git a/src/main/cpp/util/file_posix.cc b/src/main/cpp/util/file_posix.cc
index 6199d04..b04076a 100644
--- a/src/main/cpp/util/file_posix.cc
+++ b/src/main/cpp/util/file_posix.cc
@@ -160,17 +160,18 @@
IPipe* CreatePipe() {
int fd[2];
if (pipe(fd) < 0) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "pipe()");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "pipe() failed: %s",
+ GetLastErrorString().c_str());
}
if (fcntl(fd[0], F_SETFD, FD_CLOEXEC) == -1) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "fcntl(F_SETFD, FD_CLOEXEC) failed");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "fcntl(F_SETFD, FD_CLOEXEC) failed: %s", GetLastErrorString().c_str());
}
if (fcntl(fd[1], F_SETFD, FD_CLOEXEC) == -1) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "fcntl(F_SETFD, FD_CLOEXEC) failed");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "fcntl(F_SETFD, FD_CLOEXEC) failed: %s", GetLastErrorString().c_str());
}
return new PosixPipe(fd[0], fd[1]);
@@ -328,12 +329,13 @@
const char* file_path = path.c_str();
int fd = open(file_path, O_RDONLY);
if (fd < 0) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "failed to open '%s' for syncing", file_path);
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "failed to open '%s' for syncing: %s", file_path,
+ GetLastErrorString().c_str());
}
if (fsync(fd) < 0) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "failed to sync '%s'",
- file_path);
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "failed to sync '%s': %s",
+ file_path, GetLastErrorString().c_str());
}
close(fd);
}
@@ -390,7 +392,8 @@
time_t PosixFileMtime::GetNow() {
time_t result = time(NULL);
if (result == -1) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "time(NULL) failed");
+ die(blaze_exit_code::INTERNAL_ERROR, "time(NULL) failed: %s",
+ GetLastErrorString().c_str());
}
return result;
}
@@ -414,7 +417,8 @@
string GetCwd() {
char cwdbuf[PATH_MAX];
if (getcwd(cwdbuf, sizeof cwdbuf) == NULL) {
- pdie(blaze_exit_code::INTERNAL_ERROR, "getcwd() failed");
+ die(blaze_exit_code::INTERNAL_ERROR, "getcwd() failed: %s",
+ GetLastErrorString().c_str());
}
return string(cwdbuf);
}
@@ -443,7 +447,8 @@
if (ent->d_type == DT_UNKNOWN) {
struct stat buf;
if (lstat(filename.c_str(), &buf) == -1) {
- die(blaze_exit_code::INTERNAL_ERROR, "stat failed");
+ die(blaze_exit_code::INTERNAL_ERROR, "stat failed: %s",
+ GetLastErrorString().c_str());
}
is_directory = S_ISDIR(buf.st_mode);
} else {
diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc
index 4768f5c..2db123e 100644
--- a/src/main/cpp/util/file_windows.cc
+++ b/src/main/cpp/util/file_windows.cc
@@ -149,7 +149,8 @@
HANDLE read_handle = INVALID_HANDLE_VALUE;
HANDLE write_handle = INVALID_HANDLE_VALUE;
if (!CreatePipe(&read_handle, &write_handle, &sa, 0)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "CreatePipe");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "CreatePipe: %s",
+ GetLastErrorString().c_str());
}
return new WindowsPipe(read_handle, write_handle);
}
@@ -184,9 +185,10 @@
}
wstring wpath;
if (!AsAbsoluteWindowsPath(path, &wpath)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "WindowsFileMtime::GetIfInDistantFuture(%s): AsAbsoluteWindowsPath",
- path.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "WindowsFileMtime::GetIfInDistantFuture(%s): AsAbsoluteWindowsPath "
+ "failed: %s",
+ path.c_str(), GetLastErrorString().c_str());
}
AutoHandle handle(::CreateFileW(
@@ -236,8 +238,9 @@
}
wstring wpath;
if (!AsAbsoluteWindowsPath(path, &wpath)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "WindowsFileMtime::Set(%s): AsAbsoluteWindowsPath", path.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "WindowsFileMtime::Set(%s): AsAbsoluteWindowsPath failed: %s",
+ path.c_str(), GetLastErrorString().c_str());
return false;
}
@@ -267,8 +270,9 @@
::GetSystemTime(&sys_time);
FILETIME file_time;
if (!::SystemTimeToFileTime(&sys_time, &file_time)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "WindowsFileMtime::GetNow: SystemTimeToFileTime");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "WindowsFileMtime::GetNow: SystemTimeToFileTime failed: %s",
+ GetLastErrorString().c_str());
}
return file_time;
}
@@ -286,8 +290,9 @@
future_time.wMilliseconds = 0;
FILETIME file_time;
if (!::SystemTimeToFileTime(&future_time, &file_time)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "WindowsFileMtime::GetFuture: SystemTimeToFileTime");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "WindowsFileMtime::GetFuture: SystemTimeToFileTime failed: %s",
+ GetLastErrorString().c_str());
}
return file_time;
}
@@ -552,8 +557,9 @@
wstring wpath;
wstring wsuffix;
if (!AsAbsoluteWindowsPath(path, &wpath)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "AsShortWindowsPath(%s): AsAbsoluteWindowsPath", path.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "AsShortWindowsPath(%s): AsAbsoluteWindowsPath failed: %s",
+ path.c_str(), GetLastErrorString().c_str());
return false;
}
DWORD size = ::GetShortPathNameW(wpath.c_str(), nullptr, 0);
@@ -593,9 +599,9 @@
unique_ptr<WCHAR[]> wshort(
new WCHAR[size]); // size includes null-terminator
if (size - 1 != ::GetShortPathNameW(wpath.c_str(), wshort.get(), size)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "AsShortWindowsPath(%s): GetShortPathNameW(%S)", path.c_str(),
- wpath.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "AsShortWindowsPath(%s): GetShortPathNameW(%S) failed: %s",
+ path.c_str(), wpath.c_str(), GetLastErrorString().c_str());
}
// GetShortPathNameW may preserve the UNC prefix in the result, so strip it.
wresult = wstring(RemoveUncPrefixMaybe(wshort.get())) + wsuffix;
@@ -616,8 +622,9 @@
}
wstring wfilename;
if (!AsAbsoluteWindowsPath(filename, &wfilename)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "OpenFileForReading(%s): AsAbsoluteWindowsPath", filename.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "OpenFileForReading(%s): AsAbsoluteWindowsPath failed: %s",
+ filename.c_str(), GetLastErrorString().c_str());
}
*result = ::CreateFileW(
/* lpFileName */ wfilename.c_str(),
@@ -685,8 +692,9 @@
}
wstring wpath;
if (!AsAbsoluteWindowsPath(filename, &wpath)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "WriteFile(%s): AsAbsoluteWindowsPath", filename.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "WriteFile(%s): AsAbsoluteWindowsPath failed: %s", filename.c_str(),
+ GetLastErrorString().c_str());
return false;
}
@@ -727,17 +735,19 @@
int RenameDirectory(const std::string& old_name, const std::string& new_name) {
wstring wold_name;
if (!AsAbsoluteWindowsPath(old_name, &wold_name)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "RenameDirectory(%s, %s): AsAbsoluteWindowsPath(%s)", old_name.c_str(),
- new_name.c_str(), old_name.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "RenameDirectory(%s, %s): AsAbsoluteWindowsPath(%s) failed: %s",
+ old_name.c_str(), new_name.c_str(), old_name.c_str(),
+ GetLastErrorString().c_str());
return kRenameDirectoryFailureOtherError;
}
wstring wnew_name;
if (!AsAbsoluteWindowsPath(new_name, &wnew_name)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "RenameDirectory(%s, %s): AsAbsoluteWindowsPath(%s)", old_name.c_str(),
- new_name.c_str(), new_name.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "RenameDirectory(%s, %s): AsAbsoluteWindowsPath(%s) failed: %s",
+ old_name.c_str(), new_name.c_str(), new_name.c_str(),
+ GetLastErrorString().c_str());
return kRenameDirectoryFailureOtherError;
}
@@ -777,8 +787,9 @@
wstring wpath;
if (!AsAbsoluteWindowsPath(file_path, &wpath)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "UnlinkPath(%s): AsAbsoluteWindowsPath", file_path.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "UnlinkPath(%s): AsAbsoluteWindowsPath failed: %s", file_path.c_str(),
+ GetLastErrorString().c_str());
return false;
}
return UnlinkPathW(wpath);
@@ -909,8 +920,9 @@
bool ReadDirectorySymlink(const string& name, string* result) {
wstring wname;
if (!AsAbsoluteWindowsPath(name, &wname)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "ReadDirectorySymlink(%s): AsAbsoluteWindowsPath", name.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "ReadDirectorySymlink(%s): AsAbsoluteWindowsPath failed: %s",
+ name.c_str(), GetLastErrorString().c_str());
return false;
}
unique_ptr<WCHAR[]> result_ptr;
@@ -930,8 +942,9 @@
}
wstring wpath;
if (!AsAbsoluteWindowsPath(path, &wpath)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "PathExists(%s): AsAbsoluteWindowsPath", path.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "PathExists(%s): AsAbsoluteWindowsPath failed: %s", path.c_str(),
+ GetLastErrorString().c_str());
return false;
}
return JunctionResolver().Resolve(wpath.c_str(), nullptr);
@@ -946,8 +959,9 @@
return "";
}
if (!AsAbsoluteWindowsPath(path, &wpath)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "MakeCanonical(%s): AsAbsoluteWindowsPath", path);
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "MakeCanonical(%s): AsAbsoluteWindowsPath failed: %s", path,
+ GetLastErrorString().c_str());
}
// Resolve all segments of the path. Do this from leaf to root, so we always
@@ -1052,8 +1066,9 @@
bool CanReadFile(const std::string& path) {
wstring wpath;
if (!AsAbsoluteWindowsPath(path, &wpath)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "CanReadFile(%s): AsAbsoluteWindowsPath", path.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "CanReadFile(%s): AsAbsoluteWindowsPath failed: %s", path.c_str(),
+ GetLastErrorString().c_str());
return false;
}
return CanReadFileW(wpath);
@@ -1062,8 +1077,9 @@
bool CanExecuteFile(const std::string& path) {
wstring wpath;
if (!AsAbsoluteWindowsPath(path, &wpath)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "CanExecuteFile(%s): AsAbsoluteWindowsPath", path.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "CanExecuteFile(%s): AsAbsoluteWindowsPath failed: %s", path.c_str(),
+ GetLastErrorString().c_str());
return false;
}
return CanReadFileW(wpath) && (ends_with(wpath, wstring(L".exe")) ||
@@ -1075,8 +1091,9 @@
bool CanAccessDirectory(const std::string& path) {
wstring wpath;
if (!AsAbsoluteWindowsPath(path, &wpath)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "CanAccessDirectory(%s): AsAbsoluteWindowsPath", path.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "CanAccessDirectory(%s): AsAbsoluteWindowsPath failed: %s",
+ path.c_str(), GetLastErrorString().c_str());
return false;
}
DWORD attr = ::GetFileAttributesW(wpath.c_str());
@@ -1142,8 +1159,9 @@
}
wstring wpath;
if (!AsAbsoluteWindowsPath(path, &wpath)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "IsDirectory(%s): AsAbsoluteWindowsPath", path.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "IsDirectory(%s): AsAbsoluteWindowsPath failed: %s", path.c_str(),
+ GetLastErrorString().c_str());
return false;
}
return IsDirectoryW(wpath);
@@ -1175,8 +1193,9 @@
if (parent.empty()) {
// Since `path` is not a root directory, there should have been at least one
// directory above it.
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "MakeDirectoriesW(%S), could not find dirname", path.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "MakeDirectoriesW(%S), could not find dirname: %s", path.c_str(),
+ GetLastErrorString().c_str());
}
return MakeDirectoriesW(parent) &&
::CreateDirectoryW(path.c_str(), NULL) == TRUE;
@@ -1192,8 +1211,9 @@
// According to MSDN, CreateDirectory's limit without the UNC prefix is
// 248 characters (so it could fit another filename before reaching MAX_PATH).
if (!AsAbsoluteWindowsPath(path, &wpath)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "MakeDirectories(%s): AsAbsoluteWindowsPath", path.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "MakeDirectories(%s): AsAbsoluteWindowsPath failed: %s", path.c_str(),
+ GetLastErrorString().c_str());
return false;
}
return MakeDirectoriesW(wpath);
@@ -1203,7 +1223,8 @@
DWORD len = ::GetCurrentDirectoryW(0, nullptr);
unique_ptr<WCHAR[]> cwd(new WCHAR[len]);
if (!::GetCurrentDirectoryW(len, cwd.get())) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "GetCurrentDirectoryW");
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "GetCurrentDirectoryW failed: %s", GetLastErrorString().c_str());
}
for (WCHAR* p = cwd.get(); *p != 0; ++p) {
*p = towlower(*p);
@@ -1235,8 +1256,9 @@
return;
}
if (!AsWindowsPath(path, &wpath)) {
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "ForEachDirectoryEntry(%s): AsWindowsPath", path.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "ForEachDirectoryEntry(%s): AsWindowsPath failed: %s", path.c_str(),
+ GetLastErrorString().c_str());
}
static const wstring kUncPrefix(L"\\\\?\\");
@@ -1273,8 +1295,8 @@
}
if (path[0] == '/') {
// This is an absolute MSYS path, error out.
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "NormalizeWindowsPath(%s): expected a Windows path", path.c_str());
+ die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "NormalizeWindowsPath(%s): expected a Windows path", path.c_str());
}
if (path.size() >= 4 && HasUncPrefix(path.c_str())) {
path = path.substr(4);