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