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