blob: fb6351695dfd2915d0bbe73e293bc035a350517d [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 Spencer1cf2d042005-07-07 23:35:23 +000097 result.set(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 Spencer1cf2d042005-07-07 23:35:23 +0000131 result.set("C:\\");
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));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000141 if (tmpPath.set(tmp))
Reid Spencerc7f08322005-07-07 18:21:42 +0000142 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000143 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)
Reid Spencer1cf2d042005-07-07 23:35:23 +0000148 if (tmpPath.set(std::string(at)))
Reid Spencerc7f08322005-07-07 18:21:42 +0000149 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000150 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;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000169 if (tmpPath.set(LLVM_LIBDIR))
Reid Spencerc7f08322005-07-07 18:21:42 +0000170 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000171 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 Spencer1cf2d042005-07-07 23:35:23 +0000189 if (result.set(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 {
Reid Spencerdd04df02005-07-07 23:21:43 +0000198 WIN32_FILE_ATTRIBUTE_DATA fi;
199 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
200 ThrowError(std::string(path) + ": Can't get status: ");
201 return fi.dwFileAttributes & FILE_ATTRIBUTE_NORMAL;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000202}
203
204bool
Reid Spencer07adb282004-11-05 22:15:36 +0000205Path::isDirectory() const {
Reid Spencerdd04df02005-07-07 23:21:43 +0000206 WIN32_FILE_ATTRIBUTE_DATA fi;
207 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
208 ThrowError(std::string(path) + ": Can't get status: ");
209 return fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000210}
211
212std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000213Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000214 // Find the last slash
215 size_t slash = path.rfind('/');
216 if (slash == std::string::npos)
217 slash = 0;
218 else
219 slash++;
220
221 return path.substr(slash, path.rfind('.'));
222}
223
Reid Spencer07adb282004-11-05 22:15:36 +0000224bool Path::hasMagicNumber(const std::string &Magic) const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000225 std::string actualMagic;
226 if (getMagicNumber(actualMagic, Magic.size()))
227 return Magic == actualMagic;
228 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000229}
230
231bool
Reid Spencer07adb282004-11-05 22:15:36 +0000232Path::isBytecodeFile() const {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000233 std::string actualMagic;
234 if (!getMagicNumber(actualMagic, 4))
235 return false;
236 return actualMagic == "llvc" || actualMagic == "llvm";
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000237}
238
239bool
Reid Spencerb016a372004-09-15 05:49:50 +0000240Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000241 DWORD attr = GetFileAttributes(path.c_str());
242 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000243}
244
245bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000246Path::canRead() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000247 // FIXME: take security attributes into account.
248 DWORD attr = GetFileAttributes(path.c_str());
249 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000250}
251
252bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000253Path::canWrite() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000254 // FIXME: take security attributes into account.
255 DWORD attr = GetFileAttributes(path.c_str());
256 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000257}
258
259bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000260Path::canExecute() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000261 // FIXME: take security attributes into account.
262 DWORD attr = GetFileAttributes(path.c_str());
263 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000264}
265
266std::string
267Path::getLast() const {
268 // Find the last slash
269 size_t pos = path.rfind('/');
270
271 // Handle the corner cases
272 if (pos == std::string::npos)
273 return path;
274
275 // If the last character is a slash
276 if (pos == path.length()-1) {
277 // Find the second to last slash
278 size_t pos2 = path.rfind('/', pos-1);
279 if (pos2 == std::string::npos)
280 return path.substr(0,pos);
281 else
282 return path.substr(pos2+1,pos-pos2-1);
283 }
284 // Return everything after the last slash
285 return path.substr(pos+1);
286}
287
Jeff Cohen626e38e2004-12-14 05:26:43 +0000288void
289Path::getStatusInfo(StatusInfo& info) const {
290 WIN32_FILE_ATTRIBUTE_DATA fi;
291 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
292 ThrowError(std::string(path) + ": Can't get status: ");
293
294 info.fileSize = fi.nFileSizeHigh;
295 info.fileSize <<= 32;
296 info.fileSize += fi.nFileSizeLow;
297
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000298 info.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000299 info.user = 9999; // Not applicable to Windows, so...
300 info.group = 9999; // Not applicable to Windows, so...
301
302 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
303 info.modTime.fromWin32Time(ft);
304
305 info.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
306 if (info.isDir && path[path.length() - 1] != '/')
307 path += '/';
308 else if (!info.isDir && path[path.length() - 1] == '/')
309 path.erase(path.length() - 1);
310}
311
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000312static bool AddPermissionBits(const std::string& Filename, int bits) {
313 DWORD attr = GetFileAttributes(Filename.c_str());
314
315 // If it doesn't exist, we're done.
316 if (attr == INVALID_FILE_ATTRIBUTES)
317 return false;
318
319 // The best we can do to interpret Unix permission bits is to use
320 // the owner writable bit.
321 if ((attr & FILE_ATTRIBUTE_READONLY) && (bits & 0200)) {
322 if (!SetFileAttributes(Filename.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohend40a7de2004-12-31 05:07:26 +0000323 ThrowError(Filename + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000324 }
325 return true;
326}
327
Reid Spencer77cc91d2004-12-13 19:59:50 +0000328void Path::makeReadable() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000329 // All files are readable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000330}
331
332void Path::makeWriteable() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000333 DWORD attr = GetFileAttributes(path.c_str());
334
335 // If it doesn't exist, we're done.
336 if (attr == INVALID_FILE_ATTRIBUTES)
337 return;
338
339 if (attr & FILE_ATTRIBUTE_READONLY) {
340 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
341 ThrowError(std::string(path) + ": Can't make file writable: ");
342 }
Reid Spencer77cc91d2004-12-13 19:59:50 +0000343}
344
345void Path::makeExecutable() {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000346 // All files are executable on Windows (ignoring security attributes).
Reid Spencer77cc91d2004-12-13 19:59:50 +0000347}
348
Reid Spencerb016a372004-09-15 05:49:50 +0000349bool
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000350Path::getDirectoryContents(std::set<Path>& result) const {
351 if (!isDirectory())
352 return false;
353
354 result.clear();
355 WIN32_FIND_DATA fd;
Jeff Cohen9437bb62005-01-27 03:49:03 +0000356 std::string searchpath = path + "*";
357 HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000358 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohen9437bb62005-01-27 03:49:03 +0000359 if (GetLastError() == ERROR_FILE_NOT_FOUND)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000360 return true; // not really an error, now is it?
Jeff Cohend40a7de2004-12-31 05:07:26 +0000361 ThrowError(path + ": Can't read directory: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000362 }
363
364 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000365 if (fd.cFileName[0] == '.')
366 continue;
367 Path aPath(path + &fd.cFileName[0]);
368 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
369 aPath.path += "/";
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000370 result.insert(aPath);
371 } while (FindNextFile(h, &fd));
372
Jeff Cohen51b8d212004-12-31 19:01:08 +0000373 DWORD err = GetLastError();
374 FindClose(h);
375 if (err != ERROR_NO_MORE_FILES) {
376 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000377 ThrowError(path + ": Can't read directory: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000378 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000379 return true;
380}
381
382bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000383Path::set(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000384 if (a_path.size() == 0)
385 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000386 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000387 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000388 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000389 size_t last = a_path.size() -1;
Reid Spencer07adb282004-11-05 22:15:36 +0000390 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000391 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000392 return false;
393 }
394 return true;
395}
396
397bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000398Path::appendComponent(const std::string& dir) {
399 if (name.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000400 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000401 std::string save(path);
402 if (!path.empty()) {
403 size_t last = path.size() - 1;
404 if (path[last] != '/')
405 path += '/';
406 }
407 path += name;
Reid Spencer07adb282004-11-05 22:15:36 +0000408 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000409 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000410 return false;
411 }
412 return true;
413}
414
415bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000416Path::eraseComponent() {
Reid Spencerb016a372004-09-15 05:49:50 +0000417 size_t slashpos = path.rfind('/',path.size());
418 if (slashpos == 0 || slashpos == std::string::npos)
419 return false;
420 if (slashpos == path.size() - 1)
421 slashpos = path.rfind('/',slashpos-1);
422 if (slashpos == std::string::npos)
423 return false;
424 path.erase(slashpos);
425 return true;
426}
427
428bool
Reid Spencer07adb282004-11-05 22:15:36 +0000429Path::appendSuffix(const std::string& suffix) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000430 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000431 path.append(".");
432 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000433 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000434 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000435 return false;
436 }
437 return true;
438}
439
440bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000441Path::eraseSuffix() {
Reid Spencerb016a372004-09-15 05:49:50 +0000442 size_t dotpos = path.rfind('.',path.size());
443 size_t slashpos = path.rfind('/',path.size());
444 if (slashpos != std::string::npos && dotpos != std::string::npos &&
445 dotpos > slashpos) {
446 path.erase(dotpos, path.size()-dotpos);
447 return true;
448 }
449 return false;
450}
451
Reid Spencerb016a372004-09-15 05:49:50 +0000452bool
Reid Spencer07adb282004-11-05 22:15:36 +0000453Path::createDirectory( bool create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000454 // Get a writeable copy of the path name
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000455 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1));
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000456 path.copy(pathname,path.length());
457 pathname[path.length()] = 0;
Reid Spencerb016a372004-09-15 05:49:50 +0000458
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000459 // Determine starting point for initial / search.
460 char *next = pathname;
461 if (pathname[0] == '/' && pathname[1] == '/') {
462 // Skip host name.
463 next = strchr(pathname+2, '/');
464 if (next == NULL)
465 throw std::string(pathname) + ": badly formed remote directory";
466 // Skip share name.
467 next = strchr(next+1, '/');
468 if (next == NULL)
469 throw std::string(pathname) + ": badly formed remote directory";
470 next++;
471 if (*next == 0)
472 throw std::string(pathname) + ": badly formed remote directory";
473 } else {
474 if (pathname[1] == ':')
475 next += 2; // skip drive letter
476 if (*next == '/')
477 next++; // skip root directory
478 }
Reid Spencerb016a372004-09-15 05:49:50 +0000479
480 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000481 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000482 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000483 while (*next) {
484 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000485 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000486 if (!CreateDirectory(pathname, NULL))
487 ThrowError(std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000488 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000489 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000490 } else {
491 // Drop trailing slash.
492 pathname[path.size()-1] = 0;
493 if (!CreateDirectory(pathname, NULL)) {
494 ThrowError(std::string(pathname) + ": Can't create directory: ");
495 }
Reid Spencerb016a372004-09-15 05:49:50 +0000496 }
497 return true;
498}
499
500bool
Reid Spencer07adb282004-11-05 22:15:36 +0000501Path::createFile() {
Reid Spencerb016a372004-09-15 05:49:50 +0000502 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000503 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000504 FILE_ATTRIBUTE_NORMAL, NULL);
505 if (h == INVALID_HANDLE_VALUE)
Jeff Cohen51b8d212004-12-31 19:01:08 +0000506 ThrowError(path + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000507
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000508 CloseHandle(h);
Reid Spencerb016a372004-09-15 05:49:50 +0000509 return true;
510}
511
512bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000513Path::destroy(bool remove_contents) const {
514 if (isFile()) {
515 DWORD attr = GetFileAttributes(path.c_str());
Reid Spencerb016a372004-09-15 05:49:50 +0000516
Reid Spencer1cf2d042005-07-07 23:35:23 +0000517 // If it doesn't exist, we're done.
518 if (attr == INVALID_FILE_ATTRIBUTES)
519 return true;
Reid Spencerb016a372004-09-15 05:49:50 +0000520
Reid Spencer1cf2d042005-07-07 23:35:23 +0000521 // Read-only files cannot be deleted on Windows. Must remove the read-only
522 // attribute first.
523 if (attr & FILE_ATTRIBUTE_READONLY) {
524 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
525 ThrowError(path + ": Can't destroy file: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000526 }
Jeff Cohen51b8d212004-12-31 19:01:08 +0000527
Reid Spencer1cf2d042005-07-07 23:35:23 +0000528 if (!DeleteFile(path.c_str()))
Jeff Cohen51b8d212004-12-31 19:01:08 +0000529 ThrowError(path + ": Can't destroy file: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000530 return true;
531 } else if (isDirectory()) {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000532
Reid Spencer1cf2d042005-07-07 23:35:23 +0000533 // If it doesn't exist, we're done.
534 if (!exists())
535 return true;
536
537 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+2));
538 int lastchar = path.length() - 1 ;
539 path.copy(pathname,lastchar+2);
540
541 // Make path end with '/*'.
542 pathname[lastchar+1] = '*';
543 pathname[lastchar+2] = 0;
544
545 if (remove_contents) {
546 WIN32_FIND_DATA fd;
547 HANDLE h = FindFirstFile(pathname, &fd);
548
549 // It's a bad idea to alter the contents of a directory while enumerating
550 // its contents. So build a list of its contents first, then destroy them.
551
552 if (h != INVALID_HANDLE_VALUE) {
553 std::vector<Path> list;
554
555 do {
556 if (strcmp(fd.cFileName, ".") == 0)
557 continue;
558 if (strcmp(fd.cFileName, "..") == 0)
559 continue;
560
561 Path aPath(path + &fd.cFileName[0]);
562 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
563 aPath.path += "/";
564 list.push_back(aPath);
565 } while (FindNextFile(h, &fd));
566
567 DWORD err = GetLastError();
568 FindClose(h);
569 if (err != ERROR_NO_MORE_FILES) {
570 SetLastError(err);
571 ThrowError(path + ": Can't read directory: ");
572 }
573
574 for (std::vector<Path>::iterator I = list.begin(); I != list.end();
575 ++I) {
576 Path &aPath = *I;
577 aPath.destroy(true);
578 }
579 } else {
580 if (GetLastError() != ERROR_FILE_NOT_FOUND)
581 ThrowError(path + ": Can't read directory: ");
582 }
583 }
584
585 pathname[lastchar] = 0;
586 if (!RemoveDirectory(pathname))
587 ThrowError(std::string(pathname) + ": Can't destroy directory: ");
588 return true;
589 }
Reid Spencerb016a372004-09-15 05:49:50 +0000590}
591
Reid Spencer3b0cc782004-12-14 18:42:13 +0000592bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
593 if (!isFile())
594 return false;
595 assert(len < 1024 && "Request for magic string too long");
596 char* buf = (char*) alloca(1 + len);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000597
598 HANDLE h = CreateFile(path.c_str(),
599 GENERIC_READ,
600 FILE_SHARE_READ,
601 NULL,
602 OPEN_EXISTING,
603 FILE_ATTRIBUTE_NORMAL,
604 NULL);
605 if (h == INVALID_HANDLE_VALUE)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000606 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000607
608 DWORD nRead = 0;
609 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
610 CloseHandle(h);
611
612 if (!ret || nRead != len)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000613 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000614
Reid Spencer3b0cc782004-12-14 18:42:13 +0000615 buf[len] = '\0';
616 Magic = buf;
617 return true;
618}
619
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000620bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000621Path::rename(const Path& newName) {
622 // FIXME: This should rename a directory too.
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000623 if (!isFile()) return false;
624 if (!MoveFile(path.c_str(), newName.c_str()))
625 ThrowError("Can't move '" + path +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000626 "' to '" + newName.path + "': ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000627 return true;
628}
629
630bool
631Path::setStatusInfo(const StatusInfo& si) const {
632 if (!isFile()) return false;
633
634 HANDLE h = CreateFile(path.c_str(),
635 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
636 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000637 NULL,
638 OPEN_EXISTING,
639 FILE_ATTRIBUTE_NORMAL,
640 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000641 if (h == INVALID_HANDLE_VALUE)
642 return false;
643
644 BY_HANDLE_FILE_INFORMATION bhfi;
645 if (!GetFileInformationByHandle(h, &bhfi)) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000646 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000647 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000648 SetLastError(err);
Jeff Cohend40a7de2004-12-31 05:07:26 +0000649 ThrowError(path + ": GetFileInformationByHandle: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000650 }
651
652 FILETIME ft;
653 (uint64_t&)ft = si.modTime.toWin32Time();
654 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000655 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000656 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000657 if (!ret) {
658 SetLastError(err);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000659 ThrowError(path + ": SetFileTime: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000660 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000661
662 // Best we can do with Unix permission bits is to interpret the owner
663 // writable bit.
664 if (si.mode & 0200) {
665 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
666 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000667 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000668 ThrowError(path + ": SetFileAttributes: ");
669 }
670 } else {
671 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
672 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000673 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000674 ThrowError(path + ": SetFileAttributes: ");
675 }
676 }
677
678 return true;
679}
680
Reid Spencerc29befb2004-12-15 01:50:13 +0000681void
Reid Spencera36c9a42004-12-23 22:14:32 +0000682sys::CopyFile(const sys::Path &Dest, const sys::Path &Src) {
Jeff Cohencb652552004-12-24 02:38:34 +0000683 // Can't use CopyFile macro defined in Windows.h because it would mess up the
684 // above line. We use the expansion it would have in a non-UNICODE build.
685 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Reid Spencerc29befb2004-12-15 01:50:13 +0000686 ThrowError("Can't copy '" + Src.toString() +
Jeff Cohend40a7de2004-12-31 05:07:26 +0000687 "' to '" + Dest.toString() + "': ");
Reid Spencerc29befb2004-12-15 01:50:13 +0000688}
689
690void
Jeff Cohencb652552004-12-24 02:38:34 +0000691Path::makeUnique(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000692 if (reuse_current && !exists())
Reid Spencerc29befb2004-12-15 01:50:13 +0000693 return; // File doesn't exist already, just use it!
694
Jeff Cohen9437bb62005-01-27 03:49:03 +0000695 // Reserve space for -XXXXXX at the end.
696 char *FNBuffer = (char*) alloca(path.size()+8);
697 unsigned offset = path.size();
698 path.copy(FNBuffer, offset);
Reid Spencerc29befb2004-12-15 01:50:13 +0000699
Jeff Cohen9437bb62005-01-27 03:49:03 +0000700 // Find a numeric suffix that isn't used by an existing file.
701 static unsigned FCounter = 0;
702 do {
703 sprintf(FNBuffer+offset, "-%06u", FCounter);
704 if (++FCounter > 999999)
705 FCounter = 0;
706 path = FNBuffer;
707 } while (exists());
Reid Spencerc29befb2004-12-15 01:50:13 +0000708}
709
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000710bool
711Path::createTemporaryFile(bool reuse_current) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000712 // Make this into a unique file name
713 makeUnique( reuse_current );
Jeff Cohen9437bb62005-01-27 03:49:03 +0000714
715 // Now go and create it
716 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
717 FILE_ATTRIBUTE_NORMAL, NULL);
718 if (h == INVALID_HANDLE_VALUE)
719 return false;
720
721 CloseHandle(h);
Jeff Cohenf8cdb852004-12-18 06:42:15 +0000722 return true;
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000723}
724
Reid Spencerb016a372004-09-15 05:49:50 +0000725}
Reid Spencercbad7012004-09-11 04:59:30 +0000726}
727
Reid Spencerb016a372004-09-15 05:49:50 +0000728