Implemented stout/os/windows/exists.hpp. Review: https://reviews.apache.org/r/37344
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/989eca4d Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/989eca4d Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/989eca4d Branch: refs/heads/master Commit: 989eca4db7301f8cdae37cc9f801095ff0061929 Parents: 3b03427 Author: Alex Clemmer <[email protected]> Authored: Thu Sep 10 12:07:21 2015 -0700 Committer: Joris Van Remoortere <[email protected]> Committed: Thu Sep 10 17:15:06 2015 -0700 ---------------------------------------------------------------------- .../stout/include/stout/os/posix/exists.hpp | 3 ++ .../stout/include/stout/os/windows/exists.hpp | 51 ++++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/989eca4d/3rdparty/libprocess/3rdparty/stout/include/stout/os/posix/exists.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/posix/exists.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/posix/exists.hpp index 835466b..48489b8 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/os/posix/exists.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/posix/exists.hpp @@ -23,9 +23,11 @@ namespace os { + inline bool exists(const std::string& path) { struct stat s; + if (::lstat(path.c_str(), &s) < 0) { return false; } @@ -48,6 +50,7 @@ inline bool exists(pid_t pid) return false; } + } // namespace os { #endif // __STOUT_OS_POSIX_EXISTS_HPP__ http://git-wip-us.apache.org/repos/asf/mesos/blob/989eca4d/3rdparty/libprocess/3rdparty/stout/include/stout/os/windows/exists.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/windows/exists.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/windows/exists.hpp index 46f8fd2..ddcda7b 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/os/windows/exists.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/windows/exists.hpp @@ -16,23 +16,66 @@ #include <string> +#include <stout/windows.hpp> + namespace os { + inline bool exists(const std::string& path) { - UNIMPLEMENTED; + // TODO(hausdorff): (MESOS-3386) os.hpp is not yet fully ported to Windows, + // but when it is, we should change this to use `os::realpath` instead, + // rather than the raw `::realpath` API. + // + // NOTE: `::realpath` will correctly error out if path length is greater than + // `PATH_MAX`. + char absolutePath[PATH_MAX]; + + if (::realpath(path.c_str(), absolutePath) == NULL) { + return false; + } + + // NOTE: GetFileAttributes does not support unicode natively. See also + // "documentation"[1] for why this is a check-if-file-exists idiom. + // + // [1] http://blogs.msdn.com/b/oldnewthing/archive/2007/10/23/5612082.aspx + DWORD attributes = GetFileAttributes(absolutePath); + + bool fileNotFound = GetLastError() == ERROR_FILE_NOT_FOUND; + + return !((attributes == INVALID_FILE_ATTRIBUTES) && fileNotFound); } // Determine if the process identified by pid exists. -// NOTE: Zombie processes have a pid and therefore exist. See os::process(pid) -// to get details of a process. +// NOTE: Zombie processes have a pid and therefore exist. See +// os::process(pid) to get details of a process. inline bool exists(pid_t pid) { - UNIMPLEMENTED; + HANDLE handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid); + + // NOTE: `GetExitCode` will gracefully deal with the case that `handle` is + // `NULL`. + DWORD exitCode = 0; + BOOL exitCodeExists = GetExitCodeProcess(handle, &exitCode); + + // `CloseHandle`, on the other hand, will throw an exception in the + // VS debugger if you pass it a broken handle. (cf. "Return value" + // section of the documentation[1].) + // + // [1] https://msdn.microsoft.com/en-us/library/windows/desktop/ms724211(v=vs.85).aspx + if (handle != NULL) { + CloseHandle(handle); + } + + // NOTE: Windows quirk, the exit code returned by the process can + // be the same number as `STILL_ACTIVE`, in which case this + // function will mis-report that the process still exists. + return exitCodeExists && (exitCode == STILL_ACTIVE); } + } // namespace os { #endif // __STOUT_OS_WINDOWS_EXISTS_HPP__
