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