1 //===- llvm/Support/Win32/Path.cpp - Win32 Path Implementation ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the Win32 specific implementation of the Path class.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic Win32 code that
16 //=== is guaranteed to work on *all* Win32 variants.
17 //===----------------------------------------------------------------------===//
23 // We need to undo a macro defined in Windows.h, otherwise we won't compile:
24 #undef GetCurrentDirectory
26 // Windows happily accepts either forward or backward slashes, though any path
27 // returned by a Win32 API will have backward slashes. As LLVM code basically
28 // assumes forward slashes are used, backward slashs are converted where they
29 // can be introduced into a path.
31 // Another invariant is that a path ends with a slash if and only if the path
32 // is a root directory. Any other use of a trailing slash is stripped. Unlike
33 // in Unix, Windows has a rather complicated notion of a root path and this
34 // invariant helps simply the code.
36 static void FlipBackSlashes(std::string& s) {
37 for (size_t i = 0; i < s.size(); i++)
45 StringRef Path::GetEXESuffix() {
49 Path::Path(llvm::StringRef p)
51 FlipBackSlashes(path);
54 Path::Path(const char *StrStart, unsigned StrLen)
55 : path(StrStart, StrLen) {
56 FlipBackSlashes(path);
60 Path::operator=(StringRef that) {
61 path.assign(that.data(), that.size());
62 FlipBackSlashes(path);
67 Path::isValid() const {
71 size_t len = path.size();
72 // If there is a null character, it and all its successors are ignored.
73 size_t pos = path.find_first_of('\0');
74 if (pos != std::string::npos)
77 // If there is a colon, it must be the second character, preceded by a letter
78 // and followed by something.
79 pos = path.rfind(':',len);
81 if (pos != std::string::npos) {
82 if (pos != 1 || !isalpha(static_cast<unsigned char>(path[0])) || len < 3)
87 // Look for a UNC path, and if found adjust our notion of the root slash.
88 if (len > 3 && path[0] == '/' && path[1] == '/') {
89 rootslash = path.find('/', 2);
90 if (rootslash == std::string::npos)
94 // Check for illegal characters.
95 if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012"
96 "\013\014\015\016\017\020\021\022\023\024\025\026"
97 "\027\030\031\032\033\034\035\036\037")
101 // Remove trailing slash, unless it's a root slash.
102 if (len > rootslash+1 && path[len-1] == '/')
105 // Check each component for legality.
106 for (pos = 0; pos < len; ++pos) {
107 // A component may not end in a space.
108 if (path[pos] == ' ') {
109 if (pos+1 == len || path[pos+1] == '/' || path[pos+1] == '\0')
113 // A component may not end in a period.
114 if (path[pos] == '.') {
115 if (pos+1 == len || path[pos+1] == '/') {
116 // Unless it is the pseudo-directory "."...
117 if (pos == 0 || path[pos-1] == '/' || path[pos-1] == ':')
120 if (pos > 0 && path[pos-1] == '.') {
121 if (pos == 1 || path[pos-2] == '/' || path[pos-2] == ':')
132 void Path::makeAbsolute() {
133 TCHAR FullPath[MAX_PATH + 1] = {0};
134 LPTSTR FilePart = NULL;
136 DWORD RetLength = ::GetFullPathNameA(path.c_str(),
137 sizeof(FullPath)/sizeof(FullPath[0]),
138 FullPath, &FilePart);
140 if (0 == RetLength) {
141 // FIXME: Report the error GetLastError()
142 assert(0 && "Unable to make absolute path!");
143 } else if (RetLength > MAX_PATH) {
144 // FIXME: Report too small buffer (needed RetLength bytes).
145 assert(0 && "Unable to make absolute path!");
151 static Path *TempDirectory;
154 Path::GetTemporaryDirectory(std::string* ErrMsg) {
156 #if defined(_MSC_VER)
157 // Visual Studio gets confused and emits a diagnostic about calling exists,
158 // even though this is the implementation for PathV1. Temporarily
159 // disable the deprecated warning message
160 #pragma warning(push)
161 #pragma warning(disable:4996)
163 assert(TempDirectory->exists() && "Who has removed TempDirectory?");
164 #if defined(_MSC_VER)
167 return *TempDirectory;
170 char pathname[MAX_PATH];
171 if (!GetTempPath(MAX_PATH, pathname)) {
173 *ErrMsg = "Can't determine temporary directory";
178 result.set(pathname);
180 // Append a subdirectory based on our process id so multiple LLVMs don't
181 // step on each other's toes.
183 // Mingw's Win32 header files are broken.
184 sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId()));
186 sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
188 result.appendComponent(pathname);
190 // If there's a directory left over from a previous LLVM execution that
191 // happened to have the same process id, get rid of it.
192 result.eraseFromDisk(true);
194 // And finally (re-)create the empty directory.
195 result.createDirectoryOnDisk(false);
196 TempDirectory = new Path(result);
197 return *TempDirectory;
201 Path::GetCurrentDirectory() {
202 char pathname[MAX_PATH];
203 ::GetCurrentDirectoryA(MAX_PATH,pathname);
204 return Path(pathname);
207 /// GetMainExecutable - Return the path to the main executable, given the
208 /// value of argv[0] from program startup.
209 Path Path::GetMainExecutable(const char *argv0, void *MainAddr) {
210 char pathname[MAX_PATH];
211 DWORD ret = ::GetModuleFileNameA(NULL, pathname, MAX_PATH);
212 return ret != MAX_PATH ? Path(pathname) : Path();
216 // FIXME: the above set of functions don't map to Windows very well.
219 Path::exists() const {
220 DWORD attr = GetFileAttributes(path.c_str());
221 return attr != INVALID_FILE_ATTRIBUTES;
225 Path::isDirectory() const {
226 DWORD attr = GetFileAttributes(path.c_str());
227 return (attr != INVALID_FILE_ATTRIBUTES) &&
228 (attr & FILE_ATTRIBUTE_DIRECTORY);
232 Path::isSymLink() const {
233 DWORD attributes = GetFileAttributes(path.c_str());
235 if (attributes == INVALID_FILE_ATTRIBUTES)
236 // There's no sane way to report this :(.
237 assert(0 && "GetFileAttributes returned INVALID_FILE_ATTRIBUTES");
239 // This isn't exactly what defines a NTFS symlink, but it is only true for
240 // paths that act like a symlink.
241 return attributes & FILE_ATTRIBUTE_REPARSE_POINT;
245 Path::isRegularFile() const {
247 if (fs::is_regular_file(path, res))
253 PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const {
254 if (!fsIsValid || update) {
255 WIN32_FILE_ATTRIBUTE_DATA fi;
256 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
257 MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) +
258 ": Can't get status: ");
262 status.fileSize = fi.nFileSizeHigh;
263 status.fileSize <<= sizeof(fi.nFileSizeHigh)*8;
264 status.fileSize += fi.nFileSizeLow;
266 status.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
267 status.user = 9999; // Not applicable to Windows, so...
268 status.group = 9999; // Not applicable to Windows, so...
271 ui.LowPart = fi.ftLastWriteTime.dwLowDateTime;
272 ui.HighPart = fi.ftLastWriteTime.dwHighDateTime;
273 status.modTime.fromWin32Time(ui.QuadPart);
275 status.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
281 bool Path::makeReadableOnDisk(std::string* ErrMsg) {
282 // All files are readable on Windows (ignoring security attributes).
286 bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
287 DWORD attr = GetFileAttributes(path.c_str());
289 // If it doesn't exist, we're done.
290 if (attr == INVALID_FILE_ATTRIBUTES)
293 if (attr & FILE_ATTRIBUTE_READONLY) {
294 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) {
295 MakeErrMsg(ErrMsg, std::string(path) + ": Can't make file writable: ");
303 Path::set(StringRef a_path) {
306 std::string save(path);
308 FlipBackSlashes(path);
317 Path::appendComponent(StringRef name) {
320 std::string save(path);
322 size_t last = path.size() - 1;
323 if (path[last] != '/')
335 Path::eraseComponent() {
336 size_t slashpos = path.rfind('/',path.size());
337 if (slashpos == path.size() - 1 || slashpos == std::string::npos)
339 std::string save(path);
340 path.erase(slashpos);
349 Path::eraseSuffix() {
350 size_t dotpos = path.rfind('.',path.size());
351 size_t slashpos = path.rfind('/',path.size());
352 if (dotpos != std::string::npos) {
353 if (slashpos == std::string::npos || dotpos > slashpos+1) {
354 std::string save(path);
355 path.erase(dotpos, path.size()-dotpos);
366 inline bool PathMsg(std::string* ErrMsg, const char* pathname, const char*msg) {
368 *ErrMsg = std::string(pathname) + ": " + std::string(msg);
373 Path::createDirectoryOnDisk(bool create_parents, std::string* ErrMsg) {
374 // Get a writeable copy of the path name
375 size_t len = path.length();
376 char *pathname = reinterpret_cast<char *>(_alloca(len+2));
377 path.copy(pathname, len);
380 // Make sure it ends with a slash.
381 if (len == 0 || pathname[len - 1] != '/') {
386 // Determine starting point for initial / search.
387 char *next = pathname;
388 if (pathname[0] == '/' && pathname[1] == '/') {
390 next = strchr(pathname+2, '/');
392 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
395 next = strchr(next+1, '/');
397 return PathMsg(ErrMsg, pathname,"badly formed remote directory");
401 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
404 if (pathname[1] == ':')
405 next += 2; // skip drive letter
407 next++; // skip root directory
410 // If we're supposed to create intermediate directories
411 if (create_parents) {
412 // Loop through the directory components until we're done
414 next = strchr(next, '/');
416 if (!CreateDirectory(pathname, NULL) &&
417 GetLastError() != ERROR_ALREADY_EXISTS)
418 return MakeErrMsg(ErrMsg,
419 std::string(pathname) + ": Can't create directory: ");
423 // Drop trailing slash.
425 if (!CreateDirectory(pathname, NULL) &&
426 GetLastError() != ERROR_ALREADY_EXISTS) {
427 return MakeErrMsg(ErrMsg, std::string(pathname) +
428 ": Can't create directory: ");
435 Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
436 WIN32_FILE_ATTRIBUTE_DATA fi;
437 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
440 if (fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
441 // If it doesn't exist, we're done.
443 if (fs::exists(path, Exists) || !Exists)
446 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
447 int lastchar = path.length() - 1 ;
448 path.copy(pathname, lastchar+1);
450 // Make path end with '/*'.
451 if (pathname[lastchar] != '/')
452 pathname[++lastchar] = '/';
453 pathname[lastchar+1] = '*';
454 pathname[lastchar+2] = 0;
456 if (remove_contents) {
458 HANDLE h = FindFirstFile(pathname, &fd);
460 // It's a bad idea to alter the contents of a directory while enumerating
461 // its contents. So build a list of its contents first, then destroy them.
463 if (h != INVALID_HANDLE_VALUE) {
464 std::vector<Path> list;
467 if (strcmp(fd.cFileName, ".") == 0)
469 if (strcmp(fd.cFileName, "..") == 0)
473 aPath.appendComponent(&fd.cFileName[0]);
474 list.push_back(aPath);
475 } while (FindNextFile(h, &fd));
477 DWORD err = GetLastError();
479 if (err != ERROR_NO_MORE_FILES) {
481 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
484 for (std::vector<Path>::iterator I = list.begin(); I != list.end();
487 aPath.eraseFromDisk(true);
490 if (GetLastError() != ERROR_FILE_NOT_FOUND)
491 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
495 pathname[lastchar] = 0;
496 if (!RemoveDirectory(pathname))
497 return MakeErrMsg(ErrStr,
498 std::string(pathname) + ": Can't destroy directory: ");
501 // Read-only files cannot be deleted on Windows. Must remove the read-only
503 if (fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
504 if (!SetFileAttributes(path.c_str(),
505 fi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
506 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
509 if (!DeleteFile(path.c_str()))
510 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
516 Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
517 if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING))
518 return MakeErrMsg(ErrMsg, "Can't move '" + path + "' to '" + newName.path
524 Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrMsg) const {
525 // FIXME: should work on directories also.
530 HANDLE h = CreateFile(path.c_str(),
531 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
532 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
535 FILE_ATTRIBUTE_NORMAL,
537 if (h == INVALID_HANDLE_VALUE)
540 BY_HANDLE_FILE_INFORMATION bhfi;
541 if (!GetFileInformationByHandle(h, &bhfi)) {
542 DWORD err = GetLastError();
545 return MakeErrMsg(ErrMsg, path + ": GetFileInformationByHandle: ");
549 ui.QuadPart = si.modTime.toWin32Time();
551 ft.dwLowDateTime = ui.LowPart;
552 ft.dwHighDateTime = ui.HighPart;
553 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
554 DWORD err = GetLastError();
558 return MakeErrMsg(ErrMsg, path + ": SetFileTime: ");
561 // Best we can do with Unix permission bits is to interpret the owner
563 if (si.mode & 0200) {
564 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
565 if (!SetFileAttributes(path.c_str(),
566 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
567 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
570 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
571 if (!SetFileAttributes(path.c_str(),
572 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
573 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
581 Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
583 if (reuse_current && (fs::exists(path, Exists) || !Exists))
584 return false; // File doesn't exist already, just use it!
586 // Reserve space for -XXXXXX at the end.
587 char *FNBuffer = (char*) alloca(path.size()+8);
588 unsigned offset = path.size();
589 path.copy(FNBuffer, offset);
591 // Find a numeric suffix that isn't used by an existing file. Assume there
592 // won't be more than 1 million files with the same prefix. Probably a safe
594 static int FCounter = -1;
596 // Give arbitrary initial seed.
597 // FIXME: We should use sys::fs::unique_file() in future.
599 DWORD x = GetCurrentProcessId();
600 x = (x << 16) | (x >> 16);
601 if (QueryPerformanceCounter(&cnt64)) // RDTSC
602 x ^= cnt64.HighPart ^ cnt64.LowPart;
603 FCounter = x % 1000000;
606 sprintf(FNBuffer+offset, "-%06u", FCounter);
607 if (++FCounter > 999999)
610 } while (!fs::exists(path, Exists) && Exists);
615 Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
616 // Make this into a unique file name
617 makeUnique(reuse_current, ErrMsg);
619 // Now go and create it
620 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
621 FILE_ATTRIBUTE_NORMAL, NULL);
622 if (h == INVALID_HANDLE_VALUE)
623 return MakeErrMsg(ErrMsg, path + ": can't create file");