LLVM API Documentation
00001 //===- llvm/System/Win32/Path.cpp - Win32 Path Implementation ---*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 // Modified by Henrik Bach to comply with at least MinGW. 00009 // Ported to Win32 by Jeff Cohen. 00010 // 00011 //===----------------------------------------------------------------------===// 00012 // 00013 // This file provides the Win32 specific implementation of the Path class. 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 //===----------------------------------------------------------------------===// 00018 //=== WARNING: Implementation here must contain only generic Win32 code that 00019 //=== is guaranteed to work on *all* Win32 variants. 00020 //===----------------------------------------------------------------------===// 00021 00022 #include "Win32.h" 00023 #include <malloc.h> 00024 #include <cstdio> 00025 00026 // We need to undo a macro defined in Windows.h, otherwise we won't compile: 00027 #undef CopyFile 00028 #undef GetCurrentDirectory 00029 00030 // Windows happily accepts either forward or backward slashes, though any path 00031 // returned by a Win32 API will have backward slashes. As LLVM code basically 00032 // assumes forward slashes are used, backward slashs are converted where they 00033 // can be introduced into a path. 00034 // 00035 // Another invariant is that a path ends with a slash if and only if the path 00036 // is a root directory. Any other use of a trailing slash is stripped. Unlike 00037 // in Unix, Windows has a rather complicated notion of a root path and this 00038 // invariant helps simply the code. 00039 00040 static void FlipBackSlashes(std::string& s) { 00041 for (size_t i = 0; i < s.size(); i++) 00042 if (s[i] == '\\') 00043 s[i] = '/'; 00044 } 00045 00046 namespace llvm { 00047 namespace sys { 00048 const char PathSeparator = ';'; 00049 00050 Path::Path(const std::string& p) 00051 : path(p) { 00052 FlipBackSlashes(path); 00053 } 00054 00055 Path::Path(const char *StrStart, unsigned StrLen) 00056 : path(StrStart, StrLen) { 00057 FlipBackSlashes(path); 00058 } 00059 00060 Path& 00061 Path::operator=(const std::string &that) { 00062 path = that; 00063 FlipBackSlashes(path); 00064 return *this; 00065 } 00066 00067 bool 00068 Path::isValid() const { 00069 if (path.empty()) 00070 return false; 00071 00072 // If there is a colon, it must be the second character, preceded by a letter 00073 // and followed by something. 00074 size_t len = path.size(); 00075 size_t pos = path.rfind(':',len); 00076 size_t rootslash = 0; 00077 if (pos != std::string::npos) { 00078 if (pos != 1 || !isalpha(path[0]) || len < 3) 00079 return false; 00080 rootslash = 2; 00081 } 00082 00083 // Look for a UNC path, and if found adjust our notion of the root slash. 00084 if (len > 3 && path[0] == '/' && path[1] == '/') { 00085 rootslash = path.find('/', 2); 00086 if (rootslash == std::string::npos) 00087 rootslash = 0; 00088 } 00089 00090 // Check for illegal characters. 00091 if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012" 00092 "\013\014\015\016\017\020\021\022\023\024\025\026" 00093 "\027\030\031\032\033\034\035\036\037") 00094 != std::string::npos) 00095 return false; 00096 00097 // Remove trailing slash, unless it's a root slash. 00098 if (len > rootslash+1 && path[len-1] == '/') 00099 path.erase(--len); 00100 00101 // Check each component for legality. 00102 for (pos = 0; pos < len; ++pos) { 00103 // A component may not end in a space. 00104 if (path[pos] == ' ') { 00105 if (path[pos+1] == '/' || path[pos+1] == '\0') 00106 return false; 00107 } 00108 00109 // A component may not end in a period. 00110 if (path[pos] == '.') { 00111 if (path[pos+1] == '/' || path[pos+1] == '\0') { 00112 // Unless it is the pseudo-directory "."... 00113 if (pos == 0 || path[pos-1] == '/' || path[pos-1] == ':') 00114 return true; 00115 // or "..". 00116 if (pos > 0 && path[pos-1] == '.') { 00117 if (pos == 1 || path[pos-2] == '/' || path[pos-2] == ':') 00118 return true; 00119 } 00120 return false; 00121 } 00122 } 00123 } 00124 00125 return true; 00126 } 00127 00128 void Path::makeAbsolute() { 00129 TCHAR FullPath[MAX_PATH + 1] = {0}; 00130 LPTSTR FilePart = NULL; 00131 00132 DWORD RetLength = ::GetFullPathNameA(path.c_str(), 00133 sizeof(FullPath)/sizeof(FullPath[0]), 00134 FullPath, &FilePart); 00135 00136 if (0 == RetLength) { 00137 // FIXME: Report the error GetLastError() 00138 assert(0 && "Unable to make absolute path!"); 00139 } else if (RetLength > MAX_PATH) { 00140 // FIXME: Report too small buffer (needed RetLength bytes). 00141 assert(0 && "Unable to make absolute path!"); 00142 } else { 00143 path = FullPath; 00144 } 00145 } 00146 00147 bool 00148 Path::isAbsolute(const char *NameStart, unsigned NameLen) { 00149 assert(NameStart); 00150 // FIXME: This does not handle correctly an absolute path starting from 00151 // a drive letter or in UNC format. 00152 switch (NameLen) { 00153 case 0: 00154 return false; 00155 case 1: 00156 case 2: 00157 return NameStart[0] == '/'; 00158 default: 00159 return (NameStart[0] == '/' || (NameStart[1] == ':' && NameStart[2] == '/')) || 00160 (NameStart[0] == '\\' || (NameStart[1] == ':' && NameStart[2] == '\\')); 00161 } 00162 } 00163 00164 bool 00165 Path::isAbsolute() const { 00166 // FIXME: This does not handle correctly an absolute path starting from 00167 // a drive letter or in UNC format. 00168 switch (path.length()) { 00169 case 0: 00170 return false; 00171 case 1: 00172 case 2: 00173 return path[0] == '/'; 00174 default: 00175 return path[0] == '/' || (path[1] == ':' && path[2] == '/'); 00176 } 00177 } 00178 00179 static Path *TempDirectory = NULL; 00180 00181 Path 00182 Path::GetTemporaryDirectory(std::string* ErrMsg) { 00183 if (TempDirectory) 00184 return *TempDirectory; 00185 00186 char pathname[MAX_PATH]; 00187 if (!GetTempPath(MAX_PATH, pathname)) { 00188 if (ErrMsg) 00189 *ErrMsg = "Can't determine temporary directory"; 00190 return Path(); 00191 } 00192 00193 Path result; 00194 result.set(pathname); 00195 00196 // Append a subdirectory passed on our process id so multiple LLVMs don't 00197 // step on each other's toes. 00198 #ifdef __MINGW32__ 00199 // Mingw's Win32 header files are broken. 00200 sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId())); 00201 #else 00202 sprintf(pathname, "LLVM_%u", GetCurrentProcessId()); 00203 #endif 00204 result.appendComponent(pathname); 00205 00206 // If there's a directory left over from a previous LLVM execution that 00207 // happened to have the same process id, get rid of it. 00208 result.eraseFromDisk(true); 00209 00210 // And finally (re-)create the empty directory. 00211 result.createDirectoryOnDisk(false); 00212 TempDirectory = new Path(result); 00213 return *TempDirectory; 00214 } 00215 00216 // FIXME: the following set of functions don't map to Windows very well. 00217 Path 00218 Path::GetRootDirectory() { 00219 Path result; 00220 result.set("C:/"); 00221 return result; 00222 } 00223 00224 void 00225 Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) { 00226 Paths.push_back(sys::Path("C:/WINDOWS/SYSTEM32")); 00227 Paths.push_back(sys::Path("C:/WINDOWS")); 00228 } 00229 00230 void 00231 Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) { 00232 char * env_var = getenv("LLVM_LIB_SEARCH_PATH"); 00233 if (env_var != 0) { 00234 getPathList(env_var,Paths); 00235 } 00236 #ifdef LLVM_LIBDIR 00237 { 00238 Path tmpPath; 00239 if (tmpPath.set(LLVM_LIBDIR)) 00240 if (tmpPath.canRead()) 00241 Paths.push_back(tmpPath); 00242 } 00243 #endif 00244 GetSystemLibraryPaths(Paths); 00245 } 00246 00247 Path 00248 Path::GetLLVMDefaultConfigDir() { 00249 // TODO: this isn't going to fly on Windows 00250 return Path("/etc/llvm"); 00251 } 00252 00253 Path 00254 Path::GetUserHomeDirectory() { 00255 // TODO: Typical Windows setup doesn't define HOME. 00256 const char* home = getenv("HOME"); 00257 if (home) { 00258 Path result; 00259 if (result.set(home)) 00260 return result; 00261 } 00262 return GetRootDirectory(); 00263 } 00264 00265 Path 00266 Path::GetCurrentDirectory() { 00267 char pathname[MAX_PATH]; 00268 ::GetCurrentDirectoryA(MAX_PATH,pathname); 00269 return Path(pathname); 00270 } 00271 00272 /// GetMainExecutable - Return the path to the main executable, given the 00273 /// value of argv[0] from program startup. 00274 Path Path::GetMainExecutable(const char *argv0, void *MainAddr) { 00275 char pathname[MAX_PATH]; 00276 DWORD ret = ::GetModuleFileNameA(NULL, pathname, MAX_PATH); 00277 return ret != MAX_PATH ? Path(pathname) : Path(); 00278 } 00279 00280 00281 // FIXME: the above set of functions don't map to Windows very well. 00282 00283 00284 bool 00285 Path::isRootDirectory() const { 00286 size_t len = path.size(); 00287 return len > 0 && path[len-1] == '/'; 00288 } 00289 00290 std::string Path::getDirname() const { 00291 return getDirnameCharSep(path, '/'); 00292 } 00293 00294 std::string 00295 Path::getBasename() const { 00296 // Find the last slash 00297 size_t slash = path.rfind('/'); 00298 if (slash == std::string::npos) 00299 slash = 0; 00300 else 00301 slash++; 00302 00303 size_t dot = path.rfind('.'); 00304 if (dot == std::string::npos || dot < slash) 00305 return path.substr(slash); 00306 else 00307 return path.substr(slash, dot - slash); 00308 } 00309 00310 std::string 00311 Path::getSuffix() const { 00312 // Find the last slash 00313 size_t slash = path.rfind('/'); 00314 if (slash == std::string::npos) 00315 slash = 0; 00316 else 00317 slash++; 00318 00319 size_t dot = path.rfind('.'); 00320 if (dot == std::string::npos || dot < slash) 00321 return std::string(); 00322 else 00323 return path.substr(dot + 1); 00324 } 00325 00326 bool 00327 Path::exists() const { 00328 DWORD attr = GetFileAttributes(path.c_str()); 00329 return attr != INVALID_FILE_ATTRIBUTES; 00330 } 00331 00332 bool 00333 Path::isDirectory() const { 00334 DWORD attr = GetFileAttributes(path.c_str()); 00335 return (attr != INVALID_FILE_ATTRIBUTES) && 00336 (attr & FILE_ATTRIBUTE_DIRECTORY); 00337 } 00338 00339 bool 00340 Path::canRead() const { 00341 // FIXME: take security attributes into account. 00342 DWORD attr = GetFileAttributes(path.c_str()); 00343 return attr != INVALID_FILE_ATTRIBUTES; 00344 } 00345 00346 bool 00347 Path::canWrite() const { 00348 // FIXME: take security attributes into account. 00349 DWORD attr = GetFileAttributes(path.c_str()); 00350 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY); 00351 } 00352 00353 bool 00354 Path::canExecute() const { 00355 // FIXME: take security attributes into account. 00356 DWORD attr = GetFileAttributes(path.c_str()); 00357 return attr != INVALID_FILE_ATTRIBUTES; 00358 } 00359 00360 std::string 00361 Path::getLast() const { 00362 // Find the last slash 00363 size_t pos = path.rfind('/'); 00364 00365 // Handle the corner cases 00366 if (pos == std::string::npos) 00367 return path; 00368 00369 // If the last character is a slash, we have a root directory 00370 if (pos == path.length()-1) 00371 return path; 00372 00373 // Return everything after the last slash 00374 return path.substr(pos+1); 00375 } 00376 00377 const FileStatus * 00378 PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const { 00379 if (!fsIsValid || update) { 00380 WIN32_FILE_ATTRIBUTE_DATA fi; 00381 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) { 00382 MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) + 00383 ": Can't get status: "); 00384 return 0; 00385 } 00386 00387 status.fileSize = fi.nFileSizeHigh; 00388 status.fileSize <<= sizeof(fi.nFileSizeHigh)*8; 00389 status.fileSize += fi.nFileSizeLow; 00390 00391 status.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777; 00392 status.user = 9999; // Not applicable to Windows, so... 00393 status.group = 9999; // Not applicable to Windows, so... 00394 00395 // FIXME: this is only unique if the file is accessed by the same file path. 00396 // How do we do this for C:\dir\file and ..\dir\file ? Unix has inode 00397 // numbers, but the concept doesn't exist in Windows. 00398 status.uniqueID = 0; 00399 for (unsigned i = 0; i < path.length(); ++i) 00400 status.uniqueID += path[i]; 00401 00402 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime); 00403 status.modTime.fromWin32Time(ft); 00404 00405 status.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; 00406 fsIsValid = true; 00407 } 00408 return &status; 00409 } 00410 00411 bool Path::makeReadableOnDisk(std::string* ErrMsg) { 00412 // All files are readable on Windows (ignoring security attributes). 00413 return false; 00414 } 00415 00416 bool Path::makeWriteableOnDisk(std::string* ErrMsg) { 00417 DWORD attr = GetFileAttributes(path.c_str()); 00418 00419 // If it doesn't exist, we're done. 00420 if (attr == INVALID_FILE_ATTRIBUTES) 00421 return false; 00422 00423 if (attr & FILE_ATTRIBUTE_READONLY) { 00424 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) { 00425 MakeErrMsg(ErrMsg, std::string(path) + ": Can't make file writable: "); 00426 return true; 00427 } 00428 } 00429 return false; 00430 } 00431 00432 bool Path::makeExecutableOnDisk(std::string* ErrMsg) { 00433 // All files are executable on Windows (ignoring security attributes). 00434 return false; 00435 } 00436 00437 bool 00438 Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const { 00439 WIN32_FILE_ATTRIBUTE_DATA fi; 00440 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) { 00441 MakeErrMsg(ErrMsg, path + ": can't get status of file"); 00442 return true; 00443 } 00444 00445 if (!(fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { 00446 if (ErrMsg) 00447 *ErrMsg = path + ": not a directory"; 00448 return true; 00449 } 00450 00451 result.clear(); 00452 WIN32_FIND_DATA fd; 00453 std::string searchpath = path; 00454 if (path.size() == 0 || searchpath[path.size()-1] == '/') 00455 searchpath += "*"; 00456 else 00457 searchpath += "/*"; 00458 00459 HANDLE h = FindFirstFile(searchpath.c_str(), &fd); 00460 if (h == INVALID_HANDLE_VALUE) { 00461 if (GetLastError() == ERROR_FILE_NOT_FOUND) 00462 return true; // not really an error, now is it? 00463 MakeErrMsg(ErrMsg, path + ": Can't read directory: "); 00464 return true; 00465 } 00466 00467 do { 00468 if (fd.cFileName[0] == '.') 00469 continue; 00470 Path aPath(path); 00471 aPath.appendComponent(&fd.cFileName[0]); 00472 result.insert(aPath); 00473 } while (FindNextFile(h, &fd)); 00474 00475 DWORD err = GetLastError(); 00476 FindClose(h); 00477 if (err != ERROR_NO_MORE_FILES) { 00478 SetLastError(err); 00479 MakeErrMsg(ErrMsg, path + ": Can't read directory: "); 00480 return true; 00481 } 00482 return false; 00483 } 00484 00485 bool 00486 Path::set(const std::string& a_path) { 00487 if (a_path.empty()) 00488 return false; 00489 std::string save(path); 00490 path = a_path; 00491 FlipBackSlashes(path); 00492 if (!isValid()) { 00493 path = save; 00494 return false; 00495 } 00496 return true; 00497 } 00498 00499 bool 00500 Path::appendComponent(const std::string& name) { 00501 if (name.empty()) 00502 return false; 00503 std::string save(path); 00504 if (!path.empty()) { 00505 size_t last = path.size() - 1; 00506 if (path[last] != '/') 00507 path += '/'; 00508 } 00509 path += name; 00510 if (!isValid()) { 00511 path = save; 00512 return false; 00513 } 00514 return true; 00515 } 00516 00517 bool 00518 Path::eraseComponent() { 00519 size_t slashpos = path.rfind('/',path.size()); 00520 if (slashpos == path.size() - 1 || slashpos == std::string::npos) 00521 return false; 00522 std::string save(path); 00523 path.erase(slashpos); 00524 if (!isValid()) { 00525 path = save; 00526 return false; 00527 } 00528 return true; 00529 } 00530 00531 bool 00532 Path::appendSuffix(const std::string& suffix) { 00533 std::string save(path); 00534 path.append("."); 00535 path.append(suffix); 00536 if (!isValid()) { 00537 path = save; 00538 return false; 00539 } 00540 return true; 00541 } 00542 00543 bool 00544 Path::eraseSuffix() { 00545 size_t dotpos = path.rfind('.',path.size()); 00546 size_t slashpos = path.rfind('/',path.size()); 00547 if (dotpos != std::string::npos) { 00548 if (slashpos == std::string::npos || dotpos > slashpos+1) { 00549 std::string save(path); 00550 path.erase(dotpos, path.size()-dotpos); 00551 if (!isValid()) { 00552 path = save; 00553 return false; 00554 } 00555 return true; 00556 } 00557 } 00558 return false; 00559 } 00560 00561 inline bool PathMsg(std::string* ErrMsg, const char* pathname, const char*msg) { 00562 if (ErrMsg) 00563 *ErrMsg = std::string(pathname) + ": " + std::string(msg); 00564 return true; 00565 } 00566 00567 bool 00568 Path::createDirectoryOnDisk(bool create_parents, std::string* ErrMsg) { 00569 // Get a writeable copy of the path name 00570 size_t len = path.length(); 00571 char *pathname = reinterpret_cast<char *>(_alloca(len+2)); 00572 path.copy(pathname, len); 00573 pathname[len] = 0; 00574 00575 // Make sure it ends with a slash. 00576 if (len == 0 || pathname[len - 1] != '/') { 00577 pathname[len] = '/'; 00578 pathname[++len] = 0; 00579 } 00580 00581 // Determine starting point for initial / search. 00582 char *next = pathname; 00583 if (pathname[0] == '/' && pathname[1] == '/') { 00584 // Skip host name. 00585 next = strchr(pathname+2, '/'); 00586 if (next == NULL) 00587 return PathMsg(ErrMsg, pathname, "badly formed remote directory"); 00588 00589 // Skip share name. 00590 next = strchr(next+1, '/'); 00591 if (next == NULL) 00592 return PathMsg(ErrMsg, pathname,"badly formed remote directory"); 00593 00594 next++; 00595 if (*next == 0) 00596 return PathMsg(ErrMsg, pathname, "badly formed remote directory"); 00597 00598 } else { 00599 if (pathname[1] == ':') 00600 next += 2; // skip drive letter 00601 if (*next == '/') 00602 next++; // skip root directory 00603 } 00604 00605 // If we're supposed to create intermediate directories 00606 if (create_parents) { 00607 // Loop through the directory components until we're done 00608 while (*next) { 00609 next = strchr(next, '/'); 00610 *next = 0; 00611 if (!CreateDirectory(pathname, NULL)) 00612 return MakeErrMsg(ErrMsg, 00613 std::string(pathname) + ": Can't create directory: "); 00614 *next++ = '/'; 00615 } 00616 } else { 00617 // Drop trailing slash. 00618 pathname[len-1] = 0; 00619 if (!CreateDirectory(pathname, NULL)) { 00620 return MakeErrMsg(ErrMsg, std::string(pathname) + ": Can't create directory: "); 00621 } 00622 } 00623 return false; 00624 } 00625 00626 bool 00627 Path::createFileOnDisk(std::string* ErrMsg) { 00628 // Create the file 00629 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW, 00630 FILE_ATTRIBUTE_NORMAL, NULL); 00631 if (h == INVALID_HANDLE_VALUE) 00632 return MakeErrMsg(ErrMsg, path + ": Can't create file: "); 00633 00634 CloseHandle(h); 00635 return false; 00636 } 00637 00638 bool 00639 Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const { 00640 WIN32_FILE_ATTRIBUTE_DATA fi; 00641 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) 00642 return true; 00643 00644 if (fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { 00645 // If it doesn't exist, we're done. 00646 if (!exists()) 00647 return false; 00648 00649 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3)); 00650 int lastchar = path.length() - 1 ; 00651 path.copy(pathname, lastchar+1); 00652 00653 // Make path end with '/*'. 00654 if (pathname[lastchar] != '/') 00655 pathname[++lastchar] = '/'; 00656 pathname[lastchar+1] = '*'; 00657 pathname[lastchar+2] = 0; 00658 00659 if (remove_contents) { 00660 WIN32_FIND_DATA fd; 00661 HANDLE h = FindFirstFile(pathname, &fd); 00662 00663 // It's a bad idea to alter the contents of a directory while enumerating 00664 // its contents. So build a list of its contents first, then destroy them. 00665 00666 if (h != INVALID_HANDLE_VALUE) { 00667 std::vector<Path> list; 00668 00669 do { 00670 if (strcmp(fd.cFileName, ".") == 0) 00671 continue; 00672 if (strcmp(fd.cFileName, "..") == 0) 00673 continue; 00674 00675 Path aPath(path); 00676 aPath.appendComponent(&fd.cFileName[0]); 00677 list.push_back(aPath); 00678 } while (FindNextFile(h, &fd)); 00679 00680 DWORD err = GetLastError(); 00681 FindClose(h); 00682 if (err != ERROR_NO_MORE_FILES) { 00683 SetLastError(err); 00684 return MakeErrMsg(ErrStr, path + ": Can't read directory: "); 00685 } 00686 00687 for (std::vector<Path>::iterator I = list.begin(); I != list.end(); 00688 ++I) { 00689 Path &aPath = *I; 00690 aPath.eraseFromDisk(true); 00691 } 00692 } else { 00693 if (GetLastError() != ERROR_FILE_NOT_FOUND) 00694 return MakeErrMsg(ErrStr, path + ": Can't read directory: "); 00695 } 00696 } 00697 00698 pathname[lastchar] = 0; 00699 if (!RemoveDirectory(pathname)) 00700 return MakeErrMsg(ErrStr, 00701 std::string(pathname) + ": Can't destroy directory: "); 00702 return false; 00703 } else { 00704 // Read-only files cannot be deleted on Windows. Must remove the read-only 00705 // attribute first. 00706 if (fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) { 00707 if (!SetFileAttributes(path.c_str(), 00708 fi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY)) 00709 return MakeErrMsg(ErrStr, path + ": Can't destroy file: "); 00710 } 00711 00712 if (!DeleteFile(path.c_str())) 00713 return MakeErrMsg(ErrStr, path + ": Can't destroy file: "); 00714 return false; 00715 } 00716 } 00717 00718 bool Path::getMagicNumber(std::string& Magic, unsigned len) const { 00719 assert(len < 1024 && "Request for magic string too long"); 00720 char* buf = (char*) alloca(1 + len); 00721 00722 HANDLE h = CreateFile(path.c_str(), 00723 GENERIC_READ, 00724 FILE_SHARE_READ, 00725 NULL, 00726 OPEN_EXISTING, 00727 FILE_ATTRIBUTE_NORMAL, 00728 NULL); 00729 if (h == INVALID_HANDLE_VALUE) 00730 return false; 00731 00732 DWORD nRead = 0; 00733 BOOL ret = ReadFile(h, buf, len, &nRead, NULL); 00734 CloseHandle(h); 00735 00736 if (!ret || nRead != len) 00737 return false; 00738 00739 buf[len] = '\0'; 00740 Magic = buf; 00741 return true; 00742 } 00743 00744 bool 00745 Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) { 00746 if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING)) 00747 return MakeErrMsg(ErrMsg, "Can't move '" + path + "' to '" + newName.path 00748 + "': "); 00749 return false; 00750 } 00751 00752 bool 00753 Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrMsg) const { 00754 // FIXME: should work on directories also. 00755 if (!si.isFile) { 00756 return true; 00757 } 00758 00759 HANDLE h = CreateFile(path.c_str(), 00760 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES, 00761 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 00762 NULL, 00763 OPEN_EXISTING, 00764 FILE_ATTRIBUTE_NORMAL, 00765 NULL); 00766 if (h == INVALID_HANDLE_VALUE) 00767 return true; 00768 00769 BY_HANDLE_FILE_INFORMATION bhfi; 00770 if (!GetFileInformationByHandle(h, &bhfi)) { 00771 DWORD err = GetLastError(); 00772 CloseHandle(h); 00773 SetLastError(err); 00774 return MakeErrMsg(ErrMsg, path + ": GetFileInformationByHandle: "); 00775 } 00776 00777 FILETIME ft; 00778 (uint64_t&)ft = si.modTime.toWin32Time(); 00779 BOOL ret = SetFileTime(h, NULL, &ft, &ft); 00780 DWORD err = GetLastError(); 00781 CloseHandle(h); 00782 if (!ret) { 00783 SetLastError(err); 00784 return MakeErrMsg(ErrMsg, path + ": SetFileTime: "); 00785 } 00786 00787 // Best we can do with Unix permission bits is to interpret the owner 00788 // writable bit. 00789 if (si.mode & 0200) { 00790 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) { 00791 if (!SetFileAttributes(path.c_str(), 00792 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY)) 00793 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: "); 00794 } 00795 } else { 00796 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) { 00797 if (!SetFileAttributes(path.c_str(), 00798 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY)) 00799 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: "); 00800 } 00801 } 00802 00803 return false; 00804 } 00805 00806 bool 00807 CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg) { 00808 // Can't use CopyFile macro defined in Windows.h because it would mess up the 00809 // above line. We use the expansion it would have in a non-UNICODE build. 00810 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false)) 00811 return MakeErrMsg(ErrMsg, "Can't copy '" + Src.toString() + 00812 "' to '" + Dest.toString() + "': "); 00813 return false; 00814 } 00815 00816 bool 00817 Path::makeUnique(bool reuse_current, std::string* ErrMsg) { 00818 if (reuse_current && !exists()) 00819 return false; // File doesn't exist already, just use it! 00820 00821 // Reserve space for -XXXXXX at the end. 00822 char *FNBuffer = (char*) alloca(path.size()+8); 00823 unsigned offset = path.size(); 00824 path.copy(FNBuffer, offset); 00825 00826 // Find a numeric suffix that isn't used by an existing file. Assume there 00827 // won't be more than 1 million files with the same prefix. Probably a safe 00828 // bet. 00829 static unsigned FCounter = 0; 00830 do { 00831 sprintf(FNBuffer+offset, "-%06u", FCounter); 00832 if (++FCounter > 999999) 00833 FCounter = 0; 00834 path = FNBuffer; 00835 } while (exists()); 00836 return false; 00837 } 00838 00839 bool 00840 Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) { 00841 // Make this into a unique file name 00842 makeUnique(reuse_current, ErrMsg); 00843 00844 // Now go and create it 00845 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW, 00846 FILE_ATTRIBUTE_NORMAL, NULL); 00847 if (h == INVALID_HANDLE_VALUE) 00848 return MakeErrMsg(ErrMsg, path + ": can't create file"); 00849 00850 CloseHandle(h); 00851 return false; 00852 } 00853 00854 /// MapInFilePages - Not yet implemented on win32. 00855 const char *Path::MapInFilePages(int FD, uint64_t FileSize) { 00856 return 0; 00857 } 00858 00859 /// MapInFilePages - Not yet implemented on win32. 00860 void Path::UnMapFilePages(const char *Base, uint64_t FileSize) { 00861 assert(0 && "NOT IMPLEMENTED"); 00862 } 00863 00864 } 00865 }