blob: 8f3ceebb28b0b9b550f4b22ca53cd441e86b265e [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);
225 else if (GetLastError() != ERROR_NOT_FOUND)
226 ThrowError(std::string(path) + ": Can't get status: ");
227 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000228}
229
230bool
Reid Spencer07adb282004-11-05 22:15:36 +0000231Path::isDirectory() const {
Reid Spencerdd04df02005-07-07 23:21:43 +0000232 WIN32_FILE_ATTRIBUTE_DATA fi;
Jeff Cohen966fa412005-07-09 18:42:49 +0000233 BOOL rc = GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi);
234 if (rc)
235 return fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
236 else if (GetLastError() != ERROR_NOT_FOUND)
Reid Spencerdd04df02005-07-07 23:21:43 +0000237 ThrowError(std::string(path) + ": Can't get status: ");
Jeff Cohen966fa412005-07-09 18:42:49 +0000238 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000239}
240
Reid Spencera229c5c2005-07-08 03:08:58 +0000241bool
242Path::isHidden() const {
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000243 WIN32_FILE_ATTRIBUTE_DATA fi;
Jeff Cohen966fa412005-07-09 18:42:49 +0000244 BOOL rc = GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi);
245 if (rc)
246 return fi.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN;
247 else if (GetLastError() != ERROR_NOT_FOUND)
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000248 ThrowError(std::string(path) + ": Can't get status: ");
Jeff Cohen966fa412005-07-09 18:42:49 +0000249 return false;
250}
251
252bool
253Path::isRootDirectory() const {
254 size_t len = path.size();
255 return len > 0 && path[len-1] == '/';
Reid Spencera229c5c2005-07-08 03:08:58 +0000256}
257
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000258std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000259Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000260 // Find the last slash
261 size_t slash = path.rfind('/');
262 if (slash == std::string::npos)
263 slash = 0;
264 else
265 slash++;
266
Jeff Cohen966fa412005-07-09 18:42:49 +0000267 size_t dot = path.rfind('.');
268 if (dot == std::string::npos || dot < slash)
269 return path.substr(slash);
270 else
271 return path.substr(slash, dot - slash);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000272}
273
Reid Spencer07adb282004-11-05 22:15:36 +0000274bool Path::hasMagicNumber(const std::string &Magic) const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000275 std::string actualMagic;
276 if (getMagicNumber(actualMagic, Magic.size()))
277 return Magic == actualMagic;
278 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000279}
280
Jeff Cohen85c716f2005-07-08 05:02:13 +0000281bool
Reid Spencer07adb282004-11-05 22:15:36 +0000282Path::isBytecodeFile() const {
Reid Spencer8b2d1aa2005-07-08 17:46:10 +0000283 if (!isFile())
284 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000285 std::string actualMagic;
286 if (!getMagicNumber(actualMagic, 4))
287 return false;
288 return actualMagic == "llvc" || actualMagic == "llvm";
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000289}
290
291bool
Reid Spencerb016a372004-09-15 05:49:50 +0000292Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000293 DWORD attr = GetFileAttributes(path.c_str());
294 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000295}
296
297bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000298Path::canRead() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000299 // FIXME: take security attributes into account.
300 DWORD attr = GetFileAttributes(path.c_str());
301 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000302}
303
304bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000305Path::canWrite() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000306 // FIXME: take security attributes into account.
307 DWORD attr = GetFileAttributes(path.c_str());
308 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000309}
310
311bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000312Path::canExecute() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000313 // FIXME: take security attributes into account.
314 DWORD attr = GetFileAttributes(path.c_str());
315 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000316}
317
318std::string
319Path::getLast() const {
320 // Find the last slash
321 size_t pos = path.rfind('/');
322
323 // Handle the corner cases
324 if (pos == std::string::npos)
325 return path;
326
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000327 // If the last character is a slash, we have a root directory
328 if (pos == path.length()-1)
329 return path;
330
Reid Spencerb016a372004-09-15 05:49:50 +0000331 // Return everything after the last slash
332 return path.substr(pos+1);
333}
334
Jeff Cohen626e38e2004-12-14 05:26:43 +0000335void
336Path::getStatusInfo(StatusInfo& info) const {
337 WIN32_FILE_ATTRIBUTE_DATA fi;
338 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
339 ThrowError(std::string(path) + ": Can't get status: ");
340
341 info.fileSize = fi.nFileSizeHigh;
342 info.fileSize <<= 32;
343 info.fileSize += fi.nFileSizeLow;
344
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000345 info.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000346 info.user = 9999; // Not applicable to Windows, so...
347 info.group = 9999; // Not applicable to Windows, so...
348
349 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
350 info.modTime.fromWin32Time(ft);
351
352 info.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000353}
354
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000355static bool AddPermissionBits(const std::string& Filename, int bits) {
356 DWORD attr = GetFileAttributes(Filename.c_str());
357
358 // If it doesn't exist, we're done.
359 if (attr == INVALID_FILE_ATTRIBUTES)
360 return false;
361
362 // The best we can do to interpret Unix permission bits is to use
363 // the owner writable bit.
364 if ((attr & FILE_ATTRIBUTE_READONLY) && (bits & 0200)) {
365 if (!SetFileAttributes(Filename.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohend40a7de2004-12-31 05:07:26 +0000366 ThrowError(Filename + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000367 }
368 return true;
369}
370
Reid Spencera229c5c2005-07-08 03:08:58 +0000371void Path::makeReadableOnDisk() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000372 // All files are readable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000373}
374
Reid Spencera229c5c2005-07-08 03:08:58 +0000375void Path::makeWriteableOnDisk() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000376 DWORD attr = GetFileAttributes(path.c_str());
377
378 // If it doesn't exist, we're done.
379 if (attr == INVALID_FILE_ATTRIBUTES)
380 return;
381
382 if (attr & FILE_ATTRIBUTE_READONLY) {
383 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
384 ThrowError(std::string(path) + ": Can't make file writable: ");
385 }
Reid Spencer77cc91d2004-12-13 19:59:50 +0000386}
387
Reid Spencera229c5c2005-07-08 03:08:58 +0000388void Path::makeExecutableOnDisk() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000389 // All files are executable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000390}
391
Reid Spencerb016a372004-09-15 05:49:50 +0000392bool
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000393Path::getDirectoryContents(std::set<Path>& result) const {
394 if (!isDirectory())
395 return false;
396
397 result.clear();
398 WIN32_FIND_DATA fd;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000399 std::string searchpath = path;
400 if (path.size() == 0 || searchpath[path.size()-1] == '/')
Jeff Cohen85c716f2005-07-08 05:02:13 +0000401 searchpath += "*";
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000402 else
403 searchpath += "/*";
Jeff Cohen85c716f2005-07-08 05:02:13 +0000404
Jeff Cohen9437bb62005-01-27 03:49:03 +0000405 HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000406 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohen9437bb62005-01-27 03:49:03 +0000407 if (GetLastError() == ERROR_FILE_NOT_FOUND)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000408 return true; // not really an error, now is it?
Jeff Cohend40a7de2004-12-31 05:07:26 +0000409 ThrowError(path + ": Can't read directory: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000410 }
411
412 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000413 if (fd.cFileName[0] == '.')
414 continue;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000415 Path aPath(path);
416 aPath.appendComponent(&fd.cFileName[0]);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000417 result.insert(aPath);
418 } while (FindNextFile(h, &fd));
419
Jeff Cohen51b8d212004-12-31 19:01:08 +0000420 DWORD err = GetLastError();
421 FindClose(h);
422 if (err != ERROR_NO_MORE_FILES) {
423 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000424 ThrowError(path + ": Can't read directory: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000425 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000426 return true;
427}
428
429bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000430Path::set(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000431 if (a_path.size() == 0)
432 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000433 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000434 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000435 FlipBackSlashes(path);
Reid Spencer07adb282004-11-05 22:15:36 +0000436 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000437 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000438 return false;
439 }
440 return true;
441}
442
443bool
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000444Path::appendComponent(const std::string& name) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000445 if (name.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000446 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000447 std::string save(path);
448 if (!path.empty()) {
449 size_t last = path.size() - 1;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000450 if (path[last] != '/')
Reid Spencer1cf2d042005-07-07 23:35:23 +0000451 path += '/';
452 }
453 path += name;
Reid Spencer07adb282004-11-05 22:15:36 +0000454 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000455 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000456 return false;
457 }
458 return true;
459}
460
461bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000462Path::eraseComponent() {
Reid Spencerb016a372004-09-15 05:49:50 +0000463 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000464 if (slashpos == path.size() - 1 || slashpos == std::string::npos)
Reid Spencerb016a372004-09-15 05:49:50 +0000465 return false;
Jeff Cohen966fa412005-07-09 18:42:49 +0000466 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000467 path.erase(slashpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000468 if (!isValid()) {
469 path = save;
470 return false;
471 }
Reid Spencerb016a372004-09-15 05:49:50 +0000472 return true;
473}
474
475bool
Reid Spencer07adb282004-11-05 22:15:36 +0000476Path::appendSuffix(const std::string& suffix) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000477 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000478 path.append(".");
479 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000480 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000481 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000482 return false;
483 }
484 return true;
485}
486
487bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000488Path::eraseSuffix() {
Reid Spencerb016a372004-09-15 05:49:50 +0000489 size_t dotpos = path.rfind('.',path.size());
490 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000491 if (dotpos != std::string::npos) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000492 if (slashpos == std::string::npos || dotpos > slashpos+1) {
493 std::string save(path);
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000494 path.erase(dotpos, path.size()-dotpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000495 if (!isValid()) {
496 path = save;
497 return false;
498 }
Jeff Cohen85c716f2005-07-08 05:02:13 +0000499 return true;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000500 }
Reid Spencerb016a372004-09-15 05:49:50 +0000501 }
502 return false;
503}
504
Reid Spencerb016a372004-09-15 05:49:50 +0000505bool
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000506Path::createDirectoryOnDisk(bool create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000507 // Get a writeable copy of the path name
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000508 size_t len = path.length();
509 char *pathname = reinterpret_cast<char *>(_alloca(len+2));
510 path.copy(pathname, len);
511 pathname[len] = 0;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000512
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000513 // Make sure it ends with a slash.
514 if (len == 0 || pathname[len - 1] != '/') {
515 pathname[len] = '/';
516 pathname[++len] = 0;
517 }
Reid Spencerb016a372004-09-15 05:49:50 +0000518
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000519 // Determine starting point for initial / search.
520 char *next = pathname;
521 if (pathname[0] == '/' && pathname[1] == '/') {
522 // Skip host name.
523 next = strchr(pathname+2, '/');
524 if (next == NULL)
525 throw std::string(pathname) + ": badly formed remote directory";
526 // Skip share name.
527 next = strchr(next+1, '/');
528 if (next == NULL)
529 throw std::string(pathname) + ": badly formed remote directory";
530 next++;
531 if (*next == 0)
532 throw std::string(pathname) + ": badly formed remote directory";
533 } else {
534 if (pathname[1] == ':')
535 next += 2; // skip drive letter
536 if (*next == '/')
537 next++; // skip root directory
538 }
Reid Spencerb016a372004-09-15 05:49:50 +0000539
540 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000541 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000542 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000543 while (*next) {
544 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000545 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000546 if (!CreateDirectory(pathname, NULL))
547 ThrowError(std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000548 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000549 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000550 } else {
551 // Drop trailing slash.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000552 pathname[len-1] = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000553 if (!CreateDirectory(pathname, NULL)) {
554 ThrowError(std::string(pathname) + ": Can't create directory: ");
555 }
Reid Spencerb016a372004-09-15 05:49:50 +0000556 }
557 return true;
558}
559
560bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000561Path::createFileOnDisk() {
Reid Spencerb016a372004-09-15 05:49:50 +0000562 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000563 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000564 FILE_ATTRIBUTE_NORMAL, NULL);
565 if (h == INVALID_HANDLE_VALUE)
Jeff Cohen51b8d212004-12-31 19:01:08 +0000566 ThrowError(path + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000567
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000568 CloseHandle(h);
Reid Spencerb016a372004-09-15 05:49:50 +0000569 return true;
570}
571
572bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000573Path::eraseFromDisk(bool remove_contents) const {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000574 if (isFile()) {
575 DWORD attr = GetFileAttributes(path.c_str());
Reid Spencerb016a372004-09-15 05:49:50 +0000576
Reid Spencer1cf2d042005-07-07 23:35:23 +0000577 // If it doesn't exist, we're done.
578 if (attr == INVALID_FILE_ATTRIBUTES)
579 return true;
Reid Spencerb016a372004-09-15 05:49:50 +0000580
Reid Spencer1cf2d042005-07-07 23:35:23 +0000581 // Read-only files cannot be deleted on Windows. Must remove the read-only
582 // attribute first.
583 if (attr & FILE_ATTRIBUTE_READONLY) {
584 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
585 ThrowError(path + ": Can't destroy file: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000586 }
Jeff Cohen51b8d212004-12-31 19:01:08 +0000587
Reid Spencer1cf2d042005-07-07 23:35:23 +0000588 if (!DeleteFile(path.c_str()))
Jeff Cohen51b8d212004-12-31 19:01:08 +0000589 ThrowError(path + ": Can't destroy file: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000590 return true;
Jeff Cohen966fa412005-07-09 18:42:49 +0000591 } else if (isDirectory()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000592 // If it doesn't exist, we're done.
Jeff Cohen85c716f2005-07-08 05:02:13 +0000593 if (!exists())
Reid Spencer1cf2d042005-07-07 23:35:23 +0000594 return true;
595
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000596 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000597 int lastchar = path.length() - 1 ;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000598 path.copy(pathname, lastchar+1);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000599
600 // Make path end with '/*'.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000601 if (pathname[lastchar] != '/')
602 pathname[++lastchar] = '/';
Reid Spencer1cf2d042005-07-07 23:35:23 +0000603 pathname[lastchar+1] = '*';
604 pathname[lastchar+2] = 0;
605
606 if (remove_contents) {
607 WIN32_FIND_DATA fd;
608 HANDLE h = FindFirstFile(pathname, &fd);
609
610 // It's a bad idea to alter the contents of a directory while enumerating
611 // its contents. So build a list of its contents first, then destroy them.
612
613 if (h != INVALID_HANDLE_VALUE) {
614 std::vector<Path> list;
615
616 do {
617 if (strcmp(fd.cFileName, ".") == 0)
618 continue;
619 if (strcmp(fd.cFileName, "..") == 0)
620 continue;
621
Jeff Cohen85c716f2005-07-08 05:02:13 +0000622 Path aPath(path);
623 aPath.appendComponent(&fd.cFileName[0]);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000624 list.push_back(aPath);
625 } while (FindNextFile(h, &fd));
626
627 DWORD err = GetLastError();
628 FindClose(h);
629 if (err != ERROR_NO_MORE_FILES) {
630 SetLastError(err);
631 ThrowError(path + ": Can't read directory: ");
632 }
633
Jeff Cohen85c716f2005-07-08 05:02:13 +0000634 for (std::vector<Path>::iterator I = list.begin(); I != list.end();
Reid Spencer1cf2d042005-07-07 23:35:23 +0000635 ++I) {
636 Path &aPath = *I;
Reid Spencera229c5c2005-07-08 03:08:58 +0000637 aPath.eraseFromDisk(true);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000638 }
639 } else {
640 if (GetLastError() != ERROR_FILE_NOT_FOUND)
641 ThrowError(path + ": Can't read directory: ");
642 }
643 }
644
645 pathname[lastchar] = 0;
646 if (!RemoveDirectory(pathname))
647 ThrowError(std::string(pathname) + ": Can't destroy directory: ");
648 return true;
Jeff Cohen966fa412005-07-09 18:42:49 +0000649 } else {
650 // It appears the path doesn't exist.
651 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000652 }
Reid Spencerb016a372004-09-15 05:49:50 +0000653}
654
Reid Spencer3b0cc782004-12-14 18:42:13 +0000655bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
656 if (!isFile())
657 return false;
658 assert(len < 1024 && "Request for magic string too long");
659 char* buf = (char*) alloca(1 + len);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000660
661 HANDLE h = CreateFile(path.c_str(),
662 GENERIC_READ,
663 FILE_SHARE_READ,
664 NULL,
665 OPEN_EXISTING,
666 FILE_ATTRIBUTE_NORMAL,
667 NULL);
668 if (h == INVALID_HANDLE_VALUE)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000669 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000670
671 DWORD nRead = 0;
672 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
673 CloseHandle(h);
674
675 if (!ret || nRead != len)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000676 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000677
Reid Spencer3b0cc782004-12-14 18:42:13 +0000678 buf[len] = '\0';
679 Magic = buf;
680 return true;
681}
682
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000683bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000684Path::renamePathOnDisk(const Path& newName) {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000685 if (!MoveFile(path.c_str(), newName.c_str()))
Jeff Cohen85c716f2005-07-08 05:02:13 +0000686 ThrowError("Can't move '" + path +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000687 "' to '" + newName.path + "': ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000688 return true;
689}
690
691bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000692Path::setStatusInfoOnDisk(const StatusInfo& si) const {
Jeff Cohen966fa412005-07-09 18:42:49 +0000693 // FIXME: should work on directories also.
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000694 if (!isFile()) return false;
695
696 HANDLE h = CreateFile(path.c_str(),
697 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
698 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000699 NULL,
700 OPEN_EXISTING,
701 FILE_ATTRIBUTE_NORMAL,
702 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000703 if (h == INVALID_HANDLE_VALUE)
704 return false;
705
706 BY_HANDLE_FILE_INFORMATION bhfi;
707 if (!GetFileInformationByHandle(h, &bhfi)) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000708 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000709 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000710 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000711 ThrowError(path + ": GetFileInformationByHandle: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000712 }
713
714 FILETIME ft;
715 (uint64_t&)ft = si.modTime.toWin32Time();
716 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000717 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000718 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000719 if (!ret) {
720 SetLastError(err);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000721 ThrowError(path + ": SetFileTime: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000722 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000723
724 // Best we can do with Unix permission bits is to interpret the owner
725 // writable bit.
726 if (si.mode & 0200) {
727 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
728 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000729 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000730 ThrowError(path + ": SetFileAttributes: ");
731 }
732 } else {
733 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
734 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000735 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000736 ThrowError(path + ": SetFileAttributes: ");
737 }
738 }
739
740 return true;
741}
742
Jeff Cohen85c716f2005-07-08 05:02:13 +0000743void
Jeff Cohen10a59ce2006-04-29 18:41:44 +0000744CopyFile(const sys::Path &Dest, const sys::Path &Src) {
Jeff Cohencb652552004-12-24 02:38:34 +0000745 // Can't use CopyFile macro defined in Windows.h because it would mess up the
746 // above line. We use the expansion it would have in a non-UNICODE build.
747 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Jeff Cohen85c716f2005-07-08 05:02:13 +0000748 ThrowError("Can't copy '" + Src.toString() +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000749 "' to '" + Dest.toString() + "': ");
Reid Spencerc29befb2004-12-15 01:50:13 +0000750}
751
Jeff Cohen85c716f2005-07-08 05:02:13 +0000752void
Jeff Cohencb652552004-12-24 02:38:34 +0000753Path::makeUnique(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000754 if (reuse_current && !exists())
Reid Spencerc29befb2004-12-15 01:50:13 +0000755 return; // File doesn't exist already, just use it!
756
Jeff Cohen9437bb62005-01-27 03:49:03 +0000757 // Reserve space for -XXXXXX at the end.
758 char *FNBuffer = (char*) alloca(path.size()+8);
759 unsigned offset = path.size();
760 path.copy(FNBuffer, offset);
Reid Spencerc29befb2004-12-15 01:50:13 +0000761
Jeff Cohen966fa412005-07-09 18:42:49 +0000762 // Find a numeric suffix that isn't used by an existing file. Assume there
763 // won't be more than 1 million files with the same prefix. Probably a safe
764 // bet.
Jeff Cohen9437bb62005-01-27 03:49:03 +0000765 static unsigned FCounter = 0;
766 do {
767 sprintf(FNBuffer+offset, "-%06u", FCounter);
768 if (++FCounter > 999999)
769 FCounter = 0;
770 path = FNBuffer;
771 } while (exists());
Reid Spencerc29befb2004-12-15 01:50:13 +0000772}
773
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000774bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000775Path::createTemporaryFileOnDisk(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000776 // Make this into a unique file name
Jeff Cohen966fa412005-07-09 18:42:49 +0000777 makeUnique(reuse_current);
Jeff Cohen9437bb62005-01-27 03:49:03 +0000778
779 // Now go and create it
780 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
781 FILE_ATTRIBUTE_NORMAL, NULL);
782 if (h == INVALID_HANDLE_VALUE)
783 return false;
784
785 CloseHandle(h);
Jeff Cohenf8cdb852004-12-18 06:42:15 +0000786 return true;
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000787}
788
Reid Spencerb016a372004-09-15 05:49:50 +0000789}
Reid Spencercbad7012004-09-11 04:59:30 +0000790}
791
Reid Spencerb016a372004-09-15 05:49:50 +0000792