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