Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 1 | //===-- PathV2.cpp - Implement OS Path Concept ------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the operating system PathV2 API. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Support/PathV2.h" |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 15 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ErrorHandling.h" |
| 17 | #include <cctype> |
Michael J. Spencer | 628467e | 2010-12-28 01:49:01 +0000 | [diff] [blame] | 18 | #include <cstdio> |
| 19 | #include <cstring> |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 20 | |
| 21 | namespace { |
| 22 | using llvm::StringRef; |
| 23 | |
| 24 | bool is_separator(const char value) { |
| 25 | switch(value) { |
| 26 | #ifdef LLVM_ON_WIN32 |
| 27 | case '\\': // fall through |
| 28 | #endif |
| 29 | case '/': return true; |
| 30 | default: return false; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | #ifdef LLVM_ON_WIN32 |
| 35 | const StringRef separators = "\\/"; |
Michael J. Spencer | ca3a339 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 36 | const char prefered_separator = '\\'; |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 37 | #else |
| 38 | const StringRef separators = "/"; |
| 39 | const char prefered_separator = '/'; |
| 40 | #endif |
| 41 | |
Michael J. Spencer | 470ae13 | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 42 | const llvm::error_code success; |
| 43 | |
Benjamin Kramer | 7fb8666 | 2010-12-17 18:59:09 +0000 | [diff] [blame] | 44 | StringRef find_first_component(StringRef path) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 45 | // Look for this first component in the following order. |
| 46 | // * empty (in this case we return an empty string) |
| 47 | // * either C: or {//,\\}net. |
| 48 | // * {/,\} |
| 49 | // * {.,..} |
| 50 | // * {file,directory}name |
| 51 | |
| 52 | if (path.empty()) |
| 53 | return path; |
| 54 | |
Michael J. Spencer | ca3a339 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 55 | #ifdef LLVM_ON_WIN32 |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 56 | // C: |
| 57 | if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':') |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 58 | return path.substr(0, 2); |
Michael J. Spencer | ca3a339 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 59 | #endif |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 60 | |
| 61 | // //net |
| 62 | if ((path.size() > 2) && |
Michael J. Spencer | ca3a339 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 63 | is_separator(path[0]) && |
| 64 | path[0] == path[1] && |
| 65 | !is_separator(path[2])) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 66 | // Find the next directory separator. |
Michael J. Spencer | ca3a339 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 67 | size_t end = path.find_first_of(separators, 2); |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 68 | return path.substr(0, end); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // {/,\} |
Michael J. Spencer | ca3a339 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 72 | if (is_separator(path[0])) |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 73 | return path.substr(0, 1); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 74 | |
| 75 | if (path.startswith("..")) |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 76 | return path.substr(0, 2); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 77 | |
| 78 | if (path[0] == '.') |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 79 | return path.substr(0, 1); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 80 | |
| 81 | // * {file,directory}name |
Michael J. Spencer | ca3a339 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 82 | size_t end = path.find_first_of(separators, 2); |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 83 | return path.substr(0, end); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 84 | } |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 85 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 86 | size_t filename_pos(StringRef str) { |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 87 | if (str.size() == 2 && |
| 88 | is_separator(str[0]) && |
Michael J. Spencer | ca3a339 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 89 | str[0] == str[1]) |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 90 | return 0; |
| 91 | |
| 92 | if (str.size() > 0 && is_separator(str[str.size() - 1])) |
| 93 | return str.size() - 1; |
| 94 | |
| 95 | size_t pos = str.find_last_of(separators, str.size() - 1); |
| 96 | |
| 97 | #ifdef LLVM_ON_WIN32 |
| 98 | if (pos == StringRef::npos) |
| 99 | pos = str.find_last_of(':', str.size() - 2); |
| 100 | #endif |
| 101 | |
| 102 | if (pos == StringRef::npos || |
| 103 | (pos == 1 && is_separator(str[0]))) |
| 104 | return 0; |
| 105 | |
| 106 | return pos + 1; |
| 107 | } |
| 108 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 109 | size_t root_dir_start(StringRef str) { |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 110 | // case "c:/" |
| 111 | #ifdef LLVM_ON_WIN32 |
| 112 | if (str.size() > 2 && |
| 113 | str[1] == ':' && |
| 114 | is_separator(str[2])) |
| 115 | return 2; |
| 116 | #endif |
| 117 | |
| 118 | // case "//" |
| 119 | if (str.size() == 2 && |
| 120 | is_separator(str[0]) && |
| 121 | str[0] == str[1]) |
| 122 | return StringRef::npos; |
| 123 | |
| 124 | // case "//net" |
| 125 | if (str.size() > 3 && |
| 126 | is_separator(str[0]) && |
| 127 | str[0] == str[1] && |
| 128 | !is_separator(str[2])) { |
| 129 | return str.find_first_of(separators, 2); |
| 130 | } |
| 131 | |
| 132 | // case "/" |
| 133 | if (str.size() > 0 && is_separator(str[0])) |
| 134 | return 0; |
| 135 | |
| 136 | return StringRef::npos; |
| 137 | } |
| 138 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 139 | size_t parent_path_end(StringRef path) { |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 140 | size_t end_pos = filename_pos(path); |
| 141 | |
| 142 | bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]); |
| 143 | |
| 144 | // Skip separators except for root dir. |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 145 | size_t root_dir_pos = root_dir_start(path.substr(0, end_pos)); |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 146 | |
| 147 | while(end_pos > 0 && |
| 148 | (end_pos - 1) != root_dir_pos && |
| 149 | is_separator(path[end_pos - 1])) |
| 150 | --end_pos; |
| 151 | |
| 152 | if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep) |
| 153 | return StringRef::npos; |
| 154 | |
| 155 | return end_pos; |
| 156 | } |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | namespace llvm { |
| 160 | namespace sys { |
| 161 | namespace path { |
| 162 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 163 | const_iterator begin(StringRef path) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 164 | const_iterator i; |
| 165 | i.Path = path; |
| 166 | i.Component = find_first_component(path); |
| 167 | i.Position = 0; |
| 168 | return i; |
| 169 | } |
| 170 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 171 | const_iterator end(StringRef path) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 172 | const_iterator i; |
| 173 | i.Path = path; |
| 174 | i.Position = path.size(); |
| 175 | return i; |
| 176 | } |
| 177 | |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 178 | const_iterator &const_iterator::operator++() { |
| 179 | assert(Position < Path.size() && "Tried to increment past end!"); |
| 180 | |
| 181 | // Increment Position to past the current component |
| 182 | Position += Component.size(); |
| 183 | |
| 184 | // Check for end. |
| 185 | if (Position == Path.size()) { |
| 186 | Component = StringRef(); |
| 187 | return *this; |
| 188 | } |
| 189 | |
| 190 | // Both POSIX and Windows treat paths that begin with exactly two separators |
| 191 | // specially. |
| 192 | bool was_net = Component.size() > 2 && |
| 193 | is_separator(Component[0]) && |
| 194 | Component[1] == Component[0] && |
| 195 | !is_separator(Component[2]); |
| 196 | |
| 197 | // Handle separators. |
| 198 | if (is_separator(Path[Position])) { |
| 199 | // Root dir. |
| 200 | if (was_net |
| 201 | #ifdef LLVM_ON_WIN32 |
| 202 | // c:/ |
| 203 | || Component.endswith(":") |
| 204 | #endif |
| 205 | ) { |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 206 | Component = Path.substr(Position, 1); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 207 | return *this; |
| 208 | } |
| 209 | |
| 210 | // Skip extra separators. |
| 211 | while (Position != Path.size() && |
| 212 | is_separator(Path[Position])) { |
| 213 | ++Position; |
| 214 | } |
| 215 | |
| 216 | // Treat trailing '/' as a '.'. |
| 217 | if (Position == Path.size()) { |
| 218 | --Position; |
| 219 | Component = "."; |
| 220 | return *this; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // Find next component. |
| 225 | size_t end_pos = Path.find_first_of(separators, Position); |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 226 | Component = Path.slice(Position, end_pos); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 227 | |
| 228 | return *this; |
| 229 | } |
| 230 | |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 231 | const_iterator &const_iterator::operator--() { |
| 232 | // If we're at the end and the previous char was a '/', return '.'. |
| 233 | if (Position == Path.size() && |
| 234 | Path.size() > 1 && |
| 235 | is_separator(Path[Position - 1]) |
| 236 | #ifdef LLVM_ON_WIN32 |
| 237 | && Path[Position - 2] != ':' |
| 238 | #endif |
| 239 | ) { |
| 240 | --Position; |
| 241 | Component = "."; |
| 242 | return *this; |
| 243 | } |
| 244 | |
| 245 | // Skip separators unless it's the root directory. |
| 246 | size_t root_dir_pos = root_dir_start(Path); |
| 247 | size_t end_pos = Position; |
| 248 | |
| 249 | while(end_pos > 0 && |
| 250 | (end_pos - 1) != root_dir_pos && |
| 251 | is_separator(Path[end_pos - 1])) |
| 252 | --end_pos; |
| 253 | |
| 254 | // Find next separator. |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 255 | size_t start_pos = filename_pos(Path.substr(0, end_pos)); |
| 256 | Component = Path.slice(start_pos, end_pos); |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 257 | Position = start_pos; |
| 258 | return *this; |
| 259 | } |
| 260 | |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 261 | bool const_iterator::operator==(const const_iterator &RHS) const { |
| 262 | return Path.begin() == RHS.Path.begin() && |
| 263 | Position == RHS.Position; |
| 264 | } |
| 265 | |
| 266 | bool const_iterator::operator!=(const const_iterator &RHS) const { |
| 267 | return !(*this == RHS); |
| 268 | } |
| 269 | |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 270 | ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const { |
| 271 | return Position - RHS.Position; |
| 272 | } |
| 273 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 274 | const StringRef root_path(StringRef path) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 275 | const_iterator b = begin(path), |
| 276 | pos = b, |
| 277 | e = end(path); |
| 278 | if (b != e) { |
| 279 | bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0]; |
| 280 | bool has_drive = |
| 281 | #ifdef LLVM_ON_WIN32 |
| 282 | b->endswith(":"); |
| 283 | #else |
| 284 | false; |
| 285 | #endif |
| 286 | |
| 287 | if (has_net || has_drive) { |
| 288 | if ((++pos != e) && is_separator((*pos)[0])) { |
| 289 | // {C:/,//net/}, so get the first two components. |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 290 | return path.substr(0, b->size() + pos->size()); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 291 | } else { |
| 292 | // just {C:,//net}, return the first component. |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 293 | return *b; |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | // POSIX style root directory. |
| 298 | if (is_separator((*b)[0])) { |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 299 | return *b; |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 300 | } |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 303 | return StringRef(); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 306 | const StringRef root_name(StringRef path) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 307 | const_iterator b = begin(path), |
| 308 | e = end(path); |
| 309 | if (b != e) { |
| 310 | bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0]; |
| 311 | bool has_drive = |
| 312 | #ifdef LLVM_ON_WIN32 |
| 313 | b->endswith(":"); |
| 314 | #else |
| 315 | false; |
| 316 | #endif |
| 317 | |
| 318 | if (has_net || has_drive) { |
| 319 | // just {C:,//net}, return the first component. |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 320 | return *b; |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
| 324 | // No path or no name. |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 325 | return StringRef(); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 328 | const StringRef root_directory(StringRef path) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 329 | const_iterator b = begin(path), |
| 330 | pos = b, |
| 331 | e = end(path); |
| 332 | if (b != e) { |
| 333 | bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0]; |
| 334 | bool has_drive = |
| 335 | #ifdef LLVM_ON_WIN32 |
| 336 | b->endswith(":"); |
| 337 | #else |
| 338 | false; |
| 339 | #endif |
| 340 | |
| 341 | if ((has_net || has_drive) && |
| 342 | // {C:,//net}, skip to the next component. |
| 343 | (++pos != e) && is_separator((*pos)[0])) { |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 344 | return *pos; |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | // POSIX style root directory. |
| 348 | if (!has_net && is_separator((*b)[0])) { |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 349 | return *b; |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | // No path or no root. |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 354 | return StringRef(); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 357 | const StringRef relative_path(StringRef path) { |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 358 | StringRef root = root_path(path); |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 359 | return root.substr(root.size()); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 362 | void append(SmallVectorImpl<char> &path, const Twine &a, |
| 363 | const Twine &b, |
| 364 | const Twine &c, |
| 365 | const Twine &d) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 366 | SmallString<32> a_storage; |
| 367 | SmallString<32> b_storage; |
| 368 | SmallString<32> c_storage; |
| 369 | SmallString<32> d_storage; |
| 370 | |
| 371 | SmallVector<StringRef, 4> components; |
| 372 | if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage)); |
| 373 | if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage)); |
| 374 | if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage)); |
| 375 | if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage)); |
| 376 | |
| 377 | for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(), |
| 378 | e = components.end(); |
| 379 | i != e; ++i) { |
| 380 | bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]); |
| 381 | bool component_has_sep = !i->empty() && is_separator((*i)[0]); |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 382 | bool is_root_name = has_root_name(*i); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 383 | |
| 384 | if (path_has_sep) { |
| 385 | // Strip separators from beginning of component. |
| 386 | size_t loc = i->find_first_not_of(separators); |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 387 | StringRef c = i->substr(loc); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 388 | |
| 389 | // Append it. |
| 390 | path.append(c.begin(), c.end()); |
| 391 | continue; |
| 392 | } |
| 393 | |
Michael J. Spencer | f150e76 | 2010-12-06 04:28:23 +0000 | [diff] [blame] | 394 | if (!component_has_sep && !(path.empty() || is_root_name)) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 395 | // Add a separator. |
| 396 | path.push_back(prefered_separator); |
| 397 | } |
| 398 | |
| 399 | path.append(i->begin(), i->end()); |
| 400 | } |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 403 | const StringRef parent_path(StringRef path) { |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 404 | size_t end_pos = parent_path_end(path); |
| 405 | if (end_pos == StringRef::npos) |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 406 | return StringRef(); |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 407 | else |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 408 | return path.substr(0, end_pos); |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 411 | void remove_filename(SmallVectorImpl<char> &path) { |
Michael J. Spencer | dbfb56b | 2010-12-01 00:52:28 +0000 | [diff] [blame] | 412 | size_t end_pos = parent_path_end(StringRef(path.begin(), path.size())); |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 413 | if (end_pos != StringRef::npos) |
| 414 | path.set_size(end_pos); |
Michael J. Spencer | dbfb56b | 2010-12-01 00:52:28 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 417 | void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) { |
Michael J. Spencer | 52ed867 | 2010-12-01 00:52:55 +0000 | [diff] [blame] | 418 | StringRef p(path.begin(), path.size()); |
| 419 | SmallString<32> ext_storage; |
| 420 | StringRef ext = extension.toStringRef(ext_storage); |
| 421 | |
| 422 | // Erase existing extension. |
| 423 | size_t pos = p.find_last_of('.'); |
| 424 | if (pos != StringRef::npos && pos >= filename_pos(p)) |
| 425 | path.set_size(pos); |
| 426 | |
| 427 | // Append '.' if needed. |
| 428 | if (ext.size() > 0 && ext[0] != '.') |
| 429 | path.push_back('.'); |
| 430 | |
| 431 | // Append extension. |
| 432 | path.append(ext.begin(), ext.end()); |
Michael J. Spencer | 52ed867 | 2010-12-01 00:52:55 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 435 | void native(const Twine &path, SmallVectorImpl<char> &result) { |
Michael J. Spencer | 722d5ad | 2010-12-01 02:48:27 +0000 | [diff] [blame] | 436 | // Clear result. |
Michael J. Spencer | fbd1bbd | 2010-12-07 01:23:19 +0000 | [diff] [blame] | 437 | result.clear(); |
Michael J. Spencer | 722d5ad | 2010-12-01 02:48:27 +0000 | [diff] [blame] | 438 | #ifdef LLVM_ON_WIN32 |
| 439 | SmallString<128> path_storage; |
| 440 | StringRef p = path.toStringRef(path_storage); |
| 441 | result.reserve(p.size()); |
| 442 | for (StringRef::const_iterator i = p.begin(), |
| 443 | e = p.end(); |
| 444 | i != e; |
| 445 | ++i) { |
| 446 | if (*i == '/') |
| 447 | result.push_back('\\'); |
| 448 | else |
| 449 | result.push_back(*i); |
| 450 | } |
| 451 | #else |
| 452 | path.toVector(result); |
| 453 | #endif |
Michael J. Spencer | 722d5ad | 2010-12-01 02:48:27 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 456 | const StringRef filename(StringRef path) { |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 457 | return *(--end(path)); |
Michael J. Spencer | a979355 | 2010-12-01 03:18:17 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 460 | const StringRef stem(StringRef path) { |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 461 | StringRef fname = filename(path); |
Michael J. Spencer | 34ab1f6 | 2010-12-01 03:18:33 +0000 | [diff] [blame] | 462 | size_t pos = fname.find_last_of('.'); |
| 463 | if (pos == StringRef::npos) |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 464 | return fname; |
Michael J. Spencer | 34ab1f6 | 2010-12-01 03:18:33 +0000 | [diff] [blame] | 465 | else |
| 466 | if ((fname.size() == 1 && fname == ".") || |
| 467 | (fname.size() == 2 && fname == "..")) |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 468 | return fname; |
Michael J. Spencer | 34ab1f6 | 2010-12-01 03:18:33 +0000 | [diff] [blame] | 469 | else |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 470 | return fname.substr(0, pos); |
Michael J. Spencer | 34ab1f6 | 2010-12-01 03:18:33 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 473 | const StringRef extension(StringRef path) { |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 474 | StringRef fname = filename(path); |
Michael J. Spencer | 5265f22 | 2010-12-01 03:37:41 +0000 | [diff] [blame] | 475 | size_t pos = fname.find_last_of('.'); |
| 476 | if (pos == StringRef::npos) |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 477 | return StringRef(); |
Michael J. Spencer | 5265f22 | 2010-12-01 03:37:41 +0000 | [diff] [blame] | 478 | else |
| 479 | if ((fname.size() == 1 && fname == ".") || |
| 480 | (fname.size() == 2 && fname == "..")) |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 481 | return StringRef(); |
Michael J. Spencer | 5265f22 | 2010-12-01 03:37:41 +0000 | [diff] [blame] | 482 | else |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 483 | return fname.substr(pos); |
Michael J. Spencer | 5265f22 | 2010-12-01 03:37:41 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 486 | bool has_root_name(const Twine &path) { |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 487 | SmallString<128> path_storage; |
| 488 | StringRef p = path.toStringRef(path_storage); |
| 489 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 490 | return !root_name(p).empty(); |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 493 | bool has_root_directory(const Twine &path) { |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 494 | SmallString<128> path_storage; |
| 495 | StringRef p = path.toStringRef(path_storage); |
| 496 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 497 | return !root_directory(p).empty(); |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 500 | bool has_root_path(const Twine &path) { |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 501 | SmallString<128> path_storage; |
| 502 | StringRef p = path.toStringRef(path_storage); |
| 503 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 504 | return !root_path(p).empty(); |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Michael J. Spencer | 2d484eb | 2010-12-20 13:30:28 +0000 | [diff] [blame] | 507 | bool has_relative_path(const Twine &path) { |
| 508 | SmallString<128> path_storage; |
| 509 | StringRef p = path.toStringRef(path_storage); |
| 510 | |
| 511 | return !relative_path(p).empty(); |
| 512 | } |
| 513 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 514 | bool has_filename(const Twine &path) { |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 515 | SmallString<128> path_storage; |
| 516 | StringRef p = path.toStringRef(path_storage); |
| 517 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 518 | return !filename(p).empty(); |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 521 | bool has_parent_path(const Twine &path) { |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 522 | SmallString<128> path_storage; |
| 523 | StringRef p = path.toStringRef(path_storage); |
| 524 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 525 | return !parent_path(p).empty(); |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 528 | bool has_stem(const Twine &path) { |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 529 | SmallString<128> path_storage; |
| 530 | StringRef p = path.toStringRef(path_storage); |
| 531 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 532 | return !stem(p).empty(); |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 535 | bool has_extension(const Twine &path) { |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 536 | SmallString<128> path_storage; |
| 537 | StringRef p = path.toStringRef(path_storage); |
| 538 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 539 | return !extension(p).empty(); |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 542 | bool is_absolute(const Twine &path) { |
Michael J. Spencer | ce2b68f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 543 | SmallString<128> path_storage; |
| 544 | StringRef p = path.toStringRef(path_storage); |
| 545 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 546 | bool rootDir = has_root_directory(p), |
Michael J. Spencer | ce2b68f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 547 | #ifdef LLVM_ON_WIN32 |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 548 | rootName = has_root_name(p); |
Michael J. Spencer | ce2b68f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 549 | #else |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 550 | rootName = true; |
Michael J. Spencer | ce2b68f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 551 | #endif |
| 552 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 553 | return rootDir && rootName; |
Michael J. Spencer | ce2b68f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 556 | bool is_relative(const Twine &path) { |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 557 | return !is_absolute(path); |
Michael J. Spencer | ce2b68f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 560 | } // end namespace path |
| 561 | |
| 562 | namespace fs { |
| 563 | |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 564 | error_code make_absolute(SmallVectorImpl<char> &path) { |
| 565 | StringRef p(path.data(), path.size()); |
| 566 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 567 | bool rootName = path::has_root_name(p), |
| 568 | rootDirectory = path::has_root_directory(p); |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 569 | |
| 570 | // Already absolute. |
| 571 | if (rootName && rootDirectory) |
| 572 | return success; |
| 573 | |
| 574 | // All of the following conditions will need the current directory. |
| 575 | SmallString<128> current_dir; |
| 576 | if (error_code ec = current_path(current_dir)) return ec; |
| 577 | |
| 578 | // Relative path. Prepend the current directory. |
| 579 | if (!rootName && !rootDirectory) { |
| 580 | // Append path to the current directory. |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 581 | path::append(current_dir, p); |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 582 | // Set path to the result. |
| 583 | path.swap(current_dir); |
| 584 | return success; |
| 585 | } |
| 586 | |
| 587 | if (!rootName && rootDirectory) { |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 588 | StringRef cdrn = path::root_name(current_dir); |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 589 | SmallString<128> curDirRootName(cdrn.begin(), cdrn.end()); |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 590 | path::append(curDirRootName, p); |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 591 | // Set path to the result. |
| 592 | path.swap(curDirRootName); |
| 593 | return success; |
| 594 | } |
| 595 | |
| 596 | if (rootName && !rootDirectory) { |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 597 | StringRef pRootName = path::root_name(p); |
| 598 | StringRef bRootDirectory = path::root_directory(current_dir); |
| 599 | StringRef bRelativePath = path::relative_path(current_dir); |
| 600 | StringRef pRelativePath = path::relative_path(p); |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 601 | |
| 602 | SmallString<128> res; |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 603 | path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath); |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 604 | path.swap(res); |
| 605 | return success; |
| 606 | } |
| 607 | |
| 608 | llvm_unreachable("All rootName and rootDirectory combinations should have " |
| 609 | "occurred above!"); |
| 610 | } |
| 611 | |
Michael J. Spencer | b83769f | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 612 | error_code create_directories(const Twine &path, bool &existed) { |
| 613 | SmallString<128> path_storage; |
| 614 | StringRef p = path.toStringRef(path_storage); |
| 615 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 616 | StringRef parent = path::parent_path(p); |
Michael J. Spencer | b83769f | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 617 | bool parent_exists; |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 618 | |
Michael J. Spencer | b83769f | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 619 | if (error_code ec = fs::exists(parent, parent_exists)) return ec; |
| 620 | |
| 621 | if (!parent_exists) |
| 622 | return create_directories(parent, existed); |
| 623 | |
| 624 | return create_directory(p, existed); |
| 625 | } |
| 626 | |
Michael J. Spencer | 61187dd | 2010-12-09 17:37:02 +0000 | [diff] [blame] | 627 | bool exists(file_status status) { |
| 628 | return status_known(status) && status.type() != file_type::file_not_found; |
| 629 | } |
| 630 | |
| 631 | bool status_known(file_status s) { |
| 632 | return s.type() != file_type::status_error; |
| 633 | } |
| 634 | |
| 635 | bool is_directory(file_status status) { |
| 636 | return status.type() == file_type::directory_file; |
| 637 | } |
| 638 | |
| 639 | bool is_regular_file(file_status status) { |
| 640 | return status.type() == file_type::regular_file; |
| 641 | } |
| 642 | |
| 643 | bool is_symlink(file_status status) { |
| 644 | return status.type() == file_type::symlink_file; |
| 645 | } |
| 646 | |
| 647 | bool is_other(file_status status) { |
| 648 | return exists(status) && |
| 649 | !is_regular_file(status) && |
| 650 | !is_directory(status) && |
| 651 | !is_symlink(status); |
| 652 | } |
| 653 | |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 654 | void directory_entry::replace_filename(const Twine &filename, file_status st, |
| 655 | file_status symlink_st) { |
| 656 | SmallString<128> path(Path.begin(), Path.end()); |
| 657 | path::remove_filename(path); |
| 658 | path::append(path, filename); |
| 659 | Path = path.str(); |
| 660 | Status = st; |
| 661 | SymlinkStatus = symlink_st; |
| 662 | } |
| 663 | |
Michael J. Spencer | 628467e | 2010-12-28 01:49:01 +0000 | [diff] [blame] | 664 | error_code has_magic(const Twine &path, const Twine &magic, bool &result) { |
| 665 | SmallString<128> PathStorage; |
| 666 | SmallString<32> MagicStorage; |
| 667 | StringRef Path = path.toNullTerminatedStringRef(PathStorage); |
| 668 | StringRef Magic = magic.toNullTerminatedStringRef(MagicStorage); |
| 669 | |
| 670 | assert(Magic.size() > 0 && "magic must be non-empty!"); |
| 671 | |
| 672 | SmallString<32> BufferStorage; |
| 673 | BufferStorage.reserve(Magic.size()); |
| 674 | |
| 675 | // Open file. |
| 676 | std::FILE *file = std::fopen(Path.data(), "rb"); |
| 677 | if (file == 0) |
| 678 | return error_code(errno, posix_category()); |
Benjamin Kramer | 3fee954 | 2010-12-28 13:05:13 +0000 | [diff] [blame^] | 679 | size_t size = ::fread(BufferStorage.data(), 1, Magic.size(), file); |
Michael J. Spencer | 628467e | 2010-12-28 01:49:01 +0000 | [diff] [blame] | 680 | if (size != Magic.size()) { |
| 681 | int error = errno; |
| 682 | bool eof = std::feof(file) != 0; |
| 683 | std::fclose(file); |
| 684 | if (eof) { |
| 685 | // EOF, return false. |
| 686 | result = false; |
| 687 | return success; |
| 688 | } |
| 689 | return error_code(error, posix_category()); |
| 690 | } |
| 691 | std::fclose(file); |
| 692 | |
| 693 | if (std::memcmp(BufferStorage.data(), Magic.data(), Magic.size()) != 0) |
| 694 | result = false; |
| 695 | else |
| 696 | result = true; |
| 697 | return success; |
| 698 | } |
| 699 | |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 700 | } // end namespace fs |
| 701 | } // end namespace sys |
| 702 | } // end namespace llvm |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 703 | |
| 704 | // Include the truly platform-specific parts. |
| 705 | #if defined(LLVM_ON_UNIX) |
| 706 | #include "Unix/PathV2.inc" |
| 707 | #endif |
| 708 | #if defined(LLVM_ON_WIN32) |
| 709 | #include "Windows/PathV2.inc" |
| 710 | #endif |