• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 12976993364

26 Jan 2025 05:51PM UTC coverage: 91.251% (-0.005%) from 91.256%
12976993364

push

github

web-flow
Merge pull request #4599 from randombit/jack/split-api-and-compiler-headers

Split compiler.h into two headers

93963 of 102972 relevant lines covered (91.25%)

11437005.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.29
/src/lib/utils/filesystem.cpp
1
/*
2
* (C) 2015,2017,2019 Jack Lloyd
3
* (C) 2015 Simon Warta (Kullo GmbH)
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/exceptn.h>
9

10
#include <botan/assert.h>
11
#include <botan/internal/filesystem.h>
12
#include <algorithm>
13
#include <deque>
14
#include <memory>
15
#include <sstream>
16

17
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
18
   #include <dirent.h>
19
   #include <functional>
20
   #include <sys/stat.h>
21
   #include <sys/types.h>
22
#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
23
   #define NOMINMAX 1
24
   #define _WINSOCKAPI_  // stop windows.h including winsock.h
25
   #include <windows.h>
26
#endif
27

28
namespace Botan {
29

30
namespace {
31

32
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
33

34
std::vector<std::string> impl_readdir(std::string_view dir_path) {
277✔
35
   std::vector<std::string> out;
277✔
36
   std::deque<std::string> dir_list;
277✔
37
   dir_list.push_back(std::string(dir_path));
554✔
38

39
   while(!dir_list.empty()) {
645✔
40
      const std::string cur_path = dir_list[0];
368✔
41
      dir_list.pop_front();
368✔
42

43
      std::unique_ptr<DIR, std::function<int(DIR*)>> dir(::opendir(cur_path.c_str()), ::closedir);
736✔
44

45
      if(dir) {
368✔
46
         while(struct dirent* dirent = ::readdir(dir.get())) {
2,565✔
47
            const std::string filename = dirent->d_name;
2,208✔
48
            if(filename == "." || filename == "..") {
2,208✔
49
               continue;
714✔
50
            }
51

52
            std::ostringstream full_path_sstr;
1,494✔
53
            full_path_sstr << cur_path << "/" << filename;
1,494✔
54
            const std::string full_path = full_path_sstr.str();
1,494✔
55

56
            struct stat stat_buf;
1,494✔
57

58
            if(::stat(full_path.c_str(), &stat_buf) == -1) {
1,494✔
59
               continue;
×
60
            }
61

62
            if(S_ISDIR(stat_buf.st_mode)) {
1,494✔
63
               dir_list.push_back(full_path);
91✔
64
            } else if(S_ISREG(stat_buf.st_mode)) {
1,403✔
65
               out.push_back(full_path);
1,403✔
66
            }
67
         }
2,208✔
68
      }
69
   }
368✔
70

71
   return out;
277✔
72
}
277✔
73

74
#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
75

76
std::vector<std::string> impl_win32(std::string_view dir_path) {
77
   std::vector<std::string> out;
78
   std::deque<std::string> dir_list;
79
   dir_list.push_back(std::string(dir_path));
80

81
   while(!dir_list.empty()) {
82
      const std::string cur_path = dir_list[0];
83
      dir_list.pop_front();
84

85
      WIN32_FIND_DATAA find_data;
86
      HANDLE dir = ::FindFirstFileA((cur_path + "/*").c_str(), &find_data);
87

88
      if(dir != INVALID_HANDLE_VALUE) {
89
         do {
90
            const std::string filename = find_data.cFileName;
91
            if(filename == "." || filename == "..")
92
               continue;
93
            const std::string full_path = cur_path + "/" + filename;
94

95
            if(find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
96
               dir_list.push_back(full_path);
97
            } else {
98
               out.push_back(full_path);
99
            }
100
         } while(::FindNextFileA(dir, &find_data));
101
      }
102

103
      ::FindClose(dir);
104
   }
105

106
   return out;
107
}
108
#endif
109

110
}  // namespace
111

112
bool has_filesystem_impl() {
11✔
113
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
114
   return true;
11✔
115
#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
116
   return true;
117
#else
118
   return false;
119
#endif
120
}
121

122
std::vector<std::string> get_files_recursive(std::string_view dir) {
277✔
123
   std::vector<std::string> files;
277✔
124

125
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
126
   files = impl_readdir(dir);
277✔
127
#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
128
   files = impl_win32(dir);
129
#else
130
   BOTAN_UNUSED(dir);
131
   throw No_Filesystem_Access();
132
#endif
133

134
   std::sort(files.begin(), files.end());
277✔
135

136
   return files;
277✔
137
}
×
138

139
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc