bakaid commented on a change in pull request #681: MINIFICPP-1085
URL: https://github.com/apache/nifi-minifi-cpp/pull/681#discussion_r346419830
 
 

 ##########
 File path: extensions/windows-event-log/Bookmark.cpp
 ##########
 @@ -0,0 +1,217 @@
+#include "Bookmark.h"
+
+#include <direct.h>
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace processors {
+
+Bookmark::Bookmark(const std::string& uuid, std::shared_ptr<logging::Logger> 
logger)
+  :logger_(logger) {
+  if (createUUIDDir(uuid, filePath_)) {
+    filePath_ += "Bookmark.txt";
+  }
+
+  if (!filePath_.empty()) {
+    if (!getBookmarkXmlFromFile()) {
+      return;
+    }
+  }
+
+  if (bookmarkXml_.empty()) {
+    if (!(hBookmark_ = EvtCreateBookmark(0))) {
+      logger_->log_error("!EvtCreateBookmark error: %d", GetLastError());
+      return;
+    }
+  } else {
+    if (!(hBookmark_ = EvtCreateBookmark(bookmarkXml_.c_str()))) {
+      logger_->log_error("!EvtCreateBookmark error: %d bookmarkXml_ '%s'", 
GetLastError(), bookmarkXml_.c_str());
+      return;
+    }
+  }
+
+  ok_ = true;
+}
+
+Bookmark::~Bookmark() {
+  if (file_.is_open()) {
+    file_.close();
+  }
+
+  if (hBookmark_) {
+    EvtClose(hBookmark_);
+  }
+}
+
+Bookmark::operator bool() {
+  return ok_;
+}
+  
+bool Bookmark::hasBookmarkXml() {
+  return !bookmarkXml_.empty();
+}
+
+EVT_HANDLE Bookmark::bookmarkHandle() {
+  return hBookmark_;
+}
+
+bool Bookmark::saveBookmark(EVT_HANDLE hEvent)
+{
+  if (!EvtUpdateBookmark(hBookmark_, hEvent)) {
+    logger_->log_error("!EvtUpdateBookmark error: %d.", GetLastError());
+    return false;
+  }
+
+  // Render the bookmark as an XML string that can be persisted.
+  DWORD bufferSize{};
+  DWORD bufferUsed{};
+  DWORD propertyCount{};
+  if (!EvtRender(0, hBookmark_, EvtRenderBookmark, bufferSize, 0, &bufferUsed, 
&propertyCount)) {
+    DWORD status = ERROR_SUCCESS;
+    if (ERROR_INSUFFICIENT_BUFFER == (status = GetLastError())) {
+      bufferSize = bufferUsed;
+
+      std::vector<wchar_t> buf(bufferSize / 2 + 1);
+
+      if (!EvtRender(0, hBookmark_, EvtRenderBookmark, bufferSize, &buf[0], 
&bufferUsed, &propertyCount)) {
+        logger_->log_error("!EvtRender error: %d.", GetLastError());
+        return false;
+      }
+
+      file_.seekp(std::ios::beg);
+
+      // Write new bookmark over old and in the end write '!'. Then new 
bookmark is read until '!'.
+      file_ << &buf[0] << L'!';
+
+      file_.flush();
+    }
+    else if (ERROR_SUCCESS != (status = GetLastError())) {
+      logger_->log_error("!EvtRender error: %d.", GetLastError());
+      return false;
+    }
+  }
+
+  return true;
+}
+
+bool Bookmark::createEmptyBookmarkXmlFile() {
+  if (file_.is_open()) {
+    file_.close();
+  }
+
+  file_.open(filePath_, std::ios::out);
+
+  auto ret = file_.is_open();
+  if (!ret) {
+    logger_->log_error("Cannot open %s", filePath_.c_str());
+  }
+
+  return ret;
+}
+
+// Creates directory "processor_repository\ConsumeWindowsEventLog\uuid\{uuid}" 
under "bin" directory.
 
 Review comment:
   This is bad practice, we never place or access anything relative to the 
executable's path, only relative to MINIFI_HOME or the values configured in 
properties or the config.yml.
   Please take a look at how TailFile's state storage is implemented (it has a 
Property to set it).
   By the way (for testing reasons) getting the executable's path and dir are 
already implemented in `FileUtils::get_executable_path` and 
`FileUtils::get_executable_dir`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to