Module: check_mk
Branch: master
Commit: 7525fb1ce2697989bdc5a78ec49eba7f2fa4fdf8
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=7525fb1ce26979…
Author: Jukka Aro <ja(a)mathias-kettner.de>
Date: Fri Dec 22 13:31:22 2017 +0100
Windows agent: use RAII for search handles
Change-Id: I27b26e8c85a43671e60e4bee07a561a72b0fd61a
---
agents/windows/build_version | 2 +-
agents/windows/sections/SectionFileinfo.cc | 26 ++++++++++++--------------
agents/windows/sections/SectionLogwatch.cc | 11 +----------
agents/windows/sections/SectionSpool.cc | 12 ++++++------
agents/windows/types.h | 11 +++++++++++
5 files changed, 31 insertions(+), 31 deletions(-)
diff --git a/agents/windows/build_version b/agents/windows/build_version
index 488b08b..c930dca 100644
--- a/agents/windows/build_version
+++ b/agents/windows/build_version
@@ -1 +1 @@
-3036
+3038
diff --git a/agents/windows/sections/SectionFileinfo.cc
b/agents/windows/sections/SectionFileinfo.cc
index aa89c03..3eb4178 100644
--- a/agents/windows/sections/SectionFileinfo.cc
+++ b/agents/windows/sections/SectionFileinfo.cc
@@ -51,13 +51,13 @@ bool SectionFileinfo::produceOutputInner(std::ostream &out) {
void SectionFileinfo::get_directories(std::string base_path) {
WIN32_FIND_DATA findData;
- HANDLE findHandle;
std::stringstream ss;
ss << base_path << "\\"
<< "*.*";
- if ((findHandle = _winapi.FindFirstFile(ss.str().c_str(), &findData)) !=
- INVALID_HANDLE_VALUE) {
+ SearchHandle findHandle{_winapi.FindFirstFile(ss.str().c_str(), &findData),
+ _winapi};
+ if (findHandle) {
do {
if (strcmp(findData.cFileName, ".") &&
strcmp(findData.cFileName,
@@ -72,8 +72,7 @@ void SectionFileinfo::get_directories(std::string base_path) {
else
_temp_files.push_back(ss.str());
}
- } while (_winapi.FindNextFile(findHandle, &findData));
- _winapi.FindClose(findHandle);
+ } while (_winapi.FindNextFile(findHandle.get(), &findData));
}
}
@@ -91,12 +90,13 @@ void SectionFileinfo::determine_filepaths_full_search(
void SectionFileinfo::determine_filepaths_simple_search(
std::string base_path, std::string search_pattern) {
WIN32_FIND_DATA data;
- auto findHandle =
+ SearchHandle findHandle{
_winapi.FindFirstFileEx(search_pattern.c_str(), FindExInfoStandard,
- &data, FindExSearchNameMatch, NULL, 0);
+ &data, FindExSearchNameMatch, NULL, 0),
+ _winapi};
std::stringstream ss;
- if (findHandle != INVALID_HANDLE_VALUE) {
+ if (findHandle) {
do {
if (0 == (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
ss.str("");
@@ -104,8 +104,7 @@ void SectionFileinfo::determine_filepaths_simple_search(
ss << base_path << "\\" << data.cFileName;
_found_files.push_back(ss.str());
}
- } while (_winapi.FindNextFile(findHandle, &data));
- _winapi.FindClose(findHandle);
+ } while (_winapi.FindNextFile(findHandle.get(), &data));
}
}
@@ -144,16 +143,15 @@ void SectionFileinfo::outputFileinfos(std::ostream &out, const
char *path) {
bool SectionFileinfo::outputFileinfo(std::ostream &out,
const std::string filename) {
WIN32_FIND_DATA findData;
- HANDLE findHandle;
- if ((findHandle = _winapi.FindFirstFile(filename.c_str(), &findData)) !=
- INVALID_HANDLE_VALUE) {
+ SearchHandle findHandle{_winapi.FindFirstFile(filename.c_str(), &findData),
+ _winapi};
+ if (findHandle) {
unsigned long long size =
(unsigned long long)findData.nFileSizeLow +
(((unsigned long long)findData.nFileSizeHigh) << 32);
out << filename << "|" << size << "|"
<< std::fixed
<< std::setprecision(0) <<
file_time(&findData.ftLastWriteTime)
<< "\n";
- _winapi.FindClose(findHandle);
return true;
}
return false;
diff --git a/agents/windows/sections/SectionLogwatch.cc
b/agents/windows/sections/SectionLogwatch.cc
index a16ac51..dd69279 100644
--- a/agents/windows/sections/SectionLogwatch.cc
+++ b/agents/windows/sections/SectionLogwatch.cc
@@ -38,15 +38,6 @@ using std::ofstream;
namespace {
-struct SearchHandleTraits {
- using HandleT = HANDLE;
- static HandleT invalidValue() { return INVALID_HANDLE_VALUE; }
-
- static void closeHandle(HandleT value, const WinApiAdaptor &winapi) {
- winapi.FindClose(value);
- }
-};
-
const size_t UNICODE_BUFFER_SIZE = 8192;
// Process content of the given textfile
@@ -206,7 +197,7 @@ std::vector<SectionLogwatch::FileEntryType>
SectionLogwatch::globMatches(
}
WIN32_FIND_DATA data;
- WrappedHandle<SearchHandleTraits> searchHandle{
+ SearchHandle searchHandle{
_winapi.FindFirstFileEx(pattern, FindExInfoStandard, &data,
FindExSearchNameMatch, nullptr, 0),
_winapi};
diff --git a/agents/windows/sections/SectionSpool.cc
b/agents/windows/sections/SectionSpool.cc
index 6635e00..4ae7581 100644
--- a/agents/windows/sections/SectionSpool.cc
+++ b/agents/windows/sections/SectionSpool.cc
@@ -26,9 +26,9 @@
#include <sys/types.h>
#include "Environment.h"
#include "Logger.h"
-typedef short SHORT;
-#include "WinApiAdaptor.h"
#include "SectionSpool.h"
+#include "WinApiAdaptor.h"
+#include "types.h"
extern double file_time(const FILETIME *filetime);
@@ -64,12 +64,12 @@ bool SectionSpool::produceOutputInner(std::ostream &out) {
if (isdigit(*name)) max_age = atoi(name);
if (max_age >= 0) {
- HANDLE h =
+ SearchHandle searchHandle{
_winapi.FindFirstFileEx(path, FindExInfoStandard, &filedata,
- FindExSearchNameMatch, NULL, 0);
- if (h != INVALID_HANDLE_VALUE) {
+ FindExSearchNameMatch, NULL, 0),
+ _winapi};
+ if (searchHandle) {
double mtime = file_time(&(filedata.ftLastWriteTime));
- _winapi.FindClose(h);
int age = now - mtime;
if (age > max_age) {
Informational(_logger)
diff --git a/agents/windows/types.h b/agents/windows/types.h
index c4b20eb..b79aa77 100644
--- a/agents/windows/types.h
+++ b/agents/windows/types.h
@@ -482,4 +482,15 @@ struct ServiceHandleTraits {
using ServiceHandle = WrappedHandle<ServiceHandleTraits>;
+struct SearchHandleTraits {
+ using HandleT = HANDLE;
+ static HandleT invalidValue() { return INVALID_HANDLE_VALUE; }
+
+ static void closeHandle(HandleT value, const WinApiAdaptor &winapi) {
+ winapi.FindClose(value);
+ }
+};
+
+using SearchHandle = WrappedHandle<SearchHandleTraits>;
+
#endif // types_h