blob: a049f16f13d47399d51890c5b0687461d1e5c4da [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 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
Jeff Cohen85c716f2005-07-08 05:02:13 +0000249bool
Reid Spencer07adb282004-11-05 22:15:36 +0000250Path::isBytecodeFile() const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000251 std::string actualMagic;
252 if (!getMagicNumber(actualMagic, 4))
253 return false;
254 return actualMagic == "llvc" || actualMagic == "llvm";
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000255}
256
257bool
Reid Spencerb016a372004-09-15 05:49:50 +0000258Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000259 DWORD attr = GetFileAttributes(path.c_str());
260 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000261}
262
263bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000264Path::canRead() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000265 // FIXME: take security attributes into account.
266 DWORD attr = GetFileAttributes(path.c_str());
267 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000268}
269
270bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000271Path::canWrite() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000272 // FIXME: take security attributes into account.
273 DWORD attr = GetFileAttributes(path.c_str());
274 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000275}
276
277bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000278Path::canExecute() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000279 // FIXME: take security attributes into account.
280 DWORD attr = GetFileAttributes(path.c_str());
281 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000282}
283
284std::string
285Path::getLast() const {
286 // Find the last slash
287 size_t pos = path.rfind('/');
288
289 // Handle the corner cases
290 if (pos == std::string::npos)
291 return path;
292
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000293 // If the last character is a slash, we have a root directory
294 if (pos == path.length()-1)
295 return path;
296
Reid Spencerb016a372004-09-15 05:49:50 +0000297 // Return everything after the last slash
298 return path.substr(pos+1);
299}
300
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] == '/')
Jeff Cohen85c716f2005-07-08 05:02:13 +0000367 searchpath += "*";
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000368 else
369 searchpath += "/*";
Jeff Cohen85c716f2005-07-08 05:02:13 +0000370
Jeff Cohen9437bb62005-01-27 03:49:03 +0000371 HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000372 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohen9437bb62005-01-27 03:49:03 +0000373 if (GetLastError() == ERROR_FILE_NOT_FOUND)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000374 return true; // not really an error, now is it?
Jeff Cohend40a7de2004-12-31 05:07:26 +0000375 ThrowError(path + ": Can't read directory: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000376 }
377
378 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000379 if (fd.cFileName[0] == '.')
380 continue;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000381 Path aPath(path);
382 aPath.appendComponent(&fd.cFileName[0]);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000383 result.insert(aPath);
384 } while (FindNextFile(h, &fd));
385
Jeff Cohen51b8d212004-12-31 19:01:08 +0000386 DWORD err = GetLastError();
387 FindClose(h);
388 if (err != ERROR_NO_MORE_FILES) {
389 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000390 ThrowError(path + ": Can't read directory: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000391 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000392 return true;
393}
394
395bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000396Path::set(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000397 if (a_path.size() == 0)
398 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000399 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000400 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000401 FlipBackSlashes(path);
Reid Spencer07adb282004-11-05 22:15:36 +0000402 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000403 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000404 return false;
405 }
406 return true;
407}
408
409bool
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000410Path::appendComponent(const std::string& name) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000411 if (name.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000412 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000413 std::string save(path);
414 if (!path.empty()) {
415 size_t last = path.size() - 1;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000416 if (path[last] != '/')
Reid Spencer1cf2d042005-07-07 23:35:23 +0000417 path += '/';
418 }
419 path += name;
Reid Spencer07adb282004-11-05 22:15:36 +0000420 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000421 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000422 return false;
423 }
424 return true;
425}
426
427bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000428Path::eraseComponent() {
Reid Spencerb016a372004-09-15 05:49:50 +0000429 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000430 if (slashpos == path.size() - 1 || slashpos == std::string::npos)
Reid Spencerb016a372004-09-15 05:49:50 +0000431 return false;
432 path.erase(slashpos);
433 return true;
434}
435
436bool
Reid Spencer07adb282004-11-05 22:15:36 +0000437Path::appendSuffix(const std::string& suffix) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000438 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000439 path.append(".");
440 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000441 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000442 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000443 return false;
444 }
445 return true;
446}
447
448bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000449Path::eraseSuffix() {
Reid Spencerb016a372004-09-15 05:49:50 +0000450 size_t dotpos = path.rfind('.',path.size());
451 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000452 if (dotpos != std::string::npos) {
453 if (slashpos == std::string::npos || dotpos > slashpos) {
454 path.erase(dotpos, path.size()-dotpos);
Jeff Cohen85c716f2005-07-08 05:02:13 +0000455 return true;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000456 }
Reid Spencerb016a372004-09-15 05:49:50 +0000457 }
458 return false;
459}
460
Reid Spencerb016a372004-09-15 05:49:50 +0000461bool
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000462Path::createDirectoryOnDisk(bool create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000463 // Get a writeable copy of the path name
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000464 size_t len = path.length();
465 char *pathname = reinterpret_cast<char *>(_alloca(len+2));
466 path.copy(pathname, len);
467 pathname[len] = 0;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000468
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000469 // Make sure it ends with a slash.
470 if (len == 0 || pathname[len - 1] != '/') {
471 pathname[len] = '/';
472 pathname[++len] = 0;
473 }
Reid Spencerb016a372004-09-15 05:49:50 +0000474
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000475 // Determine starting point for initial / search.
476 char *next = pathname;
477 if (pathname[0] == '/' && pathname[1] == '/') {
478 // Skip host name.
479 next = strchr(pathname+2, '/');
480 if (next == NULL)
481 throw std::string(pathname) + ": badly formed remote directory";
482 // Skip share name.
483 next = strchr(next+1, '/');
484 if (next == NULL)
485 throw std::string(pathname) + ": badly formed remote directory";
486 next++;
487 if (*next == 0)
488 throw std::string(pathname) + ": badly formed remote directory";
489 } else {
490 if (pathname[1] == ':')
491 next += 2; // skip drive letter
492 if (*next == '/')
493 next++; // skip root directory
494 }
Reid Spencerb016a372004-09-15 05:49:50 +0000495
496 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000497 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000498 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000499 while (*next) {
500 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000501 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000502 if (!CreateDirectory(pathname, NULL))
503 ThrowError(std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000504 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000505 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000506 } else {
507 // Drop trailing slash.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000508 pathname[len-1] = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000509 if (!CreateDirectory(pathname, NULL)) {
510 ThrowError(std::string(pathname) + ": Can't create directory: ");
511 }
Reid Spencerb016a372004-09-15 05:49:50 +0000512 }
513 return true;
514}
515
516bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000517Path::createFileOnDisk() {
Reid Spencerb016a372004-09-15 05:49:50 +0000518 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000519 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000520 FILE_ATTRIBUTE_NORMAL, NULL);
521 if (h == INVALID_HANDLE_VALUE)
Jeff Cohen51b8d212004-12-31 19:01:08 +0000522 ThrowError(path + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000523
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000524 CloseHandle(h);
Reid Spencerb016a372004-09-15 05:49:50 +0000525 return true;
526}
527
528bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000529Path::eraseFromDisk(bool remove_contents) const {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000530 if (isFile()) {
531 DWORD attr = GetFileAttributes(path.c_str());
Reid Spencerb016a372004-09-15 05:49:50 +0000532
Reid Spencer1cf2d042005-07-07 23:35:23 +0000533 // If it doesn't exist, we're done.
534 if (attr == INVALID_FILE_ATTRIBUTES)
535 return true;
Reid Spencerb016a372004-09-15 05:49:50 +0000536
Reid Spencer1cf2d042005-07-07 23:35:23 +0000537 // Read-only files cannot be deleted on Windows. Must remove the read-only
538 // attribute first.
539 if (attr & FILE_ATTRIBUTE_READONLY) {
540 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
541 ThrowError(path + ": Can't destroy file: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000542 }
Jeff Cohen51b8d212004-12-31 19:01:08 +0000543
Reid Spencer1cf2d042005-07-07 23:35:23 +0000544 if (!DeleteFile(path.c_str()))
Jeff Cohen51b8d212004-12-31 19:01:08 +0000545 ThrowError(path + ": Can't destroy file: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000546 return true;
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000547 } else /* isDirectory() */ {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000548 // If it doesn't exist, we're done.
Jeff Cohen85c716f2005-07-08 05:02:13 +0000549 if (!exists())
Reid Spencer1cf2d042005-07-07 23:35:23 +0000550 return true;
551
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000552 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000553 int lastchar = path.length() - 1 ;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000554 path.copy(pathname, lastchar+1);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000555
556 // Make path end with '/*'.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000557 if (pathname[lastchar] != '/')
558 pathname[++lastchar] = '/';
Reid Spencer1cf2d042005-07-07 23:35:23 +0000559 pathname[lastchar+1] = '*';
560 pathname[lastchar+2] = 0;
561
562 if (remove_contents) {
563 WIN32_FIND_DATA fd;
564 HANDLE h = FindFirstFile(pathname, &fd);
565
566 // It's a bad idea to alter the contents of a directory while enumerating
567 // its contents. So build a list of its contents first, then destroy them.
568
569 if (h != INVALID_HANDLE_VALUE) {
570 std::vector<Path> list;
571
572 do {
573 if (strcmp(fd.cFileName, ".") == 0)
574 continue;
575 if (strcmp(fd.cFileName, "..") == 0)
576 continue;
577
Jeff Cohen85c716f2005-07-08 05:02:13 +0000578 Path aPath(path);
579 aPath.appendComponent(&fd.cFileName[0]);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000580 list.push_back(aPath);
581 } while (FindNextFile(h, &fd));
582
583 DWORD err = GetLastError();
584 FindClose(h);
585 if (err != ERROR_NO_MORE_FILES) {
586 SetLastError(err);
587 ThrowError(path + ": Can't read directory: ");
588 }
589
Jeff Cohen85c716f2005-07-08 05:02:13 +0000590 for (std::vector<Path>::iterator I = list.begin(); I != list.end();
Reid Spencer1cf2d042005-07-07 23:35:23 +0000591 ++I) {
592 Path &aPath = *I;
Reid Spencera229c5c2005-07-08 03:08:58 +0000593 aPath.eraseFromDisk(true);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000594 }
595 } else {
596 if (GetLastError() != ERROR_FILE_NOT_FOUND)
597 ThrowError(path + ": Can't read directory: ");
598 }
599 }
600
601 pathname[lastchar] = 0;
602 if (!RemoveDirectory(pathname))
603 ThrowError(std::string(pathname) + ": Can't destroy directory: ");
604 return true;
605 }
Reid Spencerb016a372004-09-15 05:49:50 +0000606}
607
Reid Spencer3b0cc782004-12-14 18:42:13 +0000608bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
609 if (!isFile())
610 return false;
611 assert(len < 1024 && "Request for magic string too long");
612 char* buf = (char*) alloca(1 + len);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000613
614 HANDLE h = CreateFile(path.c_str(),
615 GENERIC_READ,
616 FILE_SHARE_READ,
617 NULL,
618 OPEN_EXISTING,
619 FILE_ATTRIBUTE_NORMAL,
620 NULL);
621 if (h == INVALID_HANDLE_VALUE)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000622 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000623
624 DWORD nRead = 0;
625 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
626 CloseHandle(h);
627
628 if (!ret || nRead != len)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000629 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000630
Reid Spencer3b0cc782004-12-14 18:42:13 +0000631 buf[len] = '\0';
632 Magic = buf;
633 return true;
634}
635
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000636bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000637Path::renamePathOnDisk(const Path& newName) {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000638 if (!MoveFile(path.c_str(), newName.c_str()))
Jeff Cohen85c716f2005-07-08 05:02:13 +0000639 ThrowError("Can't move '" + path +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000640 "' to '" + newName.path + "': ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000641 return true;
642}
643
644bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000645Path::setStatusInfoOnDisk(const StatusInfo& si) const {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000646 if (!isFile()) return false;
647
648 HANDLE h = CreateFile(path.c_str(),
649 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
650 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000651 NULL,
652 OPEN_EXISTING,
653 FILE_ATTRIBUTE_NORMAL,
654 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000655 if (h == INVALID_HANDLE_VALUE)
656 return false;
657
658 BY_HANDLE_FILE_INFORMATION bhfi;
659 if (!GetFileInformationByHandle(h, &bhfi)) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000660 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000661 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000662 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000663 ThrowError(path + ": GetFileInformationByHandle: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000664 }
665
666 FILETIME ft;
667 (uint64_t&)ft = si.modTime.toWin32Time();
668 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000669 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000670 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000671 if (!ret) {
672 SetLastError(err);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000673 ThrowError(path + ": SetFileTime: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000674 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000675
676 // Best we can do with Unix permission bits is to interpret the owner
677 // writable bit.
678 if (si.mode & 0200) {
679 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
680 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000681 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000682 ThrowError(path + ": SetFileAttributes: ");
683 }
684 } else {
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 }
691
692 return true;
693}
694
Jeff Cohen85c716f2005-07-08 05:02:13 +0000695void
Reid Spencera36c9a42004-12-23 22:14:32 +0000696sys::CopyFile(const sys::Path &Dest, const sys::Path &Src) {
Jeff Cohencb652552004-12-24 02:38:34 +0000697 // Can't use CopyFile macro defined in Windows.h because it would mess up the
698 // above line. We use the expansion it would have in a non-UNICODE build.
699 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Jeff Cohen85c716f2005-07-08 05:02:13 +0000700 ThrowError("Can't copy '" + Src.toString() +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000701 "' to '" + Dest.toString() + "': ");
Reid Spencerc29befb2004-12-15 01:50:13 +0000702}
703
Jeff Cohen85c716f2005-07-08 05:02:13 +0000704void
Jeff Cohencb652552004-12-24 02:38:34 +0000705Path::makeUnique(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000706 if (reuse_current && !exists())
Reid Spencerc29befb2004-12-15 01:50:13 +0000707 return; // File doesn't exist already, just use it!
708
Jeff Cohen9437bb62005-01-27 03:49:03 +0000709 // Reserve space for -XXXXXX at the end.
710 char *FNBuffer = (char*) alloca(path.size()+8);
711 unsigned offset = path.size();
712 path.copy(FNBuffer, offset);
Reid Spencerc29befb2004-12-15 01:50:13 +0000713
Jeff Cohen9437bb62005-01-27 03:49:03 +0000714 // Find a numeric suffix that isn't used by an existing file.
715 static unsigned FCounter = 0;
716 do {
717 sprintf(FNBuffer+offset, "-%06u", FCounter);
718 if (++FCounter > 999999)
719 FCounter = 0;
720 path = FNBuffer;
721 } while (exists());
Reid Spencerc29befb2004-12-15 01:50:13 +0000722}
723
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000724bool
Reid Spencera229c5c2005-07-08 03:08:58 +0000725Path::createTemporaryFileOnDisk(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000726 // Make this into a unique file name
727 makeUnique( reuse_current );
Jeff Cohen9437bb62005-01-27 03:49:03 +0000728
729 // Now go and create it
730 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
731 FILE_ATTRIBUTE_NORMAL, NULL);
732 if (h == INVALID_HANDLE_VALUE)
733 return false;
734
735 CloseHandle(h);
Jeff Cohenf8cdb852004-12-18 06:42:15 +0000736 return true;
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000737}
738
Reid Spencerb016a372004-09-15 05:49:50 +0000739}
Reid Spencercbad7012004-09-11 04:59:30 +0000740}
741
Reid Spencerb016a372004-09-15 05:49:50 +0000742