blob: f5edaa07edbd98b6a4757f2cdf5bb1bb6284249b [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 {
110 if (path.length() < 3)
111 return false;
112 return path[0] == 'C' && path[1] == ':' && path[2] == '\\';
113}
114
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000115static Path *TempDirectory = NULL;
116
Reid Spencerb016a372004-09-15 05:49:50 +0000117Path
Reid Spencercab0e432006-08-22 22:46:39 +0000118Path::GetTemporaryDirectory(std::string* ErrMsg) {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000119 if (TempDirectory)
120 return *TempDirectory;
121
122 char pathname[MAX_PATH];
Reid Spencercab0e432006-08-22 22:46:39 +0000123 if (!GetTempPath(MAX_PATH, pathname)) {
124 if (ErrMsg)
125 *ErrMsg = "Can't determine temporary directory";
126 return Path();
127 }
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000128
Reid Spencerb016a372004-09-15 05:49:50 +0000129 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000130 result.set(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000131
132 // Append a subdirectory passed on our process id so multiple LLVMs don't
133 // step on each other's toes.
Jeff Cohend41b30d2006-11-05 19:31:28 +0000134#ifdef __MINGW32__
135 // Mingw's Win32 header files are broken.
Reid Spencerab4d9b02006-06-08 18:08:43 +0000136 sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId()));
Jeff Cohend41b30d2006-11-05 19:31:28 +0000137#else
138 sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
139#endif
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000140 result.appendComponent(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000141
142 // If there's a directory left over from a previous LLVM execution that
143 // happened to have the same process id, get rid of it.
Reid Spencera229c5c2005-07-08 03:08:58 +0000144 result.eraseFromDisk(true);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000145
146 // And finally (re-)create the empty directory.
Reid Spencera229c5c2005-07-08 03:08:58 +0000147 result.createDirectoryOnDisk(false);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000148 TempDirectory = new Path(result);
149 return *TempDirectory;
Reid Spencerb016a372004-09-15 05:49:50 +0000150}
151
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000152// FIXME: the following set of functions don't map to Windows very well.
Reid Spencerb016a372004-09-15 05:49:50 +0000153Path
154Path::GetRootDirectory() {
155 Path result;
Jeff Cohen966fa412005-07-09 18:42:49 +0000156 result.set("C:/");
Reid Spencerb016a372004-09-15 05:49:50 +0000157 return result;
158}
159
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000160static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
161 const char* at = path;
162 const char* delim = strchr(at, ';');
163 Path tmpPath;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000164 while (delim != 0) {
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000165 std::string tmp(at, size_t(delim-at));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000166 if (tmpPath.set(tmp))
Reid Spencerc7f08322005-07-07 18:21:42 +0000167 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000168 Paths.push_back(tmpPath);
169 at = delim + 1;
170 delim = strchr(at, ';');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000171 }
Jeff Cohen85c716f2005-07-08 05:02:13 +0000172
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000173 if (*at != 0)
Reid Spencer1cf2d042005-07-07 23:35:23 +0000174 if (tmpPath.set(std::string(at)))
Reid Spencerc7f08322005-07-07 18:21:42 +0000175 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000176 Paths.push_back(tmpPath);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000177}
178
Jeff Cohen85c716f2005-07-08 05:02:13 +0000179void
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000180Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000181 Paths.push_back(sys::Path("C:/WINDOWS/SYSTEM32"));
182 Paths.push_back(sys::Path("C:/WINDOWS"));
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000183}
184
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000185void
186Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
187 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
188 if (env_var != 0) {
189 getPathList(env_var,Paths);
190 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000191#ifdef LLVM_LIBDIR
192 {
193 Path tmpPath;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000194 if (tmpPath.set(LLVM_LIBDIR))
Reid Spencerc7f08322005-07-07 18:21:42 +0000195 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000196 Paths.push_back(tmpPath);
197 }
198#endif
199 GetSystemLibraryPaths(Paths);
Reid Spencerb016a372004-09-15 05:49:50 +0000200}
201
202Path
203Path::GetLLVMDefaultConfigDir() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000204 // TODO: this isn't going to fly on Windows
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000205 return Path("/etc/llvm");
Reid Spencerb016a372004-09-15 05:49:50 +0000206}
207
208Path
Reid Spencerb016a372004-09-15 05:49:50 +0000209Path::GetUserHomeDirectory() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000210 // TODO: Typical Windows setup doesn't define HOME.
Reid Spencerb016a372004-09-15 05:49:50 +0000211 const char* home = getenv("HOME");
212 if (home) {
213 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000214 if (result.set(home))
Reid Spencerb016a372004-09-15 05:49:50 +0000215 return result;
216 }
217 return GetRootDirectory();
218}
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000219// FIXME: the above set of functions don't map to Windows very well.
220
Jeff Cohen966fa412005-07-09 18:42:49 +0000221
222bool
223Path::isRootDirectory() const {
224 size_t len = path.size();
225 return len > 0 && path[len-1] == '/';
Reid Spencera229c5c2005-07-08 03:08:58 +0000226}
227
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000228std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000229Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000230 // Find the last slash
231 size_t slash = path.rfind('/');
232 if (slash == std::string::npos)
233 slash = 0;
234 else
235 slash++;
236
Jeff Cohen966fa412005-07-09 18:42:49 +0000237 size_t dot = path.rfind('.');
238 if (dot == std::string::npos || dot < slash)
239 return path.substr(slash);
240 else
241 return path.substr(slash, dot - slash);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000242}
243
Reid Spencer07adb282004-11-05 22:15:36 +0000244bool Path::hasMagicNumber(const std::string &Magic) const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000245 std::string actualMagic;
246 if (getMagicNumber(actualMagic, Magic.size()))
247 return Magic == actualMagic;
248 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000249}
250
Jeff Cohen85c716f2005-07-08 05:02:13 +0000251bool
Reid Spencer07adb282004-11-05 22:15:36 +0000252Path::isBytecodeFile() const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000253 std::string actualMagic;
254 if (!getMagicNumber(actualMagic, 4))
255 return false;
256 return actualMagic == "llvc" || actualMagic == "llvm";
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000257}
258
259bool
Reid Spencerb016a372004-09-15 05:49:50 +0000260Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000261 DWORD attr = GetFileAttributes(path.c_str());
262 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000263}
264
265bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000266Path::canRead() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000267 // FIXME: take security attributes into account.
268 DWORD attr = GetFileAttributes(path.c_str());
269 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000270}
271
272bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000273Path::canWrite() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000274 // FIXME: take security attributes into account.
275 DWORD attr = GetFileAttributes(path.c_str());
276 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000277}
278
279bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000280Path::canExecute() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000281 // FIXME: take security attributes into account.
282 DWORD attr = GetFileAttributes(path.c_str());
283 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000284}
285
286std::string
287Path::getLast() const {
288 // Find the last slash
289 size_t pos = path.rfind('/');
290
291 // Handle the corner cases
292 if (pos == std::string::npos)
293 return path;
294
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000295 // If the last character is a slash, we have a root directory
296 if (pos == path.length()-1)
297 return path;
298
Reid Spencerb016a372004-09-15 05:49:50 +0000299 // Return everything after the last slash
300 return path.substr(pos+1);
301}
302
Chris Lattner7dea1012006-07-28 22:32:09 +0000303bool
Reid Spencer69cce812007-03-29 16:43:20 +0000304Path::getFileStatus(FileStatus &info, bool update, std::string *ErrStr) const {
305 if (status == 0 || update) {
306 WIN32_FILE_ATTRIBUTE_DATA fi;
307 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
308 return MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) +
309 ": Can't get status: ");
Jeff Cohen626e38e2004-12-14 05:26:43 +0000310
Reid Spencer69cce812007-03-29 16:43:20 +0000311 if (status == 0)
312 status = new FileStatus;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000313
Reid Spencer69cce812007-03-29 16:43:20 +0000314 status->fileSize = fi.nFileSizeHigh;
315 status->fileSize <<= sizeof(fi.nFileSizeHigh)*8;
316 status->fileSize += fi.nFileSizeLow;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000317
Reid Spencer69cce812007-03-29 16:43:20 +0000318 status->mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
319 status->user = 9999; // Not applicable to Windows, so...
320 status->group = 9999; // Not applicable to Windows, so...
Jeff Cohen626e38e2004-12-14 05:26:43 +0000321
Reid Spencer4031bef2007-03-29 17:00:31 +0000322 // FIXME: this is only unique if the file is accessed by the same file path.
323 // How do we do this for C:\dir\file and ..\dir\file ? Unix has inode
324 // numbers, but the concept doesn't exist in Windows.
325 status->uniqueID = 0;
326 for (unsigned i = 0; i < path.length(); ++i)
327 status->uniqueID += path[i];
328
Reid Spencer69cce812007-03-29 16:43:20 +0000329 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
330 status->modTime.fromWin32Time(ft);
331
332 status->isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
333 }
334 info = *status;
Chris Lattner7dea1012006-07-28 22:32:09 +0000335 return false;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000336}
337
Reid Spencera34a1572006-08-22 23:54:35 +0000338bool Path::makeReadableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000339 // All files are readable on Windows (ignoring security attributes).
Reid Spencera34a1572006-08-22 23:54:35 +0000340 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000341}
342
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000343bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000344 DWORD attr = GetFileAttributes(path.c_str());
345
346 // If it doesn't exist, we're done.
347 if (attr == INVALID_FILE_ATTRIBUTES)
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000348 return false;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000349
350 if (attr & FILE_ATTRIBUTE_READONLY) {
Reid Spencera34a1572006-08-22 23:54:35 +0000351 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) {
352 MakeErrMsg(ErrMsg, std::string(path) + ": Can't make file writable: ");
353 return true;
354 }
Jeff Cohen626e38e2004-12-14 05:26:43 +0000355 }
Reid Spencera34a1572006-08-22 23:54:35 +0000356 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000357}
358
Reid Spencera34a1572006-08-22 23:54:35 +0000359bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000360 // All files are executable on Windows (ignoring security attributes).
Reid Spencera34a1572006-08-22 23:54:35 +0000361 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000362}
363
Reid Spencerb016a372004-09-15 05:49:50 +0000364bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000365Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000366 FileStatus Status;
367 if (getFileStatus(Status, ErrMsg))
368 return true;
369
370 if (!Status.isDir) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000371 MakeErrMsg(ErrMsg, path + ": not a directory");
372 return true;
373 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000374
375 result.clear();
376 WIN32_FIND_DATA fd;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000377 std::string searchpath = path;
378 if (path.size() == 0 || searchpath[path.size()-1] == '/')
Jeff Cohen85c716f2005-07-08 05:02:13 +0000379 searchpath += "*";
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000380 else
381 searchpath += "/*";
Jeff Cohen85c716f2005-07-08 05:02:13 +0000382
Jeff Cohen9437bb62005-01-27 03:49:03 +0000383 HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000384 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohen9437bb62005-01-27 03:49:03 +0000385 if (GetLastError() == ERROR_FILE_NOT_FOUND)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000386 return true; // not really an error, now is it?
Reid Spencer142ca8e2006-08-23 06:56:27 +0000387 MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
388 return true;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000389 }
390
391 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000392 if (fd.cFileName[0] == '.')
393 continue;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000394 Path aPath(path);
395 aPath.appendComponent(&fd.cFileName[0]);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000396 result.insert(aPath);
397 } while (FindNextFile(h, &fd));
398
Jeff Cohen51b8d212004-12-31 19:01:08 +0000399 DWORD err = GetLastError();
400 FindClose(h);
401 if (err != ERROR_NO_MORE_FILES) {
402 SetLastError(err);
Reid Spencer142ca8e2006-08-23 06:56:27 +0000403 MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
404 return true;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000405 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000406 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000407}
408
409bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000410Path::set(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000411 if (a_path.size() == 0)
412 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000413 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000414 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000415 FlipBackSlashes(path);
Reid Spencer07adb282004-11-05 22:15:36 +0000416 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000417 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000418 return false;
419 }
420 return true;
421}
422
423bool
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000424Path::appendComponent(const std::string& name) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000425 if (name.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000426 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000427 std::string save(path);
428 if (!path.empty()) {
429 size_t last = path.size() - 1;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000430 if (path[last] != '/')
Reid Spencer1cf2d042005-07-07 23:35:23 +0000431 path += '/';
432 }
433 path += name;
Reid Spencer07adb282004-11-05 22:15:36 +0000434 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000435 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000436 return false;
437 }
438 return true;
439}
440
441bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000442Path::eraseComponent() {
Reid Spencerb016a372004-09-15 05:49:50 +0000443 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000444 if (slashpos == path.size() - 1 || slashpos == std::string::npos)
Reid Spencerb016a372004-09-15 05:49:50 +0000445 return false;
Jeff Cohen966fa412005-07-09 18:42:49 +0000446 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000447 path.erase(slashpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000448 if (!isValid()) {
449 path = save;
450 return false;
451 }
Reid Spencerb016a372004-09-15 05:49:50 +0000452 return true;
453}
454
455bool
Reid Spencer07adb282004-11-05 22:15:36 +0000456Path::appendSuffix(const std::string& suffix) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000457 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000458 path.append(".");
459 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000460 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000461 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000462 return false;
463 }
464 return true;
465}
466
467bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000468Path::eraseSuffix() {
Reid Spencerb016a372004-09-15 05:49:50 +0000469 size_t dotpos = path.rfind('.',path.size());
470 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000471 if (dotpos != std::string::npos) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000472 if (slashpos == std::string::npos || dotpos > slashpos+1) {
473 std::string save(path);
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000474 path.erase(dotpos, path.size()-dotpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000475 if (!isValid()) {
476 path = save;
477 return false;
478 }
Jeff Cohen85c716f2005-07-08 05:02:13 +0000479 return true;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000480 }
Reid Spencerb016a372004-09-15 05:49:50 +0000481 }
482 return false;
483}
484
Reid Spencer30300992006-08-24 18:58:37 +0000485inline bool PathMsg(std::string* ErrMsg, const char* pathname, const char*msg) {
486 if (ErrMsg)
487 *ErrMsg = std::string(pathname) + ": " + std::string(msg);
488 return true;
489}
490
Reid Spencerb016a372004-09-15 05:49:50 +0000491bool
Reid Spencer30300992006-08-24 18:58:37 +0000492Path::createDirectoryOnDisk(bool create_parents, std::string* ErrMsg) {
Reid Spencerb016a372004-09-15 05:49:50 +0000493 // Get a writeable copy of the path name
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000494 size_t len = path.length();
495 char *pathname = reinterpret_cast<char *>(_alloca(len+2));
496 path.copy(pathname, len);
497 pathname[len] = 0;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000498
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000499 // Make sure it ends with a slash.
500 if (len == 0 || pathname[len - 1] != '/') {
501 pathname[len] = '/';
502 pathname[++len] = 0;
503 }
Reid Spencerb016a372004-09-15 05:49:50 +0000504
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000505 // Determine starting point for initial / search.
506 char *next = pathname;
507 if (pathname[0] == '/' && pathname[1] == '/') {
508 // Skip host name.
509 next = strchr(pathname+2, '/');
510 if (next == NULL)
Reid Spencer30300992006-08-24 18:58:37 +0000511 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
512
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000513 // Skip share name.
514 next = strchr(next+1, '/');
515 if (next == NULL)
Reid Spencer30300992006-08-24 18:58:37 +0000516 return PathMsg(ErrMsg, pathname,"badly formed remote directory");
517
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000518 next++;
519 if (*next == 0)
Reid Spencer30300992006-08-24 18:58:37 +0000520 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
521
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000522 } else {
523 if (pathname[1] == ':')
524 next += 2; // skip drive letter
525 if (*next == '/')
526 next++; // skip root directory
527 }
Reid Spencerb016a372004-09-15 05:49:50 +0000528
529 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000530 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000531 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000532 while (*next) {
533 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000534 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000535 if (!CreateDirectory(pathname, NULL))
Reid Spencer30300992006-08-24 18:58:37 +0000536 return MakeErrMsg(ErrMsg,
537 std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000538 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000539 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000540 } else {
541 // Drop trailing slash.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000542 pathname[len-1] = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000543 if (!CreateDirectory(pathname, NULL)) {
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000544 return MakeErrMsg(ErrMsg, std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000545 }
Reid Spencerb016a372004-09-15 05:49:50 +0000546 }
Reid Spencer30300992006-08-24 18:58:37 +0000547 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000548}
549
550bool
Reid Spencer30300992006-08-24 18:58:37 +0000551Path::createFileOnDisk(std::string* ErrMsg) {
Reid Spencerb016a372004-09-15 05:49:50 +0000552 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000553 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000554 FILE_ATTRIBUTE_NORMAL, NULL);
555 if (h == INVALID_HANDLE_VALUE)
Reid Spencer30300992006-08-24 18:58:37 +0000556 return MakeErrMsg(ErrMsg, path + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000557
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000558 CloseHandle(h);
Reid Spencer30300992006-08-24 18:58:37 +0000559 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000560}
561
562bool
Chris Lattner0c332312006-07-28 22:29:50 +0000563Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000564 FileStatus Status;
565 if (getFileStatus(Status, ErrStr))
Reid Spencer30300992006-08-24 18:58:37 +0000566 return false;
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000567
568 if (Status.isFile) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000569 DWORD attr = GetFileAttributes(path.c_str());
Reid Spencerb016a372004-09-15 05:49:50 +0000570
Reid Spencer1cf2d042005-07-07 23:35:23 +0000571 // If it doesn't exist, we're done.
572 if (attr == INVALID_FILE_ATTRIBUTES)
Reid Spencer30300992006-08-24 18:58:37 +0000573 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000574
Reid Spencer1cf2d042005-07-07 23:35:23 +0000575 // Read-only files cannot be deleted on Windows. Must remove the read-only
576 // attribute first.
577 if (attr & FILE_ATTRIBUTE_READONLY) {
578 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
Reid Spencer05545752006-08-25 21:37:17 +0000579 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000580 }
Jeff Cohen51b8d212004-12-31 19:01:08 +0000581
Reid Spencer1cf2d042005-07-07 23:35:23 +0000582 if (!DeleteFile(path.c_str()))
Reid Spencer30300992006-08-24 18:58:37 +0000583 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000584 return false;
585 } else if (Status.isDir) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000586 // If it doesn't exist, we're done.
Jeff Cohen85c716f2005-07-08 05:02:13 +0000587 if (!exists())
Chris Lattner0c332312006-07-28 22:29:50 +0000588 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000589
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000590 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000591 int lastchar = path.length() - 1 ;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000592 path.copy(pathname, lastchar+1);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000593
594 // Make path end with '/*'.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000595 if (pathname[lastchar] != '/')
596 pathname[++lastchar] = '/';
Reid Spencer1cf2d042005-07-07 23:35:23 +0000597 pathname[lastchar+1] = '*';
598 pathname[lastchar+2] = 0;
599
600 if (remove_contents) {
601 WIN32_FIND_DATA fd;
602 HANDLE h = FindFirstFile(pathname, &fd);
603
604 // It's a bad idea to alter the contents of a directory while enumerating
605 // its contents. So build a list of its contents first, then destroy them.
606
607 if (h != INVALID_HANDLE_VALUE) {
608 std::vector<Path> list;
609
610 do {
611 if (strcmp(fd.cFileName, ".") == 0)
612 continue;
613 if (strcmp(fd.cFileName, "..") == 0)
614 continue;
615
Jeff Cohen85c716f2005-07-08 05:02:13 +0000616 Path aPath(path);
617 aPath.appendComponent(&fd.cFileName[0]);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000618 list.push_back(aPath);
619 } while (FindNextFile(h, &fd));
620
621 DWORD err = GetLastError();
622 FindClose(h);
623 if (err != ERROR_NO_MORE_FILES) {
624 SetLastError(err);
Reid Spencer05545752006-08-25 21:37:17 +0000625 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000626 }
627
Jeff Cohen85c716f2005-07-08 05:02:13 +0000628 for (std::vector<Path>::iterator I = list.begin(); I != list.end();
Reid Spencer1cf2d042005-07-07 23:35:23 +0000629 ++I) {
630 Path &aPath = *I;
Reid Spencera229c5c2005-07-08 03:08:58 +0000631 aPath.eraseFromDisk(true);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000632 }
633 } else {
634 if (GetLastError() != ERROR_FILE_NOT_FOUND)
Reid Spencer05545752006-08-25 21:37:17 +0000635 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000636 }
637 }
638
639 pathname[lastchar] = 0;
640 if (!RemoveDirectory(pathname))
Reid Spencer05545752006-08-25 21:37:17 +0000641 return MakeErrMsg(ErrStr,
642 std::string(pathname) + ": Can't destroy directory: ");
Chris Lattner0c332312006-07-28 22:29:50 +0000643 return false;
Reid Spencer30300992006-08-24 18:58:37 +0000644 }
645 // It appears the path doesn't exist.
646 return true;
Reid Spencerb016a372004-09-15 05:49:50 +0000647}
648
Reid Spencer3b0cc782004-12-14 18:42:13 +0000649bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
Reid Spencer3b0cc782004-12-14 18:42:13 +0000650 assert(len < 1024 && "Request for magic string too long");
651 char* buf = (char*) alloca(1 + len);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000652
653 HANDLE h = CreateFile(path.c_str(),
654 GENERIC_READ,
655 FILE_SHARE_READ,
656 NULL,
657 OPEN_EXISTING,
658 FILE_ATTRIBUTE_NORMAL,
659 NULL);
660 if (h == INVALID_HANDLE_VALUE)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000661 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000662
663 DWORD nRead = 0;
664 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
665 CloseHandle(h);
666
667 if (!ret || nRead != len)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000668 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000669
Reid Spencer3b0cc782004-12-14 18:42:13 +0000670 buf[len] = '\0';
671 Magic = buf;
672 return true;
673}
674
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000675bool
Reid Spencer5a060772006-08-23 07:30:48 +0000676Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
Jeff Cohene564de22006-05-07 02:51:51 +0000677 if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING))
Reid Spencer5a060772006-08-23 07:30:48 +0000678 return MakeErrMsg(ErrMsg, "Can't move '" + path + "' to '" + newName.path
679 + "': ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000680 return true;
681}
682
683bool
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000684Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrMsg) const {
Jeff Cohen966fa412005-07-09 18:42:49 +0000685 // FIXME: should work on directories also.
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000686 if (!si.isFile) {
687 return true;
688 }
689
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000690 HANDLE h = CreateFile(path.c_str(),
691 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
692 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000693 NULL,
694 OPEN_EXISTING,
695 FILE_ATTRIBUTE_NORMAL,
696 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000697 if (h == INVALID_HANDLE_VALUE)
Chris Lattner1bebfb52006-07-28 22:36:17 +0000698 return true;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000699
700 BY_HANDLE_FILE_INFORMATION bhfi;
701 if (!GetFileInformationByHandle(h, &bhfi)) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000702 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000703 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000704 SetLastError(err);
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000705 return MakeErrMsg(ErrMsg, path + ": GetFileInformationByHandle: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000706 }
707
708 FILETIME ft;
709 (uint64_t&)ft = si.modTime.toWin32Time();
710 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000711 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000712 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000713 if (!ret) {
714 SetLastError(err);
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000715 return MakeErrMsg(ErrMsg, path + ": SetFileTime: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000716 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000717
718 // Best we can do with Unix permission bits is to interpret the owner
719 // writable bit.
720 if (si.mode & 0200) {
721 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
722 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000723 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000724 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000725 }
726 } else {
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 }
733
Chris Lattner1bebfb52006-07-28 22:36:17 +0000734 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000735}
736
Reid Spencer30300992006-08-24 18:58:37 +0000737bool
738CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg) {
Jeff Cohencb652552004-12-24 02:38:34 +0000739 // Can't use CopyFile macro defined in Windows.h because it would mess up the
740 // above line. We use the expansion it would have in a non-UNICODE build.
741 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Reid Spencer30300992006-08-24 18:58:37 +0000742 return MakeErrMsg(ErrMsg, "Can't copy '" + Src.toString() +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000743 "' to '" + Dest.toString() + "': ");
Reid Spencer30300992006-08-24 18:58:37 +0000744 return false;
Reid Spencerc29befb2004-12-15 01:50:13 +0000745}
746
Reid Spencer30300992006-08-24 18:58:37 +0000747bool
748Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000749 if (reuse_current && !exists())
Reid Spencer30300992006-08-24 18:58:37 +0000750 return false; // File doesn't exist already, just use it!
Reid Spencerc29befb2004-12-15 01:50:13 +0000751
Jeff Cohen9437bb62005-01-27 03:49:03 +0000752 // Reserve space for -XXXXXX at the end.
753 char *FNBuffer = (char*) alloca(path.size()+8);
754 unsigned offset = path.size();
755 path.copy(FNBuffer, offset);
Reid Spencerc29befb2004-12-15 01:50:13 +0000756
Jeff Cohen966fa412005-07-09 18:42:49 +0000757 // Find a numeric suffix that isn't used by an existing file. Assume there
758 // won't be more than 1 million files with the same prefix. Probably a safe
759 // bet.
Jeff Cohen9437bb62005-01-27 03:49:03 +0000760 static unsigned FCounter = 0;
761 do {
762 sprintf(FNBuffer+offset, "-%06u", FCounter);
763 if (++FCounter > 999999)
764 FCounter = 0;
765 path = FNBuffer;
766 } while (exists());
Reid Spencer30300992006-08-24 18:58:37 +0000767 return false;
Reid Spencerc29befb2004-12-15 01:50:13 +0000768}
769
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000770bool
Reid Spencer30300992006-08-24 18:58:37 +0000771Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000772 // Make this into a unique file name
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000773 makeUnique(reuse_current, ErrMsg);
Jeff Cohen9437bb62005-01-27 03:49:03 +0000774
775 // Now go and create it
776 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
777 FILE_ATTRIBUTE_NORMAL, NULL);
778 if (h == INVALID_HANDLE_VALUE)
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000779 return MakeErrMsg(ErrMsg, path + ": can't create file");
Jeff Cohen9437bb62005-01-27 03:49:03 +0000780
781 CloseHandle(h);
Reid Spencer30300992006-08-24 18:58:37 +0000782 return false;
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000783}
784
Reid Spencerb016a372004-09-15 05:49:50 +0000785}
Reid Spencercbad7012004-09-11 04:59:30 +0000786}