blob: f7be77f8c90945b5263b82ecf2ca0a4e86013c4c [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.
124 sprintf(pathname, "LLVM_%u", 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
219bool
Reid Spencer07adb282004-11-05 22:15:36 +0000220Path::isFile() const {
Jeff Cohen966fa412005-07-09 18:42:49 +0000221 WIN32_FILE_ATTRIBUTE_DATA fi;
222 BOOL rc = GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi);
223 if (rc)
224 return !(fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
Reid Spencer3e0c1542006-06-05 15:44:46 +0000225 else if (GetLastError() != ERROR_FILE_NOT_FOUND) {
226 ThrowError("isFile(): " + std::string(path) + ": Can't get status: ");
227 }
Jeff Cohen966fa412005-07-09 18:42:49 +0000228 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000229}
230
231bool
Reid Spencer07adb282004-11-05 22:15:36 +0000232Path::isDirectory() const {
Reid Spencerdd04df02005-07-07 23:21:43 +0000233 WIN32_FILE_ATTRIBUTE_DATA fi;
Jeff Cohen966fa412005-07-09 18:42:49 +0000234 BOOL rc = GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi);
235 if (rc)
236 return fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Reid Spencer3e0c1542006-06-05 15:44:46 +0000237 else if (GetLastError() != ERROR_FILE_NOT_FOUND)
238 ThrowError("isDirectory(): " + std::string(path) + ": Can't get status: ");
Jeff Cohen966fa412005-07-09 18:42:49 +0000239 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000240}
241
Reid Spencera229c5c2005-07-08 03:08:58 +0000242bool
243Path::isHidden() const {
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000244 WIN32_FILE_ATTRIBUTE_DATA fi;
Jeff Cohen966fa412005-07-09 18:42:49 +0000245 BOOL rc = GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi);
246 if (rc)
247 return fi.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN;
Reid Spencer3e0c1542006-06-05 15:44:46 +0000248 else if (GetLastError() != ERROR_FILE_NOT_FOUND)
249 ThrowError("isHidden(): " + std::string(path) + ": Can't get status: ");
Jeff Cohen966fa412005-07-09 18:42:49 +0000250 return false;
251}
252
253bool
254Path::isRootDirectory() const {
255 size_t len = path.size();
256 return len > 0 && path[len-1] == '/';
Reid Spencera229c5c2005-07-08 03:08:58 +0000257}
258
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000259std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000260Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000261 // Find the last slash
262 size_t slash = path.rfind('/');
263 if (slash == std::string::npos)
264 slash = 0;
265 else
266 slash++;
267
Jeff Cohen966fa412005-07-09 18:42:49 +0000268 size_t dot = path.rfind('.');
269 if (dot == std::string::npos || dot < slash)
270 return path.substr(slash);
271 else
272 return path.substr(slash, dot - slash);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000273}
274
Reid Spencer07adb282004-11-05 22:15:36 +0000275bool Path::hasMagicNumber(const std::string &Magic) const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000276 std::string actualMagic;
277 if (getMagicNumber(actualMagic, Magic.size()))
278 return Magic == actualMagic;
279 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000280}
281
Jeff Cohen85c716f2005-07-08 05:02:13 +0000282bool
Reid Spencer07adb282004-11-05 22:15:36 +0000283Path::isBytecodeFile() const {
Reid Spencer8b2d1aa2005-07-08 17:46:10 +0000284 if (!isFile())
285 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000286 std::string actualMagic;
287 if (!getMagicNumber(actualMagic, 4))
288 return false;
289 return actualMagic == "llvc" || actualMagic == "llvm";
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000290}
291
292bool
Reid Spencerb016a372004-09-15 05:49:50 +0000293Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000294 DWORD attr = GetFileAttributes(path.c_str());
295 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000296}
297
298bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000299Path::canRead() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000300 // FIXME: take security attributes into account.
301 DWORD attr = GetFileAttributes(path.c_str());
302 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000303}
304
305bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000306Path::canWrite() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000307 // FIXME: take security attributes into account.
308 DWORD attr = GetFileAttributes(path.c_str());
309 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000310}
311
312bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000313Path::canExecute() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000314 // FIXME: take security attributes into account.
315 DWORD attr = GetFileAttributes(path.c_str());
316 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000317}
318
319std::string
320Path::getLast() const {
321 // Find the last slash
322 size_t pos = path.rfind('/');
323
324 // Handle the corner cases
325 if (pos == std::string::npos)
326 return path;
327
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000328 // If the last character is a slash, we have a root directory
329 if (pos == path.length()-1)
330 return path;
331
Reid Spencerb016a372004-09-15 05:49:50 +0000332 // Return everything after the last slash
333 return path.substr(pos+1);
334}
335
Jeff Cohen626e38e2004-12-14 05:26:43 +0000336void
337Path::getStatusInfo(StatusInfo& info) const {
338 WIN32_FILE_ATTRIBUTE_DATA fi;
339 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
Reid Spencer3e0c1542006-06-05 15:44:46 +0000340 ThrowError("getStatusInfo():" + std::string(path) + ": Can't get status: ");
Jeff Cohen626e38e2004-12-14 05:26:43 +0000341
342 info.fileSize = fi.nFileSizeHigh;
343 info.fileSize <<= 32;
344 info.fileSize += fi.nFileSizeLow;
345
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000346 info.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000347 info.user = 9999; // Not applicable to Windows, so...
348 info.group = 9999; // Not applicable to Windows, so...
349
350 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
351 info.modTime.fromWin32Time(ft);
352
353 info.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000354}
355
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000356static bool AddPermissionBits(const std::string& Filename, int bits) {
357 DWORD attr = GetFileAttributes(Filename.c_str());
358
359 // If it doesn't exist, we're done.
360 if (attr == INVALID_FILE_ATTRIBUTES)
361 return false;
362
363 // The best we can do to interpret Unix permission bits is to use
364 // the owner writable bit.
365 if ((attr & FILE_ATTRIBUTE_READONLY) && (bits & 0200)) {
366 if (!SetFileAttributes(Filename.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohend40a7de2004-12-31 05:07:26 +0000367 ThrowError(Filename + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000368 }
369 return true;
370}
371
Reid Spencera229c5c2005-07-08 03:08:58 +0000372void Path::makeReadableOnDisk() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000373 // All files are readable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000374}
375
Reid Spencera229c5c2005-07-08 03:08:58 +0000376void Path::makeWriteableOnDisk() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000377 DWORD attr = GetFileAttributes(path.c_str());
378
379 // If it doesn't exist, we're done.
380 if (attr == INVALID_FILE_ATTRIBUTES)
381 return;
382
383 if (attr & FILE_ATTRIBUTE_READONLY) {
384 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
385 ThrowError(std::string(path) + ": Can't make file writable: ");
386 }
Reid Spencer77cc91d2004-12-13 19:59:50 +0000387}
388
Reid Spencera229c5c2005-07-08 03:08:58 +0000389void Path::makeExecutableOnDisk() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000390 // All files are executable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000391}
392
Reid Spencerb016a372004-09-15 05:49:50 +0000393bool
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000394Path::getDirectoryContents(std::set<Path>& result) const {
395 if (!isDirectory())
396 return false;
397
398 result.clear();
399 WIN32_FIND_DATA fd;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000400 std::string searchpath = path;
401 if (path.size() == 0 || searchpath[path.size()-1] == '/')
Jeff Cohen85c716f2005-07-08 05:02:13 +0000402 searchpath += "*";
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000403 else
404 searchpath += "/*";
Jeff Cohen85c716f2005-07-08 05:02:13 +0000405
Jeff Cohen9437bb62005-01-27 03:49:03 +0000406 HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000407 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohen9437bb62005-01-27 03:49:03 +0000408 if (GetLastError() == ERROR_FILE_NOT_FOUND)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000409 return true; // not really an error, now is it?
Jeff Cohend40a7de2004-12-31 05:07:26 +0000410 ThrowError(path + ": Can't read directory: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000411 }
412
413 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000414 if (fd.cFileName[0] == '.')
415 continue;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000416 Path aPath(path);
417 aPath.appendComponent(&fd.cFileName[0]);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000418 result.insert(aPath);
419 } while (FindNextFile(h, &fd));
420
Jeff Cohen51b8d212004-12-31 19:01:08 +0000421 DWORD err = GetLastError();
422 FindClose(h);
423 if (err != ERROR_NO_MORE_FILES) {
424 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000425 ThrowError(path + ": Can't read directory: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000426 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000427 return true;
428}
429
430bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000431Path::set(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000432 if (a_path.size() == 0)
433 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000434 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000435 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000436 FlipBackSlashes(path);
Reid Spencer07adb282004-11-05 22:15:36 +0000437 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000438 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000439 return false;
440 }
441 return true;
442}
443
444bool
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000445Path::appendComponent(const std::string& name) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000446 if (name.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000447 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000448 std::string save(path);
449 if (!path.empty()) {
450 size_t last = path.size() - 1;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000451 if (path[last] != '/')
Reid Spencer1cf2d042005-07-07 23:35:23 +0000452 path += '/';
453 }
454 path += name;
Reid Spencer07adb282004-11-05 22:15:36 +0000455 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000456 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000457 return false;
458 }
459 return true;
460}
461
462bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000463Path::eraseComponent() {
Reid Spencerb016a372004-09-15 05:49:50 +0000464 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000465 if (slashpos == path.size() - 1 || slashpos == std::string::npos)
Reid Spencerb016a372004-09-15 05:49:50 +0000466 return false;
Jeff Cohen966fa412005-07-09 18:42:49 +0000467 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000468 path.erase(slashpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000469 if (!isValid()) {
470 path = save;
471 return false;
472 }
Reid Spencerb016a372004-09-15 05:49:50 +0000473 return true;
474}
475
476bool
Reid Spencer07adb282004-11-05 22:15:36 +0000477Path::appendSuffix(const std::string& suffix) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000478 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000479 path.append(".");
480 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000481 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000482 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000483 return false;
484 }
485 return true;
486}
487
488bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000489Path::eraseSuffix() {
Reid Spencerb016a372004-09-15 05:49:50 +0000490 size_t dotpos = path.rfind('.',path.size());
491 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000492 if (dotpos != std::string::npos) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000493 if (slashpos == std::string::npos || dotpos > slashpos+1) {
494 std::string save(path);
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000495 path.erase(dotpos, path.size()-dotpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000496 if (!isValid()) {
497 path = save;
498 return false;
499 }
Jeff Cohen85c716f2005-07-08 05:02:13 +0000500 return true;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000501 }
Reid Spencerb016a372004-09-15 05:49:50 +0000502 }
503 return false;
504}
505
Reid Spencerb016a372004-09-15 05:49:50 +0000506bool
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000507Path::createDirectoryOnDisk(bool create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000508 // Get a writeable copy of the path name
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000509 size_t len = path.length();
510 char *pathname = reinterpret_cast<char *>(_alloca(len+2));
511 path.copy(pathname, len);
512 pathname[len] = 0;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000513
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000514 // Make sure it ends with a slash.
515 if (len == 0 || pathname[len - 1] != '/') {
516 pathname[len] = '/';
517 pathname[++len] = 0;
518 }
Reid Spencerb016a372004-09-15 05:49:50 +0000519
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000520 // Determine starting point for initial / search.
521 char *next = pathname;
522 if (pathname[0] == '/' && pathname[1] == '/') {
523 // Skip host name.
524 next = strchr(pathname+2, '/');
525 if (next == NULL)
526 throw std::string(pathname) + ": badly formed remote directory";
527 // Skip share name.
528 next = strchr(next+1, '/');
529 if (next == NULL)
530 throw std::string(pathname) + ": badly formed remote directory";
531 next++;
532 if (*next == 0)
533 throw std::string(pathname) + ": badly formed remote directory";
534 } else {
535 if (pathname[1] == ':')
536 next += 2; // skip drive letter
537 if (*next == '/')
538 next++; // skip root directory
539 }
Reid Spencerb016a372004-09-15 05:49:50 +0000540
541 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000542 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000543 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000544 while (*next) {
545 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000546 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000547 if (!CreateDirectory(pathname, NULL))
548 ThrowError(std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000549 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000550 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000551 } else {
552 // Drop trailing slash.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000553 pathname[len-1] = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000554 if (!CreateDirectory(pathname, NULL)) {
555 ThrowError(std::string(pathname) + ": Can't create directory: ");
556 }
Reid Spencerb016a372004-09-15 05:49:50 +0000557 }
558 return true;
559}
560
561bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000562Path::createFileOnDisk() {
Reid Spencerb016a372004-09-15 05:49:50 +0000563 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000564 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000565 FILE_ATTRIBUTE_NORMAL, NULL);
566 if (h == INVALID_HANDLE_VALUE)
Jeff Cohen51b8d212004-12-31 19:01:08 +0000567 ThrowError(path + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000568
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000569 CloseHandle(h);
Reid Spencerb016a372004-09-15 05:49:50 +0000570 return true;
571}
572
573bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000574Path::eraseFromDisk(bool remove_contents) const {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000575 if (isFile()) {
576 DWORD attr = GetFileAttributes(path.c_str());
Reid Spencerb016a372004-09-15 05:49:50 +0000577
Reid Spencer1cf2d042005-07-07 23:35:23 +0000578 // If it doesn't exist, we're done.
579 if (attr == INVALID_FILE_ATTRIBUTES)
580 return true;
Reid Spencerb016a372004-09-15 05:49:50 +0000581
Reid Spencer1cf2d042005-07-07 23:35:23 +0000582 // Read-only files cannot be deleted on Windows. Must remove the read-only
583 // attribute first.
584 if (attr & FILE_ATTRIBUTE_READONLY) {
585 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
586 ThrowError(path + ": Can't destroy file: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000587 }
Jeff Cohen51b8d212004-12-31 19:01:08 +0000588
Reid Spencer1cf2d042005-07-07 23:35:23 +0000589 if (!DeleteFile(path.c_str()))
Jeff Cohen51b8d212004-12-31 19:01:08 +0000590 ThrowError(path + ": Can't destroy file: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000591 return true;
Jeff Cohen966fa412005-07-09 18:42:49 +0000592 } else if (isDirectory()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000593 // If it doesn't exist, we're done.
Jeff Cohen85c716f2005-07-08 05:02:13 +0000594 if (!exists())
Reid Spencer1cf2d042005-07-07 23:35:23 +0000595 return true;
596
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000597 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000598 int lastchar = path.length() - 1 ;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000599 path.copy(pathname, lastchar+1);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000600
601 // Make path end with '/*'.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000602 if (pathname[lastchar] != '/')
603 pathname[++lastchar] = '/';
Reid Spencer1cf2d042005-07-07 23:35:23 +0000604 pathname[lastchar+1] = '*';
605 pathname[lastchar+2] = 0;
606
607 if (remove_contents) {
608 WIN32_FIND_DATA fd;
609 HANDLE h = FindFirstFile(pathname, &fd);
610
611 // It's a bad idea to alter the contents of a directory while enumerating
612 // its contents. So build a list of its contents first, then destroy them.
613
614 if (h != INVALID_HANDLE_VALUE) {
615 std::vector<Path> list;
616
617 do {
618 if (strcmp(fd.cFileName, ".") == 0)
619 continue;
620 if (strcmp(fd.cFileName, "..") == 0)
621 continue;
622
Jeff Cohen85c716f2005-07-08 05:02:13 +0000623 Path aPath(path);
624 aPath.appendComponent(&fd.cFileName[0]);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000625 list.push_back(aPath);
626 } while (FindNextFile(h, &fd));
627
628 DWORD err = GetLastError();
629 FindClose(h);
630 if (err != ERROR_NO_MORE_FILES) {
631 SetLastError(err);
632 ThrowError(path + ": Can't read directory: ");
633 }
634
Jeff Cohen85c716f2005-07-08 05:02:13 +0000635 for (std::vector<Path>::iterator I = list.begin(); I != list.end();
Reid Spencer1cf2d042005-07-07 23:35:23 +0000636 ++I) {
637 Path &aPath = *I;
Reid Spencera229c5c2005-07-08 03:08:58 +0000638 aPath.eraseFromDisk(true);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000639 }
640 } else {
641 if (GetLastError() != ERROR_FILE_NOT_FOUND)
642 ThrowError(path + ": Can't read directory: ");
643 }
644 }
645
646 pathname[lastchar] = 0;
647 if (!RemoveDirectory(pathname))
648 ThrowError(std::string(pathname) + ": Can't destroy directory: ");
649 return true;
Jeff Cohen966fa412005-07-09 18:42:49 +0000650 } else {
651 // It appears the path doesn't exist.
652 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000653 }
Reid Spencerb016a372004-09-15 05:49:50 +0000654}
655
Reid Spencer3b0cc782004-12-14 18:42:13 +0000656bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
657 if (!isFile())
658 return false;
659 assert(len < 1024 && "Request for magic string too long");
660 char* buf = (char*) alloca(1 + len);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000661
662 HANDLE h = CreateFile(path.c_str(),
663 GENERIC_READ,
664 FILE_SHARE_READ,
665 NULL,
666 OPEN_EXISTING,
667 FILE_ATTRIBUTE_NORMAL,
668 NULL);
669 if (h == INVALID_HANDLE_VALUE)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000670 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000671
672 DWORD nRead = 0;
673 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
674 CloseHandle(h);
675
676 if (!ret || nRead != len)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000677 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000678
Reid Spencer3b0cc782004-12-14 18:42:13 +0000679 buf[len] = '\0';
680 Magic = buf;
681 return true;
682}
683
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000684bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000685Path::renamePathOnDisk(const Path& newName) {
Jeff Cohene564de22006-05-07 02:51:51 +0000686 if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING))
Jeff Cohen85c716f2005-07-08 05:02:13 +0000687 ThrowError("Can't move '" + path +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000688 "' to '" + newName.path + "': ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000689 return true;
690}
691
692bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000693Path::setStatusInfoOnDisk(const StatusInfo& si) const {
Jeff Cohen966fa412005-07-09 18:42:49 +0000694 // FIXME: should work on directories also.
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000695 if (!isFile()) return false;
696
697 HANDLE h = CreateFile(path.c_str(),
698 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
699 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000700 NULL,
701 OPEN_EXISTING,
702 FILE_ATTRIBUTE_NORMAL,
703 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000704 if (h == INVALID_HANDLE_VALUE)
705 return false;
706
707 BY_HANDLE_FILE_INFORMATION bhfi;
708 if (!GetFileInformationByHandle(h, &bhfi)) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000709 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000710 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000711 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000712 ThrowError(path + ": GetFileInformationByHandle: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000713 }
714
715 FILETIME ft;
716 (uint64_t&)ft = si.modTime.toWin32Time();
717 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000718 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000719 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000720 if (!ret) {
721 SetLastError(err);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000722 ThrowError(path + ": SetFileTime: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000723 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000724
725 // Best we can do with Unix permission bits is to interpret the owner
726 // writable bit.
727 if (si.mode & 0200) {
728 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
729 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000730 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000731 ThrowError(path + ": SetFileAttributes: ");
732 }
733 } else {
734 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
735 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000736 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000737 ThrowError(path + ": SetFileAttributes: ");
738 }
739 }
740
741 return true;
742}
743
Jeff Cohen85c716f2005-07-08 05:02:13 +0000744void
Jeff Cohen10a59ce2006-04-29 18:41:44 +0000745CopyFile(const sys::Path &Dest, const sys::Path &Src) {
Jeff Cohencb652552004-12-24 02:38:34 +0000746 // Can't use CopyFile macro defined in Windows.h because it would mess up the
747 // above line. We use the expansion it would have in a non-UNICODE build.
748 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Jeff Cohen85c716f2005-07-08 05:02:13 +0000749 ThrowError("Can't copy '" + Src.toString() +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000750 "' to '" + Dest.toString() + "': ");
Reid Spencerc29befb2004-12-15 01:50:13 +0000751}
752
Jeff Cohen85c716f2005-07-08 05:02:13 +0000753void
Jeff Cohencb652552004-12-24 02:38:34 +0000754Path::makeUnique(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000755 if (reuse_current && !exists())
Reid Spencerc29befb2004-12-15 01:50:13 +0000756 return; // File doesn't exist already, just use it!
757
Jeff Cohen9437bb62005-01-27 03:49:03 +0000758 // Reserve space for -XXXXXX at the end.
759 char *FNBuffer = (char*) alloca(path.size()+8);
760 unsigned offset = path.size();
761 path.copy(FNBuffer, offset);
Reid Spencerc29befb2004-12-15 01:50:13 +0000762
Jeff Cohen966fa412005-07-09 18:42:49 +0000763 // Find a numeric suffix that isn't used by an existing file. Assume there
764 // won't be more than 1 million files with the same prefix. Probably a safe
765 // bet.
Jeff Cohen9437bb62005-01-27 03:49:03 +0000766 static unsigned FCounter = 0;
767 do {
768 sprintf(FNBuffer+offset, "-%06u", FCounter);
769 if (++FCounter > 999999)
770 FCounter = 0;
771 path = FNBuffer;
772 } while (exists());
Reid Spencerc29befb2004-12-15 01:50:13 +0000773}
774
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000775bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000776Path::createTemporaryFileOnDisk(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000777 // Make this into a unique file name
Jeff Cohen966fa412005-07-09 18:42:49 +0000778 makeUnique(reuse_current);
Jeff Cohen9437bb62005-01-27 03:49:03 +0000779
780 // Now go and create it
781 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
782 FILE_ATTRIBUTE_NORMAL, NULL);
783 if (h == INVALID_HANDLE_VALUE)
784 return false;
785
786 CloseHandle(h);
Jeff Cohenf8cdb852004-12-18 06:42:15 +0000787 return true;
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000788}
789
Reid Spencerb016a372004-09-15 05:49:50 +0000790}
Reid Spencercbad7012004-09-11 04:59:30 +0000791}
792
Reid Spencerb016a372004-09-15 05:49:50 +0000793