blob: f30e8a8b0cb70499994853a4464ea8f84990fb39 [file] [log] [blame]
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001//===-- Path.cpp - Implement OS Path Concept ------------------------------===//
Michael J. Spencerebad2f92010-11-29 22:28:51 +00002//
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 Espindolaf1fc3822013-06-26 19:33:03 +000010// This file implements the operating system Path API.
Michael J. Spencerebad2f92010-11-29 22:28:51 +000011//
12//===----------------------------------------------------------------------===//
13
Zachary Turner82a0c972017-03-20 23:33:18 +000014#include "llvm/Support/Path.h"
15#include "llvm/ADT/ArrayRef.h"
Rui Ueyama5c69ff52014-09-11 22:34:32 +000016#include "llvm/Support/Endian.h"
Rafael Espindola2a826e42014-06-13 17:20:48 +000017#include "llvm/Support/Errc.h"
Michael J. Spencerebad2f92010-11-29 22:28:51 +000018#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Support/FileSystem.h"
Aaron Ballman07e76182014-02-11 03:40:14 +000020#include "llvm/Support/Process.h"
Michael J. Spencerebad2f92010-11-29 22:28:51 +000021#include <cctype>
Michael J. Spencer848f46b2010-12-28 01:49:01 +000022#include <cstring>
Rafael Espindola4526b1d2013-06-18 17:01:00 +000023
24#if !defined(_MSC_VER) && !defined(__MINGW32__)
Douglas Gregora86ddf02013-03-21 21:46:10 +000025#include <unistd.h>
Rafael Espindola4526b1d2013-06-18 17:01:00 +000026#else
27#include <io.h>
Douglas Gregora86ddf02013-03-21 21:46:10 +000028#endif
Michael J. Spencerebad2f92010-11-29 22:28:51 +000029
Rafael Espindola7a0b6402014-02-24 03:07:41 +000030using namespace llvm;
Rui Ueyama3206b792015-03-02 21:19:12 +000031using namespace llvm::support::endian;
Rafael Espindola7a0b6402014-02-24 03:07:41 +000032
Michael J. Spencerebad2f92010-11-29 22:28:51 +000033namespace {
34 using llvm::StringRef;
Zhanyong Wan606bb1a2011-02-11 21:24:40 +000035 using llvm::sys::path::is_separator;
Zachary Turner5c5091f2017-03-16 22:28:04 +000036 using llvm::sys::path::Style;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000037
Zachary Turner5c5091f2017-03-16 22:28:04 +000038 inline Style real_style(Style style) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000039#ifdef LLVM_ON_WIN32
Zachary Turner5c5091f2017-03-16 22:28:04 +000040 return (style == Style::posix) ? Style::posix : Style::windows;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000041#else
Zachary Turner5c5091f2017-03-16 22:28:04 +000042 return (style == Style::windows) ? Style::windows : Style::posix;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000043#endif
Zachary Turner5c5091f2017-03-16 22:28:04 +000044 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +000045
Zachary Turner5c5091f2017-03-16 22:28:04 +000046 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. Spencerebad2f92010-11-29 22:28:51 +000059 // 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. Spencerebad2f92010-11-29 22:28:51 +000063 // * {file,directory}name
64
65 if (path.empty())
66 return path;
67
Zachary Turner5c5091f2017-03-16 22:28:04 +000068 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. Spencerebad2f92010-11-29 22:28:51 +000074
75 // //net
Zachary Turner5c5091f2017-03-16 22:28:04 +000076 if ((path.size() > 2) && is_separator(path[0], style) &&
77 path[0] == path[1] && !is_separator(path[2], style)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000078 // Find the next directory separator.
Zachary Turner5c5091f2017-03-16 22:28:04 +000079 size_t end = path.find_first_of(separators(style), 2);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000080 return path.substr(0, end);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000081 }
82
83 // {/,\}
Zachary Turner5c5091f2017-03-16 22:28:04 +000084 if (is_separator(path[0], style))
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000085 return path.substr(0, 1);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000086
Michael J. Spencerebad2f92010-11-29 22:28:51 +000087 // * {file,directory}name
Zachary Turner5c5091f2017-03-16 22:28:04 +000088 size_t end = path.find_first_of(separators(style));
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000089 return path.substr(0, end);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000090 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000091
Zachary Turner5c5091f2017-03-16 22:28:04 +000092 size_t filename_pos(StringRef str, Style style) {
93 if (str.size() == 2 && is_separator(str[0], style) && str[0] == str[1])
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000094 return 0;
95
Zachary Turner5c5091f2017-03-16 22:28:04 +000096 if (str.size() > 0 && is_separator(str[str.size() - 1], style))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000097 return str.size() - 1;
98
Zachary Turner5c5091f2017-03-16 22:28:04 +000099 size_t pos = str.find_last_of(separators(style), str.size() - 1);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000100
Zachary Turner5c5091f2017-03-16 22:28:04 +0000101 if (real_style(style) == Style::windows) {
102 if (pos == StringRef::npos)
103 pos = str.find_last_of(':', str.size() - 2);
104 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000105
Zachary Turner5c5091f2017-03-16 22:28:04 +0000106 if (pos == StringRef::npos || (pos == 1 && is_separator(str[0], style)))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000107 return 0;
108
109 return pos + 1;
110 }
111
Zachary Turner5c5091f2017-03-16 22:28:04 +0000112 size_t root_dir_start(StringRef str, Style style) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000113 // case "c:/"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000114 if (real_style(style) == Style::windows) {
115 if (str.size() > 2 && str[1] == ':' && is_separator(str[2], style))
116 return 2;
117 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000118
119 // case "//"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000120 if (str.size() == 2 && is_separator(str[0], style) && str[0] == str[1])
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000121 return StringRef::npos;
122
123 // case "//net"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000124 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. Spencer545cbdf2010-11-30 23:28:07 +0000127 }
128
129 // case "/"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000130 if (str.size() > 0 && is_separator(str[0], style))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000131 return 0;
132
133 return StringRef::npos;
134 }
135
Zachary Turner5c5091f2017-03-16 22:28:04 +0000136 size_t parent_path_end(StringRef path, Style style) {
137 size_t end_pos = filename_pos(path, style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000138
Zachary Turner5c5091f2017-03-16 22:28:04 +0000139 bool filename_was_sep =
140 path.size() > 0 && is_separator(path[end_pos], style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000141
142 // Skip separators except for root dir.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000143 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos), style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000144
Zachary Turner5c5091f2017-03-16 22:28:04 +0000145 while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
146 is_separator(path[end_pos - 1], style))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000147 --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 Wan606bb1a2011-02-11 21:24:40 +0000154} // end unnamed namespace
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000155
Rafael Espindolae79a8722013-06-28 03:48:47 +0000156enum FSEntity {
157 FS_Dir,
158 FS_File,
159 FS_Name
160};
161
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000162static std::error_code createUniqueEntity(const Twine &Model, int &ResultFD,
163 SmallVectorImpl<char> &ResultPath,
164 bool MakeAbsolute, unsigned Mode,
165 FSEntity Type) {
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000166 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 Espindola016a6d52014-08-26 14:47:52 +0000173 sys::path::system_temp_directory(true, TDir);
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000174 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
186retry_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 Espindoladb4ed0b2014-06-13 02:24:39 +0000196 if (std::error_code EC =
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000197 sys::fs::openFileForWrite(Twine(ResultPath.begin()), ResultFD,
198 sys::fs::F_RW | sys::fs::F_Excl, Mode)) {
Rafael Espindola2a826e42014-06-13 17:20:48 +0000199 if (EC == errc::file_exists)
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000200 goto retry_random_path;
201 return EC;
202 }
203
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000204 return std::error_code();
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000205 }
206
207 case FS_Name: {
Rafael Espindola281f23a2014-09-11 20:30:02 +0000208 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 Espindola7a0b6402014-02-24 03:07:41 +0000212 if (EC)
213 return EC;
Rafael Espindola281f23a2014-09-11 20:30:02 +0000214 goto retry_random_path;
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000215 }
216
217 case FS_Dir: {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000218 if (std::error_code EC =
219 sys::fs::create_directory(ResultPath.begin(), false)) {
Rafael Espindola2a826e42014-06-13 17:20:48 +0000220 if (EC == errc::file_exists)
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000221 goto retry_random_path;
222 return EC;
223 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000224 return std::error_code();
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000225 }
226 }
227 llvm_unreachable("Invalid Type");
228}
Rafael Espindolae79a8722013-06-28 03:48:47 +0000229
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000230namespace llvm {
231namespace sys {
232namespace path {
233
Zachary Turner5c5091f2017-03-16 22:28:04 +0000234const_iterator begin(StringRef path, Style style) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000235 const_iterator i;
236 i.Path = path;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000237 i.Component = find_first_component(path, style);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000238 i.Position = 0;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000239 i.S = style;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000240 return i;
241}
242
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000243const_iterator end(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000244 const_iterator i;
245 i.Path = path;
246 i.Position = path.size();
247 return i;
248}
249
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000250const_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 Turner5c5091f2017-03-16 22:28:04 +0000264 bool was_net = Component.size() > 2 && is_separator(Component[0], S) &&
265 Component[1] == Component[0] && !is_separator(Component[2], S);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000266
267 // Handle separators.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000268 if (is_separator(Path[Position], S)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000269 // Root dir.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000270 if (was_net ||
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000271 // c:/
Zachary Turner5c5091f2017-03-16 22:28:04 +0000272 (real_style(S) == Style::windows && Component.endswith(":"))) {
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000273 Component = Path.substr(Position, 1);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000274 return *this;
275 }
276
277 // Skip extra separators.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000278 while (Position != Path.size() && is_separator(Path[Position], S)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000279 ++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 Turner5c5091f2017-03-16 22:28:04 +0000291 size_t end_pos = Path.find_first_of(separators(S), Position);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000292 Component = Path.slice(Position, end_pos);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000293
294 return *this;
295}
296
Justin Bogner487e7642014-08-04 17:36:41 +0000297bool const_iterator::operator==(const const_iterator &RHS) const {
298 return Path.begin() == RHS.Path.begin() && Position == RHS.Position;
299}
300
301ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
302 return Position - RHS.Position;
303}
304
Zachary Turner5c5091f2017-03-16 22:28:04 +0000305reverse_iterator rbegin(StringRef Path, Style style) {
Justin Bogner487e7642014-08-04 17:36:41 +0000306 reverse_iterator I;
307 I.Path = Path;
308 I.Position = Path.size();
Zachary Turner5c5091f2017-03-16 22:28:04 +0000309 I.S = style;
Justin Bogner487e7642014-08-04 17:36:41 +0000310 return ++I;
311}
312
313reverse_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
321reverse_iterator &reverse_iterator::operator++() {
Ben Langmuir8d116392014-03-05 19:56:30 +0000322 // If we're at the end and the previous char was a '/', return '.' unless
323 // we are the root path.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000324 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. Spencer545cbdf2010-11-30 23:28:07 +0000327 --Position;
328 Component = ".";
329 return *this;
330 }
331
332 // Skip separators unless it's the root directory.
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000333 size_t end_pos = Position;
334
Zachary Turner5c5091f2017-03-16 22:28:04 +0000335 while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
336 is_separator(Path[end_pos - 1], S))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000337 --end_pos;
338
339 // Find next separator.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000340 size_t start_pos = filename_pos(Path.substr(0, end_pos), S);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000341 Component = Path.slice(start_pos, end_pos);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000342 Position = start_pos;
343 return *this;
344}
345
Justin Bogner487e7642014-08-04 17:36:41 +0000346bool reverse_iterator::operator==(const reverse_iterator &RHS) const {
347 return Path.begin() == RHS.Path.begin() && Component == RHS.Component &&
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000348 Position == RHS.Position;
349}
350
Filipe Cabecinhas78949382016-04-29 16:48:07 +0000351ptrdiff_t reverse_iterator::operator-(const reverse_iterator &RHS) const {
352 return Position - RHS.Position;
353}
354
Zachary Turner5c5091f2017-03-16 22:28:04 +0000355StringRef root_path(StringRef path, Style style) {
356 const_iterator b = begin(path, style), pos = b, e = end(path);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000357 if (b != e) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000358 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. Spencerebad2f92010-11-29 22:28:51 +0000361
362 if (has_net || has_drive) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000363 if ((++pos != e) && is_separator((*pos)[0], style)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000364 // {C:/,//net/}, so get the first two components.
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000365 return path.substr(0, b->size() + pos->size());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000366 } else {
367 // just {C:,//net}, return the first component.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000368 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000369 }
370 }
371
372 // POSIX style root directory.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000373 if (is_separator((*b)[0], style)) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000374 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000375 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000376 }
377
Michael J. Spencerf616b212010-12-07 17:04:04 +0000378 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000379}
380
Zachary Turner5c5091f2017-03-16 22:28:04 +0000381StringRef root_name(StringRef path, Style style) {
382 const_iterator b = begin(path, style), e = end(path);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000383 if (b != e) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000384 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. Spencerebad2f92010-11-29 22:28:51 +0000387
388 if (has_net || has_drive) {
389 // just {C:,//net}, return the first component.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000390 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000391 }
392 }
393
394 // No path or no name.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000395 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000396}
397
Zachary Turner5c5091f2017-03-16 22:28:04 +0000398StringRef root_directory(StringRef path, Style style) {
399 const_iterator b = begin(path, style), pos = b, e = end(path);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000400 if (b != e) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000401 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. Spencerebad2f92010-11-29 22:28:51 +0000404
405 if ((has_net || has_drive) &&
406 // {C:,//net}, skip to the next component.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000407 (++pos != e) && is_separator((*pos)[0], style)) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000408 return *pos;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000409 }
410
411 // POSIX style root directory.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000412 if (!has_net && is_separator((*b)[0], style)) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000413 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000414 }
415 }
416
417 // No path or no root.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000418 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000419}
420
Zachary Turner5c5091f2017-03-16 22:28:04 +0000421StringRef relative_path(StringRef path, Style style) {
422 StringRef root = root_path(path, style);
Michael J. Spencere6462392012-02-29 00:06:24 +0000423 return path.substr(root.size());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000424}
425
Zachary Turner5c5091f2017-03-16 22:28:04 +0000426void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
427 const Twine &b, const Twine &c, const Twine &d) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000428 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 Bylica64d08ff2015-10-22 08:12:15 +0000439 for (auto &component : components) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000440 bool path_has_sep =
441 !path.empty() && is_separator(path[path.size() - 1], style);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000442 if (path_has_sep) {
443 // Strip separators from beginning of component.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000444 size_t loc = component.find_first_not_of(separators(style));
Pawel Bylica64d08ff2015-10-22 08:12:15 +0000445 StringRef c = component.substr(loc);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000446
447 // Append it.
448 path.append(c.begin(), c.end());
449 continue;
450 }
451
Benjamin Kramer324d96b2017-08-09 22:06:32 +0000452 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. Spencerebad2f92010-11-29 22:28:51 +0000456 // Add a separator.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000457 path.push_back(preferred_separator(style));
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000458 }
459
Pawel Bylica64d08ff2015-10-22 08:12:15 +0000460 path.append(component.begin(), component.end());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000461 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000462}
463
Zachary Turner5c5091f2017-03-16 22:28:04 +0000464void 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 Kyrtzidisa61736f2011-02-15 17:51:19 +0000467}
468
Zachary Turner5c5091f2017-03-16 22:28:04 +0000469void 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
475StringRef parent_path(StringRef path, Style style) {
476 size_t end_pos = parent_path_end(path, style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000477 if (end_pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000478 return StringRef();
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000479 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000480 return path.substr(0, end_pos);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000481}
482
Zachary Turner5c5091f2017-03-16 22:28:04 +0000483void remove_filename(SmallVectorImpl<char> &path, Style style) {
484 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()), style);
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000485 if (end_pos != StringRef::npos)
486 path.set_size(end_pos);
Michael J. Spencer9c594092010-12-01 00:52:28 +0000487}
488
Zachary Turner5c5091f2017-03-16 22:28:04 +0000489void replace_extension(SmallVectorImpl<char> &path, const Twine &extension,
490 Style style) {
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000491 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 Turner5c5091f2017-03-16 22:28:04 +0000497 if (pos != StringRef::npos && pos >= filename_pos(p, style))
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000498 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. Spencerfb3a95d2010-12-01 00:52:55 +0000506}
507
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000508void replace_path_prefix(SmallVectorImpl<char> &Path,
Zachary Turner5c5091f2017-03-16 22:28:04 +0000509 const StringRef &OldPrefix, const StringRef &NewPrefix,
510 Style style) {
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000511 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 Turner5c5091f2017-03-16 22:28:04 +0000526 path::append(NewPath, style, NewPrefix);
527 path::append(NewPath, style, RelPath);
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000528 Path.swap(NewPath);
529}
530
Zachary Turner5c5091f2017-03-16 22:28:04 +0000531void native(const Twine &path, SmallVectorImpl<char> &result, Style style) {
Benjamin Kramerbd4ac9b2013-09-11 10:45:21 +0000532 assert((!path.isSingleStringRef() ||
533 path.getSingleStringRef().data() != result.data()) &&
534 "path and result are not allowed to overlap!");
Michael J. Spencer80025002010-12-01 02:48:27 +0000535 // Clear result.
Michael J. Spencer98c7a112010-12-07 01:23:19 +0000536 result.clear();
Michael J. Spencer80025002010-12-01 02:48:27 +0000537 path.toVector(result);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000538 native(result, style);
Benjamin Kramerbd4ac9b2013-09-11 10:45:21 +0000539}
540
Zachary Turner5c5091f2017-03-16 22:28:04 +0000541void native(SmallVectorImpl<char> &Path, Style style) {
Serge Pavlov9c761a32017-03-01 09:38:15 +0000542 if (Path.empty())
543 return;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000544 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 Espindolad649b9d2014-08-08 21:29:34 +0000561 }
562 }
Michael J. Spencer80025002010-12-01 02:48:27 +0000563}
564
Zachary Turner5c5091f2017-03-16 22:28:04 +0000565std::string convert_to_slash(StringRef path, Style style) {
566 if (real_style(style) != Style::windows)
567 return path;
568
Rui Ueyama3e649032017-01-09 01:47:15 +0000569 std::string s = path.str();
570 std::replace(s.begin(), s.end(), '\\', '/');
571 return s;
Rui Ueyama3e649032017-01-09 01:47:15 +0000572}
573
Zachary Turner5c5091f2017-03-16 22:28:04 +0000574StringRef filename(StringRef path, Style style) { return *rbegin(path, style); }
Michael J. Spencer14269202010-12-01 03:18:17 +0000575
Zachary Turner5c5091f2017-03-16 22:28:04 +0000576StringRef stem(StringRef path, Style style) {
577 StringRef fname = filename(path, style);
Michael J. Spencer956955e2010-12-01 03:18:33 +0000578 size_t pos = fname.find_last_of('.');
579 if (pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000580 return fname;
Michael J. Spencer956955e2010-12-01 03:18:33 +0000581 else
582 if ((fname.size() == 1 && fname == ".") ||
583 (fname.size() == 2 && fname == ".."))
Michael J. Spencerf616b212010-12-07 17:04:04 +0000584 return fname;
Michael J. Spencer956955e2010-12-01 03:18:33 +0000585 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000586 return fname.substr(0, pos);
Michael J. Spencer956955e2010-12-01 03:18:33 +0000587}
588
Zachary Turner5c5091f2017-03-16 22:28:04 +0000589StringRef extension(StringRef path, Style style) {
590 StringRef fname = filename(path, style);
Michael J. Spencer87106c52010-12-01 03:37:41 +0000591 size_t pos = fname.find_last_of('.');
592 if (pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000593 return StringRef();
Michael J. Spencer87106c52010-12-01 03:37:41 +0000594 else
595 if ((fname.size() == 1 && fname == ".") ||
596 (fname.size() == 2 && fname == ".."))
Michael J. Spencerf616b212010-12-07 17:04:04 +0000597 return StringRef();
Michael J. Spencer87106c52010-12-01 03:37:41 +0000598 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000599 return fname.substr(pos);
Michael J. Spencer87106c52010-12-01 03:37:41 +0000600}
601
Zachary Turner5c5091f2017-03-16 22:28:04 +0000602bool 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 Wan606bb1a2011-02-11 21:24:40 +0000608}
609
Zachary Turner5c5091f2017-03-16 22:28:04 +0000610StringRef get_separator(Style style) {
611 if (real_style(style) == Style::windows)
612 return "\\";
613 return "/";
Yaron Keren15217202014-05-16 13:16:30 +0000614}
615
Zachary Turner5c5091f2017-03-16 22:28:04 +0000616bool has_root_name(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000617 SmallString<128> path_storage;
618 StringRef p = path.toStringRef(path_storage);
619
Zachary Turner5c5091f2017-03-16 22:28:04 +0000620 return !root_name(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000621}
622
Zachary Turner5c5091f2017-03-16 22:28:04 +0000623bool has_root_directory(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000624 SmallString<128> path_storage;
625 StringRef p = path.toStringRef(path_storage);
626
Zachary Turner5c5091f2017-03-16 22:28:04 +0000627 return !root_directory(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000628}
629
Zachary Turner5c5091f2017-03-16 22:28:04 +0000630bool has_root_path(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000631 SmallString<128> path_storage;
632 StringRef p = path.toStringRef(path_storage);
633
Zachary Turner5c5091f2017-03-16 22:28:04 +0000634 return !root_path(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000635}
636
Zachary Turner5c5091f2017-03-16 22:28:04 +0000637bool has_relative_path(const Twine &path, Style style) {
Michael J. Spencer6d4b7e72010-12-20 13:30:28 +0000638 SmallString<128> path_storage;
639 StringRef p = path.toStringRef(path_storage);
640
Zachary Turner5c5091f2017-03-16 22:28:04 +0000641 return !relative_path(p, style).empty();
Michael J. Spencer6d4b7e72010-12-20 13:30:28 +0000642}
643
Zachary Turner5c5091f2017-03-16 22:28:04 +0000644bool has_filename(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000645 SmallString<128> path_storage;
646 StringRef p = path.toStringRef(path_storage);
647
Zachary Turner5c5091f2017-03-16 22:28:04 +0000648 return !filename(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000649}
650
Zachary Turner5c5091f2017-03-16 22:28:04 +0000651bool has_parent_path(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000652 SmallString<128> path_storage;
653 StringRef p = path.toStringRef(path_storage);
654
Zachary Turner5c5091f2017-03-16 22:28:04 +0000655 return !parent_path(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000656}
657
Zachary Turner5c5091f2017-03-16 22:28:04 +0000658bool has_stem(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000659 SmallString<128> path_storage;
660 StringRef p = path.toStringRef(path_storage);
661
Zachary Turner5c5091f2017-03-16 22:28:04 +0000662 return !stem(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000663}
664
Zachary Turner5c5091f2017-03-16 22:28:04 +0000665bool has_extension(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000666 SmallString<128> path_storage;
667 StringRef p = path.toStringRef(path_storage);
668
Zachary Turner5c5091f2017-03-16 22:28:04 +0000669 return !extension(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000670}
671
Zachary Turner5c5091f2017-03-16 22:28:04 +0000672bool is_absolute(const Twine &path, Style style) {
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000673 SmallString<128> path_storage;
674 StringRef p = path.toStringRef(path_storage);
675
Zachary Turner5c5091f2017-03-16 22:28:04 +0000676 bool rootDir = has_root_directory(p, style);
677 bool rootName =
678 (real_style(style) != Style::windows) || has_root_name(p, style);
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000679
Michael J. Spencerf616b212010-12-07 17:04:04 +0000680 return rootDir && rootName;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000681}
682
Zachary Turner5c5091f2017-03-16 22:28:04 +0000683bool is_relative(const Twine &path, Style style) {
684 return !is_absolute(path, style);
685}
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000686
Zachary Turner5c5091f2017-03-16 22:28:04 +0000687StringRef remove_leading_dotslash(StringRef Path, Style style) {
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000688 // Remove leading "./" (or ".//" or "././" etc.)
Zachary Turner5c5091f2017-03-16 22:28:04 +0000689 while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1], style)) {
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000690 Path = Path.substr(2);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000691 while (Path.size() > 0 && is_separator(Path[0], style))
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000692 Path = Path.substr(1);
693 }
694 return Path;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000695}
696
Zachary Turner5c5091f2017-03-16 22:28:04 +0000697static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot,
698 Style style) {
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000699 SmallVector<StringRef, 16> components;
700
701 // Skip the root path, then look for traversal in the components.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000702 StringRef rel = path::relative_path(path, style);
703 for (StringRef C :
704 llvm::make_range(path::begin(rel, style), path::end(rel))) {
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000705 if (C == ".")
706 continue;
Benjamin Kramer937dd7a2016-10-17 13:28:21 +0000707 // 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 Turner5c5091f2017-03-16 22:28:04 +0000713 if (path::is_absolute(path, style))
Benjamin Kramer937dd7a2016-10-17 13:28:21 +0000714 continue;
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000715 }
716 components.push_back(C);
717 }
718
Zachary Turner5c5091f2017-03-16 22:28:04 +0000719 SmallString<256> buffer = path::root_path(path, style);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000720 for (StringRef C : components)
Zachary Turner5c5091f2017-03-16 22:28:04 +0000721 path::append(buffer, style, C);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000722 return buffer;
723}
724
Zachary Turner5c5091f2017-03-16 22:28:04 +0000725bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot,
726 Style style) {
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000727 StringRef p(path.data(), path.size());
728
Zachary Turner5c5091f2017-03-16 22:28:04 +0000729 SmallString<256> result = remove_dots(p, remove_dot_dot, style);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000730 if (result == path)
731 return false;
732
733 path.swap(result);
734 return true;
735}
736
Michael J. Spencer9fc1d9d2010-12-01 19:32:01 +0000737} // end namespace path
738
739namespace fs {
740
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000741std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
Rafael Espindola7f822a92013-07-29 21:26:49 +0000742 file_status Status;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000743 std::error_code EC = status(Path, Status);
Rafael Espindola7f822a92013-07-29 21:26:49 +0000744 if (EC)
745 return EC;
746 Result = Status.getUniqueID();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000747 return std::error_code();
Rafael Espindola7f822a92013-07-29 21:26:49 +0000748}
749
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000750std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
751 SmallVectorImpl<char> &ResultPath,
752 unsigned Mode) {
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000753 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
754}
755
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000756std::error_code createUniqueFile(const Twine &Model,
757 SmallVectorImpl<char> &ResultPath) {
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000758 int Dummy;
759 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
760}
761
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000762static std::error_code
763createTemporaryFile(const Twine &Model, int &ResultFD,
764 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000765 SmallString<128> Storage;
766 StringRef P = Model.toNullTerminatedStringRef(Storage);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000767 assert(P.find_first_of(separators(Style::native)) == StringRef::npos &&
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000768 "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 Espindoladb4ed0b2014-06-13 02:24:39 +0000774static std::error_code
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000775createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000776 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
Rafael Espindolad3c89042013-07-25 15:00:17 +0000777 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
778 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000779 Type);
780}
781
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000782std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
783 int &ResultFD,
784 SmallVectorImpl<char> &ResultPath) {
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000785 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
786}
787
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000788std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
789 SmallVectorImpl<char> &ResultPath) {
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000790 int Dummy;
791 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
792}
793
794
Rafael Espindolae79a8722013-06-28 03:48:47 +0000795// This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
Rafael Espindola31a24432013-06-28 10:55:41 +0000796// for consistency. We should try using mkdtemp.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000797std::error_code createUniqueDirectory(const Twine &Prefix,
798 SmallVectorImpl<char> &ResultPath) {
Rafael Espindolae79a8722013-06-28 03:48:47 +0000799 int Dummy;
800 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
801 true, 0, FS_Dir);
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000802}
803
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000804static std::error_code make_absolute(const Twine &current_directory,
805 SmallVectorImpl<char> &path,
806 bool use_current_directory) {
Michael J. Spencer92903a32010-12-07 03:57:17 +0000807 StringRef p(path.data(), path.size());
808
Zachary Turner5c5091f2017-03-16 22:28:04 +0000809 bool rootDirectory = path::has_root_directory(p);
810 bool rootName =
811 (real_style(Style::native) != Style::windows) || path::has_root_name(p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000812
813 // Already absolute.
814 if (rootName && rootDirectory)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000815 return std::error_code();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000816
817 // All of the following conditions will need the current directory.
818 SmallString<128> current_dir;
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000819 if (use_current_directory)
820 current_directory.toVector(current_dir);
821 else if (std::error_code ec = current_path(current_dir))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000822 return ec;
Michael J. Spencer92903a32010-12-07 03:57:17 +0000823
824 // Relative path. Prepend the current directory.
825 if (!rootName && !rootDirectory) {
826 // Append path to the current directory.
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000827 path::append(current_dir, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000828 // Set path to the result.
829 path.swap(current_dir);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000830 return std::error_code();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000831 }
832
833 if (!rootName && rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000834 StringRef cdrn = path::root_name(current_dir);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000835 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000836 path::append(curDirRootName, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000837 // Set path to the result.
838 path.swap(curDirRootName);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000839 return std::error_code();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000840 }
841
842 if (rootName && !rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000843 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. Spencer92903a32010-12-07 03:57:17 +0000847
848 SmallString<128> res;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000849 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000850 path.swap(res);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000851 return std::error_code();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000852 }
853
854 llvm_unreachable("All rootName and rootDirectory combinations should have "
855 "occurred above!");
856}
857
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000858std::error_code make_absolute(const Twine &current_directory,
859 SmallVectorImpl<char> &path) {
860 return make_absolute(current_directory, path, true);
861}
862
863std::error_code make_absolute(SmallVectorImpl<char> &path) {
864 return make_absolute(Twine(), path, false);
865}
866
Frederic Riss6b9396c2015-08-06 21:04:55 +0000867std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
868 perms Perms) {
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000869 SmallString<128> PathStorage;
870 StringRef P = Path.toStringRef(PathStorage);
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000871
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000872 // Be optimistic and try to create the directory
Frederic Riss6b9396c2015-08-06 21:04:55 +0000873 std::error_code EC = create_directory(P, IgnoreExisting, Perms);
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000874 // If we succeeded, or had any error other than the parent not existing, just
875 // return it.
Rafael Espindola2a826e42014-06-13 17:20:48 +0000876 if (EC != errc::no_such_file_or_directory)
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000877 return EC;
Michael J. Spencerf616b212010-12-07 17:04:04 +0000878
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000879 // 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. Spencer31e310c2010-12-03 05:42:11 +0000884
Frederic Riss6b9396c2015-08-06 21:04:55 +0000885 if ((EC = create_directories(Parent, IgnoreExisting, Perms)))
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000886 return EC;
887
Frederic Riss6b9396c2015-08-06 21:04:55 +0000888 return create_directory(P, IgnoreExisting, Perms);
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000889}
890
Justin Bognercd45f962014-06-19 19:35:39 +0000891std::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 Noblesmith42836d92014-08-26 02:03:30 +0000901 char *Buf = new char[BufSize];
Justin Bognercd45f962014-06-19 19:35:39 +0000902 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 Noblesmith42836d92014-08-26 02:03:30 +0000918 delete[] Buf;
Justin Bognercd45f962014-06-19 19:35:39 +0000919
920 if (BytesRead < 0 || BytesWritten < 0)
921 return std::error_code(errno, std::generic_category());
922 return std::error_code();
923}
924
Zachary Turner82a0c972017-03-20 23:33:18 +0000925ErrorOr<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
945ErrorOr<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. Spencer730f51a2010-12-09 17:37:02 +0000955bool exists(file_status status) {
956 return status_known(status) && status.type() != file_type::file_not_found;
957}
958
959bool status_known(file_status s) {
960 return s.type() != file_type::status_error;
961}
962
Zachary Turner82dd5422017-03-07 16:10:10 +0000963file_type get_file_type(const Twine &Path, bool Follow) {
Zachary Turner990e3cd2017-03-07 03:43:17 +0000964 file_status st;
Zachary Turner82dd5422017-03-07 16:10:10 +0000965 if (status(Path, st, Follow))
Zachary Turner990e3cd2017-03-07 03:43:17 +0000966 return file_type::status_error;
967 return st.type();
968}
969
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000970bool is_directory(file_status status) {
971 return status.type() == file_type::directory_file;
972}
973
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000974std::error_code is_directory(const Twine &path, bool &result) {
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000975 file_status st;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000976 if (std::error_code ec = status(path, st))
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000977 return ec;
978 result = is_directory(st);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000979 return std::error_code();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000980}
981
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000982bool is_regular_file(file_status status) {
983 return status.type() == file_type::regular_file;
984}
985
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000986std::error_code is_regular_file(const Twine &path, bool &result) {
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000987 file_status st;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000988 if (std::error_code ec = status(path, st))
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000989 return ec;
990 result = is_regular_file(st);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000991 return std::error_code();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000992}
993
Zachary Turner7d86ee52017-03-08 17:56:08 +0000994bool is_symlink_file(file_status status) {
995 return status.type() == file_type::symlink_file;
996}
997
998std::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. Spencer730f51a2010-12-09 17:37:02 +00001006bool is_other(file_status status) {
1007 return exists(status) &&
1008 !is_regular_file(status) &&
Rafael Espindola20063062014-03-20 17:39:04 +00001009 !is_directory(status);
Michael J. Spencer730f51a2010-12-09 17:37:02 +00001010}
1011
Juergen Ributzka84ba3422014-12-18 18:19:47 +00001012std::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 Kramer91ead3c2011-09-14 01:14:36 +00001020void directory_entry::replace_filename(const Twine &filename, file_status st) {
Rafael Espindolaf662e002015-07-15 21:24:07 +00001021 SmallString<128> path = path::parent_path(Path);
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +00001022 path::append(path, filename);
1023 Path = path.str();
1024 Status = st;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +00001025}
1026
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001027std::error_code directory_entry::status(file_status &result) const {
Aaron Ballman345012d2017-03-13 12:24:51 +00001028 return fs::status(Path, result, FollowSymlinks);
1029}
1030
James Henderson566fdf42017-03-16 11:22:09 +00001031ErrorOr<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 Ballman345012d2017-03-13 12:24:51 +00001039} // end namespace fs
1040} // end namespace sys
1041} // end namespace llvm
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001042
1043// Include the truly platform-specific parts.
1044#if defined(LLVM_ON_UNIX)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001045#include "Unix/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001046#endif
1047#if defined(LLVM_ON_WIN32)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001048#include "Windows/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001049#endif
Pawel Bylica0e97e5c2015-11-02 09:49:17 +00001050
1051namespace llvm {
1052namespace sys {
1053namespace path {
1054
1055bool 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