| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 1 | //===- llvm/System/Linux/Path.cpp - Linux Path Implementation ---*- C++ -*-===// | 
 | 2 | // | 
| Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 3 | //                     The LLVM Compiler Infrastructure | 
 | 4 | // | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 5 | // This file was developed by Reid Spencer and is distributed under the | 
| Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 7 | // | 
 | 8 | // Modified by Henrik Bach to comply with at least MinGW. | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 9 | // Ported to Win32 by Jeff Cohen. | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 10 | // | 
| Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 11 | //===----------------------------------------------------------------------===// | 
 | 12 | // | 
 | 13 | // This file provides the Win32 specific implementation of the Path class. | 
 | 14 | // | 
 | 15 | //===----------------------------------------------------------------------===// | 
 | 16 |  | 
 | 17 | //===----------------------------------------------------------------------===// | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 18 | //=== WARNING: Implementation here must contain only generic Win32 code that | 
 | 19 | //===          is guaranteed to work on *all* Win32 variants. | 
| Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// | 
 | 21 |  | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 22 | #include "Win32.h" | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 23 | #include <malloc.h> | 
| Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 24 |  | 
| Jeff Cohen | cb65255 | 2004-12-24 02:38:34 +0000 | [diff] [blame] | 25 | // We need to undo a macro defined in Windows.h, otherwise we won't compile: | 
 | 26 | #undef CopyFile | 
 | 27 |  | 
| Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 28 | // Windows happily accepts either forward or backward slashes, though any path | 
 | 29 | // returned by a Win32 API will have backward slashes.  As LLVM code basically | 
 | 30 | // assumes forward slashes are used, backward slashs are converted where they | 
 | 31 | // can be introduced into a path. | 
 | 32 | // | 
 | 33 | // Another invariant is that a path ends with a slash if and only if the path | 
 | 34 | // is a root directory.  Any other use of a trailing slash is stripped.  Unlike | 
 | 35 | // in Unix, Windows has a rather complicated notion of a root path and this | 
 | 36 | // invariant helps simply the code. | 
 | 37 |  | 
| Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 38 | static void FlipBackSlashes(std::string& s) { | 
 | 39 |   for (size_t i = 0; i < s.size(); i++) | 
 | 40 |     if (s[i] == '\\') | 
 | 41 |       s[i] = '/'; | 
 | 42 | } | 
 | 43 |  | 
| Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 44 | namespace llvm { | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 45 | namespace sys { | 
| Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 46 |  | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 47 | bool | 
| Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 48 | Path::isValid() const { | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 49 |   if (path.empty()) | 
 | 50 |     return false; | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 51 |  | 
| Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 52 |   // If there is a colon, it must be the second character, preceded by a letter | 
 | 53 |   // and followed by something. | 
 | 54 |   size_t len = path.size(); | 
 | 55 |   size_t pos = path.rfind(':',len); | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 56 |   size_t rootslash = 0; | 
| Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 57 |   if (pos != std::string::npos) { | 
 | 58 |     if (pos != 1 || !isalpha(path[0]) || len < 3) | 
 | 59 |       return false; | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 60 |       rootslash = 2; | 
 | 61 |   } | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 62 |  | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 63 |   // Look for a UNC path, and if found adjust our notion of the root slash. | 
 | 64 |   if (len > 3 && path[0] == '/' && path[1] == '/') { | 
 | 65 |     rootslash = path.find('/', 2); | 
 | 66 |     if (rootslash == std::string::npos) | 
 | 67 |       rootslash = 0; | 
| Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 68 |   } | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 69 |  | 
| Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 70 |   // Check for illegal characters. | 
 | 71 |   if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012" | 
 | 72 |                          "\013\014\015\016\017\020\021\022\023\024\025\026" | 
 | 73 |                          "\027\030\031\032\033\034\035\036\037") | 
 | 74 |       != std::string::npos) | 
 | 75 |     return false; | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 76 |  | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 77 |   // Remove trailing slash, unless it's a root slash. | 
 | 78 |   if (len > rootslash+1 && path[len-1] == '/') | 
 | 79 |     path.erase(--len); | 
| Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 80 |  | 
| Jeff Cohen | 3bbbcc1 | 2005-01-14 04:09:39 +0000 | [diff] [blame] | 81 |   // Check each component for legality. | 
 | 82 |   for (pos = 0; pos < len; ++pos) { | 
 | 83 |     // A component may not end in a space. | 
 | 84 |     if (path[pos] == ' ') { | 
 | 85 |       if (path[pos+1] == '/' || path[pos+1] == '\0') | 
 | 86 |         return false; | 
 | 87 |     } | 
| Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 88 |  | 
| Jeff Cohen | 3bbbcc1 | 2005-01-14 04:09:39 +0000 | [diff] [blame] | 89 |     // A component may not end in a period. | 
 | 90 |     if (path[pos] == '.') { | 
 | 91 |       if (path[pos+1] == '/' || path[pos+1] == '\0') { | 
 | 92 |         // Unless it is the pseudo-directory "."... | 
 | 93 |         if (pos == 0 || path[pos-1] == '/' || path[pos-1] == ':') | 
 | 94 |           return true; | 
 | 95 |         // or "..". | 
 | 96 |         if (pos > 0 && path[pos-1] == '.') { | 
 | 97 |           if (pos == 1 || path[pos-2] == '/' || path[pos-2] == ':') | 
 | 98 |             return true; | 
 | 99 |         } | 
 | 100 |         return false; | 
 | 101 |       } | 
 | 102 |     } | 
 | 103 |   } | 
| Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 104 |  | 
 | 105 |   return true; | 
| Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 106 | } | 
 | 107 |  | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 108 | static Path *TempDirectory = NULL; | 
 | 109 |  | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 110 | Path | 
| Reid Spencer | cab0e43 | 2006-08-22 22:46:39 +0000 | [diff] [blame^] | 111 | Path::GetTemporaryDirectory(std::string* ErrMsg) { | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 112 |   if (TempDirectory) | 
 | 113 |     return *TempDirectory; | 
 | 114 |  | 
 | 115 |   char pathname[MAX_PATH]; | 
| Reid Spencer | cab0e43 | 2006-08-22 22:46:39 +0000 | [diff] [blame^] | 116 |   if (!GetTempPath(MAX_PATH, pathname)) { | 
 | 117 |     if (ErrMsg) | 
 | 118 |       *ErrMsg = "Can't determine temporary directory"; | 
 | 119 |     return Path(); | 
 | 120 |   } | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 121 |  | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 122 |   Path result; | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 123 |   result.set(pathname); | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 124 |  | 
 | 125 |   // Append a subdirectory passed on our process id so multiple LLVMs don't | 
 | 126 |   // step on each other's toes. | 
| Reid Spencer | ab4d9b0 | 2006-06-08 18:08:43 +0000 | [diff] [blame] | 127 |   sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId())); | 
| Jeff Cohen | edb9d6b | 2005-07-08 02:48:42 +0000 | [diff] [blame] | 128 |   result.appendComponent(pathname); | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 129 |  | 
 | 130 |   // If there's a directory left over from a previous LLVM execution that | 
 | 131 |   // happened to have the same process id, get rid of it. | 
| Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 132 |   result.eraseFromDisk(true); | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 133 |  | 
 | 134 |   // And finally (re-)create the empty directory. | 
| Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 135 |   result.createDirectoryOnDisk(false); | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 136 |   TempDirectory = new Path(result); | 
 | 137 |   return *TempDirectory; | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 138 | } | 
 | 139 |  | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 140 | // 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] | 141 | Path | 
 | 142 | Path::GetRootDirectory() { | 
 | 143 |   Path result; | 
| Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 144 |   result.set("C:/"); | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 145 |   return result; | 
 | 146 | } | 
 | 147 |  | 
| Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 148 | static void getPathList(const char*path, std::vector<sys::Path>& Paths) { | 
 | 149 |   const char* at = path; | 
 | 150 |   const char* delim = strchr(at, ';'); | 
 | 151 |   Path tmpPath; | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 152 |   while (delim != 0) { | 
| Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 153 |     std::string tmp(at, size_t(delim-at)); | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 154 |     if (tmpPath.set(tmp)) | 
| Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 155 |       if (tmpPath.canRead()) | 
| Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 156 |         Paths.push_back(tmpPath); | 
 | 157 |     at = delim + 1; | 
 | 158 |     delim = strchr(at, ';'); | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 159 |   } | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 160 |  | 
| Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 161 |   if (*at != 0) | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 162 |     if (tmpPath.set(std::string(at))) | 
| Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 163 |       if (tmpPath.canRead()) | 
| Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 164 |         Paths.push_back(tmpPath); | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 165 | } | 
 | 166 |  | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 167 | void | 
| Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 168 | Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) { | 
| Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 169 |   Paths.push_back(sys::Path("C:/WINDOWS/SYSTEM32")); | 
 | 170 |   Paths.push_back(sys::Path("C:/WINDOWS")); | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 171 | } | 
 | 172 |  | 
| Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 173 | void | 
 | 174 | Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) { | 
 | 175 |   char * env_var = getenv("LLVM_LIB_SEARCH_PATH"); | 
 | 176 |   if (env_var != 0) { | 
 | 177 |     getPathList(env_var,Paths); | 
 | 178 |   } | 
| Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 179 | #ifdef LLVM_LIBDIR | 
 | 180 |   { | 
 | 181 |     Path tmpPath; | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 182 |     if (tmpPath.set(LLVM_LIBDIR)) | 
| Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 183 |       if (tmpPath.canRead()) | 
| Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 184 |         Paths.push_back(tmpPath); | 
 | 185 |   } | 
 | 186 | #endif | 
 | 187 |   GetSystemLibraryPaths(Paths); | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 188 | } | 
 | 189 |  | 
 | 190 | Path | 
 | 191 | Path::GetLLVMDefaultConfigDir() { | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 192 |   // TODO: this isn't going to fly on Windows | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 193 |   return Path("/etc/llvm"); | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 194 | } | 
 | 195 |  | 
 | 196 | Path | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 197 | Path::GetUserHomeDirectory() { | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 198 |   // TODO: Typical Windows setup doesn't define HOME. | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 199 |   const char* home = getenv("HOME"); | 
 | 200 |   if (home) { | 
 | 201 |     Path result; | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 202 |     if (result.set(home)) | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 203 |       return result; | 
 | 204 |   } | 
 | 205 |   return GetRootDirectory(); | 
 | 206 | } | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 207 | // FIXME: the above set of functions don't map to Windows very well. | 
 | 208 |  | 
| Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 209 |  | 
 | 210 | bool | 
 | 211 | Path::isRootDirectory() const { | 
 | 212 |   size_t len = path.size(); | 
 | 213 |   return len > 0 && path[len-1] == '/'; | 
| Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 214 | } | 
 | 215 |  | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 216 | std::string | 
| Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 217 | Path::getBasename() const { | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 218 |   // Find the last slash | 
 | 219 |   size_t slash = path.rfind('/'); | 
 | 220 |   if (slash == std::string::npos) | 
 | 221 |     slash = 0; | 
 | 222 |   else | 
 | 223 |     slash++; | 
 | 224 |  | 
| Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 225 |   size_t dot = path.rfind('.'); | 
 | 226 |   if (dot == std::string::npos || dot < slash) | 
 | 227 |     return path.substr(slash); | 
 | 228 |   else | 
 | 229 |     return path.substr(slash, dot - slash); | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 230 | } | 
 | 231 |  | 
| Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 232 | bool Path::hasMagicNumber(const std::string &Magic) const { | 
| Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 233 |   std::string actualMagic; | 
 | 234 |   if (getMagicNumber(actualMagic, Magic.size())) | 
 | 235 |     return Magic == actualMagic; | 
 | 236 |   return false; | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 237 | } | 
 | 238 |  | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 239 | bool | 
| Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 240 | Path::isBytecodeFile() const { | 
| Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 241 |   std::string actualMagic; | 
 | 242 |   if (!getMagicNumber(actualMagic, 4)) | 
 | 243 |     return false; | 
 | 244 |   return actualMagic == "llvc" || actualMagic == "llvm"; | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 245 | } | 
 | 246 |  | 
 | 247 | bool | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 248 | Path::exists() const { | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 249 |   DWORD attr = GetFileAttributes(path.c_str()); | 
 | 250 |   return attr != INVALID_FILE_ATTRIBUTES; | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 251 | } | 
 | 252 |  | 
 | 253 | bool | 
| Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 254 | Path::canRead() const { | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 255 |   // FIXME: take security attributes into account. | 
 | 256 |   DWORD attr = GetFileAttributes(path.c_str()); | 
 | 257 |   return attr != INVALID_FILE_ATTRIBUTES; | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 258 | } | 
 | 259 |  | 
 | 260 | bool | 
| Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 261 | Path::canWrite() const { | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 262 |   // FIXME: take security attributes into account. | 
 | 263 |   DWORD attr = GetFileAttributes(path.c_str()); | 
 | 264 |   return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY); | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 265 | } | 
 | 266 |  | 
 | 267 | bool | 
| Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 268 | Path::canExecute() const { | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 269 |   // FIXME: take security attributes into account. | 
 | 270 |   DWORD attr = GetFileAttributes(path.c_str()); | 
 | 271 |   return attr != INVALID_FILE_ATTRIBUTES; | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 272 | } | 
 | 273 |  | 
 | 274 | std::string | 
 | 275 | Path::getLast() const { | 
 | 276 |   // Find the last slash | 
 | 277 |   size_t pos = path.rfind('/'); | 
 | 278 |  | 
 | 279 |   // Handle the corner cases | 
 | 280 |   if (pos == std::string::npos) | 
 | 281 |     return path; | 
 | 282 |  | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 283 |   // If the last character is a slash, we have a root directory | 
 | 284 |   if (pos == path.length()-1) | 
 | 285 |     return path; | 
 | 286 |  | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 287 |   // Return everything after the last slash | 
 | 288 |   return path.substr(pos+1); | 
 | 289 | } | 
 | 290 |  | 
| Chris Lattner | 7dea101 | 2006-07-28 22:32:09 +0000 | [diff] [blame] | 291 | bool | 
| Anton Korobeynikov | 1a7d326 | 2006-08-01 08:07:22 +0000 | [diff] [blame] | 292 | Path::getFileStatus(FileStatus &info, std::string *ErrStr) const { | 
| Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 293 |   WIN32_FILE_ATTRIBUTE_DATA fi; | 
 | 294 |   if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) | 
| Chris Lattner | 7dea101 | 2006-07-28 22:32:09 +0000 | [diff] [blame] | 295 |     return GetError("getStatusInfo():" + std::string(path) + | 
 | 296 |                     ": Can't get status: ", ErrStr); | 
| Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 297 |  | 
 | 298 |   info.fileSize = fi.nFileSizeHigh; | 
| Reid Spencer | 393830a | 2006-06-08 17:00:08 +0000 | [diff] [blame] | 299 |   info.fileSize <<= sizeof(fi.nFileSizeHigh)*8; | 
| Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 300 |   info.fileSize += fi.nFileSizeLow; | 
 | 301 |  | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 302 |   info.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777; | 
| Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 303 |   info.user = 9999;    // Not applicable to Windows, so... | 
 | 304 |   info.group = 9999;   // Not applicable to Windows, so... | 
 | 305 |  | 
 | 306 |   __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime); | 
 | 307 |   info.modTime.fromWin32Time(ft); | 
 | 308 |  | 
 | 309 |   info.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; | 
| Chris Lattner | 7dea101 | 2006-07-28 22:32:09 +0000 | [diff] [blame] | 310 |   return false; | 
| Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 311 | } | 
 | 312 |  | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 313 | static bool AddPermissionBits(const std::string& Filename, int bits) { | 
 | 314 |   DWORD attr = GetFileAttributes(Filename.c_str()); | 
 | 315 |  | 
 | 316 |   // If it doesn't exist, we're done. | 
 | 317 |   if (attr == INVALID_FILE_ATTRIBUTES) | 
 | 318 |     return false; | 
 | 319 |  | 
 | 320 |   // The best we can do to interpret Unix permission bits is to use | 
 | 321 |   // the owner writable bit. | 
 | 322 |   if ((attr & FILE_ATTRIBUTE_READONLY) && (bits & 0200)) { | 
 | 323 |     if (!SetFileAttributes(Filename.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) | 
| Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 324 |       ThrowError(Filename + ": SetFileAttributes: "); | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 325 |   } | 
 | 326 |   return true; | 
 | 327 | } | 
 | 328 |  | 
| Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 329 | void Path::makeReadableOnDisk() { | 
| Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 330 |   // All files are readable on Windows (ignoring security attributes). | 
| Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 331 | } | 
 | 332 |  | 
| Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 333 | void Path::makeWriteableOnDisk() { | 
| Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 334 |   DWORD attr = GetFileAttributes(path.c_str()); | 
 | 335 |  | 
 | 336 |   // If it doesn't exist, we're done. | 
 | 337 |   if (attr == INVALID_FILE_ATTRIBUTES) | 
 | 338 |     return; | 
 | 339 |  | 
 | 340 |   if (attr & FILE_ATTRIBUTE_READONLY) { | 
 | 341 |     if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) | 
 | 342 |       ThrowError(std::string(path) + ": Can't make file writable: "); | 
 | 343 |   } | 
| Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 344 | } | 
 | 345 |  | 
| Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 346 | void Path::makeExecutableOnDisk() { | 
| Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 347 |   // All files are executable on Windows (ignoring security attributes). | 
| Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 348 | } | 
 | 349 |  | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 350 | bool | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 351 | Path::getDirectoryContents(std::set<Path>& result) const { | 
 | 352 |   if (!isDirectory()) | 
 | 353 |     return false; | 
 | 354 |  | 
 | 355 |   result.clear(); | 
 | 356 |   WIN32_FIND_DATA fd; | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 357 |   std::string searchpath = path; | 
 | 358 |   if (path.size() == 0 || searchpath[path.size()-1] == '/') | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 359 |     searchpath += "*"; | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 360 |   else | 
 | 361 |     searchpath += "/*"; | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 362 |  | 
| Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 363 |   HANDLE h = FindFirstFile(searchpath.c_str(), &fd); | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 364 |   if (h == INVALID_HANDLE_VALUE) { | 
| Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 365 |     if (GetLastError() == ERROR_FILE_NOT_FOUND) | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 366 |       return true; // not really an error, now is it? | 
| Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 367 |     ThrowError(path + ": Can't read directory: "); | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 368 |   } | 
 | 369 |  | 
 | 370 |   do { | 
| Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 371 |     if (fd.cFileName[0] == '.') | 
 | 372 |       continue; | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 373 |     Path aPath(path); | 
 | 374 |     aPath.appendComponent(&fd.cFileName[0]); | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 375 |     result.insert(aPath); | 
 | 376 |   } while (FindNextFile(h, &fd)); | 
 | 377 |  | 
| Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 378 |   DWORD err = GetLastError(); | 
 | 379 |   FindClose(h); | 
 | 380 |   if (err != ERROR_NO_MORE_FILES) { | 
 | 381 |     SetLastError(err); | 
| Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 382 |     ThrowError(path + ": Can't read directory: "); | 
| Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 383 |   } | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 384 |   return true; | 
 | 385 | } | 
 | 386 |  | 
 | 387 | bool | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 388 | Path::set(const std::string& a_path) { | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 389 |   if (a_path.size() == 0) | 
 | 390 |     return false; | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 391 |   std::string save(path); | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 392 |   path = a_path; | 
| Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 393 |   FlipBackSlashes(path); | 
| Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 394 |   if (!isValid()) { | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 395 |     path = save; | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 396 |     return false; | 
 | 397 |   } | 
 | 398 |   return true; | 
 | 399 | } | 
 | 400 |  | 
 | 401 | bool | 
| Jeff Cohen | edb9d6b | 2005-07-08 02:48:42 +0000 | [diff] [blame] | 402 | Path::appendComponent(const std::string& name) { | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 403 |   if (name.empty()) | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 404 |     return false; | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 405 |   std::string save(path); | 
 | 406 |   if (!path.empty()) { | 
 | 407 |     size_t last = path.size() - 1; | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 408 |     if (path[last] != '/') | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 409 |       path += '/'; | 
 | 410 |   } | 
 | 411 |   path += name; | 
| Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 412 |   if (!isValid()) { | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 413 |     path = save; | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 414 |     return false; | 
 | 415 |   } | 
 | 416 |   return true; | 
 | 417 | } | 
 | 418 |  | 
 | 419 | bool | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 420 | Path::eraseComponent() { | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 421 |   size_t slashpos = path.rfind('/',path.size()); | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 422 |   if (slashpos == path.size() - 1 || slashpos == std::string::npos) | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 423 |     return false; | 
| Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 424 |   std::string save(path); | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 425 |   path.erase(slashpos); | 
| Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 426 |   if (!isValid()) { | 
 | 427 |     path = save; | 
 | 428 |     return false; | 
 | 429 |   } | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 430 |   return true; | 
 | 431 | } | 
 | 432 |  | 
 | 433 | bool | 
| Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 434 | Path::appendSuffix(const std::string& suffix) { | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 435 |   std::string save(path); | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 436 |   path.append("."); | 
 | 437 |   path.append(suffix); | 
| Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 438 |   if (!isValid()) { | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 439 |     path = save; | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 440 |     return false; | 
 | 441 |   } | 
 | 442 |   return true; | 
 | 443 | } | 
 | 444 |  | 
 | 445 | bool | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 446 | Path::eraseSuffix() { | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 447 |   size_t dotpos = path.rfind('.',path.size()); | 
 | 448 |   size_t slashpos = path.rfind('/',path.size()); | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 449 |   if (dotpos != std::string::npos) { | 
| Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 450 |     if (slashpos == std::string::npos || dotpos > slashpos+1) { | 
 | 451 |       std::string save(path); | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 452 |       path.erase(dotpos, path.size()-dotpos); | 
| Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 453 |       if (!isValid()) { | 
 | 454 |         path = save; | 
 | 455 |         return false; | 
 | 456 |       } | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 457 |       return true; | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 458 |     } | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 459 |   } | 
 | 460 |   return false; | 
 | 461 | } | 
 | 462 |  | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 463 | bool | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 464 | Path::createDirectoryOnDisk(bool create_parents) { | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 465 |   // Get a writeable copy of the path name | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 466 |   size_t len = path.length(); | 
 | 467 |   char *pathname = reinterpret_cast<char *>(_alloca(len+2)); | 
 | 468 |   path.copy(pathname, len); | 
 | 469 |   pathname[len] = 0; | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 470 |  | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 471 |   // Make sure it ends with a slash. | 
 | 472 |   if (len == 0 || pathname[len - 1] != '/') { | 
 | 473 |     pathname[len] = '/'; | 
 | 474 |     pathname[++len] = 0; | 
 | 475 |   } | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 476 |  | 
| Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 477 |   // Determine starting point for initial / search. | 
 | 478 |   char *next = pathname; | 
 | 479 |   if (pathname[0] == '/' && pathname[1] == '/') { | 
 | 480 |     // Skip host name. | 
 | 481 |     next = strchr(pathname+2, '/'); | 
 | 482 |     if (next == NULL) | 
 | 483 |       throw std::string(pathname) + ": badly formed remote directory"; | 
 | 484 |     // Skip share name. | 
 | 485 |     next = strchr(next+1, '/'); | 
 | 486 |     if (next == NULL) | 
 | 487 |       throw std::string(pathname) + ": badly formed remote directory"; | 
 | 488 |     next++; | 
 | 489 |     if (*next == 0) | 
 | 490 |       throw std::string(pathname) + ": badly formed remote directory"; | 
 | 491 |   } else { | 
 | 492 |     if (pathname[1] == ':') | 
 | 493 |       next += 2;    // skip drive letter | 
 | 494 |     if (*next == '/') | 
 | 495 |       next++;       // skip root directory | 
 | 496 |   } | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 497 |  | 
 | 498 |   // If we're supposed to create intermediate directories | 
| Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 499 |   if (create_parents) { | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 500 |     // Loop through the directory components until we're done | 
| Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 501 |     while (*next) { | 
 | 502 |       next = strchr(next, '/'); | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 503 |       *next = 0; | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 504 |       if (!CreateDirectory(pathname, NULL)) | 
 | 505 |           ThrowError(std::string(pathname) + ": Can't create directory: "); | 
| Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 506 |       *next++ = '/'; | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 507 |     } | 
| Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 508 |   } else { | 
 | 509 |     // Drop trailing slash. | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 510 |     pathname[len-1] = 0; | 
| Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 511 |     if (!CreateDirectory(pathname, NULL)) { | 
 | 512 |       ThrowError(std::string(pathname) + ": Can't create directory: "); | 
 | 513 |     } | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 514 |   } | 
 | 515 |   return true; | 
 | 516 | } | 
 | 517 |  | 
 | 518 | bool | 
| Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 519 | Path::createFileOnDisk() { | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 520 |   // Create the file | 
| Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 521 |   HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW, | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 522 |                         FILE_ATTRIBUTE_NORMAL, NULL); | 
 | 523 |   if (h == INVALID_HANDLE_VALUE) | 
| Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 524 |     ThrowError(path + ": Can't create file: "); | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 525 |  | 
| Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 526 |   CloseHandle(h); | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 527 |   return true; | 
 | 528 | } | 
 | 529 |  | 
 | 530 | bool | 
| Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 531 | Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const { | 
| Chris Lattner | c7c453a | 2006-08-01 17:51:09 +0000 | [diff] [blame] | 532 |   FileStatus Status; | 
 | 533 |   if (getFileStatus(Status, ErrStr)) | 
 | 534 |     return true; | 
 | 535 |      | 
 | 536 |   if (Status.isFile) { | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 537 |     DWORD attr = GetFileAttributes(path.c_str()); | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 538 |  | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 539 |     // If it doesn't exist, we're done. | 
 | 540 |     if (attr == INVALID_FILE_ATTRIBUTES) | 
| Chris Lattner | c7c453a | 2006-08-01 17:51:09 +0000 | [diff] [blame] | 541 |       return true; | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 542 |  | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 543 |     // Read-only files cannot be deleted on Windows.  Must remove the read-only | 
 | 544 |     // attribute first. | 
 | 545 |     if (attr & FILE_ATTRIBUTE_READONLY) { | 
 | 546 |       if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) | 
| Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 547 |         return GetError(path + ": Can't destroy file: ", ErrStr); | 
| Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 548 |     } | 
| Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 549 |  | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 550 |     if (!DeleteFile(path.c_str())) | 
| Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 551 |       ThrowError(path + ": Can't destroy file: "); | 
| Chris Lattner | c7c453a | 2006-08-01 17:51:09 +0000 | [diff] [blame] | 552 |     return false; | 
 | 553 |   } else if (Status.isDir) { | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 554 |     // If it doesn't exist, we're done. | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 555 |     if (!exists()) | 
| Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 556 |       return false; | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 557 |  | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 558 |     char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3)); | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 559 |     int lastchar = path.length() - 1 ; | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 560 |     path.copy(pathname, lastchar+1); | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 561 |  | 
 | 562 |     // Make path end with '/*'. | 
| Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 563 |     if (pathname[lastchar] != '/') | 
 | 564 |       pathname[++lastchar] = '/'; | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 565 |     pathname[lastchar+1] = '*'; | 
 | 566 |     pathname[lastchar+2] = 0; | 
 | 567 |  | 
 | 568 |     if (remove_contents) { | 
 | 569 |       WIN32_FIND_DATA fd; | 
 | 570 |       HANDLE h = FindFirstFile(pathname, &fd); | 
 | 571 |  | 
 | 572 |       // It's a bad idea to alter the contents of a directory while enumerating | 
 | 573 |       // its contents. So build a list of its contents first, then destroy them. | 
 | 574 |  | 
 | 575 |       if (h != INVALID_HANDLE_VALUE) { | 
 | 576 |         std::vector<Path> list; | 
 | 577 |  | 
 | 578 |         do { | 
 | 579 |           if (strcmp(fd.cFileName, ".") == 0) | 
 | 580 |             continue; | 
 | 581 |           if (strcmp(fd.cFileName, "..") == 0) | 
 | 582 |             continue; | 
 | 583 |  | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 584 |           Path aPath(path); | 
 | 585 |           aPath.appendComponent(&fd.cFileName[0]); | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 586 |           list.push_back(aPath); | 
 | 587 |         } while (FindNextFile(h, &fd)); | 
 | 588 |  | 
 | 589 |         DWORD err = GetLastError(); | 
 | 590 |         FindClose(h); | 
 | 591 |         if (err != ERROR_NO_MORE_FILES) { | 
 | 592 |           SetLastError(err); | 
| Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 593 |           return GetError(path + ": Can't read directory: ", ErrStr); | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 594 |         } | 
 | 595 |  | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 596 |         for (std::vector<Path>::iterator I = list.begin(); I != list.end(); | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 597 |              ++I) { | 
 | 598 |           Path &aPath = *I; | 
| Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 599 |           aPath.eraseFromDisk(true); | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 600 |         } | 
 | 601 |       } else { | 
 | 602 |         if (GetLastError() != ERROR_FILE_NOT_FOUND) | 
| Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 603 |           return GetError(path + ": Can't read directory: ", ErrStr); | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 604 |       } | 
 | 605 |     } | 
 | 606 |  | 
 | 607 |     pathname[lastchar] = 0; | 
 | 608 |     if (!RemoveDirectory(pathname)) | 
| Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 609 |       return GetError(std::string(pathname) + ": Can't destroy directory: ", | 
 | 610 |                       ErrStr); | 
 | 611 |     return false; | 
| Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 612 |   } else { | 
 | 613 |     // It appears the path doesn't exist. | 
| Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 614 |     return true; | 
| Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 615 |   } | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 616 | } | 
 | 617 |  | 
| Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 618 | bool Path::getMagicNumber(std::string& Magic, unsigned len) const { | 
| Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 619 |   assert(len < 1024 && "Request for magic string too long"); | 
 | 620 |   char* buf = (char*) alloca(1 + len); | 
| Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 621 |  | 
 | 622 |   HANDLE h = CreateFile(path.c_str(), | 
 | 623 |                         GENERIC_READ, | 
 | 624 |                         FILE_SHARE_READ, | 
 | 625 |                         NULL, | 
 | 626 |                         OPEN_EXISTING, | 
 | 627 |                         FILE_ATTRIBUTE_NORMAL, | 
 | 628 |                         NULL); | 
 | 629 |   if (h == INVALID_HANDLE_VALUE) | 
| Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 630 |     return false; | 
| Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 631 |  | 
 | 632 |   DWORD nRead = 0; | 
 | 633 |   BOOL ret = ReadFile(h, buf, len, &nRead, NULL); | 
 | 634 |   CloseHandle(h); | 
 | 635 |  | 
 | 636 |   if (!ret || nRead != len) | 
| Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 637 |     return false; | 
| Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 638 |  | 
| Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 639 |   buf[len] = '\0'; | 
 | 640 |   Magic = buf; | 
 | 641 |   return true; | 
 | 642 | } | 
 | 643 |  | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 644 | bool | 
| Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 645 | Path::renamePathOnDisk(const Path& newName) { | 
| Jeff Cohen | e564de2 | 2006-05-07 02:51:51 +0000 | [diff] [blame] | 646 |   if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING)) | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 647 |     ThrowError("Can't move '" + path + | 
| Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 648 |                "' to '" + newName.path + "': "); | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 649 |   return true; | 
 | 650 | } | 
 | 651 |  | 
 | 652 | bool | 
| Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 653 | Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrStr) const { | 
| Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 654 |   // FIXME: should work on directories also. | 
| Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 655 |   if (!isFile()) return true; | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 656 |  | 
 | 657 |   HANDLE h = CreateFile(path.c_str(), | 
 | 658 |                         FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES, | 
 | 659 |                         FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, | 
| Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 660 |                         NULL, | 
 | 661 |                         OPEN_EXISTING, | 
 | 662 |                         FILE_ATTRIBUTE_NORMAL, | 
 | 663 |                         NULL); | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 664 |   if (h == INVALID_HANDLE_VALUE) | 
| Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 665 |     return true; | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 666 |  | 
 | 667 |   BY_HANDLE_FILE_INFORMATION bhfi; | 
 | 668 |   if (!GetFileInformationByHandle(h, &bhfi)) { | 
| Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 669 |     DWORD err = GetLastError(); | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 670 |     CloseHandle(h); | 
| Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 671 |     SetLastError(err); | 
| Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 672 |     return GetError(path + ": GetFileInformationByHandle: ", ErrStr); | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 673 |   } | 
 | 674 |  | 
 | 675 |   FILETIME ft; | 
 | 676 |   (uint64_t&)ft = si.modTime.toWin32Time(); | 
 | 677 |   BOOL ret = SetFileTime(h, NULL, &ft, &ft); | 
| Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 678 |   DWORD err = GetLastError(); | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 679 |   CloseHandle(h); | 
| Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 680 |   if (!ret) { | 
 | 681 |     SetLastError(err); | 
| Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 682 |     return GetError(path + ": SetFileTime: ", ErrStr); | 
| Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 683 |   } | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 684 |  | 
 | 685 |   // Best we can do with Unix permission bits is to interpret the owner | 
 | 686 |   // writable bit. | 
 | 687 |   if (si.mode & 0200) { | 
 | 688 |     if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) { | 
 | 689 |       if (!SetFileAttributes(path.c_str(), | 
| Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 690 |               bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY)) | 
| Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 691 |         return GetError(path + ": SetFileAttributes: ", ErrStr); | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 692 |     } | 
 | 693 |   } else { | 
 | 694 |     if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) { | 
 | 695 |       if (!SetFileAttributes(path.c_str(), | 
| Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 696 |               bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY)) | 
| Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 697 |         return GetError(path + ": SetFileAttributes: ", ErrStr); | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 698 |     } | 
 | 699 |   } | 
 | 700 |  | 
| Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 701 |   return false; | 
| Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 702 | } | 
 | 703 |  | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 704 | void | 
| Jeff Cohen | 10a59ce | 2006-04-29 18:41:44 +0000 | [diff] [blame] | 705 | CopyFile(const sys::Path &Dest, const sys::Path &Src) { | 
| Jeff Cohen | cb65255 | 2004-12-24 02:38:34 +0000 | [diff] [blame] | 706 |   // Can't use CopyFile macro defined in Windows.h because it would mess up the | 
 | 707 |   // above line.  We use the expansion it would have in a non-UNICODE build. | 
 | 708 |   if (!::CopyFileA(Src.c_str(), Dest.c_str(), false)) | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 709 |     ThrowError("Can't copy '" + Src.toString() + | 
| Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 710 |                "' to '" + Dest.toString() + "': "); | 
| Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 711 | } | 
 | 712 |  | 
| Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 713 | void | 
| Jeff Cohen | cb65255 | 2004-12-24 02:38:34 +0000 | [diff] [blame] | 714 | Path::makeUnique(bool reuse_current) { | 
| Reid Spencer | 07f9f4e | 2004-12-15 08:32:45 +0000 | [diff] [blame] | 715 |   if (reuse_current && !exists()) | 
| Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 716 |     return; // File doesn't exist already, just use it! | 
 | 717 |  | 
| Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 718 |   // Reserve space for -XXXXXX at the end. | 
 | 719 |   char *FNBuffer = (char*) alloca(path.size()+8); | 
 | 720 |   unsigned offset = path.size(); | 
 | 721 |   path.copy(FNBuffer, offset); | 
| Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 722 |  | 
| Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 723 |   // Find a numeric suffix that isn't used by an existing file.  Assume there | 
 | 724 |   // won't be more than 1 million files with the same prefix.  Probably a safe | 
 | 725 |   // bet. | 
| Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 726 |   static unsigned FCounter = 0; | 
 | 727 |   do { | 
 | 728 |     sprintf(FNBuffer+offset, "-%06u", FCounter); | 
 | 729 |     if (++FCounter > 999999) | 
 | 730 |       FCounter = 0; | 
 | 731 |     path = FNBuffer; | 
 | 732 |   } while (exists()); | 
| Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 733 | } | 
 | 734 |  | 
| Reid Spencer | 07f9f4e | 2004-12-15 08:32:45 +0000 | [diff] [blame] | 735 | bool | 
| Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 736 | Path::createTemporaryFileOnDisk(bool reuse_current) { | 
| Reid Spencer | 07f9f4e | 2004-12-15 08:32:45 +0000 | [diff] [blame] | 737 |   // Make this into a unique file name | 
| Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 738 |   makeUnique(reuse_current); | 
| Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 739 |  | 
 | 740 |   // Now go and create it | 
 | 741 |   HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW, | 
 | 742 |                         FILE_ATTRIBUTE_NORMAL, NULL); | 
 | 743 |   if (h == INVALID_HANDLE_VALUE) | 
 | 744 |     return false; | 
 | 745 |  | 
 | 746 |   CloseHandle(h); | 
| Jeff Cohen | f8cdb85 | 2004-12-18 06:42:15 +0000 | [diff] [blame] | 747 |   return true; | 
| Reid Spencer | 07f9f4e | 2004-12-15 08:32:45 +0000 | [diff] [blame] | 748 | } | 
 | 749 |  | 
| Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 750 | } | 
| Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 751 | } |