Charles Davis | 53ca1f3 | 2010-11-29 19:44:50 +0000 | [diff] [blame] | 1 | //===- llvm/Support/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 | |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 19 | #include "Windows.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 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 69 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 70 | Path::isValid() const { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 71 | if (path.empty()) |
| 72 | return false; |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 73 | |
NAKAMURA Takumi | 5d07007 | 2011-10-24 03:27:19 +0000 | [diff] [blame] | 74 | size_t len = path.size(); |
| 75 | // If there is a null character, it and all its successors are ignored. |
| 76 | size_t pos = path.find_first_of('\0'); |
| 77 | if (pos != std::string::npos) |
| 78 | len = pos; |
| 79 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 80 | // If there is a colon, it must be the second character, preceded by a letter |
| 81 | // and followed by something. |
NAKAMURA Takumi | 5d07007 | 2011-10-24 03:27:19 +0000 | [diff] [blame] | 82 | pos = path.rfind(':',len); |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 83 | size_t rootslash = 0; |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 84 | if (pos != std::string::npos) { |
| 85 | if (pos != 1 || !isalpha(path[0]) || len < 3) |
| 86 | return false; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 87 | rootslash = 2; |
| 88 | } |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 89 | |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 90 | // Look for a UNC path, and if found adjust our notion of the root slash. |
| 91 | if (len > 3 && path[0] == '/' && path[1] == '/') { |
| 92 | rootslash = path.find('/', 2); |
| 93 | if (rootslash == std::string::npos) |
| 94 | rootslash = 0; |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 95 | } |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 96 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 97 | // Check for illegal characters. |
| 98 | if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012" |
| 99 | "\013\014\015\016\017\020\021\022\023\024\025\026" |
| 100 | "\027\030\031\032\033\034\035\036\037") |
| 101 | != std::string::npos) |
| 102 | return false; |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 103 | |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 104 | // Remove trailing slash, unless it's a root slash. |
| 105 | if (len > rootslash+1 && path[len-1] == '/') |
| 106 | path.erase(--len); |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 107 | |
Jeff Cohen | 3bbbcc1 | 2005-01-14 04:09:39 +0000 | [diff] [blame] | 108 | // Check each component for legality. |
| 109 | for (pos = 0; pos < len; ++pos) { |
| 110 | // A component may not end in a space. |
| 111 | if (path[pos] == ' ') { |
NAKAMURA Takumi | 5d07007 | 2011-10-24 03:27:19 +0000 | [diff] [blame] | 112 | if (pos+1 == len || path[pos+1] == '/' || path[pos+1] == '\0') |
Jeff Cohen | 3bbbcc1 | 2005-01-14 04:09:39 +0000 | [diff] [blame] | 113 | return false; |
| 114 | } |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 115 | |
Jeff Cohen | 3bbbcc1 | 2005-01-14 04:09:39 +0000 | [diff] [blame] | 116 | // A component may not end in a period. |
| 117 | if (path[pos] == '.') { |
NAKAMURA Takumi | 5d07007 | 2011-10-24 03:27:19 +0000 | [diff] [blame] | 118 | if (pos+1 == len || path[pos+1] == '/') { |
Jeff Cohen | 3bbbcc1 | 2005-01-14 04:09:39 +0000 | [diff] [blame] | 119 | // Unless it is the pseudo-directory "."... |
| 120 | if (pos == 0 || path[pos-1] == '/' || path[pos-1] == ':') |
| 121 | return true; |
| 122 | // or "..". |
| 123 | if (pos > 0 && path[pos-1] == '.') { |
| 124 | if (pos == 1 || path[pos-2] == '/' || path[pos-2] == ':') |
| 125 | return true; |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | } |
| 130 | } |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 131 | |
| 132 | return true; |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Daniel Dunbar | a00e85c | 2009-07-12 20:23:56 +0000 | [diff] [blame] | 135 | void Path::makeAbsolute() { |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 136 | TCHAR FullPath[MAX_PATH + 1] = {0}; |
Daniel Dunbar | a00e85c | 2009-07-12 20:23:56 +0000 | [diff] [blame] | 137 | LPTSTR FilePart = NULL; |
| 138 | |
| 139 | DWORD RetLength = ::GetFullPathNameA(path.c_str(), |
| 140 | sizeof(FullPath)/sizeof(FullPath[0]), |
| 141 | FullPath, &FilePart); |
| 142 | |
| 143 | if (0 == RetLength) { |
| 144 | // FIXME: Report the error GetLastError() |
Daniel Dunbar | 2749b3e | 2009-07-26 21:16:42 +0000 | [diff] [blame] | 145 | assert(0 && "Unable to make absolute path!"); |
Daniel Dunbar | a00e85c | 2009-07-12 20:23:56 +0000 | [diff] [blame] | 146 | } else if (RetLength > MAX_PATH) { |
| 147 | // FIXME: Report too small buffer (needed RetLength bytes). |
Daniel Dunbar | 2749b3e | 2009-07-26 21:16:42 +0000 | [diff] [blame] | 148 | assert(0 && "Unable to make absolute path!"); |
Daniel Dunbar | a00e85c | 2009-07-12 20:23:56 +0000 | [diff] [blame] | 149 | } else { |
| 150 | path = FullPath; |
| 151 | } |
| 152 | } |
| 153 | |
Chris Lattner | 88d7e40 | 2009-06-15 04:17:07 +0000 | [diff] [blame] | 154 | bool |
| 155 | Path::isAbsolute(const char *NameStart, unsigned NameLen) { |
| 156 | assert(NameStart); |
Daniel Dunbar | a00e85c | 2009-07-12 20:23:56 +0000 | [diff] [blame] | 157 | // FIXME: This does not handle correctly an absolute path starting from |
| 158 | // a drive letter or in UNC format. |
Chris Lattner | 88d7e40 | 2009-06-15 04:17:07 +0000 | [diff] [blame] | 159 | switch (NameLen) { |
| 160 | case 0: |
| 161 | return false; |
| 162 | case 1: |
| 163 | case 2: |
| 164 | return NameStart[0] == '/'; |
| 165 | default: |
Mikhail Glushenkov | 0f2ec15 | 2010-11-02 20:32:31 +0000 | [diff] [blame] | 166 | return |
| 167 | (NameStart[0] == '/' || (NameStart[1] == ':' && NameStart[2] == '/')) || |
| 168 | (NameStart[0] == '\\' || (NameStart[1] == ':' && NameStart[2] == '\\')); |
Chris Lattner | 88d7e40 | 2009-06-15 04:17:07 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 172 | bool |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 173 | Path::isAbsolute() const { |
Daniel Dunbar | a00e85c | 2009-07-12 20:23:56 +0000 | [diff] [blame] | 174 | // FIXME: This does not handle correctly an absolute path starting from |
| 175 | // a drive letter or in UNC format. |
Jeff Cohen | 84892be | 2007-03-29 17:27:38 +0000 | [diff] [blame] | 176 | switch (path.length()) { |
| 177 | case 0: |
| 178 | return false; |
| 179 | case 1: |
| 180 | case 2: |
| 181 | return path[0] == '/'; |
| 182 | default: |
| 183 | return path[0] == '/' || (path[1] == ':' && path[2] == '/'); |
| 184 | } |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 185 | } |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 186 | |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 187 | static Path *TempDirectory; |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 188 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 189 | Path |
Reid Spencer | cab0e43 | 2006-08-22 22:46:39 +0000 | [diff] [blame] | 190 | Path::GetTemporaryDirectory(std::string* ErrMsg) { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 191 | if (TempDirectory) |
| 192 | return *TempDirectory; |
| 193 | |
| 194 | char pathname[MAX_PATH]; |
Reid Spencer | cab0e43 | 2006-08-22 22:46:39 +0000 | [diff] [blame] | 195 | if (!GetTempPath(MAX_PATH, pathname)) { |
| 196 | if (ErrMsg) |
| 197 | *ErrMsg = "Can't determine temporary directory"; |
| 198 | return Path(); |
| 199 | } |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 200 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 201 | Path result; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 202 | result.set(pathname); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 203 | |
| 204 | // Append a subdirectory passed on our process id so multiple LLVMs don't |
| 205 | // step on each other's toes. |
Jeff Cohen | d41b30d | 2006-11-05 19:31:28 +0000 | [diff] [blame] | 206 | #ifdef __MINGW32__ |
| 207 | // Mingw's Win32 header files are broken. |
Reid Spencer | ab4d9b0 | 2006-06-08 18:08:43 +0000 | [diff] [blame] | 208 | sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId())); |
Jeff Cohen | d41b30d | 2006-11-05 19:31:28 +0000 | [diff] [blame] | 209 | #else |
| 210 | sprintf(pathname, "LLVM_%u", GetCurrentProcessId()); |
| 211 | #endif |
Jeff Cohen | edb9d6b | 2005-07-08 02:48:42 +0000 | [diff] [blame] | 212 | result.appendComponent(pathname); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 213 | |
| 214 | // If there's a directory left over from a previous LLVM execution that |
| 215 | // happened to have the same process id, get rid of it. |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 216 | result.eraseFromDisk(true); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 217 | |
| 218 | // And finally (re-)create the empty directory. |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 219 | result.createDirectoryOnDisk(false); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 220 | TempDirectory = new Path(result); |
| 221 | return *TempDirectory; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 224 | // 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] | 225 | Path |
| 226 | Path::GetRootDirectory() { |
Michael J. Spencer | 87e0697 | 2010-11-09 15:10:45 +0000 | [diff] [blame] | 227 | // This is the only notion that that Windows has of a root directory. Nothing |
| 228 | // is here except for drives. |
| 229 | return Path("file:///"); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 232 | void |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 233 | Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) { |
Michael J. Spencer | eebe970 | 2010-11-09 15:11:19 +0000 | [diff] [blame] | 234 | char buff[MAX_PATH]; |
| 235 | // Generic form of C:\Windows\System32 |
| 236 | HRESULT res = SHGetFolderPathA(NULL, |
| 237 | CSIDL_FLAG_CREATE | CSIDL_SYSTEM, |
| 238 | NULL, |
| 239 | SHGFP_TYPE_CURRENT, |
| 240 | buff); |
| 241 | if (res != S_OK) { |
| 242 | assert(0 && "Failed to get system directory"); |
| 243 | return; |
| 244 | } |
| 245 | Paths.push_back(sys::Path(buff)); |
| 246 | |
| 247 | // Reset buff. |
| 248 | buff[0] = 0; |
| 249 | // Generic form of C:\Windows |
| 250 | res = SHGetFolderPathA(NULL, |
| 251 | CSIDL_FLAG_CREATE | CSIDL_WINDOWS, |
| 252 | NULL, |
| 253 | SHGFP_TYPE_CURRENT, |
| 254 | buff); |
| 255 | if (res != S_OK) { |
| 256 | assert(0 && "Failed to get windows directory"); |
| 257 | return; |
| 258 | } |
| 259 | Paths.push_back(sys::Path(buff)); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 262 | void |
Gabor Greif | db5565a | 2007-07-06 20:28:40 +0000 | [diff] [blame] | 263 | Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) { |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 264 | char * env_var = getenv("LLVM_LIB_SEARCH_PATH"); |
| 265 | if (env_var != 0) { |
| 266 | getPathList(env_var,Paths); |
| 267 | } |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 268 | #ifdef LLVM_LIBDIR |
| 269 | { |
| 270 | Path tmpPath; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 271 | if (tmpPath.set(LLVM_LIBDIR)) |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 272 | if (tmpPath.canRead()) |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 273 | Paths.push_back(tmpPath); |
| 274 | } |
| 275 | #endif |
| 276 | GetSystemLibraryPaths(Paths); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | Path |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 280 | Path::GetUserHomeDirectory() { |
Michael J. Spencer | 05283c2 | 2010-11-09 15:11:31 +0000 | [diff] [blame] | 281 | char buff[MAX_PATH]; |
Michael J. Spencer | 6c43de4 | 2010-11-10 15:06:00 +0000 | [diff] [blame] | 282 | HRESULT res = SHGetFolderPathA(NULL, |
| 283 | CSIDL_FLAG_CREATE | CSIDL_APPDATA, |
| 284 | NULL, |
| 285 | SHGFP_TYPE_CURRENT, |
| 286 | buff); |
Michael J. Spencer | 05283c2 | 2010-11-09 15:11:31 +0000 | [diff] [blame] | 287 | if (res != S_OK) |
| 288 | assert(0 && "Failed to get user home directory"); |
| 289 | return Path(buff); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 290 | } |
Ted Kremenek | 7920078 | 2007-12-18 22:07:33 +0000 | [diff] [blame] | 291 | |
| 292 | Path |
| 293 | Path::GetCurrentDirectory() { |
| 294 | char pathname[MAX_PATH]; |
Anton Korobeynikov | 64ddbe4 | 2007-12-22 14:26:49 +0000 | [diff] [blame] | 295 | ::GetCurrentDirectoryA(MAX_PATH,pathname); |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 296 | return Path(pathname); |
Ted Kremenek | 7920078 | 2007-12-18 22:07:33 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Chris Lattner | 1a09144 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 299 | /// GetMainExecutable - Return the path to the main executable, given the |
| 300 | /// value of argv[0] from program startup. |
| 301 | Path Path::GetMainExecutable(const char *argv0, void *MainAddr) { |
Chris Lattner | c5718d0 | 2009-06-15 05:38:04 +0000 | [diff] [blame] | 302 | char pathname[MAX_PATH]; |
| 303 | DWORD ret = ::GetModuleFileNameA(NULL, pathname, MAX_PATH); |
| 304 | return ret != MAX_PATH ? Path(pathname) : Path(); |
Chris Lattner | 1a09144 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Ted Kremenek | 7920078 | 2007-12-18 22:07:33 +0000 | [diff] [blame] | 307 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 308 | // FIXME: the above set of functions don't map to Windows very well. |
| 309 | |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 310 | |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 311 | StringRef Path::getDirname() const { |
| 312 | return getDirnameCharSep(path, "/"); |
Ted Kremenek | 9b01cc0 | 2008-04-07 22:01:32 +0000 | [diff] [blame] | 313 | } |
Ted Kremenek | cf55c8e | 2008-04-07 21:53:57 +0000 | [diff] [blame] | 314 | |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 315 | StringRef |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 316 | Path::getBasename() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 317 | // Find the last slash |
| 318 | size_t slash = path.rfind('/'); |
| 319 | if (slash == std::string::npos) |
| 320 | slash = 0; |
| 321 | else |
| 322 | slash++; |
| 323 | |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 324 | size_t dot = path.rfind('.'); |
| 325 | if (dot == std::string::npos || dot < slash) |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 326 | return StringRef(path).substr(slash); |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 327 | else |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 328 | return StringRef(path).substr(slash, dot - slash); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 331 | StringRef |
Argyrios Kyrtzidis | fc19988 | 2008-06-15 15:15:19 +0000 | [diff] [blame] | 332 | Path::getSuffix() const { |
| 333 | // Find the last slash |
| 334 | size_t slash = path.rfind('/'); |
| 335 | if (slash == std::string::npos) |
| 336 | slash = 0; |
| 337 | else |
| 338 | slash++; |
| 339 | |
| 340 | size_t dot = path.rfind('.'); |
| 341 | if (dot == std::string::npos || dot < slash) |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 342 | return StringRef(""); |
Argyrios Kyrtzidis | fc19988 | 2008-06-15 15:15:19 +0000 | [diff] [blame] | 343 | else |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 344 | return StringRef(path).substr(dot + 1); |
Argyrios Kyrtzidis | fc19988 | 2008-06-15 15:15:19 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 347 | bool |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 348 | Path::exists() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 349 | DWORD attr = GetFileAttributes(path.c_str()); |
| 350 | return attr != INVALID_FILE_ATTRIBUTES; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | bool |
Ted Kremenek | fd52711 | 2007-12-18 19:46:22 +0000 | [diff] [blame] | 354 | Path::isDirectory() const { |
| 355 | DWORD attr = GetFileAttributes(path.c_str()); |
| 356 | return (attr != INVALID_FILE_ATTRIBUTES) && |
| 357 | (attr & FILE_ATTRIBUTE_DIRECTORY); |
| 358 | } |
| 359 | |
| 360 | bool |
Rafael Espindola | 9e07a14 | 2010-11-07 04:36:50 +0000 | [diff] [blame] | 361 | Path::isSymLink() const { |
Michael J. Spencer | 339b912 | 2010-11-10 15:05:39 +0000 | [diff] [blame] | 362 | DWORD attributes = GetFileAttributes(path.c_str()); |
| 363 | |
| 364 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 365 | // There's no sane way to report this :(. |
| 366 | assert(0 && "GetFileAttributes returned INVALID_FILE_ATTRIBUTES"); |
| 367 | |
| 368 | // This isn't exactly what defines a NTFS symlink, but it is only true for |
| 369 | // paths that act like a symlink. |
| 370 | return attributes & FILE_ATTRIBUTE_REPARSE_POINT; |
Rafael Espindola | 9e07a14 | 2010-11-07 04:36:50 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | bool |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 374 | Path::canRead() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 375 | // FIXME: take security attributes into account. |
| 376 | DWORD attr = GetFileAttributes(path.c_str()); |
| 377 | return attr != INVALID_FILE_ATTRIBUTES; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | bool |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 381 | Path::canWrite() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 382 | // FIXME: take security attributes into account. |
| 383 | DWORD attr = GetFileAttributes(path.c_str()); |
| 384 | return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | bool |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 388 | Path::canExecute() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 389 | // FIXME: take security attributes into account. |
| 390 | DWORD attr = GetFileAttributes(path.c_str()); |
| 391 | return attr != INVALID_FILE_ATTRIBUTES; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Edward O'Callaghan | d41e944 | 2009-11-24 15:19:10 +0000 | [diff] [blame] | 394 | bool |
Edward O'Callaghan | e49a8e4 | 2009-11-25 06:32:19 +0000 | [diff] [blame] | 395 | Path::isRegularFile() const { |
Michael J. Spencer | 218dc3e | 2011-01-11 01:21:55 +0000 | [diff] [blame] | 396 | bool res; |
| 397 | if (fs::is_regular_file(path, res)) |
Edward O'Callaghan | e49a8e4 | 2009-11-25 06:32:19 +0000 | [diff] [blame] | 398 | return false; |
Michael J. Spencer | 218dc3e | 2011-01-11 01:21:55 +0000 | [diff] [blame] | 399 | return res; |
Edward O'Callaghan | d41e944 | 2009-11-24 15:19:10 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 402 | StringRef |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 403 | Path::getLast() const { |
| 404 | // Find the last slash |
| 405 | size_t pos = path.rfind('/'); |
| 406 | |
| 407 | // Handle the corner cases |
| 408 | if (pos == std::string::npos) |
| 409 | return path; |
| 410 | |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 411 | // If the last character is a slash, we have a root directory |
| 412 | if (pos == path.length()-1) |
| 413 | return path; |
| 414 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 415 | // Return everything after the last slash |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 416 | return StringRef(path).substr(pos+1); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Reid Spencer | 8475ec0 | 2007-03-29 19:05:44 +0000 | [diff] [blame] | 419 | const FileStatus * |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 420 | PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const { |
| 421 | if (!fsIsValid || update) { |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 422 | WIN32_FILE_ATTRIBUTE_DATA fi; |
Reid Spencer | 8475ec0 | 2007-03-29 19:05:44 +0000 | [diff] [blame] | 423 | if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) { |
| 424 | MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) + |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 425 | ": Can't get status: "); |
Reid Spencer | 8475ec0 | 2007-03-29 19:05:44 +0000 | [diff] [blame] | 426 | return 0; |
| 427 | } |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 428 | |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 429 | status.fileSize = fi.nFileSizeHigh; |
| 430 | status.fileSize <<= sizeof(fi.nFileSizeHigh)*8; |
| 431 | status.fileSize += fi.nFileSizeLow; |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 432 | |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 433 | status.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777; |
| 434 | status.user = 9999; // Not applicable to Windows, so... |
| 435 | status.group = 9999; // Not applicable to Windows, so... |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 436 | |
Reid Spencer | 4031bef | 2007-03-29 17:00:31 +0000 | [diff] [blame] | 437 | // FIXME: this is only unique if the file is accessed by the same file path. |
| 438 | // How do we do this for C:\dir\file and ..\dir\file ? Unix has inode |
| 439 | // numbers, but the concept doesn't exist in Windows. |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 440 | status.uniqueID = 0; |
Reid Spencer | 4031bef | 2007-03-29 17:00:31 +0000 | [diff] [blame] | 441 | for (unsigned i = 0; i < path.length(); ++i) |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 442 | status.uniqueID += path[i]; |
Reid Spencer | 4031bef | 2007-03-29 17:00:31 +0000 | [diff] [blame] | 443 | |
Michael J. Spencer | 44edb0b | 2010-08-28 16:39:32 +0000 | [diff] [blame] | 444 | ULARGE_INTEGER ui; |
| 445 | ui.LowPart = fi.ftLastWriteTime.dwLowDateTime; |
| 446 | ui.HighPart = fi.ftLastWriteTime.dwHighDateTime; |
| 447 | status.modTime.fromWin32Time(ui.QuadPart); |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 448 | |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 449 | status.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
| 450 | fsIsValid = true; |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 451 | } |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 452 | return &status; |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 455 | bool Path::makeReadableOnDisk(std::string* ErrMsg) { |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 456 | // All files are readable on Windows (ignoring security attributes). |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 457 | return false; |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 460 | bool Path::makeWriteableOnDisk(std::string* ErrMsg) { |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 461 | DWORD attr = GetFileAttributes(path.c_str()); |
| 462 | |
| 463 | // If it doesn't exist, we're done. |
| 464 | if (attr == INVALID_FILE_ATTRIBUTES) |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 465 | return false; |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 466 | |
| 467 | if (attr & FILE_ATTRIBUTE_READONLY) { |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 468 | if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) { |
| 469 | MakeErrMsg(ErrMsg, std::string(path) + ": Can't make file writable: "); |
| 470 | return true; |
| 471 | } |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 472 | } |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 473 | return false; |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 476 | bool Path::makeExecutableOnDisk(std::string* ErrMsg) { |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 477 | // All files are executable on Windows (ignoring security attributes). |
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 | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 481 | bool |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 482 | Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const { |
Jeff Cohen | 3110289 | 2007-04-07 20:47:27 +0000 | [diff] [blame] | 483 | WIN32_FILE_ATTRIBUTE_DATA fi; |
| 484 | if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) { |
| 485 | MakeErrMsg(ErrMsg, path + ": can't get status of file"); |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 486 | return true; |
Jeff Cohen | 3110289 | 2007-04-07 20:47:27 +0000 | [diff] [blame] | 487 | } |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 488 | |
Jeff Cohen | 3110289 | 2007-04-07 20:47:27 +0000 | [diff] [blame] | 489 | if (!(fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 490 | if (ErrMsg) |
| 491 | *ErrMsg = path + ": not a directory"; |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 492 | return true; |
| 493 | } |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 494 | |
| 495 | result.clear(); |
| 496 | WIN32_FIND_DATA fd; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 497 | std::string searchpath = path; |
| 498 | if (path.size() == 0 || searchpath[path.size()-1] == '/') |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 499 | searchpath += "*"; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 500 | else |
| 501 | searchpath += "/*"; |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 502 | |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 503 | HANDLE h = FindFirstFile(searchpath.c_str(), &fd); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 504 | if (h == INVALID_HANDLE_VALUE) { |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 505 | if (GetLastError() == ERROR_FILE_NOT_FOUND) |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 506 | return true; // not really an error, now is it? |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 507 | MakeErrMsg(ErrMsg, path + ": Can't read directory: "); |
| 508 | return true; |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | do { |
Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 512 | if (fd.cFileName[0] == '.') |
| 513 | continue; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 514 | Path aPath(path); |
| 515 | aPath.appendComponent(&fd.cFileName[0]); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 516 | result.insert(aPath); |
| 517 | } while (FindNextFile(h, &fd)); |
| 518 | |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 519 | DWORD err = GetLastError(); |
| 520 | FindClose(h); |
| 521 | if (err != ERROR_NO_MORE_FILES) { |
| 522 | SetLastError(err); |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 523 | MakeErrMsg(ErrMsg, path + ": Can't read directory: "); |
| 524 | return true; |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 525 | } |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 526 | return false; |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | bool |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 530 | Path::set(StringRef a_path) { |
Dan Gohman | 3035959 | 2008-01-29 13:02:09 +0000 | [diff] [blame] | 531 | if (a_path.empty()) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 532 | return false; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 533 | std::string save(path); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 534 | path = a_path; |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 535 | FlipBackSlashes(path); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 536 | if (!isValid()) { |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 537 | path = save; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 538 | return false; |
| 539 | } |
| 540 | return true; |
| 541 | } |
| 542 | |
| 543 | bool |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 544 | Path::appendComponent(StringRef name) { |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 545 | if (name.empty()) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 546 | return false; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 547 | std::string save(path); |
| 548 | if (!path.empty()) { |
| 549 | size_t last = path.size() - 1; |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 550 | if (path[last] != '/') |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 551 | path += '/'; |
| 552 | } |
| 553 | path += name; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 554 | if (!isValid()) { |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 555 | path = save; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 556 | return false; |
| 557 | } |
| 558 | return true; |
| 559 | } |
| 560 | |
| 561 | bool |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 562 | Path::eraseComponent() { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 563 | size_t slashpos = path.rfind('/',path.size()); |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 564 | if (slashpos == path.size() - 1 || slashpos == std::string::npos) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 565 | return false; |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 566 | std::string save(path); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 567 | path.erase(slashpos); |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 568 | if (!isValid()) { |
| 569 | path = save; |
| 570 | return false; |
| 571 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 572 | return true; |
| 573 | } |
| 574 | |
| 575 | bool |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 576 | Path::eraseSuffix() { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 577 | size_t dotpos = path.rfind('.',path.size()); |
| 578 | size_t slashpos = path.rfind('/',path.size()); |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 579 | if (dotpos != std::string::npos) { |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 580 | if (slashpos == std::string::npos || dotpos > slashpos+1) { |
| 581 | std::string save(path); |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 582 | path.erase(dotpos, path.size()-dotpos); |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 583 | if (!isValid()) { |
| 584 | path = save; |
| 585 | return false; |
| 586 | } |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 587 | return true; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 588 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 589 | } |
| 590 | return false; |
| 591 | } |
| 592 | |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 593 | inline bool PathMsg(std::string* ErrMsg, const char* pathname, const char*msg) { |
| 594 | if (ErrMsg) |
| 595 | *ErrMsg = std::string(pathname) + ": " + std::string(msg); |
| 596 | return true; |
| 597 | } |
| 598 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 599 | bool |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 600 | Path::createDirectoryOnDisk(bool create_parents, std::string* ErrMsg) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 601 | // Get a writeable copy of the path name |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 602 | size_t len = path.length(); |
| 603 | char *pathname = reinterpret_cast<char *>(_alloca(len+2)); |
| 604 | path.copy(pathname, len); |
| 605 | pathname[len] = 0; |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 606 | |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 607 | // Make sure it ends with a slash. |
| 608 | if (len == 0 || pathname[len - 1] != '/') { |
| 609 | pathname[len] = '/'; |
| 610 | pathname[++len] = 0; |
| 611 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 612 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 613 | // Determine starting point for initial / search. |
| 614 | char *next = pathname; |
| 615 | if (pathname[0] == '/' && pathname[1] == '/') { |
| 616 | // Skip host name. |
| 617 | next = strchr(pathname+2, '/'); |
| 618 | if (next == NULL) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 619 | return PathMsg(ErrMsg, pathname, "badly formed remote directory"); |
| 620 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 621 | // Skip share name. |
| 622 | next = strchr(next+1, '/'); |
| 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 | next++; |
| 627 | if (*next == 0) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 628 | return PathMsg(ErrMsg, pathname, "badly formed remote directory"); |
| 629 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 630 | } else { |
| 631 | if (pathname[1] == ':') |
| 632 | next += 2; // skip drive letter |
| 633 | if (*next == '/') |
| 634 | next++; // skip root directory |
| 635 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 636 | |
| 637 | // If we're supposed to create intermediate directories |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 638 | if (create_parents) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 639 | // Loop through the directory components until we're done |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 640 | while (*next) { |
| 641 | next = strchr(next, '/'); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 642 | *next = 0; |
Benjamin Kramer | e9684c6 | 2009-11-05 14:32:40 +0000 | [diff] [blame] | 643 | if (!CreateDirectory(pathname, NULL) && |
| 644 | GetLastError() != ERROR_ALREADY_EXISTS) |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 645 | return MakeErrMsg(ErrMsg, |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 646 | std::string(pathname) + ": Can't create directory: "); |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 647 | *next++ = '/'; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 648 | } |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 649 | } else { |
| 650 | // Drop trailing slash. |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 651 | pathname[len-1] = 0; |
Benjamin Kramer | e9684c6 | 2009-11-05 14:32:40 +0000 | [diff] [blame] | 652 | if (!CreateDirectory(pathname, NULL) && |
| 653 | GetLastError() != ERROR_ALREADY_EXISTS) { |
Mikhail Glushenkov | 0f2ec15 | 2010-11-02 20:32:31 +0000 | [diff] [blame] | 654 | return MakeErrMsg(ErrMsg, std::string(pathname) + |
| 655 | ": Can't create directory: "); |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 656 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 657 | } |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 658 | return false; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | bool |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 662 | Path::createFileOnDisk(std::string* ErrMsg) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 663 | // Create the file |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 664 | HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW, |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 665 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 666 | if (h == INVALID_HANDLE_VALUE) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 667 | return MakeErrMsg(ErrMsg, path + ": Can't create file: "); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 668 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 669 | CloseHandle(h); |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 670 | return false; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | bool |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 674 | Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const { |
Jeff Cohen | 3110289 | 2007-04-07 20:47:27 +0000 | [diff] [blame] | 675 | WIN32_FILE_ATTRIBUTE_DATA fi; |
| 676 | if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) |
| 677 | return true; |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 678 | |
Jeff Cohen | 3110289 | 2007-04-07 20:47:27 +0000 | [diff] [blame] | 679 | if (fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 680 | // If it doesn't exist, we're done. |
Michael J. Spencer | 54453f2 | 2011-01-10 02:34:23 +0000 | [diff] [blame] | 681 | bool Exists; |
| 682 | if (fs::exists(path, Exists) || !Exists) |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 683 | return false; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 684 | |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 685 | char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3)); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 686 | int lastchar = path.length() - 1 ; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 687 | path.copy(pathname, lastchar+1); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 688 | |
| 689 | // Make path end with '/*'. |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 690 | if (pathname[lastchar] != '/') |
| 691 | pathname[++lastchar] = '/'; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 692 | pathname[lastchar+1] = '*'; |
| 693 | pathname[lastchar+2] = 0; |
| 694 | |
| 695 | if (remove_contents) { |
| 696 | WIN32_FIND_DATA fd; |
| 697 | HANDLE h = FindFirstFile(pathname, &fd); |
| 698 | |
| 699 | // It's a bad idea to alter the contents of a directory while enumerating |
| 700 | // its contents. So build a list of its contents first, then destroy them. |
| 701 | |
| 702 | if (h != INVALID_HANDLE_VALUE) { |
| 703 | std::vector<Path> list; |
| 704 | |
| 705 | do { |
| 706 | if (strcmp(fd.cFileName, ".") == 0) |
| 707 | continue; |
| 708 | if (strcmp(fd.cFileName, "..") == 0) |
| 709 | continue; |
| 710 | |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 711 | Path aPath(path); |
| 712 | aPath.appendComponent(&fd.cFileName[0]); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 713 | list.push_back(aPath); |
| 714 | } while (FindNextFile(h, &fd)); |
| 715 | |
| 716 | DWORD err = GetLastError(); |
| 717 | FindClose(h); |
| 718 | if (err != ERROR_NO_MORE_FILES) { |
| 719 | SetLastError(err); |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 720 | return MakeErrMsg(ErrStr, path + ": Can't read directory: "); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 723 | for (std::vector<Path>::iterator I = list.begin(); I != list.end(); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 724 | ++I) { |
| 725 | Path &aPath = *I; |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 726 | aPath.eraseFromDisk(true); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 727 | } |
| 728 | } else { |
| 729 | if (GetLastError() != ERROR_FILE_NOT_FOUND) |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 730 | return MakeErrMsg(ErrStr, path + ": Can't read directory: "); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
| 734 | pathname[lastchar] = 0; |
| 735 | if (!RemoveDirectory(pathname)) |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 736 | return MakeErrMsg(ErrStr, |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 737 | std::string(pathname) + ": Can't destroy directory: "); |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 738 | return false; |
Jeff Cohen | 3110289 | 2007-04-07 20:47:27 +0000 | [diff] [blame] | 739 | } else { |
| 740 | // Read-only files cannot be deleted on Windows. Must remove the read-only |
| 741 | // attribute first. |
| 742 | if (fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) { |
| 743 | if (!SetFileAttributes(path.c_str(), |
| 744 | fi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY)) |
| 745 | return MakeErrMsg(ErrStr, path + ": Can't destroy file: "); |
| 746 | } |
| 747 | |
| 748 | if (!DeleteFile(path.c_str())) |
| 749 | return MakeErrMsg(ErrStr, path + ": Can't destroy file: "); |
| 750 | return false; |
| 751 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 754 | bool Path::getMagicNumber(std::string& Magic, unsigned len) const { |
Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 755 | assert(len < 1024 && "Request for magic string too long"); |
Michael J. Spencer | 1211d43 | 2010-08-31 06:36:33 +0000 | [diff] [blame] | 756 | char* buf = reinterpret_cast<char*>(alloca(len)); |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 757 | |
| 758 | HANDLE h = CreateFile(path.c_str(), |
| 759 | GENERIC_READ, |
| 760 | FILE_SHARE_READ, |
| 761 | NULL, |
| 762 | OPEN_EXISTING, |
| 763 | FILE_ATTRIBUTE_NORMAL, |
| 764 | NULL); |
| 765 | if (h == INVALID_HANDLE_VALUE) |
Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 766 | return false; |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 767 | |
| 768 | DWORD nRead = 0; |
| 769 | BOOL ret = ReadFile(h, buf, len, &nRead, NULL); |
| 770 | CloseHandle(h); |
| 771 | |
| 772 | if (!ret || nRead != len) |
Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 773 | return false; |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 774 | |
Michael J. Spencer | 1211d43 | 2010-08-31 06:36:33 +0000 | [diff] [blame] | 775 | Magic = std::string(buf, len); |
Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 776 | return true; |
| 777 | } |
| 778 | |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 779 | bool |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 780 | Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) { |
Francois Pichet | 3eddd98 | 2010-09-30 00:44:58 +0000 | [diff] [blame] | 781 | if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING)) |
| 782 | return MakeErrMsg(ErrMsg, "Can't move '" + path + "' to '" + newName.path |
| 783 | + "': "); |
| 784 | return false; |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | bool |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 788 | Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrMsg) const { |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 789 | // FIXME: should work on directories also. |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 790 | if (!si.isFile) { |
| 791 | return true; |
| 792 | } |
Kovarththanan Rajaratnam | 16ceb3a | 2010-03-12 14:17:24 +0000 | [diff] [blame] | 793 | |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 794 | HANDLE h = CreateFile(path.c_str(), |
| 795 | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES, |
| 796 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 797 | NULL, |
| 798 | OPEN_EXISTING, |
| 799 | FILE_ATTRIBUTE_NORMAL, |
| 800 | NULL); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 801 | if (h == INVALID_HANDLE_VALUE) |
Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 802 | return true; |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 803 | |
| 804 | BY_HANDLE_FILE_INFORMATION bhfi; |
| 805 | if (!GetFileInformationByHandle(h, &bhfi)) { |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 806 | DWORD err = GetLastError(); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 807 | CloseHandle(h); |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 808 | SetLastError(err); |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 809 | return MakeErrMsg(ErrMsg, path + ": GetFileInformationByHandle: "); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Michael J. Spencer | 44edb0b | 2010-08-28 16:39:32 +0000 | [diff] [blame] | 812 | ULARGE_INTEGER ui; |
| 813 | ui.QuadPart = si.modTime.toWin32Time(); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 814 | FILETIME ft; |
Michael J. Spencer | 44edb0b | 2010-08-28 16:39:32 +0000 | [diff] [blame] | 815 | ft.dwLowDateTime = ui.LowPart; |
| 816 | ft.dwHighDateTime = ui.HighPart; |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 817 | BOOL ret = SetFileTime(h, NULL, &ft, &ft); |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 818 | DWORD err = GetLastError(); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 819 | CloseHandle(h); |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 820 | if (!ret) { |
| 821 | SetLastError(err); |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 822 | return MakeErrMsg(ErrMsg, path + ": SetFileTime: "); |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 823 | } |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 824 | |
| 825 | // Best we can do with Unix permission bits is to interpret the owner |
| 826 | // writable bit. |
| 827 | if (si.mode & 0200) { |
| 828 | if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) { |
| 829 | if (!SetFileAttributes(path.c_str(), |
Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 830 | bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY)) |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 831 | return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: "); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 832 | } |
| 833 | } else { |
| 834 | if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) { |
| 835 | if (!SetFileAttributes(path.c_str(), |
Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 836 | bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY)) |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 837 | return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: "); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | |
Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 841 | return false; |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 844 | bool |
| 845 | CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg) { |
Jeff Cohen | cb65255 | 2004-12-24 02:38:34 +0000 | [diff] [blame] | 846 | // Can't use CopyFile macro defined in Windows.h because it would mess up the |
| 847 | // above line. We use the expansion it would have in a non-UNICODE build. |
| 848 | if (!::CopyFileA(Src.c_str(), Dest.c_str(), false)) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 849 | return MakeErrMsg(ErrMsg, "Can't copy '" + Src.str() + |
| 850 | "' to '" + Dest.str() + "': "); |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 851 | return false; |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 854 | bool |
| 855 | Path::makeUnique(bool reuse_current, std::string* ErrMsg) { |
Michael J. Spencer | 54453f2 | 2011-01-10 02:34:23 +0000 | [diff] [blame] | 856 | bool Exists; |
| 857 | if (reuse_current && (fs::exists(path, Exists) || !Exists)) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 858 | return false; // File doesn't exist already, just use it! |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 859 | |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 860 | // Reserve space for -XXXXXX at the end. |
| 861 | char *FNBuffer = (char*) alloca(path.size()+8); |
| 862 | unsigned offset = path.size(); |
| 863 | path.copy(FNBuffer, offset); |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 864 | |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 865 | // Find a numeric suffix that isn't used by an existing file. Assume there |
| 866 | // won't be more than 1 million files with the same prefix. Probably a safe |
| 867 | // bet. |
NAKAMURA Takumi | bfb25cd | 2011-03-16 02:53:24 +0000 | [diff] [blame] | 868 | static int FCounter = -1; |
| 869 | if (FCounter < 0) { |
| 870 | // Give arbitrary initial seed. |
| 871 | // FIXME: We should use sys::fs::unique_file() in future. |
| 872 | LARGE_INTEGER cnt64; |
| 873 | DWORD x = GetCurrentProcessId(); |
| 874 | x = (x << 16) | (x >> 16); |
| 875 | if (QueryPerformanceCounter(&cnt64)) // RDTSC |
| 876 | x ^= cnt64.HighPart ^ cnt64.LowPart; |
| 877 | FCounter = x % 1000000; |
| 878 | } |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 879 | do { |
| 880 | sprintf(FNBuffer+offset, "-%06u", FCounter); |
| 881 | if (++FCounter > 999999) |
| 882 | FCounter = 0; |
| 883 | path = FNBuffer; |
Michael J. Spencer | 54453f2 | 2011-01-10 02:34:23 +0000 | [diff] [blame] | 884 | } while (!fs::exists(path, Exists) && Exists); |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 885 | return false; |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Reid Spencer | 07f9f4e | 2004-12-15 08:32:45 +0000 | [diff] [blame] | 888 | bool |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 889 | Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) { |
Reid Spencer | 07f9f4e | 2004-12-15 08:32:45 +0000 | [diff] [blame] | 890 | // Make this into a unique file name |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 891 | makeUnique(reuse_current, ErrMsg); |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 892 | |
| 893 | // Now go and create it |
| 894 | HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW, |
| 895 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 896 | if (h == INVALID_HANDLE_VALUE) |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 897 | return MakeErrMsg(ErrMsg, path + ": can't create file"); |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 898 | |
| 899 | CloseHandle(h); |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 900 | return false; |
Reid Spencer | 07f9f4e | 2004-12-15 08:32:45 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Chris Lattner | 799ed10 | 2008-04-01 06:00:12 +0000 | [diff] [blame] | 903 | /// MapInFilePages - Not yet implemented on win32. |
Rafael Espindola | b78e2ae | 2011-03-10 18:30:48 +0000 | [diff] [blame] | 904 | const char *Path::MapInFilePages(int FD, size_t FileSize, off_t Offset) { |
Chris Lattner | 799ed10 | 2008-04-01 06:00:12 +0000 | [diff] [blame] | 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | /// MapInFilePages - Not yet implemented on win32. |
Rafael Espindola | b78e2ae | 2011-03-10 18:30:48 +0000 | [diff] [blame] | 909 | void Path::UnMapFilePages(const char *Base, size_t FileSize) { |
Chris Lattner | 799ed10 | 2008-04-01 06:00:12 +0000 | [diff] [blame] | 910 | assert(0 && "NOT IMPLEMENTED"); |
| 911 | } |
| 912 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 913 | } |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 914 | } |