szaszm commented on a change in pull request #1138: URL: https://github.com/apache/nifi-minifi-cpp/pull/1138#discussion_r704552398
########## File path: libminifi/test/unit/FilePatternTests.cpp ########## @@ -0,0 +1,253 @@ +/** + * 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. + */ + +#define CUSTOM_EXTENSION_INIT + +#include <filesystem> + +#include "../TestBase.h" +#include "utils/file/FilePattern.h" +#include "range/v3/view/transform.hpp" +#include "range/v3/view/map.hpp" +#include "range/v3/view/join.hpp" +#include "range/v3/view/cache1.hpp" +#include "range/v3/view/c_str.hpp" +#include "range/v3/range/conversion.hpp" +#include "range/v3/range.hpp" + +struct FilePatternTestAccessor { + using FilePatternSegment = fileutils::FilePattern::FilePatternSegment; +}; + +using FilePatternSegment = FilePatternTestAccessor::FilePatternSegment; +using FilePattern = fileutils::FilePattern; + +#define REQUIRE_INCLUDE(val) REQUIRE((val == FilePatternSegment::MatchResult::INCLUDE)) +#define REQUIRE_EXCLUDE(val) REQUIRE((val == FilePatternSegment::MatchResult::EXCLUDE)) +#define REQUIRE_NOT_MATCHING(val) REQUIRE((val == FilePatternSegment::MatchResult::NOT_MATCHING)) + +#ifdef WIN32 +static std::filesystem::path root{"C:\\"}; +#else +static std::filesystem::path root{"/"}; +#endif + +TEST_CASE("Invalid paths") { + REQUIRE_THROWS(FilePatternSegment("")); + REQUIRE_THROWS(FilePatternSegment(".")); + REQUIRE_THROWS(FilePatternSegment("..")); + REQUIRE_THROWS(FilePatternSegment("!")); + REQUIRE_THROWS(FilePatternSegment("!.")); + REQUIRE_THROWS(FilePatternSegment("!..")); + // parent accessor after wildcard + FilePatternSegment("./../file.txt"); // sanity check + REQUIRE_THROWS(FilePatternSegment("./*/../file.txt")); +} + +TEST_CASE("FilePattern reports error in correct subpattern") { + std::vector<std::pair<std::string, std::string>> invalid_subpattern{ Review comment: This could use string_view instead of string for simpler generated code. In general, static strings don't need to be copied to the heap with std::string, because they will stay in static memory. https://godbolt.org/z/8GGe3qfao (600 something asm lines with string_view) vs https://godbolt.org/z/zeW38svPa (1100 lines with string) ########## File path: libminifi/src/core/extension/DynamicLibrary.cpp ########## @@ -0,0 +1,195 @@ +/** + * 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 <memory> +#ifndef WIN32 +#include <dlfcn.h> +#define DLL_EXPORT +#else +#include <system_error> +#define WIN32_LEAN_AND_MEAN 1 +#include <Windows.h> // Windows specific libraries for collecting software metrics. +#include <Psapi.h> +#pragma comment(lib, "psapi.lib" ) +#define DLL_EXPORT __declspec(dllexport) +#define RTLD_LAZY 0 +#define RTLD_NOW 0 + +#define RTLD_GLOBAL (1 << 1) +#define RTLD_LOCAL (1 << 2) +#endif + +#include "core/extension/DynamicLibrary.h" +#include "core/extension/Extension.h" +#include "utils/GeneralUtils.h" +#include "core/logging/LoggerConfiguration.h" + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { +namespace core { +namespace extension { + +std::shared_ptr<logging::Logger> DynamicLibrary::logger_ = logging::LoggerFactory<DynamicLibrary>::getLogger(); + +DynamicLibrary::DynamicLibrary(std::string name, std::filesystem::path library_path) + : Module(std::move(name)), + library_path_(std::move(library_path)) { +} + +bool DynamicLibrary::load() { + dlerror(); + handle_ = dlopen(library_path_.string().c_str(), RTLD_NOW | RTLD_LOCAL); + if (!handle_) { + logger_->log_error("Failed to load extension '%s' at '%s': %s", name_, library_path_.string(), dlerror()); + return false; + } else { + logger_->log_trace("Loaded extension '%s' at '%s'", name_, library_path_.string()); + return true; + } +} + +bool DynamicLibrary::unload() { + logger_->log_trace("Unloading library '%s' at '%s'", name_, library_path_.string()); + if (!handle_) { + logger_->log_error("Extension does not have a handle_ '%s' at '%s'", name_, library_path_.string()); + return true; + } + dlerror(); + if (dlclose(handle_)) { + logger_->log_error("Failed to unload extension '%s' at '%': %s", name_, library_path_.string(), dlerror()); + return false; + } + logger_->log_trace("Unloaded extension '%s' at '%s'", name_, library_path_.string()); + handle_ = nullptr; + return true; +} + +DynamicLibrary::~DynamicLibrary() = default; + +#ifdef WIN32 + +void DynamicLibrary::store_error() { + auto error = GetLastError(); + + if (error == 0) { + error_str_ = ""; + return; + } + + current_error_ = std::system_category().message(error); +} + +void* DynamicLibrary::dlsym(void* handle, const char* name) { + FARPROC symbol; + + symbol = GetProcAddress((HMODULE)handle, name); + + if (symbol == nullptr) { + store_error(); + + for (auto hndl : resource_mapping_) { + symbol = GetProcAddress((HMODULE)hndl.first, name); + if (symbol != nullptr) { + break; + } + } + } + +#ifdef _MSC_VER +#pragma warning(suppress: 4054 ) +#endif + return reinterpret_cast<void*>(symbol); +} + +const char* DynamicLibrary::dlerror() { + error_str_ = current_error_; + + current_error_ = ""; + + return error_str_.c_str(); +} + +void* DynamicLibrary::dlopen(const char* file, int mode) { + HMODULE object; + uint32_t uMode = SetErrorMode(SEM_FAILCRITICALERRORS); + if (nullptr == file) { + HMODULE allModules[1024]; + HANDLE current_process_handle = GetCurrentProcess(); + DWORD cbNeeded; + object = GetModuleHandle(NULL); + + if (!object) { + store_error(); + } + + if (EnumProcessModules(current_process_handle, allModules, sizeof(allModules), &cbNeeded) != 0) { + for (uint32_t i = 0; i < cbNeeded / sizeof(HMODULE); i++) { + // Get the full path to the module's file. + resource_mapping_.insert(std::make_pair(reinterpret_cast<void*>(allModules[i]), "minifi-system")); Review comment: nitpicking: static_cast is enough here (and a few other places), as it's the same as reinterpret_cast for void*. > Any object pointer type T1* can be converted to another object pointer type cv T2*. This is exactly equivalent to static_cast<cv T2*>(static_cast<cv void*>(expression)) (from the [cppreference reinterpret_cast page](https://en.cppreference.com/w/cpp/language/reinterpret_cast)) ########## File path: libminifi/include/core/logging/Logger.h ########## @@ -26,6 +26,7 @@ #include <iostream> #include <vector> #include <algorithm> +#include <string_view> Review comment: I'm assuming this is a leftover from a suggestion to implement conditional_conversion for string_view that couldn't be done. ########## File path: libminifi/src/core/extension/ExtensionManager.cpp ########## @@ -0,0 +1,136 @@ +/** + * 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 "core/extension/ExtensionManager.h" +#include "core/logging/LoggerConfiguration.h" +#include "utils/file/FileUtils.h" +#include "core/extension/Executable.h" +#include "utils/file/FilePattern.h" +#include "core/extension/DynamicLibrary.h" + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { +namespace core { +namespace extension { + +namespace { + +struct LibraryDescriptor { + std::string name; + std::filesystem::path dir; + std::string filename; + + bool verify(const std::shared_ptr<logging::Logger>& /*logger*/) const { + // TODO(adebreceni): check signature + return true; + } + + std::filesystem::path getFullPath() const { + return dir / filename; + } Review comment: Consider marking getters `[[nodiscard]]`. Clang-tidy warns me for all of these with modernize-use-nodiscard through CLion. ########## File path: libminifi/include/core/extension/DynamicLibrary.h ########## @@ -0,0 +1,69 @@ +/** + * 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. + */ + +#pragma once + +#include <memory> +#include <map> +#include <string> +#include <filesystem> + +#include "Module.h" + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { +namespace core { +namespace extension { + +class DynamicLibrary : public Module { + friend class ExtensionManager; + + public: + DynamicLibrary(std::string name, std::filesystem::path library_path); + ~DynamicLibrary() override; + + private: +#ifdef WIN32 + std::map<void*, std::string> resource_mapping_; + + std::string error_str_; + std::string current_error_; + + void store_error(); + void* dlsym(void* handle, const char* name); + const char* dlerror(); + void* dlopen(const char* file, int mode); + int dlclose(void* handle); +#endif + + bool load(); + bool unload(); + + std::filesystem::path library_path_; + gsl::owner<void*> handle_ = nullptr; + + static std::shared_ptr<logging::Logger> logger_; Review comment: This issue seems to be back ########## File path: libminifi/include/utils/file/FilePattern.h ########## @@ -0,0 +1,118 @@ +/** + * 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. + */ + +#pragma once + +#include <string> +#include <vector> +#include <utility> +#include <memory> +#include <filesystem> +#include <set> + +#include "utils/OptionalUtils.h" +#include "core/logging/Logger.h" Review comment: Logger and OptionalUtils are not used in this file, so I think it's best to remove them. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
