blob: 6b361f0fee7affce37b3dd6c8f29316538ed6e43 [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
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000028static void FlipBackSlashes(std::string& s) {
29 for (size_t i = 0; i < s.size(); i++)
30 if (s[i] == '\\')
31 s[i] = '/';
32}
33
Reid Spencercbad7012004-09-11 04:59:30 +000034namespace llvm {
Reid Spencerb016a372004-09-15 05:49:50 +000035namespace sys {
Reid Spencercbad7012004-09-11 04:59:30 +000036
Reid Spencerb016a372004-09-15 05:49:50 +000037bool
Reid Spencer07adb282004-11-05 22:15:36 +000038Path::isValid() const {
Reid Spencerb016a372004-09-15 05:49:50 +000039 if (path.empty())
40 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000041
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000042 // If there is a colon, it must be the second character, preceded by a letter
43 // and followed by something.
44 size_t len = path.size();
45 size_t pos = path.rfind(':',len);
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000046 size_t rootslash = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000047 if (pos != std::string::npos) {
48 if (pos != 1 || !isalpha(path[0]) || len < 3)
49 return false;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000050 rootslash = 2;
51 }
52
53 // Look for a UNC path, and if found adjust our notion of the root slash.
54 if (len > 3 && path[0] == '/' && path[1] == '/') {
55 rootslash = path.find('/', 2);
56 if (rootslash == std::string::npos)
57 rootslash = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000058 }
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000059
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000060 // Check for illegal characters.
61 if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012"
62 "\013\014\015\016\017\020\021\022\023\024\025\026"
63 "\027\030\031\032\033\034\035\036\037")
64 != std::string::npos)
65 return false;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000066
67 // Remove trailing slash, unless it's a root slash.
68 if (len > rootslash+1 && path[len-1] == '/')
69 path.erase(--len);
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000070
Jeff Cohen3bbbcc12005-01-14 04:09:39 +000071 // Check each component for legality.
72 for (pos = 0; pos < len; ++pos) {
73 // A component may not end in a space.
74 if (path[pos] == ' ') {
75 if (path[pos+1] == '/' || path[pos+1] == '\0')
76 return false;
77 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000078
Jeff Cohen3bbbcc12005-01-14 04:09:39 +000079 // A component may not end in a period.
80 if (path[pos] == '.') {
81 if (path[pos+1] == '/' || path[pos+1] == '\0') {
82 // Unless it is the pseudo-directory "."...
83 if (pos == 0 || path[pos-1] == '/' || path[pos-1] == ':')
84 return true;
85 // or "..".
86 if (pos > 0 && path[pos-1] == '.') {
87 if (pos == 1 || path[pos-2] == '/' || path[pos-2] == ':')
88 return true;
89 }
90 return false;
91 }
92 }
93 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000094
95 return true;
Reid Spencercbad7012004-09-11 04:59:30 +000096}
97
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000098static Path *TempDirectory = NULL;
99
Reid Spencerb016a372004-09-15 05:49:50 +0000100Path
101Path::GetTemporaryDirectory() {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000102 if (TempDirectory)
103 return *TempDirectory;
104
105 char pathname[MAX_PATH];
106 if (!GetTempPath(MAX_PATH, pathname))
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000107 throw std::string("Can't determine temporary directory");
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000108
Reid Spencerb016a372004-09-15 05:49:50 +0000109 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000110 result.set(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000111
112 // Append a subdirectory passed on our process id so multiple LLVMs don't
113 // step on each other's toes.
114 sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000115 result.appendComponent(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000116
117 // If there's a directory left over from a previous LLVM execution that
118 // happened to have the same process id, get rid of it.
Reid Spencera229c5c2005-07-08 03:08:58 +0000119 result.eraseFromDisk(true);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000120
121 // And finally (re-)create the empty directory.
Reid Spencera229c5c2005-07-08 03:08:58 +0000122 result.createDirectoryOnDisk(false);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000123 TempDirectory = new Path(result);
124 return *TempDirectory;
Reid Spencerb016a372004-09-15 05:49:50 +0000125}
126
Reid Spencer732f92d2004-12-13 06:57:15 +0000127Path::Path(const std::string& unverified_path)
Reid Spencerb016a372004-09-15 05:49:50 +0000128 : path(unverified_path)
129{
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000130 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000131 if (unverified_path.empty())
132 return;
Reid Spencer07adb282004-11-05 22:15:36 +0000133 if (this->isValid())
Reid Spencerb016a372004-09-15 05:49:50 +0000134 return;
135 // oops, not valid.
136 path.clear();
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000137 throw std::string(unverified_path + ": path is not valid");
Reid Spencerb016a372004-09-15 05:49:50 +0000138}
139
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000140// FIXME: the following set of functions don't map to Windows very well.
Reid Spencerb016a372004-09-15 05:49:50 +0000141Path
142Path::GetRootDirectory() {
143 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000144 result.set("C:\\");
Reid Spencerb016a372004-09-15 05:49:50 +0000145 return result;
146}
147
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000148static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
149 const char* at = path;
150 const char* delim = strchr(at, ';');
151 Path tmpPath;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000152 while (delim != 0) {
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000153 std::string tmp(at, size_t(delim-at));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000154 if (tmpPath.set(tmp))
Reid Spencerc7f08322005-07-07 18:21:42 +0000155 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000156 Paths.push_back(tmpPath);
157 at = delim + 1;
158 delim = strchr(at, ';');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000159 }
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000160
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000161 if (*at != 0)
Reid Spencer1cf2d042005-07-07 23:35:23 +0000162 if (tmpPath.set(std::string(at)))
Reid Spencerc7f08322005-07-07 18:21:42 +0000163 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000164 Paths.push_back(tmpPath);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000165}
166
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000167void
168Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000169 Paths.push_back(sys::Path("C:\\WINDOWS\\SYSTEM32"));
170 Paths.push_back(sys::Path("C:\\WINDOWS"));
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000171}
172
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000173void
174Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
175 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
176 if (env_var != 0) {
177 getPathList(env_var,Paths);
178 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000179#ifdef LLVM_LIBDIR
180 {
181 Path tmpPath;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000182 if (tmpPath.set(LLVM_LIBDIR))
Reid Spencerc7f08322005-07-07 18:21:42 +0000183 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000184 Paths.push_back(tmpPath);
185 }
186#endif
187 GetSystemLibraryPaths(Paths);
Reid Spencerb016a372004-09-15 05:49:50 +0000188}
189
190Path
191Path::GetLLVMDefaultConfigDir() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000192 // TODO: this isn't going to fly on Windows
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000193 return Path("/etc/llvm");
Reid Spencerb016a372004-09-15 05:49:50 +0000194}
195
196Path
Reid Spencerb016a372004-09-15 05:49:50 +0000197Path::GetUserHomeDirectory() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000198 // TODO: Typical Windows setup doesn't define HOME.
Reid Spencerb016a372004-09-15 05:49:50 +0000199 const char* home = getenv("HOME");
200 if (home) {
201 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000202 if (result.set(home))
Reid Spencerb016a372004-09-15 05:49:50 +0000203 return result;
204 }
205 return GetRootDirectory();
206}
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000207// FIXME: the above set of functions don't map to Windows very well.
208
209bool
Reid Spencer07adb282004-11-05 22:15:36 +0000210Path::isFile() const {
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000211 return !isDirectory();
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000212}
213
214bool
Reid Spencer07adb282004-11-05 22:15:36 +0000215Path::isDirectory() const {
Reid Spencerdd04df02005-07-07 23:21:43 +0000216 WIN32_FILE_ATTRIBUTE_DATA fi;
217 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
218 ThrowError(std::string(path) + ": Can't get status: ");
219 return fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000220}
221
Reid Spencera229c5c2005-07-08 03:08:58 +0000222bool
223Path::isHidden() const {
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000224 WIN32_FILE_ATTRIBUTE_DATA fi;
225 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
226 ThrowError(std::string(path) + ": Can't get status: ");
227 return fi.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN;
Reid Spencera229c5c2005-07-08 03:08:58 +0000228}
229
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000230std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000231Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000232 // Find the last slash
233 size_t slash = path.rfind('/');
234 if (slash == std::string::npos)
235 slash = 0;
236 else
237 slash++;
238
239 return path.substr(slash, path.rfind('.'));
240}
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
249bool
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
Jeff Cohen626e38e2004-12-14 05:26:43 +0000301void
302Path::getStatusInfo(StatusInfo& info) const {
303 WIN32_FILE_ATTRIBUTE_DATA fi;
304 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
305 ThrowError(std::string(path) + ": Can't get status: ");
306
307 info.fileSize = fi.nFileSizeHigh;
308 info.fileSize <<= 32;
309 info.fileSize += fi.nFileSizeLow;
310
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000311 info.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000312 info.user = 9999; // Not applicable to Windows, so...
313 info.group = 9999; // Not applicable to Windows, so...
314
315 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
316 info.modTime.fromWin32Time(ft);
317
318 info.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000319}
320
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000321static bool AddPermissionBits(const std::string& Filename, int bits) {
322 DWORD attr = GetFileAttributes(Filename.c_str());
323
324 // If it doesn't exist, we're done.
325 if (attr == INVALID_FILE_ATTRIBUTES)
326 return false;
327
328 // The best we can do to interpret Unix permission bits is to use
329 // the owner writable bit.
330 if ((attr & FILE_ATTRIBUTE_READONLY) && (bits & 0200)) {
331 if (!SetFileAttributes(Filename.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohend40a7de2004-12-31 05:07:26 +0000332 ThrowError(Filename + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000333 }
334 return true;
335}
336
Reid Spencera229c5c2005-07-08 03:08:58 +0000337void Path::makeReadableOnDisk() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000338 // All files are readable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000339}
340
Reid Spencera229c5c2005-07-08 03:08:58 +0000341void Path::makeWriteableOnDisk() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000342 DWORD attr = GetFileAttributes(path.c_str());
343
344 // If it doesn't exist, we're done.
345 if (attr == INVALID_FILE_ATTRIBUTES)
346 return;
347
348 if (attr & FILE_ATTRIBUTE_READONLY) {
349 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
350 ThrowError(std::string(path) + ": Can't make file writable: ");
351 }
Reid Spencer77cc91d2004-12-13 19:59:50 +0000352}
353
Reid Spencera229c5c2005-07-08 03:08:58 +0000354void Path::makeExecutableOnDisk() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000355 // All files are executable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000356}
357
Reid Spencerb016a372004-09-15 05:49:50 +0000358bool
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000359Path::getDirectoryContents(std::set<Path>& result) const {
360 if (!isDirectory())
361 return false;
362
363 result.clear();
364 WIN32_FIND_DATA fd;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000365 std::string searchpath = path;
366 if (path.size() == 0 || searchpath[path.size()-1] == '/')
367 searchpath += "*";
368 else
369 searchpath += "/*";
370
371
Jeff Cohen9437bb62005-01-27 03:49:03 +0000372 HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000373 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohen9437bb62005-01-27 03:49:03 +0000374 if (GetLastError() == ERROR_FILE_NOT_FOUND)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000375 return true; // not really an error, now is it?
Jeff Cohend40a7de2004-12-31 05:07:26 +0000376 ThrowError(path + ": Can't read directory: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000377 }
378
379 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000380 if (fd.cFileName[0] == '.')
381 continue;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000382 Path aPath(path);
383 aPath.appendComponent(&fd.cFileName[0]);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000384 result.insert(aPath);
385 } while (FindNextFile(h, &fd));
386
Jeff Cohen51b8d212004-12-31 19:01:08 +0000387 DWORD err = GetLastError();
388 FindClose(h);
389 if (err != ERROR_NO_MORE_FILES) {
390 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000391 ThrowError(path + ": Can't read directory: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000392 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000393 return true;
394}
395
396bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000397Path::set(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000398 if (a_path.size() == 0)
399 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000400 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000401 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000402 FlipBackSlashes(path);
Reid Spencer07adb282004-11-05 22:15:36 +0000403 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000404 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000405 return false;
406 }
407 return true;
408}
409
410bool
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000411Path::appendComponent(const std::string& name) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000412 if (name.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000413 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000414 std::string save(path);
415 if (!path.empty()) {
416 size_t last = path.size() - 1;
417 if (path[last] != '/')
418 path += '/';
419 }
420 path += name;
Reid Spencer07adb282004-11-05 22:15:36 +0000421 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000422 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000423 return false;
424 }
425 return true;
426}
427
428bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000429Path::eraseComponent() {
Reid Spencerb016a372004-09-15 05:49:50 +0000430 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000431 if (slashpos == path.size() - 1 || slashpos == std::string::npos)
Reid Spencerb016a372004-09-15 05:49:50 +0000432 return false;
433 path.erase(slashpos);
434 return true;
435}
436
437bool
Reid Spencer07adb282004-11-05 22:15:36 +0000438Path::appendSuffix(const std::string& suffix) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000439 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000440 path.append(".");
441 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000442 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000443 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000444 return false;
445 }
446 return true;
447}
448
449bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000450Path::eraseSuffix() {
Reid Spencerb016a372004-09-15 05:49:50 +0000451 size_t dotpos = path.rfind('.',path.size());
452 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000453 if (dotpos != std::string::npos) {
454 if (slashpos == std::string::npos || dotpos > slashpos) {
455 path.erase(dotpos, path.size()-dotpos);
456 return true;
457 }
Reid Spencerb016a372004-09-15 05:49:50 +0000458 }
459 return false;
460}
461
Reid Spencerb016a372004-09-15 05:49:50 +0000462bool
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000463Path::createDirectoryOnDisk(bool create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000464 // Get a writeable copy of the path name
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000465 size_t len = path.length();
466 char *pathname = reinterpret_cast<char *>(_alloca(len+2));
467 path.copy(pathname, len);
468 pathname[len] = 0;
469
470 // Make sure it ends with a slash.
471 if (len == 0 || pathname[len - 1] != '/') {
472 pathname[len] = '/';
473 pathname[++len] = 0;
474 }
Reid Spencerb016a372004-09-15 05:49:50 +0000475
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000476 // Determine starting point for initial / search.
477 char *next = pathname;
478 if (pathname[0] == '/' && pathname[1] == '/') {
479 // Skip host name.
480 next = strchr(pathname+2, '/');
481 if (next == NULL)
482 throw std::string(pathname) + ": badly formed remote directory";
483 // Skip share name.
484 next = strchr(next+1, '/');
485 if (next == NULL)
486 throw std::string(pathname) + ": badly formed remote directory";
487 next++;
488 if (*next == 0)
489 throw std::string(pathname) + ": badly formed remote directory";
490 } else {
491 if (pathname[1] == ':')
492 next += 2; // skip drive letter
493 if (*next == '/')
494 next++; // skip root directory
495 }
Reid Spencerb016a372004-09-15 05:49:50 +0000496
497 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000498 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000499 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000500 while (*next) {
501 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000502 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000503 if (!CreateDirectory(pathname, NULL))
504 ThrowError(std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000505 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000506 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000507 } else {
508 // Drop trailing slash.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000509 pathname[len-1] = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000510 if (!CreateDirectory(pathname, NULL)) {
511 ThrowError(std::string(pathname) + ": Can't create directory: ");
512 }
Reid Spencerb016a372004-09-15 05:49:50 +0000513 }
514 return true;
515}
516
517bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000518Path::createFileOnDisk() {
Reid Spencerb016a372004-09-15 05:49:50 +0000519 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000520 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000521 FILE_ATTRIBUTE_NORMAL, NULL);
522 if (h == INVALID_HANDLE_VALUE)
Jeff Cohen51b8d212004-12-31 19:01:08 +0000523 ThrowError(path + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000524
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000525 CloseHandle(h);
Reid Spencerb016a372004-09-15 05:49:50 +0000526 return true;
527}
528
529bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000530Path::eraseFromDisk(bool remove_contents) const {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000531 if (isFile()) {
532 DWORD attr = GetFileAttributes(path.c_str());
Reid Spencerb016a372004-09-15 05:49:50 +0000533
Reid Spencer1cf2d042005-07-07 23:35:23 +0000534 // If it doesn't exist, we're done.
535 if (attr == INVALID_FILE_ATTRIBUTES)
536 return true;
Reid Spencerb016a372004-09-15 05:49:50 +0000537
Reid Spencer1cf2d042005-07-07 23:35:23 +0000538 // Read-only files cannot be deleted on Windows. Must remove the read-only
539 // attribute first.
540 if (attr & FILE_ATTRIBUTE_READONLY) {
541 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
542 ThrowError(path + ": Can't destroy file: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000543 }
Jeff Cohen51b8d212004-12-31 19:01:08 +0000544
Reid Spencer1cf2d042005-07-07 23:35:23 +0000545 if (!DeleteFile(path.c_str()))
Jeff Cohen51b8d212004-12-31 19:01:08 +0000546 ThrowError(path + ": Can't destroy file: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000547 return true;
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000548 } else /* isDirectory() */ {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000549 // If it doesn't exist, we're done.
550 if (!exists())
551 return true;
552
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000553 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000554 int lastchar = path.length() - 1 ;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000555 path.copy(pathname, lastchar+1);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000556
557 // Make path end with '/*'.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000558 if (pathname[lastchar] != '/')
559 pathname[++lastchar] = '/';
Reid Spencer1cf2d042005-07-07 23:35:23 +0000560 pathname[lastchar+1] = '*';
561 pathname[lastchar+2] = 0;
562
563 if (remove_contents) {
564 WIN32_FIND_DATA fd;
565 HANDLE h = FindFirstFile(pathname, &fd);
566
567 // It's a bad idea to alter the contents of a directory while enumerating
568 // its contents. So build a list of its contents first, then destroy them.
569
570 if (h != INVALID_HANDLE_VALUE) {
571 std::vector<Path> list;
572
573 do {
574 if (strcmp(fd.cFileName, ".") == 0)
575 continue;
576 if (strcmp(fd.cFileName, "..") == 0)
577 continue;
578
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000579 Path aPath(path);
580 aPath.appendComponent(&fd.cFileName[0]);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000581 list.push_back(aPath);
582 } while (FindNextFile(h, &fd));
583
584 DWORD err = GetLastError();
585 FindClose(h);
586 if (err != ERROR_NO_MORE_FILES) {
587 SetLastError(err);
588 ThrowError(path + ": Can't read directory: ");
589 }
590
591 for (std::vector<Path>::iterator I = list.begin(); I != list.end();
592 ++I) {
593 Path &aPath = *I;
Reid Spencera229c5c2005-07-08 03:08:58 +0000594 aPath.eraseFromDisk(true);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000595 }
596 } else {
597 if (GetLastError() != ERROR_FILE_NOT_FOUND)
598 ThrowError(path + ": Can't read directory: ");
599 }
600 }
601
602 pathname[lastchar] = 0;
603 if (!RemoveDirectory(pathname))
604 ThrowError(std::string(pathname) + ": Can't destroy directory: ");
605 return true;
606 }
Reid Spencerb016a372004-09-15 05:49:50 +0000607}
608
Reid Spencer3b0cc782004-12-14 18:42:13 +0000609bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
610 if (!isFile())
611 return false;
612 assert(len < 1024 && "Request for magic string too long");
613 char* buf = (char*) alloca(1 + len);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000614
615 HANDLE h = CreateFile(path.c_str(),
616 GENERIC_READ,
617 FILE_SHARE_READ,
618 NULL,
619 OPEN_EXISTING,
620 FILE_ATTRIBUTE_NORMAL,
621 NULL);
622 if (h == INVALID_HANDLE_VALUE)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000623 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000624
625 DWORD nRead = 0;
626 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
627 CloseHandle(h);
628
629 if (!ret || nRead != len)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000630 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000631
Reid Spencer3b0cc782004-12-14 18:42:13 +0000632 buf[len] = '\0';
633 Magic = buf;
634 return true;
635}
636
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000637bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000638Path::renamePathOnDisk(const Path& newName) {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000639 if (!MoveFile(path.c_str(), newName.c_str()))
640 ThrowError("Can't move '" + path +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000641 "' to '" + newName.path + "': ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000642 return true;
643}
644
645bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000646Path::setStatusInfoOnDisk(const StatusInfo& si) const {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000647 if (!isFile()) return false;
648
649 HANDLE h = CreateFile(path.c_str(),
650 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
651 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000652 NULL,
653 OPEN_EXISTING,
654 FILE_ATTRIBUTE_NORMAL,
655 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000656 if (h == INVALID_HANDLE_VALUE)
657 return false;
658
659 BY_HANDLE_FILE_INFORMATION bhfi;
660 if (!GetFileInformationByHandle(h, &bhfi)) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000661 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000662 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000663 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000664 ThrowError(path + ": GetFileInformationByHandle: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000665 }
666
667 FILETIME ft;
668 (uint64_t&)ft = si.modTime.toWin32Time();
669 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000670 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000671 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000672 if (!ret) {
673 SetLastError(err);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000674 ThrowError(path + ": SetFileTime: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000675 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000676
677 // Best we can do with Unix permission bits is to interpret the owner
678 // writable bit.
679 if (si.mode & 0200) {
680 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
681 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000682 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000683 ThrowError(path + ": SetFileAttributes: ");
684 }
685 } else {
686 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
687 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000688 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000689 ThrowError(path + ": SetFileAttributes: ");
690 }
691 }
692
693 return true;
694}
695
Reid Spencerc29befb2004-12-15 01:50:13 +0000696void
Reid Spencera36c9a42004-12-23 22:14:32 +0000697sys::CopyFile(const sys::Path &Dest, const sys::Path &Src) {
Jeff Cohencb652552004-12-24 02:38:34 +0000698 // Can't use CopyFile macro defined in Windows.h because it would mess up the
699 // above line. We use the expansion it would have in a non-UNICODE build.
700 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Reid Spencerc29befb2004-12-15 01:50:13 +0000701 ThrowError("Can't copy '" + Src.toString() +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000702 "' to '" + Dest.toString() + "': ");
Reid Spencerc29befb2004-12-15 01:50:13 +0000703}
704
705void
Jeff Cohencb652552004-12-24 02:38:34 +0000706Path::makeUnique(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000707 if (reuse_current && !exists())
Reid Spencerc29befb2004-12-15 01:50:13 +0000708 return; // File doesn't exist already, just use it!
709
Jeff Cohen9437bb62005-01-27 03:49:03 +0000710 // Reserve space for -XXXXXX at the end.
711 char *FNBuffer = (char*) alloca(path.size()+8);
712 unsigned offset = path.size();
713 path.copy(FNBuffer, offset);
Reid Spencerc29befb2004-12-15 01:50:13 +0000714
Jeff Cohen9437bb62005-01-27 03:49:03 +0000715 // Find a numeric suffix that isn't used by an existing file.
716 static unsigned FCounter = 0;
717 do {
718 sprintf(FNBuffer+offset, "-%06u", FCounter);
719 if (++FCounter > 999999)
720 FCounter = 0;
721 path = FNBuffer;
722 } while (exists());
Reid Spencerc29befb2004-12-15 01:50:13 +0000723}
724
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000725bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000726Path::createTemporaryFileOnDisk(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000727 // Make this into a unique file name
728 makeUnique( reuse_current );
Jeff Cohen9437bb62005-01-27 03:49:03 +0000729
730 // Now go and create it
731 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
732 FILE_ATTRIBUTE_NORMAL, NULL);
733 if (h == INVALID_HANDLE_VALUE)
734 return false;
735
736 CloseHandle(h);
Jeff Cohenf8cdb852004-12-18 06:42:15 +0000737 return true;
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000738}
739
Reid Spencerb016a372004-09-15 05:49:50 +0000740}
Reid Spencercbad7012004-09-11 04:59:30 +0000741}
742
Reid Spencerb016a372004-09-15 05:49:50 +0000743