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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 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) {
264✔
34
   std::vector<std::string> out;
264✔
35
   std::deque<std::string> dir_list;
264✔
36
   dir_list.push_back(std::string(dir_path));
528✔
37

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

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

44
      if(dir) {
355✔
45
         while(struct dirent* dirent = ::readdir(dir.get())) {
2,505✔
46
            const std::string filename = dirent->d_name;
2,155✔
47
            if(filename == "." || filename == "..")
2,155✔
48
               continue;
700✔
49

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

54
            struct stat stat_buf;
1,455✔
55

56
            if(::stat(full_path.c_str(), &stat_buf) == -1)
1,455✔
57
               continue;
×
58

59
            if(S_ISDIR(stat_buf.st_mode))
1,455✔
60
               dir_list.push_back(full_path);
91✔
61
            else if(S_ISREG(stat_buf.st_mode))
1,364✔
62
               out.push_back(full_path);
1,364✔
63
         }
2,504✔
64
      }
65
   }
709✔
66

67
   return out;
264✔
68
}
264✔
69

70
#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
71

72
std::vector<std::string> impl_win32(std::string_view dir_path) {
73
   std::vector<std::string> out;
74
   std::deque<std::string> dir_list;
75
   dir_list.push_back(std::string(dir_path));
76

77
   while(!dir_list.empty()) {
78
      const std::string cur_path = dir_list[0];
79
      dir_list.pop_front();
80

81
      WIN32_FIND_DATAA find_data;
82
      HANDLE dir = ::FindFirstFileA((cur_path + "/*").c_str(), &find_data);
83

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

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

99
      ::FindClose(dir);
100
   }
101

102
   return out;
103
}
104
#endif
105

106
}
107

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

118
std::vector<std::string> get_files_recursive(std::string_view dir) {
264✔
119
   std::vector<std::string> files;
264✔
120

121
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
122
   files = impl_readdir(dir);
264✔
123
#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
124
   files = impl_win32(dir);
125
#else
126
   BOTAN_UNUSED(dir);
127
   throw No_Filesystem_Access();
128
#endif
129

130
   std::sort(files.begin(), files.end());
264✔
131

132
   return files;
264✔
133
}
×
134

135
}
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

© 2025 Coveralls, Inc