GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 43.4% 59 / 0 / 136
Functions: 50.0% 11 / 0 / 22
Branches: 28.3% 49 / 0 / 173

src/utilities/core/FilesystemHelpers.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "FilesystemHelpers.hpp"
7 #include "Filesystem.hpp"
8 #include "Path.hpp"
9 #include "Assert.hpp"
10
11 #include <fmt/format.h>
12
13 namespace openstudio {
14 namespace filesystem {
15 23 std::vector<char> read(openstudio::filesystem::ifstream& t_file) {
16
2/4
✓ Branch 1 taken 23 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
✗ Branch 6 not taken.
23 OS_ASSERT(t_file.good());
17
1/1
✓ Branch 1 taken 23 times.
23 const auto cur_pos = t_file.tellg();
18
1/1
✓ Branch 1 taken 23 times.
23 t_file.seekg(0, std::ios_base::end);
19
1/1
✓ Branch 1 taken 23 times.
23 const auto end_pos = t_file.tellg();
20 23 const auto file_len = end_pos - cur_pos;
21
1/1
✓ Branch 1 taken 23 times.
23 t_file.seekg(cur_pos);
22
1/1
✓ Branch 1 taken 23 times.
23 std::vector<char> buf(file_len);
23
1/1
✓ Branch 2 taken 23 times.
23 t_file.read(&buf.front(), file_len);
24 46 return buf;
25 ✗ }
26
27 ✗ std::vector<char> read(const openstudio::path& t_path) {
28 ✗ openstudio::filesystem::ifstream f(t_path, std::ios_base::binary);
29 ✗ return read(f);
30 ✗ }
31
32 23 std::string read_as_string(openstudio::filesystem::ifstream& t_file) {
33
1/1
✓ Branch 1 taken 23 times.
23 const auto vec = read(t_file);
34
1/1
✓ Branch 3 taken 23 times.
46 return std::string(vec.begin(), vec.end());
35 23 }
36
37 23 std::string read_as_string(const openstudio::path& t_path) {
38
1/1
✓ Branch 1 taken 23 times.
23 openstudio::filesystem::ifstream f(t_path, std::ios_base::binary);
39
1/1
✓ Branch 1 taken 23 times.
46 return read_as_string(f);
40 23 }
41
42 template <typename Itr, typename D>
43 339 Itr advance_itr_copy(Itr i, D distance) {
44 std::advance(i, distance);
45 339 return i;
46 }
47
48 template <typename Itr>
49 339 openstudio::path rebuild_path(Itr begin, const Itr& end) {
50 339 openstudio::path result;
51
3/3
✓ Branch 1 taken 686 times.
✓ Branch 3 taken 347 times.
✓ Branch 4 taken 339 times.
686 while (begin != end) {
52
1/1
✓ Branch 1 taken 347 times.
347 result /= *begin;
53
1/1
✓ Branch 1 taken 347 times.
347 ++begin;
54 }
55 339 return result;
56 ✗ }
57
58 template <typename Predicate>
59 ✗ std::vector<openstudio::path> directory_elements(const openstudio::path& t_path, Predicate predicate) {
60 ✗ std::vector<openstudio::path> files;
61
62 ✗ const auto num_path_elems = [&]() {
63 const auto distance = std::distance(t_path.begin(), t_path.end());
64 if (openstudio::toString(t_path.filename()) == ".") {
65 return distance - 1;
66 } else {
67 return distance;
68 }
69 ✗ }();
70
71 ✗ for (auto dir_itr = openstudio::filesystem::directory_iterator(t_path); dir_itr != openstudio::filesystem::directory_iterator(); ++dir_itr) {
72 // skip the path so these names are all relative
73 ✗ const auto p = dir_itr->path();
74 ✗ if (predicate(p)) {
75 ✗ files.push_back(rebuild_path(advance_itr_copy(p.begin(), num_path_elems), p.end()));
76 ✗ LOG_FREE(Debug, "FilesystemHelpers",
77 "directory_elements '" << openstudio::toString(p) << "' -> '" << openstudio::toString(files.back()) << "'");
78 }
79 }
80
81 ✗ return files;
82 ✗ }
83
84 ✗ std::vector<openstudio::path> directory_files(const openstudio::path& t_path) {
85 ✗ return directory_elements(t_path, [](const auto& f) { return openstudio::filesystem::is_regular_file(f); });
86 }
87
88 ✗ std::vector<openstudio::path> directory_directories(const openstudio::path& t_path) {
89 ✗ return directory_elements(t_path, [](const auto& f) { return openstudio::filesystem::is_directory(f); });
90 }
91
92 27 std::vector<openstudio::path> recursive_directory_files(const openstudio::path& t_path) {
93 27 std::vector<openstudio::path> files;
94
95 ✗ const auto num_path_elems = [&]() {
96
2/2
✓ Branch 1 taken 27 times.
✓ Branch 4 taken 27 times.
54 const auto distance = std::distance(t_path.begin(), t_path.end());
97
4/5
✓ Branch 1 taken 27 times.
✓ Branch 4 taken 27 times.
✓ Branch 7 taken 27 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 27 times.
54 if (openstudio::toString(t_path.filename()) == ".") {
98 ✗ return distance - 1;
99 } else {
100 27 return distance;
101 }
102
1/1
✓ Branch 1 taken 27 times.
27 }();
103
104
1/1
✓ Branch 1 taken 27 times.
27 for (auto dir_itr = openstudio::filesystem::recursive_directory_iterator(t_path);
105
4/4
✓ Branch 1 taken 373 times.
✓ Branch 5 taken 400 times.
✓ Branch 8 taken 373 times.
✓ Branch 9 taken 27 times.
773 dir_itr != openstudio::filesystem::recursive_directory_iterator(); ++dir_itr) {
106 // skip the path so these names are all relative
107
2/2
✓ Branch 1 taken 373 times.
✓ Branch 5 taken 373 times.
373 const auto p = dir_itr->path();
108
3/3
✓ Branch 1 taken 373 times.
✓ Branch 3 taken 339 times.
✓ Branch 4 taken 34 times.
373 if (openstudio::filesystem::is_regular_file(p)) {
109
5/5
✓ Branch 1 taken 339 times.
✓ Branch 4 taken 339 times.
✓ Branch 7 taken 339 times.
✓ Branch 10 taken 339 times.
✓ Branch 13 taken 339 times.
339 files.push_back(rebuild_path(advance_itr_copy(p.begin(), num_path_elems), p.end()));
110 }
111 400 }
112
113 27 return files;
114 ✗ }
115
116 22 bool copy_file_no_throw(const openstudio::path& t_src, const openstudio::path& t_dest) {
117 try {
118
1/1
✓ Branch 1 taken 22 times.
22 openstudio::filesystem::copy_file(t_src, t_dest);
119 22 return true;
120 ✗ } catch (const std::exception&) {
121 ✗ return false;
122 ✗ }
123 }
124
125 9 time_t to_time_t(time_t t) {
126 9 return t;
127 }
128
129 template <typename T>
130 time_t to_time_t(const T& t) {
131 // for future move from boost to std filesystem, which uses std::chrono
132 return T::clock::to_time_t(t);
133 }
134
135 9 time_t last_write_time_as_time_t(const openstudio::path& t_path) {
136 9 return to_time_t(openstudio::filesystem::last_write_time(t_path));
137 }
138
139 530 openstudio::path create_temporary_directory(const openstudio::path& basename) {
140
1/1
✓ Branch 1 taken 530 times.
530 const auto base_temp_dir = openstudio::filesystem::temp_directory_path();
141 // std::filesystem::unique_path doesn't exist, if moving to std::filesystem, use removeBraces(createUUID())
142
2/2
✓ Branch 1 taken 530 times.
✓ Branch 4 taken 530 times.
530 auto upath = boost::filesystem::unique_path();
143
1/1
✓ Branch 5 taken 530 times.
1590 const auto temp_dir = base_temp_dir / fmt::format("{}-{}-{}-", basename.string(), upath.string(), std::time(nullptr));
144
145 // unique_path is supposed to be unique already...
146 530 unsigned int count = 0;
147 530 constexpr unsigned int allowed_attempts = 1000;
148
1/2
✓ Branch 0 taken 530 times.
✗ Branch 1 not taken.
530 while (count < allowed_attempts) {
149 // concat number to path basename, without adding a new path element
150
1/1
✓ Branch 1 taken 530 times.
530 auto full_pathname = temp_dir;
151
1/1
✓ Branch 2 taken 530 times.
530 full_pathname += std::to_string(count++);
152 try {
153
2/3
✓ Branch 1 taken 530 times.
✓ Branch 3 taken 530 times.
✗ Branch 4 not taken.
530 if (openstudio::filesystem::create_directories(full_pathname)) {
154 // if the path was created, then we know it was created for us
155 530 return full_pathname;
156 }
157 ✗ } catch (...) {
158 // swallow exceptions, we don't care, we only care when it succeeds
159 ✗ }
160 530 }
161
162 // after too many attempts we never made a path
163 ✗ return {};
164 530 }
165
166 ✗ openstudio::path home_path() {
167 ✗ const auto build_path = [](const auto&... elem) {
168 auto path_elem = [](auto& missing, const auto& env_var) {
169 if (const auto var = std::getenv(env_var); var != nullptr) {
170 auto path = toPath(var);
171 if (path.empty()) {
172 missing = true;
173 }
174 return path;
175 } else {
176 missing = true;
177 return openstudio::path{};
178 }
179 };
180
181 ✗ bool missing_element = false;
182
183 // build up the path from the environment variable names passed in.
184 ✗ auto path = (path_elem(missing_element, elem) / ...).lexically_normal();
185
186 ✗ if (!missing_element && openstudio::filesystem::is_directory(path)) {
187 ✗ return path;
188 } else {
189 ✗ return openstudio::path{};
190 }
191 ✗ };
192
193 // TODO: instead of trace, just comment out?
194 ✗ if (auto home = build_path("USERPROFILE"); !home.empty()) {
195 ✗ LOG_FREE(Trace, "FilesystemHelpers", "home_path USERPROFILE: " << toString(home));
196 ✗ return home;
197 ✗ }
198
199 ✗ if (auto home = build_path("HOMEDRIVE", "HOMEPATH"); !home.empty()) {
200 ✗ LOG_FREE(Trace, "FilesystemHelpers", "home_path HOMEDRIVE/HOMEPATH: " << toString(home));
201 ✗ return home;
202 ✗ }
203
204 ✗ if (auto home = build_path("HOME"); !home.empty()) {
205 ✗ LOG_FREE(Trace, "FilesystemHelpers", "home_path HOME: " << toString(home));
206 ✗ return home;
207 ✗ }
208
209 ✗ LOG_FREE(Warn, "FilesystemHelpers", "home_path No Home Found");
210 ✗ return toPath("/");
211 }
212 } // namespace filesystem
213 } // namespace openstudio
214