blob: 048bf602975ffed01b5e1511285fc03c09cc575e [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.
Reid Spencerab4d9b02006-06-08 18:08:43 +0000127 sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId()));
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000128 result.appendComponent(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000129
130 // If there's a directory left over from a previous LLVM execution that
131 // happened to have the same process id, get rid of it.
Reid Spencera229c5c2005-07-08 03:08:58 +0000132 result.eraseFromDisk(true);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000133
134 // And finally (re-)create the empty directory.
Reid Spencera229c5c2005-07-08 03:08:58 +0000135 result.createDirectoryOnDisk(false);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000136 TempDirectory = new Path(result);
137 return *TempDirectory;
Reid Spencerb016a372004-09-15 05:49:50 +0000138}
139
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000140// FIXME: the following set of functions don't map to Windows very well.
Reid Spencerb016a372004-09-15 05:49:50 +0000141Path
142Path::GetRootDirectory() {
143 Path result;
Jeff Cohen966fa412005-07-09 18:42:49 +0000144 result.set("C:/");
Reid Spencerb016a372004-09-15 05:49:50 +0000145 return result;
146}
147
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000148static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
149 const char* at = path;
150 const char* delim = strchr(at, ';');
151 Path tmpPath;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000152 while (delim != 0) {
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000153 std::string tmp(at, size_t(delim-at));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000154 if (tmpPath.set(tmp))
Reid Spencerc7f08322005-07-07 18:21:42 +0000155 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000156 Paths.push_back(tmpPath);
157 at = delim + 1;
158 delim = strchr(at, ';');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000159 }
Jeff Cohen85c716f2005-07-08 05:02:13 +0000160
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000161 if (*at != 0)
Reid Spencer1cf2d042005-07-07 23:35:23 +0000162 if (tmpPath.set(std::string(at)))
Reid Spencerc7f08322005-07-07 18:21:42 +0000163 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000164 Paths.push_back(tmpPath);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000165}
166
Jeff Cohen85c716f2005-07-08 05:02:13 +0000167void
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000168Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000169 Paths.push_back(sys::Path("C:/WINDOWS/SYSTEM32"));
170 Paths.push_back(sys::Path("C:/WINDOWS"));
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000171}
172
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000173void
174Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
175 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
176 if (env_var != 0) {
177 getPathList(env_var,Paths);
178 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000179#ifdef LLVM_LIBDIR
180 {
181 Path tmpPath;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000182 if (tmpPath.set(LLVM_LIBDIR))
Reid Spencerc7f08322005-07-07 18:21:42 +0000183 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000184 Paths.push_back(tmpPath);
185 }
186#endif
187 GetSystemLibraryPaths(Paths);
Reid Spencerb016a372004-09-15 05:49:50 +0000188}
189
190Path
191Path::GetLLVMDefaultConfigDir() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000192 // TODO: this isn't going to fly on Windows
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000193 return Path("/etc/llvm");
Reid Spencerb016a372004-09-15 05:49:50 +0000194}
195
196Path
Reid Spencerb016a372004-09-15 05:49:50 +0000197Path::GetUserHomeDirectory() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000198 // TODO: Typical Windows setup doesn't define HOME.
Reid Spencerb016a372004-09-15 05:49:50 +0000199 const char* home = getenv("HOME");
200 if (home) {
201 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000202 if (result.set(home))
Reid Spencerb016a372004-09-15 05:49:50 +0000203 return result;
204 }
205 return GetRootDirectory();
206}
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000207// FIXME: the above set of functions don't map to Windows very well.
208
Jeff Cohen966fa412005-07-09 18:42:49 +0000209
210bool
211Path::isRootDirectory() const {
212 size_t len = path.size();
213 return len > 0 && path[len-1] == '/';
Reid Spencera229c5c2005-07-08 03:08:58 +0000214}
215
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000216std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000217Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000218 // Find the last slash
219 size_t slash = path.rfind('/');
220 if (slash == std::string::npos)
221 slash = 0;
222 else
223 slash++;
224
Jeff Cohen966fa412005-07-09 18:42:49 +0000225 size_t dot = path.rfind('.');
226 if (dot == std::string::npos || dot < slash)
227 return path.substr(slash);
228 else
229 return path.substr(slash, dot - slash);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000230}
231
Reid Spencer07adb282004-11-05 22:15:36 +0000232bool Path::hasMagicNumber(const std::string &Magic) const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000233 std::string actualMagic;
234 if (getMagicNumber(actualMagic, Magic.size()))
235 return Magic == actualMagic;
236 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000237}
238
Jeff Cohen85c716f2005-07-08 05:02:13 +0000239bool
Reid Spencer07adb282004-11-05 22:15:36 +0000240Path::isBytecodeFile() const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000241 std::string actualMagic;
242 if (!getMagicNumber(actualMagic, 4))
243 return false;
244 return actualMagic == "llvc" || actualMagic == "llvm";
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000245}
246
247bool
Reid Spencerb016a372004-09-15 05:49:50 +0000248Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000249 DWORD attr = GetFileAttributes(path.c_str());
250 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000251}
252
253bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000254Path::canRead() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000255 // FIXME: take security attributes into account.
256 DWORD attr = GetFileAttributes(path.c_str());
257 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000258}
259
260bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000261Path::canWrite() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000262 // FIXME: take security attributes into account.
263 DWORD attr = GetFileAttributes(path.c_str());
264 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000265}
266
267bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000268Path::canExecute() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000269 // FIXME: take security attributes into account.
270 DWORD attr = GetFileAttributes(path.c_str());
271 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000272}
273
274std::string
275Path::getLast() const {
276 // Find the last slash
277 size_t pos = path.rfind('/');
278
279 // Handle the corner cases
280 if (pos == std::string::npos)
281 return path;
282
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000283 // If the last character is a slash, we have a root directory
284 if (pos == path.length()-1)
285 return path;
286
Reid Spencerb016a372004-09-15 05:49:50 +0000287 // Return everything after the last slash
288 return path.substr(pos+1);
289}
290
Chris Lattner7dea1012006-07-28 22:32:09 +0000291bool
Anton Korobeynikov1a7d3262006-08-01 08:07:22 +0000292Path::getFileStatus(FileStatus &info, std::string *ErrStr) const {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000293 WIN32_FILE_ATTRIBUTE_DATA fi;
294 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
Reid Spencer05545752006-08-25 21:37:17 +0000295 return MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) +
296 ": Can't get status: ");
Jeff Cohen626e38e2004-12-14 05:26:43 +0000297
298 info.fileSize = fi.nFileSizeHigh;
Reid Spencer393830a2006-06-08 17:00:08 +0000299 info.fileSize <<= sizeof(fi.nFileSizeHigh)*8;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000300 info.fileSize += fi.nFileSizeLow;
301
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000302 info.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000303 info.user = 9999; // Not applicable to Windows, so...
304 info.group = 9999; // Not applicable to Windows, so...
305
306 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
307 info.modTime.fromWin32Time(ft);
308
309 info.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Chris Lattner7dea1012006-07-28 22:32:09 +0000310 return false;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000311}
312
Reid Spencera34a1572006-08-22 23:54:35 +0000313bool Path::makeReadableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000314 // All files are readable on Windows (ignoring security attributes).
Reid Spencera34a1572006-08-22 23:54:35 +0000315 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000316}
317
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000318bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000319 DWORD attr = GetFileAttributes(path.c_str());
320
321 // If it doesn't exist, we're done.
322 if (attr == INVALID_FILE_ATTRIBUTES)
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000323 return false;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000324
325 if (attr & FILE_ATTRIBUTE_READONLY) {
Reid Spencera34a1572006-08-22 23:54:35 +0000326 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) {
327 MakeErrMsg(ErrMsg, std::string(path) + ": Can't make file writable: ");
328 return true;
329 }
Jeff Cohen626e38e2004-12-14 05:26:43 +0000330 }
Reid Spencera34a1572006-08-22 23:54:35 +0000331 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000332}
333
Reid Spencera34a1572006-08-22 23:54:35 +0000334bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000335 // All files are executable on Windows (ignoring security attributes).
Reid Spencera34a1572006-08-22 23:54:35 +0000336 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000337}
338
Reid Spencerb016a372004-09-15 05:49:50 +0000339bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000340Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000341 FileStatus Status;
342 if (getFileStatus(Status, ErrMsg))
343 return true;
344
345 if (!Status.isDir) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000346 MakeErrMsg(ErrMsg, path + ": not a directory");
347 return true;
348 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000349
350 result.clear();
351 WIN32_FIND_DATA fd;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000352 std::string searchpath = path;
353 if (path.size() == 0 || searchpath[path.size()-1] == '/')
Jeff Cohen85c716f2005-07-08 05:02:13 +0000354 searchpath += "*";
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000355 else
356 searchpath += "/*";
Jeff Cohen85c716f2005-07-08 05:02:13 +0000357
Jeff Cohen9437bb62005-01-27 03:49:03 +0000358 HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000359 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohen9437bb62005-01-27 03:49:03 +0000360 if (GetLastError() == ERROR_FILE_NOT_FOUND)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000361 return true; // not really an error, now is it?
Reid Spencer142ca8e2006-08-23 06:56:27 +0000362 MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
363 return true;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000364 }
365
366 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000367 if (fd.cFileName[0] == '.')
368 continue;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000369 Path aPath(path);
370 aPath.appendComponent(&fd.cFileName[0]);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000371 result.insert(aPath);
372 } while (FindNextFile(h, &fd));
373
Jeff Cohen51b8d212004-12-31 19:01:08 +0000374 DWORD err = GetLastError();
375 FindClose(h);
376 if (err != ERROR_NO_MORE_FILES) {
377 SetLastError(err);
Reid Spencer142ca8e2006-08-23 06:56:27 +0000378 MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
379 return true;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000380 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000381 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000382}
383
384bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000385Path::set(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000386 if (a_path.size() == 0)
387 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000388 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000389 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000390 FlipBackSlashes(path);
Reid Spencer07adb282004-11-05 22:15:36 +0000391 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000392 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000393 return false;
394 }
395 return true;
396}
397
398bool
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000399Path::appendComponent(const std::string& name) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000400 if (name.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000401 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000402 std::string save(path);
403 if (!path.empty()) {
404 size_t last = path.size() - 1;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000405 if (path[last] != '/')
Reid Spencer1cf2d042005-07-07 23:35:23 +0000406 path += '/';
407 }
408 path += name;
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
Reid Spencer1cf2d042005-07-07 23:35:23 +0000417Path::eraseComponent() {
Reid Spencerb016a372004-09-15 05:49:50 +0000418 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000419 if (slashpos == path.size() - 1 || slashpos == std::string::npos)
Reid Spencerb016a372004-09-15 05:49:50 +0000420 return false;
Jeff Cohen966fa412005-07-09 18:42:49 +0000421 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000422 path.erase(slashpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000423 if (!isValid()) {
424 path = save;
425 return false;
426 }
Reid Spencerb016a372004-09-15 05:49:50 +0000427 return true;
428}
429
430bool
Reid Spencer07adb282004-11-05 22:15:36 +0000431Path::appendSuffix(const std::string& suffix) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000432 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000433 path.append(".");
434 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000435 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000436 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000437 return false;
438 }
439 return true;
440}
441
442bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000443Path::eraseSuffix() {
Reid Spencerb016a372004-09-15 05:49:50 +0000444 size_t dotpos = path.rfind('.',path.size());
445 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000446 if (dotpos != std::string::npos) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000447 if (slashpos == std::string::npos || dotpos > slashpos+1) {
448 std::string save(path);
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000449 path.erase(dotpos, path.size()-dotpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000450 if (!isValid()) {
451 path = save;
452 return false;
453 }
Jeff Cohen85c716f2005-07-08 05:02:13 +0000454 return true;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000455 }
Reid Spencerb016a372004-09-15 05:49:50 +0000456 }
457 return false;
458}
459
Reid Spencer30300992006-08-24 18:58:37 +0000460inline bool PathMsg(std::string* ErrMsg, const char* pathname, const char*msg) {
461 if (ErrMsg)
462 *ErrMsg = std::string(pathname) + ": " + std::string(msg);
463 return true;
464}
465
Reid Spencerb016a372004-09-15 05:49:50 +0000466bool
Reid Spencer30300992006-08-24 18:58:37 +0000467Path::createDirectoryOnDisk(bool create_parents, std::string* ErrMsg) {
Reid Spencerb016a372004-09-15 05:49:50 +0000468 // Get a writeable copy of the path name
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000469 size_t len = path.length();
470 char *pathname = reinterpret_cast<char *>(_alloca(len+2));
471 path.copy(pathname, len);
472 pathname[len] = 0;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000473
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000474 // Make sure it ends with a slash.
475 if (len == 0 || pathname[len - 1] != '/') {
476 pathname[len] = '/';
477 pathname[++len] = 0;
478 }
Reid Spencerb016a372004-09-15 05:49:50 +0000479
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000480 // Determine starting point for initial / search.
481 char *next = pathname;
482 if (pathname[0] == '/' && pathname[1] == '/') {
483 // Skip host name.
484 next = strchr(pathname+2, '/');
485 if (next == NULL)
Reid Spencer30300992006-08-24 18:58:37 +0000486 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
487
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000488 // Skip share name.
489 next = strchr(next+1, '/');
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 next++;
494 if (*next == 0)
Reid Spencer30300992006-08-24 18:58:37 +0000495 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
496
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000497 } else {
498 if (pathname[1] == ':')
499 next += 2; // skip drive letter
500 if (*next == '/')
501 next++; // skip root directory
502 }
Reid Spencerb016a372004-09-15 05:49:50 +0000503
504 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000505 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000506 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000507 while (*next) {
508 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000509 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000510 if (!CreateDirectory(pathname, NULL))
Reid Spencer30300992006-08-24 18:58:37 +0000511 return MakeErrMsg(ErrMsg,
512 std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000513 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000514 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000515 } else {
516 // Drop trailing slash.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000517 pathname[len-1] = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000518 if (!CreateDirectory(pathname, NULL)) {
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000519 return MakeErrMsg(ErrMsg, std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000520 }
Reid Spencerb016a372004-09-15 05:49:50 +0000521 }
Reid Spencer30300992006-08-24 18:58:37 +0000522 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000523}
524
525bool
Reid Spencer30300992006-08-24 18:58:37 +0000526Path::createFileOnDisk(std::string* ErrMsg) {
Reid Spencerb016a372004-09-15 05:49:50 +0000527 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000528 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000529 FILE_ATTRIBUTE_NORMAL, NULL);
530 if (h == INVALID_HANDLE_VALUE)
Reid Spencer30300992006-08-24 18:58:37 +0000531 return MakeErrMsg(ErrMsg, path + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000532
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000533 CloseHandle(h);
Reid Spencer30300992006-08-24 18:58:37 +0000534 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000535}
536
537bool
Chris Lattner0c332312006-07-28 22:29:50 +0000538Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000539 FileStatus Status;
540 if (getFileStatus(Status, ErrStr))
Reid Spencer30300992006-08-24 18:58:37 +0000541 return false;
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000542
543 if (Status.isFile) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000544 DWORD attr = GetFileAttributes(path.c_str());
Reid Spencerb016a372004-09-15 05:49:50 +0000545
Reid Spencer1cf2d042005-07-07 23:35:23 +0000546 // If it doesn't exist, we're done.
547 if (attr == INVALID_FILE_ATTRIBUTES)
Reid Spencer30300992006-08-24 18:58:37 +0000548 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000549
Reid Spencer1cf2d042005-07-07 23:35:23 +0000550 // Read-only files cannot be deleted on Windows. Must remove the read-only
551 // attribute first.
552 if (attr & FILE_ATTRIBUTE_READONLY) {
553 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
Reid Spencer05545752006-08-25 21:37:17 +0000554 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000555 }
Jeff Cohen51b8d212004-12-31 19:01:08 +0000556
Reid Spencer1cf2d042005-07-07 23:35:23 +0000557 if (!DeleteFile(path.c_str()))
Reid Spencer30300992006-08-24 18:58:37 +0000558 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000559 return false;
560 } else if (Status.isDir) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000561 // If it doesn't exist, we're done.
Jeff Cohen85c716f2005-07-08 05:02:13 +0000562 if (!exists())
Chris Lattner0c332312006-07-28 22:29:50 +0000563 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000564
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000565 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000566 int lastchar = path.length() - 1 ;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000567 path.copy(pathname, lastchar+1);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000568
569 // Make path end with '/*'.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000570 if (pathname[lastchar] != '/')
571 pathname[++lastchar] = '/';
Reid Spencer1cf2d042005-07-07 23:35:23 +0000572 pathname[lastchar+1] = '*';
573 pathname[lastchar+2] = 0;
574
575 if (remove_contents) {
576 WIN32_FIND_DATA fd;
577 HANDLE h = FindFirstFile(pathname, &fd);
578
579 // It's a bad idea to alter the contents of a directory while enumerating
580 // its contents. So build a list of its contents first, then destroy them.
581
582 if (h != INVALID_HANDLE_VALUE) {
583 std::vector<Path> list;
584
585 do {
586 if (strcmp(fd.cFileName, ".") == 0)
587 continue;
588 if (strcmp(fd.cFileName, "..") == 0)
589 continue;
590
Jeff Cohen85c716f2005-07-08 05:02:13 +0000591 Path aPath(path);
592 aPath.appendComponent(&fd.cFileName[0]);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000593 list.push_back(aPath);
594 } while (FindNextFile(h, &fd));
595
596 DWORD err = GetLastError();
597 FindClose(h);
598 if (err != ERROR_NO_MORE_FILES) {
599 SetLastError(err);
Reid Spencer05545752006-08-25 21:37:17 +0000600 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000601 }
602
Jeff Cohen85c716f2005-07-08 05:02:13 +0000603 for (std::vector<Path>::iterator I = list.begin(); I != list.end();
Reid Spencer1cf2d042005-07-07 23:35:23 +0000604 ++I) {
605 Path &aPath = *I;
Reid Spencera229c5c2005-07-08 03:08:58 +0000606 aPath.eraseFromDisk(true);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000607 }
608 } else {
609 if (GetLastError() != ERROR_FILE_NOT_FOUND)
Reid Spencer05545752006-08-25 21:37:17 +0000610 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000611 }
612 }
613
614 pathname[lastchar] = 0;
615 if (!RemoveDirectory(pathname))
Reid Spencer05545752006-08-25 21:37:17 +0000616 return MakeErrMsg(ErrStr,
617 std::string(pathname) + ": Can't destroy directory: ");
Chris Lattner0c332312006-07-28 22:29:50 +0000618 return false;
Reid Spencer30300992006-08-24 18:58:37 +0000619 }
620 // It appears the path doesn't exist.
621 return true;
Reid Spencerb016a372004-09-15 05:49:50 +0000622}
623
Reid Spencer3b0cc782004-12-14 18:42:13 +0000624bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
Reid Spencer3b0cc782004-12-14 18:42:13 +0000625 assert(len < 1024 && "Request for magic string too long");
626 char* buf = (char*) alloca(1 + len);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000627
628 HANDLE h = CreateFile(path.c_str(),
629 GENERIC_READ,
630 FILE_SHARE_READ,
631 NULL,
632 OPEN_EXISTING,
633 FILE_ATTRIBUTE_NORMAL,
634 NULL);
635 if (h == INVALID_HANDLE_VALUE)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000636 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000637
638 DWORD nRead = 0;
639 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
640 CloseHandle(h);
641
642 if (!ret || nRead != len)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000643 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000644
Reid Spencer3b0cc782004-12-14 18:42:13 +0000645 buf[len] = '\0';
646 Magic = buf;
647 return true;
648}
649
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000650bool
Reid Spencer5a060772006-08-23 07:30:48 +0000651Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
Jeff Cohene564de22006-05-07 02:51:51 +0000652 if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING))
Reid Spencer5a060772006-08-23 07:30:48 +0000653 return MakeErrMsg(ErrMsg, "Can't move '" + path + "' to '" + newName.path
654 + "': ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000655 return true;
656}
657
658bool
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000659Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrMsg) const {
Jeff Cohen966fa412005-07-09 18:42:49 +0000660 // FIXME: should work on directories also.
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000661 if (!si.isFile) {
662 return true;
663 }
664
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000665 HANDLE h = CreateFile(path.c_str(),
666 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
667 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000668 NULL,
669 OPEN_EXISTING,
670 FILE_ATTRIBUTE_NORMAL,
671 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000672 if (h == INVALID_HANDLE_VALUE)
Chris Lattner1bebfb52006-07-28 22:36:17 +0000673 return true;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000674
675 BY_HANDLE_FILE_INFORMATION bhfi;
676 if (!GetFileInformationByHandle(h, &bhfi)) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000677 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000678 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000679 SetLastError(err);
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000680 return MakeErrMsg(ErrMsg, path + ": GetFileInformationByHandle: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000681 }
682
683 FILETIME ft;
684 (uint64_t&)ft = si.modTime.toWin32Time();
685 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000686 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000687 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000688 if (!ret) {
689 SetLastError(err);
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000690 return MakeErrMsg(ErrMsg, path + ": SetFileTime: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000691 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000692
693 // Best we can do with Unix permission bits is to interpret the owner
694 // writable bit.
695 if (si.mode & 0200) {
696 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
697 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000698 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000699 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000700 }
701 } else {
702 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
703 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000704 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000705 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000706 }
707 }
708
Chris Lattner1bebfb52006-07-28 22:36:17 +0000709 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000710}
711
Reid Spencer30300992006-08-24 18:58:37 +0000712bool
713CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg) {
Jeff Cohencb652552004-12-24 02:38:34 +0000714 // Can't use CopyFile macro defined in Windows.h because it would mess up the
715 // above line. We use the expansion it would have in a non-UNICODE build.
716 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Reid Spencer30300992006-08-24 18:58:37 +0000717 return MakeErrMsg(ErrMsg, "Can't copy '" + Src.toString() +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000718 "' to '" + Dest.toString() + "': ");
Reid Spencer30300992006-08-24 18:58:37 +0000719 return false;
Reid Spencerc29befb2004-12-15 01:50:13 +0000720}
721
Reid Spencer30300992006-08-24 18:58:37 +0000722bool
723Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000724 if (reuse_current && !exists())
Reid Spencer30300992006-08-24 18:58:37 +0000725 return false; // File doesn't exist already, just use it!
Reid Spencerc29befb2004-12-15 01:50:13 +0000726
Jeff Cohen9437bb62005-01-27 03:49:03 +0000727 // Reserve space for -XXXXXX at the end.
728 char *FNBuffer = (char*) alloca(path.size()+8);
729 unsigned offset = path.size();
730 path.copy(FNBuffer, offset);
Reid Spencerc29befb2004-12-15 01:50:13 +0000731
Jeff Cohen966fa412005-07-09 18:42:49 +0000732 // Find a numeric suffix that isn't used by an existing file. Assume there
733 // won't be more than 1 million files with the same prefix. Probably a safe
734 // bet.
Jeff Cohen9437bb62005-01-27 03:49:03 +0000735 static unsigned FCounter = 0;
736 do {
737 sprintf(FNBuffer+offset, "-%06u", FCounter);
738 if (++FCounter > 999999)
739 FCounter = 0;
740 path = FNBuffer;
741 } while (exists());
Reid Spencer30300992006-08-24 18:58:37 +0000742 return false;
Reid Spencerc29befb2004-12-15 01:50:13 +0000743}
744
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000745bool
Reid Spencer30300992006-08-24 18:58:37 +0000746Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000747 // Make this into a unique file name
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000748 makeUnique(reuse_current, ErrMsg);
Jeff Cohen9437bb62005-01-27 03:49:03 +0000749
750 // Now go and create it
751 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
752 FILE_ATTRIBUTE_NORMAL, NULL);
753 if (h == INVALID_HANDLE_VALUE)
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000754 return MakeErrMsg(ErrMsg, path + ": can't create file");
Jeff Cohen9437bb62005-01-27 03:49:03 +0000755
756 CloseHandle(h);
Reid Spencer30300992006-08-24 18:58:37 +0000757 return false;
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000758}
759
Reid Spencerb016a372004-09-15 05:49:50 +0000760}
Reid Spencercbad7012004-09-11 04:59:30 +0000761}