blob: 1eee2bb3c1f795390dca1acc518e39267a531a9e [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 Spencerd0c9e0e2004-09-18 19:29:16 +0000108static Path *TempDirectory = NULL;
109
Reid Spencerb016a372004-09-15 05:49:50 +0000110Path
Reid Spencercab0e432006-08-22 22:46:39 +0000111Path::GetTemporaryDirectory(std::string* ErrMsg) {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000112 if (TempDirectory)
113 return *TempDirectory;
114
115 char pathname[MAX_PATH];
Reid Spencercab0e432006-08-22 22:46:39 +0000116 if (!GetTempPath(MAX_PATH, pathname)) {
117 if (ErrMsg)
118 *ErrMsg = "Can't determine temporary directory";
119 return Path();
120 }
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000121
Reid Spencerb016a372004-09-15 05:49:50 +0000122 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000123 result.set(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000124
125 // Append a subdirectory passed on our process id so multiple LLVMs don't
126 // step on each other's toes.
Jeff Cohend41b30d2006-11-05 19:31:28 +0000127#ifdef __MINGW32__
128 // Mingw's Win32 header files are broken.
Reid Spencerab4d9b02006-06-08 18:08:43 +0000129 sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId()));
Jeff Cohend41b30d2006-11-05 19:31:28 +0000130#else
131 sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
132#endif
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000133 result.appendComponent(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000134
135 // If there's a directory left over from a previous LLVM execution that
136 // happened to have the same process id, get rid of it.
Reid Spencera229c5c2005-07-08 03:08:58 +0000137 result.eraseFromDisk(true);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000138
139 // And finally (re-)create the empty directory.
Reid Spencera229c5c2005-07-08 03:08:58 +0000140 result.createDirectoryOnDisk(false);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000141 TempDirectory = new Path(result);
142 return *TempDirectory;
Reid Spencerb016a372004-09-15 05:49:50 +0000143}
144
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000145// FIXME: the following set of functions don't map to Windows very well.
Reid Spencerb016a372004-09-15 05:49:50 +0000146Path
147Path::GetRootDirectory() {
148 Path result;
Jeff Cohen966fa412005-07-09 18:42:49 +0000149 result.set("C:/");
Reid Spencerb016a372004-09-15 05:49:50 +0000150 return result;
151}
152
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000153static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
154 const char* at = path;
155 const char* delim = strchr(at, ';');
156 Path tmpPath;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000157 while (delim != 0) {
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000158 std::string tmp(at, size_t(delim-at));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000159 if (tmpPath.set(tmp))
Reid Spencerc7f08322005-07-07 18:21:42 +0000160 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000161 Paths.push_back(tmpPath);
162 at = delim + 1;
163 delim = strchr(at, ';');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000164 }
Jeff Cohen85c716f2005-07-08 05:02:13 +0000165
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000166 if (*at != 0)
Reid Spencer1cf2d042005-07-07 23:35:23 +0000167 if (tmpPath.set(std::string(at)))
Reid Spencerc7f08322005-07-07 18:21:42 +0000168 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000169 Paths.push_back(tmpPath);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000170}
171
Jeff Cohen85c716f2005-07-08 05:02:13 +0000172void
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000173Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000174 Paths.push_back(sys::Path("C:/WINDOWS/SYSTEM32"));
175 Paths.push_back(sys::Path("C:/WINDOWS"));
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000176}
177
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000178void
179Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
180 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
181 if (env_var != 0) {
182 getPathList(env_var,Paths);
183 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000184#ifdef LLVM_LIBDIR
185 {
186 Path tmpPath;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000187 if (tmpPath.set(LLVM_LIBDIR))
Reid Spencerc7f08322005-07-07 18:21:42 +0000188 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000189 Paths.push_back(tmpPath);
190 }
191#endif
192 GetSystemLibraryPaths(Paths);
Reid Spencerb016a372004-09-15 05:49:50 +0000193}
194
195Path
196Path::GetLLVMDefaultConfigDir() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000197 // TODO: this isn't going to fly on Windows
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000198 return Path("/etc/llvm");
Reid Spencerb016a372004-09-15 05:49:50 +0000199}
200
201Path
Reid Spencerb016a372004-09-15 05:49:50 +0000202Path::GetUserHomeDirectory() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000203 // TODO: Typical Windows setup doesn't define HOME.
Reid Spencerb016a372004-09-15 05:49:50 +0000204 const char* home = getenv("HOME");
205 if (home) {
206 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000207 if (result.set(home))
Reid Spencerb016a372004-09-15 05:49:50 +0000208 return result;
209 }
210 return GetRootDirectory();
211}
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000212// FIXME: the above set of functions don't map to Windows very well.
213
Jeff Cohen966fa412005-07-09 18:42:49 +0000214
215bool
216Path::isRootDirectory() const {
217 size_t len = path.size();
218 return len > 0 && path[len-1] == '/';
Reid Spencera229c5c2005-07-08 03:08:58 +0000219}
220
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000221std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000222Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000223 // Find the last slash
224 size_t slash = path.rfind('/');
225 if (slash == std::string::npos)
226 slash = 0;
227 else
228 slash++;
229
Jeff Cohen966fa412005-07-09 18:42:49 +0000230 size_t dot = path.rfind('.');
231 if (dot == std::string::npos || dot < slash)
232 return path.substr(slash);
233 else
234 return path.substr(slash, dot - slash);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000235}
236
Reid Spencer07adb282004-11-05 22:15:36 +0000237bool Path::hasMagicNumber(const std::string &Magic) const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000238 std::string actualMagic;
239 if (getMagicNumber(actualMagic, Magic.size()))
240 return Magic == actualMagic;
241 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000242}
243
Jeff Cohen85c716f2005-07-08 05:02:13 +0000244bool
Reid Spencer07adb282004-11-05 22:15:36 +0000245Path::isBytecodeFile() const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000246 std::string actualMagic;
247 if (!getMagicNumber(actualMagic, 4))
248 return false;
249 return actualMagic == "llvc" || actualMagic == "llvm";
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000250}
251
252bool
Reid Spencerb016a372004-09-15 05:49:50 +0000253Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000254 DWORD attr = GetFileAttributes(path.c_str());
255 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000256}
257
258bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000259Path::canRead() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000260 // FIXME: take security attributes into account.
261 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::canWrite() 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) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000270}
271
272bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000273Path::canExecute() 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;
Reid Spencerb016a372004-09-15 05:49:50 +0000277}
278
279std::string
280Path::getLast() const {
281 // Find the last slash
282 size_t pos = path.rfind('/');
283
284 // Handle the corner cases
285 if (pos == std::string::npos)
286 return path;
287
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000288 // If the last character is a slash, we have a root directory
289 if (pos == path.length()-1)
290 return path;
291
Reid Spencerb016a372004-09-15 05:49:50 +0000292 // Return everything after the last slash
293 return path.substr(pos+1);
294}
295
Chris Lattner7dea1012006-07-28 22:32:09 +0000296bool
Anton Korobeynikov1a7d3262006-08-01 08:07:22 +0000297Path::getFileStatus(FileStatus &info, std::string *ErrStr) const {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000298 WIN32_FILE_ATTRIBUTE_DATA fi;
299 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
Reid Spencer05545752006-08-25 21:37:17 +0000300 return MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) +
301 ": Can't get status: ");
Jeff Cohen626e38e2004-12-14 05:26:43 +0000302
303 info.fileSize = fi.nFileSizeHigh;
Reid Spencer393830a2006-06-08 17:00:08 +0000304 info.fileSize <<= sizeof(fi.nFileSizeHigh)*8;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000305 info.fileSize += fi.nFileSizeLow;
306
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000307 info.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000308 info.user = 9999; // Not applicable to Windows, so...
309 info.group = 9999; // Not applicable to Windows, so...
310
311 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
312 info.modTime.fromWin32Time(ft);
313
314 info.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Chris Lattner7dea1012006-07-28 22:32:09 +0000315 return false;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000316}
317
Reid Spencera34a1572006-08-22 23:54:35 +0000318bool Path::makeReadableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000319 // All files are readable on Windows (ignoring security attributes).
Reid Spencera34a1572006-08-22 23:54:35 +0000320 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000321}
322
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000323bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000324 DWORD attr = GetFileAttributes(path.c_str());
325
326 // If it doesn't exist, we're done.
327 if (attr == INVALID_FILE_ATTRIBUTES)
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000328 return false;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000329
330 if (attr & FILE_ATTRIBUTE_READONLY) {
Reid Spencera34a1572006-08-22 23:54:35 +0000331 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) {
332 MakeErrMsg(ErrMsg, std::string(path) + ": Can't make file writable: ");
333 return true;
334 }
Jeff Cohen626e38e2004-12-14 05:26:43 +0000335 }
Reid Spencera34a1572006-08-22 23:54:35 +0000336 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000337}
338
Reid Spencera34a1572006-08-22 23:54:35 +0000339bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000340 // All files are executable on Windows (ignoring security attributes).
Reid Spencera34a1572006-08-22 23:54:35 +0000341 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000342}
343
Reid Spencerb016a372004-09-15 05:49:50 +0000344bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000345Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000346 FileStatus Status;
347 if (getFileStatus(Status, ErrMsg))
348 return true;
349
350 if (!Status.isDir) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000351 MakeErrMsg(ErrMsg, path + ": not a directory");
352 return true;
353 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000354
355 result.clear();
356 WIN32_FIND_DATA fd;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000357 std::string searchpath = path;
358 if (path.size() == 0 || searchpath[path.size()-1] == '/')
Jeff Cohen85c716f2005-07-08 05:02:13 +0000359 searchpath += "*";
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000360 else
361 searchpath += "/*";
Jeff Cohen85c716f2005-07-08 05:02:13 +0000362
Jeff Cohen9437bb62005-01-27 03:49:03 +0000363 HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000364 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohen9437bb62005-01-27 03:49:03 +0000365 if (GetLastError() == ERROR_FILE_NOT_FOUND)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000366 return true; // not really an error, now is it?
Reid Spencer142ca8e2006-08-23 06:56:27 +0000367 MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
368 return true;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000369 }
370
371 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000372 if (fd.cFileName[0] == '.')
373 continue;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000374 Path aPath(path);
375 aPath.appendComponent(&fd.cFileName[0]);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000376 result.insert(aPath);
377 } while (FindNextFile(h, &fd));
378
Jeff Cohen51b8d212004-12-31 19:01:08 +0000379 DWORD err = GetLastError();
380 FindClose(h);
381 if (err != ERROR_NO_MORE_FILES) {
382 SetLastError(err);
Reid Spencer142ca8e2006-08-23 06:56:27 +0000383 MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
384 return true;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000385 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000386 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000387}
388
389bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000390Path::set(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000391 if (a_path.size() == 0)
392 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000393 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000394 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000395 FlipBackSlashes(path);
Reid Spencer07adb282004-11-05 22:15:36 +0000396 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000397 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000398 return false;
399 }
400 return true;
401}
402
403bool
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000404Path::appendComponent(const std::string& name) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000405 if (name.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000406 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000407 std::string save(path);
408 if (!path.empty()) {
409 size_t last = path.size() - 1;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000410 if (path[last] != '/')
Reid Spencer1cf2d042005-07-07 23:35:23 +0000411 path += '/';
412 }
413 path += name;
Reid Spencer07adb282004-11-05 22:15:36 +0000414 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000415 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000416 return false;
417 }
418 return true;
419}
420
421bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000422Path::eraseComponent() {
Reid Spencerb016a372004-09-15 05:49:50 +0000423 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000424 if (slashpos == path.size() - 1 || slashpos == std::string::npos)
Reid Spencerb016a372004-09-15 05:49:50 +0000425 return false;
Jeff Cohen966fa412005-07-09 18:42:49 +0000426 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000427 path.erase(slashpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000428 if (!isValid()) {
429 path = save;
430 return false;
431 }
Reid Spencerb016a372004-09-15 05:49:50 +0000432 return true;
433}
434
435bool
Reid Spencer07adb282004-11-05 22:15:36 +0000436Path::appendSuffix(const std::string& suffix) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000437 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000438 path.append(".");
439 path.append(suffix);
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::eraseSuffix() {
Reid Spencerb016a372004-09-15 05:49:50 +0000449 size_t dotpos = path.rfind('.',path.size());
450 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000451 if (dotpos != std::string::npos) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000452 if (slashpos == std::string::npos || dotpos > slashpos+1) {
453 std::string save(path);
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000454 path.erase(dotpos, path.size()-dotpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000455 if (!isValid()) {
456 path = save;
457 return false;
458 }
Jeff Cohen85c716f2005-07-08 05:02:13 +0000459 return true;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000460 }
Reid Spencerb016a372004-09-15 05:49:50 +0000461 }
462 return false;
463}
464
Reid Spencer30300992006-08-24 18:58:37 +0000465inline bool PathMsg(std::string* ErrMsg, const char* pathname, const char*msg) {
466 if (ErrMsg)
467 *ErrMsg = std::string(pathname) + ": " + std::string(msg);
468 return true;
469}
470
Reid Spencerb016a372004-09-15 05:49:50 +0000471bool
Reid Spencer30300992006-08-24 18:58:37 +0000472Path::createDirectoryOnDisk(bool create_parents, std::string* ErrMsg) {
Reid Spencerb016a372004-09-15 05:49:50 +0000473 // Get a writeable copy of the path name
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000474 size_t len = path.length();
475 char *pathname = reinterpret_cast<char *>(_alloca(len+2));
476 path.copy(pathname, len);
477 pathname[len] = 0;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000478
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000479 // Make sure it ends with a slash.
480 if (len == 0 || pathname[len - 1] != '/') {
481 pathname[len] = '/';
482 pathname[++len] = 0;
483 }
Reid Spencerb016a372004-09-15 05:49:50 +0000484
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000485 // Determine starting point for initial / search.
486 char *next = pathname;
487 if (pathname[0] == '/' && pathname[1] == '/') {
488 // Skip host name.
489 next = strchr(pathname+2, '/');
490 if (next == NULL)
Reid Spencer30300992006-08-24 18:58:37 +0000491 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
492
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000493 // Skip share name.
494 next = strchr(next+1, '/');
495 if (next == NULL)
Reid Spencer30300992006-08-24 18:58:37 +0000496 return PathMsg(ErrMsg, pathname,"badly formed remote directory");
497
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000498 next++;
499 if (*next == 0)
Reid Spencer30300992006-08-24 18:58:37 +0000500 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
501
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000502 } else {
503 if (pathname[1] == ':')
504 next += 2; // skip drive letter
505 if (*next == '/')
506 next++; // skip root directory
507 }
Reid Spencerb016a372004-09-15 05:49:50 +0000508
509 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000510 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000511 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000512 while (*next) {
513 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000514 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000515 if (!CreateDirectory(pathname, NULL))
Reid Spencer30300992006-08-24 18:58:37 +0000516 return MakeErrMsg(ErrMsg,
517 std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000518 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000519 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000520 } else {
521 // Drop trailing slash.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000522 pathname[len-1] = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000523 if (!CreateDirectory(pathname, NULL)) {
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000524 return MakeErrMsg(ErrMsg, std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000525 }
Reid Spencerb016a372004-09-15 05:49:50 +0000526 }
Reid Spencer30300992006-08-24 18:58:37 +0000527 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000528}
529
530bool
Reid Spencer30300992006-08-24 18:58:37 +0000531Path::createFileOnDisk(std::string* ErrMsg) {
Reid Spencerb016a372004-09-15 05:49:50 +0000532 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000533 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000534 FILE_ATTRIBUTE_NORMAL, NULL);
535 if (h == INVALID_HANDLE_VALUE)
Reid Spencer30300992006-08-24 18:58:37 +0000536 return MakeErrMsg(ErrMsg, path + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000537
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000538 CloseHandle(h);
Reid Spencer30300992006-08-24 18:58:37 +0000539 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000540}
541
542bool
Chris Lattner0c332312006-07-28 22:29:50 +0000543Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000544 FileStatus Status;
545 if (getFileStatus(Status, ErrStr))
Reid Spencer30300992006-08-24 18:58:37 +0000546 return false;
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000547
548 if (Status.isFile) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000549 DWORD attr = GetFileAttributes(path.c_str());
Reid Spencerb016a372004-09-15 05:49:50 +0000550
Reid Spencer1cf2d042005-07-07 23:35:23 +0000551 // If it doesn't exist, we're done.
552 if (attr == INVALID_FILE_ATTRIBUTES)
Reid Spencer30300992006-08-24 18:58:37 +0000553 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000554
Reid Spencer1cf2d042005-07-07 23:35:23 +0000555 // Read-only files cannot be deleted on Windows. Must remove the read-only
556 // attribute first.
557 if (attr & FILE_ATTRIBUTE_READONLY) {
558 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
Reid Spencer05545752006-08-25 21:37:17 +0000559 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000560 }
Jeff Cohen51b8d212004-12-31 19:01:08 +0000561
Reid Spencer1cf2d042005-07-07 23:35:23 +0000562 if (!DeleteFile(path.c_str()))
Reid Spencer30300992006-08-24 18:58:37 +0000563 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000564 return false;
565 } else if (Status.isDir) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000566 // If it doesn't exist, we're done.
Jeff Cohen85c716f2005-07-08 05:02:13 +0000567 if (!exists())
Chris Lattner0c332312006-07-28 22:29:50 +0000568 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000569
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000570 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000571 int lastchar = path.length() - 1 ;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000572 path.copy(pathname, lastchar+1);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000573
574 // Make path end with '/*'.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000575 if (pathname[lastchar] != '/')
576 pathname[++lastchar] = '/';
Reid Spencer1cf2d042005-07-07 23:35:23 +0000577 pathname[lastchar+1] = '*';
578 pathname[lastchar+2] = 0;
579
580 if (remove_contents) {
581 WIN32_FIND_DATA fd;
582 HANDLE h = FindFirstFile(pathname, &fd);
583
584 // It's a bad idea to alter the contents of a directory while enumerating
585 // its contents. So build a list of its contents first, then destroy them.
586
587 if (h != INVALID_HANDLE_VALUE) {
588 std::vector<Path> list;
589
590 do {
591 if (strcmp(fd.cFileName, ".") == 0)
592 continue;
593 if (strcmp(fd.cFileName, "..") == 0)
594 continue;
595
Jeff Cohen85c716f2005-07-08 05:02:13 +0000596 Path aPath(path);
597 aPath.appendComponent(&fd.cFileName[0]);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000598 list.push_back(aPath);
599 } while (FindNextFile(h, &fd));
600
601 DWORD err = GetLastError();
602 FindClose(h);
603 if (err != ERROR_NO_MORE_FILES) {
604 SetLastError(err);
Reid Spencer05545752006-08-25 21:37:17 +0000605 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000606 }
607
Jeff Cohen85c716f2005-07-08 05:02:13 +0000608 for (std::vector<Path>::iterator I = list.begin(); I != list.end();
Reid Spencer1cf2d042005-07-07 23:35:23 +0000609 ++I) {
610 Path &aPath = *I;
Reid Spencera229c5c2005-07-08 03:08:58 +0000611 aPath.eraseFromDisk(true);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000612 }
613 } else {
614 if (GetLastError() != ERROR_FILE_NOT_FOUND)
Reid Spencer05545752006-08-25 21:37:17 +0000615 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000616 }
617 }
618
619 pathname[lastchar] = 0;
620 if (!RemoveDirectory(pathname))
Reid Spencer05545752006-08-25 21:37:17 +0000621 return MakeErrMsg(ErrStr,
622 std::string(pathname) + ": Can't destroy directory: ");
Chris Lattner0c332312006-07-28 22:29:50 +0000623 return false;
Reid Spencer30300992006-08-24 18:58:37 +0000624 }
625 // It appears the path doesn't exist.
626 return true;
Reid Spencerb016a372004-09-15 05:49:50 +0000627}
628
Reid Spencer3b0cc782004-12-14 18:42:13 +0000629bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
Reid Spencer3b0cc782004-12-14 18:42:13 +0000630 assert(len < 1024 && "Request for magic string too long");
631 char* buf = (char*) alloca(1 + len);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000632
633 HANDLE h = CreateFile(path.c_str(),
634 GENERIC_READ,
635 FILE_SHARE_READ,
636 NULL,
637 OPEN_EXISTING,
638 FILE_ATTRIBUTE_NORMAL,
639 NULL);
640 if (h == INVALID_HANDLE_VALUE)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000641 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000642
643 DWORD nRead = 0;
644 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
645 CloseHandle(h);
646
647 if (!ret || nRead != len)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000648 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000649
Reid Spencer3b0cc782004-12-14 18:42:13 +0000650 buf[len] = '\0';
651 Magic = buf;
652 return true;
653}
654
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000655bool
Reid Spencer5a060772006-08-23 07:30:48 +0000656Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
Jeff Cohene564de22006-05-07 02:51:51 +0000657 if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING))
Reid Spencer5a060772006-08-23 07:30:48 +0000658 return MakeErrMsg(ErrMsg, "Can't move '" + path + "' to '" + newName.path
659 + "': ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000660 return true;
661}
662
663bool
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000664Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrMsg) const {
Jeff Cohen966fa412005-07-09 18:42:49 +0000665 // FIXME: should work on directories also.
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000666 if (!si.isFile) {
667 return true;
668 }
669
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000670 HANDLE h = CreateFile(path.c_str(),
671 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
672 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000673 NULL,
674 OPEN_EXISTING,
675 FILE_ATTRIBUTE_NORMAL,
676 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000677 if (h == INVALID_HANDLE_VALUE)
Chris Lattner1bebfb52006-07-28 22:36:17 +0000678 return true;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000679
680 BY_HANDLE_FILE_INFORMATION bhfi;
681 if (!GetFileInformationByHandle(h, &bhfi)) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000682 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000683 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000684 SetLastError(err);
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000685 return MakeErrMsg(ErrMsg, path + ": GetFileInformationByHandle: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000686 }
687
688 FILETIME ft;
689 (uint64_t&)ft = si.modTime.toWin32Time();
690 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000691 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000692 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000693 if (!ret) {
694 SetLastError(err);
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000695 return MakeErrMsg(ErrMsg, path + ": SetFileTime: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000696 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000697
698 // Best we can do with Unix permission bits is to interpret the owner
699 // writable bit.
700 if (si.mode & 0200) {
701 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
702 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000703 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000704 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000705 }
706 } else {
707 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
708 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000709 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000710 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000711 }
712 }
713
Chris Lattner1bebfb52006-07-28 22:36:17 +0000714 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000715}
716
Reid Spencer30300992006-08-24 18:58:37 +0000717bool
718CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg) {
Jeff Cohencb652552004-12-24 02:38:34 +0000719 // Can't use CopyFile macro defined in Windows.h because it would mess up the
720 // above line. We use the expansion it would have in a non-UNICODE build.
721 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Reid Spencer30300992006-08-24 18:58:37 +0000722 return MakeErrMsg(ErrMsg, "Can't copy '" + Src.toString() +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000723 "' to '" + Dest.toString() + "': ");
Reid Spencer30300992006-08-24 18:58:37 +0000724 return false;
Reid Spencerc29befb2004-12-15 01:50:13 +0000725}
726
Reid Spencer30300992006-08-24 18:58:37 +0000727bool
728Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000729 if (reuse_current && !exists())
Reid Spencer30300992006-08-24 18:58:37 +0000730 return false; // File doesn't exist already, just use it!
Reid Spencerc29befb2004-12-15 01:50:13 +0000731
Jeff Cohen9437bb62005-01-27 03:49:03 +0000732 // Reserve space for -XXXXXX at the end.
733 char *FNBuffer = (char*) alloca(path.size()+8);
734 unsigned offset = path.size();
735 path.copy(FNBuffer, offset);
Reid Spencerc29befb2004-12-15 01:50:13 +0000736
Jeff Cohen966fa412005-07-09 18:42:49 +0000737 // Find a numeric suffix that isn't used by an existing file. Assume there
738 // won't be more than 1 million files with the same prefix. Probably a safe
739 // bet.
Jeff Cohen9437bb62005-01-27 03:49:03 +0000740 static unsigned FCounter = 0;
741 do {
742 sprintf(FNBuffer+offset, "-%06u", FCounter);
743 if (++FCounter > 999999)
744 FCounter = 0;
745 path = FNBuffer;
746 } while (exists());
Reid Spencer30300992006-08-24 18:58:37 +0000747 return false;
Reid Spencerc29befb2004-12-15 01:50:13 +0000748}
749
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000750bool
Reid Spencer30300992006-08-24 18:58:37 +0000751Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000752 // Make this into a unique file name
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000753 makeUnique(reuse_current, ErrMsg);
Jeff Cohen9437bb62005-01-27 03:49:03 +0000754
755 // Now go and create it
756 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
757 FILE_ATTRIBUTE_NORMAL, NULL);
758 if (h == INVALID_HANDLE_VALUE)
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000759 return MakeErrMsg(ErrMsg, path + ": can't create file");
Jeff Cohen9437bb62005-01-27 03:49:03 +0000760
761 CloseHandle(h);
Reid Spencer30300992006-08-24 18:58:37 +0000762 return false;
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000763}
764
Reid Spencerb016a372004-09-15 05:49:50 +0000765}
Reid Spencercbad7012004-09-11 04:59:30 +0000766}