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