diff options
author | lloyd <[email protected]> | 2015-02-21 14:14:45 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2015-02-21 14:14:45 +0000 |
commit | 5ef7108d620a00ce5b2f6997c8b6ffc0708467d6 (patch) | |
tree | 670771c55dfb1e77e0783cd791355327ecdf1ef6 /src/lib/utils | |
parent | ee2ac0c46d0c76e04b0fa68f9bb73825b60a4b09 (diff) |
Hide all uses of boost filesystem in fs.cpp. Use readdir as an
alternate implementation for Unix and add some feature checks so a
boost-free build of the tests and command line are possible again.
Diffstat (limited to 'src/lib/utils')
-rw-r--r-- | src/lib/utils/fs.cpp | 80 | ||||
-rw-r--r-- | src/lib/utils/fs.h | 21 | ||||
-rw-r--r-- | src/lib/utils/info.txt | 1 |
3 files changed, 102 insertions, 0 deletions
diff --git a/src/lib/utils/fs.cpp b/src/lib/utils/fs.cpp new file mode 100644 index 000000000..268512325 --- /dev/null +++ b/src/lib/utils/fs.cpp @@ -0,0 +1,80 @@ +/* +* (C) 2015 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include <botan/fs.h> +#include <algorithm> +#include <deque> + +#if defined(BOTAN_HAS_BOOST_FILESYSTEM) + #include <boost/filesystem.hpp> + +#elif defined(BOTAN_TARGET_OS_HAS_READDIR) + #include <sys/types.h> + #include <sys/stat.h> + #include <dirent.h> +#endif + +namespace Botan { + +std::vector<std::string> +list_all_readable_files_in_or_under(const std::string& dir_path) + { + std::vector<std::string> paths; + +#if defined(BOTAN_HAS_BOOST_FILESYSTEM) + namespace fs = boost::filesystem; + + fs::recursive_directory_iterator end; + for(fs::recursive_directory_iterator dir(dir_path); dir != end; ++dir) + { + if(fs::is_regular_file(dir->path())) + paths.push_back(dir->path().string()); + } + +#elif defined(BOTAN_TARGET_OS_HAS_READDIR) + + std::deque<std::string> dir_list; + dir_list.push_back(dir_path); + + while(!dir_list.empty()) + { + const std::string cur_path = dir_list[0]; + dir_list.pop_front(); + + std::unique_ptr<DIR, std::function<int (DIR*)>> dir(::opendir(cur_path.c_str()), ::closedir); + + if(dir) + { + while(struct dirent* dirent = ::readdir(dir.get())) + { + const std::string filename = dirent->d_name; + if(filename == "." || filename == "..") + continue; + const std::string full_path = cur_path + '/' + filename; + + struct stat stat_buf; + + if(::lstat(full_path.c_str(), &stat_buf) == -1) + continue; + + if(S_ISDIR(stat_buf.st_mode)) + dir_list.push_back(full_path); + else if(S_ISREG(stat_buf.st_mode)) + paths.push_back(full_path); + } + } + } +#else + #warning "No filesystem access enabled" +#endif + + std::sort(paths.begin(), paths.end()); + + return paths; + } + +} + diff --git a/src/lib/utils/fs.h b/src/lib/utils/fs.h new file mode 100644 index 000000000..25ec5ecd1 --- /dev/null +++ b/src/lib/utils/fs.h @@ -0,0 +1,21 @@ +/* +* (C) 2015 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#ifndef BOTAN_UTIL_FS_H__ +#define BOTAN_UTIL_FS_H__ + +#include <botan/types.h> +#include <vector> +#include <string> + +namespace Botan { + +BOTAN_DLL std::vector<std::string> +list_all_readable_files_in_or_under(const std::string& dir); + +} + +#endif diff --git a/src/lib/utils/info.txt b/src/lib/utils/info.txt index fafb73c72..90f45aa3f 100644 --- a/src/lib/utils/info.txt +++ b/src/lib/utils/info.txt @@ -21,6 +21,7 @@ charset.h cpuid.h database.h exceptn.h +fs.h get_byte.h loadstor.h mem_ops.h |