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

randombit / botan / 12993346889

27 Jan 2025 04:15PM UTC coverage: 91.248% (+0.002%) from 91.246%
12993346889

push

github

butteronarchbtw
add (s)afi encoding & decoding

94261 of 103302 relevant lines covered (91.25%)

11708235.22 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/internal/filesystem.h>
11
#include <algorithm>
12
#include <deque>
13
#include <memory>
14
#include <sstream>
15

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

27
namespace Botan {
28

29
namespace {
30

31
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
32

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

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

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

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

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

55
            struct stat stat_buf;
1,494✔
56

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

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

70
   return out;
277✔
71
}
277✔
72

73
#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
74

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

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

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

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

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

102
      ::FindClose(dir);
103
   }
104

105
   return out;
106
}
107
#endif
108

109
}  // namespace
110

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

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

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

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

135
   return files;
277✔
136
}
×
137

138
}  // 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