blob: a28fd82d38a536efb1c4b562e899026c1c6ff4c5 [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);
46 if (pos != std::string::npos) {
47 if (pos != 1 || !isalpha(path[0]) || len < 3)
48 return false;
49 }
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000050
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000051 // Check for illegal characters.
52 if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012"
53 "\013\014\015\016\017\020\021\022\023\024\025\026"
54 "\027\030\031\032\033\034\035\036\037")
55 != std::string::npos)
56 return false;
57
Jeff Cohen3bbbcc12005-01-14 04:09:39 +000058 // Check each component for legality.
59 for (pos = 0; pos < len; ++pos) {
60 // A component may not end in a space.
61 if (path[pos] == ' ') {
62 if (path[pos+1] == '/' || path[pos+1] == '\0')
63 return false;
64 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000065
Jeff Cohen3bbbcc12005-01-14 04:09:39 +000066 // A component may not end in a period.
67 if (path[pos] == '.') {
68 if (path[pos+1] == '/' || path[pos+1] == '\0') {
69 // Unless it is the pseudo-directory "."...
70 if (pos == 0 || path[pos-1] == '/' || path[pos-1] == ':')
71 return true;
72 // or "..".
73 if (pos > 0 && path[pos-1] == '.') {
74 if (pos == 1 || path[pos-2] == '/' || path[pos-2] == ':')
75 return true;
76 }
77 return false;
78 }
79 }
80 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000081
82 return true;
Reid Spencercbad7012004-09-11 04:59:30 +000083}
84
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000085static Path *TempDirectory = NULL;
86
Reid Spencerb016a372004-09-15 05:49:50 +000087Path
88Path::GetTemporaryDirectory() {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000089 if (TempDirectory)
90 return *TempDirectory;
91
92 char pathname[MAX_PATH];
93 if (!GetTempPath(MAX_PATH, pathname))
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000094 throw std::string("Can't determine temporary directory");
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000095
Reid Spencerb016a372004-09-15 05:49:50 +000096 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +000097 result.setDirectory(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000098
99 // Append a subdirectory passed on our process id so multiple LLVMs don't
100 // step on each other's toes.
101 sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
Reid Spencer07adb282004-11-05 22:15:36 +0000102 result.appendDirectory(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000103
104 // If there's a directory left over from a previous LLVM execution that
105 // happened to have the same process id, get rid of it.
Reid Spencer07adb282004-11-05 22:15:36 +0000106 result.destroyDirectory(true);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000107
108 // And finally (re-)create the empty directory.
Reid Spencer07adb282004-11-05 22:15:36 +0000109 result.createDirectory(false);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000110 TempDirectory = new Path(result);
111 return *TempDirectory;
Reid Spencerb016a372004-09-15 05:49:50 +0000112}
113
Reid Spencer732f92d2004-12-13 06:57:15 +0000114Path::Path(const std::string& unverified_path)
Reid Spencerb016a372004-09-15 05:49:50 +0000115 : path(unverified_path)
116{
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000117 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000118 if (unverified_path.empty())
119 return;
Reid Spencer07adb282004-11-05 22:15:36 +0000120 if (this->isValid())
Reid Spencerb016a372004-09-15 05:49:50 +0000121 return;
122 // oops, not valid.
123 path.clear();
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000124 throw std::string(unverified_path + ": path is not valid");
Reid Spencerb016a372004-09-15 05:49:50 +0000125}
126
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000127// FIXME: the following set of functions don't map to Windows very well.
Reid Spencerb016a372004-09-15 05:49:50 +0000128Path
129Path::GetRootDirectory() {
130 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +0000131 result.setDirectory("/");
Reid Spencerb016a372004-09-15 05:49:50 +0000132 return result;
133}
134
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000135static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
136 const char* at = path;
137 const char* delim = strchr(at, ';');
138 Path tmpPath;
139 while( delim != 0 ) {
140 std::string tmp(at, size_t(delim-at));
141 if (tmpPath.setDirectory(tmp))
142 if (tmpPath.readable())
143 Paths.push_back(tmpPath);
144 at = delim + 1;
145 delim = strchr(at, ';');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000146 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000147 if (*at != 0)
148 if (tmpPath.setDirectory(std::string(at)))
149 if (tmpPath.readable())
150 Paths.push_back(tmpPath);
151
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000152}
153
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000154void
155Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000156 Paths.push_back(sys::Path("C:\\WINDOWS\\SYSTEM32\\"));
157 Paths.push_back(sys::Path("C:\\WINDOWS\\"));
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000158}
159
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000160void
161Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
162 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
163 if (env_var != 0) {
164 getPathList(env_var,Paths);
165 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000166#ifdef LLVM_LIBDIR
167 {
168 Path tmpPath;
169 if (tmpPath.setDirectory(LLVM_LIBDIR))
170 if (tmpPath.readable())
171 Paths.push_back(tmpPath);
172 }
173#endif
174 GetSystemLibraryPaths(Paths);
Reid Spencerb016a372004-09-15 05:49:50 +0000175}
176
177Path
178Path::GetLLVMDefaultConfigDir() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000179 // TODO: this isn't going to fly on Windows
Reid Spencerb016a372004-09-15 05:49:50 +0000180 return Path("/etc/llvm/");
181}
182
183Path
Reid Spencerb016a372004-09-15 05:49:50 +0000184Path::GetUserHomeDirectory() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000185 // TODO: Typical Windows setup doesn't define HOME.
Reid Spencerb016a372004-09-15 05:49:50 +0000186 const char* home = getenv("HOME");
187 if (home) {
188 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +0000189 if (result.setDirectory(home))
Reid Spencerb016a372004-09-15 05:49:50 +0000190 return result;
191 }
192 return GetRootDirectory();
193}
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000194// FIXME: the above set of functions don't map to Windows very well.
195
196bool
Reid Spencer07adb282004-11-05 22:15:36 +0000197Path::isFile() const {
198 return (isValid() && path[path.length()-1] != '/');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000199}
200
201bool
Reid Spencer07adb282004-11-05 22:15:36 +0000202Path::isDirectory() const {
203 return (isValid() && path[path.length()-1] == '/');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000204}
205
206std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000207Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000208 // Find the last slash
209 size_t slash = path.rfind('/');
210 if (slash == std::string::npos)
211 slash = 0;
212 else
213 slash++;
214
215 return path.substr(slash, path.rfind('.'));
216}
217
Reid Spencer07adb282004-11-05 22:15:36 +0000218bool Path::hasMagicNumber(const std::string &Magic) const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000219 std::string actualMagic;
220 if (getMagicNumber(actualMagic, Magic.size()))
221 return Magic == actualMagic;
222 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000223}
224
225bool
Reid Spencer07adb282004-11-05 22:15:36 +0000226Path::isBytecodeFile() const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000227 std::string actualMagic;
228 if (!getMagicNumber(actualMagic, 4))
229 return false;
230 return actualMagic == "llvc" || actualMagic == "llvm";
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000231}
232
233bool
Reid Spencerb016a372004-09-15 05:49:50 +0000234Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000235 DWORD attr = GetFileAttributes(path.c_str());
236 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000237}
238
239bool
240Path::readable() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000241 // FIXME: take security attributes into account.
242 DWORD attr = GetFileAttributes(path.c_str());
243 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000244}
245
246bool
247Path::writable() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000248 // FIXME: take security attributes into account.
249 DWORD attr = GetFileAttributes(path.c_str());
250 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000251}
252
253bool
254Path::executable() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000255 // FIXME: take security attributes into account.
256 DWORD attr = GetFileAttributes(path.c_str());
257 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000258}
259
260std::string
261Path::getLast() const {
262 // Find the last slash
263 size_t pos = path.rfind('/');
264
265 // Handle the corner cases
266 if (pos == std::string::npos)
267 return path;
268
269 // If the last character is a slash
270 if (pos == path.length()-1) {
271 // Find the second to last slash
272 size_t pos2 = path.rfind('/', pos-1);
273 if (pos2 == std::string::npos)
274 return path.substr(0,pos);
275 else
276 return path.substr(pos2+1,pos-pos2-1);
277 }
278 // Return everything after the last slash
279 return path.substr(pos+1);
280}
281
Jeff Cohen626e38e2004-12-14 05:26:43 +0000282void
283Path::getStatusInfo(StatusInfo& info) const {
284 WIN32_FILE_ATTRIBUTE_DATA fi;
285 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
286 ThrowError(std::string(path) + ": Can't get status: ");
287
288 info.fileSize = fi.nFileSizeHigh;
289 info.fileSize <<= 32;
290 info.fileSize += fi.nFileSizeLow;
291
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000292 info.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000293 info.user = 9999; // Not applicable to Windows, so...
294 info.group = 9999; // Not applicable to Windows, so...
295
296 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
297 info.modTime.fromWin32Time(ft);
298
299 info.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
300 if (info.isDir && path[path.length() - 1] != '/')
301 path += '/';
302 else if (!info.isDir && path[path.length() - 1] == '/')
303 path.erase(path.length() - 1);
304}
305
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000306static bool AddPermissionBits(const std::string& Filename, int bits) {
307 DWORD attr = GetFileAttributes(Filename.c_str());
308
309 // If it doesn't exist, we're done.
310 if (attr == INVALID_FILE_ATTRIBUTES)
311 return false;
312
313 // The best we can do to interpret Unix permission bits is to use
314 // the owner writable bit.
315 if ((attr & FILE_ATTRIBUTE_READONLY) && (bits & 0200)) {
316 if (!SetFileAttributes(Filename.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohend40a7de2004-12-31 05:07:26 +0000317 ThrowError(Filename + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000318 }
319 return true;
320}
321
Reid Spencer77cc91d2004-12-13 19:59:50 +0000322void Path::makeReadable() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000323 // All files are readable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000324}
325
326void Path::makeWriteable() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000327 DWORD attr = GetFileAttributes(path.c_str());
328
329 // If it doesn't exist, we're done.
330 if (attr == INVALID_FILE_ATTRIBUTES)
331 return;
332
333 if (attr & FILE_ATTRIBUTE_READONLY) {
334 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
335 ThrowError(std::string(path) + ": Can't make file writable: ");
336 }
Reid Spencer77cc91d2004-12-13 19:59:50 +0000337}
338
339void Path::makeExecutable() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000340 // All files are executable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000341}
342
Reid Spencerb016a372004-09-15 05:49:50 +0000343bool
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000344Path::getDirectoryContents(std::set<Path>& result) const {
345 if (!isDirectory())
346 return false;
347
348 result.clear();
349 WIN32_FIND_DATA fd;
350 HANDLE h = FindFirstFile(path.c_str(), &fd);
351 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000352 if (GetLastError() == ERROR_NO_MORE_FILES)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000353 return true; // not really an error, now is it?
Jeff Cohend40a7de2004-12-31 05:07:26 +0000354 ThrowError(path + ": Can't read directory: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000355 }
356
357 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000358 if (fd.cFileName[0] == '.')
359 continue;
360 Path aPath(path + &fd.cFileName[0]);
361 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
362 aPath.path += "/";
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000363 result.insert(aPath);
364 } while (FindNextFile(h, &fd));
365
Jeff Cohen51b8d212004-12-31 19:01:08 +0000366 DWORD err = GetLastError();
367 FindClose(h);
368 if (err != ERROR_NO_MORE_FILES) {
369 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000370 ThrowError(path + ": Can't read directory: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000371 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000372 return true;
373}
374
375bool
Reid Spencer07adb282004-11-05 22:15:36 +0000376Path::setDirectory(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000377 if (a_path.size() == 0)
378 return false;
379 Path save(*this);
380 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000381 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000382 size_t last = a_path.size() -1;
Reid Spencerb0e18872004-12-13 07:51:52 +0000383 if (a_path[last] != '/')
Reid Spencerb016a372004-09-15 05:49:50 +0000384 path += '/';
Reid Spencer07adb282004-11-05 22:15:36 +0000385 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000386 path = save.path;
387 return false;
388 }
389 return true;
390}
391
392bool
Reid Spencer07adb282004-11-05 22:15:36 +0000393Path::setFile(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000394 if (a_path.size() == 0)
395 return false;
396 Path save(*this);
397 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000398 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000399 size_t last = a_path.size() - 1;
400 while (last > 0 && a_path[last] == '/')
401 last--;
402 path.erase(last+1);
Reid Spencer07adb282004-11-05 22:15:36 +0000403 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000404 path = save.path;
405 return false;
406 }
407 return true;
408}
409
410bool
Reid Spencer07adb282004-11-05 22:15:36 +0000411Path::appendDirectory(const std::string& dir) {
412 if (isFile())
Reid Spencerb016a372004-09-15 05:49:50 +0000413 return false;
414 Path save(*this);
415 path += dir;
416 path += "/";
Reid Spencer07adb282004-11-05 22:15:36 +0000417 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000418 path = save.path;
419 return false;
420 }
421 return true;
422}
423
424bool
Reid Spencer07adb282004-11-05 22:15:36 +0000425Path::elideDirectory() {
426 if (isFile())
Reid Spencerb016a372004-09-15 05:49:50 +0000427 return false;
428 size_t slashpos = path.rfind('/',path.size());
429 if (slashpos == 0 || slashpos == std::string::npos)
430 return false;
431 if (slashpos == path.size() - 1)
432 slashpos = path.rfind('/',slashpos-1);
433 if (slashpos == std::string::npos)
434 return false;
435 path.erase(slashpos);
436 return true;
437}
438
439bool
Reid Spencer07adb282004-11-05 22:15:36 +0000440Path::appendFile(const std::string& file) {
441 if (!isDirectory())
Reid Spencerb016a372004-09-15 05:49:50 +0000442 return false;
443 Path save(*this);
444 path += file;
Reid Spencer07adb282004-11-05 22:15:36 +0000445 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000446 path = save.path;
447 return false;
448 }
449 return true;
450}
451
452bool
Reid Spencer07adb282004-11-05 22:15:36 +0000453Path::elideFile() {
454 if (isDirectory())
Reid Spencerb016a372004-09-15 05:49:50 +0000455 return false;
456 size_t slashpos = path.rfind('/',path.size());
457 if (slashpos == std::string::npos)
458 return false;
459 path.erase(slashpos+1);
460 return true;
461}
462
463bool
Reid Spencer07adb282004-11-05 22:15:36 +0000464Path::appendSuffix(const std::string& suffix) {
465 if (isDirectory())
Reid Spencerb016a372004-09-15 05:49:50 +0000466 return false;
467 Path save(*this);
468 path.append(".");
469 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000470 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000471 path = save.path;
472 return false;
473 }
474 return true;
475}
476
477bool
Reid Spencer07adb282004-11-05 22:15:36 +0000478Path::elideSuffix() {
479 if (isDirectory()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000480 size_t dotpos = path.rfind('.',path.size());
481 size_t slashpos = path.rfind('/',path.size());
482 if (slashpos != std::string::npos && dotpos != std::string::npos &&
483 dotpos > slashpos) {
484 path.erase(dotpos, path.size()-dotpos);
485 return true;
486 }
487 return false;
488}
489
490
491bool
Reid Spencer07adb282004-11-05 22:15:36 +0000492Path::createDirectory( bool create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000493 // Make sure we're dealing with a directory
Reid Spencer07adb282004-11-05 22:15:36 +0000494 if (!isDirectory()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000495
496 // Get a writeable copy of the path name
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000497 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1));
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000498 path.copy(pathname,path.length());
499 pathname[path.length()] = 0;
Reid Spencerb016a372004-09-15 05:49:50 +0000500
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000501 // Determine starting point for initial / search.
502 char *next = pathname;
503 if (pathname[0] == '/' && pathname[1] == '/') {
504 // Skip host name.
505 next = strchr(pathname+2, '/');
506 if (next == NULL)
507 throw std::string(pathname) + ": badly formed remote directory";
508 // Skip share name.
509 next = strchr(next+1, '/');
510 if (next == NULL)
511 throw std::string(pathname) + ": badly formed remote directory";
512 next++;
513 if (*next == 0)
514 throw std::string(pathname) + ": badly formed remote directory";
515 } else {
516 if (pathname[1] == ':')
517 next += 2; // skip drive letter
518 if (*next == '/')
519 next++; // skip root directory
520 }
Reid Spencerb016a372004-09-15 05:49:50 +0000521
522 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000523 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000524 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000525 while (*next) {
526 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000527 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000528 if (!CreateDirectory(pathname, NULL))
529 ThrowError(std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000530 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000531 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000532 } else {
533 // Drop trailing slash.
534 pathname[path.size()-1] = 0;
535 if (!CreateDirectory(pathname, NULL)) {
536 ThrowError(std::string(pathname) + ": Can't create directory: ");
537 }
Reid Spencerb016a372004-09-15 05:49:50 +0000538 }
539 return true;
540}
541
542bool
Reid Spencer07adb282004-11-05 22:15:36 +0000543Path::createFile() {
Reid Spencerb016a372004-09-15 05:49:50 +0000544 // Make sure we're dealing with a file
Reid Spencer07adb282004-11-05 22:15:36 +0000545 if (!isFile()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000546
547 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000548 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000549 FILE_ATTRIBUTE_NORMAL, NULL);
550 if (h == INVALID_HANDLE_VALUE)
Jeff Cohen51b8d212004-12-31 19:01:08 +0000551 ThrowError(path + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000552
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000553 CloseHandle(h);
Reid Spencerb016a372004-09-15 05:49:50 +0000554 return true;
555}
556
557bool
Reid Spencer00e89302004-12-15 23:02:10 +0000558Path::destroyDirectory(bool remove_contents) const {
Reid Spencerb016a372004-09-15 05:49:50 +0000559 // Make sure we're dealing with a directory
Reid Spencer07adb282004-11-05 22:15:36 +0000560 if (!isDirectory()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000561
562 // If it doesn't exist, we're done.
563 if (!exists()) return true;
564
Jeff Cohenc690cc02005-01-22 16:28:33 +0000565 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+2));
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000566 int lastchar = path.length() - 1 ;
Jeff Cohenc690cc02005-01-22 16:28:33 +0000567 path.copy(pathname,lastchar+2);
568
569 // Make path end with '/*'.
570 pathname[lastchar+1] = '*';
571 pathname[lastchar+2] = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000572
Reid Spencerb016a372004-09-15 05:49:50 +0000573 if (remove_contents) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000574 WIN32_FIND_DATA fd;
Jeff Cohenc690cc02005-01-22 16:28:33 +0000575 HANDLE h = FindFirstFile(pathname, &fd);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000576
577 // It's a bad idea to alter the contents of a directory while enumerating
578 // its contents. So build a list of its contents first, then destroy them.
579
580 if (h != INVALID_HANDLE_VALUE) {
581 std::vector<Path> list;
582
583 do {
584 if (strcmp(fd.cFileName, ".") == 0)
585 continue;
586 if (strcmp(fd.cFileName, "..") == 0)
587 continue;
588
589 Path aPath(path + &fd.cFileName[0]);
590 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
591 aPath.path += "/";
592 list.push_back(aPath);
593 } while (FindNextFile(h, &fd));
594
595 DWORD err = GetLastError();
596 FindClose(h);
597 if (err != ERROR_NO_MORE_FILES) {
598 SetLastError(err);
599 ThrowError(path + ": Can't read directory: ");
600 }
601
Jeff Cohen45e88d62004-12-31 19:03:31 +0000602 for (std::vector<Path>::iterator I = list.begin(); I != list.end(); ++I) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000603 Path &aPath = *I;
604 if (aPath.isDirectory())
605 aPath.destroyDirectory(true);
606 else
607 aPath.destroyFile();
608 }
609 } else {
610 if (GetLastError() != ERROR_NO_MORE_FILES)
611 ThrowError(path + ": Can't read directory: ");
612 }
Reid Spencerb016a372004-09-15 05:49:50 +0000613 }
Jeff Cohen51b8d212004-12-31 19:01:08 +0000614
Jeff Cohenc690cc02005-01-22 16:28:33 +0000615 pathname[lastchar] = 0;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000616 if (!RemoveDirectory(pathname))
617 ThrowError(std::string(pathname) + ": Can't destroy directory: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000618 return true;
619}
620
621bool
Reid Spencer00e89302004-12-15 23:02:10 +0000622Path::destroyFile() const {
Reid Spencer07adb282004-11-05 22:15:36 +0000623 if (!isFile()) return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000624
625 DWORD attr = GetFileAttributes(path.c_str());
626
627 // If it doesn't exist, we're done.
628 if (attr == INVALID_FILE_ATTRIBUTES)
629 return true;
630
631 // Read-only files cannot be deleted on Windows. Must remove the read-only
632 // attribute first.
633 if (attr & FILE_ATTRIBUTE_READONLY) {
634 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohen51b8d212004-12-31 19:01:08 +0000635 ThrowError(path + ": Can't destroy file: ");
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000636 }
637
638 if (!DeleteFile(path.c_str()))
Jeff Cohen51b8d212004-12-31 19:01:08 +0000639 ThrowError(path + ": Can't destroy file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000640 return true;
641}
642
Reid Spencer3b0cc782004-12-14 18:42:13 +0000643bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
644 if (!isFile())
645 return false;
646 assert(len < 1024 && "Request for magic string too long");
647 char* buf = (char*) alloca(1 + len);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000648
649 HANDLE h = CreateFile(path.c_str(),
650 GENERIC_READ,
651 FILE_SHARE_READ,
652 NULL,
653 OPEN_EXISTING,
654 FILE_ATTRIBUTE_NORMAL,
655 NULL);
656 if (h == INVALID_HANDLE_VALUE)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000657 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000658
659 DWORD nRead = 0;
660 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
661 CloseHandle(h);
662
663 if (!ret || nRead != len)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000664 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000665
Reid Spencer3b0cc782004-12-14 18:42:13 +0000666 buf[len] = '\0';
667 Magic = buf;
668 return true;
669}
670
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000671bool
672Path::renameFile(const Path& newName) {
673 if (!isFile()) return false;
674 if (!MoveFile(path.c_str(), newName.c_str()))
675 ThrowError("Can't move '" + path +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000676 "' to '" + newName.path + "': ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000677 return true;
678}
679
680bool
681Path::setStatusInfo(const StatusInfo& si) const {
682 if (!isFile()) return false;
683
684 HANDLE h = CreateFile(path.c_str(),
685 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
686 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000687 NULL,
688 OPEN_EXISTING,
689 FILE_ATTRIBUTE_NORMAL,
690 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000691 if (h == INVALID_HANDLE_VALUE)
692 return false;
693
694 BY_HANDLE_FILE_INFORMATION bhfi;
695 if (!GetFileInformationByHandle(h, &bhfi)) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000696 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000697 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000698 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000699 ThrowError(path + ": GetFileInformationByHandle: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000700 }
701
702 FILETIME ft;
703 (uint64_t&)ft = si.modTime.toWin32Time();
704 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000705 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000706 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000707 if (!ret) {
708 SetLastError(err);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000709 ThrowError(path + ": SetFileTime: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000710 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000711
712 // Best we can do with Unix permission bits is to interpret the owner
713 // writable bit.
714 if (si.mode & 0200) {
715 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
716 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000717 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000718 ThrowError(path + ": SetFileAttributes: ");
719 }
720 } else {
721 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
722 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000723 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000724 ThrowError(path + ": SetFileAttributes: ");
725 }
726 }
727
728 return true;
729}
730
Reid Spencerc29befb2004-12-15 01:50:13 +0000731void
Reid Spencera36c9a42004-12-23 22:14:32 +0000732sys::CopyFile(const sys::Path &Dest, const sys::Path &Src) {
Jeff Cohencb652552004-12-24 02:38:34 +0000733 // Can't use CopyFile macro defined in Windows.h because it would mess up the
734 // above line. We use the expansion it would have in a non-UNICODE build.
735 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Reid Spencerc29befb2004-12-15 01:50:13 +0000736 ThrowError("Can't copy '" + Src.toString() +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000737 "' to '" + Dest.toString() + "': ");
Reid Spencerc29befb2004-12-15 01:50:13 +0000738}
739
740void
Jeff Cohencb652552004-12-24 02:38:34 +0000741Path::makeUnique(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000742 if (reuse_current && !exists())
Reid Spencerc29befb2004-12-15 01:50:13 +0000743 return; // File doesn't exist already, just use it!
744
745 Path dir (*this);
746 dir.elideFile();
747 std::string fname = this->getLast();
748
Jeff Cohenab68df02004-12-15 04:08:15 +0000749 char newName[MAX_PATH + 1];
Reid Spencerc29befb2004-12-15 01:50:13 +0000750 if (!GetTempFileName(dir.c_str(), fname.c_str(), 0, newName))
Jeff Cohen51b8d212004-12-31 19:01:08 +0000751 ThrowError("Cannot make unique filename for '" + path + "': ");
Reid Spencerc29befb2004-12-15 01:50:13 +0000752
753 path = newName;
754}
755
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000756bool
757Path::createTemporaryFile(bool reuse_current) {
758 // Make sure we're dealing with a file
759 if (!isFile())
760 return false;
761
762 // Make this into a unique file name
763 makeUnique( reuse_current );
Jeff Cohenf8cdb852004-12-18 06:42:15 +0000764 return true;
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000765}
766
Reid Spencerb016a372004-09-15 05:49:50 +0000767}
Reid Spencercbad7012004-09-11 04:59:30 +0000768}
769
770// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
Reid Spencerb016a372004-09-15 05:49:50 +0000771