src/utilities/core/PathHelpers.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 "PathHelpers.hpp" | ||
| 7 | #include "Assert.hpp" | ||
| 8 | #include "FilesystemHelpers.hpp" | ||
| 9 | |||
| 10 | #include <boost/regex.hpp> | ||
| 11 | |||
| 12 | #if (defined(_WIN32) || defined(_WIN64)) | ||
| 13 | # include <Windows.h> | ||
| 14 | #endif | ||
| 15 | |||
| 16 | namespace openstudio { | ||
| 17 | |||
| 18 | ✗ | std::string executableFileExtension() { | |
| 19 | #ifdef _WIN32 | ||
| 20 | return ".exe"; | ||
| 21 | #else | ||
| 22 | ✗ | return ""; | |
| 23 | #endif | ||
| 24 | } | ||
| 25 | |||
| 26 | 788 | std::string modelFileExtension() { | |
| 27 |
1/1✓ Branch 1 taken 788 times.
|
1576 | return {"osm"}; |
| 28 | } | ||
| 29 | |||
| 30 | 814 | std::string componentFileExtension() { | |
| 31 |
1/1✓ Branch 1 taken 814 times.
|
1628 | return {"osc"}; |
| 32 | } | ||
| 33 | |||
| 34 | ✗ | std::string tableFileExtension() { | |
| 35 | ✗ | return {"ost"}; | |
| 36 | } | ||
| 37 | |||
| 38 | ✗ | std::string documentFileExtension() { | |
| 39 | ✗ | return {"osd"}; | |
| 40 | } | ||
| 41 | |||
| 42 | ✗ | std::string rulesetFileExtension() { | |
| 43 | ✗ | return {"osr"}; | |
| 44 | } | ||
| 45 | |||
| 46 | 208 | bool pathBeginsWith(const openstudio::path& t_shorter, const openstudio::path& t_longer) { | |
| 47 |
1/1✓ Branch 1 taken 208 times.
|
208 | openstudio::path::const_iterator begin1 = t_shorter.begin(); |
| 48 |
1/1✓ Branch 1 taken 208 times.
|
208 | openstudio::path::const_iterator end1 = t_shorter.end(); |
| 49 | |||
| 50 |
1/1✓ Branch 1 taken 208 times.
|
208 | openstudio::path::const_iterator begin2 = t_longer.begin(); |
| 51 |
1/1✓ Branch 1 taken 208 times.
|
208 | openstudio::path::const_iterator end2 = t_longer.end(); |
| 52 | |||
| 53 |
6/6✓ Branch 1 taken 208 times.
✓ Branch 4 taken 208 times.
✓ Branch 7 taken 208 times.
✓ Branch 10 taken 208 times.
✓ Branch 16 taken 2 times.
✓ Branch 17 taken 206 times.
|
624 | if (std::distance(begin1, end1) > std::distance(begin2, end2)) { |
| 54 | 2 | return false; // the run dir has fewer elements than the build dir - cannot be running from builddir | |
| 55 | } | ||
| 56 | |||
| 57 | // if the rundir begins with the builddir, we know it's running from the builddir | ||
| 58 |
4/4✓ Branch 1 taken 206 times.
✓ Branch 4 taken 206 times.
✓ Branch 7 taken 206 times.
✓ Branch 10 taken 206 times.
|
206 | return std::equal(begin1, end1, begin2); |
| 59 | 208 | } | |
| 60 | |||
| 61 | 590 | path completePathToFile(const path& p, const path& base, const std::string& ext, bool warnOnMismatch) { | |
| 62 |
1/1✓ Branch 1 taken 590 times.
|
590 | path result(p); |
| 63 | |||
| 64 | // handle file extension | ||
| 65 |
2/2✓ Branch 1 taken 387 times.
✓ Branch 2 taken 203 times.
|
590 | if (!ext.empty()) { |
| 66 |
1/1✓ Branch 1 taken 387 times.
|
387 | result = setFileExtension(p, ext, false, warnOnMismatch); |
| 67 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 387 times.
|
387 | if (result.empty()) { |
| 68 | ✗ | result = p; | |
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | // complete path | ||
| 73 |
3/3✓ Branch 1 taken 590 times.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 568 times.
|
590 | if (!result.is_absolute()) { |
| 74 | try { | ||
| 75 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 2 taken 21 times.
|
22 | if (!base.empty()) { |
| 76 |
1/1✓ Branch 1 taken 1 time.
|
1 | result = openstudio::filesystem::absolute(result, base); |
| 77 | } else { | ||
| 78 |
2/2✓ Branch 1 taken 21 times.
✓ Branch 4 taken 21 times.
|
21 | result = openstudio::filesystem::absolute(result); |
| 79 | } | ||
| 80 | ✗ | } catch (...) { | |
| 81 | ✗ | LOG_FREE(Info, "openstudio.completePathToFile", "Unable to compete path '" << toString(p) << "'. Returning an empty path."); | |
| 82 | ✗ | return {}; | |
| 83 | ✗ | } | |
| 84 | } | ||
| 85 | |||
| 86 | // check that result is a file | ||
| 87 |
3/3✓ Branch 1 taken 590 times.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 575 times.
|
590 | if (!openstudio::filesystem::is_regular_file(result)) { |
| 88 |
8/8✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 15 times.
✓ Branch 10 taken 15 times.
✓ Branch 13 taken 15 times.
✓ Branch 17 taken 15 times.
✓ Branch 20 taken 15 times.
✓ Branch 23 taken 15 times.
|
45 | LOG_FREE(Info, "openstudio.completePathToFile", |
| 89 | "Path '" << toString(p) << "' could not be resolved to an existing file. Returning an empty path."); | ||
| 90 | 15 | return {}; | |
| 91 | } | ||
| 92 | |||
| 93 | 575 | return result; | |
| 94 | 590 | } | |
| 95 | |||
| 96 |
1/1✓ Branch 1 taken 1842 times.
|
1842 | std::string getFileExtension(const path& p) { |
| 97 |
1/1✓ Branch 2 taken 1842 times.
|
1842 | std::string pext = p.extension().string(); |
| 98 |
2/2✓ Branch 1 taken 1838 times.
✓ Branch 2 taken 4 times.
|
1842 | if (!pext.empty()) { |
| 99 | // remove '.' | ||
| 100 |
1/1✓ Branch 4 taken 1838 times.
|
1838 | pext = std::string(++pext.begin(), pext.end()); |
| 101 | } | ||
| 102 | 1842 | return pext; | |
| 103 | ✗ | } | |
| 104 | |||
| 105 | 885 | path setFileExtension(const path& p, const std::string& ext, bool replaceOnMismatch, bool warnOnMismatch) { | |
| 106 |
1/1✓ Branch 1 taken 885 times.
|
885 | path result(p); |
| 107 |
1/1✓ Branch 1 taken 885 times.
|
885 | path wext = toPath(ext); |
| 108 |
1/1✓ Branch 1 taken 885 times.
|
885 | std::string pext = getFileExtension(p); |
| 109 |
2/2✓ Branch 1 taken 881 times.
✓ Branch 2 taken 4 times.
|
885 | if (!pext.empty()) { |
| 110 |
2/2✓ Branch 2 taken 40 times.
✓ Branch 3 taken 841 times.
|
881 | if (pext != wext.string()) { |
| 111 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 4 times.
|
40 | if (warnOnMismatch) { |
| 112 |
11/11✓ Branch 1 taken 36 times.
✓ Branch 4 taken 36 times.
✓ Branch 7 taken 36 times.
✓ Branch 10 taken 36 times.
✓ Branch 13 taken 36 times.
✓ Branch 16 taken 36 times.
✓ Branch 19 taken 36 times.
✓ Branch 22 taken 36 times.
✓ Branch 27 taken 36 times.
✓ Branch 30 taken 36 times.
✓ Branch 33 taken 36 times.
|
108 | LOG_FREE(Warn, "openstudio.setFileExtension", |
| 113 | "Path p, '" << toString(p) << "', has an unexpected file extension. Was expecting '" << toString(wext) << "'."); | ||
| 114 | } | ||
| 115 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 35 times.
|
40 | if (!replaceOnMismatch) { |
| 116 | 5 | return result; | |
| 117 | } | ||
| 118 | } | ||
| 119 | } // if | ||
| 120 | |||
| 121 |
1/1✓ Branch 2 taken 880 times.
|
1760 | result.replace_extension(wext.string()); |
| 122 | 880 | return result; | |
| 123 | 885 | } | |
| 124 | |||
| 125 | 627 | bool makeParentFolder(const path& p, const path& base, bool recursive) { | |
| 126 | |||
| 127 | // get path to last directory | ||
| 128 |
1/1✓ Branch 1 taken 627 times.
|
627 | path wp(p); |
| 129 |
2/2✓ Branch 1 taken 626 times.
✓ Branch 2 taken 1 time.
|
627 | if (base.empty()) { |
| 130 |
2/2✓ Branch 1 taken 626 times.
✓ Branch 4 taken 626 times.
|
626 | wp = openstudio::filesystem::absolute(wp); |
| 131 | } else { | ||
| 132 |
1/1✓ Branch 1 taken 1 time.
|
1 | wp = openstudio::filesystem::absolute(wp, base); |
| 133 | } | ||
| 134 |
1/2✓ Branch 0 taken 627 times.
✗ Branch 1 not taken.
|
627 | if (wp.has_filename()) { |
| 135 |
1/1✓ Branch 1 taken 627 times.
|
627 | wp = wp.parent_path(); |
| 136 | } | ||
| 137 | |||
| 138 | // make one or more directories as needed and as directed by recursive | ||
| 139 | 627 | bool result = true; | |
| 140 |
3/3✓ Branch 1 taken 627 times.
✓ Branch 3 taken 599 times.
✓ Branch 4 taken 28 times.
|
627 | if (openstudio::filesystem::is_directory(wp)) { |
| 141 | 599 | return result; | |
| 142 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 17 times.
|
28 | } else if (recursive) { |
| 143 | try { | ||
| 144 |
1/1✓ Branch 1 taken 11 times.
|
11 | result = create_directories(wp); |
| 145 | ✗ | } catch (...) { | |
| 146 | ✗ | result = false; | |
| 147 | ✗ | } | |
| 148 | } else { | ||
| 149 | try { | ||
| 150 |
1/1✓ Branch 1 taken 16 times.
|
17 | result = create_directory(wp); |
| 151 | 1 | } catch (...) { | |
| 152 | 1 | result = false; | |
| 153 | 1 | } | |
| 154 | } | ||
| 155 | |||
| 156 | 28 | return result; | |
| 157 | 627 | } | |
| 158 | |||
| 159 | 65 | path relativePath(const path& p, const path& base) { | |
| 160 | // Note: could just use openstudio::filesystem::relative(p, base); | ||
| 161 | |||
| 162 |
1/1✓ Branch 1 taken 65 times.
|
65 | path wp = p; |
| 163 |
1/1✓ Branch 1 taken 65 times.
|
65 | path wBase = base; |
| 164 | |||
| 165 |
1/1✓ Branch 1 taken 65 times.
|
65 | path::const_iterator wpIt = wp.begin(); |
| 166 |
1/1✓ Branch 1 taken 65 times.
|
65 | path::const_iterator wpEnd = wp.end(); |
| 167 |
1/1✓ Branch 1 taken 65 times.
|
65 | path::const_iterator wBaseIt = wBase.begin(); |
| 168 |
1/1✓ Branch 1 taken 65 times.
|
65 | path::const_iterator wBaseEnd = wBase.end(); |
| 169 |
12/13✓ Branch 1 taken 201 times.
✓ Branch 3 taken 201 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 201 times.
✓ Branch 8 taken 137 times.
✓ Branch 9 taken 64 times.
✓ Branch 11 taken 137 times.
✓ Branch 14 taken 137 times.
✓ Branch 17 taken 137 times.
✓ Branch 19 taken 136 times.
✓ Branch 20 taken 1 time.
✓ Branch 21 taken 136 times.
✓ Branch 22 taken 65 times.
|
338 | while ((wpIt != wpEnd) && (wBaseIt != wBaseEnd) && ((*wpIt) == (*wBaseIt))) { |
| 170 |
1/1✓ Branch 1 taken 136 times.
|
136 | ++wpIt; |
| 171 |
1/1✓ Branch 1 taken 136 times.
|
136 | ++wBaseIt; |
| 172 | } | ||
| 173 | // p is not an extension of base, try to complete, then return p | ||
| 174 |
10/14✓ Branch 1 taken 65 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 64 times.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 12 taken 1 time.
✗ Branch 14 not taken.
✓ Branch 15 taken 1 time.
✓ Branch 16 taken 1 time.
✓ Branch 17 taken 64 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 65 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
|
65 | if (!((wBaseIt == wBaseEnd) || (toString(*wBaseIt) == "."))) { |
| 175 | ✗ | path completeP = openstudio::filesystem::absolute(p); | |
| 176 | ✗ | path completeBase = openstudio::filesystem::absolute(base); | |
| 177 | ✗ | if ((completeP != wp) || (completeBase != wBase)) { | |
| 178 | ✗ | LOG_FREE(Debug, "openstudio.utilities.core", | |
| 179 | "Path '" << toString(p) << "' does not extend base '" << toString(base) << "'. Try again after completing both paths."); | ||
| 180 | ✗ | return relativePath(completeP, completeBase); | |
| 181 | } else { | ||
| 182 | ✗ | LOG_FREE(Debug, "openstudio.utilities.core", "Path '" << toString(p) << "' does not extend base '" << toString(base) << "'."); | |
| 183 | ✗ | return {}; | |
| 184 | } | ||
| 185 | ✗ | } | |
| 186 | |||
| 187 | // p is an extension of base, keep whatever remains of p | ||
| 188 | 65 | path result; | |
| 189 |
3/3✓ Branch 1 taken 204 times.
✓ Branch 3 taken 139 times.
✓ Branch 4 taken 65 times.
|
204 | while (wpIt != wpEnd) { |
| 190 |
2/2✓ Branch 1 taken 139 times.
✓ Branch 4 taken 139 times.
|
278 | result = result / (*wpIt); |
| 191 |
1/1✓ Branch 1 taken 139 times.
|
139 | ++wpIt; |
| 192 | } | ||
| 193 | 65 | return result; | |
| 194 | 65 | } | |
| 195 | |||
| 196 | 16 | const char pathDelimiter() { | |
| 197 | #if defined _WIN32 | ||
| 198 | const char delimiter = ';'; | ||
| 199 | #else | ||
| 200 | 16 | const char delimiter = ':'; | |
| 201 | #endif | ||
| 202 | 16 | return delimiter; | |
| 203 | } | ||
| 204 | |||
| 205 | 2 | path findInSystemPath(const path& p) { | |
| 206 | |||
| 207 | 2 | path result; | |
| 208 | // Ensure that this is just a name and not a path | ||
| 209 |
2/3✓ Branch 1 taken 2 times.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
|
2 | if (p.parent_path().empty()) { |
| 210 |
2/2✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
|
2 | std::istringstream pathstream(getenv("PATH")); |
| 211 |
7/7✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 16 taken 2 times.
✓ Branch 19 taken 2 times.
|
6 | LOG_FREE(Debug, "PathHelpers", "findInSystemPath, searching for '" << p << "' in PATH'"); |
| 212 | |||
| 213 | 2 | std::string pathstring; | |
| 214 |
5/5✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 15 times.
✓ Branch 9 taken 14 times.
✓ Branch 10 taken 1 time.
|
15 | while (std::getline(pathstream, pathstring, pathDelimiter())) { |
| 215 |
9/9✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
✓ Branch 7 taken 14 times.
✓ Branch 10 taken 14 times.
✓ Branch 13 taken 14 times.
✓ Branch 16 taken 14 times.
✓ Branch 19 taken 14 times.
✓ Branch 22 taken 14 times.
✓ Branch 25 taken 14 times.
|
42 | LOG_FREE(Trace, "PathHelpers", "findInSystemPath, searching for '" << p << "' in '" << pathstring << "'"); |
| 216 | |||
| 217 |
1/1✓ Branch 1 taken 14 times.
|
28 | auto maybepath = toPath(pathstring) / p; |
| 218 |
7/8✓ Branch 1 taken 14 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 13 times.
✓ Branch 6 taken 1 time.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✓ Branch 11 taken 13 times.
|
14 | if (openstudio::filesystem::exists(maybepath) && !openstudio::filesystem::is_directory(maybepath)) { |
| 219 |
8/8✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 22 taken 1 time.
|
3 | LOG_FREE(Debug, "PathHelpers", "findInSystemPath, found '" << p << "' in PATH: '" << pathstring); |
| 220 |
1/1✓ Branch 1 taken 1 time.
|
1 | result = maybepath; |
| 221 | 1 | break; | |
| 222 | } | ||
| 223 | 14 | } | |
| 224 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
|
2 | if (result.empty()) { |
| 225 |
5/5✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
|
3 | LOG_FREE(Debug, "PathHelpers", "findInSystemPath, p wasn't found in PATH, leaving as is"); |
| 226 |
1/1✓ Branch 1 taken 1 time.
|
1 | result = p; |
| 227 | } | ||
| 228 | |||
| 229 | 2 | } else { | |
| 230 | ✗ | LOG_FREE(Debug, "PathHelpers", "findInSystemPath, p isn't just a name, leaving as is"); | |
| 231 | ✗ | result = p; | |
| 232 | } | ||
| 233 | |||
| 234 | 2 | return result; | |
| 235 | ✗ | } | |
| 236 | |||
| 237 | 930 | path completeAndNormalize(const path& p) { | |
| 238 | |||
| 239 |
1/1✓ Branch 1 taken 930 times.
|
930 | path temp = openstudio::filesystem::system_complete(p); |
| 240 | |||
| 241 |
6/6✓ Branch 1 taken 930 times.
✓ Branch 4 taken 930 times.
✓ Branch 7 taken 930 times.
✓ Branch 10 taken 930 times.
✓ Branch 13 taken 930 times.
✓ Branch 16 taken 930 times.
|
2790 | LOG_FREE(Trace, "PathHelpers", "completeAndNormalize: looking for p = " << p); |
| 242 |
6/6✓ Branch 1 taken 930 times.
✓ Branch 4 taken 930 times.
✓ Branch 7 taken 930 times.
✓ Branch 10 taken 930 times.
✓ Branch 13 taken 930 times.
✓ Branch 16 taken 930 times.
|
2790 | LOG_FREE(Trace, "PathHelpers", "completeAndNormalize: temp = " << temp); |
| 243 | |||
| 244 | // TODO: is there a point continuing if temp doesn't exist? | ||
| 245 |
3/3✓ Branch 1 taken 930 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 929 times.
|
930 | if (!openstudio::filesystem::exists(temp)) { // || openstudio::filesystem::is_directory( temp ) ) { |
| 246 |
5/5✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
|
3 | LOG_FREE(Trace, "PathHelpers", "completeAndNormalize: temp doesn't exists"); |
| 247 | } | ||
| 248 | |||
| 249 |
3/3✓ Branch 1 taken 1277 times.
✓ Branch 3 taken 347 times.
✓ Branch 4 taken 930 times.
|
1277 | while (openstudio::filesystem::is_symlink(temp)) { |
| 250 |
1/1✓ Branch 1 taken 347 times.
|
347 | auto linkpath = openstudio::filesystem::read_symlink(temp); |
| 251 |
6/6✓ Branch 1 taken 347 times.
✓ Branch 4 taken 347 times.
✓ Branch 7 taken 347 times.
✓ Branch 10 taken 347 times.
✓ Branch 13 taken 347 times.
✓ Branch 16 taken 347 times.
|
1041 | LOG_FREE(Trace, "PathHelpers", "completeAndNormalize: It's a symlink, linkpath = " << linkpath); |
| 252 | |||
| 253 |
3/3✓ Branch 1 taken 347 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 345 times.
|
347 | if (linkpath.is_absolute()) { |
| 254 |
1/1✓ Branch 1 taken 2 times.
|
2 | temp = linkpath; |
| 255 |
5/5✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
|
6 | LOG_FREE(Trace, "PathHelpers", "completeAndNormalize: temp is an absolute symlink (= linkpath)"); |
| 256 | |||
| 257 | } else { | ||
| 258 | // Note JM 2019-04-24: temp will end up absolute but not canonical yet | ||
| 259 | // eg: | ||
| 260 | // temp="/home/a_folder/a_symlink" | ||
| 261 | // linkpath="../another_folder/a_file" | ||
| 262 |
1/1✓ Branch 1 taken 345 times.
|
690 | temp = temp.parent_path() / linkpath; |
| 263 | // eg: temp ="/home/a_folder/../another_folder/a_file" | ||
| 264 | |||
| 265 |
6/6✓ Branch 1 taken 345 times.
✓ Branch 4 taken 345 times.
✓ Branch 7 taken 345 times.
✓ Branch 10 taken 345 times.
✓ Branch 13 taken 345 times.
✓ Branch 16 taken 345 times.
|
1035 | LOG_FREE(Trace, "PathHelpers", "completeAndNormalize: temp is a relative symlink, pointing to = " << temp); |
| 266 | } | ||
| 267 | 347 | } | |
| 268 | // TODO: can this actually happen? | ||
| 269 |
2/6✗ Branch 1 not taken.
✓ Branch 2 taken 930 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 930 times.
|
930 | if (temp.empty() && !p.empty()) { |
| 270 | ✗ | LOG_FREE(Trace, "PathHelpers", "completeAndNormalize: temp is empty, reseting to p"); | |
| 271 | ✗ | temp = p; | |
| 272 | } | ||
| 273 | 930 | path result; | |
| 274 | |||
| 275 | // TODO: In develop3, which has boost 1.68, we can replace it with boost::filesystem::weakly_canonical | ||
| 276 | // Note JM 2019-04-24: temp right now is absolute, but it isn't necessarilly canonical. | ||
| 277 | // This block resolves a canonical path, even if it doesn't exist (yet?) on disk. | ||
| 278 |
6/6✓ Branch 1 taken 930 times.
✓ Branch 4 taken 8404 times.
✓ Branch 7 taken 9334 times.
✓ Branch 10 taken 9334 times.
✓ Branch 13 taken 8404 times.
✓ Branch 14 taken 930 times.
|
9334 | for (openstudio::path::iterator it = temp.begin(); it != temp.end(); ++it) { |
| 279 |
5/5✓ Branch 1 taken 8404 times.
✓ Branch 4 taken 8404 times.
✓ Branch 7 taken 8404 times.
✓ Branch 10 taken 7 times.
✓ Branch 11 taken 8397 times.
|
16808 | if (*it == toPath("..")) { |
| 280 |
9/18✓ Branch 1 taken 7 times.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 7 times.
✓ Branch 9 taken 7 times.
✓ Branch 12 taken 7 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 7 times.
✓ Branch 16 taken 7 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 7 times.
✗ Branch 20 not taken.
✗ Branch 22 not taken.
✓ Branch 23 taken 7 times.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
|
21 | if (openstudio::filesystem::is_symlink(result) || (result.filename() == toPath(".."))) { |
| 281 | ✗ | result /= *it; | |
| 282 | } else { | ||
| 283 |
1/1✓ Branch 1 taken 7 times.
|
7 | result = result.parent_path(); |
| 284 | } | ||
| 285 |
4/5✓ Branch 1 taken 8397 times.
✓ Branch 4 taken 8397 times.
✓ Branch 7 taken 8397 times.
✓ Branch 10 taken 8397 times.
✗ Branch 11 not taken.
|
16794 | } else if (*it != toPath(".")) { |
| 286 |
1/1✓ Branch 1 taken 8397 times.
|
8397 | result /= *it; |
| 287 | } | ||
| 288 | 930 | } | |
| 289 | |||
| 290 |
6/6✓ Branch 1 taken 930 times.
✓ Branch 4 taken 930 times.
✓ Branch 7 taken 930 times.
✓ Branch 10 taken 930 times.
✓ Branch 13 taken 930 times.
✓ Branch 16 taken 930 times.
|
2790 | LOG_FREE(Debug, "PathHelpers", "completeAndNormalize: result = " << result); |
| 291 | |||
| 292 | 1860 | return result; | |
| 293 | 930 | } | |
| 294 | |||
| 295 | ✗ | path relocatePath(const path& originalPath, const path& originalBase, const path& newBase) { | |
| 296 | ✗ | path result; | |
| 297 | ✗ | path temp = relativePath(originalPath, originalBase); | |
| 298 | ✗ | LOG_FREE(Debug, "openstudio.utilities.core", | |
| 299 | "Original path '" << toString(originalPath) << "', relative to '" << toString(originalBase) << "' is '" << toString(temp) << "'."); | ||
| 300 | ✗ | if (!temp.empty()) { | |
| 301 | ✗ | result = newBase / temp; | |
| 302 | ✗ | LOG_FREE(Debug, "openstudio.utilities.core", "Relocating path to '" << toString(result) << "'."); | |
| 303 | } | ||
| 304 | ✗ | return result; | |
| 305 | ✗ | } | |
| 306 | |||
| 307 |
1/1✓ Branch 1 taken 11 times.
|
11 | path getCompanionFolder(const path& osmPath) { |
| 308 | // Note: JM 2018-09-05 | ||
| 309 | // We could include a variety of checks (verify that we passed a file, that the file exists, that extension is osm) | ||
| 310 | // if( boost::filesystem::is_regular_file(osmPath) && (getFileExtension(osmPath) == "osm") && boost::filesystem::exists(osmPath) ) | ||
| 311 | // Not doing it for speed, we assume we do only use this with the path to an OSM, so stem() will do the right thing, that is split on the last "." | ||
| 312 |
1/1✓ Branch 1 taken 11 times.
|
22 | return osmPath.parent_path() / osmPath.stem(); |
| 313 | } | ||
| 314 | |||
| 315 | 1 | path getLastLevelDirectoryName(const path& directory) { | |
| 316 | // Note: JM 2018-09-05 | ||
| 317 | // We could do a variety of checks here to ensure that we did pass a directory and not a file, not doing it for speed | ||
| 318 | // (boost::filesystem::is_directory and boost::filesystem::exists) | ||
| 319 | |||
| 320 | // We are using filename() (badly named here) and not stem() because we passed a directory, and if there is a "." in the directory name | ||
| 321 | // stem() is going to think the part after the last '.' is the extension. | ||
| 322 | // filename() just strips out the parent_path which is exactly what we need | ||
| 323 | 1 | return directory.filename(); | |
| 324 | } | ||
| 325 | |||
| 326 | 8 | std::ostream& printPathInformation(std::ostream& os, const path& p) { | |
| 327 |
3/3✓ Branch 3 taken 8 times.
✓ Branch 6 taken 8 times.
✓ Branch 9 taken 8 times.
|
8 | os << "p.string() = " << toString(p.string()) << '\n'; |
| 328 |
3/3✓ Branch 3 taken 8 times.
✓ Branch 6 taken 8 times.
✓ Branch 9 taken 8 times.
|
8 | os << "p.native() = " << toString(p.native()) << '\n'; |
| 329 | |||
| 330 |
4/4✓ Branch 2 taken 8 times.
✓ Branch 5 taken 8 times.
✓ Branch 8 taken 8 times.
✓ Branch 11 taken 8 times.
|
8 | os << "p.root_name() = " << toString(p.root_name()) << '\n'; |
| 331 |
4/4✓ Branch 2 taken 8 times.
✓ Branch 5 taken 8 times.
✓ Branch 8 taken 8 times.
✓ Branch 11 taken 8 times.
|
8 | os << "p.root_directory() = " << toString(p.root_directory()) << '\n'; |
| 332 | |||
| 333 |
4/4✓ Branch 2 taken 8 times.
✓ Branch 5 taken 8 times.
✓ Branch 8 taken 8 times.
✓ Branch 11 taken 8 times.
|
8 | os << "p.parent_path() = " << toString(p.parent_path()) << '\n'; |
| 334 |
4/4✓ Branch 2 taken 8 times.
✓ Branch 5 taken 8 times.
✓ Branch 8 taken 8 times.
✓ Branch 11 taken 8 times.
|
16 | os << "p.filename() = " << toString(p.filename()) << '\n'; |
| 335 | |||
| 336 |
4/4✓ Branch 2 taken 8 times.
✓ Branch 5 taken 8 times.
✓ Branch 8 taken 8 times.
✓ Branch 11 taken 8 times.
|
16 | os << "p.stem() = " << toString(p.stem()) << '\n'; |
| 337 |
4/4✓ Branch 2 taken 8 times.
✓ Branch 5 taken 8 times.
✓ Branch 8 taken 8 times.
✓ Branch 11 taken 8 times.
|
16 | os << "p.extension() = " << toString(p.extension()) << '\n'; |
| 338 | |||
| 339 | 8 | os << "p.empty() = " << std::boolalpha << p.empty() << '\n'; | |
| 340 | 8 | os << "p.is_absolute() = " << p.is_absolute() << '\n'; | |
| 341 | |||
| 342 |
5/5✓ Branch 2 taken 8 times.
✓ Branch 5 taken 8 times.
✓ Branch 8 taken 8 times.
✓ Branch 11 taken 8 times.
✓ Branch 14 taken 8 times.
|
8 | os << "openstudio::filesystem::absolute(p) = " << toString(openstudio::filesystem::absolute(p)) << '\n'; |
| 343 |
4/4✓ Branch 2 taken 8 times.
✓ Branch 5 taken 8 times.
✓ Branch 8 taken 8 times.
✓ Branch 11 taken 8 times.
|
8 | os << "openstudio::filesystem::system_complete(p) = " << toString(openstudio::filesystem::system_complete(p)) << '\n'; |
| 344 | |||
| 345 | 8 | return os; | |
| 346 | } | ||
| 347 | |||
| 348 | 16 | bool removeDirectory(const path& dirName) { | |
| 349 | try { | ||
| 350 |
1/1✓ Branch 1 taken 16 times.
|
16 | openstudio::filesystem::remove_all(dirName); |
| 351 | 16 | return true; | |
| 352 | ✗ | } catch (const std::exception&) { | |
| 353 | ✗ | return false; | |
| 354 | ✗ | } | |
| 355 | } | ||
| 356 | |||
| 357 | 6 | bool copyDirectory(const path& source, const path& destination) { | |
| 358 | // note : we are not using openstudio::filesystem::copy to copy recursively | ||
| 359 | // because that copies the entire directory into the destination, not just the | ||
| 360 | // contents of the directory | ||
| 361 | |||
| 362 | // Start by creating the destination directory if it doesn't exist | ||
| 363 | 6 | openstudio::filesystem::create_directories(destination); | |
| 364 | |||
| 365 |
7/7✓ Branch 1 taken 6 times.
✓ Branch 5 taken 6 times.
✓ Branch 9 taken 10 times.
✓ Branch 12 taken 10 times.
✓ Branch 15 taken 16 times.
✓ Branch 17 taken 10 times.
✓ Branch 18 taken 6 times.
|
16 | for (const auto& dirEnt : openstudio::filesystem::directory_iterator{source}) { |
| 366 | |||
| 367 | 10 | const auto& srcFolderPath = dirEnt.path(); | |
| 368 |
1/1✓ Branch 1 taken 10 times.
|
10 | const auto& relativeFolderPath = openstudio::filesystem::relative(srcFolderPath, source); |
| 369 | |||
| 370 | try { | ||
| 371 | // copy with recursive will deal with creating subfolders as needed | ||
| 372 |
2/2✓ Branch 2 taken 10 times.
✓ Branch 5 taken 10 times.
|
20 | openstudio::filesystem::copy(srcFolderPath, destination / relativeFolderPath, |
| 373 | openstudio::filesystem::copy_options::recursive | openstudio::filesystem::copy_options::overwrite_existing); | ||
| 374 | ✗ | } catch (const std::exception&) { | |
| 375 | ✗ | return false; | |
| 376 | ✗ | } | |
| 377 | 16 | } | |
| 378 | |||
| 379 | 6 | return true; | |
| 380 | } | ||
| 381 | |||
| 382 | ✗ | bool isEmptyDirectory(const path& dirName) { | |
| 383 | ✗ | if (!openstudio::filesystem::exists(dirName)) { | |
| 384 | ✗ | return false; | |
| 385 | } | ||
| 386 | |||
| 387 | ✗ | if (!openstudio::filesystem::is_directory(dirName)) { | |
| 388 | ✗ | return false; | |
| 389 | } | ||
| 390 | |||
| 391 | ✗ | return openstudio::filesystem::is_empty(dirName); | |
| 392 | } | ||
| 393 | |||
| 394 | 9 | boost::optional<std::string> windowsDriveLetter(const path& p) { | |
| 395 | 9 | boost::optional<std::string> result; | |
| 396 | |||
| 397 |
1/1✓ Branch 1 taken 9 times.
|
9 | std::string path_str = toString(p); |
| 398 | |||
| 399 |
1/1✓ Branch 1 taken 9 times.
|
9 | boost::regex regex("^([a-zA-Z]):"); |
| 400 |
1/1✓ Branch 1 taken 9 times.
|
9 | boost::smatch m; |
| 401 |
3/3✓ Branch 1 taken 9 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 4 times.
|
9 | if (boost::regex_search(path_str, m, regex)) { |
| 402 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
|
5 | result = m[1]; |
| 403 | } | ||
| 404 | |||
| 405 | 18 | return result; | |
| 406 | 9 | } | |
| 407 | |||
| 408 | 4 | bool isNetworkPath(const path& p) { | |
| 409 |
5/6✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 2 times.
|
4 | if (p.empty() || !p.is_absolute()) { |
| 410 | 2 | return false; | |
| 411 | } | ||
| 412 | |||
| 413 | #if (defined(_WIN32) || defined(_WIN64)) | ||
| 414 | |||
| 415 | // TODO: JM 2018-11-06: couldn't this entire block be replaced by "PathIsNetworkPath"? | ||
| 416 | |||
| 417 | // if this is a windows drive, check if this is mapped to a remote drive | ||
| 418 | boost::optional<std::string> wdl = windowsDriveLetter(p); | ||
| 419 | if (wdl) { | ||
| 420 | std::string pstring = wdl.get() + ":\\"; | ||
| 421 | if (GetDriveType(pstring.c_str()) == DRIVE_REMOTE) { | ||
| 422 | return true; | ||
| 423 | } | ||
| 424 | } | ||
| 425 | |||
| 426 | // check if path begins with \\, e.g. \\server\file | ||
| 427 | // Shouldn't convert backslashes to slashes | ||
| 428 | boost::regex regex("^\\\\\\\\"); | ||
| 429 | if (boost::regex_search(p.string(), regex)) { | ||
| 430 | return true; | ||
| 431 | } | ||
| 432 | |||
| 433 | #endif | ||
| 434 | |||
| 435 | 2 | return false; | |
| 436 | } | ||
| 437 | |||
| 438 | 2 | bool isNetworkPathAvailable(const path& p) { | |
| 439 | // cppcheck-suppress knownConditionTrueFalse | ||
| 440 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | if (!isNetworkPath(p)) { |
| 441 | 2 | return false; | |
| 442 | } | ||
| 443 | |||
| 444 | #if (defined(_WIN32) || defined(_WIN64)) | ||
| 445 | |||
| 446 | std::string fullyQualifiedName; | ||
| 447 | |||
| 448 | // if we get a drive letter, use WNetGetConnection | ||
| 449 | boost::optional<std::string> wdl = windowsDriveLetter(p); | ||
| 450 | if (wdl) { | ||
| 451 | std::string pstring = wdl.get() + ":"; | ||
| 452 | |||
| 453 | TCHAR szDeviceName[MAX_PATH]; | ||
| 454 | DWORD dwResult, cchBuff = sizeof(szDeviceName); | ||
| 455 | dwResult = WNetGetConnection(pstring.c_str(), szDeviceName, &cchBuff); | ||
| 456 | if (dwResult != NO_ERROR) { | ||
| 457 | return false; | ||
| 458 | } | ||
| 459 | |||
| 460 | fullyQualifiedName = szDeviceName; | ||
| 461 | } else { | ||
| 462 | |||
| 463 | // otherwise we have a fully qualified resource name, e.g. \\server\file | ||
| 464 | fullyQualifiedName = p.string(); // toString(p) converts backslashes to slashes | ||
| 465 | } | ||
| 466 | |||
| 467 | // use WNetGetResourceInformation to check status | ||
| 468 | OS_ASSERT(!fullyQualifiedName.empty()); | ||
| 469 | |||
| 470 | DWORD dwBufferSize = sizeof(NETRESOURCE); | ||
| 471 | LPBYTE lpBuffer; // buffer | ||
| 472 | NETRESOURCE nr; | ||
| 473 | LPTSTR pszSystem = NULL; // variable-length strings | ||
| 474 | |||
| 475 | // Set the block of memory to zero; then initialize | ||
| 476 | // the NETRESOURCE structure. | ||
| 477 | ZeroMemory(&nr, sizeof(nr)); | ||
| 478 | |||
| 479 | nr.dwScope = RESOURCE_GLOBALNET; | ||
| 480 | nr.dwType = RESOURCETYPE_ANY; | ||
| 481 | nr.lpRemoteName = &fullyQualifiedName[0]; | ||
| 482 | |||
| 483 | // First call the WNetGetResourceInformation function with | ||
| 484 | // memory allocated to hold only a NETRESOURCE structure. This | ||
| 485 | // method can succeed if all the NETRESOURCE pointers are NULL. | ||
| 486 | // If the call fails because the buffer is too small, allocate | ||
| 487 | // a larger buffer. | ||
| 488 | lpBuffer = (LPBYTE)malloc(dwBufferSize); | ||
| 489 | if (lpBuffer == NULL) { | ||
| 490 | return false; | ||
| 491 | } | ||
| 492 | |||
| 493 | bool moreData = true; | ||
| 494 | while (moreData) { | ||
| 495 | DWORD dwResult = WNetGetResourceInformation(&nr, lpBuffer, &dwBufferSize, &pszSystem); | ||
| 496 | if (dwResult == NO_ERROR) { | ||
| 497 | free(lpBuffer); | ||
| 498 | return true; | ||
| 499 | } else if (dwResult == ERROR_MORE_DATA) { | ||
| 500 | |||
| 501 | auto newptr = (LPBYTE)realloc(lpBuffer, dwBufferSize); | ||
| 502 | if (newptr == nullptr) { | ||
| 503 | // whoops we had a memory allocation error and we need to clean things up | ||
| 504 | free(lpBuffer); | ||
| 505 | throw std::runtime_error("Memory error while attempting to allocate buffer"); | ||
| 506 | } else { | ||
| 507 | lpBuffer = newptr; | ||
| 508 | } | ||
| 509 | |||
| 510 | moreData = true; | ||
| 511 | |||
| 512 | } else { | ||
| 513 | moreData = false; | ||
| 514 | } | ||
| 515 | } | ||
| 516 | |||
| 517 | free(lpBuffer); | ||
| 518 | |||
| 519 | #endif | ||
| 520 | |||
| 521 | ✗ | return false; | |
| 522 | } | ||
| 523 | |||
| 524 | } // namespace openstudio | ||
| 525 |