blob: a8862380e9eb03e53b7577aceacb87a77c961fc0 [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
58 // A file or directory name may not end in a period.
59 if (path[len-1] == '.')
60 return false;
61 if (len >= 2 && path[len-2] == '.' && path[len-1] == '/')
62 return false;
63
64 // A file or directory name may not end in a space.
65 if (path[len-1] == ' ')
66 return false;
67 if (len >= 2 && path[len-2] == ' ' && path[len-1] == '/')
68 return false;
69
70 return true;
Reid Spencercbad7012004-09-11 04:59:30 +000071}
72
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000073static Path *TempDirectory = NULL;
74
Reid Spencerb016a372004-09-15 05:49:50 +000075Path
76Path::GetTemporaryDirectory() {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000077 if (TempDirectory)
78 return *TempDirectory;
79
80 char pathname[MAX_PATH];
81 if (!GetTempPath(MAX_PATH, pathname))
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000082 throw std::string("Can't determine temporary directory");
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000083
Reid Spencerb016a372004-09-15 05:49:50 +000084 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +000085 result.setDirectory(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000086
87 // Append a subdirectory passed on our process id so multiple LLVMs don't
88 // step on each other's toes.
89 sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
Reid Spencer07adb282004-11-05 22:15:36 +000090 result.appendDirectory(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000091
92 // If there's a directory left over from a previous LLVM execution that
93 // happened to have the same process id, get rid of it.
Reid Spencer07adb282004-11-05 22:15:36 +000094 result.destroyDirectory(true);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000095
96 // And finally (re-)create the empty directory.
Reid Spencer07adb282004-11-05 22:15:36 +000097 result.createDirectory(false);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000098 TempDirectory = new Path(result);
99 return *TempDirectory;
Reid Spencerb016a372004-09-15 05:49:50 +0000100}
101
Reid Spencer732f92d2004-12-13 06:57:15 +0000102Path::Path(const std::string& unverified_path)
Reid Spencerb016a372004-09-15 05:49:50 +0000103 : path(unverified_path)
104{
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000105 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000106 if (unverified_path.empty())
107 return;
Reid Spencer07adb282004-11-05 22:15:36 +0000108 if (this->isValid())
Reid Spencerb016a372004-09-15 05:49:50 +0000109 return;
110 // oops, not valid.
111 path.clear();
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000112 throw std::string(unverified_path + ": path is not valid");
Reid Spencerb016a372004-09-15 05:49:50 +0000113}
114
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000115// FIXME: the following set of functions don't map to Windows very well.
Reid Spencerb016a372004-09-15 05:49:50 +0000116Path
117Path::GetRootDirectory() {
118 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +0000119 result.setDirectory("/");
Reid Spencerb016a372004-09-15 05:49:50 +0000120 return result;
121}
122
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000123static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
124 const char* at = path;
125 const char* delim = strchr(at, ';');
126 Path tmpPath;
127 while( delim != 0 ) {
128 std::string tmp(at, size_t(delim-at));
129 if (tmpPath.setDirectory(tmp))
130 if (tmpPath.readable())
131 Paths.push_back(tmpPath);
132 at = delim + 1;
133 delim = strchr(at, ';');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000134 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000135 if (*at != 0)
136 if (tmpPath.setDirectory(std::string(at)))
137 if (tmpPath.readable())
138 Paths.push_back(tmpPath);
139
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000140}
141
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000142void
143Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000144 Paths.push_back(sys::Path("C:\\WINDOWS\\SYSTEM32\\"));
145 Paths.push_back(sys::Path("C:\\WINDOWS\\"));
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000146}
147
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000148void
149Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
150 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
151 if (env_var != 0) {
152 getPathList(env_var,Paths);
153 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000154#ifdef LLVM_LIBDIR
155 {
156 Path tmpPath;
157 if (tmpPath.setDirectory(LLVM_LIBDIR))
158 if (tmpPath.readable())
159 Paths.push_back(tmpPath);
160 }
161#endif
162 GetSystemLibraryPaths(Paths);
Reid Spencerb016a372004-09-15 05:49:50 +0000163}
164
165Path
166Path::GetLLVMDefaultConfigDir() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000167 // TODO: this isn't going to fly on Windows
Reid Spencerb016a372004-09-15 05:49:50 +0000168 return Path("/etc/llvm/");
169}
170
171Path
Reid Spencerb016a372004-09-15 05:49:50 +0000172Path::GetUserHomeDirectory() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000173 // TODO: Typical Windows setup doesn't define HOME.
Reid Spencerb016a372004-09-15 05:49:50 +0000174 const char* home = getenv("HOME");
175 if (home) {
176 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +0000177 if (result.setDirectory(home))
Reid Spencerb016a372004-09-15 05:49:50 +0000178 return result;
179 }
180 return GetRootDirectory();
181}
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000182// FIXME: the above set of functions don't map to Windows very well.
183
184bool
Reid Spencer07adb282004-11-05 22:15:36 +0000185Path::isFile() const {
186 return (isValid() && path[path.length()-1] != '/');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000187}
188
189bool
Reid Spencer07adb282004-11-05 22:15:36 +0000190Path::isDirectory() const {
191 return (isValid() && path[path.length()-1] == '/');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000192}
193
194std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000195Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000196 // Find the last slash
197 size_t slash = path.rfind('/');
198 if (slash == std::string::npos)
199 slash = 0;
200 else
201 slash++;
202
203 return path.substr(slash, path.rfind('.'));
204}
205
Reid Spencer07adb282004-11-05 22:15:36 +0000206bool Path::hasMagicNumber(const std::string &Magic) const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000207 std::string actualMagic;
208 if (getMagicNumber(actualMagic, Magic.size()))
209 return Magic == actualMagic;
210 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000211}
212
213bool
Reid Spencer07adb282004-11-05 22:15:36 +0000214Path::isBytecodeFile() const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000215 std::string actualMagic;
216 if (!getMagicNumber(actualMagic, 4))
217 return false;
218 return actualMagic == "llvc" || actualMagic == "llvm";
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000219}
220
221bool
Reid Spencerb016a372004-09-15 05:49:50 +0000222Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000223 DWORD attr = GetFileAttributes(path.c_str());
224 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000225}
226
227bool
228Path::readable() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000229 // FIXME: take security attributes into account.
230 DWORD attr = GetFileAttributes(path.c_str());
231 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000232}
233
234bool
235Path::writable() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000236 // FIXME: take security attributes into account.
237 DWORD attr = GetFileAttributes(path.c_str());
238 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000239}
240
241bool
242Path::executable() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000243 // FIXME: take security attributes into account.
244 DWORD attr = GetFileAttributes(path.c_str());
245 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000246}
247
248std::string
249Path::getLast() const {
250 // Find the last slash
251 size_t pos = path.rfind('/');
252
253 // Handle the corner cases
254 if (pos == std::string::npos)
255 return path;
256
257 // If the last character is a slash
258 if (pos == path.length()-1) {
259 // Find the second to last slash
260 size_t pos2 = path.rfind('/', pos-1);
261 if (pos2 == std::string::npos)
262 return path.substr(0,pos);
263 else
264 return path.substr(pos2+1,pos-pos2-1);
265 }
266 // Return everything after the last slash
267 return path.substr(pos+1);
268}
269
Jeff Cohen626e38e2004-12-14 05:26:43 +0000270void
271Path::getStatusInfo(StatusInfo& info) const {
272 WIN32_FILE_ATTRIBUTE_DATA fi;
273 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
274 ThrowError(std::string(path) + ": Can't get status: ");
275
276 info.fileSize = fi.nFileSizeHigh;
277 info.fileSize <<= 32;
278 info.fileSize += fi.nFileSizeLow;
279
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000280 info.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000281 info.user = 9999; // Not applicable to Windows, so...
282 info.group = 9999; // Not applicable to Windows, so...
283
284 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
285 info.modTime.fromWin32Time(ft);
286
287 info.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
288 if (info.isDir && path[path.length() - 1] != '/')
289 path += '/';
290 else if (!info.isDir && path[path.length() - 1] == '/')
291 path.erase(path.length() - 1);
292}
293
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000294static bool AddPermissionBits(const std::string& Filename, int bits) {
295 DWORD attr = GetFileAttributes(Filename.c_str());
296
297 // If it doesn't exist, we're done.
298 if (attr == INVALID_FILE_ATTRIBUTES)
299 return false;
300
301 // The best we can do to interpret Unix permission bits is to use
302 // the owner writable bit.
303 if ((attr & FILE_ATTRIBUTE_READONLY) && (bits & 0200)) {
304 if (!SetFileAttributes(Filename.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohend40a7de2004-12-31 05:07:26 +0000305 ThrowError(Filename + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000306 }
307 return true;
308}
309
Reid Spencer77cc91d2004-12-13 19:59:50 +0000310void Path::makeReadable() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000311 // All files are readable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000312}
313
314void Path::makeWriteable() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000315 DWORD attr = GetFileAttributes(path.c_str());
316
317 // If it doesn't exist, we're done.
318 if (attr == INVALID_FILE_ATTRIBUTES)
319 return;
320
321 if (attr & FILE_ATTRIBUTE_READONLY) {
322 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
323 ThrowError(std::string(path) + ": Can't make file writable: ");
324 }
Reid Spencer77cc91d2004-12-13 19:59:50 +0000325}
326
327void Path::makeExecutable() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000328 // All files are executable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000329}
330
Reid Spencerb016a372004-09-15 05:49:50 +0000331bool
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000332Path::getDirectoryContents(std::set<Path>& result) const {
333 if (!isDirectory())
334 return false;
335
336 result.clear();
337 WIN32_FIND_DATA fd;
338 HANDLE h = FindFirstFile(path.c_str(), &fd);
339 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000340 if (GetLastError() == ERROR_NO_MORE_FILES)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000341 return true; // not really an error, now is it?
Jeff Cohend40a7de2004-12-31 05:07:26 +0000342 ThrowError(path + ": Can't read directory: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000343 }
344
345 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000346 if (fd.cFileName[0] == '.')
347 continue;
348 Path aPath(path + &fd.cFileName[0]);
349 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
350 aPath.path += "/";
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000351 result.insert(aPath);
352 } while (FindNextFile(h, &fd));
353
Jeff Cohen51b8d212004-12-31 19:01:08 +0000354 DWORD err = GetLastError();
355 FindClose(h);
356 if (err != ERROR_NO_MORE_FILES) {
357 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000358 ThrowError(path + ": Can't read directory: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000359 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000360 return true;
361}
362
363bool
Reid Spencer07adb282004-11-05 22:15:36 +0000364Path::setDirectory(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000365 if (a_path.size() == 0)
366 return false;
367 Path save(*this);
368 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000369 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000370 size_t last = a_path.size() -1;
Reid Spencerb0e18872004-12-13 07:51:52 +0000371 if (a_path[last] != '/')
Reid Spencerb016a372004-09-15 05:49:50 +0000372 path += '/';
Reid Spencer07adb282004-11-05 22:15:36 +0000373 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000374 path = save.path;
375 return false;
376 }
377 return true;
378}
379
380bool
Reid Spencer07adb282004-11-05 22:15:36 +0000381Path::setFile(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000382 if (a_path.size() == 0)
383 return false;
384 Path save(*this);
385 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000386 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000387 size_t last = a_path.size() - 1;
388 while (last > 0 && a_path[last] == '/')
389 last--;
390 path.erase(last+1);
Reid Spencer07adb282004-11-05 22:15:36 +0000391 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000392 path = save.path;
393 return false;
394 }
395 return true;
396}
397
398bool
Reid Spencer07adb282004-11-05 22:15:36 +0000399Path::appendDirectory(const std::string& dir) {
400 if (isFile())
Reid Spencerb016a372004-09-15 05:49:50 +0000401 return false;
402 Path save(*this);
403 path += dir;
404 path += "/";
Reid Spencer07adb282004-11-05 22:15:36 +0000405 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000406 path = save.path;
407 return false;
408 }
409 return true;
410}
411
412bool
Reid Spencer07adb282004-11-05 22:15:36 +0000413Path::elideDirectory() {
414 if (isFile())
Reid Spencerb016a372004-09-15 05:49:50 +0000415 return false;
416 size_t slashpos = path.rfind('/',path.size());
417 if (slashpos == 0 || slashpos == std::string::npos)
418 return false;
419 if (slashpos == path.size() - 1)
420 slashpos = path.rfind('/',slashpos-1);
421 if (slashpos == std::string::npos)
422 return false;
423 path.erase(slashpos);
424 return true;
425}
426
427bool
Reid Spencer07adb282004-11-05 22:15:36 +0000428Path::appendFile(const std::string& file) {
429 if (!isDirectory())
Reid Spencerb016a372004-09-15 05:49:50 +0000430 return false;
431 Path save(*this);
432 path += file;
Reid Spencer07adb282004-11-05 22:15:36 +0000433 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000434 path = save.path;
435 return false;
436 }
437 return true;
438}
439
440bool
Reid Spencer07adb282004-11-05 22:15:36 +0000441Path::elideFile() {
442 if (isDirectory())
Reid Spencerb016a372004-09-15 05:49:50 +0000443 return false;
444 size_t slashpos = path.rfind('/',path.size());
445 if (slashpos == std::string::npos)
446 return false;
447 path.erase(slashpos+1);
448 return true;
449}
450
451bool
Reid Spencer07adb282004-11-05 22:15:36 +0000452Path::appendSuffix(const std::string& suffix) {
453 if (isDirectory())
Reid Spencerb016a372004-09-15 05:49:50 +0000454 return false;
455 Path save(*this);
456 path.append(".");
457 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000458 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000459 path = save.path;
460 return false;
461 }
462 return true;
463}
464
465bool
Reid Spencer07adb282004-11-05 22:15:36 +0000466Path::elideSuffix() {
467 if (isDirectory()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000468 size_t dotpos = path.rfind('.',path.size());
469 size_t slashpos = path.rfind('/',path.size());
470 if (slashpos != std::string::npos && dotpos != std::string::npos &&
471 dotpos > slashpos) {
472 path.erase(dotpos, path.size()-dotpos);
473 return true;
474 }
475 return false;
476}
477
478
479bool
Reid Spencer07adb282004-11-05 22:15:36 +0000480Path::createDirectory( bool create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000481 // Make sure we're dealing with a directory
Reid Spencer07adb282004-11-05 22:15:36 +0000482 if (!isDirectory()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000483
484 // Get a writeable copy of the path name
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000485 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1));
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000486 path.copy(pathname,path.length());
487 pathname[path.length()] = 0;
Reid Spencerb016a372004-09-15 05:49:50 +0000488
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000489 // Determine starting point for initial / search.
490 char *next = pathname;
491 if (pathname[0] == '/' && pathname[1] == '/') {
492 // Skip host name.
493 next = strchr(pathname+2, '/');
494 if (next == NULL)
495 throw std::string(pathname) + ": badly formed remote directory";
496 // Skip share name.
497 next = strchr(next+1, '/');
498 if (next == NULL)
499 throw std::string(pathname) + ": badly formed remote directory";
500 next++;
501 if (*next == 0)
502 throw std::string(pathname) + ": badly formed remote directory";
503 } else {
504 if (pathname[1] == ':')
505 next += 2; // skip drive letter
506 if (*next == '/')
507 next++; // skip root directory
508 }
Reid Spencerb016a372004-09-15 05:49:50 +0000509
510 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000511 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000512 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000513 while (*next) {
514 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000515 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000516 if (!CreateDirectory(pathname, NULL))
517 ThrowError(std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000518 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000519 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000520 } else {
521 // Drop trailing slash.
522 pathname[path.size()-1] = 0;
523 if (!CreateDirectory(pathname, NULL)) {
524 ThrowError(std::string(pathname) + ": Can't create directory: ");
525 }
Reid Spencerb016a372004-09-15 05:49:50 +0000526 }
527 return true;
528}
529
530bool
Reid Spencer07adb282004-11-05 22:15:36 +0000531Path::createFile() {
Reid Spencerb016a372004-09-15 05:49:50 +0000532 // Make sure we're dealing with a file
Reid Spencer07adb282004-11-05 22:15:36 +0000533 if (!isFile()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000534
535 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000536 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000537 FILE_ATTRIBUTE_NORMAL, NULL);
538 if (h == INVALID_HANDLE_VALUE)
Jeff Cohen51b8d212004-12-31 19:01:08 +0000539 ThrowError(path + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000540
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000541 CloseHandle(h);
Reid Spencerb016a372004-09-15 05:49:50 +0000542 return true;
543}
544
545bool
Reid Spencer00e89302004-12-15 23:02:10 +0000546Path::destroyDirectory(bool remove_contents) const {
Reid Spencerb016a372004-09-15 05:49:50 +0000547 // Make sure we're dealing with a directory
Reid Spencer07adb282004-11-05 22:15:36 +0000548 if (!isDirectory()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000549
550 // If it doesn't exist, we're done.
551 if (!exists()) return true;
552
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000553 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1));
554 path.copy(pathname,path.length()+1);
555 int lastchar = path.length() - 1 ;
556 if (pathname[lastchar] == '/')
557 pathname[lastchar] = 0;
558
Reid Spencerb016a372004-09-15 05:49:50 +0000559 if (remove_contents) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000560 WIN32_FIND_DATA fd;
561 HANDLE h = FindFirstFile(path.c_str(), &fd);
562
563 // It's a bad idea to alter the contents of a directory while enumerating
564 // its contents. So build a list of its contents first, then destroy them.
565
566 if (h != INVALID_HANDLE_VALUE) {
567 std::vector<Path> list;
568
569 do {
570 if (strcmp(fd.cFileName, ".") == 0)
571 continue;
572 if (strcmp(fd.cFileName, "..") == 0)
573 continue;
574
575 Path aPath(path + &fd.cFileName[0]);
576 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
577 aPath.path += "/";
578 list.push_back(aPath);
579 } while (FindNextFile(h, &fd));
580
581 DWORD err = GetLastError();
582 FindClose(h);
583 if (err != ERROR_NO_MORE_FILES) {
584 SetLastError(err);
585 ThrowError(path + ": Can't read directory: ");
586 }
587
Jeff Cohen45e88d62004-12-31 19:03:31 +0000588 for (std::vector<Path>::iterator I = list.begin(); I != list.end(); ++I) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000589 Path &aPath = *I;
590 if (aPath.isDirectory())
591 aPath.destroyDirectory(true);
592 else
593 aPath.destroyFile();
594 }
595 } else {
596 if (GetLastError() != ERROR_NO_MORE_FILES)
597 ThrowError(path + ": Can't read directory: ");
598 }
Reid Spencerb016a372004-09-15 05:49:50 +0000599 }
Jeff Cohen51b8d212004-12-31 19:01:08 +0000600
601 if (!RemoveDirectory(pathname))
602 ThrowError(std::string(pathname) + ": Can't destroy directory: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000603 return true;
604}
605
606bool
Reid Spencer00e89302004-12-15 23:02:10 +0000607Path::destroyFile() const {
Reid Spencer07adb282004-11-05 22:15:36 +0000608 if (!isFile()) return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000609
610 DWORD attr = GetFileAttributes(path.c_str());
611
612 // If it doesn't exist, we're done.
613 if (attr == INVALID_FILE_ATTRIBUTES)
614 return true;
615
616 // Read-only files cannot be deleted on Windows. Must remove the read-only
617 // attribute first.
618 if (attr & FILE_ATTRIBUTE_READONLY) {
619 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohen51b8d212004-12-31 19:01:08 +0000620 ThrowError(path + ": Can't destroy file: ");
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000621 }
622
623 if (!DeleteFile(path.c_str()))
Jeff Cohen51b8d212004-12-31 19:01:08 +0000624 ThrowError(path + ": Can't destroy file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000625 return true;
626}
627
Reid Spencer3b0cc782004-12-14 18:42:13 +0000628bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
629 if (!isFile())
630 return false;
631 assert(len < 1024 && "Request for magic string too long");
632 char* buf = (char*) alloca(1 + len);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000633
634 HANDLE h = CreateFile(path.c_str(),
635 GENERIC_READ,
636 FILE_SHARE_READ,
637 NULL,
638 OPEN_EXISTING,
639 FILE_ATTRIBUTE_NORMAL,
640 NULL);
641 if (h == INVALID_HANDLE_VALUE)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000642 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000643
644 DWORD nRead = 0;
645 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
646 CloseHandle(h);
647
648 if (!ret || nRead != len)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000649 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000650
Reid Spencer3b0cc782004-12-14 18:42:13 +0000651 buf[len] = '\0';
652 Magic = buf;
653 return true;
654}
655
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000656bool
657Path::renameFile(const Path& newName) {
658 if (!isFile()) return false;
659 if (!MoveFile(path.c_str(), newName.c_str()))
660 ThrowError("Can't move '" + path +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000661 "' to '" + newName.path + "': ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000662 return true;
663}
664
665bool
666Path::setStatusInfo(const StatusInfo& si) const {
667 if (!isFile()) return false;
668
669 HANDLE h = CreateFile(path.c_str(),
670 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
671 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000672 NULL,
673 OPEN_EXISTING,
674 FILE_ATTRIBUTE_NORMAL,
675 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000676 if (h == INVALID_HANDLE_VALUE)
677 return false;
678
679 BY_HANDLE_FILE_INFORMATION bhfi;
680 if (!GetFileInformationByHandle(h, &bhfi)) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000681 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000682 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000683 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000684 ThrowError(path + ": GetFileInformationByHandle: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000685 }
686
687 FILETIME ft;
688 (uint64_t&)ft = si.modTime.toWin32Time();
689 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000690 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000691 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000692 if (!ret) {
693 SetLastError(err);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000694 ThrowError(path + ": SetFileTime: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000695 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000696
697 // Best we can do with Unix permission bits is to interpret the owner
698 // writable bit.
699 if (si.mode & 0200) {
700 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
701 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000702 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000703 ThrowError(path + ": SetFileAttributes: ");
704 }
705 } else {
706 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
707 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000708 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000709 ThrowError(path + ": SetFileAttributes: ");
710 }
711 }
712
713 return true;
714}
715
Reid Spencerc29befb2004-12-15 01:50:13 +0000716void
Reid Spencera36c9a42004-12-23 22:14:32 +0000717sys::CopyFile(const sys::Path &Dest, const sys::Path &Src) {
Jeff Cohencb652552004-12-24 02:38:34 +0000718 // Can't use CopyFile macro defined in Windows.h because it would mess up the
719 // above line. We use the expansion it would have in a non-UNICODE build.
720 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Reid Spencerc29befb2004-12-15 01:50:13 +0000721 ThrowError("Can't copy '" + Src.toString() +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000722 "' to '" + Dest.toString() + "': ");
Reid Spencerc29befb2004-12-15 01:50:13 +0000723}
724
725void
Jeff Cohencb652552004-12-24 02:38:34 +0000726Path::makeUnique(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000727 if (reuse_current && !exists())
Reid Spencerc29befb2004-12-15 01:50:13 +0000728 return; // File doesn't exist already, just use it!
729
730 Path dir (*this);
731 dir.elideFile();
732 std::string fname = this->getLast();
733
Jeff Cohenab68df02004-12-15 04:08:15 +0000734 char newName[MAX_PATH + 1];
Reid Spencerc29befb2004-12-15 01:50:13 +0000735 if (!GetTempFileName(dir.c_str(), fname.c_str(), 0, newName))
Jeff Cohen51b8d212004-12-31 19:01:08 +0000736 ThrowError("Cannot make unique filename for '" + path + "': ");
Reid Spencerc29befb2004-12-15 01:50:13 +0000737
738 path = newName;
739}
740
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000741bool
742Path::createTemporaryFile(bool reuse_current) {
743 // Make sure we're dealing with a file
744 if (!isFile())
745 return false;
746
747 // Make this into a unique file name
748 makeUnique( reuse_current );
Jeff Cohenf8cdb852004-12-18 06:42:15 +0000749 return true;
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000750}
751
Reid Spencerb016a372004-09-15 05:49:50 +0000752}
Reid Spencercbad7012004-09-11 04:59:30 +0000753}
754
755// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
Reid Spencerb016a372004-09-15 05:49:50 +0000756