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