diff options
author | Simon Warta <[email protected]> | 2015-07-16 19:42:54 +0200 |
---|---|---|
committer | Simon Warta <[email protected]> | 2015-07-16 19:42:54 +0200 |
commit | cff04b4039ffbb4ee1d7309edb62ce702bb471bc (patch) | |
tree | bae6bc6de24dbdfd732c1734c78aeae566837ece /src | |
parent | acac09fc411eeb8d52f4565ba50c057298552679 (diff) | |
parent | 748ae07caf2445f7b75f5c96ae674c6de0b0610a (diff) |
Merge pull request #201 from webmaster128/fs
Refactor internal/filesystem.h
Diffstat (limited to 'src')
-rw-r--r-- | src/build-data/os/windows.txt | 1 | ||||
-rw-r--r-- | src/lib/cert/x509/certstor.cpp | 4 | ||||
-rw-r--r-- | src/lib/utils/exceptn.h | 8 | ||||
-rw-r--r-- | src/lib/utils/filesystem.cpp | 129 | ||||
-rw-r--r-- | src/lib/utils/filesystem.h (renamed from src/lib/utils/fs.h) | 8 | ||||
-rw-r--r-- | src/lib/utils/fs.cpp | 86 | ||||
-rw-r--r-- | src/lib/utils/info.txt | 24 | ||||
-rw-r--r-- | src/tests/nist_x509.cpp | 24 | ||||
-rw-r--r-- | src/tests/tests.cpp | 21 |
9 files changed, 185 insertions, 120 deletions
diff --git a/src/build-data/os/windows.txt b/src/build-data/os/windows.txt index b72ef0768..c34f2e7ab 100644 --- a/src/build-data/os/windows.txt +++ b/src/build-data/os/windows.txt @@ -18,6 +18,7 @@ loadlibrary query_perf_counter virtual_lock rtlsecurezeromemory +stl_filesystem_msvc </target_features> <aliases> diff --git a/src/lib/cert/x509/certstor.cpp b/src/lib/cert/x509/certstor.cpp index d075fe706..e3498f602 100644 --- a/src/lib/cert/x509/certstor.cpp +++ b/src/lib/cert/x509/certstor.cpp @@ -6,7 +6,7 @@ */ #include <botan/certstor.h> -#include <botan/fs.h> +#include <botan/internal/filesystem.h> namespace Botan { @@ -118,7 +118,7 @@ Certificate_Store_In_Memory::Certificate_Store_In_Memory(const std::string& dir) if(dir == "") return; - std::vector<std::string> maybe_certs = list_all_readable_files_in_or_under(dir); + std::vector<std::string> maybe_certs = get_files_recursive(dir); for(auto&& cert_file : maybe_certs) { try diff --git a/src/lib/utils/exceptn.h b/src/lib/utils/exceptn.h index 7a10319c5..06db697bf 100644 --- a/src/lib/utils/exceptn.h +++ b/src/lib/utils/exceptn.h @@ -158,6 +158,14 @@ struct BOTAN_DLL Stream_IO_Error : public Exception }; /** +* No_Filesystem_Access Exception +*/ +struct BOTAN_DLL No_Filesystem_Access : public Exception + { + No_Filesystem_Access() : Exception("No filesystem access enabled.") {} + }; + +/** * Self Test Failure Exception */ struct BOTAN_DLL Self_Test_Failure : public Internal_Error diff --git a/src/lib/utils/filesystem.cpp b/src/lib/utils/filesystem.cpp new file mode 100644 index 000000000..950d4d4e2 --- /dev/null +++ b/src/lib/utils/filesystem.cpp @@ -0,0 +1,129 @@ +/* +* (C) 2015 Jack Lloyd +* (C) 2015 Simon Warta (Kullo GmbH) +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include <botan/exceptn.h> +#include <botan/internal/filesystem.h> +#include <algorithm> + +#if defined(BOTAN_TARGET_OS_HAS_STL_FILESYSTEM_MSVC) && defined(BOTAN_BUILD_COMPILER_IS_MSVC) + #include <filesystem> +#elif 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> + #include <deque> + #include <memory> + #include <functional> +#endif + +namespace Botan { + +namespace { + +#if defined(BOTAN_TARGET_OS_HAS_STL_FILESYSTEM_MSVC) && defined(BOTAN_BUILD_COMPILER_IS_MSVC) +std::vector<std::string> impl_stl_filesystem(const std::string& dir) + { + using namespace std::tr2::sys; + + std::vector<std::string> out; + + path p(dir); + + if (is_directory(p)) + { + for (recursive_directory_iterator itr(p), end; itr != end; ++itr) + { + if (is_regular_file(itr->path())) + { + out.push_back(itr->path().string()); + } + } + } + + return out; + } +#elif defined(BOTAN_HAS_BOOST_FILESYSTEM) +std::vector<std::string> impl_boost_filesystem(const std::string& dir_path) +{ + namespace fs = boost::filesystem; + + std::vector<std::string> out; + + for(fs::recursive_directory_iterator dir(dir_path), end; dir != end; ++dir) + { + if(fs::is_regular_file(dir->path())) + { + out.push_back(dir->path().string()); + } + } + + return out; +} +#elif defined(BOTAN_TARGET_OS_HAS_READDIR) +std::vector<std::string> impl_readdir(const std::string& dir_path) + { + std::vector<std::string> out; + 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)) + out.push_back(full_path); + } + } + } + + return out; + } +#endif + +} + +std::vector<std::string> get_files_recursive(const std::string& dir) + { + std::vector<std::string> files; + +#if defined(BOTAN_TARGET_OS_HAS_STL_FILESYSTEM_MSVC) && defined(BOTAN_BUILD_COMPILER_IS_MSVC) + files = impl_stl_filesystem(dir); +#elif defined(BOTAN_HAS_BOOST_FILESYSTEM) + files = impl_boost_filesystem(dir); +#elif defined(BOTAN_TARGET_OS_HAS_READDIR) + files = impl_readdir(dir); +#else + throw No_Filesystem_Access(); +#endif + + std::sort(files.begin(), files.end()); + + return files; + } + +} diff --git a/src/lib/utils/fs.h b/src/lib/utils/filesystem.h index 25ec5ecd1..419f94b99 100644 --- a/src/lib/utils/fs.h +++ b/src/lib/utils/filesystem.h @@ -1,11 +1,12 @@ /* * (C) 2015 Jack Lloyd +* (C) 2015 Simon Warta (Kullo GmbH) * * Botan is released under the Simplified BSD License (see license.txt) */ -#ifndef BOTAN_UTIL_FS_H__ -#define BOTAN_UTIL_FS_H__ +#ifndef BOTAN_UTIL_FILESYSTEM_H__ +#define BOTAN_UTIL_FILESYSTEM_H__ #include <botan/types.h> #include <vector> @@ -13,8 +14,7 @@ namespace Botan { -BOTAN_DLL std::vector<std::string> -list_all_readable_files_in_or_under(const std::string& dir); +BOTAN_DLL std::vector<std::string> get_files_recursive(const std::string& dir); } diff --git a/src/lib/utils/fs.cpp b/src/lib/utils/fs.cpp deleted file mode 100644 index b1ada17db..000000000 --- a/src/lib/utils/fs.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/* -* (C) 2015 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#include <botan/fs.h> -#include <algorithm> -#include <deque> -#include <functional> -#include <memory> - -#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 -#if defined(_MSC_VER) - #pragma message ( "No filesystem access enabled" ) -#else - #warning "No filesystem access enabled" -#endif -#endif - - std::sort(paths.begin(), paths.end()); - - return paths; - } - -} - diff --git a/src/lib/utils/info.txt b/src/lib/utils/info.txt index 90f45aa3f..b8c7b85d2 100644 --- a/src/lib/utils/info.txt +++ b/src/lib/utils/info.txt @@ -2,17 +2,6 @@ define UTIL_FUNCTIONS 20140123 load_on always -<header:internal> -bit_ops.h -donna128.h -prefetch.h -rounding.h -semaphore.h -stl_util.h -ta_utils.h -xor_buf.h -</header:internal> - <header:public> assert.h bswap.h @@ -21,7 +10,6 @@ charset.h cpuid.h database.h exceptn.h -fs.h get_byte.h loadstor.h mem_ops.h @@ -31,3 +19,15 @@ rotate.h types.h version.h </header:public> + +<header:internal> +bit_ops.h +donna128.h +filesystem.h +prefetch.h +rounding.h +semaphore.h +stl_util.h +ta_utils.h +xor_buf.h +</header:internal> diff --git a/src/tests/nist_x509.cpp b/src/tests/nist_x509.cpp index 734f2b355..25578d239 100644 --- a/src/tests/nist_x509.cpp +++ b/src/tests/nist_x509.cpp @@ -18,7 +18,7 @@ #if defined(BOTAN_HAS_X509_CERTIFICATES) #include <botan/x509path.h> -#include <botan/fs.h> +#include <botan/internal/filesystem.h> #include <algorithm> #include <iostream> @@ -37,10 +37,14 @@ size_t test_nist_x509() const std::string root_test_dir = "src/tests/data/nist_x509/"; const size_t total_tests = 76; - if(list_all_readable_files_in_or_under(root_test_dir).empty()) + try { - std::cout << "No FS access, skipping NIST X.509 validation tests" << std::endl; - test_report("NIST X.509 path validation", 0, 0); + // Do nothing, just test filesystem access + get_files_recursive(root_test_dir); + } + catch(No_Filesystem_Access) + { + std::cout << "Warning: No filesystem access, skipping NIST X.509 validation tests" << std::endl; return 0; } @@ -57,17 +61,17 @@ size_t test_nist_x509() for(size_t test_no = 1; test_no <= total_tests; ++test_no) { const std::string test_dir = root_test_dir + "/test" + (test_no <= 9 ? "0" : "") + std::to_string(test_no); - const std::vector<std::string> all_files = list_all_readable_files_in_or_under(test_dir); + + const std::vector<std::string> all_files = get_files_recursive(test_dir); + if (all_files.empty()) + std::cout << "Warning: No test files found in '" << test_dir << "'" << std::endl; std::vector<std::string> certs, crls; std::string root_cert, to_verify; - for(size_t k = 0; k != all_files.size(); k++) + for(const auto ¤t : all_files) { - const std::string current = all_files[k]; - - if(current.find("int") != std::string::npos && - current.find(".crt") != std::string::npos) + if(current.find("int") != std::string::npos && current.find(".crt") != std::string::npos) certs.push_back(current); else if(current.find("root.crt") != std::string::npos) root_cert = current; diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp index de83965f1..a0fd17879 100644 --- a/src/tests/tests.cpp +++ b/src/tests/tests.cpp @@ -8,7 +8,7 @@ #include <iostream> #include <fstream> #include <botan/auto_rng.h> -#include <botan/fs.h> +#include <botan/internal/filesystem.h> #define CATCH_CONFIG_RUNNER #define CATCH_CONFIG_CONSOLE_WIDTH 60 @@ -19,6 +19,8 @@ #include <botan/system_rng.h> #endif +using namespace Botan; + Botan::RandomNumberGenerator& test_rng() { #if defined(BOTAN_HAS_SYSTEM_RNG) @@ -33,14 +35,21 @@ size_t run_tests_in_dir(const std::string& dir, std::function<size_t (const std: { size_t fails = 0; - auto files = Botan::list_all_readable_files_in_or_under(dir); - if (files.empty()) + try + { + auto files = get_files_recursive(dir); + + if (files.empty()) + std::cout << "Warning: No test files found in '" << dir << "'" << std::endl; + + for(const auto file: files) + fails += fn(file); + } + catch(No_Filesystem_Access) { - std::cout << "Warning: No test files found in '" << dir << "'" << std::endl; + std::cout << "Warning: No filesystem access available to read test files in '" << dir << "'" << std::endl; } - for(const auto file: files) - fails += fn(file); return fails; } |