blob: a4d55a0f048f6df98662698c1e839f8bdf85ecbe [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//
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>
Chris Lattnerc23c1fc2009-04-01 02:03:38 +000024#include <cstdio>
Reid Spencercbad7012004-09-11 04:59:30 +000025
Jeff Cohencb652552004-12-24 02:38:34 +000026// We need to undo a macro defined in Windows.h, otherwise we won't compile:
27#undef CopyFile
Anton Korobeynikov64ddbe42007-12-22 14:26:49 +000028#undef GetCurrentDirectory
Jeff Cohencb652552004-12-24 02:38:34 +000029
Jeff Cohen966fa412005-07-09 18:42:49 +000030// Windows happily accepts either forward or backward slashes, though any path
31// returned by a Win32 API will have backward slashes. As LLVM code basically
32// assumes forward slashes are used, backward slashs are converted where they
33// can be introduced into a path.
34//
35// Another invariant is that a path ends with a slash if and only if the path
36// is a root directory. Any other use of a trailing slash is stripped. Unlike
37// in Unix, Windows has a rather complicated notion of a root path and this
38// invariant helps simply the code.
39
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000040static void FlipBackSlashes(std::string& s) {
41 for (size_t i = 0; i < s.size(); i++)
42 if (s[i] == '\\')
43 s[i] = '/';
44}
45
Reid Spencercbad7012004-09-11 04:59:30 +000046namespace llvm {
Reid Spencerb016a372004-09-15 05:49:50 +000047namespace sys {
Mikhail Glushenkovf5a95ce2010-11-02 20:32:26 +000048
Chris Lattnera17fa282008-03-13 05:17:59 +000049const char PathSeparator = ';';
Chris Lattnere1b332a2008-02-27 06:17:10 +000050
Mikhail Glushenkovf5a95ce2010-11-02 20:32:26 +000051StringRef Path::GetEXESuffix() {
52 return "exe";
53}
54
Daniel Dunbar1edcafe2009-12-18 19:59:48 +000055Path::Path(llvm::StringRef p)
Nick Lewyckyfff116f2008-05-11 17:37:40 +000056 : path(p) {
57 FlipBackSlashes(path);
58}
59
60Path::Path(const char *StrStart, unsigned StrLen)
61 : path(StrStart, StrLen) {
62 FlipBackSlashes(path);
63}
64
Chris Lattner0eab5e22008-08-11 23:39:47 +000065Path&
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +000066Path::operator=(StringRef that) {
67 path.assign(that.data(), that.size());
Chris Lattner0eab5e22008-08-11 23:39:47 +000068 FlipBackSlashes(path);
69 return *this;
70}
71
Michael J. Spencera9cf7b82010-10-20 15:23:58 +000072// push_back 0 on create, and pop_back on delete.
73struct ScopedNullTerminator {
74 std::string &str;
75 ScopedNullTerminator(std::string &s) : str(s) { str.push_back(0); }
Michael J. Spencer9f608b12010-10-20 16:00:45 +000076 ~ScopedNullTerminator() {
77 // str.pop_back(); But wait, C++03 doesn't have this...
78 assert(!str.empty() && str[str.size() - 1] == 0
79 && "Null char not present!");
80 str.resize(str.size() - 1);
81 }
Michael J. Spencera9cf7b82010-10-20 15:23:58 +000082};
83
Reid Spencerb016a372004-09-15 05:49:50 +000084bool
Reid Spencer07adb282004-11-05 22:15:36 +000085Path::isValid() const {
Reid Spencerb016a372004-09-15 05:49:50 +000086 if (path.empty())
87 return false;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +000088
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000089 // If there is a colon, it must be the second character, preceded by a letter
90 // and followed by something.
91 size_t len = path.size();
Michael J. Spencera9cf7b82010-10-20 15:23:58 +000092 // This code assumes that path is null terminated, so make sure it is.
93 ScopedNullTerminator snt(path);
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000094 size_t pos = path.rfind(':',len);
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000095 size_t rootslash = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +000096 if (pos != std::string::npos) {
97 if (pos != 1 || !isalpha(path[0]) || len < 3)
98 return false;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +000099 rootslash = 2;
100 }
Jeff Cohen85c716f2005-07-08 05:02:13 +0000101
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000102 // Look for a UNC path, and if found adjust our notion of the root slash.
103 if (len > 3 && path[0] == '/' && path[1] == '/') {
104 rootslash = path.find('/', 2);
105 if (rootslash == std::string::npos)
106 rootslash = 0;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000107 }
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000108
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000109 // Check for illegal characters.
110 if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012"
111 "\013\014\015\016\017\020\021\022\023\024\025\026"
112 "\027\030\031\032\033\034\035\036\037")
113 != std::string::npos)
114 return false;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000115
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000116 // Remove trailing slash, unless it's a root slash.
117 if (len > rootslash+1 && path[len-1] == '/')
118 path.erase(--len);
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000119
Jeff Cohen3bbbcc12005-01-14 04:09:39 +0000120 // Check each component for legality.
121 for (pos = 0; pos < len; ++pos) {
122 // A component may not end in a space.
123 if (path[pos] == ' ') {
124 if (path[pos+1] == '/' || path[pos+1] == '\0')
125 return false;
126 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000127
Jeff Cohen3bbbcc12005-01-14 04:09:39 +0000128 // A component may not end in a period.
129 if (path[pos] == '.') {
130 if (path[pos+1] == '/' || path[pos+1] == '\0') {
131 // Unless it is the pseudo-directory "."...
132 if (pos == 0 || path[pos-1] == '/' || path[pos-1] == ':')
133 return true;
134 // or "..".
135 if (pos > 0 && path[pos-1] == '.') {
136 if (pos == 1 || path[pos-2] == '/' || path[pos-2] == ':')
137 return true;
138 }
139 return false;
140 }
141 }
142 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000143
144 return true;
Reid Spencercbad7012004-09-11 04:59:30 +0000145}
146
Daniel Dunbara00e85c2009-07-12 20:23:56 +0000147void Path::makeAbsolute() {
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000148 TCHAR FullPath[MAX_PATH + 1] = {0};
Daniel Dunbara00e85c2009-07-12 20:23:56 +0000149 LPTSTR FilePart = NULL;
150
151 DWORD RetLength = ::GetFullPathNameA(path.c_str(),
152 sizeof(FullPath)/sizeof(FullPath[0]),
153 FullPath, &FilePart);
154
155 if (0 == RetLength) {
156 // FIXME: Report the error GetLastError()
Daniel Dunbar2749b3e2009-07-26 21:16:42 +0000157 assert(0 && "Unable to make absolute path!");
Daniel Dunbara00e85c2009-07-12 20:23:56 +0000158 } else if (RetLength > MAX_PATH) {
159 // FIXME: Report too small buffer (needed RetLength bytes).
Daniel Dunbar2749b3e2009-07-26 21:16:42 +0000160 assert(0 && "Unable to make absolute path!");
Daniel Dunbara00e85c2009-07-12 20:23:56 +0000161 } else {
162 path = FullPath;
163 }
164}
165
Chris Lattner88d7e402009-06-15 04:17:07 +0000166bool
167Path::isAbsolute(const char *NameStart, unsigned NameLen) {
168 assert(NameStart);
Daniel Dunbara00e85c2009-07-12 20:23:56 +0000169 // FIXME: This does not handle correctly an absolute path starting from
170 // a drive letter or in UNC format.
Chris Lattner88d7e402009-06-15 04:17:07 +0000171 switch (NameLen) {
172 case 0:
173 return false;
174 case 1:
175 case 2:
176 return NameStart[0] == '/';
177 default:
Chris Lattnera79eefd2009-08-12 17:47:06 +0000178 return (NameStart[0] == '/' || (NameStart[1] == ':' && NameStart[2] == '/')) ||
179 (NameStart[0] == '\\' || (NameStart[1] == ':' && NameStart[2] == '\\'));
Chris Lattner88d7e402009-06-15 04:17:07 +0000180 }
181}
182
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000183bool
Reid Spencer69cce812007-03-29 16:43:20 +0000184Path::isAbsolute() const {
Daniel Dunbara00e85c2009-07-12 20:23:56 +0000185 // FIXME: This does not handle correctly an absolute path starting from
186 // a drive letter or in UNC format.
Jeff Cohen84892be2007-03-29 17:27:38 +0000187 switch (path.length()) {
188 case 0:
189 return false;
190 case 1:
191 case 2:
192 return path[0] == '/';
193 default:
194 return path[0] == '/' || (path[1] == ':' && path[2] == '/');
195 }
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000196}
Reid Spencer69cce812007-03-29 16:43:20 +0000197
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000198static Path *TempDirectory;
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000199
Reid Spencerb016a372004-09-15 05:49:50 +0000200Path
Reid Spencercab0e432006-08-22 22:46:39 +0000201Path::GetTemporaryDirectory(std::string* ErrMsg) {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000202 if (TempDirectory)
203 return *TempDirectory;
204
205 char pathname[MAX_PATH];
Reid Spencercab0e432006-08-22 22:46:39 +0000206 if (!GetTempPath(MAX_PATH, pathname)) {
207 if (ErrMsg)
208 *ErrMsg = "Can't determine temporary directory";
209 return Path();
210 }
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000211
Reid Spencerb016a372004-09-15 05:49:50 +0000212 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000213 result.set(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000214
215 // Append a subdirectory passed on our process id so multiple LLVMs don't
216 // step on each other's toes.
Jeff Cohend41b30d2006-11-05 19:31:28 +0000217#ifdef __MINGW32__
218 // Mingw's Win32 header files are broken.
Reid Spencerab4d9b02006-06-08 18:08:43 +0000219 sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId()));
Jeff Cohend41b30d2006-11-05 19:31:28 +0000220#else
221 sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
222#endif
Jeff Cohenedb9d6b2005-07-08 02:48:42 +0000223 result.appendComponent(pathname);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000224
225 // If there's a directory left over from a previous LLVM execution that
226 // happened to have the same process id, get rid of it.
Reid Spencera229c5c2005-07-08 03:08:58 +0000227 result.eraseFromDisk(true);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000228
229 // And finally (re-)create the empty directory.
Reid Spencera229c5c2005-07-08 03:08:58 +0000230 result.createDirectoryOnDisk(false);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000231 TempDirectory = new Path(result);
232 return *TempDirectory;
Reid Spencerb016a372004-09-15 05:49:50 +0000233}
234
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000235// FIXME: the following set of functions don't map to Windows very well.
Reid Spencerb016a372004-09-15 05:49:50 +0000236Path
237Path::GetRootDirectory() {
238 Path result;
Jeff Cohen966fa412005-07-09 18:42:49 +0000239 result.set("C:/");
Reid Spencerb016a372004-09-15 05:49:50 +0000240 return result;
241}
242
Jeff Cohen85c716f2005-07-08 05:02:13 +0000243void
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000244Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000245 Paths.push_back(sys::Path("C:/WINDOWS/SYSTEM32"));
246 Paths.push_back(sys::Path("C:/WINDOWS"));
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000247}
248
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000249void
Gabor Greifdb5565a2007-07-06 20:28:40 +0000250Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) {
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000251 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
252 if (env_var != 0) {
253 getPathList(env_var,Paths);
254 }
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000255#ifdef LLVM_LIBDIR
256 {
257 Path tmpPath;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000258 if (tmpPath.set(LLVM_LIBDIR))
Reid Spencerc7f08322005-07-07 18:21:42 +0000259 if (tmpPath.canRead())
Reid Spencer6c4b7bd2004-12-13 03:03:42 +0000260 Paths.push_back(tmpPath);
261 }
262#endif
263 GetSystemLibraryPaths(Paths);
Reid Spencerb016a372004-09-15 05:49:50 +0000264}
265
266Path
267Path::GetLLVMDefaultConfigDir() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000268 // TODO: this isn't going to fly on Windows
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000269 return Path("/etc/llvm");
Reid Spencerb016a372004-09-15 05:49:50 +0000270}
271
272Path
Reid Spencerb016a372004-09-15 05:49:50 +0000273Path::GetUserHomeDirectory() {
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000274 // TODO: Typical Windows setup doesn't define HOME.
Reid Spencerb016a372004-09-15 05:49:50 +0000275 const char* home = getenv("HOME");
276 if (home) {
277 Path result;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000278 if (result.set(home))
Reid Spencerb016a372004-09-15 05:49:50 +0000279 return result;
280 }
281 return GetRootDirectory();
282}
Ted Kremenek79200782007-12-18 22:07:33 +0000283
284Path
285Path::GetCurrentDirectory() {
286 char pathname[MAX_PATH];
Anton Korobeynikov64ddbe42007-12-22 14:26:49 +0000287 ::GetCurrentDirectoryA(MAX_PATH,pathname);
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000288 return Path(pathname);
Ted Kremenek79200782007-12-18 22:07:33 +0000289}
290
Chris Lattner1a091442008-03-03 02:55:43 +0000291/// GetMainExecutable - Return the path to the main executable, given the
292/// value of argv[0] from program startup.
293Path Path::GetMainExecutable(const char *argv0, void *MainAddr) {
Chris Lattnerc5718d02009-06-15 05:38:04 +0000294 char pathname[MAX_PATH];
295 DWORD ret = ::GetModuleFileNameA(NULL, pathname, MAX_PATH);
296 return ret != MAX_PATH ? Path(pathname) : Path();
Chris Lattner1a091442008-03-03 02:55:43 +0000297}
298
Ted Kremenek79200782007-12-18 22:07:33 +0000299
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000300// FIXME: the above set of functions don't map to Windows very well.
301
Jeff Cohen966fa412005-07-09 18:42:49 +0000302
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000303StringRef Path::getDirname() const {
304 return getDirnameCharSep(path, "/");
Ted Kremenek9b01cc02008-04-07 22:01:32 +0000305}
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000306
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000307StringRef
Reid Spencer07adb282004-11-05 22:15:36 +0000308Path::getBasename() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000309 // Find the last slash
310 size_t slash = path.rfind('/');
311 if (slash == std::string::npos)
312 slash = 0;
313 else
314 slash++;
315
Jeff Cohen966fa412005-07-09 18:42:49 +0000316 size_t dot = path.rfind('.');
317 if (dot == std::string::npos || dot < slash)
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000318 return StringRef(path).substr(slash);
Jeff Cohen966fa412005-07-09 18:42:49 +0000319 else
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000320 return StringRef(path).substr(slash, dot - slash);
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000321}
322
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000323StringRef
Argyrios Kyrtzidisfc199882008-06-15 15:15:19 +0000324Path::getSuffix() const {
325 // Find the last slash
326 size_t slash = path.rfind('/');
327 if (slash == std::string::npos)
328 slash = 0;
329 else
330 slash++;
331
332 size_t dot = path.rfind('.');
333 if (dot == std::string::npos || dot < slash)
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000334 return StringRef("");
Argyrios Kyrtzidisfc199882008-06-15 15:15:19 +0000335 else
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000336 return StringRef(path).substr(dot + 1);
Argyrios Kyrtzidisfc199882008-06-15 15:15:19 +0000337}
338
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000339bool
Reid Spencerb016a372004-09-15 05:49:50 +0000340Path::exists() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000341 DWORD attr = GetFileAttributes(path.c_str());
342 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000343}
344
345bool
Ted Kremenekfd527112007-12-18 19:46:22 +0000346Path::isDirectory() const {
347 DWORD attr = GetFileAttributes(path.c_str());
348 return (attr != INVALID_FILE_ATTRIBUTES) &&
349 (attr & FILE_ATTRIBUTE_DIRECTORY);
350}
351
352bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000353Path::canRead() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000354 // FIXME: take security attributes into account.
355 DWORD attr = GetFileAttributes(path.c_str());
356 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000357}
358
359bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000360Path::canWrite() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000361 // FIXME: take security attributes into account.
362 DWORD attr = GetFileAttributes(path.c_str());
363 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
Reid Spencerb016a372004-09-15 05:49:50 +0000364}
365
366bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000367Path::canExecute() const {
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000368 // FIXME: take security attributes into account.
369 DWORD attr = GetFileAttributes(path.c_str());
370 return attr != INVALID_FILE_ATTRIBUTES;
Reid Spencerb016a372004-09-15 05:49:50 +0000371}
372
Edward O'Callaghand41e9442009-11-24 15:19:10 +0000373bool
Edward O'Callaghane49a8e42009-11-25 06:32:19 +0000374Path::isRegularFile() const {
375 if (isDirectory())
376 return false;
377 return true;
Edward O'Callaghand41e9442009-11-24 15:19:10 +0000378}
379
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000380StringRef
Reid Spencerb016a372004-09-15 05:49:50 +0000381Path::getLast() const {
382 // Find the last slash
383 size_t pos = path.rfind('/');
384
385 // Handle the corner cases
386 if (pos == std::string::npos)
387 return path;
388
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000389 // If the last character is a slash, we have a root directory
390 if (pos == path.length()-1)
391 return path;
392
Reid Spencerb016a372004-09-15 05:49:50 +0000393 // Return everything after the last slash
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000394 return StringRef(path).substr(pos+1);
Reid Spencerb016a372004-09-15 05:49:50 +0000395}
396
Reid Spencer8475ec02007-03-29 19:05:44 +0000397const FileStatus *
Reid Spencer2ae9d112007-04-07 18:52:17 +0000398PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const {
399 if (!fsIsValid || update) {
Reid Spencer69cce812007-03-29 16:43:20 +0000400 WIN32_FILE_ATTRIBUTE_DATA fi;
Reid Spencer8475ec02007-03-29 19:05:44 +0000401 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
402 MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) +
Reid Spencer69cce812007-03-29 16:43:20 +0000403 ": Can't get status: ");
Reid Spencer8475ec02007-03-29 19:05:44 +0000404 return 0;
405 }
Jeff Cohen626e38e2004-12-14 05:26:43 +0000406
Reid Spencer2ae9d112007-04-07 18:52:17 +0000407 status.fileSize = fi.nFileSizeHigh;
408 status.fileSize <<= sizeof(fi.nFileSizeHigh)*8;
409 status.fileSize += fi.nFileSizeLow;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000410
Reid Spencer2ae9d112007-04-07 18:52:17 +0000411 status.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
412 status.user = 9999; // Not applicable to Windows, so...
413 status.group = 9999; // Not applicable to Windows, so...
Jeff Cohen626e38e2004-12-14 05:26:43 +0000414
Reid Spencer4031bef2007-03-29 17:00:31 +0000415 // FIXME: this is only unique if the file is accessed by the same file path.
416 // How do we do this for C:\dir\file and ..\dir\file ? Unix has inode
417 // numbers, but the concept doesn't exist in Windows.
Reid Spencer2ae9d112007-04-07 18:52:17 +0000418 status.uniqueID = 0;
Reid Spencer4031bef2007-03-29 17:00:31 +0000419 for (unsigned i = 0; i < path.length(); ++i)
Reid Spencer2ae9d112007-04-07 18:52:17 +0000420 status.uniqueID += path[i];
Reid Spencer4031bef2007-03-29 17:00:31 +0000421
Michael J. Spencer44edb0b2010-08-28 16:39:32 +0000422 ULARGE_INTEGER ui;
423 ui.LowPart = fi.ftLastWriteTime.dwLowDateTime;
424 ui.HighPart = fi.ftLastWriteTime.dwHighDateTime;
425 status.modTime.fromWin32Time(ui.QuadPart);
Reid Spencer69cce812007-03-29 16:43:20 +0000426
Reid Spencer2ae9d112007-04-07 18:52:17 +0000427 status.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
428 fsIsValid = true;
Reid Spencer69cce812007-03-29 16:43:20 +0000429 }
Reid Spencer2ae9d112007-04-07 18:52:17 +0000430 return &status;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000431}
432
Reid Spencera34a1572006-08-22 23:54:35 +0000433bool Path::makeReadableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000434 // All files are readable on Windows (ignoring security attributes).
Reid Spencera34a1572006-08-22 23:54:35 +0000435 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000436}
437
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000438bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000439 DWORD attr = GetFileAttributes(path.c_str());
440
441 // If it doesn't exist, we're done.
442 if (attr == INVALID_FILE_ATTRIBUTES)
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000443 return false;
Jeff Cohen626e38e2004-12-14 05:26:43 +0000444
445 if (attr & FILE_ATTRIBUTE_READONLY) {
Reid Spencera34a1572006-08-22 23:54:35 +0000446 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) {
447 MakeErrMsg(ErrMsg, std::string(path) + ": Can't make file writable: ");
448 return true;
449 }
Jeff Cohen626e38e2004-12-14 05:26:43 +0000450 }
Reid Spencera34a1572006-08-22 23:54:35 +0000451 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000452}
453
Reid Spencera34a1572006-08-22 23:54:35 +0000454bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
Jeff Cohen626e38e2004-12-14 05:26:43 +0000455 // All files are executable on Windows (ignoring security attributes).
Reid Spencera34a1572006-08-22 23:54:35 +0000456 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000457}
458
Reid Spencerb016a372004-09-15 05:49:50 +0000459bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000460Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
Jeff Cohen31102892007-04-07 20:47:27 +0000461 WIN32_FILE_ATTRIBUTE_DATA fi;
462 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
463 MakeErrMsg(ErrMsg, path + ": can't get status of file");
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000464 return true;
Jeff Cohen31102892007-04-07 20:47:27 +0000465 }
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000466
Jeff Cohen31102892007-04-07 20:47:27 +0000467 if (!(fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
468 if (ErrMsg)
469 *ErrMsg = path + ": not a directory";
Reid Spencer142ca8e2006-08-23 06:56:27 +0000470 return true;
471 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000472
473 result.clear();
474 WIN32_FIND_DATA fd;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000475 std::string searchpath = path;
476 if (path.size() == 0 || searchpath[path.size()-1] == '/')
Jeff Cohen85c716f2005-07-08 05:02:13 +0000477 searchpath += "*";
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000478 else
479 searchpath += "/*";
Jeff Cohen85c716f2005-07-08 05:02:13 +0000480
Jeff Cohen9437bb62005-01-27 03:49:03 +0000481 HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000482 if (h == INVALID_HANDLE_VALUE) {
Jeff Cohen9437bb62005-01-27 03:49:03 +0000483 if (GetLastError() == ERROR_FILE_NOT_FOUND)
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000484 return true; // not really an error, now is it?
Reid Spencer142ca8e2006-08-23 06:56:27 +0000485 MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
486 return true;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000487 }
488
489 do {
Jeff Cohend40a7de2004-12-31 05:07:26 +0000490 if (fd.cFileName[0] == '.')
491 continue;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000492 Path aPath(path);
493 aPath.appendComponent(&fd.cFileName[0]);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000494 result.insert(aPath);
495 } while (FindNextFile(h, &fd));
496
Jeff Cohen51b8d212004-12-31 19:01:08 +0000497 DWORD err = GetLastError();
498 FindClose(h);
499 if (err != ERROR_NO_MORE_FILES) {
500 SetLastError(err);
Reid Spencer142ca8e2006-08-23 06:56:27 +0000501 MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
502 return true;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000503 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000504 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000505}
506
507bool
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000508Path::set(StringRef a_path) {
Dan Gohman30359592008-01-29 13:02:09 +0000509 if (a_path.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000510 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000511 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000512 path = a_path;
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000513 FlipBackSlashes(path);
Reid Spencer07adb282004-11-05 22:15:36 +0000514 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000515 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000516 return false;
517 }
518 return true;
519}
520
521bool
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000522Path::appendComponent(StringRef name) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000523 if (name.empty())
Reid Spencerb016a372004-09-15 05:49:50 +0000524 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000525 std::string save(path);
526 if (!path.empty()) {
527 size_t last = path.size() - 1;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000528 if (path[last] != '/')
Reid Spencer1cf2d042005-07-07 23:35:23 +0000529 path += '/';
530 }
531 path += name;
Reid Spencer07adb282004-11-05 22:15:36 +0000532 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000533 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000534 return false;
535 }
536 return true;
537}
538
539bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000540Path::eraseComponent() {
Reid Spencerb016a372004-09-15 05:49:50 +0000541 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000542 if (slashpos == path.size() - 1 || slashpos == std::string::npos)
Reid Spencerb016a372004-09-15 05:49:50 +0000543 return false;
Jeff Cohen966fa412005-07-09 18:42:49 +0000544 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000545 path.erase(slashpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000546 if (!isValid()) {
547 path = save;
548 return false;
549 }
Reid Spencerb016a372004-09-15 05:49:50 +0000550 return true;
551}
552
553bool
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000554Path::appendSuffix(StringRef suffix) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000555 std::string save(path);
Reid Spencerb016a372004-09-15 05:49:50 +0000556 path.append(".");
557 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000558 if (!isValid()) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000559 path = save;
Reid Spencerb016a372004-09-15 05:49:50 +0000560 return false;
561 }
562 return true;
563}
564
565bool
Reid Spencer1cf2d042005-07-07 23:35:23 +0000566Path::eraseSuffix() {
Reid Spencerb016a372004-09-15 05:49:50 +0000567 size_t dotpos = path.rfind('.',path.size());
568 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000569 if (dotpos != std::string::npos) {
Jeff Cohen966fa412005-07-09 18:42:49 +0000570 if (slashpos == std::string::npos || dotpos > slashpos+1) {
571 std::string save(path);
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000572 path.erase(dotpos, path.size()-dotpos);
Jeff Cohen966fa412005-07-09 18:42:49 +0000573 if (!isValid()) {
574 path = save;
575 return false;
576 }
Jeff Cohen85c716f2005-07-08 05:02:13 +0000577 return true;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000578 }
Reid Spencerb016a372004-09-15 05:49:50 +0000579 }
580 return false;
581}
582
Reid Spencer30300992006-08-24 18:58:37 +0000583inline bool PathMsg(std::string* ErrMsg, const char* pathname, const char*msg) {
584 if (ErrMsg)
585 *ErrMsg = std::string(pathname) + ": " + std::string(msg);
586 return true;
587}
588
Reid Spencerb016a372004-09-15 05:49:50 +0000589bool
Reid Spencer30300992006-08-24 18:58:37 +0000590Path::createDirectoryOnDisk(bool create_parents, std::string* ErrMsg) {
Reid Spencerb016a372004-09-15 05:49:50 +0000591 // Get a writeable copy of the path name
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000592 size_t len = path.length();
593 char *pathname = reinterpret_cast<char *>(_alloca(len+2));
594 path.copy(pathname, len);
595 pathname[len] = 0;
Jeff Cohen85c716f2005-07-08 05:02:13 +0000596
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000597 // Make sure it ends with a slash.
598 if (len == 0 || pathname[len - 1] != '/') {
599 pathname[len] = '/';
600 pathname[++len] = 0;
601 }
Reid Spencerb016a372004-09-15 05:49:50 +0000602
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000603 // Determine starting point for initial / search.
604 char *next = pathname;
605 if (pathname[0] == '/' && pathname[1] == '/') {
606 // Skip host name.
607 next = strchr(pathname+2, '/');
608 if (next == NULL)
Reid Spencer30300992006-08-24 18:58:37 +0000609 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
610
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000611 // Skip share name.
612 next = strchr(next+1, '/');
613 if (next == NULL)
Reid Spencer30300992006-08-24 18:58:37 +0000614 return PathMsg(ErrMsg, pathname,"badly formed remote directory");
615
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000616 next++;
617 if (*next == 0)
Reid Spencer30300992006-08-24 18:58:37 +0000618 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
619
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000620 } else {
621 if (pathname[1] == ':')
622 next += 2; // skip drive letter
623 if (*next == '/')
624 next++; // skip root directory
625 }
Reid Spencerb016a372004-09-15 05:49:50 +0000626
627 // If we're supposed to create intermediate directories
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000628 if (create_parents) {
Reid Spencerb016a372004-09-15 05:49:50 +0000629 // Loop through the directory components until we're done
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000630 while (*next) {
631 next = strchr(next, '/');
Reid Spencerb016a372004-09-15 05:49:50 +0000632 *next = 0;
Benjamin Kramere9684c62009-11-05 14:32:40 +0000633 if (!CreateDirectory(pathname, NULL) &&
634 GetLastError() != ERROR_ALREADY_EXISTS)
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000635 return MakeErrMsg(ErrMsg,
Reid Spencer30300992006-08-24 18:58:37 +0000636 std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000637 *next++ = '/';
Reid Spencerb016a372004-09-15 05:49:50 +0000638 }
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000639 } else {
640 // Drop trailing slash.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000641 pathname[len-1] = 0;
Benjamin Kramere9684c62009-11-05 14:32:40 +0000642 if (!CreateDirectory(pathname, NULL) &&
643 GetLastError() != ERROR_ALREADY_EXISTS) {
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000644 return MakeErrMsg(ErrMsg, std::string(pathname) + ": Can't create directory: ");
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000645 }
Reid Spencerb016a372004-09-15 05:49:50 +0000646 }
Reid Spencer30300992006-08-24 18:58:37 +0000647 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000648}
649
650bool
Reid Spencer30300992006-08-24 18:58:37 +0000651Path::createFileOnDisk(std::string* ErrMsg) {
Reid Spencerb016a372004-09-15 05:49:50 +0000652 // Create the file
Reid Spencer6a0ec6f2004-09-29 00:01:17 +0000653 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000654 FILE_ATTRIBUTE_NORMAL, NULL);
655 if (h == INVALID_HANDLE_VALUE)
Reid Spencer30300992006-08-24 18:58:37 +0000656 return MakeErrMsg(ErrMsg, path + ": Can't create file: ");
Reid Spencerb016a372004-09-15 05:49:50 +0000657
Reid Spencerd0c9e0e2004-09-18 19:29:16 +0000658 CloseHandle(h);
Reid Spencer30300992006-08-24 18:58:37 +0000659 return false;
Reid Spencerb016a372004-09-15 05:49:50 +0000660}
661
662bool
Chris Lattner0c332312006-07-28 22:29:50 +0000663Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
Jeff Cohen31102892007-04-07 20:47:27 +0000664 WIN32_FILE_ATTRIBUTE_DATA fi;
665 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
666 return true;
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000667
Jeff Cohen31102892007-04-07 20:47:27 +0000668 if (fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Reid Spencer1cf2d042005-07-07 23:35:23 +0000669 // If it doesn't exist, we're done.
Jeff Cohen85c716f2005-07-08 05:02:13 +0000670 if (!exists())
Chris Lattner0c332312006-07-28 22:29:50 +0000671 return false;
Reid Spencer1cf2d042005-07-07 23:35:23 +0000672
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000673 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
Reid Spencer1cf2d042005-07-07 23:35:23 +0000674 int lastchar = path.length() - 1 ;
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000675 path.copy(pathname, lastchar+1);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000676
677 // Make path end with '/*'.
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000678 if (pathname[lastchar] != '/')
679 pathname[++lastchar] = '/';
Reid Spencer1cf2d042005-07-07 23:35:23 +0000680 pathname[lastchar+1] = '*';
681 pathname[lastchar+2] = 0;
682
683 if (remove_contents) {
684 WIN32_FIND_DATA fd;
685 HANDLE h = FindFirstFile(pathname, &fd);
686
687 // It's a bad idea to alter the contents of a directory while enumerating
688 // its contents. So build a list of its contents first, then destroy them.
689
690 if (h != INVALID_HANDLE_VALUE) {
691 std::vector<Path> list;
692
693 do {
694 if (strcmp(fd.cFileName, ".") == 0)
695 continue;
696 if (strcmp(fd.cFileName, "..") == 0)
697 continue;
698
Jeff Cohen85c716f2005-07-08 05:02:13 +0000699 Path aPath(path);
700 aPath.appendComponent(&fd.cFileName[0]);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000701 list.push_back(aPath);
702 } while (FindNextFile(h, &fd));
703
704 DWORD err = GetLastError();
705 FindClose(h);
706 if (err != ERROR_NO_MORE_FILES) {
707 SetLastError(err);
Reid Spencer05545752006-08-25 21:37:17 +0000708 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000709 }
710
Jeff Cohen85c716f2005-07-08 05:02:13 +0000711 for (std::vector<Path>::iterator I = list.begin(); I != list.end();
Reid Spencer1cf2d042005-07-07 23:35:23 +0000712 ++I) {
713 Path &aPath = *I;
Reid Spencera229c5c2005-07-08 03:08:58 +0000714 aPath.eraseFromDisk(true);
Reid Spencer1cf2d042005-07-07 23:35:23 +0000715 }
716 } else {
717 if (GetLastError() != ERROR_FILE_NOT_FOUND)
Reid Spencer05545752006-08-25 21:37:17 +0000718 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
Reid Spencer1cf2d042005-07-07 23:35:23 +0000719 }
720 }
721
722 pathname[lastchar] = 0;
723 if (!RemoveDirectory(pathname))
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000724 return MakeErrMsg(ErrStr,
Reid Spencer05545752006-08-25 21:37:17 +0000725 std::string(pathname) + ": Can't destroy directory: ");
Chris Lattner0c332312006-07-28 22:29:50 +0000726 return false;
Jeff Cohen31102892007-04-07 20:47:27 +0000727 } else {
728 // Read-only files cannot be deleted on Windows. Must remove the read-only
729 // attribute first.
730 if (fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
731 if (!SetFileAttributes(path.c_str(),
732 fi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
733 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
734 }
735
736 if (!DeleteFile(path.c_str()))
737 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
738 return false;
739 }
Reid Spencerb016a372004-09-15 05:49:50 +0000740}
741
Reid Spencer3b0cc782004-12-14 18:42:13 +0000742bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
Reid Spencer3b0cc782004-12-14 18:42:13 +0000743 assert(len < 1024 && "Request for magic string too long");
Michael J. Spencer1211d432010-08-31 06:36:33 +0000744 char* buf = reinterpret_cast<char*>(alloca(len));
Jeff Cohen51b8d212004-12-31 19:01:08 +0000745
746 HANDLE h = CreateFile(path.c_str(),
747 GENERIC_READ,
748 FILE_SHARE_READ,
749 NULL,
750 OPEN_EXISTING,
751 FILE_ATTRIBUTE_NORMAL,
752 NULL);
753 if (h == INVALID_HANDLE_VALUE)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000754 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000755
756 DWORD nRead = 0;
757 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
758 CloseHandle(h);
759
760 if (!ret || nRead != len)
Reid Spencer3b0cc782004-12-14 18:42:13 +0000761 return false;
Jeff Cohen51b8d212004-12-31 19:01:08 +0000762
Michael J. Spencer1211d432010-08-31 06:36:33 +0000763 Magic = std::string(buf, len);
Reid Spencer3b0cc782004-12-14 18:42:13 +0000764 return true;
765}
766
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000767bool
Reid Spencer5a060772006-08-23 07:30:48 +0000768Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
Francois Pichet3eddd982010-09-30 00:44:58 +0000769 if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING))
770 return MakeErrMsg(ErrMsg, "Can't move '" + path + "' to '" + newName.path
771 + "': ");
772 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000773}
774
775bool
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000776Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrMsg) const {
Jeff Cohen966fa412005-07-09 18:42:49 +0000777 // FIXME: should work on directories also.
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000778 if (!si.isFile) {
779 return true;
780 }
Kovarththanan Rajaratnam16ceb3a2010-03-12 14:17:24 +0000781
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000782 HANDLE h = CreateFile(path.c_str(),
783 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
784 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
Jeff Cohend40a7de2004-12-31 05:07:26 +0000785 NULL,
786 OPEN_EXISTING,
787 FILE_ATTRIBUTE_NORMAL,
788 NULL);
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000789 if (h == INVALID_HANDLE_VALUE)
Chris Lattner1bebfb52006-07-28 22:36:17 +0000790 return true;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000791
792 BY_HANDLE_FILE_INFORMATION bhfi;
793 if (!GetFileInformationByHandle(h, &bhfi)) {
Jeff Cohen51b8d212004-12-31 19:01:08 +0000794 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000795 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000796 SetLastError(err);
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000797 return MakeErrMsg(ErrMsg, path + ": GetFileInformationByHandle: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000798 }
799
Michael J. Spencer44edb0b2010-08-28 16:39:32 +0000800 ULARGE_INTEGER ui;
801 ui.QuadPart = si.modTime.toWin32Time();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000802 FILETIME ft;
Michael J. Spencer44edb0b2010-08-28 16:39:32 +0000803 ft.dwLowDateTime = ui.LowPart;
804 ft.dwHighDateTime = ui.HighPart;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000805 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000806 DWORD err = GetLastError();
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000807 CloseHandle(h);
Jeff Cohen51b8d212004-12-31 19:01:08 +0000808 if (!ret) {
809 SetLastError(err);
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000810 return MakeErrMsg(ErrMsg, path + ": SetFileTime: ");
Jeff Cohen51b8d212004-12-31 19:01:08 +0000811 }
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000812
813 // Best we can do with Unix permission bits is to interpret the owner
814 // writable bit.
815 if (si.mode & 0200) {
816 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
817 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000818 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000819 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000820 }
821 } else {
822 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
823 if (!SetFileAttributes(path.c_str(),
Jeff Cohend40a7de2004-12-31 05:07:26 +0000824 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000825 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000826 }
827 }
828
Chris Lattner1bebfb52006-07-28 22:36:17 +0000829 return false;
Jeff Cohenebcb9b32004-12-31 04:39:07 +0000830}
831
Reid Spencer30300992006-08-24 18:58:37 +0000832bool
833CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg) {
Jeff Cohencb652552004-12-24 02:38:34 +0000834 // Can't use CopyFile macro defined in Windows.h because it would mess up the
835 // above line. We use the expansion it would have in a non-UNICODE build.
836 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
Chris Lattner74382b72009-08-23 22:45:37 +0000837 return MakeErrMsg(ErrMsg, "Can't copy '" + Src.str() +
838 "' to '" + Dest.str() + "': ");
Reid Spencer30300992006-08-24 18:58:37 +0000839 return false;
Reid Spencerc29befb2004-12-15 01:50:13 +0000840}
841
Reid Spencer30300992006-08-24 18:58:37 +0000842bool
843Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000844 if (reuse_current && !exists())
Reid Spencer30300992006-08-24 18:58:37 +0000845 return false; // File doesn't exist already, just use it!
Reid Spencerc29befb2004-12-15 01:50:13 +0000846
Jeff Cohen9437bb62005-01-27 03:49:03 +0000847 // Reserve space for -XXXXXX at the end.
848 char *FNBuffer = (char*) alloca(path.size()+8);
849 unsigned offset = path.size();
850 path.copy(FNBuffer, offset);
Reid Spencerc29befb2004-12-15 01:50:13 +0000851
Jeff Cohen966fa412005-07-09 18:42:49 +0000852 // Find a numeric suffix that isn't used by an existing file. Assume there
853 // won't be more than 1 million files with the same prefix. Probably a safe
854 // bet.
Jeff Cohen9437bb62005-01-27 03:49:03 +0000855 static unsigned FCounter = 0;
856 do {
857 sprintf(FNBuffer+offset, "-%06u", FCounter);
858 if (++FCounter > 999999)
859 FCounter = 0;
860 path = FNBuffer;
861 } while (exists());
Reid Spencer30300992006-08-24 18:58:37 +0000862 return false;
Reid Spencerc29befb2004-12-15 01:50:13 +0000863}
864
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000865bool
Reid Spencer30300992006-08-24 18:58:37 +0000866Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000867 // Make this into a unique file name
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000868 makeUnique(reuse_current, ErrMsg);
Jeff Cohen9437bb62005-01-27 03:49:03 +0000869
870 // Now go and create it
871 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
872 FILE_ATTRIBUTE_NORMAL, NULL);
873 if (h == INVALID_HANDLE_VALUE)
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000874 return MakeErrMsg(ErrMsg, path + ": can't create file");
Jeff Cohen9437bb62005-01-27 03:49:03 +0000875
876 CloseHandle(h);
Reid Spencer30300992006-08-24 18:58:37 +0000877 return false;
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000878}
879
Chris Lattner799ed102008-04-01 06:00:12 +0000880/// MapInFilePages - Not yet implemented on win32.
881const char *Path::MapInFilePages(int FD, uint64_t FileSize) {
882 return 0;
883}
884
885/// MapInFilePages - Not yet implemented on win32.
886void Path::UnMapFilePages(const char *Base, uint64_t FileSize) {
887 assert(0 && "NOT IMPLEMENTED");
888}
889
Reid Spencerb016a372004-09-15 05:49:50 +0000890}
Reid Spencercbad7012004-09-11 04:59:30 +0000891}