From 6d47707a96752d3b876bed5cfd253d5e064cbec1 Mon Sep 17 00:00:00 2001 From: Laszlo Csomor Date: Thu, 20 Jul 2017 10:09:44 +0200 Subject: Windows: clean up error reporting In this commit: - remove blaze::PrintError in favor of blaze_util::PrintError - remove Ijar's PrintLastErrorMessage in favor of blaze_util::PrintError - use pdie every time path conversion fails, because that indicates a fatal error (bad user input for a path flag, or downright bug) - remove explicitly printing GetLastErrror; pdie and PrintError do it already - unify the pdie/PrintError message formats Fixes https://github.com/bazelbuild/bazel/issues/2935 Change-Id: I5feaf73885cab95c43a28c529ada6942e037b162 PiperOrigin-RevId: 162587490 --- third_party/ijar/mapped_file_windows.cc | 73 ++++++++++----------------------- third_party/ijar/platform_utils.cc | 3 +- 2 files changed, 22 insertions(+), 54 deletions(-) (limited to 'third_party/ijar') diff --git a/third_party/ijar/mapped_file_windows.cc b/third_party/ijar/mapped_file_windows.cc index b3fdd99088..253065e453 100644 --- a/third_party/ijar/mapped_file_windows.cc +++ b/third_party/ijar/mapped_file_windows.cc @@ -29,20 +29,6 @@ using std::wstring; static char errmsg[MAX_ERROR] = ""; -void PrintLastError(const char* op) { - char *message; - DWORD err = GetLastError(); - FormatMessage( - FORMAT_MESSAGE_ALLOCATE_BUFFER - | FORMAT_MESSAGE_FROM_SYSTEM - | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - reinterpret_cast(&message), - 0, NULL); - snprintf(errmsg, MAX_ERROR, "%s: %s", op, message); - LocalFree(message); -} - struct MappedInputFileImpl { HANDLE file_; HANDLE mapping_; @@ -60,37 +46,30 @@ MappedInputFile::MappedInputFile(const char* name) { wstring wname; if (!blaze_util::AsWindowsPathWithUncPrefix(name, &wname)) { - blaze_util::die( - 255, "MappedInputFile(%s): AsWindowsPathWithUncPrefix failed", name); + blaze_util::pdie(255, "MappedInputFile(%s): AsWindowsPathWithUncPrefix", + name); } HANDLE file = CreateFileW(wname.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if (file == INVALID_HANDLE_VALUE) { - PrintLastError("CreateFile()"); - return; + blaze_util::pdie(255, "MappedInputFile(%s): CreateFileW(%S)", name, + wname.c_str()); } LARGE_INTEGER size; if (!GetFileSizeEx(file, &size)) { - PrintLastError("GetFileSizeEx()"); - CloseHandle(file); - return; + blaze_util::pdie(255, "MappedInputFile(%s): GetFileSizeEx", name); } HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, size.HighPart, size.LowPart, NULL); if (mapping == NULL || mapping == INVALID_HANDLE_VALUE) { - PrintLastError("CreateFileMapping()"); - CloseHandle(file); - return; + blaze_util::pdie(255, "MappedInputFile(%s): CreateFileMapping", name); } void *view = MapViewOfFileEx(mapping, FILE_MAP_READ, 0, 0, 0, NULL); if (view == NULL) { - PrintLastError("MapViewOfFileEx()"); - CloseHandle(mapping); - CloseHandle(file); - return; + blaze_util::pdie(255, "MappedInputFile(%s): MapViewOfFileEx", name); } impl_ = new MappedInputFileImpl(file, mapping); @@ -111,18 +90,15 @@ void MappedInputFile::Discard(size_t bytes) { int MappedInputFile::Close() { if (!UnmapViewOfFile(buffer_)) { - PrintLastError("UnmapViewOfFile()"); - return -1; + blaze_util::pdie(255, "MappedInputFile::Close: UnmapViewOfFile"); } if (!CloseHandle(impl_->mapping_)) { - PrintLastError("CloseHandle(mapping)"); - return -1; + blaze_util::pdie(255, "MappedInputFile::Close: CloseHandle for mapping"); } if (!CloseHandle(impl_->file_)) { - PrintLastError("CloseHandle(file)"); - return -1; + blaze_util::pdie(255, "MappedInputFile::Close: CloseHandle for file"); } return 0; @@ -145,27 +121,25 @@ MappedOutputFile::MappedOutputFile(const char* name, u8 estimated_size) { wstring wname; if (!blaze_util::AsWindowsPathWithUncPrefix(name, &wname)) { - blaze_util::die( - 255, "MappedOutputFile(%s): AsWindowsPathWithUncPrefix failed", name); + blaze_util::pdie(255, "MappedOutputFile(%s): AsWindowsPathWithUncPrefix", + name); } HANDLE file = CreateFileW(wname.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); if (file == INVALID_HANDLE_VALUE) { - PrintLastError("CreateFile()"); - return; + blaze_util::pdie(255, "MappedOutputFile(%s): CreateFileW(%S)", name, + wname.c_str()); } HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READWRITE, estimated_size >> 32, estimated_size & 0xffffffffUL, NULL); if (mapping == NULL || mapping == INVALID_HANDLE_VALUE) { - PrintLastError("CreateFileMapping()"); - CloseHandle(file); - return; + blaze_util::pdie(255, "MappedOutputFile(%s): CreateFileMapping", name); } void *view = MapViewOfFileEx(mapping, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL); if (view == NULL) { - PrintLastError("MapViewOfFileEx()"); + blaze_util::pdie(255, "MappedOutputFile(%s): MapViewOfFileEx", name); CloseHandle(mapping); CloseHandle(file); return; @@ -182,28 +156,23 @@ MappedOutputFile::~MappedOutputFile() { int MappedOutputFile::Close(int size) { if (!UnmapViewOfFile(buffer_)) { - PrintLastError("UnmapViewOfFile()"); - return -1; + blaze_util::pdie(255, "MappedOutputFile::Close: UnmapViewOfFile"); } if (!CloseHandle(impl_->mapping_)) { - PrintLastError("CloseHandle(mapping)"); - return -1; + blaze_util::pdie(255, "MappedOutputFile::Close: CloseHandle for mapping"); } if (!SetFilePointer(impl_->file_, size, NULL, FILE_BEGIN)) { - PrintLastError("SetFilePointer()"); - return -1; + blaze_util::pdie(255, "MappedOutputFile::Close: SetFilePointer"); } if (!SetEndOfFile(impl_->file_)) { - PrintLastError("SetEndOfFile()"); - return -1; + blaze_util::pdie(255, "MappedOutputFile::Close: SetEndOfFile"); } if (!CloseHandle(impl_->file_)) { - PrintLastError("CloseHandle(file)"); - return -1; + blaze_util::pdie(255, "MappedOutputFile::Close: CloseHandle for file"); } return 0; diff --git a/third_party/ijar/platform_utils.cc b/third_party/ijar/platform_utils.cc index 29d468a4fc..a998cabc8e 100644 --- a/third_party/ijar/platform_utils.cc +++ b/third_party/ijar/platform_utils.cc @@ -39,8 +39,7 @@ bool stat_file(const char* path, Stat* result) { #if defined(COMPILER_MSVC) || defined(__CYGWIN__) std::wstring wpath; if (!blaze_util::AsWindowsPathWithUncPrefix(path, &wpath)) { - blaze_util::die(255, "stat_file: AsWindowsPathWithUncPrefix(%s) failed", - path); + blaze_util::pdie(255, "stat_file: AsWindowsPathWithUncPrefix(%s)", path); } bool success = false; BY_HANDLE_FILE_INFORMATION info; -- cgit v1.2.3