Argyrios Kyrtzidis | 4bd3225 | 2008-06-16 10:14:09 +0000 | [diff] [blame] | 1 | //===- llvm/System/Win32/Path.cpp - Win32 Path Implementation ---*- C++ -*-===// |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the Win32 specific implementation of the Path class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | //===----------------------------------------------------------------------===// |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 15 | //=== WARNING: Implementation here must contain only generic Win32 code that |
| 16 | //=== is guaranteed to work on *all* Win32 variants. |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 19 | #include "Win32.h" |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 20 | #include <malloc.h> |
Chris Lattner | c23c1fc | 2009-04-01 02:03:38 +0000 | [diff] [blame] | 21 | #include <cstdio> |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 22 | |
Jeff Cohen | cb65255 | 2004-12-24 02:38:34 +0000 | [diff] [blame] | 23 | // We need to undo a macro defined in Windows.h, otherwise we won't compile: |
| 24 | #undef CopyFile |
Anton Korobeynikov | 64ddbe4 | 2007-12-22 14:26:49 +0000 | [diff] [blame] | 25 | #undef GetCurrentDirectory |
Jeff Cohen | cb65255 | 2004-12-24 02:38:34 +0000 | [diff] [blame] | 26 | |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 27 | // 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 Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 37 | static 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 Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 43 | namespace llvm { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 44 | namespace sys { |
Mikhail Glushenkov | f5a95ce | 2010-11-02 20:32:26 +0000 | [diff] [blame] | 45 | |
Chris Lattner | a17fa28 | 2008-03-13 05:17:59 +0000 | [diff] [blame] | 46 | const char PathSeparator = ';'; |
Chris Lattner | e1b332a | 2008-02-27 06:17:10 +0000 | [diff] [blame] | 47 | |
Mikhail Glushenkov | f5a95ce | 2010-11-02 20:32:26 +0000 | [diff] [blame] | 48 | StringRef Path::GetEXESuffix() { |
| 49 | return "exe"; |
| 50 | } |
| 51 | |
Daniel Dunbar | 1edcafe | 2009-12-18 19:59:48 +0000 | [diff] [blame] | 52 | Path::Path(llvm::StringRef p) |
Nick Lewycky | fff116f | 2008-05-11 17:37:40 +0000 | [diff] [blame] | 53 | : path(p) { |
| 54 | FlipBackSlashes(path); |
| 55 | } |
| 56 | |
| 57 | Path::Path(const char *StrStart, unsigned StrLen) |
| 58 | : path(StrStart, StrLen) { |
| 59 | FlipBackSlashes(path); |
| 60 | } |
| 61 | |
Chris Lattner | 0eab5e2 | 2008-08-11 23:39:47 +0000 | [diff] [blame] | 62 | Path& |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 63 | Path::operator=(StringRef that) { |
| 64 | path.assign(that.data(), that.size()); |
Chris Lattner | 0eab5e2 | 2008-08-11 23:39:47 +0000 | [diff] [blame] | 65 | FlipBackSlashes(path); |
| 66 | return *this; |
| 67 | } |
| 68 | |
Michael J. Spencer | a9cf7b8 | 2010-10-20 15:23:58 +0000 | [diff] [blame] | 69 | // push_back 0 on create, and pop_back on delete. |
| 70 | struct ScopedNullTerminator { |
| 71 | std::string &str; |
| 72 | ScopedNullTerminator(std::string &s) : str(s) { str.push_back(0); } |
Michael J. Spencer | 9f608b1 | 2010-10-20 16:00:45 +0000 | [diff] [blame] | 73 | ~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. Spencer | a9cf7b8 | 2010-10-20 15:23:58 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 81 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 82 | Path::isValid() const { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 83 | if (path.empty()) |
| 84 | return false; |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 85 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 86 | // 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. Spencer | a9cf7b8 | 2010-10-20 15:23:58 +0000 | [diff] [blame] | 89 | // This code assumes that path is null terminated, so make sure it is. |
| 90 | ScopedNullTerminator snt(path); |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 91 | size_t pos = path.rfind(':',len); |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 92 | size_t rootslash = 0; |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 93 | if (pos != std::string::npos) { |
| 94 | if (pos != 1 || !isalpha(path[0]) || len < 3) |
| 95 | return false; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 96 | rootslash = 2; |
| 97 | } |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 98 | |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 99 | // 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 Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 104 | } |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 105 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 106 | // 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 Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 112 | |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 113 | // Remove trailing slash, unless it's a root slash. |
| 114 | if (len > rootslash+1 && path[len-1] == '/') |
| 115 | path.erase(--len); |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 116 | |
Jeff Cohen | 3bbbcc1 | 2005-01-14 04:09:39 +0000 | [diff] [blame] | 117 | // 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 Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 124 | |
Jeff Cohen | 3bbbcc1 | 2005-01-14 04:09:39 +0000 | [diff] [blame] | 125 | // 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 Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 140 | |
| 141 | return true; |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Daniel Dunbar | a00e85c | 2009-07-12 20:23:56 +0000 | [diff] [blame] | 144 | void Path::makeAbsolute() { |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 145 | TCHAR FullPath[MAX_PATH + 1] = {0}; |
Daniel Dunbar | a00e85c | 2009-07-12 20:23:56 +0000 | [diff] [blame] | 146 | 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 Dunbar | 2749b3e | 2009-07-26 21:16:42 +0000 | [diff] [blame] | 154 | assert(0 && "Unable to make absolute path!"); |
Daniel Dunbar | a00e85c | 2009-07-12 20:23:56 +0000 | [diff] [blame] | 155 | } else if (RetLength > MAX_PATH) { |
| 156 | // FIXME: Report too small buffer (needed RetLength bytes). |
Daniel Dunbar | 2749b3e | 2009-07-26 21:16:42 +0000 | [diff] [blame] | 157 | assert(0 && "Unable to make absolute path!"); |
Daniel Dunbar | a00e85c | 2009-07-12 20:23:56 +0000 | [diff] [blame] | 158 | } else { |
| 159 | path = FullPath; |
| 160 | } |
| 161 | } |
| 162 | |
Chris Lattner | 88d7e40 | 2009-06-15 04:17:07 +0000 | [diff] [blame] | 163 | bool |
| 164 | Path::isAbsolute(const char *NameStart, unsigned NameLen) { |
| 165 | assert(NameStart); |
Daniel Dunbar | a00e85c | 2009-07-12 20:23:56 +0000 | [diff] [blame] | 166 | // FIXME: This does not handle correctly an absolute path starting from |
| 167 | // a drive letter or in UNC format. |
Chris Lattner | 88d7e40 | 2009-06-15 04:17:07 +0000 | [diff] [blame] | 168 | switch (NameLen) { |
| 169 | case 0: |
| 170 | return false; |
| 171 | case 1: |
| 172 | case 2: |
| 173 | return NameStart[0] == '/'; |
| 174 | default: |
Mikhail Glushenkov | 0f2ec15 | 2010-11-02 20:32:31 +0000 | [diff] [blame] | 175 | return |
| 176 | (NameStart[0] == '/' || (NameStart[1] == ':' && NameStart[2] == '/')) || |
| 177 | (NameStart[0] == '\\' || (NameStart[1] == ':' && NameStart[2] == '\\')); |
Chris Lattner | 88d7e40 | 2009-06-15 04:17:07 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 181 | bool |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 182 | Path::isAbsolute() const { |
Daniel Dunbar | a00e85c | 2009-07-12 20:23:56 +0000 | [diff] [blame] | 183 | // FIXME: This does not handle correctly an absolute path starting from |
| 184 | // a drive letter or in UNC format. |
Jeff Cohen | 84892be | 2007-03-29 17:27:38 +0000 | [diff] [blame] | 185 | 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 Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 194 | } |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 195 | |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 196 | static Path *TempDirectory; |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 197 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 198 | Path |
Reid Spencer | cab0e43 | 2006-08-22 22:46:39 +0000 | [diff] [blame] | 199 | Path::GetTemporaryDirectory(std::string* ErrMsg) { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 200 | if (TempDirectory) |
| 201 | return *TempDirectory; |
| 202 | |
| 203 | char pathname[MAX_PATH]; |
Reid Spencer | cab0e43 | 2006-08-22 22:46:39 +0000 | [diff] [blame] | 204 | if (!GetTempPath(MAX_PATH, pathname)) { |
| 205 | if (ErrMsg) |
| 206 | *ErrMsg = "Can't determine temporary directory"; |
| 207 | return Path(); |
| 208 | } |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 209 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 210 | Path result; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 211 | result.set(pathname); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 212 | |
| 213 | // Append a subdirectory passed on our process id so multiple LLVMs don't |
| 214 | // step on each other's toes. |
Jeff Cohen | d41b30d | 2006-11-05 19:31:28 +0000 | [diff] [blame] | 215 | #ifdef __MINGW32__ |
| 216 | // Mingw's Win32 header files are broken. |
Reid Spencer | ab4d9b0 | 2006-06-08 18:08:43 +0000 | [diff] [blame] | 217 | sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId())); |
Jeff Cohen | d41b30d | 2006-11-05 19:31:28 +0000 | [diff] [blame] | 218 | #else |
| 219 | sprintf(pathname, "LLVM_%u", GetCurrentProcessId()); |
| 220 | #endif |
Jeff Cohen | edb9d6b | 2005-07-08 02:48:42 +0000 | [diff] [blame] | 221 | result.appendComponent(pathname); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 222 | |
| 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 Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 225 | result.eraseFromDisk(true); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 226 | |
| 227 | // And finally (re-)create the empty directory. |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 228 | result.createDirectoryOnDisk(false); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 229 | TempDirectory = new Path(result); |
| 230 | return *TempDirectory; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 233 | // FIXME: the following set of functions don't map to Windows very well. |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 234 | Path |
| 235 | Path::GetRootDirectory() { |
Michael J. Spencer | 87e0697 | 2010-11-09 15:10:45 +0000 | [diff] [blame] | 236 | // 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 Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 241 | void |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 242 | Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) { |
Michael J. Spencer | eebe970 | 2010-11-09 15:11:19 +0000 | [diff] [blame^] | 243 | 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 Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 271 | void |
Gabor Greif | db5565a | 2007-07-06 20:28:40 +0000 | [diff] [blame] | 272 | Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) { |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 273 | char * env_var = getenv("LLVM_LIB_SEARCH_PATH"); |
| 274 | if (env_var != 0) { |
| 275 | getPathList(env_var,Paths); |
| 276 | } |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 277 | #ifdef LLVM_LIBDIR |
| 278 | { |
| 279 | Path tmpPath; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 280 | if (tmpPath.set(LLVM_LIBDIR)) |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 281 | if (tmpPath.canRead()) |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 282 | Paths.push_back(tmpPath); |
| 283 | } |
| 284 | #endif |
| 285 | GetSystemLibraryPaths(Paths); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | Path |
| 289 | Path::GetLLVMDefaultConfigDir() { |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 290 | // TODO: this isn't going to fly on Windows |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 291 | return Path("/etc/llvm"); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | Path |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 295 | Path::GetUserHomeDirectory() { |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 296 | // TODO: Typical Windows setup doesn't define HOME. |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 297 | const char* home = getenv("HOME"); |
| 298 | if (home) { |
| 299 | Path result; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 300 | if (result.set(home)) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 301 | return result; |
| 302 | } |
| 303 | return GetRootDirectory(); |
| 304 | } |
Ted Kremenek | 7920078 | 2007-12-18 22:07:33 +0000 | [diff] [blame] | 305 | |
| 306 | Path |
| 307 | Path::GetCurrentDirectory() { |
| 308 | char pathname[MAX_PATH]; |
Anton Korobeynikov | 64ddbe4 | 2007-12-22 14:26:49 +0000 | [diff] [blame] | 309 | ::GetCurrentDirectoryA(MAX_PATH,pathname); |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 310 | return Path(pathname); |
Ted Kremenek | 7920078 | 2007-12-18 22:07:33 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Chris Lattner | 1a09144 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 313 | /// GetMainExecutable - Return the path to the main executable, given the |
| 314 | /// value of argv[0] from program startup. |
| 315 | Path Path::GetMainExecutable(const char *argv0, void *MainAddr) { |
Chris Lattner | c5718d0 | 2009-06-15 05:38:04 +0000 | [diff] [blame] | 316 | char pathname[MAX_PATH]; |
| 317 | DWORD ret = ::GetModuleFileNameA(NULL, pathname, MAX_PATH); |
| 318 | return ret != MAX_PATH ? Path(pathname) : Path(); |
Chris Lattner | 1a09144 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Ted Kremenek | 7920078 | 2007-12-18 22:07:33 +0000 | [diff] [blame] | 321 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 322 | // FIXME: the above set of functions don't map to Windows very well. |
| 323 | |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 324 | |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 325 | StringRef Path::getDirname() const { |
| 326 | return getDirnameCharSep(path, "/"); |
Ted Kremenek | 9b01cc0 | 2008-04-07 22:01:32 +0000 | [diff] [blame] | 327 | } |
Ted Kremenek | cf55c8e | 2008-04-07 21:53:57 +0000 | [diff] [blame] | 328 | |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 329 | StringRef |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 330 | Path::getBasename() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 331 | // 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 Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 338 | size_t dot = path.rfind('.'); |
| 339 | if (dot == std::string::npos || dot < slash) |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 340 | return StringRef(path).substr(slash); |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 341 | else |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 342 | return StringRef(path).substr(slash, dot - slash); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 345 | StringRef |
Argyrios Kyrtzidis | fc19988 | 2008-06-15 15:15:19 +0000 | [diff] [blame] | 346 | Path::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 Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 356 | return StringRef(""); |
Argyrios Kyrtzidis | fc19988 | 2008-06-15 15:15:19 +0000 | [diff] [blame] | 357 | else |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 358 | return StringRef(path).substr(dot + 1); |
Argyrios Kyrtzidis | fc19988 | 2008-06-15 15:15:19 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 361 | bool |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 362 | Path::exists() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 363 | DWORD attr = GetFileAttributes(path.c_str()); |
| 364 | return attr != INVALID_FILE_ATTRIBUTES; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | bool |
Ted Kremenek | fd52711 | 2007-12-18 19:46:22 +0000 | [diff] [blame] | 368 | Path::isDirectory() const { |
| 369 | DWORD attr = GetFileAttributes(path.c_str()); |
| 370 | return (attr != INVALID_FILE_ATTRIBUTES) && |
| 371 | (attr & FILE_ATTRIBUTE_DIRECTORY); |
| 372 | } |
| 373 | |
| 374 | bool |
Rafael Espindola | 9e07a14 | 2010-11-07 04:36:50 +0000 | [diff] [blame] | 375 | Path::isSymLink() const { |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | bool |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 380 | Path::canRead() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 381 | // FIXME: take security attributes into account. |
| 382 | DWORD attr = GetFileAttributes(path.c_str()); |
| 383 | return attr != INVALID_FILE_ATTRIBUTES; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | bool |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 387 | Path::canWrite() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 388 | // FIXME: take security attributes into account. |
| 389 | DWORD attr = GetFileAttributes(path.c_str()); |
| 390 | return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | bool |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 394 | Path::canExecute() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 395 | // FIXME: take security attributes into account. |
| 396 | DWORD attr = GetFileAttributes(path.c_str()); |
| 397 | return attr != INVALID_FILE_ATTRIBUTES; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Edward O'Callaghan | d41e944 | 2009-11-24 15:19:10 +0000 | [diff] [blame] | 400 | bool |
Edward O'Callaghan | e49a8e4 | 2009-11-25 06:32:19 +0000 | [diff] [blame] | 401 | Path::isRegularFile() const { |
| 402 | if (isDirectory()) |
| 403 | return false; |
| 404 | return true; |
Edward O'Callaghan | d41e944 | 2009-11-24 15:19:10 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 407 | StringRef |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 408 | Path::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 Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 416 | // If the last character is a slash, we have a root directory |
| 417 | if (pos == path.length()-1) |
| 418 | return path; |
| 419 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 420 | // Return everything after the last slash |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 421 | return StringRef(path).substr(pos+1); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Reid Spencer | 8475ec0 | 2007-03-29 19:05:44 +0000 | [diff] [blame] | 424 | const FileStatus * |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 425 | PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const { |
| 426 | if (!fsIsValid || update) { |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 427 | WIN32_FILE_ATTRIBUTE_DATA fi; |
Reid Spencer | 8475ec0 | 2007-03-29 19:05:44 +0000 | [diff] [blame] | 428 | if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) { |
| 429 | MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) + |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 430 | ": Can't get status: "); |
Reid Spencer | 8475ec0 | 2007-03-29 19:05:44 +0000 | [diff] [blame] | 431 | return 0; |
| 432 | } |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 433 | |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 434 | status.fileSize = fi.nFileSizeHigh; |
| 435 | status.fileSize <<= sizeof(fi.nFileSizeHigh)*8; |
| 436 | status.fileSize += fi.nFileSizeLow; |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 437 | |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 438 | 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 Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 441 | |
Reid Spencer | 4031bef | 2007-03-29 17:00:31 +0000 | [diff] [blame] | 442 | // 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 Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 445 | status.uniqueID = 0; |
Reid Spencer | 4031bef | 2007-03-29 17:00:31 +0000 | [diff] [blame] | 446 | for (unsigned i = 0; i < path.length(); ++i) |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 447 | status.uniqueID += path[i]; |
Reid Spencer | 4031bef | 2007-03-29 17:00:31 +0000 | [diff] [blame] | 448 | |
Michael J. Spencer | 44edb0b | 2010-08-28 16:39:32 +0000 | [diff] [blame] | 449 | ULARGE_INTEGER ui; |
| 450 | ui.LowPart = fi.ftLastWriteTime.dwLowDateTime; |
| 451 | ui.HighPart = fi.ftLastWriteTime.dwHighDateTime; |
| 452 | status.modTime.fromWin32Time(ui.QuadPart); |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 453 | |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 454 | status.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
| 455 | fsIsValid = true; |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 456 | } |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 457 | return &status; |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 460 | bool Path::makeReadableOnDisk(std::string* ErrMsg) { |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 461 | // All files are readable on Windows (ignoring security attributes). |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 462 | return false; |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 465 | bool Path::makeWriteableOnDisk(std::string* ErrMsg) { |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 466 | DWORD attr = GetFileAttributes(path.c_str()); |
| 467 | |
| 468 | // If it doesn't exist, we're done. |
| 469 | if (attr == INVALID_FILE_ATTRIBUTES) |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 470 | return false; |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 471 | |
| 472 | if (attr & FILE_ATTRIBUTE_READONLY) { |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 473 | 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 Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 477 | } |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 478 | return false; |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 481 | bool Path::makeExecutableOnDisk(std::string* ErrMsg) { |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 482 | // All files are executable on Windows (ignoring security attributes). |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 483 | return false; |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 486 | bool |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 487 | Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const { |
Jeff Cohen | 3110289 | 2007-04-07 20:47:27 +0000 | [diff] [blame] | 488 | WIN32_FILE_ATTRIBUTE_DATA fi; |
| 489 | if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) { |
| 490 | MakeErrMsg(ErrMsg, path + ": can't get status of file"); |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 491 | return true; |
Jeff Cohen | 3110289 | 2007-04-07 20:47:27 +0000 | [diff] [blame] | 492 | } |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 493 | |
Jeff Cohen | 3110289 | 2007-04-07 20:47:27 +0000 | [diff] [blame] | 494 | if (!(fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 495 | if (ErrMsg) |
| 496 | *ErrMsg = path + ": not a directory"; |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 497 | return true; |
| 498 | } |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 499 | |
| 500 | result.clear(); |
| 501 | WIN32_FIND_DATA fd; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 502 | std::string searchpath = path; |
| 503 | if (path.size() == 0 || searchpath[path.size()-1] == '/') |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 504 | searchpath += "*"; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 505 | else |
| 506 | searchpath += "/*"; |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 507 | |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 508 | HANDLE h = FindFirstFile(searchpath.c_str(), &fd); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 509 | if (h == INVALID_HANDLE_VALUE) { |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 510 | if (GetLastError() == ERROR_FILE_NOT_FOUND) |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 511 | return true; // not really an error, now is it? |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 512 | MakeErrMsg(ErrMsg, path + ": Can't read directory: "); |
| 513 | return true; |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | do { |
Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 517 | if (fd.cFileName[0] == '.') |
| 518 | continue; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 519 | Path aPath(path); |
| 520 | aPath.appendComponent(&fd.cFileName[0]); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 521 | result.insert(aPath); |
| 522 | } while (FindNextFile(h, &fd)); |
| 523 | |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 524 | DWORD err = GetLastError(); |
| 525 | FindClose(h); |
| 526 | if (err != ERROR_NO_MORE_FILES) { |
| 527 | SetLastError(err); |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 528 | MakeErrMsg(ErrMsg, path + ": Can't read directory: "); |
| 529 | return true; |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 530 | } |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 531 | return false; |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | bool |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 535 | Path::set(StringRef a_path) { |
Dan Gohman | 3035959 | 2008-01-29 13:02:09 +0000 | [diff] [blame] | 536 | if (a_path.empty()) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 537 | return false; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 538 | std::string save(path); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 539 | path = a_path; |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 540 | FlipBackSlashes(path); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 541 | if (!isValid()) { |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 542 | path = save; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 543 | return false; |
| 544 | } |
| 545 | return true; |
| 546 | } |
| 547 | |
| 548 | bool |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 549 | Path::appendComponent(StringRef name) { |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 550 | if (name.empty()) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 551 | return false; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 552 | std::string save(path); |
| 553 | if (!path.empty()) { |
| 554 | size_t last = path.size() - 1; |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 555 | if (path[last] != '/') |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 556 | path += '/'; |
| 557 | } |
| 558 | path += name; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 559 | if (!isValid()) { |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 560 | path = save; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 561 | return false; |
| 562 | } |
| 563 | return true; |
| 564 | } |
| 565 | |
| 566 | bool |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 567 | Path::eraseComponent() { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 568 | size_t slashpos = path.rfind('/',path.size()); |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 569 | if (slashpos == path.size() - 1 || slashpos == std::string::npos) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 570 | return false; |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 571 | std::string save(path); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 572 | path.erase(slashpos); |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 573 | if (!isValid()) { |
| 574 | path = save; |
| 575 | return false; |
| 576 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 577 | return true; |
| 578 | } |
| 579 | |
| 580 | bool |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 581 | Path::eraseSuffix() { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 582 | size_t dotpos = path.rfind('.',path.size()); |
| 583 | size_t slashpos = path.rfind('/',path.size()); |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 584 | if (dotpos != std::string::npos) { |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 585 | if (slashpos == std::string::npos || dotpos > slashpos+1) { |
| 586 | std::string save(path); |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 587 | path.erase(dotpos, path.size()-dotpos); |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 588 | if (!isValid()) { |
| 589 | path = save; |
| 590 | return false; |
| 591 | } |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 592 | return true; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 593 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 594 | } |
| 595 | return false; |
| 596 | } |
| 597 | |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 598 | inline 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 Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 604 | bool |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 605 | Path::createDirectoryOnDisk(bool create_parents, std::string* ErrMsg) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 606 | // Get a writeable copy of the path name |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 607 | size_t len = path.length(); |
| 608 | char *pathname = reinterpret_cast<char *>(_alloca(len+2)); |
| 609 | path.copy(pathname, len); |
| 610 | pathname[len] = 0; |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 611 | |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 612 | // Make sure it ends with a slash. |
| 613 | if (len == 0 || pathname[len - 1] != '/') { |
| 614 | pathname[len] = '/'; |
| 615 | pathname[++len] = 0; |
| 616 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 617 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 618 | // 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 Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 624 | return PathMsg(ErrMsg, pathname, "badly formed remote directory"); |
| 625 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 626 | // Skip share name. |
| 627 | next = strchr(next+1, '/'); |
| 628 | if (next == NULL) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 629 | return PathMsg(ErrMsg, pathname,"badly formed remote directory"); |
| 630 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 631 | next++; |
| 632 | if (*next == 0) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 633 | return PathMsg(ErrMsg, pathname, "badly formed remote directory"); |
| 634 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 635 | } else { |
| 636 | if (pathname[1] == ':') |
| 637 | next += 2; // skip drive letter |
| 638 | if (*next == '/') |
| 639 | next++; // skip root directory |
| 640 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 641 | |
| 642 | // If we're supposed to create intermediate directories |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 643 | if (create_parents) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 644 | // Loop through the directory components until we're done |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 645 | while (*next) { |
| 646 | next = strchr(next, '/'); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 647 | *next = 0; |
Benjamin Kramer | e9684c6 | 2009-11-05 14:32:40 +0000 | [diff] [blame] | 648 | if (!CreateDirectory(pathname, NULL) && |
| 649 | GetLastError() != ERROR_ALREADY_EXISTS) |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 650 | return MakeErrMsg(ErrMsg, |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 651 | std::string(pathname) + ": Can't create directory: "); |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 652 | *next++ = '/'; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 653 | } |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 654 | } else { |
| 655 | // Drop trailing slash. |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 656 | pathname[len-1] = 0; |
Benjamin Kramer | e9684c6 | 2009-11-05 14:32:40 +0000 | [diff] [blame] | 657 | if (!CreateDirectory(pathname, NULL) && |
| 658 | GetLastError() != ERROR_ALREADY_EXISTS) { |
Mikhail Glushenkov | 0f2ec15 | 2010-11-02 20:32:31 +0000 | [diff] [blame] | 659 | return MakeErrMsg(ErrMsg, std::string(pathname) + |
| 660 | ": Can't create directory: "); |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 661 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 662 | } |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 663 | return false; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | bool |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 667 | Path::createFileOnDisk(std::string* ErrMsg) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 668 | // Create the file |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 669 | HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW, |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 670 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 671 | if (h == INVALID_HANDLE_VALUE) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 672 | return MakeErrMsg(ErrMsg, path + ": Can't create file: "); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 673 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 674 | CloseHandle(h); |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 675 | return false; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | bool |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 679 | Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const { |
Jeff Cohen | 3110289 | 2007-04-07 20:47:27 +0000 | [diff] [blame] | 680 | WIN32_FILE_ATTRIBUTE_DATA fi; |
| 681 | if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) |
| 682 | return true; |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 683 | |
Jeff Cohen | 3110289 | 2007-04-07 20:47:27 +0000 | [diff] [blame] | 684 | if (fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 685 | // If it doesn't exist, we're done. |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 686 | if (!exists()) |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 687 | return false; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 688 | |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 689 | char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3)); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 690 | int lastchar = path.length() - 1 ; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 691 | path.copy(pathname, lastchar+1); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 692 | |
| 693 | // Make path end with '/*'. |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 694 | if (pathname[lastchar] != '/') |
| 695 | pathname[++lastchar] = '/'; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 696 | 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 Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 715 | Path aPath(path); |
| 716 | aPath.appendComponent(&fd.cFileName[0]); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 717 | 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 Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 724 | return MakeErrMsg(ErrStr, path + ": Can't read directory: "); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 727 | for (std::vector<Path>::iterator I = list.begin(); I != list.end(); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 728 | ++I) { |
| 729 | Path &aPath = *I; |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 730 | aPath.eraseFromDisk(true); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 731 | } |
| 732 | } else { |
| 733 | if (GetLastError() != ERROR_FILE_NOT_FOUND) |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 734 | return MakeErrMsg(ErrStr, path + ": Can't read directory: "); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
| 738 | pathname[lastchar] = 0; |
| 739 | if (!RemoveDirectory(pathname)) |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 740 | return MakeErrMsg(ErrStr, |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 741 | std::string(pathname) + ": Can't destroy directory: "); |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 742 | return false; |
Jeff Cohen | 3110289 | 2007-04-07 20:47:27 +0000 | [diff] [blame] | 743 | } 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 Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 758 | bool Path::getMagicNumber(std::string& Magic, unsigned len) const { |
Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 759 | assert(len < 1024 && "Request for magic string too long"); |
Michael J. Spencer | 1211d43 | 2010-08-31 06:36:33 +0000 | [diff] [blame] | 760 | char* buf = reinterpret_cast<char*>(alloca(len)); |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 761 | |
| 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 Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 770 | return false; |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 771 | |
| 772 | DWORD nRead = 0; |
| 773 | BOOL ret = ReadFile(h, buf, len, &nRead, NULL); |
| 774 | CloseHandle(h); |
| 775 | |
| 776 | if (!ret || nRead != len) |
Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 777 | return false; |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 778 | |
Michael J. Spencer | 1211d43 | 2010-08-31 06:36:33 +0000 | [diff] [blame] | 779 | Magic = std::string(buf, len); |
Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 780 | return true; |
| 781 | } |
| 782 | |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 783 | bool |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 784 | Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) { |
Francois Pichet | 3eddd98 | 2010-09-30 00:44:58 +0000 | [diff] [blame] | 785 | 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 Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | bool |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 792 | Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrMsg) const { |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 793 | // FIXME: should work on directories also. |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 794 | if (!si.isFile) { |
| 795 | return true; |
| 796 | } |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 797 | |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 798 | HANDLE h = CreateFile(path.c_str(), |
| 799 | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES, |
| 800 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 801 | NULL, |
| 802 | OPEN_EXISTING, |
| 803 | FILE_ATTRIBUTE_NORMAL, |
| 804 | NULL); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 805 | if (h == INVALID_HANDLE_VALUE) |
Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 806 | return true; |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 807 | |
| 808 | BY_HANDLE_FILE_INFORMATION bhfi; |
| 809 | if (!GetFileInformationByHandle(h, &bhfi)) { |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 810 | DWORD err = GetLastError(); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 811 | CloseHandle(h); |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 812 | SetLastError(err); |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 813 | return MakeErrMsg(ErrMsg, path + ": GetFileInformationByHandle: "); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Michael J. Spencer | 44edb0b | 2010-08-28 16:39:32 +0000 | [diff] [blame] | 816 | ULARGE_INTEGER ui; |
| 817 | ui.QuadPart = si.modTime.toWin32Time(); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 818 | FILETIME ft; |
Michael J. Spencer | 44edb0b | 2010-08-28 16:39:32 +0000 | [diff] [blame] | 819 | ft.dwLowDateTime = ui.LowPart; |
| 820 | ft.dwHighDateTime = ui.HighPart; |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 821 | BOOL ret = SetFileTime(h, NULL, &ft, &ft); |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 822 | DWORD err = GetLastError(); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 823 | CloseHandle(h); |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 824 | if (!ret) { |
| 825 | SetLastError(err); |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 826 | return MakeErrMsg(ErrMsg, path + ": SetFileTime: "); |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 827 | } |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 828 | |
| 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 Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 834 | bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY)) |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 835 | return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: "); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 836 | } |
| 837 | } else { |
| 838 | if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) { |
| 839 | if (!SetFileAttributes(path.c_str(), |
Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 840 | bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY)) |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 841 | return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: "); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 845 | return false; |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 848 | bool |
| 849 | CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg) { |
Jeff Cohen | cb65255 | 2004-12-24 02:38:34 +0000 | [diff] [blame] | 850 | // 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 Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 853 | return MakeErrMsg(ErrMsg, "Can't copy '" + Src.str() + |
| 854 | "' to '" + Dest.str() + "': "); |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 855 | return false; |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 858 | bool |
| 859 | Path::makeUnique(bool reuse_current, std::string* ErrMsg) { |
Reid Spencer | 07f9f4e | 2004-12-15 08:32:45 +0000 | [diff] [blame] | 860 | if (reuse_current && !exists()) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 861 | return false; // File doesn't exist already, just use it! |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 862 | |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 863 | // 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 Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 867 | |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 868 | // 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 Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 871 | 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 Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 878 | return false; |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Reid Spencer | 07f9f4e | 2004-12-15 08:32:45 +0000 | [diff] [blame] | 881 | bool |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 882 | Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) { |
Reid Spencer | 07f9f4e | 2004-12-15 08:32:45 +0000 | [diff] [blame] | 883 | // Make this into a unique file name |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 884 | makeUnique(reuse_current, ErrMsg); |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 885 | |
| 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 Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 890 | return MakeErrMsg(ErrMsg, path + ": can't create file"); |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 891 | |
| 892 | CloseHandle(h); |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 893 | return false; |
Reid Spencer | 07f9f4e | 2004-12-15 08:32:45 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Chris Lattner | 799ed10 | 2008-04-01 06:00:12 +0000 | [diff] [blame] | 896 | /// MapInFilePages - Not yet implemented on win32. |
| 897 | const char *Path::MapInFilePages(int FD, uint64_t FileSize) { |
| 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | /// MapInFilePages - Not yet implemented on win32. |
| 902 | void Path::UnMapFilePages(const char *Base, uint64_t FileSize) { |
| 903 | assert(0 && "NOT IMPLEMENTED"); |
| 904 | } |
| 905 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 906 | } |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 907 | } |