Repository: mesos Updated Branches: refs/heads/master cf3bc15f5 -> aa16bbd90
Converted os::ls to return a Try Review: https://reviews.apache.org/r/20499 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/14cc94a1 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/14cc94a1 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/14cc94a1 Branch: refs/heads/master Commit: 14cc94a15a84bf22ae7aee072a9de933c60d6311 Parents: cf3bc15 Author: Dominic Hamon <[email protected]> Authored: Fri Apr 18 13:55:50 2014 -0700 Committer: Dominic Hamon <[email protected]> Committed: Wed Jul 30 10:25:30 2014 -0700 ---------------------------------------------------------------------- .../3rdparty/stout/include/stout/os.hpp | 31 +++++++++++--------- .../3rdparty/stout/include/stout/os/ls.hpp | 13 ++++---- .../3rdparty/stout/include/stout/os/setns.hpp | 7 +++-- .../3rdparty/stout/include/stout/proc.hpp | 18 +++++++++--- .../3rdparty/stout/tests/os_tests.cpp | 7 +++-- 5 files changed, 48 insertions(+), 28 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/14cc94a1/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp index 85b49d9..0117fa5 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp @@ -783,20 +783,23 @@ inline Try<std::list<std::string> > find( return Error("'" + directory + "' is not a directory"); } - foreach (const std::string& entry, ls(directory)) { - std::string path = path::join(directory, entry); - // If it's a directory, recurse. - if (isdir(path) && !islink(path)) { - Try<std::list<std::string> > matches = find(path, pattern); - if (matches.isError()) { - return matches; - } - foreach (const std::string& match, matches.get()) { - results.push_back(match); - } - } else { - if (entry.find(pattern) != std::string::npos) { - results.push_back(path); // Matched the file pattern! + Try<std::list<std::string> > entries = ls(directory); + if (entries.isSome()) { + foreach (const std::string& entry, entries.get()) { + std::string path = path::join(directory, entry); + // If it's a directory, recurse. + if (isdir(path) && !islink(path)) { + Try<std::list<std::string> > matches = find(path, pattern); + if (matches.isError()) { + return matches; + } + foreach (const std::string& match, matches.get()) { + results.push_back(match); + } + } else { + if (entry.find(pattern) != std::string::npos) { + results.push_back(path); // Matched the file pattern! + } } } } http://git-wip-us.apache.org/repos/asf/mesos/blob/14cc94a1/3rdparty/libprocess/3rdparty/stout/include/stout/os/ls.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/ls.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/ls.hpp index e916f02..7cd4a40 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/os/ls.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/ls.hpp @@ -23,13 +23,12 @@ namespace os { -// TODO(bmahler): Wrap this with a Try. -inline std::list<std::string> ls(const std::string& directory) +inline Try<std::list<std::string> > ls(const std::string& directory) { DIR* dir = opendir(directory.c_str()); if (dir == NULL) { - return std::list<std::string>(); + return ErrnoError("Failed to opendir '" + directory + "'"); } // Calculate the size for a "directory entry". @@ -48,9 +47,10 @@ inline std::list<std::string> ls(const std::string& directory) dirent* temp = (dirent*) malloc(size); if (temp == NULL) { - free(temp); + // Preserve malloc error. + ErrnoError error("Failed to allocate directory entries"); closedir(dir); - return std::list<std::string>(); + return error; } std::list<std::string> result; @@ -68,7 +68,8 @@ inline std::list<std::string> ls(const std::string& directory) closedir(dir); if (error != 0) { - return std::list<std::string>(); + errno = error; + return ErrnoError("Failed to read directories"); } return result; http://git-wip-us.apache.org/repos/asf/mesos/blob/14cc94a1/3rdparty/libprocess/3rdparty/stout/include/stout/os/setns.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/setns.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/setns.hpp index cf2215a..5278996 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/os/setns.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/setns.hpp @@ -46,8 +46,11 @@ namespace os { inline std::set<std::string> namespaces() { std::set<std::string> result; - foreach (const std::string& ns, os::ls("/proc/self/ns")) { - result.insert(ns); + Try<std::list<std::string> > entries = os::ls("/proc/self/ns"); + if (entries.isSome()) { + foreach (const std::string& entry, entries.get()) { + result.insert(entry); + } } return result; } http://git-wip-us.apache.org/repos/asf/mesos/blob/14cc94a1/3rdparty/libprocess/3rdparty/stout/include/stout/proc.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/proc.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/proc.hpp index c550963..0004fa5 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/proc.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/proc.hpp @@ -297,10 +297,15 @@ inline Try<std::set<pid_t> > pids() { std::set<pid_t> pids; - foreach (const std::string& file, os::ls("/proc")) { - Try<pid_t> pid = numify<pid_t>(file); + Try<std::list<std::string> > entries = os::ls("/proc"); + if (entries.isError()) { + return Error("Failed to list files in /proc: " + entries.error()); + } + + foreach (const std::string& entry, entries.get()) { + Try<pid_t> pid = numify<pid_t>(entry); if (pid.isSome()) { - pids.insert(pid.get()); // Ignore files that can't be numified. + pids.insert(pid.get()); // Ignore entries that can't be numified. } } @@ -319,7 +324,12 @@ inline Try<std::set<pid_t> > threads(pid_t pid) std::set<pid_t> threads; - foreach (const std::string& entry, os::ls(path)) { + Try<std::list<std::string> > entries = os::ls(path); + if (entries.isError()) { + return Error("Failed to list files in " + path + ": " + entries.error()); + } + + foreach (const std::string& entry, entries.get()) { Try<pid_t> thread = numify<pid_t>(entry); if (thread.isSome()) { threads.insert(thread.get()); http://git-wip-us.apache.org/repos/asf/mesos/blob/14cc94a1/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp index 3f2ba47..84ef49c 100644 --- a/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp +++ b/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp @@ -46,8 +46,11 @@ using std::vector; static hashset<string> listfiles(const string& directory) { hashset<string> fileset; - foreach (const string& file, os::ls(directory)) { - fileset.insert(file); + Try<std::list<std::string> > entries = os::ls(directory); + if (entries.isSome()) { + foreach (const string& entry, entries.get()) { + fileset.insert(entry); + } } return fileset; }
