blob: 070ebb312085ea80d7eb1db75725ce94e0f681cd [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 Spencer6a0ec6f2004-09-29 00:01:17 +0000121std::string
122Path::GetDLLSuffix() {
123 return "dll";
124}
125
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000126static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
127 const char* at = path;
128 const char* delim = strchr(at, ';');
129 Path tmpPath;
130 while( delim != 0 ) {
131 std::string tmp(at, size_t(delim-at));
132 if (tmpPath.setDirectory(tmp))
133 if (tmpPath.readable())
134 Paths.push_back(tmpPath);
135 at = delim + 1;
136 delim = strchr(at, ';');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000137 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000138 if (*at != 0)
139 if (tmpPath.setDirectory(std::string(at)))
140 if (tmpPath.readable())
141 Paths.push_back(tmpPath);
142
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000143}
144
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000145void
146Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
147#ifdef LTDL_SHLIBPATH_VAR
148 char* env_var = getenv(LTDL_SHLIBPATH_VAR);
149 if (env_var != 0) {
150 getPathList(env_var,Paths);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000151 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000152#endif
153 // FIXME: Should this look at LD_LIBRARY_PATH too?
154 Paths.push_back(sys::Path("C:\\WINDOWS\\SYSTEM32\\"));
155 Paths.push_back(sys::Path("C:\\WINDOWS\\"));
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000156}
157
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000158void
159Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
160 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
161 if (env_var != 0) {
162 getPathList(env_var,Paths);
163 }
164#ifdef LLVMGCCDIR
165 {
166 Path tmpPath(std::string(LLVMGCCDIR) + "bytecode-libs/");
167 if (tmpPath.readable())
168 Paths.push_back(tmpPath);
169 }
170#endif
171#ifdef LLVM_LIBDIR
172 {
173 Path tmpPath;
174 if (tmpPath.setDirectory(LLVM_LIBDIR))
175 if (tmpPath.readable())
176 Paths.push_back(tmpPath);
177 }
178#endif
179 GetSystemLibraryPaths(Paths);
Reid Spencerb016a372004-09-15 05:49:50 +0000180}
181
182Path
183Path::GetLLVMDefaultConfigDir() {
184 return Path("/etc/llvm/");
185}
186
187Path
188Path::GetLLVMConfigDir() {
Reid Spencerb016a372004-09-15 05:49:50 +0000189 return GetLLVMDefaultConfigDir();
190}
191
192Path
193Path::GetUserHomeDirectory() {
194 const char* home = getenv("HOME");
195 if (home) {
196 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +0000197 if (result.setDirectory(home))
Reid Spencerb016a372004-09-15 05:49:50 +0000198 return result;
199 }
200 return GetRootDirectory();
201}
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000202// FIXME: the above set of functions don't map to Windows very well.
203
204bool
Reid Spencer07adb282004-11-05 22:15:36 +0000205Path::isFile() const {
206 return (isValid() && path[path.length()-1] != '/');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000207}
208
209bool
Reid Spencer07adb282004-11-05 22:15:36 +0000210Path::isDirectory() const {
211 return (isValid() && path[path.length()-1] == '/');
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000212}
213
214std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000215Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000216 // Find the last slash
217 size_t slash = path.rfind('/');
218 if (slash == std::string::npos)
219 slash = 0;
220 else
221 slash++;
222
223 return path.substr(slash, path.rfind('.'));
224}
225
Reid Spencer07adb282004-11-05 22:15:36 +0000226bool Path::hasMagicNumber(const std::string &Magic) const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000227 size_t len = Magic.size();
228 char *buf = reinterpret_cast<char *>(_alloca(len+1));
229 std::ifstream f(path.c_str());
230 f.read(buf, len);
231 buf[len] = '\0';
232 return Magic == buf;
233}
234
235bool
Reid Spencer07adb282004-11-05 22:15:36 +0000236Path::isBytecodeFile() const {
Reid Spencer4b826812004-11-09 20:27:23 +0000237 char buffer[ 4];
238 buffer[0] = 0;
239 std::ifstream f(path.c_str());
240 f.read(buffer, 4);
241 if (f.bad())
242 ThrowErrno("can't read file signature");
243 return 0 == memcmp(buffer,"llvc",4) || 0 == memcmp(buffer,"llvm",4);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000244}
245
246bool
Reid Spencerb016a372004-09-15 05:49:50 +0000247Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000248 DWORD attr = GetFileAttributes(path.c_str());
249 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000250}
251
252bool
253Path::readable() 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;
Reid Spencerb016a372004-09-15 05:49:50 +0000257}
258
259bool
260Path::writable() 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) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000264}
265
266bool
267Path::executable() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000268 // FIXME: take security attributes into account.
269 DWORD attr = GetFileAttributes(path.c_str());
270 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000271}
272
273std::string
274Path::getLast() const {
275 // Find the last slash
276 size_t pos = path.rfind('/');
277
278 // Handle the corner cases
279 if (pos == std::string::npos)
280 return path;
281
282 // If the last character is a slash
283 if (pos == path.length()-1) {
284 // Find the second to last slash
285 size_t pos2 = path.rfind('/', pos-1);
286 if (pos2 == std::string::npos)
287 return path.substr(0,pos);
288 else
289 return path.substr(pos2+1,pos-pos2-1);
290 }
291 // Return everything after the last slash
292 return path.substr(pos+1);
293}
294
295bool
Reid Spencer07adb282004-11-05 22:15:36 +0000296Path::setDirectory(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000297 if (a_path.size() == 0)
298 return false;
299 Path save(*this);
300 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000301 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000302 size_t last = a_path.size() -1;
303 if (last != 0 && a_path[last] != '/')
304 path += '/';
Reid Spencer07adb282004-11-05 22:15:36 +0000305 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000306 path = save.path;
307 return false;
308 }
309 return true;
310}
311
312bool
Reid Spencer07adb282004-11-05 22:15:36 +0000313Path::setFile(const std::string& a_path) {
Reid Spencerb016a372004-09-15 05:49:50 +0000314 if (a_path.size() == 0)
315 return false;
316 Path save(*this);
317 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000318 FlipBackSlashes(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000319 size_t last = a_path.size() - 1;
320 while (last > 0 && a_path[last] == '/')
321 last--;
322 path.erase(last+1);
Reid Spencer07adb282004-11-05 22:15:36 +0000323 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000324 path = save.path;
325 return false;
326 }
327 return true;
328}
329
330bool
Reid Spencer07adb282004-11-05 22:15:36 +0000331Path::appendDirectory(const std::string& dir) {
332 if (isFile())
Reid Spencerb016a372004-09-15 05:49:50 +0000333 return false;
334 Path save(*this);
335 path += dir;
336 path += "/";
Reid Spencer07adb282004-11-05 22:15:36 +0000337 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000338 path = save.path;
339 return false;
340 }
341 return true;
342}
343
344bool
Reid Spencer07adb282004-11-05 22:15:36 +0000345Path::elideDirectory() {
346 if (isFile())
Reid Spencerb016a372004-09-15 05:49:50 +0000347 return false;
348 size_t slashpos = path.rfind('/',path.size());
349 if (slashpos == 0 || slashpos == std::string::npos)
350 return false;
351 if (slashpos == path.size() - 1)
352 slashpos = path.rfind('/',slashpos-1);
353 if (slashpos == std::string::npos)
354 return false;
355 path.erase(slashpos);
356 return true;
357}
358
359bool
Reid Spencer07adb282004-11-05 22:15:36 +0000360Path::appendFile(const std::string& file) {
361 if (!isDirectory())
Reid Spencerb016a372004-09-15 05:49:50 +0000362 return false;
363 Path save(*this);
364 path += file;
Reid Spencer07adb282004-11-05 22:15:36 +0000365 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000366 path = save.path;
367 return false;
368 }
369 return true;
370}
371
372bool
Reid Spencer07adb282004-11-05 22:15:36 +0000373Path::elideFile() {
374 if (isDirectory())
Reid Spencerb016a372004-09-15 05:49:50 +0000375 return false;
376 size_t slashpos = path.rfind('/',path.size());
377 if (slashpos == std::string::npos)
378 return false;
379 path.erase(slashpos+1);
380 return true;
381}
382
383bool
Reid Spencer07adb282004-11-05 22:15:36 +0000384Path::appendSuffix(const std::string& suffix) {
385 if (isDirectory())
Reid Spencerb016a372004-09-15 05:49:50 +0000386 return false;
387 Path save(*this);
388 path.append(".");
389 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000390 if (!isValid()) {
Reid Spencerb016a372004-09-15 05:49:50 +0000391 path = save.path;
392 return false;
393 }
394 return true;
395}
396
397bool
Reid Spencer07adb282004-11-05 22:15:36 +0000398Path::elideSuffix() {
399 if (isDirectory()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000400 size_t dotpos = path.rfind('.',path.size());
401 size_t slashpos = path.rfind('/',path.size());
402 if (slashpos != std::string::npos && dotpos != std::string::npos &&
403 dotpos > slashpos) {
404 path.erase(dotpos, path.size()-dotpos);
405 return true;
406 }
407 return false;
408}
409
410
411bool
Reid Spencer07adb282004-11-05 22:15:36 +0000412Path::createDirectory( bool create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000413 // Make sure we're dealing with a directory
Reid Spencer07adb282004-11-05 22:15:36 +0000414 if (!isDirectory()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000415
416 // Get a writeable copy of the path name
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000417 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1));
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000418 path.copy(pathname,path.length());
419 pathname[path.length()] = 0;
Reid Spencerb016a372004-09-15 05:49:50 +0000420
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000421 // Determine starting point for initial / search.
422 char *next = pathname;
423 if (pathname[0] == '/' && pathname[1] == '/') {
424 // Skip host name.
425 next = strchr(pathname+2, '/');
426 if (next == NULL)
427 throw std::string(pathname) + ": badly formed remote directory";
428 // Skip share name.
429 next = strchr(next+1, '/');
430 if (next == NULL)
431 throw std::string(pathname) + ": badly formed remote directory";
432 next++;
433 if (*next == 0)
434 throw std::string(pathname) + ": badly formed remote directory";
435 } else {
436 if (pathname[1] == ':')
437 next += 2; // skip drive letter
438 if (*next == '/')
439 next++; // skip root directory
440 }
Reid Spencerb016a372004-09-15 05:49:50 +0000441
442 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000443 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000444 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000445 while (*next) {
446 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000447 *next = 0;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000448 if (!CreateDirectory(pathname, NULL))
449 ThrowError(std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000450 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000451 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000452 } else {
453 // Drop trailing slash.
454 pathname[path.size()-1] = 0;
455 if (!CreateDirectory(pathname, NULL)) {
456 ThrowError(std::string(pathname) + ": Can't create directory: ");
457 }
Reid Spencerb016a372004-09-15 05:49:50 +0000458 }
459 return true;
460}
461
462bool
Reid Spencer07adb282004-11-05 22:15:36 +0000463Path::createFile() {
Reid Spencerb016a372004-09-15 05:49:50 +0000464 // Make sure we're dealing with a file
Reid Spencer07adb282004-11-05 22:15:36 +0000465 if (!isFile()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000466
467 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000468 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000469 FILE_ATTRIBUTE_NORMAL, NULL);
470 if (h == INVALID_HANDLE_VALUE)
471 ThrowError(std::string(path.c_str()) + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000472
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000473 CloseHandle(h);
Reid Spencerb016a372004-09-15 05:49:50 +0000474 return true;
475}
476
477bool
Reid Spencer07adb282004-11-05 22:15:36 +0000478Path::destroyDirectory(bool remove_contents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000479 // Make sure we're dealing with a directory
Reid Spencer07adb282004-11-05 22:15:36 +0000480 if (!isDirectory()) return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000481
482 // If it doesn't exist, we're done.
483 if (!exists()) return true;
484
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000485 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1));
486 path.copy(pathname,path.length()+1);
487 int lastchar = path.length() - 1 ;
488 if (pathname[lastchar] == '/')
489 pathname[lastchar] = 0;
490
Reid Spencerb016a372004-09-15 05:49:50 +0000491 if (remove_contents) {
492 // Recursively descend the directory to remove its content
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000493 // FIXME: The correct way of doing this on Windows isn't pretty...
494 // but this may work if unix-like utils are present.
495 std::string cmd("rm -rf ");
Reid Spencerb016a372004-09-15 05:49:50 +0000496 cmd += path;
497 system(cmd.c_str());
498 } else {
499 // Otherwise, try to just remove the one directory
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000500 if (!RemoveDirectory(pathname))
501 ThrowError(std::string(pathname) + ": Can't destroy directory: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000502 }
503 return true;
504}
505
506bool
Reid Spencer07adb282004-11-05 22:15:36 +0000507Path::destroyFile() {
508 if (!isFile()) return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000509
510 DWORD attr = GetFileAttributes(path.c_str());
511
512 // If it doesn't exist, we're done.
513 if (attr == INVALID_FILE_ATTRIBUTES)
514 return true;
515
516 // Read-only files cannot be deleted on Windows. Must remove the read-only
517 // attribute first.
518 if (attr & FILE_ATTRIBUTE_READONLY) {
519 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
520 ThrowError(std::string(path.c_str()) + ": Can't destroy file: ");
521 }
522
523 if (!DeleteFile(path.c_str()))
524 ThrowError(std::string(path.c_str()) + ": Can't destroy file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000525 return true;
526}
527
528}
Reid Spencercbad7012004-09-11 04:59:30 +0000529}
530
531// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
Reid Spencerb016a372004-09-15 05:49:50 +0000532