blob: 14def83802dafea3307b62bfdf0be009dc1651ac [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Michael J. Spencerebad2f92010-11-29 22:28:51 +00006//
7//===----------------------------------------------------------------------===//
8//
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00009// This file implements the operating system Path API.
Michael J. Spencerebad2f92010-11-29 22:28:51 +000010//
11//===----------------------------------------------------------------------===//
12
Zachary Turner82a0c972017-03-20 23:33:18 +000013#include "llvm/Support/Path.h"
14#include "llvm/ADT/ArrayRef.h"
Nico Weber432a3882018-04-30 14:59:11 +000015#include "llvm/Config/llvm-config.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"
Rafael Espindola58fe67a2017-11-13 18:33:44 +000021#include "llvm/Support/Signals.h"
Michael J. Spencerebad2f92010-11-29 22:28:51 +000022#include <cctype>
Michael J. Spencer848f46b2010-12-28 01:49:01 +000023#include <cstring>
Rafael Espindola4526b1d2013-06-18 17:01:00 +000024
25#if !defined(_MSC_VER) && !defined(__MINGW32__)
Douglas Gregora86ddf02013-03-21 21:46:10 +000026#include <unistd.h>
Rafael Espindola4526b1d2013-06-18 17:01:00 +000027#else
28#include <io.h>
Douglas Gregora86ddf02013-03-21 21:46:10 +000029#endif
Michael J. Spencerebad2f92010-11-29 22:28:51 +000030
Rafael Espindola7a0b6402014-02-24 03:07:41 +000031using namespace llvm;
Rui Ueyama3206b792015-03-02 21:19:12 +000032using namespace llvm::support::endian;
Rafael Espindola7a0b6402014-02-24 03:07:41 +000033
Michael J. Spencerebad2f92010-11-29 22:28:51 +000034namespace {
35 using llvm::StringRef;
Zhanyong Wan606bb1a2011-02-11 21:24:40 +000036 using llvm::sys::path::is_separator;
Zachary Turner5c5091f2017-03-16 22:28:04 +000037 using llvm::sys::path::Style;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000038
Zachary Turner5c5091f2017-03-16 22:28:04 +000039 inline Style real_style(Style style) {
Nico Weber712e8d22018-04-29 00:45:03 +000040#ifdef _WIN32
Zachary Turner5c5091f2017-03-16 22:28:04 +000041 return (style == Style::posix) ? Style::posix : Style::windows;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000042#else
Zachary Turner5c5091f2017-03-16 22:28:04 +000043 return (style == Style::windows) ? Style::windows : Style::posix;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000044#endif
Zachary Turner5c5091f2017-03-16 22:28:04 +000045 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +000046
Zachary Turner5c5091f2017-03-16 22:28:04 +000047 inline const char *separators(Style style) {
48 if (real_style(style) == Style::windows)
49 return "\\/";
50 return "/";
51 }
52
53 inline char preferred_separator(Style style) {
54 if (real_style(style) == Style::windows)
55 return '\\';
56 return '/';
57 }
58
59 StringRef find_first_component(StringRef path, Style style) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000060 // Look for this first component in the following order.
61 // * empty (in this case we return an empty string)
62 // * either C: or {//,\\}net.
63 // * {/,\}
Michael J. Spencerebad2f92010-11-29 22:28:51 +000064 // * {file,directory}name
65
66 if (path.empty())
67 return path;
68
Zachary Turner5c5091f2017-03-16 22:28:04 +000069 if (real_style(style) == Style::windows) {
70 // C:
71 if (path.size() >= 2 &&
72 std::isalpha(static_cast<unsigned char>(path[0])) && path[1] == ':')
73 return path.substr(0, 2);
74 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +000075
76 // //net
Zachary Turner5c5091f2017-03-16 22:28:04 +000077 if ((path.size() > 2) && is_separator(path[0], style) &&
78 path[0] == path[1] && !is_separator(path[2], style)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000079 // Find the next directory separator.
Zachary Turner5c5091f2017-03-16 22:28:04 +000080 size_t end = path.find_first_of(separators(style), 2);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000081 return path.substr(0, end);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000082 }
83
84 // {/,\}
Zachary Turner5c5091f2017-03-16 22:28:04 +000085 if (is_separator(path[0], style))
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000086 return path.substr(0, 1);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000087
Michael J. Spencerebad2f92010-11-29 22:28:51 +000088 // * {file,directory}name
Zachary Turner5c5091f2017-03-16 22:28:04 +000089 size_t end = path.find_first_of(separators(style));
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000090 return path.substr(0, end);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000091 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000092
Pavel Labathd20289b2018-05-09 13:21:16 +000093 // Returns the first character of the filename in str. For paths ending in
94 // '/', it returns the position of the '/'.
Zachary Turner5c5091f2017-03-16 22:28:04 +000095 size_t filename_pos(StringRef str, Style style) {
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
Pavel Labathd20289b2018-05-09 13:21:16 +0000112 // Returns the position of the root directory in str. If there is no root
113 // directory in str, it returns StringRef::npos.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000114 size_t root_dir_start(StringRef str, Style style) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000115 // case "c:/"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000116 if (real_style(style) == Style::windows) {
117 if (str.size() > 2 && str[1] == ':' && is_separator(str[2], style))
118 return 2;
119 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000120
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000121 // case "//net"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000122 if (str.size() > 3 && is_separator(str[0], style) && str[0] == str[1] &&
123 !is_separator(str[2], style)) {
124 return str.find_first_of(separators(style), 2);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000125 }
126
127 // case "/"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000128 if (str.size() > 0 && is_separator(str[0], style))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000129 return 0;
130
131 return StringRef::npos;
132 }
133
Pavel Labathd20289b2018-05-09 13:21:16 +0000134 // Returns the position past the end of the "parent path" of path. The parent
135 // path will not end in '/', unless the parent is the root directory. If the
136 // path has no parent, 0 is returned.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000137 size_t parent_path_end(StringRef path, Style style) {
138 size_t end_pos = filename_pos(path, style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000139
Zachary Turner5c5091f2017-03-16 22:28:04 +0000140 bool filename_was_sep =
141 path.size() > 0 && is_separator(path[end_pos], style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000142
Pavel Labathd20289b2018-05-09 13:21:16 +0000143 // Skip separators until we reach root dir (or the start of the string).
144 size_t root_dir_pos = root_dir_start(path, style);
145 while (end_pos > 0 &&
146 (root_dir_pos == StringRef::npos || end_pos > root_dir_pos) &&
Zachary Turner5c5091f2017-03-16 22:28:04 +0000147 is_separator(path[end_pos - 1], style))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000148 --end_pos;
149
Pavel Labathd20289b2018-05-09 13:21:16 +0000150 if (end_pos == root_dir_pos && !filename_was_sep) {
151 // We've reached the root dir and the input path was *not* ending in a
152 // sequence of slashes. Include the root dir in the parent path.
153 return root_dir_pos + 1;
154 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000155
Pavel Labathd20289b2018-05-09 13:21:16 +0000156 // Otherwise, just include before the last slash.
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000157 return end_pos;
158 }
Zhanyong Wan606bb1a2011-02-11 21:24:40 +0000159} // end unnamed namespace
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000160
Rafael Espindolae79a8722013-06-28 03:48:47 +0000161enum FSEntity {
162 FS_Dir,
163 FS_File,
164 FS_Name
165};
166
Rafael Espindolad19c2e82017-11-27 23:44:11 +0000167static std::error_code
168createUniqueEntity(const Twine &Model, int &ResultFD,
169 SmallVectorImpl<char> &ResultPath, bool MakeAbsolute,
170 unsigned Mode, FSEntity Type,
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000171 sys::fs::OpenFlags Flags = sys::fs::OF_None) {
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000172
Bob Haarman112ebb62018-08-02 18:27:21 +0000173 // Limit the number of attempts we make, so that we don't infinite loop. E.g.
174 // "permission denied" could be for a specific file (so we retry with a
175 // different name) or for the whole directory (retry would always fail).
176 // Checking which is racy, so we try a number of times, then give up.
Bob Haarman9b36f512018-08-02 17:41:38 +0000177 std::error_code EC;
178 for (int Retries = 128; Retries > 0; --Retries) {
Jake Ehrlich5049c342019-03-18 20:35:18 +0000179 sys::fs::createUniquePath(Model, ResultPath, MakeAbsolute);
Bob Haarman9b36f512018-08-02 17:41:38 +0000180 // Try to open + create the file.
181 switch (Type) {
182 case FS_File: {
183 EC = sys::fs::openFileForReadWrite(Twine(ResultPath.begin()), ResultFD,
184 sys::fs::CD_CreateNew, Flags, Mode);
185 if (EC) {
186 // errc::permission_denied happens on Windows when we try to open a file
187 // that has been marked for deletion.
188 if (EC == errc::file_exists || EC == errc::permission_denied)
189 continue;
190 return EC;
191 }
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000192
Rafael Espindola281f23a2014-09-11 20:30:02 +0000193 return std::error_code();
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000194 }
Bob Haarman9b36f512018-08-02 17:41:38 +0000195
196 case FS_Name: {
197 EC = sys::fs::access(ResultPath.begin(), sys::fs::AccessMode::Exist);
198 if (EC == errc::no_such_file_or_directory)
199 return std::error_code();
200 if (EC)
201 return EC;
202 continue;
203 }
204
205 case FS_Dir: {
206 EC = sys::fs::create_directory(ResultPath.begin(), false);
207 if (EC) {
208 if (EC == errc::file_exists)
209 continue;
210 return EC;
211 }
212 return std::error_code();
213 }
214 }
215 llvm_unreachable("Invalid Type");
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000216 }
Bob Haarman9b36f512018-08-02 17:41:38 +0000217 return EC;
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000218}
Rafael Espindolae79a8722013-06-28 03:48:47 +0000219
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000220namespace llvm {
221namespace sys {
222namespace path {
223
Zachary Turner5c5091f2017-03-16 22:28:04 +0000224const_iterator begin(StringRef path, Style style) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000225 const_iterator i;
226 i.Path = path;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000227 i.Component = find_first_component(path, style);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000228 i.Position = 0;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000229 i.S = style;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000230 return i;
231}
232
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000233const_iterator end(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000234 const_iterator i;
235 i.Path = path;
236 i.Position = path.size();
237 return i;
238}
239
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000240const_iterator &const_iterator::operator++() {
241 assert(Position < Path.size() && "Tried to increment past end!");
242
243 // Increment Position to past the current component
244 Position += Component.size();
245
246 // Check for end.
247 if (Position == Path.size()) {
248 Component = StringRef();
249 return *this;
250 }
251
252 // Both POSIX and Windows treat paths that begin with exactly two separators
253 // specially.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000254 bool was_net = Component.size() > 2 && is_separator(Component[0], S) &&
255 Component[1] == Component[0] && !is_separator(Component[2], S);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000256
257 // Handle separators.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000258 if (is_separator(Path[Position], S)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000259 // Root dir.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000260 if (was_net ||
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000261 // c:/
Zachary Turner5c5091f2017-03-16 22:28:04 +0000262 (real_style(S) == Style::windows && Component.endswith(":"))) {
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000263 Component = Path.substr(Position, 1);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000264 return *this;
265 }
266
267 // Skip extra separators.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000268 while (Position != Path.size() && is_separator(Path[Position], S)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000269 ++Position;
270 }
271
Pavel Labathd20289b2018-05-09 13:21:16 +0000272 // Treat trailing '/' as a '.', unless it is the root dir.
273 if (Position == Path.size() && Component != "/") {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000274 --Position;
275 Component = ".";
276 return *this;
277 }
278 }
279
280 // Find next component.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000281 size_t end_pos = Path.find_first_of(separators(S), Position);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000282 Component = Path.slice(Position, end_pos);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000283
284 return *this;
285}
286
Justin Bogner487e7642014-08-04 17:36:41 +0000287bool const_iterator::operator==(const const_iterator &RHS) const {
288 return Path.begin() == RHS.Path.begin() && Position == RHS.Position;
289}
290
291ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
292 return Position - RHS.Position;
293}
294
Zachary Turner5c5091f2017-03-16 22:28:04 +0000295reverse_iterator rbegin(StringRef Path, Style style) {
Justin Bogner487e7642014-08-04 17:36:41 +0000296 reverse_iterator I;
297 I.Path = Path;
298 I.Position = Path.size();
Zachary Turner5c5091f2017-03-16 22:28:04 +0000299 I.S = style;
David Blaikie0c4dbf92019-04-25 20:09:00 +0000300 ++I;
301 return I;
Justin Bogner487e7642014-08-04 17:36:41 +0000302}
303
304reverse_iterator rend(StringRef Path) {
305 reverse_iterator I;
306 I.Path = Path;
307 I.Component = Path.substr(0, 0);
308 I.Position = 0;
309 return I;
310}
311
312reverse_iterator &reverse_iterator::operator++() {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000313 size_t root_dir_pos = root_dir_start(Path, S);
Pavel Labathd20289b2018-05-09 13:21:16 +0000314
315 // Skip separators unless it's the root directory.
316 size_t end_pos = Position;
317 while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
318 is_separator(Path[end_pos - 1], S))
319 --end_pos;
320
321 // Treat trailing '/' as a '.', unless it is the root dir.
322 if (Position == Path.size() && !Path.empty() &&
323 is_separator(Path.back(), S) &&
324 (root_dir_pos == StringRef::npos || end_pos - 1 > root_dir_pos)) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000325 --Position;
326 Component = ".";
327 return *this;
328 }
329
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000330 // Find next separator.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000331 size_t start_pos = filename_pos(Path.substr(0, end_pos), S);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000332 Component = Path.slice(start_pos, end_pos);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000333 Position = start_pos;
334 return *this;
335}
336
Justin Bogner487e7642014-08-04 17:36:41 +0000337bool reverse_iterator::operator==(const reverse_iterator &RHS) const {
338 return Path.begin() == RHS.Path.begin() && Component == RHS.Component &&
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000339 Position == RHS.Position;
340}
341
Filipe Cabecinhas78949382016-04-29 16:48:07 +0000342ptrdiff_t reverse_iterator::operator-(const reverse_iterator &RHS) const {
343 return Position - RHS.Position;
344}
345
Zachary Turner5c5091f2017-03-16 22:28:04 +0000346StringRef root_path(StringRef path, Style style) {
347 const_iterator b = begin(path, style), pos = b, e = end(path);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000348 if (b != e) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000349 bool has_net =
350 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
351 bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000352
353 if (has_net || has_drive) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000354 if ((++pos != e) && is_separator((*pos)[0], style)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000355 // {C:/,//net/}, so get the first two components.
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000356 return path.substr(0, b->size() + pos->size());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000357 } else {
358 // just {C:,//net}, return the first component.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000359 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000360 }
361 }
362
363 // POSIX style root directory.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000364 if (is_separator((*b)[0], style)) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000365 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000366 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000367 }
368
Michael J. Spencerf616b212010-12-07 17:04:04 +0000369 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000370}
371
Zachary Turner5c5091f2017-03-16 22:28:04 +0000372StringRef root_name(StringRef path, Style style) {
373 const_iterator b = begin(path, style), e = end(path);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000374 if (b != e) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000375 bool has_net =
376 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
377 bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000378
379 if (has_net || has_drive) {
380 // just {C:,//net}, return the first component.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000381 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000382 }
383 }
384
385 // No path or no name.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000386 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000387}
388
Zachary Turner5c5091f2017-03-16 22:28:04 +0000389StringRef root_directory(StringRef path, Style style) {
390 const_iterator b = begin(path, style), pos = b, e = end(path);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000391 if (b != e) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000392 bool has_net =
393 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
394 bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000395
396 if ((has_net || has_drive) &&
397 // {C:,//net}, skip to the next component.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000398 (++pos != e) && is_separator((*pos)[0], style)) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000399 return *pos;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000400 }
401
402 // POSIX style root directory.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000403 if (!has_net && is_separator((*b)[0], style)) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000404 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000405 }
406 }
407
408 // No path or no root.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000409 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000410}
411
Zachary Turner5c5091f2017-03-16 22:28:04 +0000412StringRef relative_path(StringRef path, Style style) {
413 StringRef root = root_path(path, style);
Michael J. Spencere6462392012-02-29 00:06:24 +0000414 return path.substr(root.size());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000415}
416
Zachary Turner5c5091f2017-03-16 22:28:04 +0000417void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
418 const Twine &b, const Twine &c, const Twine &d) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000419 SmallString<32> a_storage;
420 SmallString<32> b_storage;
421 SmallString<32> c_storage;
422 SmallString<32> d_storage;
423
424 SmallVector<StringRef, 4> components;
425 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
426 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
427 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
428 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
429
Pawel Bylica64d08ff2015-10-22 08:12:15 +0000430 for (auto &component : components) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000431 bool path_has_sep =
432 !path.empty() && is_separator(path[path.size() - 1], style);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000433 if (path_has_sep) {
434 // Strip separators from beginning of component.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000435 size_t loc = component.find_first_not_of(separators(style));
Pawel Bylica64d08ff2015-10-22 08:12:15 +0000436 StringRef c = component.substr(loc);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000437
438 // Append it.
439 path.append(c.begin(), c.end());
440 continue;
441 }
442
Benjamin Kramer324d96b2017-08-09 22:06:32 +0000443 bool component_has_sep =
444 !component.empty() && is_separator(component[0], style);
445 if (!component_has_sep &&
446 !(path.empty() || has_root_name(component, style))) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000447 // Add a separator.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000448 path.push_back(preferred_separator(style));
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000449 }
450
Pawel Bylica64d08ff2015-10-22 08:12:15 +0000451 path.append(component.begin(), component.end());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000452 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000453}
454
Zachary Turner5c5091f2017-03-16 22:28:04 +0000455void append(SmallVectorImpl<char> &path, const Twine &a, const Twine &b,
456 const Twine &c, const Twine &d) {
457 append(path, Style::native, a, b, c, d);
Argyrios Kyrtzidisa61736f2011-02-15 17:51:19 +0000458}
459
Zachary Turner5c5091f2017-03-16 22:28:04 +0000460void append(SmallVectorImpl<char> &path, const_iterator begin,
461 const_iterator end, Style style) {
462 for (; begin != end; ++begin)
463 path::append(path, style, *begin);
464}
465
466StringRef parent_path(StringRef path, Style style) {
467 size_t end_pos = parent_path_end(path, style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000468 if (end_pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000469 return StringRef();
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000470 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000471 return path.substr(0, end_pos);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000472}
473
Zachary Turner5c5091f2017-03-16 22:28:04 +0000474void remove_filename(SmallVectorImpl<char> &path, Style style) {
475 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()), style);
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000476 if (end_pos != StringRef::npos)
477 path.set_size(end_pos);
Michael J. Spencer9c594092010-12-01 00:52:28 +0000478}
479
Zachary Turner5c5091f2017-03-16 22:28:04 +0000480void replace_extension(SmallVectorImpl<char> &path, const Twine &extension,
481 Style style) {
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000482 StringRef p(path.begin(), path.size());
483 SmallString<32> ext_storage;
484 StringRef ext = extension.toStringRef(ext_storage);
485
486 // Erase existing extension.
487 size_t pos = p.find_last_of('.');
Zachary Turner5c5091f2017-03-16 22:28:04 +0000488 if (pos != StringRef::npos && pos >= filename_pos(p, style))
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000489 path.set_size(pos);
490
491 // Append '.' if needed.
492 if (ext.size() > 0 && ext[0] != '.')
493 path.push_back('.');
494
495 // Append extension.
496 path.append(ext.begin(), ext.end());
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000497}
498
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000499void replace_path_prefix(SmallVectorImpl<char> &Path,
Zachary Turner5c5091f2017-03-16 22:28:04 +0000500 const StringRef &OldPrefix, const StringRef &NewPrefix,
501 Style style) {
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000502 if (OldPrefix.empty() && NewPrefix.empty())
503 return;
504
505 StringRef OrigPath(Path.begin(), Path.size());
506 if (!OrigPath.startswith(OldPrefix))
507 return;
508
509 // If prefixes have the same size we can simply copy the new one over.
510 if (OldPrefix.size() == NewPrefix.size()) {
Fangrui Song75709322018-11-17 01:44:25 +0000511 llvm::copy(NewPrefix, Path.begin());
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000512 return;
513 }
514
515 StringRef RelPath = OrigPath.substr(OldPrefix.size());
516 SmallString<256> NewPath;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000517 path::append(NewPath, style, NewPrefix);
518 path::append(NewPath, style, RelPath);
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000519 Path.swap(NewPath);
520}
521
Zachary Turner5c5091f2017-03-16 22:28:04 +0000522void native(const Twine &path, SmallVectorImpl<char> &result, Style style) {
Benjamin Kramerbd4ac9b2013-09-11 10:45:21 +0000523 assert((!path.isSingleStringRef() ||
524 path.getSingleStringRef().data() != result.data()) &&
525 "path and result are not allowed to overlap!");
Michael J. Spencer80025002010-12-01 02:48:27 +0000526 // Clear result.
Michael J. Spencer98c7a112010-12-07 01:23:19 +0000527 result.clear();
Michael J. Spencer80025002010-12-01 02:48:27 +0000528 path.toVector(result);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000529 native(result, style);
Benjamin Kramerbd4ac9b2013-09-11 10:45:21 +0000530}
531
Zachary Turner5c5091f2017-03-16 22:28:04 +0000532void native(SmallVectorImpl<char> &Path, Style style) {
Serge Pavlov9c761a32017-03-01 09:38:15 +0000533 if (Path.empty())
534 return;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000535 if (real_style(style) == Style::windows) {
536 std::replace(Path.begin(), Path.end(), '/', '\\');
537 if (Path[0] == '~' && (Path.size() == 1 || is_separator(Path[1], style))) {
538 SmallString<128> PathHome;
539 home_directory(PathHome);
540 PathHome.append(Path.begin() + 1, Path.end());
541 Path = PathHome;
542 }
543 } else {
544 for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) {
545 if (*PI == '\\') {
546 auto PN = PI + 1;
547 if (PN < PE && *PN == '\\')
548 ++PI; // increment once, the for loop will move over the escaped slash
549 else
550 *PI = '/';
551 }
Rafael Espindolad649b9d2014-08-08 21:29:34 +0000552 }
553 }
Michael J. Spencer80025002010-12-01 02:48:27 +0000554}
555
Zachary Turner5c5091f2017-03-16 22:28:04 +0000556std::string convert_to_slash(StringRef path, Style style) {
557 if (real_style(style) != Style::windows)
558 return path;
559
Rui Ueyama3e649032017-01-09 01:47:15 +0000560 std::string s = path.str();
561 std::replace(s.begin(), s.end(), '\\', '/');
562 return s;
Rui Ueyama3e649032017-01-09 01:47:15 +0000563}
564
Zachary Turner5c5091f2017-03-16 22:28:04 +0000565StringRef filename(StringRef path, Style style) { return *rbegin(path, style); }
Michael J. Spencer14269202010-12-01 03:18:17 +0000566
Zachary Turner5c5091f2017-03-16 22:28:04 +0000567StringRef stem(StringRef path, Style style) {
568 StringRef fname = filename(path, style);
Michael J. Spencer956955e2010-12-01 03:18:33 +0000569 size_t pos = fname.find_last_of('.');
570 if (pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000571 return fname;
Michael J. Spencer956955e2010-12-01 03:18:33 +0000572 else
573 if ((fname.size() == 1 && fname == ".") ||
574 (fname.size() == 2 && fname == ".."))
Michael J. Spencerf616b212010-12-07 17:04:04 +0000575 return fname;
Michael J. Spencer956955e2010-12-01 03:18:33 +0000576 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000577 return fname.substr(0, pos);
Michael J. Spencer956955e2010-12-01 03:18:33 +0000578}
579
Zachary Turner5c5091f2017-03-16 22:28:04 +0000580StringRef extension(StringRef path, Style style) {
581 StringRef fname = filename(path, style);
Michael J. Spencer87106c52010-12-01 03:37:41 +0000582 size_t pos = fname.find_last_of('.');
583 if (pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000584 return StringRef();
Michael J. Spencer87106c52010-12-01 03:37:41 +0000585 else
586 if ((fname.size() == 1 && fname == ".") ||
587 (fname.size() == 2 && fname == ".."))
Michael J. Spencerf616b212010-12-07 17:04:04 +0000588 return StringRef();
Michael J. Spencer87106c52010-12-01 03:37:41 +0000589 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000590 return fname.substr(pos);
Michael J. Spencer87106c52010-12-01 03:37:41 +0000591}
592
Zachary Turner5c5091f2017-03-16 22:28:04 +0000593bool is_separator(char value, Style style) {
594 if (value == '/')
595 return true;
596 if (real_style(style) == Style::windows)
597 return value == '\\';
598 return false;
Zhanyong Wan606bb1a2011-02-11 21:24:40 +0000599}
600
Zachary Turner5c5091f2017-03-16 22:28:04 +0000601StringRef get_separator(Style style) {
602 if (real_style(style) == Style::windows)
603 return "\\";
604 return "/";
Yaron Keren15217202014-05-16 13:16:30 +0000605}
606
Zachary Turner5c5091f2017-03-16 22:28:04 +0000607bool has_root_name(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000608 SmallString<128> path_storage;
609 StringRef p = path.toStringRef(path_storage);
610
Zachary Turner5c5091f2017-03-16 22:28:04 +0000611 return !root_name(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000612}
613
Zachary Turner5c5091f2017-03-16 22:28:04 +0000614bool has_root_directory(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000615 SmallString<128> path_storage;
616 StringRef p = path.toStringRef(path_storage);
617
Zachary Turner5c5091f2017-03-16 22:28:04 +0000618 return !root_directory(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000619}
620
Zachary Turner5c5091f2017-03-16 22:28:04 +0000621bool has_root_path(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000622 SmallString<128> path_storage;
623 StringRef p = path.toStringRef(path_storage);
624
Zachary Turner5c5091f2017-03-16 22:28:04 +0000625 return !root_path(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000626}
627
Zachary Turner5c5091f2017-03-16 22:28:04 +0000628bool has_relative_path(const Twine &path, Style style) {
Michael J. Spencer6d4b7e72010-12-20 13:30:28 +0000629 SmallString<128> path_storage;
630 StringRef p = path.toStringRef(path_storage);
631
Zachary Turner5c5091f2017-03-16 22:28:04 +0000632 return !relative_path(p, style).empty();
Michael J. Spencer6d4b7e72010-12-20 13:30:28 +0000633}
634
Zachary Turner5c5091f2017-03-16 22:28:04 +0000635bool has_filename(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000636 SmallString<128> path_storage;
637 StringRef p = path.toStringRef(path_storage);
638
Zachary Turner5c5091f2017-03-16 22:28:04 +0000639 return !filename(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000640}
641
Zachary Turner5c5091f2017-03-16 22:28:04 +0000642bool has_parent_path(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000643 SmallString<128> path_storage;
644 StringRef p = path.toStringRef(path_storage);
645
Zachary Turner5c5091f2017-03-16 22:28:04 +0000646 return !parent_path(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000647}
648
Zachary Turner5c5091f2017-03-16 22:28:04 +0000649bool has_stem(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000650 SmallString<128> path_storage;
651 StringRef p = path.toStringRef(path_storage);
652
Zachary Turner5c5091f2017-03-16 22:28:04 +0000653 return !stem(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000654}
655
Zachary Turner5c5091f2017-03-16 22:28:04 +0000656bool has_extension(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000657 SmallString<128> path_storage;
658 StringRef p = path.toStringRef(path_storage);
659
Zachary Turner5c5091f2017-03-16 22:28:04 +0000660 return !extension(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000661}
662
Zachary Turner5c5091f2017-03-16 22:28:04 +0000663bool is_absolute(const Twine &path, Style style) {
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000664 SmallString<128> path_storage;
665 StringRef p = path.toStringRef(path_storage);
666
Zachary Turner5c5091f2017-03-16 22:28:04 +0000667 bool rootDir = has_root_directory(p, style);
668 bool rootName =
669 (real_style(style) != Style::windows) || has_root_name(p, style);
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000670
Michael J. Spencerf616b212010-12-07 17:04:04 +0000671 return rootDir && rootName;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000672}
673
Zachary Turner5c5091f2017-03-16 22:28:04 +0000674bool is_relative(const Twine &path, Style style) {
675 return !is_absolute(path, style);
676}
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000677
Zachary Turner5c5091f2017-03-16 22:28:04 +0000678StringRef remove_leading_dotslash(StringRef Path, Style style) {
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000679 // Remove leading "./" (or ".//" or "././" etc.)
Zachary Turner5c5091f2017-03-16 22:28:04 +0000680 while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1], style)) {
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000681 Path = Path.substr(2);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000682 while (Path.size() > 0 && is_separator(Path[0], style))
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000683 Path = Path.substr(1);
684 }
685 return Path;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000686}
687
Zachary Turner5c5091f2017-03-16 22:28:04 +0000688static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot,
689 Style style) {
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000690 SmallVector<StringRef, 16> components;
691
692 // Skip the root path, then look for traversal in the components.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000693 StringRef rel = path::relative_path(path, style);
694 for (StringRef C :
695 llvm::make_range(path::begin(rel, style), path::end(rel))) {
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000696 if (C == ".")
697 continue;
Benjamin Kramer937dd7a2016-10-17 13:28:21 +0000698 // Leading ".." will remain in the path unless it's at the root.
699 if (remove_dot_dot && C == "..") {
700 if (!components.empty() && components.back() != "..") {
701 components.pop_back();
702 continue;
703 }
Zachary Turner5c5091f2017-03-16 22:28:04 +0000704 if (path::is_absolute(path, style))
Benjamin Kramer937dd7a2016-10-17 13:28:21 +0000705 continue;
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000706 }
707 components.push_back(C);
708 }
709
Zachary Turner5c5091f2017-03-16 22:28:04 +0000710 SmallString<256> buffer = path::root_path(path, style);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000711 for (StringRef C : components)
Zachary Turner5c5091f2017-03-16 22:28:04 +0000712 path::append(buffer, style, C);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000713 return buffer;
714}
715
Zachary Turner5c5091f2017-03-16 22:28:04 +0000716bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot,
717 Style style) {
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000718 StringRef p(path.data(), path.size());
719
Zachary Turner5c5091f2017-03-16 22:28:04 +0000720 SmallString<256> result = remove_dots(p, remove_dot_dot, style);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000721 if (result == path)
722 return false;
723
724 path.swap(result);
725 return true;
726}
727
Michael J. Spencer9fc1d9d2010-12-01 19:32:01 +0000728} // end namespace path
729
730namespace fs {
731
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000732std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
Rafael Espindola7f822a92013-07-29 21:26:49 +0000733 file_status Status;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000734 std::error_code EC = status(Path, Status);
Rafael Espindola7f822a92013-07-29 21:26:49 +0000735 if (EC)
736 return EC;
737 Result = Status.getUniqueID();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000738 return std::error_code();
Rafael Espindola7f822a92013-07-29 21:26:49 +0000739}
740
Jake Ehrlich5049c342019-03-18 20:35:18 +0000741void createUniquePath(const Twine &Model, SmallVectorImpl<char> &ResultPath,
742 bool MakeAbsolute) {
743 SmallString<128> ModelStorage;
744 Model.toVector(ModelStorage);
745
746 if (MakeAbsolute) {
747 // Make model absolute by prepending a temp directory if it's not already.
748 if (!sys::path::is_absolute(Twine(ModelStorage))) {
749 SmallString<128> TDir;
750 sys::path::system_temp_directory(true, TDir);
751 sys::path::append(TDir, Twine(ModelStorage));
752 ModelStorage.swap(TDir);
753 }
754 }
755
756 ResultPath = ModelStorage;
757 ResultPath.push_back(0);
758 ResultPath.pop_back();
759
760 // Replace '%' with random chars.
761 for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) {
762 if (ModelStorage[i] == '%')
763 ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
764 }
765}
766
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000767std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
768 SmallVectorImpl<char> &ResultPath,
Zachary Turner8ac1c382018-06-05 19:58:26 +0000769 unsigned Mode) {
770 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
771}
772
773static std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
774 SmallVectorImpl<char> &ResultPath,
775 unsigned Mode, OpenFlags Flags) {
Rafael Espindolad19c2e82017-11-27 23:44:11 +0000776 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File,
777 Flags);
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000778}
779
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000780std::error_code createUniqueFile(const Twine &Model,
Ilya Biryukov84185762018-03-19 14:19:58 +0000781 SmallVectorImpl<char> &ResultPath,
782 unsigned Mode) {
783 int FD;
784 auto EC = createUniqueFile(Model, FD, ResultPath, Mode);
785 if (EC)
786 return EC;
787 // FD is only needed to avoid race conditions. Close it right away.
788 close(FD);
789 return EC;
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000790}
791
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000792static std::error_code
793createTemporaryFile(const Twine &Model, int &ResultFD,
Zachary Turner8ac1c382018-06-05 19:58:26 +0000794 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000795 SmallString<128> Storage;
796 StringRef P = Model.toNullTerminatedStringRef(Storage);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000797 assert(P.find_first_of(separators(Style::native)) == StringRef::npos &&
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000798 "Model must be a simple filename.");
799 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
Rafael Espindolad19c2e82017-11-27 23:44:11 +0000800 return createUniqueEntity(P.begin(), ResultFD, ResultPath, true,
Zachary Turner8ac1c382018-06-05 19:58:26 +0000801 owner_read | owner_write, Type);
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000802}
803
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000804static std::error_code
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000805createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
Zachary Turner8ac1c382018-06-05 19:58:26 +0000806 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
Rafael Espindolad3c89042013-07-25 15:00:17 +0000807 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
808 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
Zachary Turner8ac1c382018-06-05 19:58:26 +0000809 Type);
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000810}
811
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000812std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
813 int &ResultFD,
Zachary Turner8ac1c382018-06-05 19:58:26 +0000814 SmallVectorImpl<char> &ResultPath) {
815 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000816}
817
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000818std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
819 SmallVectorImpl<char> &ResultPath) {
Ilya Biryukov84185762018-03-19 14:19:58 +0000820 int FD;
821 auto EC = createTemporaryFile(Prefix, Suffix, FD, ResultPath);
822 if (EC)
823 return EC;
824 // FD is only needed to avoid race conditions. Close it right away.
825 close(FD);
826 return EC;
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000827}
828
829
Rafael Espindolae79a8722013-06-28 03:48:47 +0000830// This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
Rafael Espindola31a24432013-06-28 10:55:41 +0000831// for consistency. We should try using mkdtemp.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000832std::error_code createUniqueDirectory(const Twine &Prefix,
833 SmallVectorImpl<char> &ResultPath) {
Rafael Espindolae79a8722013-06-28 03:48:47 +0000834 int Dummy;
Ilya Biryukov84185762018-03-19 14:19:58 +0000835 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath, true, 0,
836 FS_Dir);
837}
838
839std::error_code
840getPotentiallyUniqueFileName(const Twine &Model,
841 SmallVectorImpl<char> &ResultPath) {
842 int Dummy;
843 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
844}
845
846std::error_code
847getPotentiallyUniqueTempFileName(const Twine &Prefix, StringRef Suffix,
848 SmallVectorImpl<char> &ResultPath) {
849 int Dummy;
850 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000851}
852
Pavel Labath1ad53ca2019-01-16 09:55:32 +0000853void make_absolute(const Twine &current_directory,
854 SmallVectorImpl<char> &path) {
Michael J. Spencer92903a32010-12-07 03:57:17 +0000855 StringRef p(path.data(), path.size());
856
Zachary Turner5c5091f2017-03-16 22:28:04 +0000857 bool rootDirectory = path::has_root_directory(p);
Jonas Devliegherecb6f2642019-08-06 15:46:45 +0000858 bool rootName = path::has_root_name(p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000859
860 // Already absolute.
Jonas Devliegherecb6f2642019-08-06 15:46:45 +0000861 if ((rootName || real_style(Style::native) != Style::windows) &&
862 rootDirectory)
Pavel Labath1ad53ca2019-01-16 09:55:32 +0000863 return;
Michael J. Spencer92903a32010-12-07 03:57:17 +0000864
865 // All of the following conditions will need the current directory.
866 SmallString<128> current_dir;
Pavel Labath1ad53ca2019-01-16 09:55:32 +0000867 current_directory.toVector(current_dir);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000868
869 // Relative path. Prepend the current directory.
870 if (!rootName && !rootDirectory) {
871 // Append path to the current directory.
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000872 path::append(current_dir, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000873 // Set path to the result.
874 path.swap(current_dir);
Pavel Labath1ad53ca2019-01-16 09:55:32 +0000875 return;
Michael J. Spencer92903a32010-12-07 03:57:17 +0000876 }
877
878 if (!rootName && rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000879 StringRef cdrn = path::root_name(current_dir);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000880 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000881 path::append(curDirRootName, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000882 // Set path to the result.
883 path.swap(curDirRootName);
Pavel Labath1ad53ca2019-01-16 09:55:32 +0000884 return;
Michael J. Spencer92903a32010-12-07 03:57:17 +0000885 }
886
887 if (rootName && !rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000888 StringRef pRootName = path::root_name(p);
889 StringRef bRootDirectory = path::root_directory(current_dir);
890 StringRef bRelativePath = path::relative_path(current_dir);
891 StringRef pRelativePath = path::relative_path(p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000892
893 SmallString<128> res;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000894 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000895 path.swap(res);
Pavel Labath1ad53ca2019-01-16 09:55:32 +0000896 return;
Michael J. Spencer92903a32010-12-07 03:57:17 +0000897 }
898
899 llvm_unreachable("All rootName and rootDirectory combinations should have "
900 "occurred above!");
901}
902
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000903std::error_code make_absolute(SmallVectorImpl<char> &path) {
Pavel Labath1ad53ca2019-01-16 09:55:32 +0000904 if (path::is_absolute(path))
905 return {};
906
907 SmallString<128> current_dir;
908 if (std::error_code ec = current_path(current_dir))
909 return ec;
910
911 make_absolute(current_dir, path);
912 return {};
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000913}
914
Frederic Riss6b9396c2015-08-06 21:04:55 +0000915std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
916 perms Perms) {
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000917 SmallString<128> PathStorage;
918 StringRef P = Path.toStringRef(PathStorage);
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000919
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000920 // Be optimistic and try to create the directory
Frederic Riss6b9396c2015-08-06 21:04:55 +0000921 std::error_code EC = create_directory(P, IgnoreExisting, Perms);
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000922 // If we succeeded, or had any error other than the parent not existing, just
923 // return it.
Rafael Espindola2a826e42014-06-13 17:20:48 +0000924 if (EC != errc::no_such_file_or_directory)
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000925 return EC;
Michael J. Spencerf616b212010-12-07 17:04:04 +0000926
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000927 // We failed because of a no_such_file_or_directory, try to create the
928 // parent.
929 StringRef Parent = path::parent_path(P);
930 if (Parent.empty())
931 return EC;
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000932
Frederic Riss6b9396c2015-08-06 21:04:55 +0000933 if ((EC = create_directories(Parent, IgnoreExisting, Perms)))
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000934 return EC;
935
Frederic Riss6b9396c2015-08-06 21:04:55 +0000936 return create_directory(P, IgnoreExisting, Perms);
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000937}
938
Zachary Turner1adca7c2018-06-28 18:49:09 +0000939static std::error_code copy_file_internal(int ReadFD, int WriteFD) {
Justin Bognercd45f962014-06-19 19:35:39 +0000940 const size_t BufSize = 4096;
Dylan Noblesmith42836d92014-08-26 02:03:30 +0000941 char *Buf = new char[BufSize];
Justin Bognercd45f962014-06-19 19:35:39 +0000942 int BytesRead = 0, BytesWritten = 0;
943 for (;;) {
944 BytesRead = read(ReadFD, Buf, BufSize);
945 if (BytesRead <= 0)
946 break;
947 while (BytesRead) {
948 BytesWritten = write(WriteFD, Buf, BytesRead);
949 if (BytesWritten < 0)
950 break;
951 BytesRead -= BytesWritten;
952 }
953 if (BytesWritten < 0)
954 break;
955 }
Dylan Noblesmith42836d92014-08-26 02:03:30 +0000956 delete[] Buf;
Justin Bognercd45f962014-06-19 19:35:39 +0000957
958 if (BytesRead < 0 || BytesWritten < 0)
959 return std::error_code(errno, std::generic_category());
960 return std::error_code();
961}
962
Adrian Prantlc90ff5e2019-04-24 19:08:43 +0000963#ifndef __APPLE__
Zachary Turner1adca7c2018-06-28 18:49:09 +0000964std::error_code copy_file(const Twine &From, const Twine &To) {
965 int ReadFD, WriteFD;
966 if (std::error_code EC = openFileForRead(From, ReadFD, OF_None))
967 return EC;
968 if (std::error_code EC =
969 openFileForWrite(To, WriteFD, CD_CreateAlways, OF_None)) {
970 close(ReadFD);
971 return EC;
972 }
973
974 std::error_code EC = copy_file_internal(ReadFD, WriteFD);
975
976 close(ReadFD);
977 close(WriteFD);
978
979 return EC;
980}
Adrian Prantlc90ff5e2019-04-24 19:08:43 +0000981#endif
Zachary Turner1adca7c2018-06-28 18:49:09 +0000982
983std::error_code copy_file(const Twine &From, int ToFD) {
984 int ReadFD;
985 if (std::error_code EC = openFileForRead(From, ReadFD, OF_None))
986 return EC;
987
988 std::error_code EC = copy_file_internal(ReadFD, ToFD);
989
990 close(ReadFD);
991
992 return EC;
993}
994
Zachary Turner82a0c972017-03-20 23:33:18 +0000995ErrorOr<MD5::MD5Result> md5_contents(int FD) {
996 MD5 Hash;
997
998 constexpr size_t BufSize = 4096;
999 std::vector<uint8_t> Buf(BufSize);
1000 int BytesRead = 0;
1001 for (;;) {
1002 BytesRead = read(FD, Buf.data(), BufSize);
1003 if (BytesRead <= 0)
1004 break;
1005 Hash.update(makeArrayRef(Buf.data(), BytesRead));
1006 }
1007
1008 if (BytesRead < 0)
1009 return std::error_code(errno, std::generic_category());
1010 MD5::MD5Result Result;
1011 Hash.final(Result);
1012 return Result;
1013}
1014
1015ErrorOr<MD5::MD5Result> md5_contents(const Twine &Path) {
1016 int FD;
Zachary Turner1f67a3c2018-06-07 19:58:58 +00001017 if (auto EC = openFileForRead(Path, FD, OF_None))
Zachary Turner82a0c972017-03-20 23:33:18 +00001018 return EC;
1019
1020 auto Result = md5_contents(FD);
1021 close(FD);
1022 return Result;
1023}
1024
Peter Collingbourne0dfdb442017-10-10 22:19:46 +00001025bool exists(const basic_file_status &status) {
Michael J. Spencer730f51a2010-12-09 17:37:02 +00001026 return status_known(status) && status.type() != file_type::file_not_found;
1027}
1028
Peter Collingbourne0dfdb442017-10-10 22:19:46 +00001029bool status_known(const basic_file_status &s) {
Michael J. Spencer730f51a2010-12-09 17:37:02 +00001030 return s.type() != file_type::status_error;
1031}
1032
Zachary Turner82dd5422017-03-07 16:10:10 +00001033file_type get_file_type(const Twine &Path, bool Follow) {
Zachary Turner990e3cd2017-03-07 03:43:17 +00001034 file_status st;
Zachary Turner82dd5422017-03-07 16:10:10 +00001035 if (status(Path, st, Follow))
Zachary Turner990e3cd2017-03-07 03:43:17 +00001036 return file_type::status_error;
1037 return st.type();
1038}
1039
Peter Collingbourne0dfdb442017-10-10 22:19:46 +00001040bool is_directory(const basic_file_status &status) {
Michael J. Spencer730f51a2010-12-09 17:37:02 +00001041 return status.type() == file_type::directory_file;
1042}
1043
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001044std::error_code is_directory(const Twine &path, bool &result) {
Michael J. Spencer0d771ed2011-01-11 01:21:55 +00001045 file_status st;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001046 if (std::error_code ec = status(path, st))
Michael J. Spencer0d771ed2011-01-11 01:21:55 +00001047 return ec;
1048 result = is_directory(st);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001049 return std::error_code();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +00001050}
1051
Peter Collingbourne0dfdb442017-10-10 22:19:46 +00001052bool is_regular_file(const basic_file_status &status) {
Michael J. Spencer730f51a2010-12-09 17:37:02 +00001053 return status.type() == file_type::regular_file;
1054}
1055
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001056std::error_code is_regular_file(const Twine &path, bool &result) {
Michael J. Spencer0d771ed2011-01-11 01:21:55 +00001057 file_status st;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001058 if (std::error_code ec = status(path, st))
Michael J. Spencer0d771ed2011-01-11 01:21:55 +00001059 return ec;
1060 result = is_regular_file(st);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001061 return std::error_code();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +00001062}
1063
Peter Collingbourne0dfdb442017-10-10 22:19:46 +00001064bool is_symlink_file(const basic_file_status &status) {
Zachary Turner7d86ee52017-03-08 17:56:08 +00001065 return status.type() == file_type::symlink_file;
1066}
1067
1068std::error_code is_symlink_file(const Twine &path, bool &result) {
1069 file_status st;
1070 if (std::error_code ec = status(path, st, false))
1071 return ec;
1072 result = is_symlink_file(st);
1073 return std::error_code();
1074}
1075
Peter Collingbourne0dfdb442017-10-10 22:19:46 +00001076bool is_other(const basic_file_status &status) {
Michael J. Spencer730f51a2010-12-09 17:37:02 +00001077 return exists(status) &&
1078 !is_regular_file(status) &&
Rafael Espindola20063062014-03-20 17:39:04 +00001079 !is_directory(status);
Michael J. Spencer730f51a2010-12-09 17:37:02 +00001080}
1081
Juergen Ributzka84ba3422014-12-18 18:19:47 +00001082std::error_code is_other(const Twine &Path, bool &Result) {
1083 file_status FileStatus;
1084 if (std::error_code EC = status(Path, FileStatus))
1085 return EC;
1086 Result = is_other(FileStatus);
1087 return std::error_code();
1088}
1089
Kristina Brooks3a55d1e2018-09-12 22:08:10 +00001090void directory_entry::replace_filename(const Twine &Filename, file_type Type,
1091 basic_file_status Status) {
1092 SmallString<128> PathStr = path::parent_path(Path);
1093 path::append(PathStr, Filename);
1094 this->Path = PathStr.str();
1095 this->Type = Type;
1096 this->Status = Status;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +00001097}
1098
James Henderson566fdf42017-03-16 11:22:09 +00001099ErrorOr<perms> getPermissions(const Twine &Path) {
1100 file_status Status;
1101 if (std::error_code EC = status(Path, Status))
1102 return EC;
1103
1104 return Status.permissions();
1105}
1106
Aaron Ballman345012d2017-03-13 12:24:51 +00001107} // end namespace fs
1108} // end namespace sys
1109} // end namespace llvm
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001110
1111// Include the truly platform-specific parts.
1112#if defined(LLVM_ON_UNIX)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001113#include "Unix/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001114#endif
Nico Weber712e8d22018-04-29 00:45:03 +00001115#if defined(_WIN32)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001116#include "Windows/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001117#endif
Pawel Bylica0e97e5c2015-11-02 09:49:17 +00001118
1119namespace llvm {
1120namespace sys {
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001121namespace fs {
1122TempFile::TempFile(StringRef Name, int FD) : TmpName(Name), FD(FD) {}
1123TempFile::TempFile(TempFile &&Other) { *this = std::move(Other); }
1124TempFile &TempFile::operator=(TempFile &&Other) {
1125 TmpName = std::move(Other.TmpName);
1126 FD = Other.FD;
1127 Other.Done = true;
Jonas Devliegherea6fe3452019-06-11 16:42:42 +00001128 Other.FD = -1;
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001129 return *this;
1130}
1131
1132TempFile::~TempFile() { assert(Done); }
1133
1134Error TempFile::discard() {
1135 Done = true;
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001136 if (FD != -1 && close(FD) == -1) {
1137 std::error_code EC = std::error_code(errno, std::generic_category());
1138 return errorCodeToError(EC);
1139 }
1140 FD = -1;
1141
Andrew Ngd27cf272019-02-14 11:08:49 +00001142#ifdef _WIN32
1143 // On windows closing will remove the file.
1144 TmpName = "";
1145 return Error::success();
1146#else
1147 // Always try to close and remove.
1148 std::error_code RemoveEC;
1149 if (!TmpName.empty()) {
1150 RemoveEC = fs::remove(TmpName);
1151 sys::DontRemoveFileOnSignal(TmpName);
1152 if (!RemoveEC)
1153 TmpName = "";
1154 }
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001155 return errorCodeToError(RemoveEC);
Andrew Ngd27cf272019-02-14 11:08:49 +00001156#endif
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001157}
1158
1159Error TempFile::keep(const Twine &Name) {
1160 assert(!Done);
1161 Done = true;
1162 // Always try to close and rename.
Nico Weber712e8d22018-04-29 00:45:03 +00001163#ifdef _WIN32
Vladimir Stefanovicbeb9d972018-07-03 17:26:43 +00001164 // If we can't cancel the delete don't rename.
Peter Collingbourne881ba102018-06-13 18:03:14 +00001165 auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
1166 std::error_code RenameEC = setDeleteDisposition(H, false);
Jeremy Morse01940652018-08-03 10:13:35 +00001167 if (!RenameEC) {
Rafael Espindola3ecd2042017-11-28 01:41:22 +00001168 RenameEC = rename_fd(FD, Name);
Jeremy Morse01940652018-08-03 10:13:35 +00001169 // If rename failed because it's cross-device, copy instead
1170 if (RenameEC ==
1171 std::error_code(ERROR_NOT_SAME_DEVICE, std::system_category())) {
1172 RenameEC = copy_file(TmpName, Name);
1173 setDeleteDisposition(H, true);
1174 }
1175 }
1176
Rafael Espindola20569e92017-12-05 16:40:56 +00001177 // If we can't rename, discard the temporary file.
1178 if (RenameEC)
Peter Collingbourne881ba102018-06-13 18:03:14 +00001179 setDeleteDisposition(H, true);
Rafael Espindola3ecd2042017-11-28 01:41:22 +00001180#else
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001181 std::error_code RenameEC = fs::rename(TmpName, Name);
Jonas Devlieghereae1727e2018-07-29 14:56:15 +00001182 if (RenameEC) {
1183 // If we can't rename, try to copy to work around cross-device link issues.
1184 RenameEC = sys::fs::copy_file(TmpName, Name);
1185 // If we can't rename or copy, discard the temporary file.
1186 if (RenameEC)
1187 remove(TmpName);
1188 }
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001189 sys::DontRemoveFileOnSignal(TmpName);
Rafael Espindola3ecd2042017-11-28 01:41:22 +00001190#endif
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001191
1192 if (!RenameEC)
1193 TmpName = "";
1194
1195 if (close(FD) == -1) {
1196 std::error_code EC(errno, std::generic_category());
1197 return errorCodeToError(EC);
1198 }
1199 FD = -1;
1200
1201 return errorCodeToError(RenameEC);
1202}
1203
1204Error TempFile::keep() {
1205 assert(!Done);
1206 Done = true;
1207
Nico Weber712e8d22018-04-29 00:45:03 +00001208#ifdef _WIN32
Peter Collingbourne881ba102018-06-13 18:03:14 +00001209 auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
1210 if (std::error_code EC = setDeleteDisposition(H, false))
Rafael Espindola3ecd2042017-11-28 01:41:22 +00001211 return errorCodeToError(EC);
1212#else
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001213 sys::DontRemoveFileOnSignal(TmpName);
Rafael Espindola3ecd2042017-11-28 01:41:22 +00001214#endif
1215
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001216 TmpName = "";
1217
1218 if (close(FD) == -1) {
1219 std::error_code EC(errno, std::generic_category());
1220 return errorCodeToError(EC);
1221 }
1222 FD = -1;
1223
1224 return Error::success();
1225}
1226
1227Expected<TempFile> TempFile::create(const Twine &Model, unsigned Mode) {
1228 int FD;
1229 SmallString<128> ResultPath;
Zachary Turner8ac1c382018-06-05 19:58:26 +00001230 if (std::error_code EC =
Zachary Turner1f67a3c2018-06-07 19:58:58 +00001231 createUniqueFile(Model, FD, ResultPath, Mode, OF_Delete))
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001232 return errorCodeToError(EC);
1233
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001234 TempFile Ret(ResultPath, FD);
Nico Weber712e8d22018-04-29 00:45:03 +00001235#ifndef _WIN32
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001236 if (sys::RemoveFileOnSignal(ResultPath)) {
Rafael Espindola3ecd2042017-11-28 01:41:22 +00001237 // Make sure we delete the file when RemoveFileOnSignal fails.
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001238 consumeError(Ret.discard());
1239 std::error_code EC(errc::operation_not_permitted);
1240 return errorCodeToError(EC);
1241 }
Rafael Espindola3ecd2042017-11-28 01:41:22 +00001242#endif
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001243 return std::move(Ret);
1244}
1245}
1246
Pawel Bylica0e97e5c2015-11-02 09:49:17 +00001247} // end namsspace sys
1248} // end namespace llvm