szaszm commented on a change in pull request #1138:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1138#discussion_r680918382



##########
File path: libminifi/src/utils/file/FileMatcher.cpp
##########
@@ -0,0 +1,294 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "utils/file/FileMatcher.h"
+#include "utils/file/FileUtils.h"
+#include "utils/StringUtils.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+namespace file {
+
+std::shared_ptr<core::logging::Logger> FileMatcher::FilePattern::logger_ = 
logging::LoggerFactory<FileMatcher::FilePattern>::getLogger();
+std::shared_ptr<core::logging::Logger> FileMatcher::logger_ = 
logging::LoggerFactory<FileMatcher>::getLogger();
+
+static bool isGlobPattern(const std::string& pattern) {
+  return pattern.find_first_of("?*") != std::string::npos;
+}
+
+static std::vector<std::string> split(const std::string& str, const 
std::vector<std::string>& delimiters) {
+  std::vector<std::string> result;
+
+  size_t prev_delim_end = 0;
+  size_t next_delim_begin = std::string::npos;
+  do {
+    for (const auto& delim : delimiters) {
+      next_delim_begin = str.find(delim, prev_delim_end);
+      if (next_delim_begin != std::string::npos) {
+        result.push_back(str.substr(prev_delim_end, next_delim_begin - 
prev_delim_end));
+        prev_delim_end = next_delim_begin + delim.length();
+        break;
+      }
+    }
+  } while (next_delim_begin != std::string::npos);
+  result.push_back(str.substr(prev_delim_end));
+  return result;
+}
+
+#ifdef WIN32
+static const std::vector<std::string> path_separators{"/", "\\"};
+#else
+static const std::vector<std::string> path_separators{"/"};
+#endif
+
+optional<FileMatcher::FilePattern> 
FileMatcher::FilePattern::fromPattern(std::string pattern) {
+  pattern = utils::StringUtils::trim(pattern);
+  bool excluding = false;
+  if (!pattern.empty() && pattern[0] == '!') {
+    excluding = true;
+    pattern = utils::StringUtils::trim(pattern.substr(1));
+  }
+  if (pattern.empty()) {
+    logger_->log_error("Empty pattern");
+    return nullopt;
+  }
+  std::string exe_dir = get_executable_dir();
+  if (exe_dir.empty() && !isAbsolutePath(pattern.c_str())) {
+    logger_->log_error("Couldn't determine executable dir, relative pattern 
'%s' not supported", pattern);
+    return nullopt;
+  }
+  pattern = resolve(exe_dir, pattern);
+  auto segments = split(pattern, path_separators);
+  gsl_Expects(!segments.empty());
+  auto file_pattern = segments.back();
+  if (file_pattern == "**") {
+    file_pattern = "*";
+  } else {
+    segments.pop_back();
+  }
+  if (file_pattern == "." || file_pattern == "..") {
+    logger_->log_error("Invalid file pattern '%s'", file_pattern);
+    return nullopt;
+  }
+  bool after_wildcard = false;
+  for (const auto& segment : segments) {
+    if (after_wildcard && segment == "..") {
+      logger_->log_error("Parent accessor is not supported after wildcards");
+      return nullopt;
+    }
+    if (isGlobPattern(segment)) {
+      after_wildcard = true;
+    }
+  }
+  return FilePattern(segments, file_pattern, excluding);
+}
+
+std::string FileMatcher::FilePattern::getBaseDirectory() const {
+  std::string base_dir;
+  for (const auto& segment : directory_segments_) {
+    // ignore segments at or after wildcards
+    if (isGlobPattern(segment)) {
+      break;
+    }
+    base_dir += segment + get_separator();
+  }
+  return base_dir;
+}
+
+FileMatcher::FileMatcher(const std::string &patterns) {
+  for (auto&& pattern : split(patterns, {","})) {
+    if (auto&& p = FilePattern::fromPattern(pattern)) {
+      patterns_.push_back(std::move(p.value()));
+    }
+  }
+}
+
+template<typename It>
+static bool advance_if_not_equal(It& it, const It& end) {
+  if (it == end) {
+    return false;
+  }
+  ++it;
+  return true;
+}
+
+static bool is_this_dir(const std::string& dir) {
+  return dir.empty() || dir == ".";
+}
+
+template<typename It, typename Fn>
+static void skip_if(It& it, const It& end, const Fn& fn) {
+  while (it != end && fn(*it)) {
+    ++it;
+  }
+}
+
+static bool matchGlob(std::string::const_iterator pattern_it, 
std::string::const_iterator pattern_end, std::string::const_iterator value_it, 
std::string::const_iterator value_end) {

Review comment:
       #1142 seems to be quite close to merge




-- 
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.

To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to