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