blob: 57d75358b814274511f8ab620125cd6bbfa22a46 [file] [log] [blame]
Reid Spencerb016a372004-09-15 05:49:50 +00001//===- llvm/System/Linux/Path.cpp - Linux Path Implementation ---*- C++ -*-===//
2//
Reid Spencercbad7012004-09-11 04:59:30 +00003// The LLVM Compiler Infrastructure
4//
Reid Spencerb016a372004-09-15 05:49:50 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencercbad7012004-09-11 04:59:30 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
Reid Spencerb016a372004-09-15 05:49:50 +00007//
8// Modified by Henrik Bach to comply with at least MinGW.
Reid Spencerd0c9e0e2004-09-18 19:29:16 +00009// Ported to Win32 by Jeff Cohen.
Reid Spencerb016a372004-09-15 05:49:50 +000010//
Reid Spencercbad7012004-09-11 04:59:30 +000011//===----------------------------------------------------------------------===//
12//
13// This file provides the Win32 specific implementation of the Path class.
14//
15//===----------------------------------------------------------------------===//
16
17//===----------------------------------------------------------------------===//
Reid Spencerb016a372004-09-15 05:49:50 +000018//=== WARNING: Implementation here must contain only generic Win32 code that
19//=== is guaranteed to work on *all* Win32 variants.
Reid Spencercbad7012004-09-11 04:59:30 +000020//===----------------------------------------------------------------------===//
21
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000022#include "Win32.h"
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000023#include <malloc.h>
Reid Spencercbad7012004-09-11 04:59:30 +000024
Jeff Cohencb652552004-12-24 02:38:34 +000025// We need to undo a macro defined in Windows.h, otherwise we won't compile:
26#undef CopyFile
27
Jeff Cohen966fa412005-07-09 18:42:49 +000028// Windows happily accepts either forward or backward slashes, though any path
29// returned by a Win32 API will have backward slashes. As LLVM code basically
30// assumes forward slashes are used, backward slashs are converted where they
31// can be introduced into a path.
32//
33// Another invariant is that a path ends with a slash if and only if the path
34// is a root directory. Any other use of a trailing slash is stripped. Unlike
35// in Unix, Windows has a rather complicated notion of a root path and this
36// invariant helps simply the code.
37
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000038static void FlipBackSlashes(std::string& s) {
39 for (size_t i = 0; i < s.size(); i++)
40 if (s[i] == '\\')
41 s[i] = '/';
42}
43
Reid Spencercbad7012004-09-11 04:59:30 +000044namespace llvm {
Reid Spencerb016a372004-09-15 05:49:50 +000045namespace sys {
Reid Spencercbad7012004-09-11 04:59:30 +000046
Reid Spencerb016a372004-09-15 05:49:50 +000047bool
Reid Spencer07adb282004-11-05 22:15:36 +000048Path::isValid() const {
Reid Spencerb016a372004-09-15 05:49:50 +000049 if (path.empty())
50 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000051
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000052 // If there is a colon, it must be the second character, preceded by a letter
53 // and followed by something.
54 size_t len = path.size();
55 size_t pos = path.rfind(':',len);
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000056 size_t rootslash = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000057 if (pos != std::string::npos) {
58 if (pos != 1 || !isalpha(path[0]) || len < 3)
59 return false;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000060 rootslash = 2;
61 }
Jeff Cohen85c716f2005-07-08 05:02:13 +000062
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000063 // Look for a UNC path, and if found adjust our notion of the root slash.
64 if (len > 3 && path[0] == '/' && path[1] == '/') {
65 rootslash = path.find('/', 2);
66 if (rootslash == std::string::npos)
67 rootslash = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000068 }
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000069
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000070 // Check for illegal characters.
71 if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012"
72 "\013\014\015\016\017\020\021\022\023\024\025\026"
73 "\027\030\031\032\033\034\035\036\037")
74 != std::string::npos)
75 return false;
Jeff Cohen85c716f2005-07-08 05:02:13 +000076
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000077 // Remove trailing slash, unless it's a root slash.
78 if (len > rootslash+1 && path[len-1] == '/')
79 path.erase(--len);
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000080
Jeff Cohen3bbbcc12005-01-14 04:09:39 +000081 // Check each component for legality.
82 for (pos = 0; pos < len; ++pos) {
83 // A component may not end in a space.
84 if (path[pos] == ' ') {
85 if (path[pos+1] == '/' || path[pos+1] == '\0')
86 return false;
87 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000088
Jeff Cohen3bbbcc12005-01-14 04:09:39 +000089 // A component may not end in a period.
90 if (path[pos] == '.') {
91 if (path[pos+1] == '/' || path[pos+1] == '\0') {
92 // Unless it is the pseudo-directory "."...
93 if (pos == 0 || path[pos-1] == '/' || path[pos-1] == ':')
94 return true;
95 // or "..".
96 if (pos > 0 && path[pos-1] == '.') {
97 if (pos == 1 || path[pos-2] == '/' || path[pos-2] == ':')
98 return true;
99 }
100 return false;
101 }
102 }
103 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000104
105 return true;
Reid Spencercbad7012004-09-11 04:59:30 +0000106}
107
Reid Spencer69cce812007-03-29 16:43:20 +0000108bool
109Path::isAbsolute() const {
Jeff Cohen84892be2007-03-29 17:27:38 +0000110 switch (path.length()) {
111 case 0:
112 return false;
113 case 1:
114 case 2:
115 return path[0] == '/';
116 default:
117 return path[0] == '/' || (path[1] == ':' && path[2] == '/');
118 }
Reid Spencer69cce812007-03-29 16:43:20 +0000119}
120
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000121static Path *TempDirectory = NULL;
122
Reid Spencerb016a372004-09-15 05:49:50 +0000123Path
Reid Spencercab0e432006-08-22 22:46:39 +0000124Path::GetTemporaryDirectory(std::string* ErrMsg) {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000125 if (TempDirectory)
126 return *TempDirectory;
127
128 char pathname[MAX_PATH];
Reid Spencercab0e432006-08-22 22:46:39 +0000129 if (!GetTempPath(MAX_PATH, pathname)) {
130 if (ErrMsg)
131 *ErrMsg = "Can't determine temporary directory";
132 return Path();
133 }
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000134
Reid Spencerb016a372004-09-15 05:49:50 +0000135 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000136 result.set(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000137
138 // Append a subdirectory passed on our process id so multiple LLVMs don't
139 // step on each other's toes.
Jeff Cohend41b30d2006-11-05 19:31:28 +0000140#ifdef __MINGW32__
141 // Mingw's Win32 header files are broken.
Reid Spencerab4d9b02006-06-08 18:08:43 +0000142 sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId()));
Jeff Cohend41b30d2006-11-05 19:31:28 +0000143#else
144 sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
145#endif
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000146 result.appendComponent(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000147
148 // If there's a directory left over from a previous LLVM execution that
149 // happened to have the same process id, get rid of it.
Reid Spencera229c5c2005-07-08 03:08:58 +0000150 result.eraseFromDisk(true);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000151
152 // And finally (re-)create the empty directory.
Reid Spencera229c5c2005-07-08 03:08:58 +0000153 result.createDirectoryOnDisk(false);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000154 TempDirectory = new Path(result);
155 return *TempDirectory;
Reid Spencerb016a372004-09-15 05:49:50 +0000156}
157
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000158// FIXME: the following set of functions don't map to Windows very well.
Reid Spencerb016a372004-09-15 05:49:50 +0000159Path
160Path::GetRootDirectory() {
161 Path result;
Jeff Cohen966fa412005-07-09 18:42:49 +0000162 result.set("C:/");
Reid Spencerb016a372004-09-15 05:49:50 +0000163 return result;
164}
165
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000166static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
167 const char* at = path;
168 const char* delim = strchr(at, ';');
169 Path tmpPath;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000170 while (delim != 0) {
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000171 std::string tmp(at, size_t(delim-at));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000172 if (tmpPath.set(tmp))
Reid Spencerc7f08322005-07-07 18:21:42 +0000173 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000174 Paths.push_back(tmpPath);
175 at = delim + 1;
176 delim = strchr(at, ';');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000177 }
Jeff Cohen85c716f2005-07-08 05:02:13 +0000178
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000179 if (*at != 0)
Reid Spencer1cf2d042005-07-07 23:35:23 +0000180 if (tmpPath.set(std::string(at)))
Reid Spencerc7f08322005-07-07 18:21:42 +0000181 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000182 Paths.push_back(tmpPath);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000183}
184
Jeff Cohen85c716f2005-07-08 05:02:13 +0000185void
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000186Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000187 Paths.push_back(sys::Path("C:/WINDOWS/SYSTEM32"));
188 Paths.push_back(sys::Path("C:/WINDOWS"));
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000189}
190
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000191void
192Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
193 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
194 if (env_var != 0) {
195 getPathList(env_var,Paths);
196 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000197#ifdef LLVM_LIBDIR
198 {
199 Path tmpPath;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000200 if (tmpPath.set(LLVM_LIBDIR))
Reid Spencerc7f08322005-07-07 18:21:42 +0000201 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000202 Paths.push_back(tmpPath);
203 }
204#endif
205 GetSystemLibraryPaths(Paths);
Reid Spencerb016a372004-09-15 05:49:50 +0000206}
207
208Path
209Path::GetLLVMDefaultConfigDir() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000210 // TODO: this isn't going to fly on Windows
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000211 return Path("/etc/llvm");
Reid Spencerb016a372004-09-15 05:49:50 +0000212}
213
214Path
Reid Spencerb016a372004-09-15 05:49:50 +0000215Path::GetUserHomeDirectory() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000216 // TODO: Typical Windows setup doesn't define HOME.
Reid Spencerb016a372004-09-15 05:49:50 +0000217 const char* home = getenv("HOME");
218 if (home) {
219 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000220 if (result.set(home))
Reid Spencerb016a372004-09-15 05:49:50 +0000221 return result;
222 }
223 return GetRootDirectory();
224}
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000225// FIXME: the above set of functions don't map to Windows very well.
226
Jeff Cohen966fa412005-07-09 18:42:49 +0000227
228bool
229Path::isRootDirectory() const {
230 size_t len = path.size();
231 return len > 0 && path[len-1] == '/';
Reid Spencera229c5c2005-07-08 03:08:58 +0000232}
233
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000234std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000235Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000236 // Find the last slash
237 size_t slash = path.rfind('/');
238 if (slash == std::string::npos)
239 slash = 0;
240 else
241 slash++;
242
Jeff Cohen966fa412005-07-09 18:42:49 +0000243 size_t dot = path.rfind('.');
244 if (dot == std::string::npos || dot < slash)
245 return path.substr(slash);
246 else
247 return path.substr(slash, dot - slash);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000248}
249
Reid Spencer07adb282004-11-05 22:15:36 +0000250bool Path::hasMagicNumber(const std::string &Magic) const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000251 std::string actualMagic;
252 if (getMagicNumber(actualMagic, Magic.size()))
253 return Magic == actualMagic;
254 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000255}
256
Jeff Cohen85c716f2005-07-08 05:02:13 +0000257bool
Reid Spencer07adb282004-11-05 22:15:36 +0000258Path::isBytecodeFile() const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000259 std::string actualMagic;
260 if (!getMagicNumber(actualMagic, 4))
261 return false;
262 return actualMagic == "llvc" || actualMagic == "llvm";
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000263}
264
265bool
Reid Spencerb016a372004-09-15 05:49:50 +0000266Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000267 DWORD attr = GetFileAttributes(path.c_str());
268 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000269}
270
271bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000272Path::canRead() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000273 // FIXME: take security attributes into account.
274 DWORD attr = GetFileAttributes(path.c_str());
275 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000276}
277
278bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000279Path::canWrite() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000280 // FIXME: take security attributes into account.
281 DWORD attr = GetFileAttributes(path.c_str());
282 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000283}
284
285bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000286Path::canExecute() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000287 // FIXME: take security attributes into account.
288 DWORD attr = GetFileAttributes(path.c_str());
289 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000290}
291
292std::string
293Path::getLast() const {
294 // Find the last slash
295 size_t pos = path.rfind('/');
296
297 // Handle the corner cases
298 if (pos == std::string::npos)
299 return path;
300
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000301 // If the last character is a slash, we have a root directory
302 if (pos == path.length()-1)
303 return path;
304
Reid Spencerb016a372004-09-15 05:49:50 +0000305 // Return everything after the last slash
306 return path.substr(pos+1);
307}
308
Reid Spencer8475ec02007-03-29 19:05:44 +0000309const FileStatus *
310Path::getFileStatus(bool update, std::string *ErrStr) const {
Reid Spencer69cce812007-03-29 16:43:20 +0000311 if (status == 0 || update) {
312 WIN32_FILE_ATTRIBUTE_DATA fi;
Reid Spencer8475ec02007-03-29 19:05:44 +0000313 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
314 MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) +
Reid Spencer69cce812007-03-29 16:43:20 +0000315 ": Can't get status: ");
Reid Spencer8475ec02007-03-29 19:05:44 +0000316 return 0;
317 }
Jeff Cohen626e38e2004-12-14 05:26:43 +0000318
Reid Spencer69cce812007-03-29 16:43:20 +0000319 if (status == 0)
320 status = new FileStatus;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000321
Reid Spencer69cce812007-03-29 16:43:20 +0000322 status->fileSize = fi.nFileSizeHigh;
323 status->fileSize <<= sizeof(fi.nFileSizeHigh)*8;
324 status->fileSize += fi.nFileSizeLow;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000325
Reid Spencer69cce812007-03-29 16:43:20 +0000326 status->mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
327 status->user = 9999; // Not applicable to Windows, so...
328 status->group = 9999; // Not applicable to Windows, so...
Jeff Cohen626e38e2004-12-14 05:26:43 +0000329
Reid Spencer4031bef2007-03-29 17:00:31 +0000330 // FIXME: this is only unique if the file is accessed by the same file path.
331 // How do we do this for C:\dir\file and ..\dir\file ? Unix has inode
332 // numbers, but the concept doesn't exist in Windows.
333 status->uniqueID = 0;
334 for (unsigned i = 0; i < path.length(); ++i)
335 status->uniqueID += path[i];
336
Reid Spencer69cce812007-03-29 16:43:20 +0000337 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
338 status->modTime.fromWin32Time(ft);
339
340 status->isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
341 }
Reid Spencer8475ec02007-03-29 19:05:44 +0000342 return status;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000343}
344
Reid Spencera34a1572006-08-22 23:54:35 +0000345bool Path::makeReadableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000346 // All files are readable on Windows (ignoring security attributes).
Reid Spencera34a1572006-08-22 23:54:35 +0000347 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000348}
349
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000350bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000351 DWORD attr = GetFileAttributes(path.c_str());
352
353 // If it doesn't exist, we're done.
354 if (attr == INVALID_FILE_ATTRIBUTES)
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000355 return false;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000356
357 if (attr & FILE_ATTRIBUTE_READONLY) {
Reid Spencera34a1572006-08-22 23:54:35 +0000358 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) {
359 MakeErrMsg(ErrMsg, std::string(path) + ": Can't make file writable: ");
360 return true;
361 }
Jeff Cohen626e38e2004-12-14 05:26:43 +0000362 }
Reid Spencera34a1572006-08-22 23:54:35 +0000363 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000364}
365
Reid Spencera34a1572006-08-22 23:54:35 +0000366bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000367 // All files are executable on Windows (ignoring security attributes).
Reid Spencera34a1572006-08-22 23:54:35 +0000368 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000369}
370
Reid Spencerb016a372004-09-15 05:49:50 +0000371bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000372Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
Reid Spencer8475ec02007-03-29 19:05:44 +0000373 const FileStatus *Status = getFileStatus(false, ErrMsg);
374 if (!Status)
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000375 return true;
Reid Spencer8475ec02007-03-29 19:05:44 +0000376 if (!Status->isDir) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000377 MakeErrMsg(ErrMsg, path + ": not a directory");
378 return true;
379 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000380
381 result.clear();
382 WIN32_FIND_DATA fd;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000383 std::string searchpath = path;
384 if (path.size() == 0 || searchpath[path.size()-1] == '/')
Jeff Cohen85c716f2005-07-08 05:02:13 +0000385 searchpath += "*";
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000386 else
387 searchpath += "/*";
Jeff Cohen85c716f2005-07-08 05:02:13 +0000388
Jeff Cohen9437bb62005-01-27 03:49:03 +0000389 HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000390 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohen9437bb62005-01-27 03:49:03 +0000391 if (GetLastError() == ERROR_FILE_NOT_FOUND)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000392 return true; // not really an error, now is it?
Reid Spencer142ca8e2006-08-23 06:56:27 +0000393 MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
394 return true;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000395 }
396
397 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000398 if (fd.cFileName[0] == '.')
399 continue;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000400 Path aPath(path);
401 aPath.appendComponent(&fd.cFileName[0]);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000402 result.insert(aPath);
403 } while (FindNextFile(h, &fd));
404
Jeff Cohen51b8d212004-12-31 19:01:08 +0000405 DWORD err = GetLastError();
406 FindClose(h);
407 if (err != ERROR_NO_MORE_FILES) {
408 SetLastError(err);
Reid Spencer142ca8e2006-08-23 06:56:27 +0000409 MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
410 return true;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000411 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000412 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000413}
414
415bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000416Path::set(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000417 if (a_path.size() == 0)
418 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000419 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000420 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000421 FlipBackSlashes(path);
Reid Spencer07adb282004-11-05 22:15:36 +0000422 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000423 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000424 return false;
425 }
426 return true;
427}
428
429bool
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000430Path::appendComponent(const std::string& name) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000431 if (name.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000432 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000433 std::string save(path);
434 if (!path.empty()) {
435 size_t last = path.size() - 1;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000436 if (path[last] != '/')
Reid Spencer1cf2d042005-07-07 23:35:23 +0000437 path += '/';
438 }
439 path += name;
Reid Spencer07adb282004-11-05 22:15:36 +0000440 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000441 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000442 return false;
443 }
444 return true;
445}
446
447bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000448Path::eraseComponent() {
Reid Spencerb016a372004-09-15 05:49:50 +0000449 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000450 if (slashpos == path.size() - 1 || slashpos == std::string::npos)
Reid Spencerb016a372004-09-15 05:49:50 +0000451 return false;
Jeff Cohen966fa412005-07-09 18:42:49 +0000452 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000453 path.erase(slashpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000454 if (!isValid()) {
455 path = save;
456 return false;
457 }
Reid Spencerb016a372004-09-15 05:49:50 +0000458 return true;
459}
460
461bool
Reid Spencer07adb282004-11-05 22:15:36 +0000462Path::appendSuffix(const std::string& suffix) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000463 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000464 path.append(".");
465 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000466 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000467 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000468 return false;
469 }
470 return true;
471}
472
473bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000474Path::eraseSuffix() {
Reid Spencerb016a372004-09-15 05:49:50 +0000475 size_t dotpos = path.rfind('.',path.size());
476 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000477 if (dotpos != std::string::npos) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000478 if (slashpos == std::string::npos || dotpos > slashpos+1) {
479 std::string save(path);
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000480 path.erase(dotpos, path.size()-dotpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000481 if (!isValid()) {
482 path = save;
483 return false;
484 }
Jeff Cohen85c716f2005-07-08 05:02:13 +0000485 return true;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000486 }
Reid Spencerb016a372004-09-15 05:49:50 +0000487 }
488 return false;
489}
490
Reid Spencer30300992006-08-24 18:58:37 +0000491inline bool PathMsg(std::string* ErrMsg, const char* pathname, const char*msg) {
492 if (ErrMsg)
493 *ErrMsg = std::string(pathname) + ": " + std::string(msg);
494 return true;
495}
496
Reid Spencerb016a372004-09-15 05:49:50 +0000497bool
Reid Spencer30300992006-08-24 18:58:37 +0000498Path::createDirectoryOnDisk(bool create_parents, std::string* ErrMsg) {
Reid Spencerb016a372004-09-15 05:49:50 +0000499 // Get a writeable copy of the path name
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000500 size_t len = path.length();
501 char *pathname = reinterpret_cast<char *>(_alloca(len+2));
502 path.copy(pathname, len);
503 pathname[len] = 0;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000504
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000505 // Make sure it ends with a slash.
506 if (len == 0 || pathname[len - 1] != '/') {
507 pathname[len] = '/';
508 pathname[++len] = 0;
509 }
Reid Spencerb016a372004-09-15 05:49:50 +0000510
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000511 // Determine starting point for initial / search.
512 char *next = pathname;
513 if (pathname[0] == '/' && pathname[1] == '/') {
514 // Skip host name.
515 next = strchr(pathname+2, '/');
516 if (next == NULL)
Reid Spencer30300992006-08-24 18:58:37 +0000517 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
518
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000519 // Skip share name.
520 next = strchr(next+1, '/');
521 if (next == NULL)
Reid Spencer30300992006-08-24 18:58:37 +0000522 return PathMsg(ErrMsg, pathname,"badly formed remote directory");
523
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000524 next++;
525 if (*next == 0)
Reid Spencer30300992006-08-24 18:58:37 +0000526 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
527
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000528 } else {
529 if (pathname[1] == ':')
530 next += 2; // skip drive letter
531 if (*next == '/')
532 next++; // skip root directory
533 }
Reid Spencerb016a372004-09-15 05:49:50 +0000534
535 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000536 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000537 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000538 while (*next) {
539 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000540 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000541 if (!CreateDirectory(pathname, NULL))
Reid Spencer30300992006-08-24 18:58:37 +0000542 return MakeErrMsg(ErrMsg,
543 std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000544 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000545 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000546 } else {
547 // Drop trailing slash.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000548 pathname[len-1] = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000549 if (!CreateDirectory(pathname, NULL)) {
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000550 return MakeErrMsg(ErrMsg, std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000551 }
Reid Spencerb016a372004-09-15 05:49:50 +0000552 }
Reid Spencer30300992006-08-24 18:58:37 +0000553 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000554}
555
556bool
Reid Spencer30300992006-08-24 18:58:37 +0000557Path::createFileOnDisk(std::string* ErrMsg) {
Reid Spencerb016a372004-09-15 05:49:50 +0000558 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000559 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000560 FILE_ATTRIBUTE_NORMAL, NULL);
561 if (h == INVALID_HANDLE_VALUE)
Reid Spencer30300992006-08-24 18:58:37 +0000562 return MakeErrMsg(ErrMsg, path + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000563
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000564 CloseHandle(h);
Reid Spencer30300992006-08-24 18:58:37 +0000565 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000566}
567
568bool
Chris Lattner0c332312006-07-28 22:29:50 +0000569Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
Reid Spencer8475ec02007-03-29 19:05:44 +0000570 const FileStatus *Status = getFileStatus(false, ErrStr);
571 if (!Status)
Reid Spencer30300992006-08-24 18:58:37 +0000572 return false;
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000573
Reid Spencer8475ec02007-03-29 19:05:44 +0000574 if (Status->isFile) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000575 DWORD attr = GetFileAttributes(path.c_str());
Reid Spencerb016a372004-09-15 05:49:50 +0000576
Reid Spencer1cf2d042005-07-07 23:35:23 +0000577 // If it doesn't exist, we're done.
578 if (attr == INVALID_FILE_ATTRIBUTES)
Reid Spencer30300992006-08-24 18:58:37 +0000579 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000580
Reid Spencer1cf2d042005-07-07 23:35:23 +0000581 // Read-only files cannot be deleted on Windows. Must remove the read-only
582 // attribute first.
583 if (attr & FILE_ATTRIBUTE_READONLY) {
584 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
Reid Spencer05545752006-08-25 21:37:17 +0000585 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000586 }
Jeff Cohen51b8d212004-12-31 19:01:08 +0000587
Reid Spencer1cf2d042005-07-07 23:35:23 +0000588 if (!DeleteFile(path.c_str()))
Reid Spencer30300992006-08-24 18:58:37 +0000589 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000590 return false;
Reid Spencer8475ec02007-03-29 19:05:44 +0000591 } else if (Status->isDir) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000592 // If it doesn't exist, we're done.
Jeff Cohen85c716f2005-07-08 05:02:13 +0000593 if (!exists())
Chris Lattner0c332312006-07-28 22:29:50 +0000594 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000595
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000596 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000597 int lastchar = path.length() - 1 ;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000598 path.copy(pathname, lastchar+1);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000599
600 // Make path end with '/*'.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000601 if (pathname[lastchar] != '/')
602 pathname[++lastchar] = '/';
Reid Spencer1cf2d042005-07-07 23:35:23 +0000603 pathname[lastchar+1] = '*';
604 pathname[lastchar+2] = 0;
605
606 if (remove_contents) {
607 WIN32_FIND_DATA fd;
608 HANDLE h = FindFirstFile(pathname, &fd);
609
610 // It's a bad idea to alter the contents of a directory while enumerating
611 // its contents. So build a list of its contents first, then destroy them.
612
613 if (h != INVALID_HANDLE_VALUE) {
614 std::vector<Path> list;
615
616 do {
617 if (strcmp(fd.cFileName, ".") == 0)
618 continue;
619 if (strcmp(fd.cFileName, "..") == 0)
620 continue;
621
Jeff Cohen85c716f2005-07-08 05:02:13 +0000622 Path aPath(path);
623 aPath.appendComponent(&fd.cFileName[0]);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000624 list.push_back(aPath);
625 } while (FindNextFile(h, &fd));
626
627 DWORD err = GetLastError();
628 FindClose(h);
629 if (err != ERROR_NO_MORE_FILES) {
630 SetLastError(err);
Reid Spencer05545752006-08-25 21:37:17 +0000631 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000632 }
633
Jeff Cohen85c716f2005-07-08 05:02:13 +0000634 for (std::vector<Path>::iterator I = list.begin(); I != list.end();
Reid Spencer1cf2d042005-07-07 23:35:23 +0000635 ++I) {
636 Path &aPath = *I;
Reid Spencera229c5c2005-07-08 03:08:58 +0000637 aPath.eraseFromDisk(true);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000638 }
639 } else {
640 if (GetLastError() != ERROR_FILE_NOT_FOUND)
Reid Spencer05545752006-08-25 21:37:17 +0000641 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000642 }
643 }
644
645 pathname[lastchar] = 0;
646 if (!RemoveDirectory(pathname))
Reid Spencer05545752006-08-25 21:37:17 +0000647 return MakeErrMsg(ErrStr,
648 std::string(pathname) + ": Can't destroy directory: ");
Chris Lattner0c332312006-07-28 22:29:50 +0000649 return false;
Reid Spencer30300992006-08-24 18:58:37 +0000650 }
651 // It appears the path doesn't exist.
652 return true;
Reid Spencerb016a372004-09-15 05:49:50 +0000653}
654
Reid Spencer3b0cc782004-12-14 18:42:13 +0000655bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
Reid Spencer3b0cc782004-12-14 18:42:13 +0000656 assert(len < 1024 && "Request for magic string too long");
657 char* buf = (char*) alloca(1 + len);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000658
659 HANDLE h = CreateFile(path.c_str(),
660 GENERIC_READ,
661 FILE_SHARE_READ,
662 NULL,
663 OPEN_EXISTING,
664 FILE_ATTRIBUTE_NORMAL,
665 NULL);
666 if (h == INVALID_HANDLE_VALUE)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000667 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000668
669 DWORD nRead = 0;
670 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
671 CloseHandle(h);
672
673 if (!ret || nRead != len)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000674 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000675
Reid Spencer3b0cc782004-12-14 18:42:13 +0000676 buf[len] = '\0';
677 Magic = buf;
678 return true;
679}
680
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000681bool
Reid Spencer5a060772006-08-23 07:30:48 +0000682Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
Jeff Cohene564de22006-05-07 02:51:51 +0000683 if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING))
Reid Spencer5a060772006-08-23 07:30:48 +0000684 return MakeErrMsg(ErrMsg, "Can't move '" + path + "' to '" + newName.path
685 + "': ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000686 return true;
687}
688
689bool
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000690Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrMsg) const {
Jeff Cohen966fa412005-07-09 18:42:49 +0000691 // FIXME: should work on directories also.
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000692 if (!si.isFile) {
693 return true;
694 }
695
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000696 HANDLE h = CreateFile(path.c_str(),
697 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
698 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000699 NULL,
700 OPEN_EXISTING,
701 FILE_ATTRIBUTE_NORMAL,
702 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000703 if (h == INVALID_HANDLE_VALUE)
Chris Lattner1bebfb52006-07-28 22:36:17 +0000704 return true;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000705
706 BY_HANDLE_FILE_INFORMATION bhfi;
707 if (!GetFileInformationByHandle(h, &bhfi)) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000708 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000709 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000710 SetLastError(err);
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000711 return MakeErrMsg(ErrMsg, path + ": GetFileInformationByHandle: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000712 }
713
714 FILETIME ft;
715 (uint64_t&)ft = si.modTime.toWin32Time();
716 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000717 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000718 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000719 if (!ret) {
720 SetLastError(err);
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000721 return MakeErrMsg(ErrMsg, path + ": SetFileTime: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000722 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000723
724 // Best we can do with Unix permission bits is to interpret the owner
725 // writable bit.
726 if (si.mode & 0200) {
727 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
728 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000729 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000730 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000731 }
732 } else {
733 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
734 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000735 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000736 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000737 }
738 }
739
Chris Lattner1bebfb52006-07-28 22:36:17 +0000740 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000741}
742
Reid Spencer30300992006-08-24 18:58:37 +0000743bool
744CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg) {
Jeff Cohencb652552004-12-24 02:38:34 +0000745 // Can't use CopyFile macro defined in Windows.h because it would mess up the
746 // above line. We use the expansion it would have in a non-UNICODE build.
747 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Reid Spencer30300992006-08-24 18:58:37 +0000748 return MakeErrMsg(ErrMsg, "Can't copy '" + Src.toString() +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000749 "' to '" + Dest.toString() + "': ");
Reid Spencer30300992006-08-24 18:58:37 +0000750 return false;
Reid Spencerc29befb2004-12-15 01:50:13 +0000751}
752
Reid Spencer30300992006-08-24 18:58:37 +0000753bool
754Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000755 if (reuse_current && !exists())
Reid Spencer30300992006-08-24 18:58:37 +0000756 return false; // File doesn't exist already, just use it!
Reid Spencerc29befb2004-12-15 01:50:13 +0000757
Jeff Cohen9437bb62005-01-27 03:49:03 +0000758 // Reserve space for -XXXXXX at the end.
759 char *FNBuffer = (char*) alloca(path.size()+8);
760 unsigned offset = path.size();
761 path.copy(FNBuffer, offset);
Reid Spencerc29befb2004-12-15 01:50:13 +0000762
Jeff Cohen966fa412005-07-09 18:42:49 +0000763 // Find a numeric suffix that isn't used by an existing file. Assume there
764 // won't be more than 1 million files with the same prefix. Probably a safe
765 // bet.
Jeff Cohen9437bb62005-01-27 03:49:03 +0000766 static unsigned FCounter = 0;
767 do {
768 sprintf(FNBuffer+offset, "-%06u", FCounter);
769 if (++FCounter > 999999)
770 FCounter = 0;
771 path = FNBuffer;
772 } while (exists());
Reid Spencer30300992006-08-24 18:58:37 +0000773 return false;
Reid Spencerc29befb2004-12-15 01:50:13 +0000774}
775
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000776bool
Reid Spencer30300992006-08-24 18:58:37 +0000777Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000778 // Make this into a unique file name
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000779 makeUnique(reuse_current, ErrMsg);
Jeff Cohen9437bb62005-01-27 03:49:03 +0000780
781 // Now go and create it
782 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
783 FILE_ATTRIBUTE_NORMAL, NULL);
784 if (h == INVALID_HANDLE_VALUE)
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000785 return MakeErrMsg(ErrMsg, path + ": can't create file");
Jeff Cohen9437bb62005-01-27 03:49:03 +0000786
787 CloseHandle(h);
Reid Spencer30300992006-08-24 18:58:37 +0000788 return false;
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000789}
790
Reid Spencerb016a372004-09-15 05:49:50 +0000791}
Reid Spencercbad7012004-09-11 04:59:30 +0000792}