blob: 526dc8dce7cf13cbfad425a3be76c39110a2e333 [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 <fstream>
24#include <malloc.h>
Reid Spencercbad7012004-09-11 04:59:30 +000025
Jeff Cohencb652552004-12-24 02:38:34 +000026// We need to undo a macro defined in Windows.h, otherwise we won't compile:
27#undef CopyFile
28
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000029static void FlipBackSlashes(std::string& s) {
30 for (size_t i = 0; i < s.size(); i++)
31 if (s[i] == '\\')
32 s[i] = '/';
33}
34
Reid Spencercbad7012004-09-11 04:59:30 +000035namespace llvm {
Reid Spencerb016a372004-09-15 05:49:50 +000036namespace sys {
Reid Spencercbad7012004-09-11 04:59:30 +000037
Reid Spencerb016a372004-09-15 05:49:50 +000038bool
Reid Spencer07adb282004-11-05 22:15:36 +000039Path::isValid() const {
Reid Spencerb016a372004-09-15 05:49:50 +000040 if (path.empty())
41 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000042
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000043 // If there is a colon, it must be the second character, preceded by a letter
44 // and followed by something.
45 size_t len = path.size();
46 size_t pos = path.rfind(':',len);
47 if (pos != std::string::npos) {
48 if (pos != 1 || !isalpha(path[0]) || len < 3)
49 return false;
50 }
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000051
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000052 // Check for illegal characters.
53 if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012"
54 "\013\014\015\016\017\020\021\022\023\024\025\026"
55 "\027\030\031\032\033\034\035\036\037")
56 != std::string::npos)
57 return false;
58
59 // A file or directory name may not end in a period.
60 if (path[len-1] == '.')
61 return false;
62 if (len >= 2 && path[len-2] == '.' && path[len-1] == '/')
63 return false;
64
65 // A file or directory name may not end in a space.
66 if (path[len-1] == ' ')
67 return false;
68 if (len >= 2 && path[len-2] == ' ' && path[len-1] == '/')
69 return false;
70
71 return true;
Reid Spencercbad7012004-09-11 04:59:30 +000072}
73
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000074static Path *TempDirectory = NULL;
75
Reid Spencerb016a372004-09-15 05:49:50 +000076Path
77Path::GetTemporaryDirectory() {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000078 if (TempDirectory)
79 return *TempDirectory;
80
81 char pathname[MAX_PATH];
82 if (!GetTempPath(MAX_PATH, pathname))
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000083 throw std::string("Can't determine temporary directory");
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000084
Reid Spencerb016a372004-09-15 05:49:50 +000085 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +000086 result.setDirectory(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000087
88 // Append a subdirectory passed on our process id so multiple LLVMs don't
89 // step on each other's toes.
90 sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
Reid Spencer07adb282004-11-05 22:15:36 +000091 result.appendDirectory(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000092
93 // If there's a directory left over from a previous LLVM execution that
94 // happened to have the same process id, get rid of it.
Reid Spencer07adb282004-11-05 22:15:36 +000095 result.destroyDirectory(true);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000096
97 // And finally (re-)create the empty directory.
Reid Spencer07adb282004-11-05 22:15:36 +000098 result.createDirectory(false);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000099 TempDirectory = new Path(result);
100 return *TempDirectory;
Reid Spencerb016a372004-09-15 05:49:50 +0000101}
102
Reid Spencer732f92d2004-12-13 06:57:15 +0000103Path::Path(const std::string& unverified_path)
Reid Spencerb016a372004-09-15 05:49:50 +0000104 : path(unverified_path)
105{
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000106 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000107 if (unverified_path.empty())
108 return;
Reid Spencer07adb282004-11-05 22:15:36 +0000109 if (this->isValid())
Reid Spencerb016a372004-09-15 05:49:50 +0000110 return;
111 // oops, not valid.
112 path.clear();
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000113 throw std::string(unverified_path + ": path is not valid");
Reid Spencerb016a372004-09-15 05:49:50 +0000114}
115
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000116// FIXME: the following set of functions don't map to Windows very well.
Reid Spencerb016a372004-09-15 05:49:50 +0000117Path
118Path::GetRootDirectory() {
119 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +0000120 result.setDirectory("/");
Reid Spencerb016a372004-09-15 05:49:50 +0000121 return result;
122}
123
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000124static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
125 const char* at = path;
126 const char* delim = strchr(at, ';');
127 Path tmpPath;
128 while( delim != 0 ) {
129 std::string tmp(at, size_t(delim-at));
130 if (tmpPath.setDirectory(tmp))
131 if (tmpPath.readable())
132 Paths.push_back(tmpPath);
133 at = delim + 1;
134 delim = strchr(at, ';');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000135 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000136 if (*at != 0)
137 if (tmpPath.setDirectory(std::string(at)))
138 if (tmpPath.readable())
139 Paths.push_back(tmpPath);
140
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000141}
142
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000143void
144Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000145 Paths.push_back(sys::Path("C:\\WINDOWS\\SYSTEM32\\"));
146 Paths.push_back(sys::Path("C:\\WINDOWS\\"));
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000147}
148
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000149void
150Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
151 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
152 if (env_var != 0) {
153 getPathList(env_var,Paths);
154 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000155#ifdef LLVM_LIBDIR
156 {
157 Path tmpPath;
158 if (tmpPath.setDirectory(LLVM_LIBDIR))
159 if (tmpPath.readable())
160 Paths.push_back(tmpPath);
161 }
162#endif
163 GetSystemLibraryPaths(Paths);
Reid Spencerb016a372004-09-15 05:49:50 +0000164}
165
166Path
167Path::GetLLVMDefaultConfigDir() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000168 // TODO: this isn't going to fly on Windows
Reid Spencerb016a372004-09-15 05:49:50 +0000169 return Path("/etc/llvm/");
170}
171
172Path
Reid Spencerb016a372004-09-15 05:49:50 +0000173Path::GetUserHomeDirectory() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000174 // TODO: Typical Windows setup doesn't define HOME.
Reid Spencerb016a372004-09-15 05:49:50 +0000175 const char* home = getenv("HOME");
176 if (home) {
177 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +0000178 if (result.setDirectory(home))
Reid Spencerb016a372004-09-15 05:49:50 +0000179 return result;
180 }
181 return GetRootDirectory();
182}
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000183// FIXME: the above set of functions don't map to Windows very well.
184
185bool
Reid Spencer07adb282004-11-05 22:15:36 +0000186Path::isFile() const {
187 return (isValid() && path[path.length()-1] != '/');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000188}
189
190bool
Reid Spencer07adb282004-11-05 22:15:36 +0000191Path::isDirectory() const {
192 return (isValid() && path[path.length()-1] == '/');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000193}
194
195std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000196Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000197 // Find the last slash
198 size_t slash = path.rfind('/');
199 if (slash == std::string::npos)
200 slash = 0;
201 else
202 slash++;
203
204 return path.substr(slash, path.rfind('.'));
205}
206
Reid Spencer07adb282004-11-05 22:15:36 +0000207bool Path::hasMagicNumber(const std::string &Magic) const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000208 size_t len = Magic.size();
209 char *buf = reinterpret_cast<char *>(_alloca(len+1));
210 std::ifstream f(path.c_str());
211 f.read(buf, len);
212 buf[len] = '\0';
213 return Magic == buf;
214}
215
216bool
Reid Spencer07adb282004-11-05 22:15:36 +0000217Path::isBytecodeFile() const {
Reid Spencer4b826812004-11-09 20:27:23 +0000218 char buffer[ 4];
219 buffer[0] = 0;
220 std::ifstream f(path.c_str());
221 f.read(buffer, 4);
222 if (f.bad())
223 ThrowErrno("can't read file signature");
224 return 0 == memcmp(buffer,"llvc",4) || 0 == memcmp(buffer,"llvm",4);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000225}
226
227bool
Reid Spencerb016a372004-09-15 05:49:50 +0000228Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000229 DWORD attr = GetFileAttributes(path.c_str());
230 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000231}
232
233bool
234Path::readable() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000235 // FIXME: take security attributes into account.
236 DWORD attr = GetFileAttributes(path.c_str());
237 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000238}
239
240bool
241Path::writable() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000242 // FIXME: take security attributes into account.
243 DWORD attr = GetFileAttributes(path.c_str());
244 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000245}
246
247bool
248Path::executable() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000249 // FIXME: take security attributes into account.
250 DWORD attr = GetFileAttributes(path.c_str());
251 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000252}
253
254std::string
255Path::getLast() const {
256 // Find the last slash
257 size_t pos = path.rfind('/');
258
259 // Handle the corner cases
260 if (pos == std::string::npos)
261 return path;
262
263 // If the last character is a slash
264 if (pos == path.length()-1) {
265 // Find the second to last slash
266 size_t pos2 = path.rfind('/', pos-1);
267 if (pos2 == std::string::npos)
268 return path.substr(0,pos);
269 else
270 return path.substr(pos2+1,pos-pos2-1);
271 }
272 // Return everything after the last slash
273 return path.substr(pos+1);
274}
275
Jeff Cohen626e38e2004-12-14 05:26:43 +0000276void
277Path::getStatusInfo(StatusInfo& info) const {
278 WIN32_FILE_ATTRIBUTE_DATA fi;
279 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
280 ThrowError(std::string(path) + ": Can't get status: ");
281
282 info.fileSize = fi.nFileSizeHigh;
283 info.fileSize <<= 32;
284 info.fileSize += fi.nFileSizeLow;
285
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000286 info.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000287 info.user = 9999; // Not applicable to Windows, so...
288 info.group = 9999; // Not applicable to Windows, so...
289
290 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
291 info.modTime.fromWin32Time(ft);
292
293 info.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
294 if (info.isDir && path[path.length() - 1] != '/')
295 path += '/';
296 else if (!info.isDir && path[path.length() - 1] == '/')
297 path.erase(path.length() - 1);
298}
299
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000300static bool AddPermissionBits(const std::string& Filename, int bits) {
301 DWORD attr = GetFileAttributes(Filename.c_str());
302
303 // If it doesn't exist, we're done.
304 if (attr == INVALID_FILE_ATTRIBUTES)
305 return false;
306
307 // The best we can do to interpret Unix permission bits is to use
308 // the owner writable bit.
309 if ((attr & FILE_ATTRIBUTE_READONLY) && (bits & 0200)) {
310 if (!SetFileAttributes(Filename.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohend40a7de2004-12-31 05:07:26 +0000311 ThrowError(Filename + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000312 }
313 return true;
314}
315
Reid Spencer77cc91d2004-12-13 19:59:50 +0000316void Path::makeReadable() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000317 // All files are readable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000318}
319
320void Path::makeWriteable() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000321 DWORD attr = GetFileAttributes(path.c_str());
322
323 // If it doesn't exist, we're done.
324 if (attr == INVALID_FILE_ATTRIBUTES)
325 return;
326
327 if (attr & FILE_ATTRIBUTE_READONLY) {
328 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
329 ThrowError(std::string(path) + ": Can't make file writable: ");
330 }
Reid Spencer77cc91d2004-12-13 19:59:50 +0000331}
332
333void Path::makeExecutable() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000334 // All files are executable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000335}
336
Reid Spencerb016a372004-09-15 05:49:50 +0000337bool
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000338Path::getDirectoryContents(std::set<Path>& result) const {
339 if (!isDirectory())
340 return false;
341
342 result.clear();
343 WIN32_FIND_DATA fd;
344 HANDLE h = FindFirstFile(path.c_str(), &fd);
345 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000346 if (GetLastError() == ERROR_NO_MORE_FILES)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000347 return true; // not really an error, now is it?
Jeff Cohend40a7de2004-12-31 05:07:26 +0000348 ThrowError(path + ": Can't read directory: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000349 }
350
351 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000352 if (fd.cFileName[0] == '.')
353 continue;
354 Path aPath(path + &fd.cFileName[0]);
355 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
356 aPath.path += "/";
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000357 result.insert(aPath);
358 } while (FindNextFile(h, &fd));
359
360 CloseHandle(h);
361 if (GetLastError() != ERROR_NO_MORE_FILES)
Jeff Cohend40a7de2004-12-31 05:07:26 +0000362 ThrowError(path + ": Can't read directory: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000363 return true;
364}
365
366bool
Reid Spencer07adb282004-11-05 22:15:36 +0000367Path::setDirectory(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000368 if (a_path.size() == 0)
369 return false;
370 Path save(*this);
371 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000372 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000373 size_t last = a_path.size() -1;
Reid Spencerb0e18872004-12-13 07:51:52 +0000374 if (a_path[last] != '/')
Reid Spencerb016a372004-09-15 05:49:50 +0000375 path += '/';
Reid Spencer07adb282004-11-05 22:15:36 +0000376 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000377 path = save.path;
378 return false;
379 }
380 return true;
381}
382
383bool
Reid Spencer07adb282004-11-05 22:15:36 +0000384Path::setFile(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000385 if (a_path.size() == 0)
386 return false;
387 Path save(*this);
388 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000389 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000390 size_t last = a_path.size() - 1;
391 while (last > 0 && a_path[last] == '/')
392 last--;
393 path.erase(last+1);
Reid Spencer07adb282004-11-05 22:15:36 +0000394 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000395 path = save.path;
396 return false;
397 }
398 return true;
399}
400
401bool
Reid Spencer07adb282004-11-05 22:15:36 +0000402Path::appendDirectory(const std::string& dir) {
403 if (isFile())
Reid Spencerb016a372004-09-15 05:49:50 +0000404 return false;
405 Path save(*this);
406 path += dir;
407 path += "/";
Reid Spencer07adb282004-11-05 22:15:36 +0000408 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000409 path = save.path;
410 return false;
411 }
412 return true;
413}
414
415bool
Reid Spencer07adb282004-11-05 22:15:36 +0000416Path::elideDirectory() {
417 if (isFile())
Reid Spencerb016a372004-09-15 05:49:50 +0000418 return false;
419 size_t slashpos = path.rfind('/',path.size());
420 if (slashpos == 0 || slashpos == std::string::npos)
421 return false;
422 if (slashpos == path.size() - 1)
423 slashpos = path.rfind('/',slashpos-1);
424 if (slashpos == std::string::npos)
425 return false;
426 path.erase(slashpos);
427 return true;
428}
429
430bool
Reid Spencer07adb282004-11-05 22:15:36 +0000431Path::appendFile(const std::string& file) {
432 if (!isDirectory())
Reid Spencerb016a372004-09-15 05:49:50 +0000433 return false;
434 Path save(*this);
435 path += file;
Reid Spencer07adb282004-11-05 22:15:36 +0000436 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000437 path = save.path;
438 return false;
439 }
440 return true;
441}
442
443bool
Reid Spencer07adb282004-11-05 22:15:36 +0000444Path::elideFile() {
445 if (isDirectory())
Reid Spencerb016a372004-09-15 05:49:50 +0000446 return false;
447 size_t slashpos = path.rfind('/',path.size());
448 if (slashpos == std::string::npos)
449 return false;
450 path.erase(slashpos+1);
451 return true;
452}
453
454bool
Reid Spencer07adb282004-11-05 22:15:36 +0000455Path::appendSuffix(const std::string& suffix) {
456 if (isDirectory())
Reid Spencerb016a372004-09-15 05:49:50 +0000457 return false;
458 Path save(*this);
459 path.append(".");
460 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000461 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000462 path = save.path;
463 return false;
464 }
465 return true;
466}
467
468bool
Reid Spencer07adb282004-11-05 22:15:36 +0000469Path::elideSuffix() {
470 if (isDirectory()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000471 size_t dotpos = path.rfind('.',path.size());
472 size_t slashpos = path.rfind('/',path.size());
473 if (slashpos != std::string::npos && dotpos != std::string::npos &&
474 dotpos > slashpos) {
475 path.erase(dotpos, path.size()-dotpos);
476 return true;
477 }
478 return false;
479}
480
481
482bool
Reid Spencer07adb282004-11-05 22:15:36 +0000483Path::createDirectory( bool create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000484 // Make sure we're dealing with a directory
Reid Spencer07adb282004-11-05 22:15:36 +0000485 if (!isDirectory()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000486
487 // Get a writeable copy of the path name
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000488 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1));
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000489 path.copy(pathname,path.length());
490 pathname[path.length()] = 0;
Reid Spencerb016a372004-09-15 05:49:50 +0000491
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000492 // Determine starting point for initial / search.
493 char *next = pathname;
494 if (pathname[0] == '/' && pathname[1] == '/') {
495 // Skip host name.
496 next = strchr(pathname+2, '/');
497 if (next == NULL)
498 throw std::string(pathname) + ": badly formed remote directory";
499 // Skip share name.
500 next = strchr(next+1, '/');
501 if (next == NULL)
502 throw std::string(pathname) + ": badly formed remote directory";
503 next++;
504 if (*next == 0)
505 throw std::string(pathname) + ": badly formed remote directory";
506 } else {
507 if (pathname[1] == ':')
508 next += 2; // skip drive letter
509 if (*next == '/')
510 next++; // skip root directory
511 }
Reid Spencerb016a372004-09-15 05:49:50 +0000512
513 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000514 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000515 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000516 while (*next) {
517 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000518 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000519 if (!CreateDirectory(pathname, NULL))
520 ThrowError(std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000521 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000522 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000523 } else {
524 // Drop trailing slash.
525 pathname[path.size()-1] = 0;
526 if (!CreateDirectory(pathname, NULL)) {
527 ThrowError(std::string(pathname) + ": Can't create directory: ");
528 }
Reid Spencerb016a372004-09-15 05:49:50 +0000529 }
530 return true;
531}
532
533bool
Reid Spencer07adb282004-11-05 22:15:36 +0000534Path::createFile() {
Reid Spencerb016a372004-09-15 05:49:50 +0000535 // Make sure we're dealing with a file
Reid Spencer07adb282004-11-05 22:15:36 +0000536 if (!isFile()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000537
538 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000539 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000540 FILE_ATTRIBUTE_NORMAL, NULL);
541 if (h == INVALID_HANDLE_VALUE)
542 ThrowError(std::string(path.c_str()) + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000543
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000544 CloseHandle(h);
Reid Spencerb016a372004-09-15 05:49:50 +0000545 return true;
546}
547
548bool
Reid Spencer00e89302004-12-15 23:02:10 +0000549Path::destroyDirectory(bool remove_contents) const {
Reid Spencerb016a372004-09-15 05:49:50 +0000550 // Make sure we're dealing with a directory
Reid Spencer07adb282004-11-05 22:15:36 +0000551 if (!isDirectory()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000552
553 // If it doesn't exist, we're done.
554 if (!exists()) return true;
555
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000556 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1));
557 path.copy(pathname,path.length()+1);
558 int lastchar = path.length() - 1 ;
559 if (pathname[lastchar] == '/')
560 pathname[lastchar] = 0;
561
Reid Spencerb016a372004-09-15 05:49:50 +0000562 if (remove_contents) {
563 // Recursively descend the directory to remove its content
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000564 // FIXME: The correct way of doing this on Windows isn't pretty...
565 // but this may work if unix-like utils are present.
566 std::string cmd("rm -rf ");
Reid Spencerb016a372004-09-15 05:49:50 +0000567 cmd += path;
568 system(cmd.c_str());
569 } else {
570 // Otherwise, try to just remove the one directory
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000571 if (!RemoveDirectory(pathname))
572 ThrowError(std::string(pathname) + ": Can't destroy directory: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000573 }
574 return true;
575}
576
577bool
Reid Spencer00e89302004-12-15 23:02:10 +0000578Path::destroyFile() const {
Reid Spencer07adb282004-11-05 22:15:36 +0000579 if (!isFile()) return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000580
581 DWORD attr = GetFileAttributes(path.c_str());
582
583 // If it doesn't exist, we're done.
584 if (attr == INVALID_FILE_ATTRIBUTES)
585 return true;
586
587 // Read-only files cannot be deleted on Windows. Must remove the read-only
588 // attribute first.
589 if (attr & FILE_ATTRIBUTE_READONLY) {
590 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
591 ThrowError(std::string(path.c_str()) + ": Can't destroy file: ");
592 }
593
594 if (!DeleteFile(path.c_str()))
595 ThrowError(std::string(path.c_str()) + ": Can't destroy file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000596 return true;
597}
598
Reid Spencer3b0cc782004-12-14 18:42:13 +0000599bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
600 if (!isFile())
601 return false;
602 assert(len < 1024 && "Request for magic string too long");
603 char* buf = (char*) alloca(1 + len);
604 std::ofstream ofs(path.c_str(),std::ofstream::in);
605 if (!ofs.is_open())
606 return false;
607 std::ifstream ifs(path.c_str());
608 if (!ifs.is_open())
609 return false;
610 ifs.read(buf, len);
611 ofs.close();
612 ifs.close();
613 buf[len] = '\0';
614 Magic = buf;
615 return true;
616}
617
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000618bool
619Path::renameFile(const Path& newName) {
620 if (!isFile()) return false;
621 if (!MoveFile(path.c_str(), newName.c_str()))
622 ThrowError("Can't move '" + path +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000623 "' to '" + newName.path + "': ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000624 return true;
625}
626
627bool
628Path::setStatusInfo(const StatusInfo& si) const {
629 if (!isFile()) return false;
630
631 HANDLE h = CreateFile(path.c_str(),
632 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
633 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000634 NULL,
635 OPEN_EXISTING,
636 FILE_ATTRIBUTE_NORMAL,
637 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000638 if (h == INVALID_HANDLE_VALUE)
639 return false;
640
641 BY_HANDLE_FILE_INFORMATION bhfi;
642 if (!GetFileInformationByHandle(h, &bhfi)) {
643 CloseHandle(h);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000644 ThrowError(path + ": GetFileInformationByHandle: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000645 }
646
647 FILETIME ft;
648 (uint64_t&)ft = si.modTime.toWin32Time();
649 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
650 CloseHandle(h);
651 if (!ret)
652 ThrowError(path + ": SetFileTime: ");
653
654 // Best we can do with Unix permission bits is to interpret the owner
655 // writable bit.
656 if (si.mode & 0200) {
657 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
658 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000659 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000660 ThrowError(path + ": SetFileAttributes: ");
661 }
662 } else {
663 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
664 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000665 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000666 ThrowError(path + ": SetFileAttributes: ");
667 }
668 }
669
670 return true;
671}
672
Reid Spencerc29befb2004-12-15 01:50:13 +0000673void
Reid Spencera36c9a42004-12-23 22:14:32 +0000674sys::CopyFile(const sys::Path &Dest, const sys::Path &Src) {
Jeff Cohencb652552004-12-24 02:38:34 +0000675 // Can't use CopyFile macro defined in Windows.h because it would mess up the
676 // above line. We use the expansion it would have in a non-UNICODE build.
677 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Reid Spencerc29befb2004-12-15 01:50:13 +0000678 ThrowError("Can't copy '" + Src.toString() +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000679 "' to '" + Dest.toString() + "': ");
Reid Spencerc29befb2004-12-15 01:50:13 +0000680}
681
682void
Jeff Cohencb652552004-12-24 02:38:34 +0000683Path::makeUnique(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000684 if (reuse_current && !exists())
Reid Spencerc29befb2004-12-15 01:50:13 +0000685 return; // File doesn't exist already, just use it!
686
687 Path dir (*this);
688 dir.elideFile();
689 std::string fname = this->getLast();
690
Jeff Cohenab68df02004-12-15 04:08:15 +0000691 char newName[MAX_PATH + 1];
Reid Spencerc29befb2004-12-15 01:50:13 +0000692 if (!GetTempFileName(dir.c_str(), fname.c_str(), 0, newName))
693 ThrowError("Cannot make unique filename for '" + path + "'");
694
695 path = newName;
696}
697
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000698bool
699Path::createTemporaryFile(bool reuse_current) {
700 // Make sure we're dealing with a file
701 if (!isFile())
702 return false;
703
704 // Make this into a unique file name
705 makeUnique( reuse_current );
Jeff Cohenf8cdb852004-12-18 06:42:15 +0000706 return true;
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000707}
708
Reid Spencerb016a372004-09-15 05:49:50 +0000709}
Reid Spencercbad7012004-09-11 04:59:30 +0000710}
711
712// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
Reid Spencerb016a372004-09-15 05:49:50 +0000713