blob: 9187c8279381bb0df82b716c3a4098051f3a8ec4 [file] [log] [blame]
Argyrios Kyrtzidis4bd32252008-06-16 10:14:09 +00001//===- llvm/System/Win32/Path.cpp - Win32 Path Implementation ---*- C++ -*-===//
Reid Spencerb016a372004-09-15 05:49:50 +00002//
Reid Spencercbad7012004-09-11 04:59:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencerb016a372004-09-15 05:49:50 +00007//
Reid Spencercbad7012004-09-11 04:59:30 +00008//===----------------------------------------------------------------------===//
9//
10// This file provides the Win32 specific implementation of the Path class.
11//
12//===----------------------------------------------------------------------===//
13
14//===----------------------------------------------------------------------===//
Reid Spencerb016a372004-09-15 05:49:50 +000015//=== WARNING: Implementation here must contain only generic Win32 code that
16//=== is guaranteed to work on *all* Win32 variants.
Reid Spencercbad7012004-09-11 04:59:30 +000017//===----------------------------------------------------------------------===//
18
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000019#include "Win32.h"
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000020#include <malloc.h>
Chris Lattnerc23c1fc2009-04-01 02:03:38 +000021#include <cstdio>
Reid Spencercbad7012004-09-11 04:59:30 +000022
Jeff Cohencb652552004-12-24 02:38:34 +000023// We need to undo a macro defined in Windows.h, otherwise we won't compile:
24#undef CopyFile
Anton Korobeynikov64ddbe42007-12-22 14:26:49 +000025#undef GetCurrentDirectory
Jeff Cohencb652552004-12-24 02:38:34 +000026
Jeff Cohen966fa412005-07-09 18:42:49 +000027// Windows happily accepts either forward or backward slashes, though any path
28// returned by a Win32 API will have backward slashes. As LLVM code basically
29// assumes forward slashes are used, backward slashs are converted where they
30// can be introduced into a path.
31//
32// Another invariant is that a path ends with a slash if and only if the path
33// is a root directory. Any other use of a trailing slash is stripped. Unlike
34// in Unix, Windows has a rather complicated notion of a root path and this
35// invariant helps simply the code.
36
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000037static void FlipBackSlashes(std::string& s) {
38 for (size_t i = 0; i < s.size(); i++)
39 if (s[i] == '\\')
40 s[i] = '/';
41}
42
Reid Spencercbad7012004-09-11 04:59:30 +000043namespace llvm {
Reid Spencerb016a372004-09-15 05:49:50 +000044namespace sys {
Mikhail Glushenkovf5a95ce2010-11-02 20:32:26 +000045
Chris Lattnera17fa282008-03-13 05:17:59 +000046const char PathSeparator = ';';
Chris Lattnere1b332a2008-02-27 06:17:10 +000047
Mikhail Glushenkovf5a95ce2010-11-02 20:32:26 +000048StringRef Path::GetEXESuffix() {
49 return "exe";
50}
51
Daniel Dunbar1edcafe2009-12-18 19:59:48 +000052Path::Path(llvm::StringRef p)
Nick Lewyckyfff116f2008-05-11 17:37:40 +000053 : path(p) {
54 FlipBackSlashes(path);
55}
56
57Path::Path(const char *StrStart, unsigned StrLen)
58 : path(StrStart, StrLen) {
59 FlipBackSlashes(path);
60}
61
Chris Lattner0eab5e22008-08-11 23:39:47 +000062Path&
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +000063Path::operator=(StringRef that) {
64 path.assign(that.data(), that.size());
Chris Lattner0eab5e22008-08-11 23:39:47 +000065 FlipBackSlashes(path);
66 return *this;
67}
68
Michael J. Spencera9cf7b82010-10-20 15:23:58 +000069// push_back 0 on create, and pop_back on delete.
70struct ScopedNullTerminator {
71 std::string &str;
72 ScopedNullTerminator(std::string &s) : str(s) { str.push_back(0); }
Michael J. Spencer9f608b12010-10-20 16:00:45 +000073 ~ScopedNullTerminator() {
74 // str.pop_back(); But wait, C++03 doesn't have this...
75 assert(!str.empty() && str[str.size() - 1] == 0
76 && "Null char not present!");
77 str.resize(str.size() - 1);
78 }
Michael J. Spencera9cf7b82010-10-20 15:23:58 +000079};
80
Reid Spencerb016a372004-09-15 05:49:50 +000081bool
Reid Spencer07adb282004-11-05 22:15:36 +000082Path::isValid() const {
Reid Spencerb016a372004-09-15 05:49:50 +000083 if (path.empty())
84 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000085
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000086 // If there is a colon, it must be the second character, preceded by a letter
87 // and followed by something.
88 size_t len = path.size();
Michael J. Spencera9cf7b82010-10-20 15:23:58 +000089 // This code assumes that path is null terminated, so make sure it is.
90 ScopedNullTerminator snt(path);
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000091 size_t pos = path.rfind(':',len);
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000092 size_t rootslash = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000093 if (pos != std::string::npos) {
94 if (pos != 1 || !isalpha(path[0]) || len < 3)
95 return false;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000096 rootslash = 2;
97 }
Jeff Cohen85c716f2005-07-08 05:02:13 +000098
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000099 // Look for a UNC path, and if found adjust our notion of the root slash.
100 if (len > 3 && path[0] == '/' && path[1] == '/') {
101 rootslash = path.find('/', 2);
102 if (rootslash == std::string::npos)
103 rootslash = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000104 }
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000105
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000106 // Check for illegal characters.
107 if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012"
108 "\013\014\015\016\017\020\021\022\023\024\025\026"
109 "\027\030\031\032\033\034\035\036\037")
110 != std::string::npos)
111 return false;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000112
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000113 // Remove trailing slash, unless it's a root slash.
114 if (len > rootslash+1 && path[len-1] == '/')
115 path.erase(--len);
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000116
Jeff Cohen3bbbcc12005-01-14 04:09:39 +0000117 // Check each component for legality.
118 for (pos = 0; pos < len; ++pos) {
119 // A component may not end in a space.
120 if (path[pos] == ' ') {
121 if (path[pos+1] == '/' || path[pos+1] == '\0')
122 return false;
123 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000124
Jeff Cohen3bbbcc12005-01-14 04:09:39 +0000125 // A component may not end in a period.
126 if (path[pos] == '.') {
127 if (path[pos+1] == '/' || path[pos+1] == '\0') {
128 // Unless it is the pseudo-directory "."...
129 if (pos == 0 || path[pos-1] == '/' || path[pos-1] == ':')
130 return true;
131 // or "..".
132 if (pos > 0 && path[pos-1] == '.') {
133 if (pos == 1 || path[pos-2] == '/' || path[pos-2] == ':')
134 return true;
135 }
136 return false;
137 }
138 }
139 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000140
141 return true;
Reid Spencercbad7012004-09-11 04:59:30 +0000142}
143
Daniel Dunbara00e85c2009-07-12 20:23:56 +0000144void Path::makeAbsolute() {
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000145 TCHAR FullPath[MAX_PATH + 1] = {0};
Daniel Dunbara00e85c2009-07-12 20:23:56 +0000146 LPTSTR FilePart = NULL;
147
148 DWORD RetLength = ::GetFullPathNameA(path.c_str(),
149 sizeof(FullPath)/sizeof(FullPath[0]),
150 FullPath, &FilePart);
151
152 if (0 == RetLength) {
153 // FIXME: Report the error GetLastError()
Daniel Dunbar2749b3e2009-07-26 21:16:42 +0000154 assert(0 && "Unable to make absolute path!");
Daniel Dunbara00e85c2009-07-12 20:23:56 +0000155 } else if (RetLength > MAX_PATH) {
156 // FIXME: Report too small buffer (needed RetLength bytes).
Daniel Dunbar2749b3e2009-07-26 21:16:42 +0000157 assert(0 && "Unable to make absolute path!");
Daniel Dunbara00e85c2009-07-12 20:23:56 +0000158 } else {
159 path = FullPath;
160 }
161}
162
Chris Lattner88d7e402009-06-15 04:17:07 +0000163bool
164Path::isAbsolute(const char *NameStart, unsigned NameLen) {
165 assert(NameStart);
Daniel Dunbara00e85c2009-07-12 20:23:56 +0000166 // FIXME: This does not handle correctly an absolute path starting from
167 // a drive letter or in UNC format.
Chris Lattner88d7e402009-06-15 04:17:07 +0000168 switch (NameLen) {
169 case 0:
170 return false;
171 case 1:
172 case 2:
173 return NameStart[0] == '/';
174 default:
Mikhail Glushenkov0f2ec152010-11-02 20:32:31 +0000175 return
176 (NameStart[0] == '/' || (NameStart[1] == ':' && NameStart[2] == '/')) ||
177 (NameStart[0] == '\\' || (NameStart[1] == ':' && NameStart[2] == '\\'));
Chris Lattner88d7e402009-06-15 04:17:07 +0000178 }
179}
180
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000181bool
Reid Spencer69cce812007-03-29 16:43:20 +0000182Path::isAbsolute() const {
Daniel Dunbara00e85c2009-07-12 20:23:56 +0000183 // FIXME: This does not handle correctly an absolute path starting from
184 // a drive letter or in UNC format.
Jeff Cohen84892be2007-03-29 17:27:38 +0000185 switch (path.length()) {
186 case 0:
187 return false;
188 case 1:
189 case 2:
190 return path[0] == '/';
191 default:
192 return path[0] == '/' || (path[1] == ':' && path[2] == '/');
193 }
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000194}
Reid Spencer69cce812007-03-29 16:43:20 +0000195
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000196static Path *TempDirectory;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000197
Reid Spencerb016a372004-09-15 05:49:50 +0000198Path
Reid Spencercab0e432006-08-22 22:46:39 +0000199Path::GetTemporaryDirectory(std::string* ErrMsg) {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000200 if (TempDirectory)
201 return *TempDirectory;
202
203 char pathname[MAX_PATH];
Reid Spencercab0e432006-08-22 22:46:39 +0000204 if (!GetTempPath(MAX_PATH, pathname)) {
205 if (ErrMsg)
206 *ErrMsg = "Can't determine temporary directory";
207 return Path();
208 }
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000209
Reid Spencerb016a372004-09-15 05:49:50 +0000210 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000211 result.set(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000212
213 // Append a subdirectory passed on our process id so multiple LLVMs don't
214 // step on each other's toes.
Jeff Cohend41b30d2006-11-05 19:31:28 +0000215#ifdef __MINGW32__
216 // Mingw's Win32 header files are broken.
Reid Spencerab4d9b02006-06-08 18:08:43 +0000217 sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId()));
Jeff Cohend41b30d2006-11-05 19:31:28 +0000218#else
219 sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
220#endif
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000221 result.appendComponent(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000222
223 // If there's a directory left over from a previous LLVM execution that
224 // happened to have the same process id, get rid of it.
Reid Spencera229c5c2005-07-08 03:08:58 +0000225 result.eraseFromDisk(true);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000226
227 // And finally (re-)create the empty directory.
Reid Spencera229c5c2005-07-08 03:08:58 +0000228 result.createDirectoryOnDisk(false);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000229 TempDirectory = new Path(result);
230 return *TempDirectory;
Reid Spencerb016a372004-09-15 05:49:50 +0000231}
232
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000233// FIXME: the following set of functions don't map to Windows very well.
Reid Spencerb016a372004-09-15 05:49:50 +0000234Path
235Path::GetRootDirectory() {
Michael J. Spencer87e06972010-11-09 15:10:45 +0000236 // This is the only notion that that Windows has of a root directory. Nothing
237 // is here except for drives.
238 return Path("file:///");
Reid Spencerb016a372004-09-15 05:49:50 +0000239}
240
Jeff Cohen85c716f2005-07-08 05:02:13 +0000241void
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000242Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
Michael J. Spencereebe9702010-11-09 15:11:19 +0000243 char buff[MAX_PATH];
244 // Generic form of C:\Windows\System32
245 HRESULT res = SHGetFolderPathA(NULL,
246 CSIDL_FLAG_CREATE | CSIDL_SYSTEM,
247 NULL,
248 SHGFP_TYPE_CURRENT,
249 buff);
250 if (res != S_OK) {
251 assert(0 && "Failed to get system directory");
252 return;
253 }
254 Paths.push_back(sys::Path(buff));
255
256 // Reset buff.
257 buff[0] = 0;
258 // Generic form of C:\Windows
259 res = SHGetFolderPathA(NULL,
260 CSIDL_FLAG_CREATE | CSIDL_WINDOWS,
261 NULL,
262 SHGFP_TYPE_CURRENT,
263 buff);
264 if (res != S_OK) {
265 assert(0 && "Failed to get windows directory");
266 return;
267 }
268 Paths.push_back(sys::Path(buff));
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000269}
270
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000271void
Gabor Greifdb5565a2007-07-06 20:28:40 +0000272Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) {
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000273 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
274 if (env_var != 0) {
275 getPathList(env_var,Paths);
276 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000277#ifdef LLVM_LIBDIR
278 {
279 Path tmpPath;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000280 if (tmpPath.set(LLVM_LIBDIR))
Reid Spencerc7f08322005-07-07 18:21:42 +0000281 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000282 Paths.push_back(tmpPath);
283 }
284#endif
285 GetSystemLibraryPaths(Paths);
Reid Spencerb016a372004-09-15 05:49:50 +0000286}
287
288Path
289Path::GetLLVMDefaultConfigDir() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000290 // TODO: this isn't going to fly on Windows
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000291 return Path("/etc/llvm");
Reid Spencerb016a372004-09-15 05:49:50 +0000292}
293
294Path
Reid Spencerb016a372004-09-15 05:49:50 +0000295Path::GetUserHomeDirectory() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000296 // TODO: Typical Windows setup doesn't define HOME.
Reid Spencerb016a372004-09-15 05:49:50 +0000297 const char* home = getenv("HOME");
298 if (home) {
299 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000300 if (result.set(home))
Reid Spencerb016a372004-09-15 05:49:50 +0000301 return result;
302 }
303 return GetRootDirectory();
304}
Ted Kremenek79200782007-12-18 22:07:33 +0000305
306Path
307Path::GetCurrentDirectory() {
308 char pathname[MAX_PATH];
Anton Korobeynikov64ddbe42007-12-22 14:26:49 +0000309 ::GetCurrentDirectoryA(MAX_PATH,pathname);
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000310 return Path(pathname);
Ted Kremenek79200782007-12-18 22:07:33 +0000311}
312
Chris Lattner1a091442008-03-03 02:55:43 +0000313/// GetMainExecutable - Return the path to the main executable, given the
314/// value of argv[0] from program startup.
315Path Path::GetMainExecutable(const char *argv0, void *MainAddr) {
Chris Lattnerc5718d02009-06-15 05:38:04 +0000316 char pathname[MAX_PATH];
317 DWORD ret = ::GetModuleFileNameA(NULL, pathname, MAX_PATH);
318 return ret != MAX_PATH ? Path(pathname) : Path();
Chris Lattner1a091442008-03-03 02:55:43 +0000319}
320
Ted Kremenek79200782007-12-18 22:07:33 +0000321
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000322// FIXME: the above set of functions don't map to Windows very well.
323
Jeff Cohen966fa412005-07-09 18:42:49 +0000324
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000325StringRef Path::getDirname() const {
326 return getDirnameCharSep(path, "/");
Ted Kremenek9b01cc02008-04-07 22:01:32 +0000327}
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000328
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000329StringRef
Reid Spencer07adb282004-11-05 22:15:36 +0000330Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000331 // Find the last slash
332 size_t slash = path.rfind('/');
333 if (slash == std::string::npos)
334 slash = 0;
335 else
336 slash++;
337
Jeff Cohen966fa412005-07-09 18:42:49 +0000338 size_t dot = path.rfind('.');
339 if (dot == std::string::npos || dot < slash)
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000340 return StringRef(path).substr(slash);
Jeff Cohen966fa412005-07-09 18:42:49 +0000341 else
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000342 return StringRef(path).substr(slash, dot - slash);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000343}
344
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000345StringRef
Argyrios Kyrtzidisfc199882008-06-15 15:15:19 +0000346Path::getSuffix() const {
347 // Find the last slash
348 size_t slash = path.rfind('/');
349 if (slash == std::string::npos)
350 slash = 0;
351 else
352 slash++;
353
354 size_t dot = path.rfind('.');
355 if (dot == std::string::npos || dot < slash)
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000356 return StringRef("");
Argyrios Kyrtzidisfc199882008-06-15 15:15:19 +0000357 else
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000358 return StringRef(path).substr(dot + 1);
Argyrios Kyrtzidisfc199882008-06-15 15:15:19 +0000359}
360
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000361bool
Reid Spencerb016a372004-09-15 05:49:50 +0000362Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000363 DWORD attr = GetFileAttributes(path.c_str());
364 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000365}
366
367bool
Ted Kremenekfd527112007-12-18 19:46:22 +0000368Path::isDirectory() const {
369 DWORD attr = GetFileAttributes(path.c_str());
370 return (attr != INVALID_FILE_ATTRIBUTES) &&
371 (attr & FILE_ATTRIBUTE_DIRECTORY);
372}
373
374bool
Rafael Espindola9e07a142010-11-07 04:36:50 +0000375Path::isSymLink() const {
376 return false;
377}
378
379bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000380Path::canRead() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000381 // FIXME: take security attributes into account.
382 DWORD attr = GetFileAttributes(path.c_str());
383 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000384}
385
386bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000387Path::canWrite() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000388 // FIXME: take security attributes into account.
389 DWORD attr = GetFileAttributes(path.c_str());
390 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000391}
392
393bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000394Path::canExecute() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000395 // FIXME: take security attributes into account.
396 DWORD attr = GetFileAttributes(path.c_str());
397 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000398}
399
Edward O'Callaghand41e9442009-11-24 15:19:10 +0000400bool
Edward O'Callaghane49a8e42009-11-25 06:32:19 +0000401Path::isRegularFile() const {
402 if (isDirectory())
403 return false;
404 return true;
Edward O'Callaghand41e9442009-11-24 15:19:10 +0000405}
406
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000407StringRef
Reid Spencerb016a372004-09-15 05:49:50 +0000408Path::getLast() const {
409 // Find the last slash
410 size_t pos = path.rfind('/');
411
412 // Handle the corner cases
413 if (pos == std::string::npos)
414 return path;
415
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000416 // If the last character is a slash, we have a root directory
417 if (pos == path.length()-1)
418 return path;
419
Reid Spencerb016a372004-09-15 05:49:50 +0000420 // Return everything after the last slash
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000421 return StringRef(path).substr(pos+1);
Reid Spencerb016a372004-09-15 05:49:50 +0000422}
423
Reid Spencer8475ec02007-03-29 19:05:44 +0000424const FileStatus *
Reid Spencer2ae9d112007-04-07 18:52:17 +0000425PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const {
426 if (!fsIsValid || update) {
Reid Spencer69cce812007-03-29 16:43:20 +0000427 WIN32_FILE_ATTRIBUTE_DATA fi;
Reid Spencer8475ec02007-03-29 19:05:44 +0000428 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
429 MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) +
Reid Spencer69cce812007-03-29 16:43:20 +0000430 ": Can't get status: ");
Reid Spencer8475ec02007-03-29 19:05:44 +0000431 return 0;
432 }
Jeff Cohen626e38e2004-12-14 05:26:43 +0000433
Reid Spencer2ae9d112007-04-07 18:52:17 +0000434 status.fileSize = fi.nFileSizeHigh;
435 status.fileSize <<= sizeof(fi.nFileSizeHigh)*8;
436 status.fileSize += fi.nFileSizeLow;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000437
Reid Spencer2ae9d112007-04-07 18:52:17 +0000438 status.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
439 status.user = 9999; // Not applicable to Windows, so...
440 status.group = 9999; // Not applicable to Windows, so...
Jeff Cohen626e38e2004-12-14 05:26:43 +0000441
Reid Spencer4031bef2007-03-29 17:00:31 +0000442 // FIXME: this is only unique if the file is accessed by the same file path.
443 // How do we do this for C:\dir\file and ..\dir\file ? Unix has inode
444 // numbers, but the concept doesn't exist in Windows.
Reid Spencer2ae9d112007-04-07 18:52:17 +0000445 status.uniqueID = 0;
Reid Spencer4031bef2007-03-29 17:00:31 +0000446 for (unsigned i = 0; i < path.length(); ++i)
Reid Spencer2ae9d112007-04-07 18:52:17 +0000447 status.uniqueID += path[i];
Reid Spencer4031bef2007-03-29 17:00:31 +0000448
Michael J. Spencer44edb0b2010-08-28 16:39:32 +0000449 ULARGE_INTEGER ui;
450 ui.LowPart = fi.ftLastWriteTime.dwLowDateTime;
451 ui.HighPart = fi.ftLastWriteTime.dwHighDateTime;
452 status.modTime.fromWin32Time(ui.QuadPart);
Reid Spencer69cce812007-03-29 16:43:20 +0000453
Reid Spencer2ae9d112007-04-07 18:52:17 +0000454 status.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
455 fsIsValid = true;
Reid Spencer69cce812007-03-29 16:43:20 +0000456 }
Reid Spencer2ae9d112007-04-07 18:52:17 +0000457 return &status;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000458}
459
Reid Spencera34a1572006-08-22 23:54:35 +0000460bool Path::makeReadableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000461 // All files are readable on Windows (ignoring security attributes).
Reid Spencera34a1572006-08-22 23:54:35 +0000462 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000463}
464
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000465bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000466 DWORD attr = GetFileAttributes(path.c_str());
467
468 // If it doesn't exist, we're done.
469 if (attr == INVALID_FILE_ATTRIBUTES)
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000470 return false;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000471
472 if (attr & FILE_ATTRIBUTE_READONLY) {
Reid Spencera34a1572006-08-22 23:54:35 +0000473 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) {
474 MakeErrMsg(ErrMsg, std::string(path) + ": Can't make file writable: ");
475 return true;
476 }
Jeff Cohen626e38e2004-12-14 05:26:43 +0000477 }
Reid Spencera34a1572006-08-22 23:54:35 +0000478 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000479}
480
Reid Spencera34a1572006-08-22 23:54:35 +0000481bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000482 // All files are executable on Windows (ignoring security attributes).
Reid Spencera34a1572006-08-22 23:54:35 +0000483 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000484}
485
Reid Spencerb016a372004-09-15 05:49:50 +0000486bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000487Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
Jeff Cohen31102892007-04-07 20:47:27 +0000488 WIN32_FILE_ATTRIBUTE_DATA fi;
489 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
490 MakeErrMsg(ErrMsg, path + ": can't get status of file");
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000491 return true;
Jeff Cohen31102892007-04-07 20:47:27 +0000492 }
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000493
Jeff Cohen31102892007-04-07 20:47:27 +0000494 if (!(fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
495 if (ErrMsg)
496 *ErrMsg = path + ": not a directory";
Reid Spencer142ca8e2006-08-23 06:56:27 +0000497 return true;
498 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000499
500 result.clear();
501 WIN32_FIND_DATA fd;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000502 std::string searchpath = path;
503 if (path.size() == 0 || searchpath[path.size()-1] == '/')
Jeff Cohen85c716f2005-07-08 05:02:13 +0000504 searchpath += "*";
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000505 else
506 searchpath += "/*";
Jeff Cohen85c716f2005-07-08 05:02:13 +0000507
Jeff Cohen9437bb62005-01-27 03:49:03 +0000508 HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000509 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohen9437bb62005-01-27 03:49:03 +0000510 if (GetLastError() == ERROR_FILE_NOT_FOUND)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000511 return true; // not really an error, now is it?
Reid Spencer142ca8e2006-08-23 06:56:27 +0000512 MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
513 return true;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000514 }
515
516 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000517 if (fd.cFileName[0] == '.')
518 continue;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000519 Path aPath(path);
520 aPath.appendComponent(&fd.cFileName[0]);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000521 result.insert(aPath);
522 } while (FindNextFile(h, &fd));
523
Jeff Cohen51b8d212004-12-31 19:01:08 +0000524 DWORD err = GetLastError();
525 FindClose(h);
526 if (err != ERROR_NO_MORE_FILES) {
527 SetLastError(err);
Reid Spencer142ca8e2006-08-23 06:56:27 +0000528 MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
529 return true;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000530 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000531 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000532}
533
534bool
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000535Path::set(StringRef a_path) {
Dan Gohman30359592008-01-29 13:02:09 +0000536 if (a_path.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000537 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000538 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000539 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000540 FlipBackSlashes(path);
Reid Spencer07adb282004-11-05 22:15:36 +0000541 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000542 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000543 return false;
544 }
545 return true;
546}
547
548bool
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000549Path::appendComponent(StringRef name) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000550 if (name.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000551 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000552 std::string save(path);
553 if (!path.empty()) {
554 size_t last = path.size() - 1;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000555 if (path[last] != '/')
Reid Spencer1cf2d042005-07-07 23:35:23 +0000556 path += '/';
557 }
558 path += name;
Reid Spencer07adb282004-11-05 22:15:36 +0000559 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000560 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000561 return false;
562 }
563 return true;
564}
565
566bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000567Path::eraseComponent() {
Reid Spencerb016a372004-09-15 05:49:50 +0000568 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000569 if (slashpos == path.size() - 1 || slashpos == std::string::npos)
Reid Spencerb016a372004-09-15 05:49:50 +0000570 return false;
Jeff Cohen966fa412005-07-09 18:42:49 +0000571 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000572 path.erase(slashpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000573 if (!isValid()) {
574 path = save;
575 return false;
576 }
Reid Spencerb016a372004-09-15 05:49:50 +0000577 return true;
578}
579
580bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000581Path::eraseSuffix() {
Reid Spencerb016a372004-09-15 05:49:50 +0000582 size_t dotpos = path.rfind('.',path.size());
583 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000584 if (dotpos != std::string::npos) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000585 if (slashpos == std::string::npos || dotpos > slashpos+1) {
586 std::string save(path);
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000587 path.erase(dotpos, path.size()-dotpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000588 if (!isValid()) {
589 path = save;
590 return false;
591 }
Jeff Cohen85c716f2005-07-08 05:02:13 +0000592 return true;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000593 }
Reid Spencerb016a372004-09-15 05:49:50 +0000594 }
595 return false;
596}
597
Reid Spencer30300992006-08-24 18:58:37 +0000598inline bool PathMsg(std::string* ErrMsg, const char* pathname, const char*msg) {
599 if (ErrMsg)
600 *ErrMsg = std::string(pathname) + ": " + std::string(msg);
601 return true;
602}
603
Reid Spencerb016a372004-09-15 05:49:50 +0000604bool
Reid Spencer30300992006-08-24 18:58:37 +0000605Path::createDirectoryOnDisk(bool create_parents, std::string* ErrMsg) {
Reid Spencerb016a372004-09-15 05:49:50 +0000606 // Get a writeable copy of the path name
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000607 size_t len = path.length();
608 char *pathname = reinterpret_cast<char *>(_alloca(len+2));
609 path.copy(pathname, len);
610 pathname[len] = 0;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000611
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000612 // Make sure it ends with a slash.
613 if (len == 0 || pathname[len - 1] != '/') {
614 pathname[len] = '/';
615 pathname[++len] = 0;
616 }
Reid Spencerb016a372004-09-15 05:49:50 +0000617
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000618 // Determine starting point for initial / search.
619 char *next = pathname;
620 if (pathname[0] == '/' && pathname[1] == '/') {
621 // Skip host name.
622 next = strchr(pathname+2, '/');
623 if (next == NULL)
Reid Spencer30300992006-08-24 18:58:37 +0000624 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
625
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000626 // Skip share name.
627 next = strchr(next+1, '/');
628 if (next == NULL)
Reid Spencer30300992006-08-24 18:58:37 +0000629 return PathMsg(ErrMsg, pathname,"badly formed remote directory");
630
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000631 next++;
632 if (*next == 0)
Reid Spencer30300992006-08-24 18:58:37 +0000633 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
634
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000635 } else {
636 if (pathname[1] == ':')
637 next += 2; // skip drive letter
638 if (*next == '/')
639 next++; // skip root directory
640 }
Reid Spencerb016a372004-09-15 05:49:50 +0000641
642 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000643 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000644 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000645 while (*next) {
646 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000647 *next = 0;
Benjamin Kramere9684c62009-11-05 14:32:40 +0000648 if (!CreateDirectory(pathname, NULL) &&
649 GetLastError() != ERROR_ALREADY_EXISTS)
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000650 return MakeErrMsg(ErrMsg,
Reid Spencer30300992006-08-24 18:58:37 +0000651 std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000652 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000653 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000654 } else {
655 // Drop trailing slash.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000656 pathname[len-1] = 0;
Benjamin Kramere9684c62009-11-05 14:32:40 +0000657 if (!CreateDirectory(pathname, NULL) &&
658 GetLastError() != ERROR_ALREADY_EXISTS) {
Mikhail Glushenkov0f2ec152010-11-02 20:32:31 +0000659 return MakeErrMsg(ErrMsg, std::string(pathname) +
660 ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000661 }
Reid Spencerb016a372004-09-15 05:49:50 +0000662 }
Reid Spencer30300992006-08-24 18:58:37 +0000663 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000664}
665
666bool
Reid Spencer30300992006-08-24 18:58:37 +0000667Path::createFileOnDisk(std::string* ErrMsg) {
Reid Spencerb016a372004-09-15 05:49:50 +0000668 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000669 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000670 FILE_ATTRIBUTE_NORMAL, NULL);
671 if (h == INVALID_HANDLE_VALUE)
Reid Spencer30300992006-08-24 18:58:37 +0000672 return MakeErrMsg(ErrMsg, path + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000673
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000674 CloseHandle(h);
Reid Spencer30300992006-08-24 18:58:37 +0000675 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000676}
677
678bool
Chris Lattner0c332312006-07-28 22:29:50 +0000679Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
Jeff Cohen31102892007-04-07 20:47:27 +0000680 WIN32_FILE_ATTRIBUTE_DATA fi;
681 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
682 return true;
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000683
Jeff Cohen31102892007-04-07 20:47:27 +0000684 if (fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000685 // If it doesn't exist, we're done.
Jeff Cohen85c716f2005-07-08 05:02:13 +0000686 if (!exists())
Chris Lattner0c332312006-07-28 22:29:50 +0000687 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000688
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000689 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000690 int lastchar = path.length() - 1 ;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000691 path.copy(pathname, lastchar+1);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000692
693 // Make path end with '/*'.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000694 if (pathname[lastchar] != '/')
695 pathname[++lastchar] = '/';
Reid Spencer1cf2d042005-07-07 23:35:23 +0000696 pathname[lastchar+1] = '*';
697 pathname[lastchar+2] = 0;
698
699 if (remove_contents) {
700 WIN32_FIND_DATA fd;
701 HANDLE h = FindFirstFile(pathname, &fd);
702
703 // It's a bad idea to alter the contents of a directory while enumerating
704 // its contents. So build a list of its contents first, then destroy them.
705
706 if (h != INVALID_HANDLE_VALUE) {
707 std::vector<Path> list;
708
709 do {
710 if (strcmp(fd.cFileName, ".") == 0)
711 continue;
712 if (strcmp(fd.cFileName, "..") == 0)
713 continue;
714
Jeff Cohen85c716f2005-07-08 05:02:13 +0000715 Path aPath(path);
716 aPath.appendComponent(&fd.cFileName[0]);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000717 list.push_back(aPath);
718 } while (FindNextFile(h, &fd));
719
720 DWORD err = GetLastError();
721 FindClose(h);
722 if (err != ERROR_NO_MORE_FILES) {
723 SetLastError(err);
Reid Spencer05545752006-08-25 21:37:17 +0000724 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000725 }
726
Jeff Cohen85c716f2005-07-08 05:02:13 +0000727 for (std::vector<Path>::iterator I = list.begin(); I != list.end();
Reid Spencer1cf2d042005-07-07 23:35:23 +0000728 ++I) {
729 Path &aPath = *I;
Reid Spencera229c5c2005-07-08 03:08:58 +0000730 aPath.eraseFromDisk(true);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000731 }
732 } else {
733 if (GetLastError() != ERROR_FILE_NOT_FOUND)
Reid Spencer05545752006-08-25 21:37:17 +0000734 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000735 }
736 }
737
738 pathname[lastchar] = 0;
739 if (!RemoveDirectory(pathname))
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000740 return MakeErrMsg(ErrStr,
Reid Spencer05545752006-08-25 21:37:17 +0000741 std::string(pathname) + ": Can't destroy directory: ");
Chris Lattner0c332312006-07-28 22:29:50 +0000742 return false;
Jeff Cohen31102892007-04-07 20:47:27 +0000743 } else {
744 // Read-only files cannot be deleted on Windows. Must remove the read-only
745 // attribute first.
746 if (fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
747 if (!SetFileAttributes(path.c_str(),
748 fi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
749 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
750 }
751
752 if (!DeleteFile(path.c_str()))
753 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
754 return false;
755 }
Reid Spencerb016a372004-09-15 05:49:50 +0000756}
757
Reid Spencer3b0cc782004-12-14 18:42:13 +0000758bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
Reid Spencer3b0cc782004-12-14 18:42:13 +0000759 assert(len < 1024 && "Request for magic string too long");
Michael J. Spencer1211d432010-08-31 06:36:33 +0000760 char* buf = reinterpret_cast<char*>(alloca(len));
Jeff Cohen51b8d212004-12-31 19:01:08 +0000761
762 HANDLE h = CreateFile(path.c_str(),
763 GENERIC_READ,
764 FILE_SHARE_READ,
765 NULL,
766 OPEN_EXISTING,
767 FILE_ATTRIBUTE_NORMAL,
768 NULL);
769 if (h == INVALID_HANDLE_VALUE)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000770 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000771
772 DWORD nRead = 0;
773 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
774 CloseHandle(h);
775
776 if (!ret || nRead != len)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000777 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000778
Michael J. Spencer1211d432010-08-31 06:36:33 +0000779 Magic = std::string(buf, len);
Reid Spencer3b0cc782004-12-14 18:42:13 +0000780 return true;
781}
782
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000783bool
Reid Spencer5a060772006-08-23 07:30:48 +0000784Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
Francois Pichet3eddd982010-09-30 00:44:58 +0000785 if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING))
786 return MakeErrMsg(ErrMsg, "Can't move '" + path + "' to '" + newName.path
787 + "': ");
788 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000789}
790
791bool
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000792Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrMsg) const {
Jeff Cohen966fa412005-07-09 18:42:49 +0000793 // FIXME: should work on directories also.
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000794 if (!si.isFile) {
795 return true;
796 }
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000797
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000798 HANDLE h = CreateFile(path.c_str(),
799 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
800 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000801 NULL,
802 OPEN_EXISTING,
803 FILE_ATTRIBUTE_NORMAL,
804 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000805 if (h == INVALID_HANDLE_VALUE)
Chris Lattner1bebfb52006-07-28 22:36:17 +0000806 return true;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000807
808 BY_HANDLE_FILE_INFORMATION bhfi;
809 if (!GetFileInformationByHandle(h, &bhfi)) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000810 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000811 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000812 SetLastError(err);
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000813 return MakeErrMsg(ErrMsg, path + ": GetFileInformationByHandle: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000814 }
815
Michael J. Spencer44edb0b2010-08-28 16:39:32 +0000816 ULARGE_INTEGER ui;
817 ui.QuadPart = si.modTime.toWin32Time();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000818 FILETIME ft;
Michael J. Spencer44edb0b2010-08-28 16:39:32 +0000819 ft.dwLowDateTime = ui.LowPart;
820 ft.dwHighDateTime = ui.HighPart;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000821 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000822 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000823 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000824 if (!ret) {
825 SetLastError(err);
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000826 return MakeErrMsg(ErrMsg, path + ": SetFileTime: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000827 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000828
829 // Best we can do with Unix permission bits is to interpret the owner
830 // writable bit.
831 if (si.mode & 0200) {
832 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
833 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000834 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000835 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000836 }
837 } else {
838 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
839 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000840 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000841 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000842 }
843 }
844
Chris Lattner1bebfb52006-07-28 22:36:17 +0000845 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000846}
847
Reid Spencer30300992006-08-24 18:58:37 +0000848bool
849CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg) {
Jeff Cohencb652552004-12-24 02:38:34 +0000850 // Can't use CopyFile macro defined in Windows.h because it would mess up the
851 // above line. We use the expansion it would have in a non-UNICODE build.
852 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Chris Lattner74382b72009-08-23 22:45:37 +0000853 return MakeErrMsg(ErrMsg, "Can't copy '" + Src.str() +
854 "' to '" + Dest.str() + "': ");
Reid Spencer30300992006-08-24 18:58:37 +0000855 return false;
Reid Spencerc29befb2004-12-15 01:50:13 +0000856}
857
Reid Spencer30300992006-08-24 18:58:37 +0000858bool
859Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000860 if (reuse_current && !exists())
Reid Spencer30300992006-08-24 18:58:37 +0000861 return false; // File doesn't exist already, just use it!
Reid Spencerc29befb2004-12-15 01:50:13 +0000862
Jeff Cohen9437bb62005-01-27 03:49:03 +0000863 // Reserve space for -XXXXXX at the end.
864 char *FNBuffer = (char*) alloca(path.size()+8);
865 unsigned offset = path.size();
866 path.copy(FNBuffer, offset);
Reid Spencerc29befb2004-12-15 01:50:13 +0000867
Jeff Cohen966fa412005-07-09 18:42:49 +0000868 // Find a numeric suffix that isn't used by an existing file. Assume there
869 // won't be more than 1 million files with the same prefix. Probably a safe
870 // bet.
Jeff Cohen9437bb62005-01-27 03:49:03 +0000871 static unsigned FCounter = 0;
872 do {
873 sprintf(FNBuffer+offset, "-%06u", FCounter);
874 if (++FCounter > 999999)
875 FCounter = 0;
876 path = FNBuffer;
877 } while (exists());
Reid Spencer30300992006-08-24 18:58:37 +0000878 return false;
Reid Spencerc29befb2004-12-15 01:50:13 +0000879}
880
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000881bool
Reid Spencer30300992006-08-24 18:58:37 +0000882Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000883 // Make this into a unique file name
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000884 makeUnique(reuse_current, ErrMsg);
Jeff Cohen9437bb62005-01-27 03:49:03 +0000885
886 // Now go and create it
887 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
888 FILE_ATTRIBUTE_NORMAL, NULL);
889 if (h == INVALID_HANDLE_VALUE)
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000890 return MakeErrMsg(ErrMsg, path + ": can't create file");
Jeff Cohen9437bb62005-01-27 03:49:03 +0000891
892 CloseHandle(h);
Reid Spencer30300992006-08-24 18:58:37 +0000893 return false;
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000894}
895
Chris Lattner799ed102008-04-01 06:00:12 +0000896/// MapInFilePages - Not yet implemented on win32.
897const char *Path::MapInFilePages(int FD, uint64_t FileSize) {
898 return 0;
899}
900
901/// MapInFilePages - Not yet implemented on win32.
902void Path::UnMapFilePages(const char *Base, uint64_t FileSize) {
903 assert(0 && "NOT IMPLEMENTED");
904}
905
Reid Spencerb016a372004-09-15 05:49:50 +0000906}
Reid Spencercbad7012004-09-11 04:59:30 +0000907}