diff --git a/userspace/falco/app/actions/create_requested_paths.cpp b/userspace/falco/app/actions/create_requested_paths.cpp index e87f45f0..4c44eb84 100644 --- a/userspace/falco/app/actions/create_requested_paths.cpp +++ b/userspace/falco/app/actions/create_requested_paths.cpp @@ -18,6 +18,7 @@ limitations under the License. #include "actions.h" #include "falco_utils.h" #include +#include #ifndef CPPPATH_SEP #ifdef _MSC_VER @@ -93,23 +94,15 @@ falco::app::run_result falco::app::actions::create_requested_paths(falco::app::s static int create_dir(const std::string &path) { - // Properly reset errno - errno = 0; - std::istringstream f(path); - std::string path_until_token; - std::string s; - // Create all the subfolder stopping at last token (f.eof()); - // Examples: - // "/tmp/foo/bar" -> "", "tmp", "foo" -> mkdir("/") + mkdir("/tmp/") + midir("/tmp/foo/") - // "tmp/foo/bar" -> "tmp", "foo" -> mkdir("tmp/") + midir("tmp/foo/") - while (getline(f, s, *CPPPATH_SEP) && !f.eof()) { - path_until_token += s + CPPPATH_SEP; - int ret = mkdir(path_until_token.c_str(), 0600); - if (ret != 0 && errno != EEXIST) - { - return ret; - } - } - return 0; + std::filesystem::path dirPath(path); + + try { + std::filesystem::create_directories(dirPath); + } catch (const std::exception& ex) { + return -1; + } + + return 0; + } diff --git a/userspace/falco/configuration.cpp b/userspace/falco/configuration.cpp index 48f29b60..d571322f 100644 --- a/userspace/falco/configuration.cpp +++ b/userspace/falco/configuration.cpp @@ -22,10 +22,12 @@ limitations under the License. #include #include -#include +#include #include #include +#ifndef _WIN32 #include +#endif #include "falco_utils.h" #include "configuration.h" @@ -33,6 +35,8 @@ limitations under the License. #include +namespace fs = std::filesystem; + // Reference: https://digitalfortress.tech/tips/top-15-commonly-used-regex/ static re2::RE2 ip_address_re("((^\\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\\s*$)|(^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1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))(%.+)?\\s*$))"); @@ -453,17 +457,9 @@ void falco_configuration::load_yaml(const std::string& config_name, const yaml_h void falco_configuration::read_rules_file_directory(const std::string &path, std::list &rules_filenames, std::list &rules_folders) { - struct stat st; + fs::path rules_path = std::string(path); - int rc = stat(path.c_str(), &st); - - if(rc != 0) - { - std::cerr << "Could not get info on rules file " << path << ": " << strerror(errno) << std::endl; - exit(-1); - } - - if(st.st_mode & S_IFDIR) + if(fs::is_directory(rules_path)) { rules_folders.push_back(path); @@ -472,34 +468,17 @@ void falco_configuration::read_rules_file_directory(const std::string &path, std // rules_filenames std::vector dir_filenames; - DIR *dir = opendir(path.c_str()); + const auto it_options = fs::directory_options::follow_directory_symlink + | fs::directory_options::follow_directory_symlink; - if(!dir) + for (auto const& dir_entry : fs::directory_iterator(rules_path, it_options)) { - std::cerr << "Could not get read contents of directory " << path << ": " << strerror(errno) << std::endl; - exit(-1); - } - - for(struct dirent *ent = readdir(dir); ent; ent = readdir(dir)) - { - std::string efile = path + "/" + ent->d_name; - - rc = stat(efile.c_str(), &st); - - if(rc != 0) + if(std::filesystem::is_regular_file(dir_entry.path())) { - std::cerr << "Could not get info on rules file " << efile << ": " << strerror(errno) << std::endl; - exit(-1); - } - - if(st.st_mode & S_IFREG) - { - dir_filenames.push_back(efile); + dir_filenames.push_back(dir_entry.path().string()); } } - closedir(dir); - std::sort(dir_filenames.begin(), dir_filenames.end());