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 | |
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')
-rw-r--r-- | src/lib/cert/x509/certstor.cpp | 25 | ||||
-rw-r--r-- | src/lib/cert/x509/info.txt | 4 | ||||
-rw-r--r-- | src/lib/entropy/proc_walk/proc_walk.cpp | 7 | ||||
-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 |
6 files changed, 110 insertions, 28 deletions
diff --git a/src/lib/cert/x509/certstor.cpp b/src/lib/cert/x509/certstor.cpp index cbb0fd670..d7e6f0a65 100644 --- a/src/lib/cert/x509/certstor.cpp +++ b/src/lib/cert/x509/certstor.cpp @@ -6,10 +6,7 @@ */ #include <botan/certstor.h> - -#if defined(BOTAN_HAS_BOOST_FILESYSTEM) -#include <boost/filesystem.hpp> -#endif +#include <botan/fs.h> namespace Botan { @@ -116,25 +113,17 @@ Certificate_Store_In_Memory::Certificate_Store_In_Memory(const std::string& dir) if(dir == "") return; -#if defined(BOTAN_HAS_BOOST_FILESYSTEM) - boost::filesystem::recursive_directory_iterator i(dir); - boost::filesystem::recursive_directory_iterator end; - - while(i != end) + std::vector<std::string> maybe_certs = list_all_readable_files_in_or_under(dir); + for(auto&& cert_file : maybe_certs) { - auto path = i->path(); - ++i; - try { - if(boost::filesystem::is_regular_file(path)) - m_certs.push_back(X509_Certificate(path.string())); + m_certs.push_back(X509_Certificate(cert_file)); + } + catch(std::exception&) + { } - catch(...) {} } -#else - throw std::runtime_error("Certificate_Store_In_Memory: FS access disabled"); -#endif } const X509_Certificate* diff --git a/src/lib/cert/x509/info.txt b/src/lib/cert/x509/info.txt index 39e51a625..19db981bc 100644 --- a/src/lib/cert/x509/info.txt +++ b/src/lib/cert/x509/info.txt @@ -6,7 +6,3 @@ asn1 datastor http_util </requires> - -<libs> -all -> boost_filesystem -</libs> diff --git a/src/lib/entropy/proc_walk/proc_walk.cpp b/src/lib/entropy/proc_walk/proc_walk.cpp index 616c76ea3..55affda40 100644 --- a/src/lib/entropy/proc_walk/proc_walk.cpp +++ b/src/lib/entropy/proc_walk/proc_walk.cpp @@ -43,11 +43,6 @@ class Directory_Walker : public File_Descriptor_Source int next_fd(); private: - void add_directory(const std::string& dirname) - { - m_dirlist.push_back(dirname); - } - std::pair<struct dirent*, std::string> get_next_dirent(); std::pair<DIR*, std::string> m_cur_dir; @@ -99,7 +94,7 @@ int Directory_Walker::next_fd() if(S_ISDIR(stat_buf.st_mode)) { - add_directory(full_path); + m_dirlist.push_back(full_path); } else if(S_ISREG(stat_buf.st_mode) && (stat_buf.st_mode & S_IROTH)) { 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 |