blob: 8bc39f498db6a8fcb2745804b0f3a1b9dd340883 [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
111Path::GetTemporaryDirectory() {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000112 if (TempDirectory)
113 return *TempDirectory;
114
115 char pathname[MAX_PATH];
116 if (!GetTempPath(MAX_PATH, pathname))
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000117 throw std::string("Can't determine temporary directory");
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000118
Reid Spencerb016a372004-09-15 05:49:50 +0000119 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000120 result.set(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000121
122 // Append a subdirectory passed on our process id so multiple LLVMs don't
123 // step on each other's toes.
Reid Spencerab4d9b02006-06-08 18:08:43 +0000124 sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId()));
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000125 result.appendComponent(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000126
127 // If there's a directory left over from a previous LLVM execution that
128 // happened to have the same process id, get rid of it.
Reid Spencera229c5c2005-07-08 03:08:58 +0000129 result.eraseFromDisk(true);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000130
131 // And finally (re-)create the empty directory.
Reid Spencera229c5c2005-07-08 03:08:58 +0000132 result.createDirectoryOnDisk(false);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000133 TempDirectory = new Path(result);
134 return *TempDirectory;
Reid Spencerb016a372004-09-15 05:49:50 +0000135}
136
Reid Spencer732f92d2004-12-13 06:57:15 +0000137Path::Path(const std::string& unverified_path)
Reid Spencerb016a372004-09-15 05:49:50 +0000138 : path(unverified_path)
139{
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000140 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000141 if (unverified_path.empty())
142 return;
Reid Spencer07adb282004-11-05 22:15:36 +0000143 if (this->isValid())
Reid Spencerb016a372004-09-15 05:49:50 +0000144 return;
145 // oops, not valid.
146 path.clear();
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000147 throw std::string(unverified_path + ": path is not valid");
Reid Spencerb016a372004-09-15 05:49:50 +0000148}
149
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000150// FIXME: the following set of functions don't map to Windows very well.
Reid Spencerb016a372004-09-15 05:49:50 +0000151Path
152Path::GetRootDirectory() {
153 Path result;
Jeff Cohen966fa412005-07-09 18:42:49 +0000154 result.set("C:/");
Reid Spencerb016a372004-09-15 05:49:50 +0000155 return result;
156}
157
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000158static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
159 const char* at = path;
160 const char* delim = strchr(at, ';');
161 Path tmpPath;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000162 while (delim != 0) {
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000163 std::string tmp(at, size_t(delim-at));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000164 if (tmpPath.set(tmp))
Reid Spencerc7f08322005-07-07 18:21:42 +0000165 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000166 Paths.push_back(tmpPath);
167 at = delim + 1;
168 delim = strchr(at, ';');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000169 }
Jeff Cohen85c716f2005-07-08 05:02:13 +0000170
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000171 if (*at != 0)
Reid Spencer1cf2d042005-07-07 23:35:23 +0000172 if (tmpPath.set(std::string(at)))
Reid Spencerc7f08322005-07-07 18:21:42 +0000173 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000174 Paths.push_back(tmpPath);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000175}
176
Jeff Cohen85c716f2005-07-08 05:02:13 +0000177void
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000178Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000179 Paths.push_back(sys::Path("C:/WINDOWS/SYSTEM32"));
180 Paths.push_back(sys::Path("C:/WINDOWS"));
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000181}
182
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000183void
184Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
185 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
186 if (env_var != 0) {
187 getPathList(env_var,Paths);
188 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000189#ifdef LLVM_LIBDIR
190 {
191 Path tmpPath;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000192 if (tmpPath.set(LLVM_LIBDIR))
Reid Spencerc7f08322005-07-07 18:21:42 +0000193 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000194 Paths.push_back(tmpPath);
195 }
196#endif
197 GetSystemLibraryPaths(Paths);
Reid Spencerb016a372004-09-15 05:49:50 +0000198}
199
200Path
201Path::GetLLVMDefaultConfigDir() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000202 // TODO: this isn't going to fly on Windows
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000203 return Path("/etc/llvm");
Reid Spencerb016a372004-09-15 05:49:50 +0000204}
205
206Path
Reid Spencerb016a372004-09-15 05:49:50 +0000207Path::GetUserHomeDirectory() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000208 // TODO: Typical Windows setup doesn't define HOME.
Reid Spencerb016a372004-09-15 05:49:50 +0000209 const char* home = getenv("HOME");
210 if (home) {
211 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000212 if (result.set(home))
Reid Spencerb016a372004-09-15 05:49:50 +0000213 return result;
214 }
215 return GetRootDirectory();
216}
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000217// FIXME: the above set of functions don't map to Windows very well.
218
Jeff Cohen966fa412005-07-09 18:42:49 +0000219
220bool
221Path::isRootDirectory() const {
222 size_t len = path.size();
223 return len > 0 && path[len-1] == '/';
Reid Spencera229c5c2005-07-08 03:08:58 +0000224}
225
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000226std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000227Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000228 // Find the last slash
229 size_t slash = path.rfind('/');
230 if (slash == std::string::npos)
231 slash = 0;
232 else
233 slash++;
234
Jeff Cohen966fa412005-07-09 18:42:49 +0000235 size_t dot = path.rfind('.');
236 if (dot == std::string::npos || dot < slash)
237 return path.substr(slash);
238 else
239 return path.substr(slash, dot - slash);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000240}
241
Reid Spencer07adb282004-11-05 22:15:36 +0000242bool Path::hasMagicNumber(const std::string &Magic) const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000243 std::string actualMagic;
244 if (getMagicNumber(actualMagic, Magic.size()))
245 return Magic == actualMagic;
246 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000247}
248
Jeff Cohen85c716f2005-07-08 05:02:13 +0000249bool
Reid Spencer07adb282004-11-05 22:15:36 +0000250Path::isBytecodeFile() const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000251 std::string actualMagic;
252 if (!getMagicNumber(actualMagic, 4))
253 return false;
254 return actualMagic == "llvc" || actualMagic == "llvm";
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000255}
256
257bool
Reid Spencerb016a372004-09-15 05:49:50 +0000258Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000259 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::canRead() 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;
Reid Spencerb016a372004-09-15 05:49:50 +0000268}
269
270bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000271Path::canWrite() 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) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000275}
276
277bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000278Path::canExecute() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000279 // FIXME: take security attributes into account.
280 DWORD attr = GetFileAttributes(path.c_str());
281 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000282}
283
284std::string
285Path::getLast() const {
286 // Find the last slash
287 size_t pos = path.rfind('/');
288
289 // Handle the corner cases
290 if (pos == std::string::npos)
291 return path;
292
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000293 // If the last character is a slash, we have a root directory
294 if (pos == path.length()-1)
295 return path;
296
Reid Spencerb016a372004-09-15 05:49:50 +0000297 // Return everything after the last slash
298 return path.substr(pos+1);
299}
300
Chris Lattner7dea1012006-07-28 22:32:09 +0000301bool
Anton Korobeynikov1a7d3262006-08-01 08:07:22 +0000302Path::getFileStatus(FileStatus &info, std::string *ErrStr) const {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000303 WIN32_FILE_ATTRIBUTE_DATA fi;
304 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
Chris Lattner7dea1012006-07-28 22:32:09 +0000305 return GetError("getStatusInfo():" + std::string(path) +
306 ": Can't get status: ", ErrStr);
Jeff Cohen626e38e2004-12-14 05:26:43 +0000307
308 info.fileSize = fi.nFileSizeHigh;
Reid Spencer393830a2006-06-08 17:00:08 +0000309 info.fileSize <<= sizeof(fi.nFileSizeHigh)*8;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000310 info.fileSize += fi.nFileSizeLow;
311
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000312 info.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000313 info.user = 9999; // Not applicable to Windows, so...
314 info.group = 9999; // Not applicable to Windows, so...
315
316 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
317 info.modTime.fromWin32Time(ft);
318
319 info.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Chris Lattner7dea1012006-07-28 22:32:09 +0000320 return false;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000321}
322
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000323static bool AddPermissionBits(const std::string& Filename, int bits) {
324 DWORD attr = GetFileAttributes(Filename.c_str());
325
326 // If it doesn't exist, we're done.
327 if (attr == INVALID_FILE_ATTRIBUTES)
328 return false;
329
330 // The best we can do to interpret Unix permission bits is to use
331 // the owner writable bit.
332 if ((attr & FILE_ATTRIBUTE_READONLY) && (bits & 0200)) {
333 if (!SetFileAttributes(Filename.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohend40a7de2004-12-31 05:07:26 +0000334 ThrowError(Filename + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000335 }
336 return true;
337}
338
Reid Spencera229c5c2005-07-08 03:08:58 +0000339void Path::makeReadableOnDisk() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000340 // All files are readable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000341}
342
Reid Spencera229c5c2005-07-08 03:08:58 +0000343void Path::makeWriteableOnDisk() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000344 DWORD attr = GetFileAttributes(path.c_str());
345
346 // If it doesn't exist, we're done.
347 if (attr == INVALID_FILE_ATTRIBUTES)
348 return;
349
350 if (attr & FILE_ATTRIBUTE_READONLY) {
351 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
352 ThrowError(std::string(path) + ": Can't make file writable: ");
353 }
Reid Spencer77cc91d2004-12-13 19:59:50 +0000354}
355
Reid Spencera229c5c2005-07-08 03:08:58 +0000356void Path::makeExecutableOnDisk() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000357 // All files are executable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000358}
359
Reid Spencerb016a372004-09-15 05:49:50 +0000360bool
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000361Path::getDirectoryContents(std::set<Path>& result) const {
362 if (!isDirectory())
363 return false;
364
365 result.clear();
366 WIN32_FIND_DATA fd;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000367 std::string searchpath = path;
368 if (path.size() == 0 || searchpath[path.size()-1] == '/')
Jeff Cohen85c716f2005-07-08 05:02:13 +0000369 searchpath += "*";
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000370 else
371 searchpath += "/*";
Jeff Cohen85c716f2005-07-08 05:02:13 +0000372
Jeff Cohen9437bb62005-01-27 03:49:03 +0000373 HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000374 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohen9437bb62005-01-27 03:49:03 +0000375 if (GetLastError() == ERROR_FILE_NOT_FOUND)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000376 return true; // not really an error, now is it?
Jeff Cohend40a7de2004-12-31 05:07:26 +0000377 ThrowError(path + ": Can't read directory: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000378 }
379
380 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000381 if (fd.cFileName[0] == '.')
382 continue;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000383 Path aPath(path);
384 aPath.appendComponent(&fd.cFileName[0]);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000385 result.insert(aPath);
386 } while (FindNextFile(h, &fd));
387
Jeff Cohen51b8d212004-12-31 19:01:08 +0000388 DWORD err = GetLastError();
389 FindClose(h);
390 if (err != ERROR_NO_MORE_FILES) {
391 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000392 ThrowError(path + ": Can't read directory: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000393 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000394 return true;
395}
396
397bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000398Path::set(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000399 if (a_path.size() == 0)
400 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000401 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000402 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000403 FlipBackSlashes(path);
Reid Spencer07adb282004-11-05 22:15:36 +0000404 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000405 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000406 return false;
407 }
408 return true;
409}
410
411bool
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000412Path::appendComponent(const std::string& name) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000413 if (name.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000414 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000415 std::string save(path);
416 if (!path.empty()) {
417 size_t last = path.size() - 1;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000418 if (path[last] != '/')
Reid Spencer1cf2d042005-07-07 23:35:23 +0000419 path += '/';
420 }
421 path += name;
Reid Spencer07adb282004-11-05 22:15:36 +0000422 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000423 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000424 return false;
425 }
426 return true;
427}
428
429bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000430Path::eraseComponent() {
Reid Spencerb016a372004-09-15 05:49:50 +0000431 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000432 if (slashpos == path.size() - 1 || slashpos == std::string::npos)
Reid Spencerb016a372004-09-15 05:49:50 +0000433 return false;
Jeff Cohen966fa412005-07-09 18:42:49 +0000434 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000435 path.erase(slashpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000436 if (!isValid()) {
437 path = save;
438 return false;
439 }
Reid Spencerb016a372004-09-15 05:49:50 +0000440 return true;
441}
442
443bool
Reid Spencer07adb282004-11-05 22:15:36 +0000444Path::appendSuffix(const std::string& suffix) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000445 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000446 path.append(".");
447 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000448 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000449 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000450 return false;
451 }
452 return true;
453}
454
455bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000456Path::eraseSuffix() {
Reid Spencerb016a372004-09-15 05:49:50 +0000457 size_t dotpos = path.rfind('.',path.size());
458 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000459 if (dotpos != std::string::npos) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000460 if (slashpos == std::string::npos || dotpos > slashpos+1) {
461 std::string save(path);
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000462 path.erase(dotpos, path.size()-dotpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000463 if (!isValid()) {
464 path = save;
465 return false;
466 }
Jeff Cohen85c716f2005-07-08 05:02:13 +0000467 return true;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000468 }
Reid Spencerb016a372004-09-15 05:49:50 +0000469 }
470 return false;
471}
472
Reid Spencerb016a372004-09-15 05:49:50 +0000473bool
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000474Path::createDirectoryOnDisk(bool create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000475 // Get a writeable copy of the path name
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000476 size_t len = path.length();
477 char *pathname = reinterpret_cast<char *>(_alloca(len+2));
478 path.copy(pathname, len);
479 pathname[len] = 0;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000480
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000481 // Make sure it ends with a slash.
482 if (len == 0 || pathname[len - 1] != '/') {
483 pathname[len] = '/';
484 pathname[++len] = 0;
485 }
Reid Spencerb016a372004-09-15 05:49:50 +0000486
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000487 // Determine starting point for initial / search.
488 char *next = pathname;
489 if (pathname[0] == '/' && pathname[1] == '/') {
490 // Skip host name.
491 next = strchr(pathname+2, '/');
492 if (next == NULL)
493 throw std::string(pathname) + ": badly formed remote directory";
494 // Skip share name.
495 next = strchr(next+1, '/');
496 if (next == NULL)
497 throw std::string(pathname) + ": badly formed remote directory";
498 next++;
499 if (*next == 0)
500 throw std::string(pathname) + ": badly formed remote directory";
501 } else {
502 if (pathname[1] == ':')
503 next += 2; // skip drive letter
504 if (*next == '/')
505 next++; // skip root directory
506 }
Reid Spencerb016a372004-09-15 05:49:50 +0000507
508 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000509 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000510 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000511 while (*next) {
512 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000513 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000514 if (!CreateDirectory(pathname, NULL))
515 ThrowError(std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000516 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000517 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000518 } else {
519 // Drop trailing slash.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000520 pathname[len-1] = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000521 if (!CreateDirectory(pathname, NULL)) {
522 ThrowError(std::string(pathname) + ": Can't create directory: ");
523 }
Reid Spencerb016a372004-09-15 05:49:50 +0000524 }
525 return true;
526}
527
528bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000529Path::createFileOnDisk() {
Reid Spencerb016a372004-09-15 05:49:50 +0000530 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000531 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000532 FILE_ATTRIBUTE_NORMAL, NULL);
533 if (h == INVALID_HANDLE_VALUE)
Jeff Cohen51b8d212004-12-31 19:01:08 +0000534 ThrowError(path + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000535
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000536 CloseHandle(h);
Reid Spencerb016a372004-09-15 05:49:50 +0000537 return true;
538}
539
540bool
Chris Lattner0c332312006-07-28 22:29:50 +0000541Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000542 FileStatus Status;
543 if (getFileStatus(Status, ErrStr))
544 return true;
545
546 if (Status.isFile) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000547 DWORD attr = GetFileAttributes(path.c_str());
Reid Spencerb016a372004-09-15 05:49:50 +0000548
Reid Spencer1cf2d042005-07-07 23:35:23 +0000549 // If it doesn't exist, we're done.
550 if (attr == INVALID_FILE_ATTRIBUTES)
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000551 return true;
Reid Spencerb016a372004-09-15 05:49:50 +0000552
Reid Spencer1cf2d042005-07-07 23:35:23 +0000553 // Read-only files cannot be deleted on Windows. Must remove the read-only
554 // attribute first.
555 if (attr & FILE_ATTRIBUTE_READONLY) {
556 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
Chris Lattner0c332312006-07-28 22:29:50 +0000557 return GetError(path + ": Can't destroy file: ", ErrStr);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000558 }
Jeff Cohen51b8d212004-12-31 19:01:08 +0000559
Reid Spencer1cf2d042005-07-07 23:35:23 +0000560 if (!DeleteFile(path.c_str()))
Jeff Cohen51b8d212004-12-31 19:01:08 +0000561 ThrowError(path + ": Can't destroy file: ");
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000562 return false;
563 } else if (Status.isDir) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000564 // If it doesn't exist, we're done.
Jeff Cohen85c716f2005-07-08 05:02:13 +0000565 if (!exists())
Chris Lattner0c332312006-07-28 22:29:50 +0000566 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000567
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000568 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000569 int lastchar = path.length() - 1 ;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000570 path.copy(pathname, lastchar+1);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000571
572 // Make path end with '/*'.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000573 if (pathname[lastchar] != '/')
574 pathname[++lastchar] = '/';
Reid Spencer1cf2d042005-07-07 23:35:23 +0000575 pathname[lastchar+1] = '*';
576 pathname[lastchar+2] = 0;
577
578 if (remove_contents) {
579 WIN32_FIND_DATA fd;
580 HANDLE h = FindFirstFile(pathname, &fd);
581
582 // It's a bad idea to alter the contents of a directory while enumerating
583 // its contents. So build a list of its contents first, then destroy them.
584
585 if (h != INVALID_HANDLE_VALUE) {
586 std::vector<Path> list;
587
588 do {
589 if (strcmp(fd.cFileName, ".") == 0)
590 continue;
591 if (strcmp(fd.cFileName, "..") == 0)
592 continue;
593
Jeff Cohen85c716f2005-07-08 05:02:13 +0000594 Path aPath(path);
595 aPath.appendComponent(&fd.cFileName[0]);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000596 list.push_back(aPath);
597 } while (FindNextFile(h, &fd));
598
599 DWORD err = GetLastError();
600 FindClose(h);
601 if (err != ERROR_NO_MORE_FILES) {
602 SetLastError(err);
Chris Lattner0c332312006-07-28 22:29:50 +0000603 return GetError(path + ": Can't read directory: ", ErrStr);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000604 }
605
Jeff Cohen85c716f2005-07-08 05:02:13 +0000606 for (std::vector<Path>::iterator I = list.begin(); I != list.end();
Reid Spencer1cf2d042005-07-07 23:35:23 +0000607 ++I) {
608 Path &aPath = *I;
Reid Spencera229c5c2005-07-08 03:08:58 +0000609 aPath.eraseFromDisk(true);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000610 }
611 } else {
612 if (GetLastError() != ERROR_FILE_NOT_FOUND)
Chris Lattner0c332312006-07-28 22:29:50 +0000613 return GetError(path + ": Can't read directory: ", ErrStr);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000614 }
615 }
616
617 pathname[lastchar] = 0;
618 if (!RemoveDirectory(pathname))
Chris Lattner0c332312006-07-28 22:29:50 +0000619 return GetError(std::string(pathname) + ": Can't destroy directory: ",
620 ErrStr);
621 return false;
Jeff Cohen966fa412005-07-09 18:42:49 +0000622 } else {
623 // It appears the path doesn't exist.
Chris Lattner0c332312006-07-28 22:29:50 +0000624 return true;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000625 }
Reid Spencerb016a372004-09-15 05:49:50 +0000626}
627
Reid Spencer3b0cc782004-12-14 18:42:13 +0000628bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
Reid Spencer3b0cc782004-12-14 18:42:13 +0000629 assert(len < 1024 && "Request for magic string too long");
630 char* buf = (char*) alloca(1 + len);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000631
632 HANDLE h = CreateFile(path.c_str(),
633 GENERIC_READ,
634 FILE_SHARE_READ,
635 NULL,
636 OPEN_EXISTING,
637 FILE_ATTRIBUTE_NORMAL,
638 NULL);
639 if (h == INVALID_HANDLE_VALUE)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000640 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000641
642 DWORD nRead = 0;
643 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
644 CloseHandle(h);
645
646 if (!ret || nRead != len)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000647 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000648
Reid Spencer3b0cc782004-12-14 18:42:13 +0000649 buf[len] = '\0';
650 Magic = buf;
651 return true;
652}
653
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000654bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000655Path::renamePathOnDisk(const Path& newName) {
Jeff Cohene564de22006-05-07 02:51:51 +0000656 if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING))
Jeff Cohen85c716f2005-07-08 05:02:13 +0000657 ThrowError("Can't move '" + path +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000658 "' to '" + newName.path + "': ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000659 return true;
660}
661
662bool
Chris Lattner1bebfb52006-07-28 22:36:17 +0000663Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrStr) const {
Jeff Cohen966fa412005-07-09 18:42:49 +0000664 // FIXME: should work on directories also.
Chris Lattner1bebfb52006-07-28 22:36:17 +0000665 if (!isFile()) return true;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000666
667 HANDLE h = CreateFile(path.c_str(),
668 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
669 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000670 NULL,
671 OPEN_EXISTING,
672 FILE_ATTRIBUTE_NORMAL,
673 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000674 if (h == INVALID_HANDLE_VALUE)
Chris Lattner1bebfb52006-07-28 22:36:17 +0000675 return true;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000676
677 BY_HANDLE_FILE_INFORMATION bhfi;
678 if (!GetFileInformationByHandle(h, &bhfi)) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000679 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000680 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000681 SetLastError(err);
Chris Lattner1bebfb52006-07-28 22:36:17 +0000682 return GetError(path + ": GetFileInformationByHandle: ", ErrStr);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000683 }
684
685 FILETIME ft;
686 (uint64_t&)ft = si.modTime.toWin32Time();
687 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
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 if (!ret) {
691 SetLastError(err);
Chris Lattner1bebfb52006-07-28 22:36:17 +0000692 return GetError(path + ": SetFileTime: ", ErrStr);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000693 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000694
695 // Best we can do with Unix permission bits is to interpret the owner
696 // writable bit.
697 if (si.mode & 0200) {
698 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
699 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000700 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
Chris Lattner1bebfb52006-07-28 22:36:17 +0000701 return GetError(path + ": SetFileAttributes: ", ErrStr);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000702 }
703 } else {
704 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
705 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000706 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
Chris Lattner1bebfb52006-07-28 22:36:17 +0000707 return GetError(path + ": SetFileAttributes: ", ErrStr);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000708 }
709 }
710
Chris Lattner1bebfb52006-07-28 22:36:17 +0000711 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000712}
713
Jeff Cohen85c716f2005-07-08 05:02:13 +0000714void
Jeff Cohen10a59ce2006-04-29 18:41:44 +0000715CopyFile(const sys::Path &Dest, const sys::Path &Src) {
Jeff Cohencb652552004-12-24 02:38:34 +0000716 // Can't use CopyFile macro defined in Windows.h because it would mess up the
717 // above line. We use the expansion it would have in a non-UNICODE build.
718 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Jeff Cohen85c716f2005-07-08 05:02:13 +0000719 ThrowError("Can't copy '" + Src.toString() +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000720 "' to '" + Dest.toString() + "': ");
Reid Spencerc29befb2004-12-15 01:50:13 +0000721}
722
Jeff Cohen85c716f2005-07-08 05:02:13 +0000723void
Jeff Cohencb652552004-12-24 02:38:34 +0000724Path::makeUnique(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000725 if (reuse_current && !exists())
Reid Spencerc29befb2004-12-15 01:50:13 +0000726 return; // File doesn't exist already, just use it!
727
Jeff Cohen9437bb62005-01-27 03:49:03 +0000728 // Reserve space for -XXXXXX at the end.
729 char *FNBuffer = (char*) alloca(path.size()+8);
730 unsigned offset = path.size();
731 path.copy(FNBuffer, offset);
Reid Spencerc29befb2004-12-15 01:50:13 +0000732
Jeff Cohen966fa412005-07-09 18:42:49 +0000733 // Find a numeric suffix that isn't used by an existing file. Assume there
734 // won't be more than 1 million files with the same prefix. Probably a safe
735 // bet.
Jeff Cohen9437bb62005-01-27 03:49:03 +0000736 static unsigned FCounter = 0;
737 do {
738 sprintf(FNBuffer+offset, "-%06u", FCounter);
739 if (++FCounter > 999999)
740 FCounter = 0;
741 path = FNBuffer;
742 } while (exists());
Reid Spencerc29befb2004-12-15 01:50:13 +0000743}
744
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000745bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000746Path::createTemporaryFileOnDisk(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000747 // Make this into a unique file name
Jeff Cohen966fa412005-07-09 18:42:49 +0000748 makeUnique(reuse_current);
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)
754 return false;
755
756 CloseHandle(h);
Jeff Cohenf8cdb852004-12-18 06:42:15 +0000757 return true;
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}