aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils/fs.cpp
blob: 268512325b854b484cd2dd4bb5eb0c36d11b172b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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;
   }

}