You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by ab...@apache.org on 2021/03/09 12:13:07 UTC

[nifi-minifi-cpp] branch main updated: MINIFICPP-1509 - Use _stat64 on windows

This is an automated email from the ASF dual-hosted git repository.

aboda pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git


The following commit(s) were added to refs/heads/main by this push:
     new f9279bc  MINIFICPP-1509 - Use _stat64 on windows
f9279bc is described below

commit f9279bc47caab16de282e7f22e0045dd0df85e39
Author: Adam Debreceni <ad...@apache.org>
AuthorDate: Wed Feb 24 14:26:54 2021 +0100

    MINIFICPP-1509 - Use _stat64 on windows
    
    Signed-off-by: Arpad Boda <ab...@apache.org>
    
    This closes #1015
---
 .../standard-processors/processors/GetFile.cpp     | 59 ++++++++++++----------
 .../standard-processors/processors/PutFile.cpp     | 10 +---
 libminifi/include/utils/file/FileUtils.h           | 38 +++++++-------
 3 files changed, 56 insertions(+), 51 deletions(-)

diff --git a/extensions/standard-processors/processors/GetFile.cpp b/extensions/standard-processors/processors/GetFile.cpp
index 5915866..98f2dd5 100644
--- a/extensions/standard-processors/processors/GetFile.cpp
+++ b/extensions/standard-processors/processors/GetFile.cpp
@@ -228,42 +228,49 @@ void GetFile::pollListing(std::queue<std::string> &list, const GetFileRequest &r
 bool GetFile::acceptFile(std::string fullName, std::string name, const GetFileRequest &request) {
   logger_->log_trace("Checking file: %s", fullName);
 
+#ifdef WIN32
+  struct _stat64 statbuf;
+  if (_stat64(fullName.c_str(), &statbuf) != 0) {
+    return false;
+  }
+#else
   struct stat statbuf;
+  if (stat(fullName.c_str(), &statbuf) != 0) {
+    return false;
+  }
+#endif
+  uint64_t file_size = gsl::narrow<uint64_t>(statbuf.st_size);
+  uint64_t modifiedTime = gsl::narrow<uint64_t>(statbuf.st_mtime) * 1000;
 
-  if (stat(fullName.c_str(), &statbuf) == 0) {
-    if (request.minSize > 0 && statbuf.st_size < (int32_t) request.minSize)
-      return false;
-
-    if (request.maxSize > 0 && statbuf.st_size > (int32_t) request.maxSize)
-      return false;
+  if (request.minSize > 0 && file_size < request.minSize)
+    return false;
 
-    uint64_t modifiedTime = ((uint64_t) (statbuf.st_mtime) * 1000);
-    uint64_t fileAge = utils::timeutils::getTimeMillis() - modifiedTime;
-    if (request.minAge > 0 && fileAge < request.minAge)
-      return false;
-    if (request.maxAge > 0 && fileAge > request.maxAge)
-      return false;
+  if (request.maxSize > 0 && file_size > request.maxSize)
+    return false;
 
-    if (request.ignoreHiddenFile && utils::file::FileUtils::is_hidden(fullName))
-      return false;
+  uint64_t fileAge = utils::timeutils::getTimeMillis() - modifiedTime;
+  if (request.minAge > 0 && fileAge < request.minAge)
+    return false;
+  if (request.maxAge > 0 && fileAge > request.maxAge)
+    return false;
 
-    if (utils::file::FileUtils::access(fullName.c_str(), R_OK) != 0)
-      return false;
+  if (request.ignoreHiddenFile && utils::file::FileUtils::is_hidden(fullName))
+    return false;
 
-    if (request.keepSourceFile == false && utils::file::FileUtils::access(fullName.c_str(), W_OK) != 0)
-      return false;
+  if (utils::file::FileUtils::access(fullName.c_str(), R_OK) != 0)
+    return false;
 
-    utils::Regex rgx(request.fileFilter);
-    if (!rgx.match(name)) {
-      return false;
-    }
+  if (request.keepSourceFile == false && utils::file::FileUtils::access(fullName.c_str(), W_OK) != 0)
+    return false;
 
-    metrics_->input_bytes_ += statbuf.st_size;
-    metrics_->accepted_files_++;
-    return true;
+  utils::Regex rgx(request.fileFilter);
+  if (!rgx.match(name)) {
+    return false;
   }
 
-  return false;
+  metrics_->input_bytes_ += file_size;
+  metrics_->accepted_files_++;
+  return true;
 }
 
 void GetFile::performListing(const GetFileRequest &request) {
diff --git a/extensions/standard-processors/processors/PutFile.cpp b/extensions/standard-processors/processors/PutFile.cpp
index 5503190..3765856 100644
--- a/extensions/standard-processors/processors/PutFile.cpp
+++ b/extensions/standard-processors/processors/PutFile.cpp
@@ -19,7 +19,6 @@
  */
 
 #include "PutFile.h"
-#include <sys/stat.h>
 #include <cstdint>
 #include <cstdio>
 #include <iostream>
@@ -149,9 +148,6 @@ void PutFile::onTrigger(core::ProcessContext *context, core::ProcessSession *ses
 
   logger_->log_debug("PutFile writing file %s into directory %s", filename, directory);
 
-  // If file exists, apply conflict resolution strategy
-  struct stat statResult;
-
   if ((max_dest_files_ != -1) && utils::file::FileUtils::is_directory(directory.c_str())) {
     int64_t count = 0;
 
@@ -171,7 +167,7 @@ void PutFile::onTrigger(core::ProcessContext *context, core::ProcessSession *ses
     }
   }
 
-  if (stat(destFile.c_str(), &statResult) == 0) {
+  if (utils::file::exists(destFile)) {
     logger_->log_warn("Destination file %s exists; applying Conflict Resolution Strategy: %s", destFile, conflict_resolution_);
 
     if (conflict_resolution_ == CONFLICT_RESOLUTION_STRATEGY_REPLACE) {
@@ -204,9 +200,7 @@ std::string PutFile::tmpWritePath(const std::string &filename, const std::string
 }
 
 bool PutFile::putFile(core::ProcessSession *session, std::shared_ptr<core::FlowFile> flowFile, const std::string &tmpFile, const std::string &destFile, const std::string &destDir) {
-  struct stat dir_stat;
-
-  if (stat(destDir.c_str(), &dir_stat) && try_mkdirs_) {
+  if (!utils::file::exists(destDir) && try_mkdirs_) {
     // Attempt to create directories in file's path
     std::stringstream dir_path_stream;
 
diff --git a/libminifi/include/utils/file/FileUtils.h b/libminifi/include/utils/file/FileUtils.h
index 7d1528a..1eaeb33 100644
--- a/libminifi/include/utils/file/FileUtils.h
+++ b/libminifi/include/utils/file/FileUtils.h
@@ -248,10 +248,9 @@ inline uint64_t last_write_time(const std::string &path) {
   if (ec.value() == 0) {
     return result;
   }
-#else
-#ifdef WIN32
-  struct _stat result;
-  if (_stat(path.c_str(), &result) == 0) {
+#elif defined(WIN32)
+  struct _stat64 result;
+  if (_stat64(path.c_str(), &result) == 0) {
     return result.st_mtime;
   }
 #else
@@ -260,7 +259,6 @@ inline uint64_t last_write_time(const std::string &path) {
     return result.st_mtime;
   }
 #endif
-#endif
   return 0;
 }
 
@@ -270,8 +268,8 @@ inline std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>
 
 inline uint64_t file_size(const std::string &path) {
 #ifdef WIN32
-  struct _stat result;
-  if (_stat(path.c_str(), &result) == 0) {
+  struct _stat64 result;
+  if (_stat64(path.c_str(), &result) == 0) {
     return result.st_size;
   }
 #else
@@ -334,25 +332,31 @@ inline bool get_uid_gid(const std::string &path, uint64_t &uid, uint64_t &gid) {
 #endif
 
 inline bool is_directory(const char * path) {
+#ifndef WIN32
   struct stat dir_stat;
-  if (stat(path, &dir_stat) < 0) {
+  if (stat(path, &dir_stat) != 0) {
+      return false;
+  }
+  return S_ISDIR(dir_stat.st_mode) != 0;
+#else
+  struct _stat64 dir_stat;
+  if (_stat64(path, &dir_stat) != 0) {
       return false;
   }
   return S_ISDIR(dir_stat.st_mode) != 0;
+#endif
 }
 
 inline bool exists(const std::string& path) {
 #ifdef USE_BOOST
   return boost::filesystem::exists(path);
-#else
-#ifdef WIN32
-  struct _stat statbuf;
-  return _stat(path.c_str(), &statbuf) == 0;
+#elif defined(WIN32)
+  struct _stat64 statbuf;
+  return _stat64(path.c_str(), &statbuf) == 0;
 #else
   struct stat statbuf;
   return stat(path.c_str(), &statbuf) == 0;
 #endif
-#endif
 }
 
 inline int create_dir(const std::string& path, bool recursive = true) {
@@ -473,11 +477,11 @@ inline void addFilesMatchingExtension(const std::shared_ptr<logging::Logger> &lo
   std::string pathToSearch = originalPath + "\\*" + extension;
   if ((hFind = FindFirstFileA(pathToSearch.c_str(), &FindFileData)) != INVALID_HANDLE_VALUE) {
     do {
-      struct _stat statbuf {};
+      struct _stat64 statbuf {};
 
       std::string path = originalPath + "\\" + FindFileData.cFileName;
       logger->log_info("Adding %s to paths", path);
-      if (_stat(path.c_str(), &statbuf) != 0) {
+      if (_stat64(path.c_str(), &statbuf) != 0) {
         logger->log_warn("Failed to stat %s", path);
         break;
       }
@@ -547,10 +551,10 @@ inline void list_dir(const std::string& dir, std::function<bool(const std::strin
   }
 
   do {
-    struct _stat statbuf {};
+    struct _stat64 statbuf {};
     if (strcmp(FindFileData.cFileName, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0) {
       std::string path = dir + get_separator() + FindFileData.cFileName;
-      if (_stat(path.c_str(), &statbuf) != 0) {
+      if (_stat64(path.c_str(), &statbuf) != 0) {
         logger->log_warn("Failed to stat %s", path);
         continue;
       }