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 | 5b08230 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Endian.h" |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 18 | #include <cctype> |
Michael J. Spencer | 628467e | 2010-12-28 01:49:01 +0000 | [diff] [blame] | 19 | #include <cstdio> |
| 20 | #include <cstring> |
Douglas Gregor | b0b5bde | 2013-03-21 21:46:10 +0000 | [diff] [blame^] | 21 | #ifdef __APPLE__ |
| 22 | #include <unistd.h> |
| 23 | #endif |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 24 | |
| 25 | namespace { |
| 26 | using llvm::StringRef; |
Zhanyong Wan | 63cc3a8 | 2011-02-11 21:24:40 +0000 | [diff] [blame] | 27 | using llvm::sys::path::is_separator; |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 28 | |
| 29 | #ifdef LLVM_ON_WIN32 |
Benjamin Kramer | 80fd2a1 | 2012-02-08 13:13:47 +0000 | [diff] [blame] | 30 | const char *separators = "\\/"; |
| 31 | const char prefered_separator = '\\'; |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 32 | #else |
Benjamin Kramer | 80fd2a1 | 2012-02-08 13:13:47 +0000 | [diff] [blame] | 33 | const char separators = '/'; |
| 34 | const char prefered_separator = '/'; |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
Benjamin Kramer | 7fb8666 | 2010-12-17 18:59:09 +0000 | [diff] [blame] | 37 | StringRef find_first_component(StringRef path) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 38 | // Look for this first component in the following order. |
| 39 | // * empty (in this case we return an empty string) |
| 40 | // * either C: or {//,\\}net. |
| 41 | // * {/,\} |
| 42 | // * {.,..} |
| 43 | // * {file,directory}name |
| 44 | |
| 45 | if (path.empty()) |
| 46 | return path; |
| 47 | |
Michael J. Spencer | ca3a339 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 48 | #ifdef LLVM_ON_WIN32 |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 49 | // C: |
Guy Benyei | 87d0b9e | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 50 | if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) && |
| 51 | path[1] == ':') |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 52 | return path.substr(0, 2); |
Michael J. Spencer | ca3a339 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 53 | #endif |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 54 | |
| 55 | // //net |
| 56 | if ((path.size() > 2) && |
Michael J. Spencer | ca3a339 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 57 | is_separator(path[0]) && |
| 58 | path[0] == path[1] && |
| 59 | !is_separator(path[2])) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 60 | // Find the next directory separator. |
Michael J. Spencer | ca3a339 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 61 | size_t end = path.find_first_of(separators, 2); |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 62 | return path.substr(0, end); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // {/,\} |
Michael J. Spencer | ca3a339 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 66 | if (is_separator(path[0])) |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 67 | return path.substr(0, 1); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 68 | |
| 69 | if (path.startswith("..")) |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 70 | return path.substr(0, 2); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 71 | |
| 72 | if (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 | // * {file,directory}name |
Michael J. Spencer | ca3a339 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 76 | size_t end = path.find_first_of(separators, 2); |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 77 | return path.substr(0, end); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 78 | } |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 79 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 80 | size_t filename_pos(StringRef str) { |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 81 | if (str.size() == 2 && |
| 82 | is_separator(str[0]) && |
Michael J. Spencer | ca3a339 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 83 | str[0] == str[1]) |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 84 | return 0; |
| 85 | |
| 86 | if (str.size() > 0 && is_separator(str[str.size() - 1])) |
| 87 | return str.size() - 1; |
| 88 | |
| 89 | size_t pos = str.find_last_of(separators, str.size() - 1); |
| 90 | |
| 91 | #ifdef LLVM_ON_WIN32 |
| 92 | if (pos == StringRef::npos) |
| 93 | pos = str.find_last_of(':', str.size() - 2); |
| 94 | #endif |
| 95 | |
| 96 | if (pos == StringRef::npos || |
| 97 | (pos == 1 && is_separator(str[0]))) |
| 98 | return 0; |
| 99 | |
| 100 | return pos + 1; |
| 101 | } |
| 102 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 103 | size_t root_dir_start(StringRef str) { |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 104 | // case "c:/" |
| 105 | #ifdef LLVM_ON_WIN32 |
| 106 | if (str.size() > 2 && |
| 107 | str[1] == ':' && |
| 108 | is_separator(str[2])) |
| 109 | return 2; |
| 110 | #endif |
| 111 | |
| 112 | // case "//" |
| 113 | if (str.size() == 2 && |
| 114 | is_separator(str[0]) && |
| 115 | str[0] == str[1]) |
| 116 | return StringRef::npos; |
| 117 | |
| 118 | // case "//net" |
| 119 | if (str.size() > 3 && |
| 120 | is_separator(str[0]) && |
| 121 | str[0] == str[1] && |
| 122 | !is_separator(str[2])) { |
| 123 | return str.find_first_of(separators, 2); |
| 124 | } |
| 125 | |
| 126 | // case "/" |
| 127 | if (str.size() > 0 && is_separator(str[0])) |
| 128 | return 0; |
| 129 | |
| 130 | return StringRef::npos; |
| 131 | } |
| 132 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 133 | size_t parent_path_end(StringRef path) { |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 134 | size_t end_pos = filename_pos(path); |
| 135 | |
| 136 | bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]); |
| 137 | |
| 138 | // Skip separators except for root dir. |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 139 | 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] | 140 | |
| 141 | while(end_pos > 0 && |
| 142 | (end_pos - 1) != root_dir_pos && |
| 143 | is_separator(path[end_pos - 1])) |
| 144 | --end_pos; |
| 145 | |
| 146 | if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep) |
| 147 | return StringRef::npos; |
| 148 | |
| 149 | return end_pos; |
| 150 | } |
Zhanyong Wan | 63cc3a8 | 2011-02-11 21:24:40 +0000 | [diff] [blame] | 151 | } // end unnamed namespace |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 152 | |
| 153 | namespace llvm { |
| 154 | namespace sys { |
| 155 | namespace path { |
| 156 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 157 | const_iterator begin(StringRef path) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 158 | const_iterator i; |
| 159 | i.Path = path; |
| 160 | i.Component = find_first_component(path); |
| 161 | i.Position = 0; |
| 162 | return i; |
| 163 | } |
| 164 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 165 | const_iterator end(StringRef path) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 166 | const_iterator i; |
| 167 | i.Path = path; |
| 168 | i.Position = path.size(); |
| 169 | return i; |
| 170 | } |
| 171 | |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 172 | const_iterator &const_iterator::operator++() { |
| 173 | assert(Position < Path.size() && "Tried to increment past end!"); |
| 174 | |
| 175 | // Increment Position to past the current component |
| 176 | Position += Component.size(); |
| 177 | |
| 178 | // Check for end. |
| 179 | if (Position == Path.size()) { |
| 180 | Component = StringRef(); |
| 181 | return *this; |
| 182 | } |
| 183 | |
| 184 | // Both POSIX and Windows treat paths that begin with exactly two separators |
| 185 | // specially. |
| 186 | bool was_net = Component.size() > 2 && |
| 187 | is_separator(Component[0]) && |
| 188 | Component[1] == Component[0] && |
| 189 | !is_separator(Component[2]); |
| 190 | |
| 191 | // Handle separators. |
| 192 | if (is_separator(Path[Position])) { |
| 193 | // Root dir. |
| 194 | if (was_net |
| 195 | #ifdef LLVM_ON_WIN32 |
| 196 | // c:/ |
| 197 | || Component.endswith(":") |
| 198 | #endif |
| 199 | ) { |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 200 | Component = Path.substr(Position, 1); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 201 | return *this; |
| 202 | } |
| 203 | |
| 204 | // Skip extra separators. |
| 205 | while (Position != Path.size() && |
| 206 | is_separator(Path[Position])) { |
| 207 | ++Position; |
| 208 | } |
| 209 | |
| 210 | // Treat trailing '/' as a '.'. |
| 211 | if (Position == Path.size()) { |
| 212 | --Position; |
| 213 | Component = "."; |
| 214 | return *this; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Find next component. |
| 219 | size_t end_pos = Path.find_first_of(separators, Position); |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 220 | Component = Path.slice(Position, end_pos); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 221 | |
| 222 | return *this; |
| 223 | } |
| 224 | |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 225 | const_iterator &const_iterator::operator--() { |
| 226 | // If we're at the end and the previous char was a '/', return '.'. |
| 227 | if (Position == Path.size() && |
| 228 | Path.size() > 1 && |
| 229 | is_separator(Path[Position - 1]) |
| 230 | #ifdef LLVM_ON_WIN32 |
| 231 | && Path[Position - 2] != ':' |
| 232 | #endif |
| 233 | ) { |
| 234 | --Position; |
| 235 | Component = "."; |
| 236 | return *this; |
| 237 | } |
| 238 | |
| 239 | // Skip separators unless it's the root directory. |
| 240 | size_t root_dir_pos = root_dir_start(Path); |
| 241 | size_t end_pos = Position; |
| 242 | |
| 243 | while(end_pos > 0 && |
| 244 | (end_pos - 1) != root_dir_pos && |
| 245 | is_separator(Path[end_pos - 1])) |
| 246 | --end_pos; |
| 247 | |
| 248 | // Find next separator. |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 249 | size_t start_pos = filename_pos(Path.substr(0, end_pos)); |
| 250 | Component = Path.slice(start_pos, end_pos); |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 251 | Position = start_pos; |
| 252 | return *this; |
| 253 | } |
| 254 | |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 255 | bool const_iterator::operator==(const const_iterator &RHS) const { |
| 256 | return Path.begin() == RHS.Path.begin() && |
| 257 | Position == RHS.Position; |
| 258 | } |
| 259 | |
| 260 | bool const_iterator::operator!=(const const_iterator &RHS) const { |
| 261 | return !(*this == RHS); |
| 262 | } |
| 263 | |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 264 | ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const { |
| 265 | return Position - RHS.Position; |
| 266 | } |
| 267 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 268 | const StringRef root_path(StringRef path) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 269 | const_iterator b = begin(path), |
| 270 | pos = b, |
| 271 | e = end(path); |
| 272 | if (b != e) { |
| 273 | bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0]; |
| 274 | bool has_drive = |
| 275 | #ifdef LLVM_ON_WIN32 |
| 276 | b->endswith(":"); |
| 277 | #else |
| 278 | false; |
| 279 | #endif |
| 280 | |
| 281 | if (has_net || has_drive) { |
| 282 | if ((++pos != e) && is_separator((*pos)[0])) { |
| 283 | // {C:/,//net/}, so get the first two components. |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 284 | return path.substr(0, b->size() + pos->size()); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 285 | } else { |
| 286 | // just {C:,//net}, return the first component. |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 287 | return *b; |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | // POSIX style root directory. |
| 292 | if (is_separator((*b)[0])) { |
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 | } |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 297 | return StringRef(); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 300 | const StringRef root_name(StringRef path) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 301 | const_iterator b = begin(path), |
| 302 | e = end(path); |
| 303 | if (b != e) { |
| 304 | bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0]; |
| 305 | bool has_drive = |
| 306 | #ifdef LLVM_ON_WIN32 |
| 307 | b->endswith(":"); |
| 308 | #else |
| 309 | false; |
| 310 | #endif |
| 311 | |
| 312 | if (has_net || has_drive) { |
| 313 | // just {C:,//net}, return the first component. |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 314 | return *b; |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | // No path or no name. |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 319 | return StringRef(); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 322 | const StringRef root_directory(StringRef path) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 323 | const_iterator b = begin(path), |
| 324 | pos = b, |
| 325 | e = end(path); |
| 326 | if (b != e) { |
| 327 | bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0]; |
| 328 | bool has_drive = |
| 329 | #ifdef LLVM_ON_WIN32 |
| 330 | b->endswith(":"); |
| 331 | #else |
| 332 | false; |
| 333 | #endif |
| 334 | |
| 335 | if ((has_net || has_drive) && |
| 336 | // {C:,//net}, skip to the next component. |
| 337 | (++pos != e) && is_separator((*pos)[0])) { |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 338 | return *pos; |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | // POSIX style root directory. |
| 342 | if (!has_net && is_separator((*b)[0])) { |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 343 | return *b; |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | // No path or no root. |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 348 | return StringRef(); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Benjamin Kramer | d53f3c8 | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 351 | const StringRef relative_path(StringRef path) { |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 352 | StringRef root = root_path(path); |
Michael J. Spencer | d0fff18 | 2012-02-29 00:06:24 +0000 | [diff] [blame] | 353 | return path.substr(root.size()); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 356 | void append(SmallVectorImpl<char> &path, const Twine &a, |
| 357 | const Twine &b, |
| 358 | const Twine &c, |
| 359 | const Twine &d) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 360 | SmallString<32> a_storage; |
| 361 | SmallString<32> b_storage; |
| 362 | SmallString<32> c_storage; |
| 363 | SmallString<32> d_storage; |
| 364 | |
| 365 | SmallVector<StringRef, 4> components; |
| 366 | if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage)); |
| 367 | if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage)); |
| 368 | if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage)); |
| 369 | if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage)); |
| 370 | |
| 371 | for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(), |
| 372 | e = components.end(); |
| 373 | i != e; ++i) { |
| 374 | bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]); |
| 375 | bool component_has_sep = !i->empty() && is_separator((*i)[0]); |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 376 | bool is_root_name = has_root_name(*i); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 377 | |
| 378 | if (path_has_sep) { |
| 379 | // Strip separators from beginning of component. |
| 380 | size_t loc = i->find_first_not_of(separators); |
Benjamin Kramer | 3ce88c9 | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 381 | StringRef c = i->substr(loc); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 382 | |
| 383 | // Append it. |
| 384 | path.append(c.begin(), c.end()); |
| 385 | continue; |
| 386 | } |
| 387 | |
Michael J. Spencer | f150e76 | 2010-12-06 04:28:23 +0000 | [diff] [blame] | 388 | if (!component_has_sep && !(path.empty() || is_root_name)) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 389 | // Add a separator. |
| 390 | path.push_back(prefered_separator); |
| 391 | } |
| 392 | |
| 393 | path.append(i->begin(), i->end()); |
| 394 | } |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Argyrios Kyrtzidis | 6e31b9b | 2011-02-15 17:51:19 +0000 | [diff] [blame] | 397 | void append(SmallVectorImpl<char> &path, |
| 398 | const_iterator begin, const_iterator end) { |
| 399 | for (; begin != end; ++begin) |
| 400 | path::append(path, *begin); |
| 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 | |
Zhanyong Wan | 63cc3a8 | 2011-02-11 21:24:40 +0000 | [diff] [blame] | 486 | bool is_separator(char value) { |
| 487 | switch(value) { |
| 488 | #ifdef LLVM_ON_WIN32 |
| 489 | case '\\': // fall through |
| 490 | #endif |
| 491 | case '/': return true; |
| 492 | default: return false; |
| 493 | } |
| 494 | } |
| 495 | |
Douglas Gregor | 55cf815 | 2011-09-14 20:27:01 +0000 | [diff] [blame] | 496 | void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) { |
| 497 | result.clear(); |
Michael J. Spencer | faebf11 | 2011-12-13 23:16:15 +0000 | [diff] [blame] | 498 | |
Douglas Gregor | b0b5bde | 2013-03-21 21:46:10 +0000 | [diff] [blame^] | 499 | #ifdef __APPLE__ |
| 500 | // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR. |
| 501 | int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR |
| 502 | : _CS_DARWIN_USER_CACHE_DIR; |
| 503 | size_t ConfLen = confstr(ConfName, 0, 0); |
| 504 | if (ConfLen > 0) { |
| 505 | do { |
| 506 | result.resize(ConfLen); |
| 507 | ConfLen = confstr(ConfName, result.data(), result.size()); |
| 508 | } while (ConfLen > 0 && ConfLen != result.size()); |
| 509 | |
| 510 | if (ConfLen > 0) { |
| 511 | assert(result.back() == 0); |
| 512 | result.pop_back(); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | result.clear(); |
| 517 | } |
| 518 | #endif |
| 519 | |
Douglas Gregor | 55cf815 | 2011-09-14 20:27:01 +0000 | [diff] [blame] | 520 | // Check whether the temporary directory is specified by an environment |
| 521 | // variable. |
| 522 | const char *EnvironmentVariable; |
| 523 | #ifdef LLVM_ON_WIN32 |
| 524 | EnvironmentVariable = "TEMP"; |
| 525 | #else |
| 526 | EnvironmentVariable = "TMPDIR"; |
| 527 | #endif |
| 528 | if (char *RequestedDir = getenv(EnvironmentVariable)) { |
| 529 | result.append(RequestedDir, RequestedDir + strlen(RequestedDir)); |
| 530 | return; |
| 531 | } |
Michael J. Spencer | faebf11 | 2011-12-13 23:16:15 +0000 | [diff] [blame] | 532 | |
Douglas Gregor | 55cf815 | 2011-09-14 20:27:01 +0000 | [diff] [blame] | 533 | // Fall back to a system default. |
| 534 | const char *DefaultResult; |
| 535 | #ifdef LLVM_ON_WIN32 |
| 536 | (void)erasedOnReboot; |
Douglas Gregor | d26d6b6 | 2011-09-14 23:21:47 +0000 | [diff] [blame] | 537 | DefaultResult = "C:\\TEMP"; |
Douglas Gregor | 55cf815 | 2011-09-14 20:27:01 +0000 | [diff] [blame] | 538 | #else |
| 539 | if (erasedOnReboot) |
| 540 | DefaultResult = "/tmp"; |
| 541 | else |
| 542 | DefaultResult = "/var/tmp"; |
| 543 | #endif |
| 544 | result.append(DefaultResult, DefaultResult + strlen(DefaultResult)); |
| 545 | } |
Michael J. Spencer | faebf11 | 2011-12-13 23:16:15 +0000 | [diff] [blame] | 546 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 547 | bool has_root_name(const Twine &path) { |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 548 | SmallString<128> path_storage; |
| 549 | StringRef p = path.toStringRef(path_storage); |
| 550 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 551 | return !root_name(p).empty(); |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 554 | bool has_root_directory(const Twine &path) { |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 555 | SmallString<128> path_storage; |
| 556 | StringRef p = path.toStringRef(path_storage); |
| 557 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 558 | return !root_directory(p).empty(); |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 561 | bool has_root_path(const Twine &path) { |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 562 | SmallString<128> path_storage; |
| 563 | StringRef p = path.toStringRef(path_storage); |
| 564 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 565 | return !root_path(p).empty(); |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Michael J. Spencer | 2d484eb | 2010-12-20 13:30:28 +0000 | [diff] [blame] | 568 | bool has_relative_path(const Twine &path) { |
| 569 | SmallString<128> path_storage; |
| 570 | StringRef p = path.toStringRef(path_storage); |
| 571 | |
| 572 | return !relative_path(p).empty(); |
| 573 | } |
| 574 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 575 | bool has_filename(const Twine &path) { |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 576 | SmallString<128> path_storage; |
| 577 | StringRef p = path.toStringRef(path_storage); |
| 578 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 579 | return !filename(p).empty(); |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 582 | bool has_parent_path(const Twine &path) { |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 583 | SmallString<128> path_storage; |
| 584 | StringRef p = path.toStringRef(path_storage); |
| 585 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 586 | return !parent_path(p).empty(); |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 589 | bool has_stem(const Twine &path) { |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 590 | SmallString<128> path_storage; |
| 591 | StringRef p = path.toStringRef(path_storage); |
| 592 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 593 | return !stem(p).empty(); |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 596 | bool has_extension(const Twine &path) { |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 597 | SmallString<128> path_storage; |
| 598 | StringRef p = path.toStringRef(path_storage); |
| 599 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 600 | return !extension(p).empty(); |
Michael J. Spencer | ae18008 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 603 | bool is_absolute(const Twine &path) { |
Michael J. Spencer | ce2b68f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 604 | SmallString<128> path_storage; |
| 605 | StringRef p = path.toStringRef(path_storage); |
| 606 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 607 | bool rootDir = has_root_directory(p), |
Michael J. Spencer | ce2b68f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 608 | #ifdef LLVM_ON_WIN32 |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 609 | rootName = has_root_name(p); |
Michael J. Spencer | ce2b68f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 610 | #else |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 611 | rootName = true; |
Michael J. Spencer | ce2b68f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 612 | #endif |
| 613 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 614 | return rootDir && rootName; |
Michael J. Spencer | ce2b68f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Michael J. Spencer | 12ccf67 | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 617 | bool is_relative(const Twine &path) { |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 618 | return !is_absolute(path); |
Michael J. Spencer | ce2b68f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 621 | } // end namespace path |
| 622 | |
| 623 | namespace fs { |
| 624 | |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 625 | error_code make_absolute(SmallVectorImpl<char> &path) { |
| 626 | StringRef p(path.data(), path.size()); |
| 627 | |
Daniel Dunbar | d1d7216 | 2012-02-29 00:20:37 +0000 | [diff] [blame] | 628 | bool rootDirectory = path::has_root_directory(p), |
| 629 | #ifdef LLVM_ON_WIN32 |
Daniel Dunbar | 95fe7b9 | 2012-02-29 00:46:46 +0000 | [diff] [blame] | 630 | rootName = path::has_root_name(p); |
Daniel Dunbar | d1d7216 | 2012-02-29 00:20:37 +0000 | [diff] [blame] | 631 | #else |
| 632 | rootName = true; |
| 633 | #endif |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 634 | |
| 635 | // Already absolute. |
| 636 | if (rootName && rootDirectory) |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 637 | return error_code::success(); |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 638 | |
| 639 | // All of the following conditions will need the current directory. |
| 640 | SmallString<128> current_dir; |
| 641 | if (error_code ec = current_path(current_dir)) return ec; |
| 642 | |
| 643 | // Relative path. Prepend the current directory. |
| 644 | if (!rootName && !rootDirectory) { |
| 645 | // Append path to the current directory. |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 646 | path::append(current_dir, p); |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 647 | // Set path to the result. |
| 648 | path.swap(current_dir); |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 649 | return error_code::success(); |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | if (!rootName && rootDirectory) { |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 653 | StringRef cdrn = path::root_name(current_dir); |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 654 | SmallString<128> curDirRootName(cdrn.begin(), cdrn.end()); |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 655 | path::append(curDirRootName, p); |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 656 | // Set path to the result. |
| 657 | path.swap(curDirRootName); |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 658 | return error_code::success(); |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | if (rootName && !rootDirectory) { |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 662 | StringRef pRootName = path::root_name(p); |
| 663 | StringRef bRootDirectory = path::root_directory(current_dir); |
| 664 | StringRef bRelativePath = path::relative_path(current_dir); |
| 665 | StringRef pRelativePath = path::relative_path(p); |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 666 | |
| 667 | SmallString<128> res; |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 668 | path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath); |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 669 | path.swap(res); |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 670 | return error_code::success(); |
Michael J. Spencer | ee271d8 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | llvm_unreachable("All rootName and rootDirectory combinations should have " |
| 674 | "occurred above!"); |
| 675 | } |
| 676 | |
Michael J. Spencer | b83769f | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 677 | error_code create_directories(const Twine &path, bool &existed) { |
| 678 | SmallString<128> path_storage; |
| 679 | StringRef p = path.toStringRef(path_storage); |
| 680 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 681 | StringRef parent = path::parent_path(p); |
Michael J. Spencer | 55f43d6 | 2012-03-21 23:09:14 +0000 | [diff] [blame] | 682 | if (!parent.empty()) { |
| 683 | bool parent_exists; |
| 684 | if (error_code ec = fs::exists(parent, parent_exists)) return ec; |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 685 | |
Michael J. Spencer | 55f43d6 | 2012-03-21 23:09:14 +0000 | [diff] [blame] | 686 | if (!parent_exists) |
| 687 | if (error_code ec = create_directories(parent, existed)) return ec; |
| 688 | } |
Michael J. Spencer | b83769f | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 689 | |
| 690 | return create_directory(p, existed); |
| 691 | } |
| 692 | |
Michael J. Spencer | 61187dd | 2010-12-09 17:37:02 +0000 | [diff] [blame] | 693 | bool exists(file_status status) { |
| 694 | return status_known(status) && status.type() != file_type::file_not_found; |
| 695 | } |
| 696 | |
| 697 | bool status_known(file_status s) { |
| 698 | return s.type() != file_type::status_error; |
| 699 | } |
| 700 | |
| 701 | bool is_directory(file_status status) { |
| 702 | return status.type() == file_type::directory_file; |
| 703 | } |
| 704 | |
Michael J. Spencer | 218dc3e | 2011-01-11 01:21:55 +0000 | [diff] [blame] | 705 | error_code is_directory(const Twine &path, bool &result) { |
| 706 | file_status st; |
| 707 | if (error_code ec = status(path, st)) |
| 708 | return ec; |
| 709 | result = is_directory(st); |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 710 | return error_code::success(); |
Michael J. Spencer | 218dc3e | 2011-01-11 01:21:55 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Michael J. Spencer | 61187dd | 2010-12-09 17:37:02 +0000 | [diff] [blame] | 713 | bool is_regular_file(file_status status) { |
| 714 | return status.type() == file_type::regular_file; |
| 715 | } |
| 716 | |
Michael J. Spencer | 218dc3e | 2011-01-11 01:21:55 +0000 | [diff] [blame] | 717 | error_code is_regular_file(const Twine &path, bool &result) { |
| 718 | file_status st; |
| 719 | if (error_code ec = status(path, st)) |
| 720 | return ec; |
| 721 | result = is_regular_file(st); |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 722 | return error_code::success(); |
Michael J. Spencer | 218dc3e | 2011-01-11 01:21:55 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Michael J. Spencer | 61187dd | 2010-12-09 17:37:02 +0000 | [diff] [blame] | 725 | bool is_symlink(file_status status) { |
| 726 | return status.type() == file_type::symlink_file; |
| 727 | } |
| 728 | |
Michael J. Spencer | 9df536c | 2011-01-12 23:55:06 +0000 | [diff] [blame] | 729 | error_code is_symlink(const Twine &path, bool &result) { |
| 730 | file_status st; |
| 731 | if (error_code ec = status(path, st)) |
| 732 | return ec; |
| 733 | result = is_symlink(st); |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 734 | return error_code::success(); |
Michael J. Spencer | 9df536c | 2011-01-12 23:55:06 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Michael J. Spencer | 61187dd | 2010-12-09 17:37:02 +0000 | [diff] [blame] | 737 | bool is_other(file_status status) { |
| 738 | return exists(status) && |
| 739 | !is_regular_file(status) && |
| 740 | !is_directory(status) && |
| 741 | !is_symlink(status); |
| 742 | } |
| 743 | |
Benjamin Kramer | 357b571 | 2011-09-14 01:14:36 +0000 | [diff] [blame] | 744 | void directory_entry::replace_filename(const Twine &filename, file_status st) { |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 745 | SmallString<128> path(Path.begin(), Path.end()); |
| 746 | path::remove_filename(path); |
| 747 | path::append(path, filename); |
| 748 | Path = path.str(); |
| 749 | Status = st; |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Michael J. Spencer | 628467e | 2010-12-28 01:49:01 +0000 | [diff] [blame] | 752 | error_code has_magic(const Twine &path, const Twine &magic, bool &result) { |
Michael J. Spencer | 628467e | 2010-12-28 01:49:01 +0000 | [diff] [blame] | 753 | SmallString<32> MagicStorage; |
Michael J. Spencer | b33594b | 2011-01-15 18:52:41 +0000 | [diff] [blame] | 754 | StringRef Magic = magic.toStringRef(MagicStorage); |
| 755 | SmallString<32> Buffer; |
Michael J. Spencer | 628467e | 2010-12-28 01:49:01 +0000 | [diff] [blame] | 756 | |
Michael J. Spencer | b33594b | 2011-01-15 18:52:41 +0000 | [diff] [blame] | 757 | if (error_code ec = get_magic(path, Magic.size(), Buffer)) { |
| 758 | if (ec == errc::value_too_large) { |
| 759 | // Magic.size() > file_size(Path). |
Michael J. Spencer | 628467e | 2010-12-28 01:49:01 +0000 | [diff] [blame] | 760 | result = false; |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 761 | return error_code::success(); |
Michael J. Spencer | 628467e | 2010-12-28 01:49:01 +0000 | [diff] [blame] | 762 | } |
Michael J. Spencer | b33594b | 2011-01-15 18:52:41 +0000 | [diff] [blame] | 763 | return ec; |
Michael J. Spencer | 628467e | 2010-12-28 01:49:01 +0000 | [diff] [blame] | 764 | } |
Michael J. Spencer | 628467e | 2010-12-28 01:49:01 +0000 | [diff] [blame] | 765 | |
Michael J. Spencer | b33594b | 2011-01-15 18:52:41 +0000 | [diff] [blame] | 766 | result = Magic == Buffer; |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 767 | return error_code::success(); |
Michael J. Spencer | 628467e | 2010-12-28 01:49:01 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Michael J. Spencer | 5b08230 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 770 | /// @brief Identify the magic in magic. |
| 771 | file_magic identify_magic(StringRef magic) { |
Michael J. Spencer | b51c8e9 | 2012-06-19 05:29:57 +0000 | [diff] [blame] | 772 | if (magic.size() < 4) |
| 773 | return file_magic::unknown; |
Michael J. Spencer | 5b08230 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 774 | switch ((unsigned char)magic[0]) { |
| 775 | case 0xDE: // 0x0B17C0DE = BC wraper |
| 776 | if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 && |
| 777 | magic[3] == (char)0x0B) |
| 778 | return file_magic::bitcode; |
| 779 | break; |
| 780 | case 'B': |
| 781 | if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE) |
| 782 | return file_magic::bitcode; |
| 783 | break; |
| 784 | case '!': |
| 785 | if (magic.size() >= 8) |
| 786 | if (memcmp(magic.data(),"!<arch>\n",8) == 0) |
| 787 | return file_magic::archive; |
| 788 | break; |
| 789 | |
| 790 | case '\177': |
| 791 | if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') { |
| 792 | if (magic.size() >= 18 && magic[17] == 0) |
| 793 | switch (magic[16]) { |
| 794 | default: break; |
| 795 | case 1: return file_magic::elf_relocatable; |
| 796 | case 2: return file_magic::elf_executable; |
| 797 | case 3: return file_magic::elf_shared_object; |
| 798 | case 4: return file_magic::elf_core; |
| 799 | } |
| 800 | } |
| 801 | break; |
| 802 | |
| 803 | case 0xCA: |
| 804 | if (magic[1] == char(0xFE) && magic[2] == char(0xBA) && |
| 805 | magic[3] == char(0xBE)) { |
| 806 | // This is complicated by an overlap with Java class files. |
| 807 | // See the Mach-O section in /usr/share/file/magic for details. |
| 808 | if (magic.size() >= 8 && magic[7] < 43) |
| 809 | // FIXME: Universal Binary of any type. |
| 810 | return file_magic::macho_dynamically_linked_shared_lib; |
| 811 | } |
| 812 | break; |
| 813 | |
| 814 | // The two magic numbers for mach-o are: |
| 815 | // 0xfeedface - 32-bit mach-o |
| 816 | // 0xfeedfacf - 64-bit mach-o |
| 817 | case 0xFE: |
| 818 | case 0xCE: |
| 819 | case 0xCF: { |
| 820 | uint16_t type = 0; |
| 821 | if (magic[0] == char(0xFE) && magic[1] == char(0xED) && |
| 822 | magic[2] == char(0xFA) && |
| 823 | (magic[3] == char(0xCE) || magic[3] == char(0xCF))) { |
| 824 | /* Native endian */ |
| 825 | if (magic.size() >= 16) type = magic[14] << 8 | magic[15]; |
| 826 | } else if ((magic[0] == char(0xCE) || magic[0] == char(0xCF)) && |
| 827 | magic[1] == char(0xFA) && magic[2] == char(0xED) && |
| 828 | magic[3] == char(0xFE)) { |
| 829 | /* Reverse endian */ |
| 830 | if (magic.size() >= 14) type = magic[13] << 8 | magic[12]; |
| 831 | } |
| 832 | switch (type) { |
| 833 | default: break; |
| 834 | case 1: return file_magic::macho_object; |
| 835 | case 2: return file_magic::macho_executable; |
| 836 | case 3: return file_magic::macho_fixed_virtual_memory_shared_lib; |
| 837 | case 4: return file_magic::macho_core; |
| 838 | case 5: return file_magic::macho_preload_executabl; |
| 839 | case 6: return file_magic::macho_dynamically_linked_shared_lib; |
| 840 | case 7: return file_magic::macho_dynamic_linker; |
| 841 | case 8: return file_magic::macho_bundle; |
| 842 | case 9: return file_magic::macho_dynamic_linker; |
| 843 | case 10: return file_magic::macho_dsym_companion; |
| 844 | } |
| 845 | break; |
| 846 | } |
| 847 | case 0xF0: // PowerPC Windows |
| 848 | case 0x83: // Alpha 32-bit |
| 849 | case 0x84: // Alpha 64-bit |
| 850 | case 0x66: // MPS R4000 Windows |
| 851 | case 0x50: // mc68K |
| 852 | case 0x4c: // 80386 Windows |
| 853 | if (magic[1] == 0x01) |
| 854 | return file_magic::coff_object; |
| 855 | |
| 856 | case 0x90: // PA-RISC Windows |
| 857 | case 0x68: // mc68K Windows |
| 858 | if (magic[1] == 0x02) |
| 859 | return file_magic::coff_object; |
| 860 | break; |
| 861 | |
| 862 | case 0x4d: // Possible MS-DOS stub on Windows PE file |
| 863 | if (magic[1] == 0x5a) { |
| 864 | uint32_t off = |
| 865 | *reinterpret_cast<const support::ulittle32_t*>(magic.data() + 0x3c); |
| 866 | // PE/COFF file, either EXE or DLL. |
| 867 | if (off < magic.size() && memcmp(magic.data() + off, "PE\0\0",4) == 0) |
| 868 | return file_magic::pecoff_executable; |
| 869 | } |
| 870 | break; |
| 871 | |
| 872 | case 0x64: // x86-64 Windows. |
| 873 | if (magic[1] == char(0x86)) |
| 874 | return file_magic::coff_object; |
| 875 | break; |
| 876 | |
| 877 | default: |
| 878 | break; |
| 879 | } |
| 880 | return file_magic::unknown; |
| 881 | } |
| 882 | |
| 883 | error_code identify_magic(const Twine &path, file_magic &result) { |
Michael J. Spencer | 28f0ed5 | 2011-01-15 20:39:36 +0000 | [diff] [blame] | 884 | SmallString<32> Magic; |
| 885 | error_code ec = get_magic(path, Magic.capacity(), Magic); |
| 886 | if (ec && ec != errc::value_too_large) |
| 887 | return ec; |
| 888 | |
Michael J. Spencer | 5b08230 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 889 | result = identify_magic(Magic); |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 890 | return error_code::success(); |
Michael J. Spencer | 28f0ed5 | 2011-01-15 20:39:36 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Michael J. Spencer | 7fd682a | 2011-01-05 16:39:38 +0000 | [diff] [blame] | 893 | namespace { |
| 894 | error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) { |
| 895 | if (ft == file_type::directory_file) { |
| 896 | // This code would be a lot better with exceptions ;/. |
| 897 | error_code ec; |
Michael J. Spencer | 1dd2ee7 | 2011-12-12 06:03:33 +0000 | [diff] [blame] | 898 | directory_iterator i(path, ec); |
| 899 | if (ec) return ec; |
| 900 | for (directory_iterator e; i != e; i.increment(ec)) { |
Michael J. Spencer | 7fd682a | 2011-01-05 16:39:38 +0000 | [diff] [blame] | 901 | if (ec) return ec; |
| 902 | file_status st; |
| 903 | if (error_code ec = i->status(st)) return ec; |
| 904 | if (error_code ec = remove_all_r(i->path(), st.type(), count)) return ec; |
| 905 | } |
| 906 | bool obviously_this_exists; |
| 907 | if (error_code ec = remove(path, obviously_this_exists)) return ec; |
| 908 | assert(obviously_this_exists); |
| 909 | ++count; // Include the directory itself in the items removed. |
| 910 | } else { |
| 911 | bool obviously_this_exists; |
| 912 | if (error_code ec = remove(path, obviously_this_exists)) return ec; |
| 913 | assert(obviously_this_exists); |
| 914 | ++count; |
| 915 | } |
| 916 | |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 917 | return error_code::success(); |
Michael J. Spencer | 7fd682a | 2011-01-05 16:39:38 +0000 | [diff] [blame] | 918 | } |
Zhanyong Wan | 63cc3a8 | 2011-02-11 21:24:40 +0000 | [diff] [blame] | 919 | } // end unnamed namespace |
Michael J. Spencer | 7fd682a | 2011-01-05 16:39:38 +0000 | [diff] [blame] | 920 | |
| 921 | error_code remove_all(const Twine &path, uint32_t &num_removed) { |
| 922 | SmallString<128> path_storage; |
| 923 | StringRef p = path.toStringRef(path_storage); |
| 924 | |
| 925 | file_status fs; |
| 926 | if (error_code ec = status(path, fs)) |
| 927 | return ec; |
| 928 | num_removed = 0; |
| 929 | return remove_all_r(p, fs.type(), num_removed); |
| 930 | } |
| 931 | |
Michael J. Spencer | 5bcdc7f | 2011-01-05 16:39:13 +0000 | [diff] [blame] | 932 | error_code directory_entry::status(file_status &result) const { |
| 933 | return fs::status(Path, result); |
| 934 | } |
| 935 | |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 936 | } // end namespace fs |
| 937 | } // end namespace sys |
| 938 | } // end namespace llvm |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 939 | |
| 940 | // Include the truly platform-specific parts. |
| 941 | #if defined(LLVM_ON_UNIX) |
| 942 | #include "Unix/PathV2.inc" |
| 943 | #endif |
| 944 | #if defined(LLVM_ON_WIN32) |
| 945 | #include "Windows/PathV2.inc" |
| 946 | #endif |