blob: e847154e583623226ba8ccdb1a49ef677724f61b [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
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000026static void FlipBackSlashes(std::string& s) {
27 for (size_t i = 0; i < s.size(); i++)
28 if (s[i] == '\\')
29 s[i] = '/';
30}
31
Reid Spencercbad7012004-09-11 04:59:30 +000032namespace llvm {
Reid Spencerb016a372004-09-15 05:49:50 +000033namespace sys {
Reid Spencercbad7012004-09-11 04:59:30 +000034
Reid Spencerb016a372004-09-15 05:49:50 +000035bool
Reid Spencer07adb282004-11-05 22:15:36 +000036Path::isValid() const {
Reid Spencerb016a372004-09-15 05:49:50 +000037 if (path.empty())
38 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000039
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000040 // If there is a colon, it must be the second character, preceded by a letter
41 // and followed by something.
42 size_t len = path.size();
43 size_t pos = path.rfind(':',len);
44 if (pos != std::string::npos) {
45 if (pos != 1 || !isalpha(path[0]) || len < 3)
46 return false;
47 }
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000048
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000049 // Check for illegal characters.
50 if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012"
51 "\013\014\015\016\017\020\021\022\023\024\025\026"
52 "\027\030\031\032\033\034\035\036\037")
53 != std::string::npos)
54 return false;
55
56 // A file or directory name may not end in a period.
57 if (path[len-1] == '.')
58 return false;
59 if (len >= 2 && path[len-2] == '.' && path[len-1] == '/')
60 return false;
61
62 // A file or directory name may not end in a space.
63 if (path[len-1] == ' ')
64 return false;
65 if (len >= 2 && path[len-2] == ' ' && path[len-1] == '/')
66 return false;
67
68 return true;
Reid Spencercbad7012004-09-11 04:59:30 +000069}
70
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000071static Path *TempDirectory = NULL;
72
Reid Spencerb016a372004-09-15 05:49:50 +000073Path
74Path::GetTemporaryDirectory() {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000075 if (TempDirectory)
76 return *TempDirectory;
77
78 char pathname[MAX_PATH];
79 if (!GetTempPath(MAX_PATH, pathname))
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000080 throw std::string("Can't determine temporary directory");
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000081
Reid Spencerb016a372004-09-15 05:49:50 +000082 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +000083 result.setDirectory(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000084
85 // Append a subdirectory passed on our process id so multiple LLVMs don't
86 // step on each other's toes.
87 sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
Reid Spencer07adb282004-11-05 22:15:36 +000088 result.appendDirectory(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000089
90 // If there's a directory left over from a previous LLVM execution that
91 // happened to have the same process id, get rid of it.
Reid Spencer07adb282004-11-05 22:15:36 +000092 result.destroyDirectory(true);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000093
94 // And finally (re-)create the empty directory.
Reid Spencer07adb282004-11-05 22:15:36 +000095 result.createDirectory(false);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000096 TempDirectory = new Path(result);
97 return *TempDirectory;
Reid Spencerb016a372004-09-15 05:49:50 +000098}
99
Reid Spencer732f92d2004-12-13 06:57:15 +0000100Path::Path(const std::string& unverified_path)
Reid Spencerb016a372004-09-15 05:49:50 +0000101 : path(unverified_path)
102{
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000103 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000104 if (unverified_path.empty())
105 return;
Reid Spencer07adb282004-11-05 22:15:36 +0000106 if (this->isValid())
Reid Spencerb016a372004-09-15 05:49:50 +0000107 return;
108 // oops, not valid.
109 path.clear();
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000110 throw std::string(unverified_path + ": path is not valid");
Reid Spencerb016a372004-09-15 05:49:50 +0000111}
112
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000113// FIXME: the following set of functions don't map to Windows very well.
Reid Spencerb016a372004-09-15 05:49:50 +0000114Path
115Path::GetRootDirectory() {
116 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +0000117 result.setDirectory("/");
Reid Spencerb016a372004-09-15 05:49:50 +0000118 return result;
119}
120
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000121static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
122 const char* at = path;
123 const char* delim = strchr(at, ';');
124 Path tmpPath;
125 while( delim != 0 ) {
126 std::string tmp(at, size_t(delim-at));
127 if (tmpPath.setDirectory(tmp))
128 if (tmpPath.readable())
129 Paths.push_back(tmpPath);
130 at = delim + 1;
131 delim = strchr(at, ';');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000132 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000133 if (*at != 0)
134 if (tmpPath.setDirectory(std::string(at)))
135 if (tmpPath.readable())
136 Paths.push_back(tmpPath);
137
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000138}
139
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000140void
141Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
142#ifdef LTDL_SHLIBPATH_VAR
143 char* env_var = getenv(LTDL_SHLIBPATH_VAR);
144 if (env_var != 0) {
145 getPathList(env_var,Paths);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000146 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000147#endif
148 // FIXME: Should this look at LD_LIBRARY_PATH too?
149 Paths.push_back(sys::Path("C:\\WINDOWS\\SYSTEM32\\"));
150 Paths.push_back(sys::Path("C:\\WINDOWS\\"));
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000151}
152
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000153void
154Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
155 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
156 if (env_var != 0) {
157 getPathList(env_var,Paths);
158 }
159#ifdef LLVMGCCDIR
160 {
161 Path tmpPath(std::string(LLVMGCCDIR) + "bytecode-libs/");
162 if (tmpPath.readable())
163 Paths.push_back(tmpPath);
164 }
165#endif
166#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() {
179 return Path("/etc/llvm/");
180}
181
182Path
183Path::GetLLVMConfigDir() {
Reid Spencerb016a372004-09-15 05:49:50 +0000184 return GetLLVMDefaultConfigDir();
185}
186
187Path
188Path::GetUserHomeDirectory() {
189 const char* home = getenv("HOME");
190 if (home) {
191 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +0000192 if (result.setDirectory(home))
Reid Spencerb016a372004-09-15 05:49:50 +0000193 return result;
194 }
195 return GetRootDirectory();
196}
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000197// FIXME: the above set of functions don't map to Windows very well.
198
199bool
Reid Spencer07adb282004-11-05 22:15:36 +0000200Path::isFile() const {
201 return (isValid() && path[path.length()-1] != '/');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000202}
203
204bool
Reid Spencer07adb282004-11-05 22:15:36 +0000205Path::isDirectory() const {
206 return (isValid() && path[path.length()-1] == '/');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000207}
208
209std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000210Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000211 // Find the last slash
212 size_t slash = path.rfind('/');
213 if (slash == std::string::npos)
214 slash = 0;
215 else
216 slash++;
217
218 return path.substr(slash, path.rfind('.'));
219}
220
Reid Spencer07adb282004-11-05 22:15:36 +0000221bool Path::hasMagicNumber(const std::string &Magic) const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000222 size_t len = Magic.size();
223 char *buf = reinterpret_cast<char *>(_alloca(len+1));
224 std::ifstream f(path.c_str());
225 f.read(buf, len);
226 buf[len] = '\0';
227 return Magic == buf;
228}
229
230bool
Reid Spencer07adb282004-11-05 22:15:36 +0000231Path::isBytecodeFile() const {
Reid Spencer4b826812004-11-09 20:27:23 +0000232 char buffer[ 4];
233 buffer[0] = 0;
234 std::ifstream f(path.c_str());
235 f.read(buffer, 4);
236 if (f.bad())
237 ThrowErrno("can't read file signature");
238 return 0 == memcmp(buffer,"llvc",4) || 0 == memcmp(buffer,"llvm",4);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000239}
240
241bool
Reid Spencerb016a372004-09-15 05:49:50 +0000242Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000243 DWORD attr = GetFileAttributes(path.c_str());
244 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000245}
246
247bool
248Path::readable() 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
254bool
255Path::writable() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000256 // FIXME: take security attributes into account.
257 DWORD attr = GetFileAttributes(path.c_str());
258 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000259}
260
261bool
262Path::executable() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000263 // FIXME: take security attributes into account.
264 DWORD attr = GetFileAttributes(path.c_str());
265 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000266}
267
268std::string
269Path::getLast() const {
270 // Find the last slash
271 size_t pos = path.rfind('/');
272
273 // Handle the corner cases
274 if (pos == std::string::npos)
275 return path;
276
277 // If the last character is a slash
278 if (pos == path.length()-1) {
279 // Find the second to last slash
280 size_t pos2 = path.rfind('/', pos-1);
281 if (pos2 == std::string::npos)
282 return path.substr(0,pos);
283 else
284 return path.substr(pos2+1,pos-pos2-1);
285 }
286 // Return everything after the last slash
287 return path.substr(pos+1);
288}
289
290bool
Reid Spencer07adb282004-11-05 22:15:36 +0000291Path::setDirectory(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000292 if (a_path.size() == 0)
293 return false;
294 Path save(*this);
295 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000296 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000297 size_t last = a_path.size() -1;
Reid Spencerb0e18872004-12-13 07:51:52 +0000298 if (a_path[last] != '/')
Reid Spencerb016a372004-09-15 05:49:50 +0000299 path += '/';
Reid Spencer07adb282004-11-05 22:15:36 +0000300 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000301 path = save.path;
302 return false;
303 }
304 return true;
305}
306
307bool
Reid Spencer07adb282004-11-05 22:15:36 +0000308Path::setFile(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000309 if (a_path.size() == 0)
310 return false;
311 Path save(*this);
312 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000313 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000314 size_t last = a_path.size() - 1;
315 while (last > 0 && a_path[last] == '/')
316 last--;
317 path.erase(last+1);
Reid Spencer07adb282004-11-05 22:15:36 +0000318 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000319 path = save.path;
320 return false;
321 }
322 return true;
323}
324
325bool
Reid Spencer07adb282004-11-05 22:15:36 +0000326Path::appendDirectory(const std::string& dir) {
327 if (isFile())
Reid Spencerb016a372004-09-15 05:49:50 +0000328 return false;
329 Path save(*this);
330 path += dir;
331 path += "/";
Reid Spencer07adb282004-11-05 22:15:36 +0000332 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000333 path = save.path;
334 return false;
335 }
336 return true;
337}
338
339bool
Reid Spencer07adb282004-11-05 22:15:36 +0000340Path::elideDirectory() {
341 if (isFile())
Reid Spencerb016a372004-09-15 05:49:50 +0000342 return false;
343 size_t slashpos = path.rfind('/',path.size());
344 if (slashpos == 0 || slashpos == std::string::npos)
345 return false;
346 if (slashpos == path.size() - 1)
347 slashpos = path.rfind('/',slashpos-1);
348 if (slashpos == std::string::npos)
349 return false;
350 path.erase(slashpos);
351 return true;
352}
353
354bool
Reid Spencer07adb282004-11-05 22:15:36 +0000355Path::appendFile(const std::string& file) {
356 if (!isDirectory())
Reid Spencerb016a372004-09-15 05:49:50 +0000357 return false;
358 Path save(*this);
359 path += file;
Reid Spencer07adb282004-11-05 22:15:36 +0000360 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000361 path = save.path;
362 return false;
363 }
364 return true;
365}
366
367bool
Reid Spencer07adb282004-11-05 22:15:36 +0000368Path::elideFile() {
369 if (isDirectory())
Reid Spencerb016a372004-09-15 05:49:50 +0000370 return false;
371 size_t slashpos = path.rfind('/',path.size());
372 if (slashpos == std::string::npos)
373 return false;
374 path.erase(slashpos+1);
375 return true;
376}
377
378bool
Reid Spencer07adb282004-11-05 22:15:36 +0000379Path::appendSuffix(const std::string& suffix) {
380 if (isDirectory())
Reid Spencerb016a372004-09-15 05:49:50 +0000381 return false;
382 Path save(*this);
383 path.append(".");
384 path.append(suffix);
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::elideSuffix() {
394 if (isDirectory()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000395 size_t dotpos = path.rfind('.',path.size());
396 size_t slashpos = path.rfind('/',path.size());
397 if (slashpos != std::string::npos && dotpos != std::string::npos &&
398 dotpos > slashpos) {
399 path.erase(dotpos, path.size()-dotpos);
400 return true;
401 }
402 return false;
403}
404
405
406bool
Reid Spencer07adb282004-11-05 22:15:36 +0000407Path::createDirectory( bool create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000408 // Make sure we're dealing with a directory
Reid Spencer07adb282004-11-05 22:15:36 +0000409 if (!isDirectory()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000410
411 // Get a writeable copy of the path name
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000412 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1));
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000413 path.copy(pathname,path.length());
414 pathname[path.length()] = 0;
Reid Spencerb016a372004-09-15 05:49:50 +0000415
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000416 // Determine starting point for initial / search.
417 char *next = pathname;
418 if (pathname[0] == '/' && pathname[1] == '/') {
419 // Skip host name.
420 next = strchr(pathname+2, '/');
421 if (next == NULL)
422 throw std::string(pathname) + ": badly formed remote directory";
423 // Skip share name.
424 next = strchr(next+1, '/');
425 if (next == NULL)
426 throw std::string(pathname) + ": badly formed remote directory";
427 next++;
428 if (*next == 0)
429 throw std::string(pathname) + ": badly formed remote directory";
430 } else {
431 if (pathname[1] == ':')
432 next += 2; // skip drive letter
433 if (*next == '/')
434 next++; // skip root directory
435 }
Reid Spencerb016a372004-09-15 05:49:50 +0000436
437 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000438 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000439 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000440 while (*next) {
441 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000442 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000443 if (!CreateDirectory(pathname, NULL))
444 ThrowError(std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000445 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000446 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000447 } else {
448 // Drop trailing slash.
449 pathname[path.size()-1] = 0;
450 if (!CreateDirectory(pathname, NULL)) {
451 ThrowError(std::string(pathname) + ": Can't create directory: ");
452 }
Reid Spencerb016a372004-09-15 05:49:50 +0000453 }
454 return true;
455}
456
457bool
Reid Spencer07adb282004-11-05 22:15:36 +0000458Path::createFile() {
Reid Spencerb016a372004-09-15 05:49:50 +0000459 // Make sure we're dealing with a file
Reid Spencer07adb282004-11-05 22:15:36 +0000460 if (!isFile()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000461
462 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000463 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000464 FILE_ATTRIBUTE_NORMAL, NULL);
465 if (h == INVALID_HANDLE_VALUE)
466 ThrowError(std::string(path.c_str()) + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000467
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000468 CloseHandle(h);
Reid Spencerb016a372004-09-15 05:49:50 +0000469 return true;
470}
471
472bool
Reid Spencer07adb282004-11-05 22:15:36 +0000473Path::destroyDirectory(bool remove_contents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000474 // Make sure we're dealing with a directory
Reid Spencer07adb282004-11-05 22:15:36 +0000475 if (!isDirectory()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000476
477 // If it doesn't exist, we're done.
478 if (!exists()) return true;
479
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000480 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1));
481 path.copy(pathname,path.length()+1);
482 int lastchar = path.length() - 1 ;
483 if (pathname[lastchar] == '/')
484 pathname[lastchar] = 0;
485
Reid Spencerb016a372004-09-15 05:49:50 +0000486 if (remove_contents) {
487 // Recursively descend the directory to remove its content
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000488 // FIXME: The correct way of doing this on Windows isn't pretty...
489 // but this may work if unix-like utils are present.
490 std::string cmd("rm -rf ");
Reid Spencerb016a372004-09-15 05:49:50 +0000491 cmd += path;
492 system(cmd.c_str());
493 } else {
494 // Otherwise, try to just remove the one directory
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000495 if (!RemoveDirectory(pathname))
496 ThrowError(std::string(pathname) + ": Can't destroy directory: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000497 }
498 return true;
499}
500
501bool
Reid Spencer07adb282004-11-05 22:15:36 +0000502Path::destroyFile() {
503 if (!isFile()) return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000504
505 DWORD attr = GetFileAttributes(path.c_str());
506
507 // If it doesn't exist, we're done.
508 if (attr == INVALID_FILE_ATTRIBUTES)
509 return true;
510
511 // Read-only files cannot be deleted on Windows. Must remove the read-only
512 // attribute first.
513 if (attr & FILE_ATTRIBUTE_READONLY) {
514 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
515 ThrowError(std::string(path.c_str()) + ": Can't destroy file: ");
516 }
517
518 if (!DeleteFile(path.c_str()))
519 ThrowError(std::string(path.c_str()) + ": Can't destroy file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000520 return true;
521}
522
523}
Reid Spencercbad7012004-09-11 04:59:30 +0000524}
525
526// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
Reid Spencerb016a372004-09-15 05:49:50 +0000527