blob: 2524310443857095b528be891f0b61c902bfc930 [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 }
Jeff Cohen85c716f2005-07-08 05:02:13 +000052
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000053 // 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 Cohen85c716f2005-07-08 05:02:13 +000066
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000067 // 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 Cohen85c716f2005-07-08 05:02:13 +0000160
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000161 if (*at != 0)
Reid Spencer1cf2d042005-07-07 23:35:23 +0000162 if (tmpPath.set(std::string(at)))
Reid Spencerc7f08322005-07-07 18:21:42 +0000163 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000164 Paths.push_back(tmpPath);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000165}
166
Jeff Cohen85c716f2005-07-08 05:02:13 +0000167void
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000168Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
Jeff 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 Spencer8b2d1aa2005-07-08 17:46:10 +0000216 if (!exists())
217 return false;
Reid Spencerdd04df02005-07-07 23:21:43 +0000218 WIN32_FILE_ATTRIBUTE_DATA fi;
219 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
220 ThrowError(std::string(path) + ": Can't get status: ");
221 return fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000222}
223
Reid Spencera229c5c2005-07-08 03:08:58 +0000224bool
225Path::isHidden() const {
Reid Spencer8b2d1aa2005-07-08 17:46:10 +0000226 if (!exists())
227 return false;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000228 WIN32_FILE_ATTRIBUTE_DATA fi;
229 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
230 ThrowError(std::string(path) + ": Can't get status: ");
231 return fi.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN;
Reid Spencera229c5c2005-07-08 03:08:58 +0000232}
233
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000234std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000235Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000236 // Find the last slash
237 size_t slash = path.rfind('/');
238 if (slash == std::string::npos)
239 slash = 0;
240 else
241 slash++;
242
243 return path.substr(slash, path.rfind('.'));
244}
245
Reid Spencer07adb282004-11-05 22:15:36 +0000246bool Path::hasMagicNumber(const std::string &Magic) const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000247 std::string actualMagic;
248 if (getMagicNumber(actualMagic, Magic.size()))
249 return Magic == actualMagic;
250 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000251}
252
Jeff Cohen85c716f2005-07-08 05:02:13 +0000253bool
Reid Spencer07adb282004-11-05 22:15:36 +0000254Path::isBytecodeFile() const {
Reid Spencer8b2d1aa2005-07-08 17:46:10 +0000255 if (!isFile())
256 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000257 std::string actualMagic;
258 if (!getMagicNumber(actualMagic, 4))
259 return false;
260 return actualMagic == "llvc" || actualMagic == "llvm";
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000261}
262
263bool
Reid Spencerb016a372004-09-15 05:49:50 +0000264Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000265 DWORD attr = GetFileAttributes(path.c_str());
266 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000267}
268
269bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000270Path::canRead() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000271 // FIXME: take security attributes into account.
272 DWORD attr = GetFileAttributes(path.c_str());
273 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000274}
275
276bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000277Path::canWrite() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000278 // FIXME: take security attributes into account.
279 DWORD attr = GetFileAttributes(path.c_str());
280 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000281}
282
283bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000284Path::canExecute() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000285 // FIXME: take security attributes into account.
286 DWORD attr = GetFileAttributes(path.c_str());
287 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000288}
289
290std::string
291Path::getLast() const {
292 // Find the last slash
293 size_t pos = path.rfind('/');
294
295 // Handle the corner cases
296 if (pos == std::string::npos)
297 return path;
298
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000299 // If the last character is a slash, we have a root directory
300 if (pos == path.length()-1)
301 return path;
302
Reid Spencerb016a372004-09-15 05:49:50 +0000303 // Return everything after the last slash
304 return path.substr(pos+1);
305}
306
Jeff Cohen626e38e2004-12-14 05:26:43 +0000307void
308Path::getStatusInfo(StatusInfo& info) const {
309 WIN32_FILE_ATTRIBUTE_DATA fi;
310 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
311 ThrowError(std::string(path) + ": Can't get status: ");
312
313 info.fileSize = fi.nFileSizeHigh;
314 info.fileSize <<= 32;
315 info.fileSize += fi.nFileSizeLow;
316
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000317 info.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000318 info.user = 9999; // Not applicable to Windows, so...
319 info.group = 9999; // Not applicable to Windows, so...
320
321 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
322 info.modTime.fromWin32Time(ft);
323
324 info.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000325}
326
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000327static bool AddPermissionBits(const std::string& Filename, int bits) {
328 DWORD attr = GetFileAttributes(Filename.c_str());
329
330 // If it doesn't exist, we're done.
331 if (attr == INVALID_FILE_ATTRIBUTES)
332 return false;
333
334 // The best we can do to interpret Unix permission bits is to use
335 // the owner writable bit.
336 if ((attr & FILE_ATTRIBUTE_READONLY) && (bits & 0200)) {
337 if (!SetFileAttributes(Filename.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohend40a7de2004-12-31 05:07:26 +0000338 ThrowError(Filename + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000339 }
340 return true;
341}
342
Reid Spencera229c5c2005-07-08 03:08:58 +0000343void Path::makeReadableOnDisk() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000344 // All files are readable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000345}
346
Reid Spencera229c5c2005-07-08 03:08:58 +0000347void Path::makeWriteableOnDisk() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000348 DWORD attr = GetFileAttributes(path.c_str());
349
350 // If it doesn't exist, we're done.
351 if (attr == INVALID_FILE_ATTRIBUTES)
352 return;
353
354 if (attr & FILE_ATTRIBUTE_READONLY) {
355 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
356 ThrowError(std::string(path) + ": Can't make file writable: ");
357 }
Reid Spencer77cc91d2004-12-13 19:59:50 +0000358}
359
Reid Spencera229c5c2005-07-08 03:08:58 +0000360void Path::makeExecutableOnDisk() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000361 // All files are executable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000362}
363
Reid Spencerb016a372004-09-15 05:49:50 +0000364bool
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000365Path::getDirectoryContents(std::set<Path>& result) const {
366 if (!isDirectory())
367 return false;
368
369 result.clear();
370 WIN32_FIND_DATA fd;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000371 std::string searchpath = path;
372 if (path.size() == 0 || searchpath[path.size()-1] == '/')
Jeff Cohen85c716f2005-07-08 05:02:13 +0000373 searchpath += "*";
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000374 else
375 searchpath += "/*";
Jeff Cohen85c716f2005-07-08 05:02:13 +0000376
Jeff Cohen9437bb62005-01-27 03:49:03 +0000377 HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000378 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohen9437bb62005-01-27 03:49:03 +0000379 if (GetLastError() == ERROR_FILE_NOT_FOUND)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000380 return true; // not really an error, now is it?
Jeff Cohend40a7de2004-12-31 05:07:26 +0000381 ThrowError(path + ": Can't read directory: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000382 }
383
384 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000385 if (fd.cFileName[0] == '.')
386 continue;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000387 Path aPath(path);
388 aPath.appendComponent(&fd.cFileName[0]);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000389 result.insert(aPath);
390 } while (FindNextFile(h, &fd));
391
Jeff Cohen51b8d212004-12-31 19:01:08 +0000392 DWORD err = GetLastError();
393 FindClose(h);
394 if (err != ERROR_NO_MORE_FILES) {
395 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000396 ThrowError(path + ": Can't read directory: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000397 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000398 return true;
399}
400
401bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000402Path::set(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000403 if (a_path.size() == 0)
404 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000405 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000406 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000407 FlipBackSlashes(path);
Reid Spencer07adb282004-11-05 22:15:36 +0000408 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000409 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000410 return false;
411 }
412 return true;
413}
414
415bool
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000416Path::appendComponent(const std::string& name) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000417 if (name.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000418 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000419 std::string save(path);
420 if (!path.empty()) {
421 size_t last = path.size() - 1;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000422 if (path[last] != '/')
Reid Spencer1cf2d042005-07-07 23:35:23 +0000423 path += '/';
424 }
425 path += name;
Reid Spencer07adb282004-11-05 22:15:36 +0000426 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000427 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000428 return false;
429 }
430 return true;
431}
432
433bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000434Path::eraseComponent() {
Reid Spencerb016a372004-09-15 05:49:50 +0000435 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000436 if (slashpos == path.size() - 1 || slashpos == std::string::npos)
Reid Spencerb016a372004-09-15 05:49:50 +0000437 return false;
438 path.erase(slashpos);
439 return true;
440}
441
442bool
Reid Spencer07adb282004-11-05 22:15:36 +0000443Path::appendSuffix(const std::string& suffix) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000444 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000445 path.append(".");
446 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000447 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000448 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000449 return false;
450 }
451 return true;
452}
453
454bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000455Path::eraseSuffix() {
Reid Spencerb016a372004-09-15 05:49:50 +0000456 size_t dotpos = path.rfind('.',path.size());
457 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000458 if (dotpos != std::string::npos) {
459 if (slashpos == std::string::npos || dotpos > slashpos) {
460 path.erase(dotpos, path.size()-dotpos);
Jeff Cohen85c716f2005-07-08 05:02:13 +0000461 return true;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000462 }
Reid Spencerb016a372004-09-15 05:49:50 +0000463 }
464 return false;
465}
466
Reid Spencerb016a372004-09-15 05:49:50 +0000467bool
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000468Path::createDirectoryOnDisk(bool create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000469 // Get a writeable copy of the path name
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000470 size_t len = path.length();
471 char *pathname = reinterpret_cast<char *>(_alloca(len+2));
472 path.copy(pathname, len);
473 pathname[len] = 0;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000474
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000475 // Make sure it ends with a slash.
476 if (len == 0 || pathname[len - 1] != '/') {
477 pathname[len] = '/';
478 pathname[++len] = 0;
479 }
Reid Spencerb016a372004-09-15 05:49:50 +0000480
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000481 // Determine starting point for initial / search.
482 char *next = pathname;
483 if (pathname[0] == '/' && pathname[1] == '/') {
484 // Skip host name.
485 next = strchr(pathname+2, '/');
486 if (next == NULL)
487 throw std::string(pathname) + ": badly formed remote directory";
488 // Skip share name.
489 next = strchr(next+1, '/');
490 if (next == NULL)
491 throw std::string(pathname) + ": badly formed remote directory";
492 next++;
493 if (*next == 0)
494 throw std::string(pathname) + ": badly formed remote directory";
495 } else {
496 if (pathname[1] == ':')
497 next += 2; // skip drive letter
498 if (*next == '/')
499 next++; // skip root directory
500 }
Reid Spencerb016a372004-09-15 05:49:50 +0000501
502 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000503 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000504 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000505 while (*next) {
506 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000507 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000508 if (!CreateDirectory(pathname, NULL))
509 ThrowError(std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000510 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000511 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000512 } else {
513 // Drop trailing slash.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000514 pathname[len-1] = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000515 if (!CreateDirectory(pathname, NULL)) {
516 ThrowError(std::string(pathname) + ": Can't create directory: ");
517 }
Reid Spencerb016a372004-09-15 05:49:50 +0000518 }
519 return true;
520}
521
522bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000523Path::createFileOnDisk() {
Reid Spencerb016a372004-09-15 05:49:50 +0000524 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000525 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000526 FILE_ATTRIBUTE_NORMAL, NULL);
527 if (h == INVALID_HANDLE_VALUE)
Jeff Cohen51b8d212004-12-31 19:01:08 +0000528 ThrowError(path + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000529
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000530 CloseHandle(h);
Reid Spencerb016a372004-09-15 05:49:50 +0000531 return true;
532}
533
534bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000535Path::eraseFromDisk(bool remove_contents) const {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000536 if (isFile()) {
537 DWORD attr = GetFileAttributes(path.c_str());
Reid Spencerb016a372004-09-15 05:49:50 +0000538
Reid Spencer1cf2d042005-07-07 23:35:23 +0000539 // If it doesn't exist, we're done.
540 if (attr == INVALID_FILE_ATTRIBUTES)
541 return true;
Reid Spencerb016a372004-09-15 05:49:50 +0000542
Reid Spencer1cf2d042005-07-07 23:35:23 +0000543 // Read-only files cannot be deleted on Windows. Must remove the read-only
544 // attribute first.
545 if (attr & FILE_ATTRIBUTE_READONLY) {
546 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
547 ThrowError(path + ": Can't destroy file: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000548 }
Jeff Cohen51b8d212004-12-31 19:01:08 +0000549
Reid Spencer1cf2d042005-07-07 23:35:23 +0000550 if (!DeleteFile(path.c_str()))
Jeff Cohen51b8d212004-12-31 19:01:08 +0000551 ThrowError(path + ": Can't destroy file: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000552 return true;
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000553 } else /* isDirectory() */ {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000554 // If it doesn't exist, we're done.
Jeff Cohen85c716f2005-07-08 05:02:13 +0000555 if (!exists())
Reid Spencer1cf2d042005-07-07 23:35:23 +0000556 return true;
557
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000558 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000559 int lastchar = path.length() - 1 ;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000560 path.copy(pathname, lastchar+1);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000561
562 // Make path end with '/*'.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000563 if (pathname[lastchar] != '/')
564 pathname[++lastchar] = '/';
Reid Spencer1cf2d042005-07-07 23:35:23 +0000565 pathname[lastchar+1] = '*';
566 pathname[lastchar+2] = 0;
567
568 if (remove_contents) {
569 WIN32_FIND_DATA fd;
570 HANDLE h = FindFirstFile(pathname, &fd);
571
572 // It's a bad idea to alter the contents of a directory while enumerating
573 // its contents. So build a list of its contents first, then destroy them.
574
575 if (h != INVALID_HANDLE_VALUE) {
576 std::vector<Path> list;
577
578 do {
579 if (strcmp(fd.cFileName, ".") == 0)
580 continue;
581 if (strcmp(fd.cFileName, "..") == 0)
582 continue;
583
Jeff Cohen85c716f2005-07-08 05:02:13 +0000584 Path aPath(path);
585 aPath.appendComponent(&fd.cFileName[0]);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000586 list.push_back(aPath);
587 } while (FindNextFile(h, &fd));
588
589 DWORD err = GetLastError();
590 FindClose(h);
591 if (err != ERROR_NO_MORE_FILES) {
592 SetLastError(err);
593 ThrowError(path + ": Can't read directory: ");
594 }
595
Jeff Cohen85c716f2005-07-08 05:02:13 +0000596 for (std::vector<Path>::iterator I = list.begin(); I != list.end();
Reid Spencer1cf2d042005-07-07 23:35:23 +0000597 ++I) {
598 Path &aPath = *I;
Reid Spencera229c5c2005-07-08 03:08:58 +0000599 aPath.eraseFromDisk(true);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000600 }
601 } else {
602 if (GetLastError() != ERROR_FILE_NOT_FOUND)
603 ThrowError(path + ": Can't read directory: ");
604 }
605 }
606
607 pathname[lastchar] = 0;
608 if (!RemoveDirectory(pathname))
609 ThrowError(std::string(pathname) + ": Can't destroy directory: ");
610 return true;
611 }
Reid Spencerb016a372004-09-15 05:49:50 +0000612}
613
Reid Spencer3b0cc782004-12-14 18:42:13 +0000614bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
615 if (!isFile())
616 return false;
617 assert(len < 1024 && "Request for magic string too long");
618 char* buf = (char*) alloca(1 + len);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000619
620 HANDLE h = CreateFile(path.c_str(),
621 GENERIC_READ,
622 FILE_SHARE_READ,
623 NULL,
624 OPEN_EXISTING,
625 FILE_ATTRIBUTE_NORMAL,
626 NULL);
627 if (h == INVALID_HANDLE_VALUE)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000628 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000629
630 DWORD nRead = 0;
631 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
632 CloseHandle(h);
633
634 if (!ret || nRead != len)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000635 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000636
Reid Spencer3b0cc782004-12-14 18:42:13 +0000637 buf[len] = '\0';
638 Magic = buf;
639 return true;
640}
641
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000642bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000643Path::renamePathOnDisk(const Path& newName) {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000644 if (!MoveFile(path.c_str(), newName.c_str()))
Jeff Cohen85c716f2005-07-08 05:02:13 +0000645 ThrowError("Can't move '" + path +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000646 "' to '" + newName.path + "': ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000647 return true;
648}
649
650bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000651Path::setStatusInfoOnDisk(const StatusInfo& si) const {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000652 if (!isFile()) return false;
653
654 HANDLE h = CreateFile(path.c_str(),
655 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
656 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000657 NULL,
658 OPEN_EXISTING,
659 FILE_ATTRIBUTE_NORMAL,
660 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000661 if (h == INVALID_HANDLE_VALUE)
662 return false;
663
664 BY_HANDLE_FILE_INFORMATION bhfi;
665 if (!GetFileInformationByHandle(h, &bhfi)) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000666 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000667 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000668 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000669 ThrowError(path + ": GetFileInformationByHandle: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000670 }
671
672 FILETIME ft;
673 (uint64_t&)ft = si.modTime.toWin32Time();
674 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000675 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000676 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000677 if (!ret) {
678 SetLastError(err);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000679 ThrowError(path + ": SetFileTime: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000680 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000681
682 // Best we can do with Unix permission bits is to interpret the owner
683 // writable bit.
684 if (si.mode & 0200) {
685 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
686 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000687 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000688 ThrowError(path + ": SetFileAttributes: ");
689 }
690 } else {
691 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
692 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000693 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000694 ThrowError(path + ": SetFileAttributes: ");
695 }
696 }
697
698 return true;
699}
700
Jeff Cohen85c716f2005-07-08 05:02:13 +0000701void
Reid Spencera36c9a42004-12-23 22:14:32 +0000702sys::CopyFile(const sys::Path &Dest, const sys::Path &Src) {
Jeff Cohencb652552004-12-24 02:38:34 +0000703 // Can't use CopyFile macro defined in Windows.h because it would mess up the
704 // above line. We use the expansion it would have in a non-UNICODE build.
705 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Jeff Cohen85c716f2005-07-08 05:02:13 +0000706 ThrowError("Can't copy '" + Src.toString() +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000707 "' to '" + Dest.toString() + "': ");
Reid Spencerc29befb2004-12-15 01:50:13 +0000708}
709
Jeff Cohen85c716f2005-07-08 05:02:13 +0000710void
Jeff Cohencb652552004-12-24 02:38:34 +0000711Path::makeUnique(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000712 if (reuse_current && !exists())
Reid Spencerc29befb2004-12-15 01:50:13 +0000713 return; // File doesn't exist already, just use it!
714
Jeff Cohen9437bb62005-01-27 03:49:03 +0000715 // Reserve space for -XXXXXX at the end.
716 char *FNBuffer = (char*) alloca(path.size()+8);
717 unsigned offset = path.size();
718 path.copy(FNBuffer, offset);
Reid Spencerc29befb2004-12-15 01:50:13 +0000719
Jeff Cohen9437bb62005-01-27 03:49:03 +0000720 // Find a numeric suffix that isn't used by an existing file.
721 static unsigned FCounter = 0;
722 do {
723 sprintf(FNBuffer+offset, "-%06u", FCounter);
724 if (++FCounter > 999999)
725 FCounter = 0;
726 path = FNBuffer;
727 } while (exists());
Reid Spencerc29befb2004-12-15 01:50:13 +0000728}
729
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000730bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000731Path::createTemporaryFileOnDisk(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000732 // Make this into a unique file name
733 makeUnique( reuse_current );
Jeff Cohen9437bb62005-01-27 03:49:03 +0000734
735 // Now go and create it
736 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
737 FILE_ATTRIBUTE_NORMAL, NULL);
738 if (h == INVALID_HANDLE_VALUE)
739 return false;
740
741 CloseHandle(h);
Jeff Cohenf8cdb852004-12-18 06:42:15 +0000742 return true;
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000743}
744
Reid Spencerb016a372004-09-15 05:49:50 +0000745}
Reid Spencercbad7012004-09-11 04:59:30 +0000746}
747
Reid Spencerb016a372004-09-15 05:49:50 +0000748