aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/utils')
-rw-r--r--src/lib/utils/fs.cpp80
-rw-r--r--src/lib/utils/fs.h21
-rw-r--r--src/lib/utils/info.txt1
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