Rafael Espindola | f1fc382 | 2013-06-26 19:33:03 +0000 | [diff] [blame] | 1 | //===-- Path.cpp - Implement OS Path Concept ------------------------------===// |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 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 | // |
Rafael Espindola | f1fc382 | 2013-06-26 19:33:03 +0000 | [diff] [blame] | 10 | // This file implements the operating system Path API. |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Rui Ueyama | 5c69ff5 | 2014-09-11 22:34:32 +0000 | [diff] [blame] | 14 | #include "llvm/Support/COFF.h" |
Kevin Enderby | 87c85b7 | 2016-01-26 23:43:37 +0000 | [diff] [blame] | 15 | #include "llvm/Support/MachO.h" |
Rui Ueyama | 5c69ff5 | 2014-09-11 22:34:32 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Endian.h" |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Errc.h" |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Path.h" |
Aaron Ballman | 07e7618 | 2014-02-11 03:40:14 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Process.h" |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 22 | #include <cctype> |
Michael J. Spencer | 848f46b | 2010-12-28 01:49:01 +0000 | [diff] [blame] | 23 | #include <cstring> |
Rafael Espindola | 4526b1d | 2013-06-18 17:01:00 +0000 | [diff] [blame] | 24 | |
| 25 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
Douglas Gregor | a86ddf0 | 2013-03-21 21:46:10 +0000 | [diff] [blame] | 26 | #include <unistd.h> |
Rafael Espindola | 4526b1d | 2013-06-18 17:01:00 +0000 | [diff] [blame] | 27 | #else |
| 28 | #include <io.h> |
Douglas Gregor | a86ddf0 | 2013-03-21 21:46:10 +0000 | [diff] [blame] | 29 | #endif |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 30 | |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 32 | using namespace llvm::support::endian; |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 33 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | using llvm::StringRef; |
Zhanyong Wan | 606bb1a | 2011-02-11 21:24:40 +0000 | [diff] [blame] | 36 | using llvm::sys::path::is_separator; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 37 | |
| 38 | #ifdef LLVM_ON_WIN32 |
Benjamin Kramer | 5e4f0a4 | 2012-02-08 13:13:47 +0000 | [diff] [blame] | 39 | const char *separators = "\\/"; |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 40 | const char preferred_separator = '\\'; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 41 | #else |
Benjamin Kramer | 5e4f0a4 | 2012-02-08 13:13:47 +0000 | [diff] [blame] | 42 | const char separators = '/'; |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 43 | const char preferred_separator = '/'; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
Benjamin Kramer | cb520cd | 2010-12-17 18:59:09 +0000 | [diff] [blame] | 46 | StringRef find_first_component(StringRef path) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 47 | // Look for this first component in the following order. |
| 48 | // * empty (in this case we return an empty string) |
| 49 | // * either C: or {//,\\}net. |
| 50 | // * {/,\} |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 51 | // * {file,directory}name |
| 52 | |
| 53 | if (path.empty()) |
| 54 | return path; |
| 55 | |
Michael J. Spencer | bfea3c7 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 56 | #ifdef LLVM_ON_WIN32 |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 57 | // C: |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 58 | if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) && |
| 59 | path[1] == ':') |
Benjamin Kramer | ffa42ce | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 60 | return path.substr(0, 2); |
Michael J. Spencer | bfea3c7 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 61 | #endif |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 62 | |
| 63 | // //net |
| 64 | if ((path.size() > 2) && |
Michael J. Spencer | bfea3c7 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 65 | is_separator(path[0]) && |
| 66 | path[0] == path[1] && |
| 67 | !is_separator(path[2])) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 68 | // Find the next directory separator. |
Michael J. Spencer | bfea3c7 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 69 | size_t end = path.find_first_of(separators, 2); |
Benjamin Kramer | ffa42ce | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 70 | return path.substr(0, end); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // {/,\} |
Michael J. Spencer | bfea3c7 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 74 | if (is_separator(path[0])) |
Benjamin Kramer | ffa42ce | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 75 | return path.substr(0, 1); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 76 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 77 | // * {file,directory}name |
Tareq A. Siraj | 73537ea | 2013-08-12 17:10:49 +0000 | [diff] [blame] | 78 | size_t end = path.find_first_of(separators); |
Benjamin Kramer | ffa42ce | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 79 | return path.substr(0, end); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 80 | } |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 81 | |
Benjamin Kramer | 292b44b | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 82 | size_t filename_pos(StringRef str) { |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 83 | if (str.size() == 2 && |
| 84 | is_separator(str[0]) && |
Michael J. Spencer | bfea3c7 | 2010-12-07 03:57:48 +0000 | [diff] [blame] | 85 | str[0] == str[1]) |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 86 | return 0; |
| 87 | |
| 88 | if (str.size() > 0 && is_separator(str[str.size() - 1])) |
| 89 | return str.size() - 1; |
| 90 | |
| 91 | size_t pos = str.find_last_of(separators, str.size() - 1); |
| 92 | |
| 93 | #ifdef LLVM_ON_WIN32 |
Zachary Turner | 36f807c | 2015-02-12 00:05:49 +0000 | [diff] [blame] | 94 | if (pos == StringRef::npos) |
| 95 | pos = str.find_last_of(':', str.size() - 2); |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 96 | #endif |
| 97 | |
| 98 | if (pos == StringRef::npos || |
| 99 | (pos == 1 && is_separator(str[0]))) |
| 100 | return 0; |
| 101 | |
| 102 | return pos + 1; |
| 103 | } |
| 104 | |
Benjamin Kramer | 292b44b | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 105 | size_t root_dir_start(StringRef str) { |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 106 | // case "c:/" |
| 107 | #ifdef LLVM_ON_WIN32 |
| 108 | if (str.size() > 2 && |
| 109 | str[1] == ':' && |
| 110 | is_separator(str[2])) |
| 111 | return 2; |
| 112 | #endif |
| 113 | |
| 114 | // case "//" |
| 115 | if (str.size() == 2 && |
| 116 | is_separator(str[0]) && |
| 117 | str[0] == str[1]) |
| 118 | return StringRef::npos; |
| 119 | |
| 120 | // case "//net" |
| 121 | if (str.size() > 3 && |
| 122 | is_separator(str[0]) && |
| 123 | str[0] == str[1] && |
| 124 | !is_separator(str[2])) { |
| 125 | return str.find_first_of(separators, 2); |
| 126 | } |
| 127 | |
| 128 | // case "/" |
| 129 | if (str.size() > 0 && is_separator(str[0])) |
| 130 | return 0; |
| 131 | |
| 132 | return StringRef::npos; |
| 133 | } |
| 134 | |
Benjamin Kramer | 292b44b | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 135 | size_t parent_path_end(StringRef path) { |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 136 | size_t end_pos = filename_pos(path); |
| 137 | |
| 138 | bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]); |
| 139 | |
| 140 | // Skip separators except for root dir. |
Benjamin Kramer | ffa42ce | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 141 | size_t root_dir_pos = root_dir_start(path.substr(0, end_pos)); |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 142 | |
| 143 | while(end_pos > 0 && |
| 144 | (end_pos - 1) != root_dir_pos && |
| 145 | is_separator(path[end_pos - 1])) |
| 146 | --end_pos; |
| 147 | |
| 148 | if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep) |
| 149 | return StringRef::npos; |
| 150 | |
| 151 | return end_pos; |
| 152 | } |
Zhanyong Wan | 606bb1a | 2011-02-11 21:24:40 +0000 | [diff] [blame] | 153 | } // end unnamed namespace |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 154 | |
Rafael Espindola | e79a872 | 2013-06-28 03:48:47 +0000 | [diff] [blame] | 155 | enum FSEntity { |
| 156 | FS_Dir, |
| 157 | FS_File, |
| 158 | FS_Name |
| 159 | }; |
| 160 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 161 | static std::error_code createUniqueEntity(const Twine &Model, int &ResultFD, |
| 162 | SmallVectorImpl<char> &ResultPath, |
| 163 | bool MakeAbsolute, unsigned Mode, |
| 164 | FSEntity Type) { |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 165 | SmallString<128> ModelStorage; |
| 166 | Model.toVector(ModelStorage); |
| 167 | |
| 168 | if (MakeAbsolute) { |
| 169 | // Make model absolute by prepending a temp directory if it's not already. |
| 170 | if (!sys::path::is_absolute(Twine(ModelStorage))) { |
| 171 | SmallString<128> TDir; |
Rafael Espindola | 016a6d5 | 2014-08-26 14:47:52 +0000 | [diff] [blame] | 172 | sys::path::system_temp_directory(true, TDir); |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 173 | sys::path::append(TDir, Twine(ModelStorage)); |
| 174 | ModelStorage.swap(TDir); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // From here on, DO NOT modify model. It may be needed if the randomly chosen |
| 179 | // path already exists. |
| 180 | ResultPath = ModelStorage; |
| 181 | // Null terminate. |
| 182 | ResultPath.push_back(0); |
| 183 | ResultPath.pop_back(); |
| 184 | |
| 185 | retry_random_path: |
| 186 | // Replace '%' with random chars. |
| 187 | for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) { |
| 188 | if (ModelStorage[i] == '%') |
| 189 | ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15]; |
| 190 | } |
| 191 | |
| 192 | // Try to open + create the file. |
| 193 | switch (Type) { |
| 194 | case FS_File: { |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 195 | if (std::error_code EC = |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 196 | sys::fs::openFileForWrite(Twine(ResultPath.begin()), ResultFD, |
| 197 | sys::fs::F_RW | sys::fs::F_Excl, Mode)) { |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 198 | if (EC == errc::file_exists) |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 199 | goto retry_random_path; |
| 200 | return EC; |
| 201 | } |
| 202 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 203 | return std::error_code(); |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | case FS_Name: { |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 207 | std::error_code EC = |
| 208 | sys::fs::access(ResultPath.begin(), sys::fs::AccessMode::Exist); |
| 209 | if (EC == errc::no_such_file_or_directory) |
| 210 | return std::error_code(); |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 211 | if (EC) |
| 212 | return EC; |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 213 | goto retry_random_path; |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | case FS_Dir: { |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 217 | if (std::error_code EC = |
| 218 | sys::fs::create_directory(ResultPath.begin(), false)) { |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 219 | if (EC == errc::file_exists) |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 220 | goto retry_random_path; |
| 221 | return EC; |
| 222 | } |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 223 | return std::error_code(); |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | llvm_unreachable("Invalid Type"); |
| 227 | } |
Rafael Espindola | e79a872 | 2013-06-28 03:48:47 +0000 | [diff] [blame] | 228 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 229 | namespace llvm { |
| 230 | namespace sys { |
| 231 | namespace path { |
| 232 | |
Benjamin Kramer | 292b44b | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 233 | const_iterator begin(StringRef path) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 234 | const_iterator i; |
| 235 | i.Path = path; |
| 236 | i.Component = find_first_component(path); |
| 237 | i.Position = 0; |
| 238 | return i; |
| 239 | } |
| 240 | |
Benjamin Kramer | 292b44b | 2010-12-17 18:19:06 +0000 | [diff] [blame] | 241 | const_iterator end(StringRef path) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 242 | const_iterator i; |
| 243 | i.Path = path; |
| 244 | i.Position = path.size(); |
| 245 | return i; |
| 246 | } |
| 247 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 248 | const_iterator &const_iterator::operator++() { |
| 249 | assert(Position < Path.size() && "Tried to increment past end!"); |
| 250 | |
| 251 | // Increment Position to past the current component |
| 252 | Position += Component.size(); |
| 253 | |
| 254 | // Check for end. |
| 255 | if (Position == Path.size()) { |
| 256 | Component = StringRef(); |
| 257 | return *this; |
| 258 | } |
| 259 | |
| 260 | // Both POSIX and Windows treat paths that begin with exactly two separators |
| 261 | // specially. |
| 262 | bool was_net = Component.size() > 2 && |
| 263 | is_separator(Component[0]) && |
| 264 | Component[1] == Component[0] && |
| 265 | !is_separator(Component[2]); |
| 266 | |
| 267 | // Handle separators. |
| 268 | if (is_separator(Path[Position])) { |
| 269 | // Root dir. |
| 270 | if (was_net |
| 271 | #ifdef LLVM_ON_WIN32 |
| 272 | // c:/ |
| 273 | || Component.endswith(":") |
| 274 | #endif |
| 275 | ) { |
Benjamin Kramer | ffa42ce | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 276 | Component = Path.substr(Position, 1); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 277 | return *this; |
| 278 | } |
| 279 | |
| 280 | // Skip extra separators. |
| 281 | while (Position != Path.size() && |
| 282 | is_separator(Path[Position])) { |
| 283 | ++Position; |
| 284 | } |
| 285 | |
| 286 | // Treat trailing '/' as a '.'. |
| 287 | if (Position == Path.size()) { |
| 288 | --Position; |
| 289 | Component = "."; |
| 290 | return *this; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // Find next component. |
| 295 | size_t end_pos = Path.find_first_of(separators, Position); |
Benjamin Kramer | ffa42ce | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 296 | Component = Path.slice(Position, end_pos); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 297 | |
| 298 | return *this; |
| 299 | } |
| 300 | |
Justin Bogner | 487e764 | 2014-08-04 17:36:41 +0000 | [diff] [blame] | 301 | bool const_iterator::operator==(const const_iterator &RHS) const { |
| 302 | return Path.begin() == RHS.Path.begin() && Position == RHS.Position; |
| 303 | } |
| 304 | |
| 305 | ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const { |
| 306 | return Position - RHS.Position; |
| 307 | } |
| 308 | |
| 309 | reverse_iterator rbegin(StringRef Path) { |
| 310 | reverse_iterator I; |
| 311 | I.Path = Path; |
| 312 | I.Position = Path.size(); |
| 313 | return ++I; |
| 314 | } |
| 315 | |
| 316 | reverse_iterator rend(StringRef Path) { |
| 317 | reverse_iterator I; |
| 318 | I.Path = Path; |
| 319 | I.Component = Path.substr(0, 0); |
| 320 | I.Position = 0; |
| 321 | return I; |
| 322 | } |
| 323 | |
| 324 | reverse_iterator &reverse_iterator::operator++() { |
Ben Langmuir | 8d11639 | 2014-03-05 19:56:30 +0000 | [diff] [blame] | 325 | // If we're at the end and the previous char was a '/', return '.' unless |
| 326 | // we are the root path. |
| 327 | size_t root_dir_pos = root_dir_start(Path); |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 328 | if (Position == Path.size() && |
Ben Langmuir | 8d11639 | 2014-03-05 19:56:30 +0000 | [diff] [blame] | 329 | Path.size() > root_dir_pos + 1 && |
| 330 | is_separator(Path[Position - 1])) { |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 331 | --Position; |
| 332 | Component = "."; |
| 333 | return *this; |
| 334 | } |
| 335 | |
| 336 | // Skip separators unless it's the root directory. |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 337 | size_t end_pos = Position; |
| 338 | |
| 339 | while(end_pos > 0 && |
| 340 | (end_pos - 1) != root_dir_pos && |
| 341 | is_separator(Path[end_pos - 1])) |
| 342 | --end_pos; |
| 343 | |
| 344 | // Find next separator. |
Benjamin Kramer | ffa42ce | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 345 | size_t start_pos = filename_pos(Path.substr(0, end_pos)); |
| 346 | Component = Path.slice(start_pos, end_pos); |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 347 | Position = start_pos; |
| 348 | return *this; |
| 349 | } |
| 350 | |
Justin Bogner | 487e764 | 2014-08-04 17:36:41 +0000 | [diff] [blame] | 351 | bool reverse_iterator::operator==(const reverse_iterator &RHS) const { |
| 352 | return Path.begin() == RHS.Path.begin() && Component == RHS.Component && |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 353 | Position == RHS.Position; |
| 354 | } |
| 355 | |
Filipe Cabecinhas | 7894938 | 2016-04-29 16:48:07 +0000 | [diff] [blame] | 356 | ptrdiff_t reverse_iterator::operator-(const reverse_iterator &RHS) const { |
| 357 | return Position - RHS.Position; |
| 358 | } |
| 359 | |
Craig Topper | 6dc4a8bc | 2014-08-30 16:48:02 +0000 | [diff] [blame] | 360 | StringRef root_path(StringRef path) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 361 | const_iterator b = begin(path), |
| 362 | pos = b, |
| 363 | e = end(path); |
| 364 | if (b != e) { |
| 365 | bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0]; |
| 366 | bool has_drive = |
| 367 | #ifdef LLVM_ON_WIN32 |
| 368 | b->endswith(":"); |
| 369 | #else |
| 370 | false; |
| 371 | #endif |
| 372 | |
| 373 | if (has_net || has_drive) { |
| 374 | if ((++pos != e) && is_separator((*pos)[0])) { |
| 375 | // {C:/,//net/}, so get the first two components. |
Benjamin Kramer | ffa42ce | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 376 | return path.substr(0, b->size() + pos->size()); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 377 | } else { |
| 378 | // just {C:,//net}, return the first component. |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 379 | return *b; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
| 383 | // POSIX style root directory. |
| 384 | if (is_separator((*b)[0])) { |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 385 | return *b; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 386 | } |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 389 | return StringRef(); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Craig Topper | 6dc4a8bc | 2014-08-30 16:48:02 +0000 | [diff] [blame] | 392 | StringRef root_name(StringRef path) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 393 | const_iterator b = begin(path), |
| 394 | e = end(path); |
| 395 | if (b != e) { |
| 396 | bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0]; |
| 397 | bool has_drive = |
| 398 | #ifdef LLVM_ON_WIN32 |
| 399 | b->endswith(":"); |
| 400 | #else |
| 401 | false; |
| 402 | #endif |
| 403 | |
| 404 | if (has_net || has_drive) { |
| 405 | // just {C:,//net}, return the first component. |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 406 | return *b; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
| 410 | // No path or no name. |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 411 | return StringRef(); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Craig Topper | 6dc4a8bc | 2014-08-30 16:48:02 +0000 | [diff] [blame] | 414 | StringRef root_directory(StringRef path) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 415 | const_iterator b = begin(path), |
| 416 | pos = b, |
| 417 | e = end(path); |
| 418 | if (b != e) { |
| 419 | bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0]; |
| 420 | bool has_drive = |
| 421 | #ifdef LLVM_ON_WIN32 |
| 422 | b->endswith(":"); |
| 423 | #else |
| 424 | false; |
| 425 | #endif |
| 426 | |
| 427 | if ((has_net || has_drive) && |
| 428 | // {C:,//net}, skip to the next component. |
| 429 | (++pos != e) && is_separator((*pos)[0])) { |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 430 | return *pos; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | // POSIX style root directory. |
| 434 | if (!has_net && is_separator((*b)[0])) { |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 435 | return *b; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | // No path or no root. |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 440 | return StringRef(); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Craig Topper | 6dc4a8bc | 2014-08-30 16:48:02 +0000 | [diff] [blame] | 443 | StringRef relative_path(StringRef path) { |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 444 | StringRef root = root_path(path); |
Michael J. Spencer | e646239 | 2012-02-29 00:06:24 +0000 | [diff] [blame] | 445 | return path.substr(root.size()); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 448 | void append(SmallVectorImpl<char> &path, const Twine &a, |
| 449 | const Twine &b, |
| 450 | const Twine &c, |
| 451 | const Twine &d) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 452 | SmallString<32> a_storage; |
| 453 | SmallString<32> b_storage; |
| 454 | SmallString<32> c_storage; |
| 455 | SmallString<32> d_storage; |
| 456 | |
| 457 | SmallVector<StringRef, 4> components; |
| 458 | if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage)); |
| 459 | if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage)); |
| 460 | if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage)); |
| 461 | if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage)); |
| 462 | |
Pawel Bylica | 64d08ff | 2015-10-22 08:12:15 +0000 | [diff] [blame] | 463 | for (auto &component : components) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 464 | bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]); |
Pawel Bylica | 64d08ff | 2015-10-22 08:12:15 +0000 | [diff] [blame] | 465 | bool component_has_sep = !component.empty() && is_separator(component[0]); |
| 466 | bool is_root_name = has_root_name(component); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 467 | |
| 468 | if (path_has_sep) { |
| 469 | // Strip separators from beginning of component. |
Pawel Bylica | 64d08ff | 2015-10-22 08:12:15 +0000 | [diff] [blame] | 470 | size_t loc = component.find_first_not_of(separators); |
| 471 | StringRef c = component.substr(loc); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 472 | |
| 473 | // Append it. |
| 474 | path.append(c.begin(), c.end()); |
| 475 | continue; |
| 476 | } |
| 477 | |
Michael J. Spencer | 95e4ac1 | 2010-12-06 04:28:23 +0000 | [diff] [blame] | 478 | if (!component_has_sep && !(path.empty() || is_root_name)) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 479 | // Add a separator. |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 480 | path.push_back(preferred_separator); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Pawel Bylica | 64d08ff | 2015-10-22 08:12:15 +0000 | [diff] [blame] | 483 | path.append(component.begin(), component.end()); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 484 | } |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Argyrios Kyrtzidis | a61736f | 2011-02-15 17:51:19 +0000 | [diff] [blame] | 487 | void append(SmallVectorImpl<char> &path, |
| 488 | const_iterator begin, const_iterator end) { |
| 489 | for (; begin != end; ++begin) |
| 490 | path::append(path, *begin); |
| 491 | } |
| 492 | |
Craig Topper | 6dc4a8bc | 2014-08-30 16:48:02 +0000 | [diff] [blame] | 493 | StringRef parent_path(StringRef path) { |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 494 | size_t end_pos = parent_path_end(path); |
| 495 | if (end_pos == StringRef::npos) |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 496 | return StringRef(); |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 497 | else |
Benjamin Kramer | ffa42ce | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 498 | return path.substr(0, end_pos); |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 501 | void remove_filename(SmallVectorImpl<char> &path) { |
Michael J. Spencer | 9c59409 | 2010-12-01 00:52:28 +0000 | [diff] [blame] | 502 | size_t end_pos = parent_path_end(StringRef(path.begin(), path.size())); |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 503 | if (end_pos != StringRef::npos) |
| 504 | path.set_size(end_pos); |
Michael J. Spencer | 9c59409 | 2010-12-01 00:52:28 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 507 | void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) { |
Michael J. Spencer | fb3a95d | 2010-12-01 00:52:55 +0000 | [diff] [blame] | 508 | StringRef p(path.begin(), path.size()); |
| 509 | SmallString<32> ext_storage; |
| 510 | StringRef ext = extension.toStringRef(ext_storage); |
| 511 | |
| 512 | // Erase existing extension. |
| 513 | size_t pos = p.find_last_of('.'); |
| 514 | if (pos != StringRef::npos && pos >= filename_pos(p)) |
| 515 | path.set_size(pos); |
| 516 | |
| 517 | // Append '.' if needed. |
| 518 | if (ext.size() > 0 && ext[0] != '.') |
| 519 | path.push_back('.'); |
| 520 | |
| 521 | // Append extension. |
| 522 | path.append(ext.begin(), ext.end()); |
Michael J. Spencer | fb3a95d | 2010-12-01 00:52:55 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Teresa Johnson | bbd10b4 | 2016-05-17 14:45:30 +0000 | [diff] [blame] | 525 | void replace_path_prefix(SmallVectorImpl<char> &Path, |
| 526 | const StringRef &OldPrefix, |
| 527 | const StringRef &NewPrefix) { |
| 528 | if (OldPrefix.empty() && NewPrefix.empty()) |
| 529 | return; |
| 530 | |
| 531 | StringRef OrigPath(Path.begin(), Path.size()); |
| 532 | if (!OrigPath.startswith(OldPrefix)) |
| 533 | return; |
| 534 | |
| 535 | // If prefixes have the same size we can simply copy the new one over. |
| 536 | if (OldPrefix.size() == NewPrefix.size()) { |
| 537 | std::copy(NewPrefix.begin(), NewPrefix.end(), Path.begin()); |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | StringRef RelPath = OrigPath.substr(OldPrefix.size()); |
| 542 | SmallString<256> NewPath; |
| 543 | path::append(NewPath, NewPrefix); |
| 544 | path::append(NewPath, RelPath); |
| 545 | Path.swap(NewPath); |
| 546 | } |
| 547 | |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 548 | void native(const Twine &path, SmallVectorImpl<char> &result) { |
Benjamin Kramer | bd4ac9b | 2013-09-11 10:45:21 +0000 | [diff] [blame] | 549 | assert((!path.isSingleStringRef() || |
| 550 | path.getSingleStringRef().data() != result.data()) && |
| 551 | "path and result are not allowed to overlap!"); |
Michael J. Spencer | 8002500 | 2010-12-01 02:48:27 +0000 | [diff] [blame] | 552 | // Clear result. |
Michael J. Spencer | 98c7a11 | 2010-12-07 01:23:19 +0000 | [diff] [blame] | 553 | result.clear(); |
Michael J. Spencer | 8002500 | 2010-12-01 02:48:27 +0000 | [diff] [blame] | 554 | path.toVector(result); |
Benjamin Kramer | bd4ac9b | 2013-09-11 10:45:21 +0000 | [diff] [blame] | 555 | native(result); |
| 556 | } |
| 557 | |
Rafael Espindola | d649b9d | 2014-08-08 21:29:34 +0000 | [diff] [blame] | 558 | void native(SmallVectorImpl<char> &Path) { |
Serge Pavlov | 9c761a3 | 2017-03-01 09:38:15 +0000 | [diff] [blame] | 559 | if (Path.empty()) |
| 560 | return; |
Benjamin Kramer | bd4ac9b | 2013-09-11 10:45:21 +0000 | [diff] [blame] | 561 | #ifdef LLVM_ON_WIN32 |
Rafael Espindola | 674ef1d | 2014-08-08 22:09:31 +0000 | [diff] [blame] | 562 | std::replace(Path.begin(), Path.end(), '/', '\\'); |
Serge Pavlov | 9c761a3 | 2017-03-01 09:38:15 +0000 | [diff] [blame] | 563 | if (Path[0] == '~' && (Path.size() == 1 || is_separator(Path[1]))) { |
| 564 | SmallString<128> PathHome; |
| 565 | home_directory(PathHome); |
| 566 | PathHome.append(Path.begin() + 1, Path.end()); |
| 567 | Path = PathHome; |
| 568 | } |
Rafael Espindola | d649b9d | 2014-08-08 21:29:34 +0000 | [diff] [blame] | 569 | #else |
| 570 | for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) { |
| 571 | if (*PI == '\\') { |
| 572 | auto PN = PI + 1; |
| 573 | if (PN < PE && *PN == '\\') |
| 574 | ++PI; // increment once, the for loop will move over the escaped slash |
| 575 | else |
| 576 | *PI = '/'; |
| 577 | } |
| 578 | } |
Michael J. Spencer | 8002500 | 2010-12-01 02:48:27 +0000 | [diff] [blame] | 579 | #endif |
Michael J. Spencer | 8002500 | 2010-12-01 02:48:27 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Rui Ueyama | 3e64903 | 2017-01-09 01:47:15 +0000 | [diff] [blame] | 582 | std::string convert_to_slash(StringRef path) { |
| 583 | #ifdef LLVM_ON_WIN32 |
| 584 | std::string s = path.str(); |
| 585 | std::replace(s.begin(), s.end(), '\\', '/'); |
| 586 | return s; |
| 587 | #else |
| 588 | return path; |
| 589 | #endif |
| 590 | } |
| 591 | |
Craig Topper | 6dc4a8bc | 2014-08-30 16:48:02 +0000 | [diff] [blame] | 592 | StringRef filename(StringRef path) { |
Justin Bogner | 487e764 | 2014-08-04 17:36:41 +0000 | [diff] [blame] | 593 | return *rbegin(path); |
Michael J. Spencer | 1426920 | 2010-12-01 03:18:17 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Craig Topper | 6dc4a8bc | 2014-08-30 16:48:02 +0000 | [diff] [blame] | 596 | StringRef stem(StringRef path) { |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 597 | StringRef fname = filename(path); |
Michael J. Spencer | 956955e | 2010-12-01 03:18:33 +0000 | [diff] [blame] | 598 | size_t pos = fname.find_last_of('.'); |
| 599 | if (pos == StringRef::npos) |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 600 | return fname; |
Michael J. Spencer | 956955e | 2010-12-01 03:18:33 +0000 | [diff] [blame] | 601 | else |
| 602 | if ((fname.size() == 1 && fname == ".") || |
| 603 | (fname.size() == 2 && fname == "..")) |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 604 | return fname; |
Michael J. Spencer | 956955e | 2010-12-01 03:18:33 +0000 | [diff] [blame] | 605 | else |
Benjamin Kramer | ffa42ce | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 606 | return fname.substr(0, pos); |
Michael J. Spencer | 956955e | 2010-12-01 03:18:33 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Craig Topper | 6dc4a8bc | 2014-08-30 16:48:02 +0000 | [diff] [blame] | 609 | StringRef extension(StringRef path) { |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 610 | StringRef fname = filename(path); |
Michael J. Spencer | 87106c5 | 2010-12-01 03:37:41 +0000 | [diff] [blame] | 611 | size_t pos = fname.find_last_of('.'); |
| 612 | if (pos == StringRef::npos) |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 613 | return StringRef(); |
Michael J. Spencer | 87106c5 | 2010-12-01 03:37:41 +0000 | [diff] [blame] | 614 | else |
| 615 | if ((fname.size() == 1 && fname == ".") || |
| 616 | (fname.size() == 2 && fname == "..")) |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 617 | return StringRef(); |
Michael J. Spencer | 87106c5 | 2010-12-01 03:37:41 +0000 | [diff] [blame] | 618 | else |
Benjamin Kramer | ffa42ce | 2010-12-17 20:27:37 +0000 | [diff] [blame] | 619 | return fname.substr(pos); |
Michael J. Spencer | 87106c5 | 2010-12-01 03:37:41 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Zhanyong Wan | 606bb1a | 2011-02-11 21:24:40 +0000 | [diff] [blame] | 622 | bool is_separator(char value) { |
| 623 | switch(value) { |
| 624 | #ifdef LLVM_ON_WIN32 |
| 625 | case '\\': // fall through |
| 626 | #endif |
| 627 | case '/': return true; |
| 628 | default: return false; |
| 629 | } |
| 630 | } |
| 631 | |
Yaron Keren | 1521720 | 2014-05-16 13:16:30 +0000 | [diff] [blame] | 632 | static const char preferred_separator_string[] = { preferred_separator, '\0' }; |
| 633 | |
Craig Topper | 6dc4a8bc | 2014-08-30 16:48:02 +0000 | [diff] [blame] | 634 | StringRef get_separator() { |
Yaron Keren | 1521720 | 2014-05-16 13:16:30 +0000 | [diff] [blame] | 635 | return preferred_separator_string; |
| 636 | } |
| 637 | |
Michael J. Spencer | a68282c | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 638 | bool has_root_name(const Twine &path) { |
Michael J. Spencer | 112a769 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 639 | SmallString<128> path_storage; |
| 640 | StringRef p = path.toStringRef(path_storage); |
| 641 | |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 642 | return !root_name(p).empty(); |
Michael J. Spencer | 112a769 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Michael J. Spencer | a68282c | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 645 | bool has_root_directory(const Twine &path) { |
Michael J. Spencer | 112a769 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 646 | SmallString<128> path_storage; |
| 647 | StringRef p = path.toStringRef(path_storage); |
| 648 | |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 649 | return !root_directory(p).empty(); |
Michael J. Spencer | 112a769 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Michael J. Spencer | a68282c | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 652 | bool has_root_path(const Twine &path) { |
Michael J. Spencer | 112a769 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 653 | SmallString<128> path_storage; |
| 654 | StringRef p = path.toStringRef(path_storage); |
| 655 | |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 656 | return !root_path(p).empty(); |
Michael J. Spencer | 112a769 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Michael J. Spencer | 6d4b7e7 | 2010-12-20 13:30:28 +0000 | [diff] [blame] | 659 | bool has_relative_path(const Twine &path) { |
| 660 | SmallString<128> path_storage; |
| 661 | StringRef p = path.toStringRef(path_storage); |
| 662 | |
| 663 | return !relative_path(p).empty(); |
| 664 | } |
| 665 | |
Michael J. Spencer | a68282c | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 666 | bool has_filename(const Twine &path) { |
Michael J. Spencer | 112a769 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 667 | SmallString<128> path_storage; |
| 668 | StringRef p = path.toStringRef(path_storage); |
| 669 | |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 670 | return !filename(p).empty(); |
Michael J. Spencer | 112a769 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Michael J. Spencer | a68282c | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 673 | bool has_parent_path(const Twine &path) { |
Michael J. Spencer | 112a769 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 674 | SmallString<128> path_storage; |
| 675 | StringRef p = path.toStringRef(path_storage); |
| 676 | |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 677 | return !parent_path(p).empty(); |
Michael J. Spencer | 112a769 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Michael J. Spencer | a68282c | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 680 | bool has_stem(const Twine &path) { |
Michael J. Spencer | 112a769 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 681 | SmallString<128> path_storage; |
| 682 | StringRef p = path.toStringRef(path_storage); |
| 683 | |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 684 | return !stem(p).empty(); |
Michael J. Spencer | 112a769 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Michael J. Spencer | a68282c | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 687 | bool has_extension(const Twine &path) { |
Michael J. Spencer | 112a769 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 688 | SmallString<128> path_storage; |
| 689 | StringRef p = path.toStringRef(path_storage); |
| 690 | |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 691 | return !extension(p).empty(); |
Michael J. Spencer | 112a769 | 2010-12-01 06:03:50 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Michael J. Spencer | a68282c | 2010-12-07 18:12:07 +0000 | [diff] [blame] | 694 | bool is_absolute(const Twine &path) { |
Michael J. Spencer | a72df5f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 695 | SmallString<128> path_storage; |
| 696 | StringRef p = path.toStringRef(path_storage); |
| 697 | |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 698 | bool rootDir = has_root_directory(p), |
Michael J. Spencer | a72df5f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 699 | #ifdef LLVM_ON_WIN32 |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 700 | rootName = has_root_name(p); |
Michael J. Spencer | a72df5f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 701 | #else |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 702 | rootName = true; |
Michael J. Spencer | a72df5f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 703 | #endif |
| 704 | |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 705 | return rootDir && rootName; |
Michael J. Spencer | a72df5f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Douglas Katzman | a26be4a | 2015-09-02 21:02:10 +0000 | [diff] [blame] | 708 | bool is_relative(const Twine &path) { return !is_absolute(path); } |
| 709 | |
| 710 | StringRef remove_leading_dotslash(StringRef Path) { |
| 711 | // Remove leading "./" (or ".//" or "././" etc.) |
| 712 | while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1])) { |
| 713 | Path = Path.substr(2); |
| 714 | while (Path.size() > 0 && is_separator(Path[0])) |
| 715 | Path = Path.substr(1); |
| 716 | } |
| 717 | return Path; |
Michael J. Spencer | a72df5f | 2010-12-01 06:21:53 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Mike Aizatsky | 662b4fd | 2015-11-09 18:56:31 +0000 | [diff] [blame] | 720 | static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot) { |
| 721 | SmallVector<StringRef, 16> components; |
| 722 | |
| 723 | // Skip the root path, then look for traversal in the components. |
| 724 | StringRef rel = path::relative_path(path); |
| 725 | for (StringRef C : llvm::make_range(path::begin(rel), path::end(rel))) { |
| 726 | if (C == ".") |
| 727 | continue; |
Benjamin Kramer | 937dd7a | 2016-10-17 13:28:21 +0000 | [diff] [blame] | 728 | // Leading ".." will remain in the path unless it's at the root. |
| 729 | if (remove_dot_dot && C == "..") { |
| 730 | if (!components.empty() && components.back() != "..") { |
| 731 | components.pop_back(); |
| 732 | continue; |
| 733 | } |
| 734 | if (path::is_absolute(path)) |
| 735 | continue; |
Mike Aizatsky | 662b4fd | 2015-11-09 18:56:31 +0000 | [diff] [blame] | 736 | } |
| 737 | components.push_back(C); |
| 738 | } |
| 739 | |
| 740 | SmallString<256> buffer = path::root_path(path); |
| 741 | for (StringRef C : components) |
| 742 | path::append(buffer, C); |
| 743 | return buffer; |
| 744 | } |
| 745 | |
| 746 | bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot) { |
| 747 | StringRef p(path.data(), path.size()); |
| 748 | |
| 749 | SmallString<256> result = remove_dots(p, remove_dot_dot); |
| 750 | if (result == path) |
| 751 | return false; |
| 752 | |
| 753 | path.swap(result); |
| 754 | return true; |
| 755 | } |
| 756 | |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 757 | } // end namespace path |
| 758 | |
| 759 | namespace fs { |
| 760 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 761 | std::error_code getUniqueID(const Twine Path, UniqueID &Result) { |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 762 | file_status Status; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 763 | std::error_code EC = status(Path, Status); |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 764 | if (EC) |
| 765 | return EC; |
| 766 | Result = Status.getUniqueID(); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 767 | return std::error_code(); |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 770 | std::error_code createUniqueFile(const Twine &Model, int &ResultFd, |
| 771 | SmallVectorImpl<char> &ResultPath, |
| 772 | unsigned Mode) { |
Rafael Espindola | c9d2e5b | 2013-07-05 21:01:08 +0000 | [diff] [blame] | 773 | return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File); |
| 774 | } |
| 775 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 776 | std::error_code createUniqueFile(const Twine &Model, |
| 777 | SmallVectorImpl<char> &ResultPath) { |
Rafael Espindola | c9d2e5b | 2013-07-05 21:01:08 +0000 | [diff] [blame] | 778 | int Dummy; |
| 779 | return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name); |
| 780 | } |
| 781 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 782 | static std::error_code |
| 783 | createTemporaryFile(const Twine &Model, int &ResultFD, |
| 784 | llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) { |
Rafael Espindola | 325fa0f | 2013-07-05 19:56:49 +0000 | [diff] [blame] | 785 | SmallString<128> Storage; |
| 786 | StringRef P = Model.toNullTerminatedStringRef(Storage); |
| 787 | assert(P.find_first_of(separators) == StringRef::npos && |
| 788 | "Model must be a simple filename."); |
| 789 | // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage. |
| 790 | return createUniqueEntity(P.begin(), ResultFD, ResultPath, |
| 791 | true, owner_read | owner_write, Type); |
| 792 | } |
| 793 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 794 | static std::error_code |
Rafael Espindola | 325fa0f | 2013-07-05 19:56:49 +0000 | [diff] [blame] | 795 | createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 796 | llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) { |
Rafael Espindola | d3c8904 | 2013-07-25 15:00:17 +0000 | [diff] [blame] | 797 | const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%."; |
| 798 | return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath, |
Rafael Espindola | 325fa0f | 2013-07-05 19:56:49 +0000 | [diff] [blame] | 799 | Type); |
| 800 | } |
| 801 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 802 | std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, |
| 803 | int &ResultFD, |
| 804 | SmallVectorImpl<char> &ResultPath) { |
Rafael Espindola | 325fa0f | 2013-07-05 19:56:49 +0000 | [diff] [blame] | 805 | return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File); |
| 806 | } |
| 807 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 808 | std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, |
| 809 | SmallVectorImpl<char> &ResultPath) { |
Rafael Espindola | 325fa0f | 2013-07-05 19:56:49 +0000 | [diff] [blame] | 810 | int Dummy; |
| 811 | return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name); |
| 812 | } |
| 813 | |
| 814 | |
Rafael Espindola | e79a872 | 2013-06-28 03:48:47 +0000 | [diff] [blame] | 815 | // This is a mkdtemp with a different pattern. We use createUniqueEntity mostly |
Rafael Espindola | 31a2443 | 2013-06-28 10:55:41 +0000 | [diff] [blame] | 816 | // for consistency. We should try using mkdtemp. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 817 | std::error_code createUniqueDirectory(const Twine &Prefix, |
| 818 | SmallVectorImpl<char> &ResultPath) { |
Rafael Espindola | e79a872 | 2013-06-28 03:48:47 +0000 | [diff] [blame] | 819 | int Dummy; |
| 820 | return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath, |
| 821 | true, 0, FS_Dir); |
Rafael Espindola | 7ffacc4 | 2013-06-27 03:45:31 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Benjamin Kramer | ae1d599 | 2015-10-05 13:02:43 +0000 | [diff] [blame] | 824 | static std::error_code make_absolute(const Twine ¤t_directory, |
| 825 | SmallVectorImpl<char> &path, |
| 826 | bool use_current_directory) { |
Michael J. Spencer | 92903a3 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 827 | StringRef p(path.data(), path.size()); |
| 828 | |
Daniel Dunbar | b23158c | 2012-02-29 00:20:37 +0000 | [diff] [blame] | 829 | bool rootDirectory = path::has_root_directory(p), |
| 830 | #ifdef LLVM_ON_WIN32 |
Daniel Dunbar | 39ea458 | 2012-02-29 00:46:46 +0000 | [diff] [blame] | 831 | rootName = path::has_root_name(p); |
Daniel Dunbar | b23158c | 2012-02-29 00:20:37 +0000 | [diff] [blame] | 832 | #else |
| 833 | rootName = true; |
| 834 | #endif |
Michael J. Spencer | 92903a3 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 835 | |
| 836 | // Already absolute. |
| 837 | if (rootName && rootDirectory) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 838 | return std::error_code(); |
Michael J. Spencer | 92903a3 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 839 | |
| 840 | // All of the following conditions will need the current directory. |
| 841 | SmallString<128> current_dir; |
Benjamin Kramer | ae1d599 | 2015-10-05 13:02:43 +0000 | [diff] [blame] | 842 | if (use_current_directory) |
| 843 | current_directory.toVector(current_dir); |
| 844 | else if (std::error_code ec = current_path(current_dir)) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 845 | return ec; |
Michael J. Spencer | 92903a3 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 846 | |
| 847 | // Relative path. Prepend the current directory. |
| 848 | if (!rootName && !rootDirectory) { |
| 849 | // Append path to the current directory. |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 850 | path::append(current_dir, p); |
Michael J. Spencer | 92903a3 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 851 | // Set path to the result. |
| 852 | path.swap(current_dir); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 853 | return std::error_code(); |
Michael J. Spencer | 92903a3 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | if (!rootName && rootDirectory) { |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 857 | StringRef cdrn = path::root_name(current_dir); |
Michael J. Spencer | 92903a3 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 858 | SmallString<128> curDirRootName(cdrn.begin(), cdrn.end()); |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 859 | path::append(curDirRootName, p); |
Michael J. Spencer | 92903a3 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 860 | // Set path to the result. |
| 861 | path.swap(curDirRootName); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 862 | return std::error_code(); |
Michael J. Spencer | 92903a3 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | if (rootName && !rootDirectory) { |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 866 | StringRef pRootName = path::root_name(p); |
| 867 | StringRef bRootDirectory = path::root_directory(current_dir); |
| 868 | StringRef bRelativePath = path::relative_path(current_dir); |
| 869 | StringRef pRelativePath = path::relative_path(p); |
Michael J. Spencer | 92903a3 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 870 | |
| 871 | SmallString<128> res; |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 872 | path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath); |
Michael J. Spencer | 92903a3 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 873 | path.swap(res); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 874 | return std::error_code(); |
Michael J. Spencer | 92903a3 | 2010-12-07 03:57:17 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | llvm_unreachable("All rootName and rootDirectory combinations should have " |
| 878 | "occurred above!"); |
| 879 | } |
| 880 | |
Benjamin Kramer | ae1d599 | 2015-10-05 13:02:43 +0000 | [diff] [blame] | 881 | std::error_code make_absolute(const Twine ¤t_directory, |
| 882 | SmallVectorImpl<char> &path) { |
| 883 | return make_absolute(current_directory, path, true); |
| 884 | } |
| 885 | |
| 886 | std::error_code make_absolute(SmallVectorImpl<char> &path) { |
| 887 | return make_absolute(Twine(), path, false); |
| 888 | } |
| 889 | |
Frederic Riss | 6b9396c | 2015-08-06 21:04:55 +0000 | [diff] [blame] | 890 | std::error_code create_directories(const Twine &Path, bool IgnoreExisting, |
| 891 | perms Perms) { |
Rafael Espindola | b6f72b2 | 2014-02-13 16:58:19 +0000 | [diff] [blame] | 892 | SmallString<128> PathStorage; |
| 893 | StringRef P = Path.toStringRef(PathStorage); |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 894 | |
Rafael Espindola | b6f72b2 | 2014-02-13 16:58:19 +0000 | [diff] [blame] | 895 | // Be optimistic and try to create the directory |
Frederic Riss | 6b9396c | 2015-08-06 21:04:55 +0000 | [diff] [blame] | 896 | std::error_code EC = create_directory(P, IgnoreExisting, Perms); |
Rafael Espindola | b6f72b2 | 2014-02-13 16:58:19 +0000 | [diff] [blame] | 897 | // If we succeeded, or had any error other than the parent not existing, just |
| 898 | // return it. |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 899 | if (EC != errc::no_such_file_or_directory) |
Rafael Espindola | b6f72b2 | 2014-02-13 16:58:19 +0000 | [diff] [blame] | 900 | return EC; |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 901 | |
Rafael Espindola | b6f72b2 | 2014-02-13 16:58:19 +0000 | [diff] [blame] | 902 | // We failed because of a no_such_file_or_directory, try to create the |
| 903 | // parent. |
| 904 | StringRef Parent = path::parent_path(P); |
| 905 | if (Parent.empty()) |
| 906 | return EC; |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 907 | |
Frederic Riss | 6b9396c | 2015-08-06 21:04:55 +0000 | [diff] [blame] | 908 | if ((EC = create_directories(Parent, IgnoreExisting, Perms))) |
Rafael Espindola | b6f72b2 | 2014-02-13 16:58:19 +0000 | [diff] [blame] | 909 | return EC; |
| 910 | |
Frederic Riss | 6b9396c | 2015-08-06 21:04:55 +0000 | [diff] [blame] | 911 | return create_directory(P, IgnoreExisting, Perms); |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Justin Bogner | cd45f96 | 2014-06-19 19:35:39 +0000 | [diff] [blame] | 914 | std::error_code copy_file(const Twine &From, const Twine &To) { |
| 915 | int ReadFD, WriteFD; |
| 916 | if (std::error_code EC = openFileForRead(From, ReadFD)) |
| 917 | return EC; |
| 918 | if (std::error_code EC = openFileForWrite(To, WriteFD, F_None)) { |
| 919 | close(ReadFD); |
| 920 | return EC; |
| 921 | } |
| 922 | |
| 923 | const size_t BufSize = 4096; |
Dylan Noblesmith | 42836d9 | 2014-08-26 02:03:30 +0000 | [diff] [blame] | 924 | char *Buf = new char[BufSize]; |
Justin Bogner | cd45f96 | 2014-06-19 19:35:39 +0000 | [diff] [blame] | 925 | int BytesRead = 0, BytesWritten = 0; |
| 926 | for (;;) { |
| 927 | BytesRead = read(ReadFD, Buf, BufSize); |
| 928 | if (BytesRead <= 0) |
| 929 | break; |
| 930 | while (BytesRead) { |
| 931 | BytesWritten = write(WriteFD, Buf, BytesRead); |
| 932 | if (BytesWritten < 0) |
| 933 | break; |
| 934 | BytesRead -= BytesWritten; |
| 935 | } |
| 936 | if (BytesWritten < 0) |
| 937 | break; |
| 938 | } |
| 939 | close(ReadFD); |
| 940 | close(WriteFD); |
Dylan Noblesmith | 42836d9 | 2014-08-26 02:03:30 +0000 | [diff] [blame] | 941 | delete[] Buf; |
Justin Bogner | cd45f96 | 2014-06-19 19:35:39 +0000 | [diff] [blame] | 942 | |
| 943 | if (BytesRead < 0 || BytesWritten < 0) |
| 944 | return std::error_code(errno, std::generic_category()); |
| 945 | return std::error_code(); |
| 946 | } |
| 947 | |
Michael J. Spencer | 730f51a | 2010-12-09 17:37:02 +0000 | [diff] [blame] | 948 | bool exists(file_status status) { |
| 949 | return status_known(status) && status.type() != file_type::file_not_found; |
| 950 | } |
| 951 | |
| 952 | bool status_known(file_status s) { |
| 953 | return s.type() != file_type::status_error; |
| 954 | } |
| 955 | |
Zachary Turner | 82dd542 | 2017-03-07 16:10:10 +0000 | [diff] [blame] | 956 | file_type get_file_type(const Twine &Path, bool Follow) { |
Zachary Turner | 990e3cd | 2017-03-07 03:43:17 +0000 | [diff] [blame] | 957 | file_status st; |
Zachary Turner | 82dd542 | 2017-03-07 16:10:10 +0000 | [diff] [blame] | 958 | if (status(Path, st, Follow)) |
Zachary Turner | 990e3cd | 2017-03-07 03:43:17 +0000 | [diff] [blame] | 959 | return file_type::status_error; |
| 960 | return st.type(); |
| 961 | } |
| 962 | |
Michael J. Spencer | 730f51a | 2010-12-09 17:37:02 +0000 | [diff] [blame] | 963 | bool is_directory(file_status status) { |
| 964 | return status.type() == file_type::directory_file; |
| 965 | } |
| 966 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 967 | std::error_code is_directory(const Twine &path, bool &result) { |
Michael J. Spencer | 0d771ed | 2011-01-11 01:21:55 +0000 | [diff] [blame] | 968 | file_status st; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 969 | if (std::error_code ec = status(path, st)) |
Michael J. Spencer | 0d771ed | 2011-01-11 01:21:55 +0000 | [diff] [blame] | 970 | return ec; |
| 971 | result = is_directory(st); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 972 | return std::error_code(); |
Michael J. Spencer | 0d771ed | 2011-01-11 01:21:55 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Michael J. Spencer | 730f51a | 2010-12-09 17:37:02 +0000 | [diff] [blame] | 975 | bool is_regular_file(file_status status) { |
| 976 | return status.type() == file_type::regular_file; |
| 977 | } |
| 978 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 979 | std::error_code is_regular_file(const Twine &path, bool &result) { |
Michael J. Spencer | 0d771ed | 2011-01-11 01:21:55 +0000 | [diff] [blame] | 980 | file_status st; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 981 | if (std::error_code ec = status(path, st)) |
Michael J. Spencer | 0d771ed | 2011-01-11 01:21:55 +0000 | [diff] [blame] | 982 | return ec; |
| 983 | result = is_regular_file(st); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 984 | return std::error_code(); |
Michael J. Spencer | 0d771ed | 2011-01-11 01:21:55 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 987 | bool is_symlink_file(file_status status) { |
| 988 | return status.type() == file_type::symlink_file; |
| 989 | } |
| 990 | |
| 991 | std::error_code is_symlink_file(const Twine &path, bool &result) { |
| 992 | file_status st; |
| 993 | if (std::error_code ec = status(path, st, false)) |
| 994 | return ec; |
| 995 | result = is_symlink_file(st); |
| 996 | return std::error_code(); |
| 997 | } |
| 998 | |
Michael J. Spencer | 730f51a | 2010-12-09 17:37:02 +0000 | [diff] [blame] | 999 | bool is_other(file_status status) { |
| 1000 | return exists(status) && |
| 1001 | !is_regular_file(status) && |
Rafael Espindola | 2006306 | 2014-03-20 17:39:04 +0000 | [diff] [blame] | 1002 | !is_directory(status); |
Michael J. Spencer | 730f51a | 2010-12-09 17:37:02 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Juergen Ributzka | 84ba342 | 2014-12-18 18:19:47 +0000 | [diff] [blame] | 1005 | std::error_code is_other(const Twine &Path, bool &Result) { |
| 1006 | file_status FileStatus; |
| 1007 | if (std::error_code EC = status(Path, FileStatus)) |
| 1008 | return EC; |
| 1009 | Result = is_other(FileStatus); |
| 1010 | return std::error_code(); |
| 1011 | } |
| 1012 | |
Benjamin Kramer | 91ead3c | 2011-09-14 01:14:36 +0000 | [diff] [blame] | 1013 | void directory_entry::replace_filename(const Twine &filename, file_status st) { |
Rafael Espindola | f662e00 | 2015-07-15 21:24:07 +0000 | [diff] [blame] | 1014 | SmallString<128> path = path::parent_path(Path); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 1015 | path::append(path, filename); |
| 1016 | Path = path.str(); |
| 1017 | Status = st; |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Rui Ueyama | 6b77ad3 | 2016-11-15 01:57:05 +0000 | [diff] [blame] | 1020 | template <size_t N> |
| 1021 | static bool startswith(StringRef Magic, const char (&S)[N]) { |
| 1022 | return Magic.startswith(StringRef(S, N - 1)); |
| 1023 | } |
| 1024 | |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1025 | /// @brief Identify the magic in magic. |
Logan Chien | c0f691d | 2014-06-25 13:46:17 +0000 | [diff] [blame] | 1026 | file_magic identify_magic(StringRef Magic) { |
Rafael Espindola | 9b40429 | 2013-06-11 17:22:12 +0000 | [diff] [blame] | 1027 | if (Magic.size() < 4) |
Michael J. Spencer | 96ebd91 | 2012-06-19 05:29:57 +0000 | [diff] [blame] | 1028 | return file_magic::unknown; |
Rafael Espindola | 9b40429 | 2013-06-11 17:22:12 +0000 | [diff] [blame] | 1029 | switch ((unsigned char)Magic[0]) { |
Rui Ueyama | fc149a6 | 2013-10-15 22:45:38 +0000 | [diff] [blame] | 1030 | case 0x00: { |
Rui Ueyama | 2d02166 | 2016-11-15 00:54:54 +0000 | [diff] [blame] | 1031 | // COFF bigobj, CL.exe's LTO object file, or short import library file |
Rui Ueyama | 6b77ad3 | 2016-11-15 01:57:05 +0000 | [diff] [blame] | 1032 | if (startswith(Magic, "\0\0\xFF\xFF")) { |
Rui Ueyama | 5c69ff5 | 2014-09-11 22:34:32 +0000 | [diff] [blame] | 1033 | size_t MinSize = offsetof(COFF::BigObjHeader, UUID) + sizeof(COFF::BigObjMagic); |
| 1034 | if (Magic.size() < MinSize) |
| 1035 | return file_magic::coff_import_library; |
| 1036 | |
Rui Ueyama | 5c69ff5 | 2014-09-11 22:34:32 +0000 | [diff] [blame] | 1037 | const char *Start = Magic.data() + offsetof(COFF::BigObjHeader, UUID); |
Rui Ueyama | 2d02166 | 2016-11-15 00:54:54 +0000 | [diff] [blame] | 1038 | if (memcmp(Start, COFF::BigObjMagic, sizeof(COFF::BigObjMagic)) == 0) |
| 1039 | return file_magic::coff_object; |
| 1040 | if (memcmp(Start, COFF::ClGlObjMagic, sizeof(COFF::BigObjMagic)) == 0) |
| 1041 | return file_magic::coff_cl_gl_object; |
| 1042 | return file_magic::coff_import_library; |
Rui Ueyama | 2acb058 | 2014-09-11 21:09:57 +0000 | [diff] [blame] | 1043 | } |
Rui Ueyama | fc149a6 | 2013-10-15 22:45:38 +0000 | [diff] [blame] | 1044 | // Windows resource file |
Rui Ueyama | 6b77ad3 | 2016-11-15 01:57:05 +0000 | [diff] [blame] | 1045 | if (startswith(Magic, "\0\0\0\0\x20\0\0\0\xFF")) |
Rui Ueyama | fc149a6 | 2013-10-15 22:45:38 +0000 | [diff] [blame] | 1046 | return file_magic::windows_resource; |
Rui Ueyama | 829c439 | 2013-11-14 22:09:08 +0000 | [diff] [blame] | 1047 | // 0x0000 = COFF unknown machine type |
| 1048 | if (Magic[1] == 0) |
| 1049 | return file_magic::coff_object; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 1050 | if (startswith(Magic, "\0asm")) |
| 1051 | return file_magic::wasm_object; |
Rui Ueyama | fc149a6 | 2013-10-15 22:45:38 +0000 | [diff] [blame] | 1052 | break; |
| 1053 | } |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1054 | case 0xDE: // 0x0B17C0DE = BC wraper |
Rui Ueyama | 6b77ad3 | 2016-11-15 01:57:05 +0000 | [diff] [blame] | 1055 | if (startswith(Magic, "\xDE\xC0\x17\x0B")) |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1056 | return file_magic::bitcode; |
| 1057 | break; |
| 1058 | case 'B': |
Rui Ueyama | 6b77ad3 | 2016-11-15 01:57:05 +0000 | [diff] [blame] | 1059 | if (startswith(Magic, "BC\xC0\xDE")) |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1060 | return file_magic::bitcode; |
| 1061 | break; |
| 1062 | case '!': |
Rui Ueyama | 6b77ad3 | 2016-11-15 01:57:05 +0000 | [diff] [blame] | 1063 | if (startswith(Magic, "!<arch>\n") || startswith(Magic, "!<thin>\n")) |
| 1064 | return file_magic::archive; |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1065 | break; |
| 1066 | |
| 1067 | case '\177': |
Rui Ueyama | 6b77ad3 | 2016-11-15 01:57:05 +0000 | [diff] [blame] | 1068 | if (startswith(Magic, "\177ELF") && Magic.size() >= 18) { |
Rafael Espindola | 9b40429 | 2013-06-11 17:22:12 +0000 | [diff] [blame] | 1069 | bool Data2MSB = Magic[5] == 2; |
Michael J. Spencer | b8055cb | 2013-04-05 20:10:04 +0000 | [diff] [blame] | 1070 | unsigned high = Data2MSB ? 16 : 17; |
| 1071 | unsigned low = Data2MSB ? 17 : 16; |
Rui Ueyama | 6b77ad3 | 2016-11-15 01:57:05 +0000 | [diff] [blame] | 1072 | if (Magic[high] == 0) { |
Rafael Espindola | 9b40429 | 2013-06-11 17:22:12 +0000 | [diff] [blame] | 1073 | switch (Magic[low]) { |
Michael J. Spencer | e368a62 | 2015-01-23 21:58:09 +0000 | [diff] [blame] | 1074 | default: return file_magic::elf; |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1075 | case 1: return file_magic::elf_relocatable; |
| 1076 | case 2: return file_magic::elf_executable; |
| 1077 | case 3: return file_magic::elf_shared_object; |
| 1078 | case 4: return file_magic::elf_core; |
| 1079 | } |
Rui Ueyama | 6b77ad3 | 2016-11-15 01:57:05 +0000 | [diff] [blame] | 1080 | } |
| 1081 | // It's still some type of ELF file. |
| 1082 | return file_magic::elf; |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1083 | } |
| 1084 | break; |
| 1085 | |
| 1086 | case 0xCA: |
Rui Ueyama | 6b77ad3 | 2016-11-15 01:57:05 +0000 | [diff] [blame] | 1087 | if (startswith(Magic, "\xCA\xFE\xBA\xBE") || |
| 1088 | startswith(Magic, "\xCA\xFE\xBA\xBF")) { |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1089 | // This is complicated by an overlap with Java class files. |
| 1090 | // See the Mach-O section in /usr/share/file/magic for details. |
Rafael Espindola | 9b40429 | 2013-06-11 17:22:12 +0000 | [diff] [blame] | 1091 | if (Magic.size() >= 8 && Magic[7] < 43) |
Alexey Samsonov | e6388e6 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 1092 | return file_magic::macho_universal_binary; |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1093 | } |
| 1094 | break; |
| 1095 | |
| 1096 | // The two magic numbers for mach-o are: |
| 1097 | // 0xfeedface - 32-bit mach-o |
| 1098 | // 0xfeedfacf - 64-bit mach-o |
| 1099 | case 0xFE: |
| 1100 | case 0xCE: |
| 1101 | case 0xCF: { |
| 1102 | uint16_t type = 0; |
Rui Ueyama | 6b77ad3 | 2016-11-15 01:57:05 +0000 | [diff] [blame] | 1103 | if (startswith(Magic, "\xFE\xED\xFA\xCE") || |
| 1104 | startswith(Magic, "\xFE\xED\xFA\xCF")) { |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1105 | /* Native endian */ |
Kevin Enderby | 87c85b7 | 2016-01-26 23:43:37 +0000 | [diff] [blame] | 1106 | size_t MinSize; |
| 1107 | if (Magic[3] == char(0xCE)) |
| 1108 | MinSize = sizeof(MachO::mach_header); |
| 1109 | else |
| 1110 | MinSize = sizeof(MachO::mach_header_64); |
| 1111 | if (Magic.size() >= MinSize) |
| 1112 | type = Magic[12] << 24 | Magic[13] << 12 | Magic[14] << 8 | Magic[15]; |
Rui Ueyama | 6b77ad3 | 2016-11-15 01:57:05 +0000 | [diff] [blame] | 1113 | } else if (startswith(Magic, "\xCE\xFA\xED\xFE") || |
| 1114 | startswith(Magic, "\xCF\xFA\xED\xFE")) { |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1115 | /* Reverse endian */ |
Kevin Enderby | 87c85b7 | 2016-01-26 23:43:37 +0000 | [diff] [blame] | 1116 | size_t MinSize; |
| 1117 | if (Magic[0] == char(0xCE)) |
| 1118 | MinSize = sizeof(MachO::mach_header); |
| 1119 | else |
| 1120 | MinSize = sizeof(MachO::mach_header_64); |
| 1121 | if (Magic.size() >= MinSize) |
| 1122 | type = Magic[15] << 24 | Magic[14] << 12 |Magic[13] << 8 | Magic[12]; |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1123 | } |
| 1124 | switch (type) { |
| 1125 | default: break; |
| 1126 | case 1: return file_magic::macho_object; |
| 1127 | case 2: return file_magic::macho_executable; |
| 1128 | case 3: return file_magic::macho_fixed_virtual_memory_shared_lib; |
| 1129 | case 4: return file_magic::macho_core; |
Rafael Espindola | 134cc99 | 2013-06-10 20:32:27 +0000 | [diff] [blame] | 1130 | case 5: return file_magic::macho_preload_executable; |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1131 | case 6: return file_magic::macho_dynamically_linked_shared_lib; |
| 1132 | case 7: return file_magic::macho_dynamic_linker; |
| 1133 | case 8: return file_magic::macho_bundle; |
Nick Kledzik | 2d2b254 | 2014-09-17 00:53:44 +0000 | [diff] [blame] | 1134 | case 9: return file_magic::macho_dynamically_linked_shared_lib_stub; |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1135 | case 10: return file_magic::macho_dsym_companion; |
Justin Bogner | a7ad4b3 | 2015-02-25 22:59:20 +0000 | [diff] [blame] | 1136 | case 11: return file_magic::macho_kext_bundle; |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1137 | } |
| 1138 | break; |
| 1139 | } |
| 1140 | case 0xF0: // PowerPC Windows |
| 1141 | case 0x83: // Alpha 32-bit |
| 1142 | case 0x84: // Alpha 64-bit |
| 1143 | case 0x66: // MPS R4000 Windows |
| 1144 | case 0x50: // mc68K |
| 1145 | case 0x4c: // 80386 Windows |
Saleem Abdulrasool | 9b7c0af | 2014-03-13 07:02:35 +0000 | [diff] [blame] | 1146 | case 0xc4: // ARMNT Windows |
Rafael Espindola | 9b40429 | 2013-06-11 17:22:12 +0000 | [diff] [blame] | 1147 | if (Magic[1] == 0x01) |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1148 | return file_magic::coff_object; |
| 1149 | |
| 1150 | case 0x90: // PA-RISC Windows |
| 1151 | case 0x68: // mc68K Windows |
Rafael Espindola | 9b40429 | 2013-06-11 17:22:12 +0000 | [diff] [blame] | 1152 | if (Magic[1] == 0x02) |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1153 | return file_magic::coff_object; |
| 1154 | break; |
| 1155 | |
David Majnemer | 5026722 | 2014-11-05 06:24:35 +0000 | [diff] [blame] | 1156 | case 'M': // Possible MS-DOS stub on Windows PE file |
Rui Ueyama | 6b77ad3 | 2016-11-15 01:57:05 +0000 | [diff] [blame] | 1157 | if (startswith(Magic, "MZ")) { |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 1158 | uint32_t off = read32le(Magic.data() + 0x3c); |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1159 | // PE/COFF file, either EXE or DLL. |
David Majnemer | 5026722 | 2014-11-05 06:24:35 +0000 | [diff] [blame] | 1160 | if (off < Magic.size() && |
| 1161 | memcmp(Magic.data()+off, COFF::PEMagic, sizeof(COFF::PEMagic)) == 0) |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1162 | return file_magic::pecoff_executable; |
| 1163 | } |
| 1164 | break; |
| 1165 | |
| 1166 | case 0x64: // x86-64 Windows. |
Rafael Espindola | 9b40429 | 2013-06-11 17:22:12 +0000 | [diff] [blame] | 1167 | if (Magic[1] == char(0x86)) |
Michael J. Spencer | 4f8a832 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 1168 | return file_magic::coff_object; |
| 1169 | break; |
| 1170 | |
| 1171 | default: |
| 1172 | break; |
| 1173 | } |
| 1174 | return file_magic::unknown; |
| 1175 | } |
| 1176 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1177 | std::error_code identify_magic(const Twine &Path, file_magic &Result) { |
Rafael Espindola | da70bfd | 2014-06-11 22:53:00 +0000 | [diff] [blame] | 1178 | int FD; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1179 | if (std::error_code EC = openFileForRead(Path, FD)) |
Rafael Espindola | da70bfd | 2014-06-11 22:53:00 +0000 | [diff] [blame] | 1180 | return EC; |
Michael J. Spencer | 94b2ab3 | 2011-01-15 20:39:36 +0000 | [diff] [blame] | 1181 | |
Rafael Espindola | da70bfd | 2014-06-11 22:53:00 +0000 | [diff] [blame] | 1182 | char Buffer[32]; |
| 1183 | int Length = read(FD, Buffer, sizeof(Buffer)); |
Rafael Espindola | a8acef6 | 2014-06-25 14:35:59 +0000 | [diff] [blame] | 1184 | if (close(FD) != 0 || Length < 0) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1185 | return std::error_code(errno, std::generic_category()); |
Rafael Espindola | da70bfd | 2014-06-11 22:53:00 +0000 | [diff] [blame] | 1186 | |
| 1187 | Result = identify_magic(StringRef(Buffer, Length)); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1188 | return std::error_code(); |
Michael J. Spencer | 94b2ab3 | 2011-01-15 20:39:36 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1191 | std::error_code directory_entry::status(file_status &result) const { |
Aaron Ballman | 345012d | 2017-03-13 12:24:51 +0000 | [diff] [blame] | 1192 | return fs::status(Path, result, FollowSymlinks); |
| 1193 | } |
| 1194 | |
| 1195 | } // end namespace fs |
| 1196 | } // end namespace sys |
| 1197 | } // end namespace llvm |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 1198 | |
| 1199 | // Include the truly platform-specific parts. |
| 1200 | #if defined(LLVM_ON_UNIX) |
Rafael Espindola | f1fc382 | 2013-06-26 19:33:03 +0000 | [diff] [blame] | 1201 | #include "Unix/Path.inc" |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 1202 | #endif |
| 1203 | #if defined(LLVM_ON_WIN32) |
Rafael Espindola | f1fc382 | 2013-06-26 19:33:03 +0000 | [diff] [blame] | 1204 | #include "Windows/Path.inc" |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 1205 | #endif |
Pawel Bylica | 0e97e5c | 2015-11-02 09:49:17 +0000 | [diff] [blame] | 1206 | |
| 1207 | namespace llvm { |
| 1208 | namespace sys { |
| 1209 | namespace path { |
| 1210 | |
| 1211 | bool user_cache_directory(SmallVectorImpl<char> &Result, const Twine &Path1, |
| 1212 | const Twine &Path2, const Twine &Path3) { |
| 1213 | if (getUserCacheDir(Result)) { |
| 1214 | append(Result, Path1, Path2, Path3); |
| 1215 | return true; |
| 1216 | } |
| 1217 | return false; |
| 1218 | } |
| 1219 | |
| 1220 | } // end namespace path |
| 1221 | } // end namsspace sys |
| 1222 | } // end namespace llvm |