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