diff options
-rw-r--r-- | src/main/cpp/blaze_util_windows.cc | 139 | ||||
-rw-r--r-- | src/main/cpp/util/errors_windows.cc | 2 | ||||
-rw-r--r-- | src/main/cpp/util/file_windows.cc | 107 | ||||
-rw-r--r-- | src/main/native/windows/file.h | 7 | ||||
-rw-r--r-- | src/test/cpp/util/BUILD | 1 | ||||
-rw-r--r-- | third_party/ijar/mapped_file_windows.cc | 73 | ||||
-rw-r--r-- | third_party/ijar/platform_utils.cc | 3 |
7 files changed, 150 insertions, 182 deletions
diff --git a/src/main/cpp/blaze_util_windows.cc b/src/main/cpp/blaze_util_windows.cc index df672e42ca..15981525f8 100644 --- a/src/main/cpp/blaze_util_windows.cc +++ b/src/main/cpp/blaze_util_windows.cc @@ -260,29 +260,6 @@ void SigPrintf(const char *format, ...) { } } -static void PrintError(const string& op) { - DWORD last_error = ::GetLastError(); - if (last_error == 0) { - return; - } - - 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); - - fprintf(stderr, "ERROR: %s: %s (%d)\n", - op.c_str(), message_buffer, last_error); - LocalFree(message_buffer); -} - static void PrintErrorW(const wstring& op) { DWORD last_error = ::GetLastError(); if (last_error == 0) { @@ -315,7 +292,8 @@ string GetProcessIdAsString() { string GetSelfPath() { WCHAR buffer[kWindowsPathBufferSize] = {0}; if (!GetModuleFileNameW(0, buffer, kWindowsPathBufferSize)) { - pdie(255, "Error %u getting executable file name\n", GetLastError()); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "GetSelfPath: GetModuleFileNameW"); } return string(blaze_util::WstringToCstring(buffer).get()); } @@ -336,8 +314,8 @@ string GetOutputRoot() { WCHAR buffer[kWindowsPathBufferSize] = {0}; if (!::GetTempPathW(kWindowsPathBufferSize, buffer)) { - PrintErrorW(L"GetTempPathW"); - pdie(255, "Could not retrieve the temp directory path"); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "GetOutputRoot: GetTempPathW"); } return string(blaze_util::WstringToCstring(buffer).get()); #else // not COMPILER_MSVC @@ -434,8 +412,7 @@ static void CreateCommandLine(CmdLine* result, const string& exe, string short_exe; if (!blaze_util::AsShortWindowsPath(exe, &short_exe)) { pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "CreateCommandLine: AsShortWindowsPath(%s) failed, err=%d", - exe.c_str(), GetLastError()); + "CreateCommandLine: AsShortWindowsPath(%s)", exe.c_str()); } bool first = true; for (const auto& s : args_vector) { @@ -486,8 +463,8 @@ static void CreateCommandLine(CmdLine* result, const string& exe, string cmdline_str = cmdline.str(); if (cmdline_str.size() >= MAX_CMDLINE_LENGTH) { - pdie(blaze_exit_code::INTERNAL_ERROR, "Command line too long: %s", - cmdline_str.c_str()); + pdie(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. @@ -503,13 +480,15 @@ string GetJvmVersion(const string& java_exe) { SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE}; if (!::CreatePipe(&pipe_read, &pipe_write, &sa, 0)) { - pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "CreatePipe"); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "GetJvmVersion: CreatePipe"); } if (!SetHandleInformation(pipe_read, HANDLE_FLAG_INHERIT, 0)) { CloseHandle(pipe_read); CloseHandle(pipe_write); - pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "SetHandleInformation"); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "GetJvmVersion: SetHandleInformation"); } PROCESS_INFORMATION processInfo = {0}; @@ -545,8 +524,7 @@ string GetJvmVersion(const string& java_exe) { CloseHandle(pipe_read); CloseHandle(pipe_write); pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "RunProgram/CreateProcess: Error %d while retrieving java version", - GetLastError()); + "RunProgram: CreateProcess(%s)", cmdline); } CloseHandle(pipe_write); @@ -613,14 +591,15 @@ 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, - "Cannot get start time of process in server dir %s", + "WriteProcessStartupTime(%s): GetProcessStartupTime", server_dir.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, - "Cannot write start time in server dir %s", server_dir.c_str()); + "WriteProcessStartupTime(%s): WriteFile(%s)", server_dir.c_str(), + start_time_file.c_str()); } } @@ -707,7 +686,8 @@ void ExecuteDaemon(const string& exe, const std::vector<string>& args_vector, wstring wdaemon_output; if (!blaze_util::AsWindowsPathWithUncPrefix(daemon_output, &wdaemon_output)) { pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "AsWindowsPathWithUncPrefix"); + "ExecuteDaemon(%s): AsWindowsPathWithUncPrefix(%s)", exe.c_str(), + daemon_output.c_str()); } SECURITY_ATTRIBUTES sa; @@ -722,13 +702,14 @@ void ExecuteDaemon(const string& exe, const std::vector<string>& args_vector, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); if (!devnull.IsValid()) { pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "ExecuteDaemon: Could not open NUL device"); + "ExecuteDaemon(%s): CreateFileA(NUL)", exe.c_str()); } AutoHandle stdout_file(CreateJvmOutputFile(wdaemon_output.c_str(), &sa)); if (!stdout_file.IsValid()) { pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "ExecuteDaemon: CreateJvmOutputFile %ls", wdaemon_output.c_str()); + "ExecuteDaemon(%s): CreateJvmOutputFile(%ls)", exe.c_str(), + wdaemon_output.c_str()); } HANDLE stderr_handle; // We must duplicate the handle to stdout, otherwise "bazel clean --expunge" @@ -744,7 +725,8 @@ void ExecuteDaemon(const string& exe, const std::vector<string>& args_vector, /* bInheritHandle */ TRUE, /* dwOptions */ DUPLICATE_SAME_ACCESS)) { pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "ExecuteDaemon: DuplicateHandle %ls", wdaemon_output.c_str()); + "ExecuteDaemon(%s): DuplicateHandle(%ls)", exe.c_str(), + wdaemon_output.c_str()); } AutoHandle stderr_file(stderr_handle); @@ -776,8 +758,7 @@ void ExecuteDaemon(const string& exe, const std::vector<string>& args_vector, if (!ok) { pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "ExecuteDaemon/CreateProcess: error %u executing: %s\n", - GetLastError(), cmdline.cmdline); + "ExecuteDaemon(%s): CreateProcess(%s)", exe.c_str(), cmdline.cmdline); } WriteProcessStartupTime(server_dir, processInfo.hProcess); @@ -830,7 +811,8 @@ static void MingwSignalHandler(int signum) { static bool IsFailureDueToNestedJobsNotSupported(HANDLE process) { BOOL is_in_job; if (!IsProcessInJob(process, NULL, &is_in_job)) { - PrintError("IsProcessInJob()"); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "IsFailureDueToNestedJobsNotSupported: IsProcessInJob"); return false; } @@ -860,7 +842,8 @@ void ExecuteProgram(const string& exe, const std::vector<string>& args_vector) { HANDLE job = CreateJobObject(NULL, NULL); if (job == NULL) { - pdie(255, "ExecuteProgram/CreateJobObject: error %u\n", GetLastError()); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "ExecuteProgram(%s): CreateJobObject", exe.c_str()); } JOBOBJECT_EXTENDED_LIMIT_INFORMATION job_info = {0}; @@ -869,8 +852,8 @@ void ExecuteProgram(const string& exe, const std::vector<string>& args_vector) { if (!SetInformationJobObject(job, JobObjectExtendedLimitInformation, &job_info, sizeof(job_info))) { - pdie(255, "ExecuteProgram/SetInformationJobObject: error %u\n", - GetLastError()); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "ExecuteProgram(%s): SetInformationJobObject", exe.c_str()); } BOOL success = CreateProcessA( @@ -886,8 +869,8 @@ void ExecuteProgram(const string& exe, const std::vector<string>& args_vector) { /* lpProcessInformation */ &processInfo); if (!success) { - pdie(255, "ExecuteProgram/CreateProcess: error %u executing: %s\n", - GetLastError(), cmdline.cmdline); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "ExecuteProgram(%s): CreateProcess(%s)", exe.c_str(), cmdline.cmdline); } // We will try to put the launched process into a Job object. This will make @@ -898,14 +881,15 @@ void ExecuteProgram(const string& exe, const std::vector<string>& args_vector) { // handling process management for us. if (!AssignProcessToJobObject(job, processInfo.hProcess) && !IsFailureDueToNestedJobsNotSupported(processInfo.hProcess)) { - pdie(255, "ExecuteProgram/AssignProcessToJobObject: error %u\n", - GetLastError()); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "ExecuteProgram(%s): AssignProcessToJobObject", exe.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(255, "ExecuteProgram/ResumeThread: error %u\n", GetLastError()); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "ExecuteProgram(%s): ResumeThread", exe.c_str()); } // msys doesn't deliver signals while a Win32 call is pending so we need to @@ -934,7 +918,8 @@ const char kListSeparator = ';'; string PathAsJvmFlag(const string& path) { string spath; if (!blaze_util::AsShortWindowsPath(path, &spath)) { - pdie(255, "PathAsJvmFlag(%s): AsShortWindowsPath failed", path.c_str()); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "PathAsJvmFlag(%s): AsShortWindowsPath", path.c_str()); } // Convert backslashes to forward slashes, in order to avoid the JVM parsing // Windows paths as if they contained escaped characters. @@ -948,7 +933,7 @@ string ConvertPath(const string& path) { // The path may not be Windows-style and may not be normalized, so convert it. wstring wpath; if (!blaze_util::AsWindowsPathWithUncPrefix(path, &wpath)) { - pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "ConvertPath(path=%s)", + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "ConvertPath(%s)", path.c_str()); } std::transform(wpath.begin(), wpath.end(), wpath.begin(), ::towlower); @@ -993,19 +978,22 @@ bool SymlinkDirectories(const string &posix_target, const string &posix_name) { wstring name; wstring target; if (!blaze_util::AsWindowsPathWithUncPrefix(posix_name, &name)) { - PrintError("SymlinkDirectories: AsWindowsPathWithUncPrefix(" + posix_name + - ")"); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "SymlinkDirectories(%s, %s): AsWindowsPathWithUncPrefix(%s)", + posix_target.c_str(), posix_name.c_str(), posix_target.c_str()); return false; } if (!blaze_util::AsWindowsPathWithUncPrefix(posix_target, &target)) { - PrintError("SymlinkDirectories: AsWindowsPathWithUncPrefix(" + - posix_target + ")"); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "SymlinkDirectories(%s, %s): AsWindowsPathWithUncPrefix(%s)", + posix_target.c_str(), posix_name.c_str(), posix_name.c_str()); return false; } string error(CreateJunction(name, target)); if (!error.empty()) { - PrintError("SymlinkDirectories(name=" + posix_name + - ", target=" + posix_target + "): " + error); + blaze_util::PrintError("SymlinkDirectories(%s, %s): CreateJunction: %s", + posix_target.c_str(), posix_name.c_str(), + error.c_str()); return false; } return true; @@ -1064,7 +1052,8 @@ bool KillServerProcess(int pid, const string& output_base) { if (!result || !AwaitServerProcessTermination(pid, output_base, kPostKillGracePeriodSeconds)) { pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "Cannot terminate server process with PID %d", pid); + "Cannot terminate server process with PID %d, output_base=(%s)", pid, + output_base.c_str()); } return result; } @@ -1106,8 +1095,7 @@ void CreateSecureOutputRoot(const string& path) { const char* root = path.c_str(); if (!blaze_util::MakeDirectories(path, 0755)) { pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "MakeDirectories(%s) failed: %s", root, - blaze_util::GetLastErrorString()); + "MakeDirectories(%s) failed", root); } #ifndef COMPILER_MSVC @@ -1237,14 +1225,16 @@ void SetupStdStreams() { LARGE_INTEGER WindowsClock::GetFrequency() { LARGE_INTEGER result; if (!QueryPerformanceFrequency(&result)) { - PrintError("QueryPerformanceFrequency"); - pdie(255, "Error getting time resolution\n"); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "WindowsClock::GetFrequency: QueryPerformanceFrequency"); } // 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(255, "QueryPerformanceFrequency returned invalid result (%llu)\n", + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "WindowsClock::GetFrequency: QueryPerformanceFrequency returned " + "invalid result (%llu)\n", result.QuadPart); } @@ -1255,8 +1245,8 @@ LARGE_INTEGER WindowsClock::GetMillisecondsAsLargeInt( const LARGE_INTEGER& freq) { LARGE_INTEGER counter; if (!QueryPerformanceCounter(&counter)) { - PrintError("QueryPerformanceCounter"); - pdie(255, "Error getting performance counter\n"); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "WindowsClock::GetMillisecondsAsLargeInt: QueryPerformanceCounter"); } LARGE_INTEGER result; @@ -1289,7 +1279,8 @@ uint64_t AcquireLock(const string& output_base, bool batch_mode, bool block, wstring wlockfile; if (!blaze_util::AsWindowsPathWithUncPrefix(lockfile, &wlockfile)) { pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "AcquireLock, lockfile=(%s)", lockfile.c_str()); + "AcquireLock(%s): AsWindowsPathWithUncPrefix(%s)", output_base.c_str(), + lockfile.c_str()); } blaze_lock->handle = INVALID_HANDLE_VALUE; @@ -1323,8 +1314,8 @@ uint64_t AcquireLock(const string& output_base, bool batch_mode, bool block, Sleep(/* dwMilliseconds */ 200); } else { pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "cannot open lockfile '%s', and not because it's held", - lockfile.c_str()); + "AcquireLock(%s): CreateFileW(%ls)", lockfile.c_str(), + wlockfile.c_str()); } } uint64_t wait_time = GetMillisecondsMonotonic() - st; @@ -1339,7 +1330,8 @@ uint64_t AcquireLock(const string& output_base, bool batch_mode, bool block, /* nNumberOfBytesToLockHigh */ 0, /* lpOverlapped */ &overlapped)) { pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "cannot lock the lockfile '%s'", lockfile.c_str()); + "AcquireLock(%s): LockFileEx(%ls)", lockfile.c_str(), + wlockfile.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 @@ -1364,9 +1356,8 @@ void ReleaseLock(BlazeLock* blaze_lock) { string GetUserName() { WCHAR buffer[UNLEN + 1]; DWORD len = UNLEN + 1; - if (!GetUserNameW(buffer, &len)) { - pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "ERROR: GetUserNameW failed, err=%d\n", GetLastError()); + if (!::GetUserNameW(buffer, &len)) { + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "GetUserNameW"); } return string(blaze_util::WstringToCstring(buffer).get()); } diff --git a/src/main/cpp/util/errors_windows.cc b/src/main/cpp/util/errors_windows.cc index b77ae21b26..292bb8a661 100644 --- a/src/main/cpp/util/errors_windows.cc +++ b/src/main/cpp/util/errors_windows.cc @@ -25,7 +25,7 @@ using std::stringstream; string GetLastErrorString() { DWORD last_error = ::GetLastError(); if (last_error == 0) { - return string(); + return "success"; } char* message_buffer; diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc index 17b4264ebb..ae15b71ed8 100644 --- a/src/main/cpp/util/file_windows.cc +++ b/src/main/cpp/util/file_windows.cc @@ -148,8 +148,7 @@ IPipe* CreatePipe() { 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 failed, err=%d", GetLastError()); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "CreatePipe"); } return new WindowsPipe(read_handle, write_handle); } @@ -184,7 +183,10 @@ bool WindowsFileMtime::GetIfInDistantFuture(const string& path, bool* result) { } wstring wpath; if (!AsWindowsPathWithUncPrefix(path, &wpath)) { - return false; + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "WindowsFileMtime::GetIfInDistantFuture(%s): " + "AsWindowsPathWithUncPrefix", + path.c_str()); } AutoHandle handle(::CreateFileW( @@ -234,6 +236,8 @@ bool WindowsFileMtime::Set(const string& path, const FILETIME& time) { } wstring wpath; if (!AsWindowsPathWithUncPrefix(path, &wpath)) { + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "WindowsFileMtime::Set(%s): AsWindowsPathWithUncPrefix", path.c_str()); return false; } @@ -264,8 +268,7 @@ FILETIME WindowsFileMtime::GetNow() { FILETIME file_time; if (!::SystemTimeToFileTime(&sys_time, &file_time)) { pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "WindowsFileMtime::GetNow: SystemTimeToFileTime failed, err=%d", - GetLastError()); + "WindowsFileMtime::GetNow: SystemTimeToFileTime"); } return file_time; } @@ -284,8 +287,7 @@ FILETIME WindowsFileMtime::GetFuture(WORD years) { FILETIME file_time; if (!::SystemTimeToFileTime(&future_time, &file_time)) { pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "WindowsFileMtime::GetFuture: SystemTimeToFileTime failed, err=%d", - GetLastError()); + "WindowsFileMtime::GetFuture: SystemTimeToFileTime"); } return file_time; } @@ -498,8 +500,6 @@ bool AsWindowsPathWithUncPrefix(const string& path, wstring* wpath, } if (!AsWindowsPath(path, wpath)) { - PrintError("AsWindowsPathWithUncPrefix(%s): AsWindowsPath failed, err=%d\n", - path.c_str(), GetLastError()); return false; } if (!IsAbsolute(path)) { @@ -519,6 +519,8 @@ bool AsShortWindowsPath(const string& path, string* result) { wstring wpath; wstring wsuffix; if (!AsWindowsPathWithUncPrefix(path, &wpath)) { + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "AsShortWindowsPath(%s): AsWindowsPathWithUncPrefix", path.c_str()); return false; } DWORD size = ::GetShortPathNameW(wpath.c_str(), nullptr, 0); @@ -559,8 +561,8 @@ bool AsShortWindowsPath(const string& path, string* result) { 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) failed, err=%d", - path.c_str(), wpath.c_str(), GetLastError()); + "AsShortWindowsPath(%s): GetShortPathNameW(%S)", path.c_str(), + wpath.c_str()); } // GetShortPathNameW may preserve the UNC prefix in the result, so strip it. wresult = wstring(RemoveUncPrefixMaybe(wshort.get())) + wsuffix; @@ -575,12 +577,15 @@ static bool OpenFileForReading(const string& filename, HANDLE* result) { if (filename.empty()) { return false; } + // TODO(laszlocsomor): remove the following check; it won't allow opening NUL. if (IsDevNull(filename)) { return true; } wstring wfilename; if (!AsWindowsPathWithUncPrefix(filename, &wfilename)) { - return false; + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "OpenFileForReading(%s): AsWindowsPathWithUncPrefix", + filename.c_str()); } *result = ::CreateFileW( /* lpFileName */ wfilename.c_str(), @@ -648,6 +653,8 @@ bool WriteFile(const void* data, size_t size, const string& filename, } wstring wpath; if (!AsWindowsPathWithUncPrefix(filename, &wpath)) { + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "WriteFile(%s): AsWindowsPathWithUncPrefix", filename.c_str()); return false; } @@ -688,19 +695,17 @@ int WriteToStdOutErr(const void* data, size_t size, bool to_stdout) { int RenameDirectory(const std::string& old_name, const std::string& new_name) { wstring wold_name; if (!AsWindowsPathWithUncPrefix(old_name, &wold_name)) { - PrintError( - "RenameDirectory(%s, %s): AsWindowsPathWithUncPrefix failed for" - " old_name", - old_name.c_str(), new_name.c_str()); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "RenameDirectory(%s, %s): AsWindowsPathWithUncPrefix(%s)", + old_name.c_str(), new_name.c_str(), old_name.c_str()); return kRenameDirectoryFailureOtherError; } wstring wnew_name; if (!AsWindowsPathWithUncPrefix(new_name, &wnew_name)) { - PrintError( - "RenameDirectory(%s, %s): AsWindowsPathWithUncPrefix failed for" - " new_name", - old_name.c_str(), new_name.c_str()); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "RenameDirectory(%s, %s): AsWindowsPathWithUncPrefix(%s)", + old_name.c_str(), new_name.c_str(), new_name.c_str()); return kRenameDirectoryFailureOtherError; } @@ -740,6 +745,8 @@ bool UnlinkPath(const string& file_path) { wstring wpath; if (!AsWindowsPathWithUncPrefix(file_path, &wpath)) { + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "UnlinkPath(%s): AsWindowsPathWithUncPrefix", file_path.c_str()); return false; } return UnlinkPathW(wpath); @@ -870,8 +877,8 @@ bool JunctionResolver::Resolve(const WCHAR* path, unique_ptr<WCHAR[]>* result) { bool ReadDirectorySymlink(const string& name, string* result) { wstring wname; if (!AsWindowsPathWithUncPrefix(name, &wname)) { - PrintError("ReadDirectorySymlink: AsWindowsPathWithUncPrefix(%s)", - name.c_str()); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "ReadDirectorySymlink(%s): AsWindowsPathWithUncPrefix", name.c_str()); return false; } unique_ptr<WCHAR[]> result_ptr; @@ -891,8 +898,8 @@ bool PathExists(const string& path) { } wstring wpath; if (!AsWindowsPathWithUncPrefix(path, &wpath)) { - PrintError("PathExists(%s): AsWindowsPathWithUncPrefix failed, err=%d\n", - path.c_str(), GetLastError()); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "PathExists(%s): AsWindowsPathWithUncPrefix", path.c_str()); return false; } return JunctionResolver().Resolve(wpath.c_str(), nullptr); @@ -903,13 +910,13 @@ string MakeCanonical(const char* path) { return "NUL"; } wstring wpath; - if (path == nullptr || path[0] == 0 || - !AsWindowsPathWithUncPrefix(path, &wpath)) { - if (path != nullptr && path[0] != 0) { - PrintError("MakeCanonical(%s): AsWindowsPathWithUncPrefix failed", path); - } + if (path == nullptr || path[0] == 0) { return ""; } + if (!AsWindowsPathWithUncPrefix(path, &wpath)) { + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "MakeCanonical(%s): AsWindowsPathWithUncPrefix", path); + } // Resolve all segments of the path. Do this from leaf to root, so we always // know that the path's tail is resolved and junctions may be found only in @@ -1010,6 +1017,8 @@ static bool CanReadFileW(const wstring& path) { bool CanReadFile(const std::string& path) { wstring wpath; if (!AsWindowsPathWithUncPrefix(path, &wpath)) { + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "CanReadFile(%s): AsWindowsPathWithUncPrefix", path.c_str()); return false; } return CanReadFileW(wpath); @@ -1018,6 +1027,8 @@ bool CanReadFile(const std::string& path) { bool CanExecuteFile(const std::string& path) { wstring wpath; if (!AsWindowsPathWithUncPrefix(path, &wpath)) { + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "CanExecuteFile(%s): AsWindowsPathWithUncPrefix", path.c_str()); return false; } return CanReadFileW(wpath) && (ends_with(wpath, wstring(L".exe")) || @@ -1029,6 +1040,8 @@ bool CanExecuteFile(const std::string& path) { bool CanAccessDirectory(const std::string& path) { wstring wpath; if (!AsWindowsPathWithUncPrefix(path, &wpath)) { + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "CanAccessDirectory(%s): AsWindowsPathWithUncPrefix", path.c_str()); return false; } DWORD attr = ::GetFileAttributesW(wpath.c_str()); @@ -1090,6 +1103,8 @@ bool IsDirectory(const string& path) { } wstring wpath; if (!AsWindowsPathWithUncPrefix(path, &wpath)) { + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "IsDirectory(%s): AsWindowsPathWithUncPrefix", path.c_str()); return false; } return IsDirectoryW(wpath); @@ -1121,18 +1136,12 @@ static bool MakeDirectoriesW(const wstring& path) { if (last_separator < 0) { // Since `path` is not a root directory, there must be at least one // directory above it. - pdie(255, "MakeDirectoriesW(%S), could not find dirname", path.c_str()); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "MakeDirectoriesW(%S), could not find dirname", path.c_str()); } wstring parent = path.substr(0, last_separator); - if (!MakeDirectoriesW(parent)) { - return false; - } - if (!::CreateDirectoryW(path.c_str(), nullptr)) { - PrintError("MakeDirectoriesW(%S), CreateDirectoryW failed, err=%d", - path.c_str(), GetLastError()); - return false; - } - return true; + return MakeDirectoriesW(parent) && + ::CreateDirectoryW(path.c_str(), NULL) == TRUE; } bool MakeDirectories(const string& path, unsigned int mode) { @@ -1145,6 +1154,8 @@ bool MakeDirectories(const string& path, unsigned int mode) { // According to MSDN, CreateDirectory's limit without the UNC prefix is // 248 characters (so it could fit another filename before reaching MAX_PATH). if (!AsWindowsPathWithUncPrefix(path, &wpath, 248)) { + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "MakeDirectories(%s): AsWindowsPathWithUncPrefix", path.c_str()); return false; } return MakeDirectoriesW(wpath); @@ -1154,7 +1165,7 @@ static unique_ptr<WCHAR[]> GetCwdW() { DWORD len = ::GetCurrentDirectoryW(0, nullptr); unique_ptr<WCHAR[]> cwd(new WCHAR[len]); if (!::GetCurrentDirectoryW(len, cwd.get())) { - die(255, "GetCurrentDirectoryW failed, err=%d\n", GetLastError()); + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "GetCurrentDirectoryW"); } return std::move(cwd); } @@ -1166,15 +1177,11 @@ string GetCwd() { bool ChangeDirectory(const string& path) { wstring wpath; if (!AsWindowsPathWithUncPrefix(path, &wpath)) { + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "ChangeDirectory(%s): AsWindowsPathWithUncPrefix", path.c_str()); return false; } - if (!::SetCurrentDirectoryW(wpath.c_str())) { - PrintError( - "ChangeDirectory(%s): SetCurrentDirectoryW(%S), failed, err=%d\n", - path.c_str(), wpath.c_str(), GetLastError()); - return false; - } - return true; + return ::SetCurrentDirectoryW(wpath.c_str()) == TRUE; } void ForEachDirectoryEntry(const string &path, @@ -1185,7 +1192,7 @@ void ForEachDirectoryEntry(const string &path, } if (!AsWindowsPath(path, &wpath)) { pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "ForEachDirectoryEntry(%s): AsWindowsPath failed", path.c_str()); + "ForEachDirectoryEntry(%s): AsWindowsPath", path.c_str()); } static const wstring kUncPrefix(L"\\\\?\\"); @@ -1222,8 +1229,8 @@ string NormalizeWindowsPath(string path) { } if (path[0] == '/') { // This is an absolute MSYS path, error out. - pdie(255, "NormalizeWindowsPath: expected a Windows path, path=(%s)", - path.c_str()); + pdie(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); diff --git a/src/main/native/windows/file.h b/src/main/native/windows/file.h index b1a57101ff..a8cbe71c5a 100644 --- a/src/main/native/windows/file.h +++ b/src/main/native/windows/file.h @@ -28,8 +28,11 @@ using std::wstring; template <typename char_type> bool HasUncPrefix(const char_type* path) { - return path[0] == '\\' && (path[1] == '\\' || path[1] == '?') && - (path[2] == '.' || path[2] == '?') && path[3] == '\\'; + // Return true iff `path` starts with "\\?\", "\\.\", or "\??\". + return path[0] == '\\' && + ((path[1] == '\\' && (path[2] == '?' || path[2] == '.')) || + (path[1] == '?' && path[2] == '?')) && + path[3] == '\\'; } // Keep in sync with j.c.g.devtools.build.lib.windows.WindowsFileOperations diff --git a/src/test/cpp/util/BUILD b/src/test/cpp/util/BUILD index bc995bfb05..119d99392b 100644 --- a/src/test/cpp/util/BUILD +++ b/src/test/cpp/util/BUILD @@ -35,7 +35,6 @@ cc_test( "file_posix_test.cc", ], }), - tags = ["manual"], deps = [ ":test_util", "//src/main/cpp/util:file", 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<char *>(&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; |