blob: ee3f622ed32ab876ddb9e4723a321ab64c0b664f [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Anton Korobeynikov64ddbe42007-12-22 14:26:49 +000027#undef GetCurrentDirectory
Jeff Cohencb652552004-12-24 02:38:34 +000028
Jeff Cohen966fa412005-07-09 18:42:49 +000029// Windows happily accepts either forward or backward slashes, though any path
30// returned by a Win32 API will have backward slashes. As LLVM code basically
31// assumes forward slashes are used, backward slashs are converted where they
32// can be introduced into a path.
33//
34// Another invariant is that a path ends with a slash if and only if the path
35// is a root directory. Any other use of a trailing slash is stripped. Unlike
36// in Unix, Windows has a rather complicated notion of a root path and this
37// invariant helps simply the code.
38
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000039static void FlipBackSlashes(std::string& s) {
40 for (size_t i = 0; i < s.size(); i++)
41 if (s[i] == '\\')
42 s[i] = '/';
43}
44
Reid Spencercbad7012004-09-11 04:59:30 +000045namespace llvm {
Reid Spencerb016a372004-09-15 05:49:50 +000046namespace sys {
Reid Spencercbad7012004-09-11 04:59:30 +000047
Chris Lattnere1b332a2008-02-27 06:17:10 +000048extern const char sys::PathSeparator = ';';
49
Reid Spencerb016a372004-09-15 05:49:50 +000050bool
Reid Spencer07adb282004-11-05 22:15:36 +000051Path::isValid() const {
Reid Spencerb016a372004-09-15 05:49:50 +000052 if (path.empty())
53 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000054
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000055 // If there is a colon, it must be the second character, preceded by a letter
56 // and followed by something.
57 size_t len = path.size();
58 size_t pos = path.rfind(':',len);
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000059 size_t rootslash = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000060 if (pos != std::string::npos) {
61 if (pos != 1 || !isalpha(path[0]) || len < 3)
62 return false;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000063 rootslash = 2;
64 }
Jeff Cohen85c716f2005-07-08 05:02:13 +000065
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000066 // Look for a UNC path, and if found adjust our notion of the root slash.
67 if (len > 3 && path[0] == '/' && path[1] == '/') {
68 rootslash = path.find('/', 2);
69 if (rootslash == std::string::npos)
70 rootslash = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000071 }
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000072
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000073 // Check for illegal characters.
74 if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012"
75 "\013\014\015\016\017\020\021\022\023\024\025\026"
76 "\027\030\031\032\033\034\035\036\037")
77 != std::string::npos)
78 return false;
Jeff Cohen85c716f2005-07-08 05:02:13 +000079
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000080 // Remove trailing slash, unless it's a root slash.
81 if (len > rootslash+1 && path[len-1] == '/')
82 path.erase(--len);
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000083
Jeff Cohen3bbbcc12005-01-14 04:09:39 +000084 // Check each component for legality.
85 for (pos = 0; pos < len; ++pos) {
86 // A component may not end in a space.
87 if (path[pos] == ' ') {
88 if (path[pos+1] == '/' || path[pos+1] == '\0')
89 return false;
90 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000091
Jeff Cohen3bbbcc12005-01-14 04:09:39 +000092 // A component may not end in a period.
93 if (path[pos] == '.') {
94 if (path[pos+1] == '/' || path[pos+1] == '\0') {
95 // Unless it is the pseudo-directory "."...
96 if (pos == 0 || path[pos-1] == '/' || path[pos-1] == ':')
97 return true;
98 // or "..".
99 if (pos > 0 && path[pos-1] == '.') {
100 if (pos == 1 || path[pos-2] == '/' || path[pos-2] == ':')
101 return true;
102 }
103 return false;
104 }
105 }
106 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000107
108 return true;
Reid Spencercbad7012004-09-11 04:59:30 +0000109}
110
Reid Spencer69cce812007-03-29 16:43:20 +0000111bool
112Path::isAbsolute() const {
Jeff Cohen84892be2007-03-29 17:27:38 +0000113 switch (path.length()) {
114 case 0:
115 return false;
116 case 1:
117 case 2:
118 return path[0] == '/';
119 default:
120 return path[0] == '/' || (path[1] == ':' && path[2] == '/');
121 }
Reid Spencer69cce812007-03-29 16:43:20 +0000122}
123
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000124static Path *TempDirectory = NULL;
125
Reid Spencerb016a372004-09-15 05:49:50 +0000126Path
Reid Spencercab0e432006-08-22 22:46:39 +0000127Path::GetTemporaryDirectory(std::string* ErrMsg) {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000128 if (TempDirectory)
129 return *TempDirectory;
130
131 char pathname[MAX_PATH];
Reid Spencercab0e432006-08-22 22:46:39 +0000132 if (!GetTempPath(MAX_PATH, pathname)) {
133 if (ErrMsg)
134 *ErrMsg = "Can't determine temporary directory";
135 return Path();
136 }
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000137
Reid Spencerb016a372004-09-15 05:49:50 +0000138 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000139 result.set(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000140
141 // Append a subdirectory passed on our process id so multiple LLVMs don't
142 // step on each other's toes.
Jeff Cohend41b30d2006-11-05 19:31:28 +0000143#ifdef __MINGW32__
144 // Mingw's Win32 header files are broken.
Reid Spencerab4d9b02006-06-08 18:08:43 +0000145 sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId()));
Jeff Cohend41b30d2006-11-05 19:31:28 +0000146#else
147 sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
148#endif
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000149 result.appendComponent(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000150
151 // If there's a directory left over from a previous LLVM execution that
152 // happened to have the same process id, get rid of it.
Reid Spencera229c5c2005-07-08 03:08:58 +0000153 result.eraseFromDisk(true);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000154
155 // And finally (re-)create the empty directory.
Reid Spencera229c5c2005-07-08 03:08:58 +0000156 result.createDirectoryOnDisk(false);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000157 TempDirectory = new Path(result);
158 return *TempDirectory;
Reid Spencerb016a372004-09-15 05:49:50 +0000159}
160
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000161// FIXME: the following set of functions don't map to Windows very well.
Reid Spencerb016a372004-09-15 05:49:50 +0000162Path
163Path::GetRootDirectory() {
164 Path result;
Jeff Cohen966fa412005-07-09 18:42:49 +0000165 result.set("C:/");
Reid Spencerb016a372004-09-15 05:49:50 +0000166 return result;
167}
168
Jeff Cohen85c716f2005-07-08 05:02:13 +0000169void
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000170Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000171 Paths.push_back(sys::Path("C:/WINDOWS/SYSTEM32"));
172 Paths.push_back(sys::Path("C:/WINDOWS"));
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000173}
174
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000175void
Gabor Greifdb5565a2007-07-06 20:28:40 +0000176Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) {
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000177 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
178 if (env_var != 0) {
179 getPathList(env_var,Paths);
180 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000181#ifdef LLVM_LIBDIR
182 {
183 Path tmpPath;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000184 if (tmpPath.set(LLVM_LIBDIR))
Reid Spencerc7f08322005-07-07 18:21:42 +0000185 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000186 Paths.push_back(tmpPath);
187 }
188#endif
189 GetSystemLibraryPaths(Paths);
Reid Spencerb016a372004-09-15 05:49:50 +0000190}
191
192Path
193Path::GetLLVMDefaultConfigDir() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000194 // TODO: this isn't going to fly on Windows
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000195 return Path("/etc/llvm");
Reid Spencerb016a372004-09-15 05:49:50 +0000196}
197
198Path
Reid Spencerb016a372004-09-15 05:49:50 +0000199Path::GetUserHomeDirectory() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000200 // TODO: Typical Windows setup doesn't define HOME.
Reid Spencerb016a372004-09-15 05:49:50 +0000201 const char* home = getenv("HOME");
202 if (home) {
203 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000204 if (result.set(home))
Reid Spencerb016a372004-09-15 05:49:50 +0000205 return result;
206 }
207 return GetRootDirectory();
208}
Ted Kremenek79200782007-12-18 22:07:33 +0000209
210Path
211Path::GetCurrentDirectory() {
212 char pathname[MAX_PATH];
Anton Korobeynikov64ddbe42007-12-22 14:26:49 +0000213 ::GetCurrentDirectoryA(MAX_PATH,pathname);
Ted Kremenek79200782007-12-18 22:07:33 +0000214 return Path(pathname);
215}
216
217
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000218// FIXME: the above set of functions don't map to Windows very well.
219
Jeff Cohen966fa412005-07-09 18:42:49 +0000220
221bool
222Path::isRootDirectory() const {
223 size_t len = path.size();
224 return len > 0 && path[len-1] == '/';
Reid Spencera229c5c2005-07-08 03:08:58 +0000225}
226
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000227std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000228Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000229 // Find the last slash
230 size_t slash = path.rfind('/');
231 if (slash == std::string::npos)
232 slash = 0;
233 else
234 slash++;
235
Jeff Cohen966fa412005-07-09 18:42:49 +0000236 size_t dot = path.rfind('.');
237 if (dot == std::string::npos || dot < slash)
238 return path.substr(slash);
239 else
240 return path.substr(slash, dot - slash);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000241}
242
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000243bool
Reid Spencerb016a372004-09-15 05:49:50 +0000244Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000245 DWORD attr = GetFileAttributes(path.c_str());
246 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000247}
248
249bool
Ted Kremenekfd527112007-12-18 19:46:22 +0000250Path::isDirectory() const {
251 DWORD attr = GetFileAttributes(path.c_str());
252 return (attr != INVALID_FILE_ATTRIBUTES) &&
253 (attr & FILE_ATTRIBUTE_DIRECTORY);
254}
255
256bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000257Path::canRead() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000258 // FIXME: take security attributes into account.
259 DWORD attr = GetFileAttributes(path.c_str());
260 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000261}
262
263bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000264Path::canWrite() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000265 // FIXME: take security attributes into account.
266 DWORD attr = GetFileAttributes(path.c_str());
267 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000268}
269
270bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000271Path::canExecute() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000272 // FIXME: take security attributes into account.
273 DWORD attr = GetFileAttributes(path.c_str());
274 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000275}
276
277std::string
278Path::getLast() const {
279 // Find the last slash
280 size_t pos = path.rfind('/');
281
282 // Handle the corner cases
283 if (pos == std::string::npos)
284 return path;
285
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000286 // If the last character is a slash, we have a root directory
287 if (pos == path.length()-1)
288 return path;
289
Reid Spencerb016a372004-09-15 05:49:50 +0000290 // Return everything after the last slash
291 return path.substr(pos+1);
292}
293
Reid Spencer8475ec02007-03-29 19:05:44 +0000294const FileStatus *
Reid Spencer2ae9d112007-04-07 18:52:17 +0000295PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const {
296 if (!fsIsValid || update) {
Reid Spencer69cce812007-03-29 16:43:20 +0000297 WIN32_FILE_ATTRIBUTE_DATA fi;
Reid Spencer8475ec02007-03-29 19:05:44 +0000298 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
299 MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) +
Reid Spencer69cce812007-03-29 16:43:20 +0000300 ": Can't get status: ");
Reid Spencer8475ec02007-03-29 19:05:44 +0000301 return 0;
302 }
Jeff Cohen626e38e2004-12-14 05:26:43 +0000303
Reid Spencer2ae9d112007-04-07 18:52:17 +0000304 status.fileSize = fi.nFileSizeHigh;
305 status.fileSize <<= sizeof(fi.nFileSizeHigh)*8;
306 status.fileSize += fi.nFileSizeLow;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000307
Reid Spencer2ae9d112007-04-07 18:52:17 +0000308 status.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
309 status.user = 9999; // Not applicable to Windows, so...
310 status.group = 9999; // Not applicable to Windows, so...
Jeff Cohen626e38e2004-12-14 05:26:43 +0000311
Reid Spencer4031bef2007-03-29 17:00:31 +0000312 // FIXME: this is only unique if the file is accessed by the same file path.
313 // How do we do this for C:\dir\file and ..\dir\file ? Unix has inode
314 // numbers, but the concept doesn't exist in Windows.
Reid Spencer2ae9d112007-04-07 18:52:17 +0000315 status.uniqueID = 0;
Reid Spencer4031bef2007-03-29 17:00:31 +0000316 for (unsigned i = 0; i < path.length(); ++i)
Reid Spencer2ae9d112007-04-07 18:52:17 +0000317 status.uniqueID += path[i];
Reid Spencer4031bef2007-03-29 17:00:31 +0000318
Reid Spencer69cce812007-03-29 16:43:20 +0000319 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
Reid Spencer2ae9d112007-04-07 18:52:17 +0000320 status.modTime.fromWin32Time(ft);
Reid Spencer69cce812007-03-29 16:43:20 +0000321
Reid Spencer2ae9d112007-04-07 18:52:17 +0000322 status.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
323 fsIsValid = true;
Reid Spencer69cce812007-03-29 16:43:20 +0000324 }
Reid Spencer2ae9d112007-04-07 18:52:17 +0000325 return &status;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000326}
327
Reid Spencera34a1572006-08-22 23:54:35 +0000328bool Path::makeReadableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000329 // All files are readable on Windows (ignoring security attributes).
Reid Spencera34a1572006-08-22 23:54:35 +0000330 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000331}
332
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000333bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000334 DWORD attr = GetFileAttributes(path.c_str());
335
336 // If it doesn't exist, we're done.
337 if (attr == INVALID_FILE_ATTRIBUTES)
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000338 return false;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000339
340 if (attr & FILE_ATTRIBUTE_READONLY) {
Reid Spencera34a1572006-08-22 23:54:35 +0000341 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) {
342 MakeErrMsg(ErrMsg, std::string(path) + ": Can't make file writable: ");
343 return true;
344 }
Jeff Cohen626e38e2004-12-14 05:26:43 +0000345 }
Reid Spencera34a1572006-08-22 23:54:35 +0000346 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000347}
348
Reid Spencera34a1572006-08-22 23:54:35 +0000349bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000350 // All files are executable on Windows (ignoring security attributes).
Reid Spencera34a1572006-08-22 23:54:35 +0000351 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000352}
353
Reid Spencerb016a372004-09-15 05:49:50 +0000354bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000355Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
Jeff Cohen31102892007-04-07 20:47:27 +0000356 WIN32_FILE_ATTRIBUTE_DATA fi;
357 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
358 MakeErrMsg(ErrMsg, path + ": can't get status of file");
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000359 return true;
Jeff Cohen31102892007-04-07 20:47:27 +0000360 }
361
362 if (!(fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
363 if (ErrMsg)
364 *ErrMsg = path + ": not a directory";
Reid Spencer142ca8e2006-08-23 06:56:27 +0000365 return true;
366 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000367
368 result.clear();
369 WIN32_FIND_DATA fd;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000370 std::string searchpath = path;
371 if (path.size() == 0 || searchpath[path.size()-1] == '/')
Jeff Cohen85c716f2005-07-08 05:02:13 +0000372 searchpath += "*";
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000373 else
374 searchpath += "/*";
Jeff Cohen85c716f2005-07-08 05:02:13 +0000375
Jeff Cohen9437bb62005-01-27 03:49:03 +0000376 HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000377 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohen9437bb62005-01-27 03:49:03 +0000378 if (GetLastError() == ERROR_FILE_NOT_FOUND)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000379 return true; // not really an error, now is it?
Reid Spencer142ca8e2006-08-23 06:56:27 +0000380 MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
381 return true;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000382 }
383
384 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000385 if (fd.cFileName[0] == '.')
386 continue;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000387 Path aPath(path);
388 aPath.appendComponent(&fd.cFileName[0]);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000389 result.insert(aPath);
390 } while (FindNextFile(h, &fd));
391
Jeff Cohen51b8d212004-12-31 19:01:08 +0000392 DWORD err = GetLastError();
393 FindClose(h);
394 if (err != ERROR_NO_MORE_FILES) {
395 SetLastError(err);
Reid Spencer142ca8e2006-08-23 06:56:27 +0000396 MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
397 return true;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000398 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000399 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000400}
401
402bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000403Path::set(const std::string& a_path) {
Dan Gohman30359592008-01-29 13:02:09 +0000404 if (a_path.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000405 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000406 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000407 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000408 FlipBackSlashes(path);
Reid Spencer07adb282004-11-05 22:15:36 +0000409 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000410 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000411 return false;
412 }
413 return true;
414}
415
416bool
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000417Path::appendComponent(const std::string& name) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000418 if (name.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000419 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000420 std::string save(path);
421 if (!path.empty()) {
422 size_t last = path.size() - 1;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000423 if (path[last] != '/')
Reid Spencer1cf2d042005-07-07 23:35:23 +0000424 path += '/';
425 }
426 path += name;
Reid Spencer07adb282004-11-05 22:15:36 +0000427 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000428 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000429 return false;
430 }
431 return true;
432}
433
434bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000435Path::eraseComponent() {
Reid Spencerb016a372004-09-15 05:49:50 +0000436 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000437 if (slashpos == path.size() - 1 || slashpos == std::string::npos)
Reid Spencerb016a372004-09-15 05:49:50 +0000438 return false;
Jeff Cohen966fa412005-07-09 18:42:49 +0000439 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000440 path.erase(slashpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000441 if (!isValid()) {
442 path = save;
443 return false;
444 }
Reid Spencerb016a372004-09-15 05:49:50 +0000445 return true;
446}
447
448bool
Reid Spencer07adb282004-11-05 22:15:36 +0000449Path::appendSuffix(const std::string& suffix) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000450 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000451 path.append(".");
452 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000453 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000454 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000455 return false;
456 }
457 return true;
458}
459
460bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000461Path::eraseSuffix() {
Reid Spencerb016a372004-09-15 05:49:50 +0000462 size_t dotpos = path.rfind('.',path.size());
463 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000464 if (dotpos != std::string::npos) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000465 if (slashpos == std::string::npos || dotpos > slashpos+1) {
466 std::string save(path);
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000467 path.erase(dotpos, path.size()-dotpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000468 if (!isValid()) {
469 path = save;
470 return false;
471 }
Jeff Cohen85c716f2005-07-08 05:02:13 +0000472 return true;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000473 }
Reid Spencerb016a372004-09-15 05:49:50 +0000474 }
475 return false;
476}
477
Reid Spencer30300992006-08-24 18:58:37 +0000478inline bool PathMsg(std::string* ErrMsg, const char* pathname, const char*msg) {
479 if (ErrMsg)
480 *ErrMsg = std::string(pathname) + ": " + std::string(msg);
481 return true;
482}
483
Reid Spencerb016a372004-09-15 05:49:50 +0000484bool
Reid Spencer30300992006-08-24 18:58:37 +0000485Path::createDirectoryOnDisk(bool create_parents, std::string* ErrMsg) {
Reid Spencerb016a372004-09-15 05:49:50 +0000486 // Get a writeable copy of the path name
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000487 size_t len = path.length();
488 char *pathname = reinterpret_cast<char *>(_alloca(len+2));
489 path.copy(pathname, len);
490 pathname[len] = 0;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000491
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000492 // Make sure it ends with a slash.
493 if (len == 0 || pathname[len - 1] != '/') {
494 pathname[len] = '/';
495 pathname[++len] = 0;
496 }
Reid Spencerb016a372004-09-15 05:49:50 +0000497
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000498 // Determine starting point for initial / search.
499 char *next = pathname;
500 if (pathname[0] == '/' && pathname[1] == '/') {
501 // Skip host name.
502 next = strchr(pathname+2, '/');
503 if (next == NULL)
Reid Spencer30300992006-08-24 18:58:37 +0000504 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
505
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000506 // Skip share name.
507 next = strchr(next+1, '/');
508 if (next == NULL)
Reid Spencer30300992006-08-24 18:58:37 +0000509 return PathMsg(ErrMsg, pathname,"badly formed remote directory");
510
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000511 next++;
512 if (*next == 0)
Reid Spencer30300992006-08-24 18:58:37 +0000513 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
514
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000515 } else {
516 if (pathname[1] == ':')
517 next += 2; // skip drive letter
518 if (*next == '/')
519 next++; // skip root directory
520 }
Reid Spencerb016a372004-09-15 05:49:50 +0000521
522 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000523 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000524 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000525 while (*next) {
526 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000527 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000528 if (!CreateDirectory(pathname, NULL))
Reid Spencer30300992006-08-24 18:58:37 +0000529 return MakeErrMsg(ErrMsg,
530 std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000531 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000532 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000533 } else {
534 // Drop trailing slash.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000535 pathname[len-1] = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000536 if (!CreateDirectory(pathname, NULL)) {
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000537 return MakeErrMsg(ErrMsg, std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000538 }
Reid Spencerb016a372004-09-15 05:49:50 +0000539 }
Reid Spencer30300992006-08-24 18:58:37 +0000540 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000541}
542
543bool
Reid Spencer30300992006-08-24 18:58:37 +0000544Path::createFileOnDisk(std::string* ErrMsg) {
Reid Spencerb016a372004-09-15 05:49:50 +0000545 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000546 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000547 FILE_ATTRIBUTE_NORMAL, NULL);
548 if (h == INVALID_HANDLE_VALUE)
Reid Spencer30300992006-08-24 18:58:37 +0000549 return MakeErrMsg(ErrMsg, path + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000550
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000551 CloseHandle(h);
Reid Spencer30300992006-08-24 18:58:37 +0000552 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000553}
554
555bool
Chris Lattner0c332312006-07-28 22:29:50 +0000556Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
Jeff Cohen31102892007-04-07 20:47:27 +0000557 WIN32_FILE_ATTRIBUTE_DATA fi;
558 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
559 return true;
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000560
Jeff Cohen31102892007-04-07 20:47:27 +0000561 if (fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000562 // If it doesn't exist, we're done.
Jeff Cohen85c716f2005-07-08 05:02:13 +0000563 if (!exists())
Chris Lattner0c332312006-07-28 22:29:50 +0000564 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000565
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000566 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000567 int lastchar = path.length() - 1 ;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000568 path.copy(pathname, lastchar+1);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000569
570 // Make path end with '/*'.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000571 if (pathname[lastchar] != '/')
572 pathname[++lastchar] = '/';
Reid Spencer1cf2d042005-07-07 23:35:23 +0000573 pathname[lastchar+1] = '*';
574 pathname[lastchar+2] = 0;
575
576 if (remove_contents) {
577 WIN32_FIND_DATA fd;
578 HANDLE h = FindFirstFile(pathname, &fd);
579
580 // It's a bad idea to alter the contents of a directory while enumerating
581 // its contents. So build a list of its contents first, then destroy them.
582
583 if (h != INVALID_HANDLE_VALUE) {
584 std::vector<Path> list;
585
586 do {
587 if (strcmp(fd.cFileName, ".") == 0)
588 continue;
589 if (strcmp(fd.cFileName, "..") == 0)
590 continue;
591
Jeff Cohen85c716f2005-07-08 05:02:13 +0000592 Path aPath(path);
593 aPath.appendComponent(&fd.cFileName[0]);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000594 list.push_back(aPath);
595 } while (FindNextFile(h, &fd));
596
597 DWORD err = GetLastError();
598 FindClose(h);
599 if (err != ERROR_NO_MORE_FILES) {
600 SetLastError(err);
Reid Spencer05545752006-08-25 21:37:17 +0000601 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000602 }
603
Jeff Cohen85c716f2005-07-08 05:02:13 +0000604 for (std::vector<Path>::iterator I = list.begin(); I != list.end();
Reid Spencer1cf2d042005-07-07 23:35:23 +0000605 ++I) {
606 Path &aPath = *I;
Reid Spencera229c5c2005-07-08 03:08:58 +0000607 aPath.eraseFromDisk(true);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000608 }
609 } else {
610 if (GetLastError() != ERROR_FILE_NOT_FOUND)
Reid Spencer05545752006-08-25 21:37:17 +0000611 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000612 }
613 }
614
615 pathname[lastchar] = 0;
616 if (!RemoveDirectory(pathname))
Reid Spencer05545752006-08-25 21:37:17 +0000617 return MakeErrMsg(ErrStr,
618 std::string(pathname) + ": Can't destroy directory: ");
Chris Lattner0c332312006-07-28 22:29:50 +0000619 return false;
Jeff Cohen31102892007-04-07 20:47:27 +0000620 } else {
621 // Read-only files cannot be deleted on Windows. Must remove the read-only
622 // attribute first.
623 if (fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
624 if (!SetFileAttributes(path.c_str(),
625 fi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
626 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
627 }
628
629 if (!DeleteFile(path.c_str()))
630 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
631 return false;
632 }
Reid Spencerb016a372004-09-15 05:49:50 +0000633}
634
Reid Spencer3b0cc782004-12-14 18:42:13 +0000635bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
Reid Spencer3b0cc782004-12-14 18:42:13 +0000636 assert(len < 1024 && "Request for magic string too long");
637 char* buf = (char*) alloca(1 + len);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000638
639 HANDLE h = CreateFile(path.c_str(),
640 GENERIC_READ,
641 FILE_SHARE_READ,
642 NULL,
643 OPEN_EXISTING,
644 FILE_ATTRIBUTE_NORMAL,
645 NULL);
646 if (h == INVALID_HANDLE_VALUE)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000647 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000648
649 DWORD nRead = 0;
650 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
651 CloseHandle(h);
652
653 if (!ret || nRead != len)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000654 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000655
Reid Spencer3b0cc782004-12-14 18:42:13 +0000656 buf[len] = '\0';
657 Magic = buf;
658 return true;
659}
660
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000661bool
Reid Spencer5a060772006-08-23 07:30:48 +0000662Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
Jeff Cohene564de22006-05-07 02:51:51 +0000663 if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING))
Reid Spencer5a060772006-08-23 07:30:48 +0000664 return MakeErrMsg(ErrMsg, "Can't move '" + path + "' to '" + newName.path
665 + "': ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000666 return true;
667}
668
669bool
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000670Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrMsg) const {
Jeff Cohen966fa412005-07-09 18:42:49 +0000671 // FIXME: should work on directories also.
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000672 if (!si.isFile) {
673 return true;
674 }
675
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000676 HANDLE h = CreateFile(path.c_str(),
677 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
678 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000679 NULL,
680 OPEN_EXISTING,
681 FILE_ATTRIBUTE_NORMAL,
682 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000683 if (h == INVALID_HANDLE_VALUE)
Chris Lattner1bebfb52006-07-28 22:36:17 +0000684 return true;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000685
686 BY_HANDLE_FILE_INFORMATION bhfi;
687 if (!GetFileInformationByHandle(h, &bhfi)) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000688 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000689 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000690 SetLastError(err);
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000691 return MakeErrMsg(ErrMsg, path + ": GetFileInformationByHandle: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000692 }
693
694 FILETIME ft;
695 (uint64_t&)ft = si.modTime.toWin32Time();
696 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000697 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000698 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000699 if (!ret) {
700 SetLastError(err);
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000701 return MakeErrMsg(ErrMsg, path + ": SetFileTime: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000702 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000703
704 // Best we can do with Unix permission bits is to interpret the owner
705 // writable bit.
706 if (si.mode & 0200) {
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 } else {
713 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
714 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000715 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000716 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000717 }
718 }
719
Chris Lattner1bebfb52006-07-28 22:36:17 +0000720 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000721}
722
Reid Spencer30300992006-08-24 18:58:37 +0000723bool
724CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg) {
Jeff Cohencb652552004-12-24 02:38:34 +0000725 // Can't use CopyFile macro defined in Windows.h because it would mess up the
726 // above line. We use the expansion it would have in a non-UNICODE build.
727 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Reid Spencer30300992006-08-24 18:58:37 +0000728 return MakeErrMsg(ErrMsg, "Can't copy '" + Src.toString() +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000729 "' to '" + Dest.toString() + "': ");
Reid Spencer30300992006-08-24 18:58:37 +0000730 return false;
Reid Spencerc29befb2004-12-15 01:50:13 +0000731}
732
Reid Spencer30300992006-08-24 18:58:37 +0000733bool
734Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000735 if (reuse_current && !exists())
Reid Spencer30300992006-08-24 18:58:37 +0000736 return false; // File doesn't exist already, just use it!
Reid Spencerc29befb2004-12-15 01:50:13 +0000737
Jeff Cohen9437bb62005-01-27 03:49:03 +0000738 // Reserve space for -XXXXXX at the end.
739 char *FNBuffer = (char*) alloca(path.size()+8);
740 unsigned offset = path.size();
741 path.copy(FNBuffer, offset);
Reid Spencerc29befb2004-12-15 01:50:13 +0000742
Jeff Cohen966fa412005-07-09 18:42:49 +0000743 // Find a numeric suffix that isn't used by an existing file. Assume there
744 // won't be more than 1 million files with the same prefix. Probably a safe
745 // bet.
Jeff Cohen9437bb62005-01-27 03:49:03 +0000746 static unsigned FCounter = 0;
747 do {
748 sprintf(FNBuffer+offset, "-%06u", FCounter);
749 if (++FCounter > 999999)
750 FCounter = 0;
751 path = FNBuffer;
752 } while (exists());
Reid Spencer30300992006-08-24 18:58:37 +0000753 return false;
Reid Spencerc29befb2004-12-15 01:50:13 +0000754}
755
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000756bool
Reid Spencer30300992006-08-24 18:58:37 +0000757Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000758 // Make this into a unique file name
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000759 makeUnique(reuse_current, ErrMsg);
Jeff Cohen9437bb62005-01-27 03:49:03 +0000760
761 // Now go and create it
762 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
763 FILE_ATTRIBUTE_NORMAL, NULL);
764 if (h == INVALID_HANDLE_VALUE)
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000765 return MakeErrMsg(ErrMsg, path + ": can't create file");
Jeff Cohen9437bb62005-01-27 03:49:03 +0000766
767 CloseHandle(h);
Reid Spencer30300992006-08-24 18:58:37 +0000768 return false;
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000769}
770
Reid Spencerb016a372004-09-15 05:49:50 +0000771}
Reid Spencercbad7012004-09-11 04:59:30 +0000772}