Module: check_mk
Branch: master
Commit: aa3df7b74c875eac0546f92accc46ed77ccef5c3
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=aa3df7b74c875e…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Tue Feb 28 15:14:29 2017 +0100
Use fs::directory_iterator instead of home-grown code.
Change-Id: I8b481064917fad09daca110fd4da270cfe4efd6a
---
livestatus/src/LogCache.cc | 35 ++++++++---------------------------
livestatus/src/LogCache.h | 4 ++--
livestatus/src/Logfile.cc | 16 ++++++++--------
livestatus/src/Logfile.h | 5 +++--
livestatus/src/LogwatchListColumn.cc | 26 +++++++++++---------------
5 files changed, 32 insertions(+), 54 deletions(-)
diff --git a/livestatus/src/LogCache.cc b/livestatus/src/LogCache.cc
index 7a22b5c..a59b6fb 100644
--- a/livestatus/src/LogCache.cc
+++ b/livestatus/src/LogCache.cc
@@ -23,9 +23,7 @@
// Boston, MA 02110-1301 USA.
#include "LogCache.h"
-#include <dirent.h>
-#include <cerrno>
-#include <ostream>
+#include <sstream>
#include <string>
#include <utility>
#include "Logfile.h"
@@ -97,34 +95,17 @@ void LogCache::updateLogfileIndex() {
scanLogfile(log_file, true);
// TODO(sp) Avoid global variable
- string dirpath = log_archive_path;
- DIR *dir = opendir(dirpath.c_str());
- if (dir == nullptr) {
- generic_error ge("cannot open log archive " + dirpath);
- Informational(_logger) << ge;
- return;
- }
-
- while (true) {
- errno = 0;
- dirent *ent = readdir(dir);
- if (ent == nullptr) {
- if (errno != 0) {
- generic_error ge("cannot read directory entry from " +
dirpath);
- Warning(_logger) << ge;
- }
- break;
- }
- string name = ent->d_name;
- if (name != "." && name != "..") {
- scanLogfile(dirpath + "/" + ent->d_name, false);
+ fs::path dirpath = log_archive_path;
+ try {
+ for (const auto &entry : fs::directory_iterator(dirpath)) {
+ scanLogfile(dirpath / entry.path(), false);
}
+ } catch (const fs::filesystem_error &e) {
+ Warning(_logger) << "error while iterating directory: " <<
e.what();
}
-
- closedir(dir);
}
-void LogCache::scanLogfile(const string &path, bool watch) {
+void LogCache::scanLogfile(const fs::path &path, bool watch) {
auto logfile = new Logfile(_logger, _commands_holder, path, watch);
time_t since = logfile->since();
if (since != 0) {
diff --git a/livestatus/src/LogCache.h b/livestatus/src/LogCache.h
index cdfcbcb..d96ffe9 100644
--- a/livestatus/src/LogCache.h
+++ b/livestatus/src/LogCache.h
@@ -30,7 +30,7 @@
#include <ctime>
#include <map>
#include <mutex>
-#include <string>
+#include "FileSystem.h"
#include "nagios.h" // IWYU pragma: keep
class Column;
class CommandsHolder;
@@ -71,7 +71,7 @@ public:
bool logCachePreChecks(MonitoringCore *core);
private:
- void scanLogfile(const std::string &path, bool watch);
+ void scanLogfile(const fs::path &path, bool watch);
_logfiles_t::iterator findLogfileStartingBefore(time_t);
};
diff --git a/livestatus/src/Logfile.cc b/livestatus/src/Logfile.cc
index 9bedbf9..c28af1a 100644
--- a/livestatus/src/Logfile.cc
+++ b/livestatus/src/Logfile.cc
@@ -26,7 +26,7 @@
#include "Logfile.h"
#include <fcntl.h>
#include <cstdlib>
-#include <fstream>
+#include <sstream>
#include <utility>
#include "LogCache.h"
#include "LogEntry.h"
@@ -53,9 +53,9 @@ using std::vector;
extern unsigned long g_max_lines_per_logfile;
Logfile::Logfile(Logger *logger, const CommandsHolder &commands_holder,
- string path, bool watch)
+ const fs::path &path, bool watch)
: _commands_holder(commands_holder)
- , _path(move(path))
+ , _path(path)
, _since(0)
, _watch(watch)
, _read_pos{}
@@ -67,7 +67,7 @@ Logfile::Logfile(Logger *logger, const CommandsHolder
&commands_holder,
, _logclasses_read(0) {
ifstream is(_path, ios::binary);
if (!is) {
- generic_error ge("cannot open logfile " + _path);
+ generic_error ge("cannot open logfile " + _path.string());
Informational(_logger) << ge;
return;
}
@@ -144,7 +144,7 @@ void Logfile::load(LogCache *logcache, time_t since, time_t until,
file = fopen(_path.c_str(), "r");
if (file == nullptr) {
- generic_error ge("cannot open logfile " + _path);
+ generic_error ge("cannot open logfile " + _path.string());
Informational(_logger) << ge;
return;
}
@@ -288,7 +288,7 @@ unique_ptr<vector<char>> Logfile::readIntoBuffer() {
unique_ptr<vector<char>> result;
ifstream is(_path, ios::binary | ios::ate);
if (!is) {
- generic_error ge("cannot open logfile " + _path);
+ generic_error ge("cannot open logfile " + _path.string());
Warning(_logger) << ge;
return result;
}
@@ -296,7 +296,7 @@ unique_ptr<vector<char>> Logfile::readIntoBuffer() {
auto end = is.tellg();
is.seekg(0, ios::beg);
if (!is) {
- generic_error ge("cannot determine size of " + _path);
+ generic_error ge("cannot determine size of " + _path.string());
Warning(_logger) << ge;
return result;
}
@@ -314,7 +314,7 @@ unique_ptr<vector<char>> Logfile::readIntoBuffer() {
is.read(&result->front() + 1, size);
if (!is) {
generic_error ge("cannot open read " + to_string(size) +
- " byted from " + _path);
+ " byted from " + _path.string());
Warning(_logger) << ge;
}
diff --git a/livestatus/src/Logfile.h b/livestatus/src/Logfile.h
index fc2aebb..9f168d0 100644
--- a/livestatus/src/Logfile.h
+++ b/livestatus/src/Logfile.h
@@ -31,6 +31,7 @@
#include <ctime>
#include <map>
#include <string>
+#include "FileSystem.h"
class CommandsHolder;
class LogCache;
class Query;
@@ -49,7 +50,7 @@ typedef std::map<uint64_t, LogEntry *>
class Logfile {
private:
const CommandsHolder &_commands_holder;
- std::string _path;
+ fs::path _path;
time_t _since; // time of first entry
bool _watch; // true only for current logfile
fpos_t _read_pos; // read until this position
@@ -63,7 +64,7 @@ private:
public:
Logfile(Logger *logger, const CommandsHolder &commands_holder,
- std::string path, bool watch);
+ const fs::path &path, bool watch);
~Logfile();
std::string path() { return _path; }
diff --git a/livestatus/src/LogwatchListColumn.cc b/livestatus/src/LogwatchListColumn.cc
index 90f51ac..fbf08b0 100644
--- a/livestatus/src/LogwatchListColumn.cc
+++ b/livestatus/src/LogwatchListColumn.cc
@@ -23,7 +23,9 @@
// Boston, MA 02110-1301 USA.
#include "LogwatchListColumn.h"
-#include <dirent.h>
+#include <ostream>
+#include "FileSystem.h"
+#include "Logger.h"
#include "Renderer.h"
#include "pnp4nagios.h"
@@ -50,21 +52,15 @@ void LogwatchListColumn::output(void *row, RowRenderer &r,
#endif
ListRenderer l(r);
- if (_logwatch_path.empty()) {
- return;
- }
- string path = _logwatch_path + pnp_cleanup(host_name);
- if (DIR *dir = opendir(path.c_str())) {
- while (true) {
- if (dirent *ent = readdir(dir)) {
- string name = ent->d_name;
- if (name != "." && name != "..") {
- l.output(name);
- }
- } else {
- break;
+ if (!_logwatch_path.empty()) {
+ try {
+ for (const auto &entry : fs::directory_iterator(
+ _logwatch_path + pnp_cleanup(host_name))) {
+ l.output(entry.path().string());
}
+ } catch (const fs::filesystem_error &e) {
+ Warning(logger()) << "error while iterating directory: "
+ << e.what();
}
- closedir(dir);
}
}