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:
25
#undef GetCurrentDirectory
27
// Windows happily accepts either forward or backward slashes, though any path
28
// returned by a Win32 API will have backward slashes. As LLVM code basically
29
// assumes forward slashes are used, backward slashs are converted where they
30
// can be introduced into a path.
32
// Another invariant is that a path ends with a slash if and only if the path
33
// is a root directory. Any other use of a trailing slash is stripped. Unlike
34
// in Unix, Windows has a rather complicated notion of a root path and this
35
// invariant helps simply the code.
37
static void FlipBackSlashes(std::string& s) {
38
for (size_t i = 0; i < s.size(); i++)
46
const char PathSeparator = ';';
48
StringRef Path::GetEXESuffix() {
52
Path::Path(llvm::StringRef p)
54
FlipBackSlashes(path);
57
Path::Path(const char *StrStart, unsigned StrLen)
58
: path(StrStart, StrLen) {
59
FlipBackSlashes(path);
63
Path::operator=(StringRef that) {
64
path.assign(that.data(), that.size());
65
FlipBackSlashes(path);
70
Path::isValid() const {
74
size_t len = path.size();
75
// If there is a null character, it and all its successors are ignored.
76
size_t pos = path.find_first_of('\0');
77
if (pos != std::string::npos)
80
// If there is a colon, it must be the second character, preceded by a letter
81
// and followed by something.
82
pos = path.rfind(':',len);
84
if (pos != std::string::npos) {
85
if (pos != 1 || !isalpha(path[0]) || len < 3)
90
// Look for a UNC path, and if found adjust our notion of the root slash.
91
if (len > 3 && path[0] == '/' && path[1] == '/') {
92
rootslash = path.find('/', 2);
93
if (rootslash == std::string::npos)
97
// Check for illegal characters.
98
if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012"
99
"\013\014\015\016\017\020\021\022\023\024\025\026"
100
"\027\030\031\032\033\034\035\036\037")
101
!= std::string::npos)
104
// Remove trailing slash, unless it's a root slash.
105
if (len > rootslash+1 && path[len-1] == '/')
108
// Check each component for legality.
109
for (pos = 0; pos < len; ++pos) {
110
// A component may not end in a space.
111
if (path[pos] == ' ') {
112
if (pos+1 == len || path[pos+1] == '/' || path[pos+1] == '\0')
116
// A component may not end in a period.
117
if (path[pos] == '.') {
118
if (pos+1 == len || path[pos+1] == '/') {
119
// Unless it is the pseudo-directory "."...
120
if (pos == 0 || path[pos-1] == '/' || path[pos-1] == ':')
123
if (pos > 0 && path[pos-1] == '.') {
124
if (pos == 1 || path[pos-2] == '/' || path[pos-2] == ':')
135
void Path::makeAbsolute() {
136
TCHAR FullPath[MAX_PATH + 1] = {0};
137
LPTSTR FilePart = NULL;
139
DWORD RetLength = ::GetFullPathNameA(path.c_str(),
140
sizeof(FullPath)/sizeof(FullPath[0]),
141
FullPath, &FilePart);
143
if (0 == RetLength) {
144
// FIXME: Report the error GetLastError()
145
assert(0 && "Unable to make absolute path!");
146
} else if (RetLength > MAX_PATH) {
147
// FIXME: Report too small buffer (needed RetLength bytes).
148
assert(0 && "Unable to make absolute path!");
155
Path::isAbsolute(const char *NameStart, unsigned NameLen) {
157
// FIXME: This does not handle correctly an absolute path starting from
158
// a drive letter or in UNC format.
164
return NameStart[0] == '/';
167
(NameStart[0] == '/' || (NameStart[1] == ':' && NameStart[2] == '/')) ||
168
(NameStart[0] == '\\' || (NameStart[1] == ':' && NameStart[2] == '\\'));
173
Path::isAbsolute() const {
174
// FIXME: This does not handle correctly an absolute path starting from
175
// a drive letter or in UNC format.
176
switch (path.length()) {
181
return path[0] == '/';
183
return path[0] == '/' || (path[1] == ':' && path[2] == '/');
187
static Path *TempDirectory;
190
Path::GetTemporaryDirectory(std::string* ErrMsg) {
192
return *TempDirectory;
194
char pathname[MAX_PATH];
195
if (!GetTempPath(MAX_PATH, pathname)) {
197
*ErrMsg = "Can't determine temporary directory";
202
result.set(pathname);
204
// Append a subdirectory passed on our process id so multiple LLVMs don't
205
// step on each other's toes.
207
// Mingw's Win32 header files are broken.
208
sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId()));
210
sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
212
result.appendComponent(pathname);
214
// If there's a directory left over from a previous LLVM execution that
215
// happened to have the same process id, get rid of it.
216
result.eraseFromDisk(true);
218
// And finally (re-)create the empty directory.
219
result.createDirectoryOnDisk(false);
220
TempDirectory = new Path(result);
221
return *TempDirectory;
224
// FIXME: the following set of functions don't map to Windows very well.
226
Path::GetRootDirectory() {
227
// This is the only notion that that Windows has of a root directory. Nothing
228
// is here except for drives.
229
return Path("file:///");
233
Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
235
// Generic form of C:\Windows\System32
236
HRESULT res = SHGetFolderPathA(NULL,
237
CSIDL_FLAG_CREATE | CSIDL_SYSTEM,
242
assert(0 && "Failed to get system directory");
245
Paths.push_back(sys::Path(buff));
249
// Generic form of C:\Windows
250
res = SHGetFolderPathA(NULL,
251
CSIDL_FLAG_CREATE | CSIDL_WINDOWS,
256
assert(0 && "Failed to get windows directory");
259
Paths.push_back(sys::Path(buff));
263
Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) {
264
char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
266
getPathList(env_var,Paths);
271
if (tmpPath.set(LLVM_LIBDIR))
272
if (tmpPath.canRead())
273
Paths.push_back(tmpPath);
276
GetSystemLibraryPaths(Paths);
280
Path::GetUserHomeDirectory() {
282
HRESULT res = SHGetFolderPathA(NULL,
283
CSIDL_FLAG_CREATE | CSIDL_APPDATA,
288
assert(0 && "Failed to get user home directory");
293
Path::GetCurrentDirectory() {
294
char pathname[MAX_PATH];
295
::GetCurrentDirectoryA(MAX_PATH,pathname);
296
return Path(pathname);
299
/// GetMainExecutable - Return the path to the main executable, given the
300
/// value of argv[0] from program startup.
301
Path Path::GetMainExecutable(const char *argv0, void *MainAddr) {
302
char pathname[MAX_PATH];
303
DWORD ret = ::GetModuleFileNameA(NULL, pathname, MAX_PATH);
304
return ret != MAX_PATH ? Path(pathname) : Path();
308
// FIXME: the above set of functions don't map to Windows very well.
311
StringRef Path::getDirname() const {
312
return getDirnameCharSep(path, "/");
316
Path::getBasename() const {
317
// Find the last slash
318
size_t slash = path.rfind('/');
319
if (slash == std::string::npos)
324
size_t dot = path.rfind('.');
325
if (dot == std::string::npos || dot < slash)
326
return StringRef(path).substr(slash);
328
return StringRef(path).substr(slash, dot - slash);
332
Path::getSuffix() const {
333
// Find the last slash
334
size_t slash = path.rfind('/');
335
if (slash == std::string::npos)
340
size_t dot = path.rfind('.');
341
if (dot == std::string::npos || dot < slash)
342
return StringRef("");
344
return StringRef(path).substr(dot + 1);
348
Path::exists() const {
349
DWORD attr = GetFileAttributes(path.c_str());
350
return attr != INVALID_FILE_ATTRIBUTES;
354
Path::isDirectory() const {
355
DWORD attr = GetFileAttributes(path.c_str());
356
return (attr != INVALID_FILE_ATTRIBUTES) &&
357
(attr & FILE_ATTRIBUTE_DIRECTORY);
361
Path::isSymLink() const {
362
DWORD attributes = GetFileAttributes(path.c_str());
364
if (attributes == INVALID_FILE_ATTRIBUTES)
365
// There's no sane way to report this :(.
366
assert(0 && "GetFileAttributes returned INVALID_FILE_ATTRIBUTES");
368
// This isn't exactly what defines a NTFS symlink, but it is only true for
369
// paths that act like a symlink.
370
return attributes & FILE_ATTRIBUTE_REPARSE_POINT;
374
Path::canRead() const {
375
// FIXME: take security attributes into account.
376
DWORD attr = GetFileAttributes(path.c_str());
377
return attr != INVALID_FILE_ATTRIBUTES;
381
Path::canWrite() const {
382
// FIXME: take security attributes into account.
383
DWORD attr = GetFileAttributes(path.c_str());
384
return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
388
Path::canExecute() const {
389
// FIXME: take security attributes into account.
390
DWORD attr = GetFileAttributes(path.c_str());
391
return attr != INVALID_FILE_ATTRIBUTES;
395
Path::isRegularFile() const {
397
if (fs::is_regular_file(path, res))
403
Path::getLast() const {
404
// Find the last slash
405
size_t pos = path.rfind('/');
407
// Handle the corner cases
408
if (pos == std::string::npos)
411
// If the last character is a slash, we have a root directory
412
if (pos == path.length()-1)
415
// Return everything after the last slash
416
return StringRef(path).substr(pos+1);
420
PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const {
421
if (!fsIsValid || update) {
422
WIN32_FILE_ATTRIBUTE_DATA fi;
423
if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
424
MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) +
425
": Can't get status: ");
429
status.fileSize = fi.nFileSizeHigh;
430
status.fileSize <<= sizeof(fi.nFileSizeHigh)*8;
431
status.fileSize += fi.nFileSizeLow;
433
status.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
434
status.user = 9999; // Not applicable to Windows, so...
435
status.group = 9999; // Not applicable to Windows, so...
437
// FIXME: this is only unique if the file is accessed by the same file path.
438
// How do we do this for C:\dir\file and ..\dir\file ? Unix has inode
439
// numbers, but the concept doesn't exist in Windows.
441
for (unsigned i = 0; i < path.length(); ++i)
442
status.uniqueID += path[i];
445
ui.LowPart = fi.ftLastWriteTime.dwLowDateTime;
446
ui.HighPart = fi.ftLastWriteTime.dwHighDateTime;
447
status.modTime.fromWin32Time(ui.QuadPart);
449
status.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
455
bool Path::makeReadableOnDisk(std::string* ErrMsg) {
456
// All files are readable on Windows (ignoring security attributes).
460
bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
461
DWORD attr = GetFileAttributes(path.c_str());
463
// If it doesn't exist, we're done.
464
if (attr == INVALID_FILE_ATTRIBUTES)
467
if (attr & FILE_ATTRIBUTE_READONLY) {
468
if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) {
469
MakeErrMsg(ErrMsg, std::string(path) + ": Can't make file writable: ");
476
bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
477
// All files are executable on Windows (ignoring security attributes).
482
Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
483
WIN32_FILE_ATTRIBUTE_DATA fi;
484
if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
485
MakeErrMsg(ErrMsg, path + ": can't get status of file");
489
if (!(fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
491
*ErrMsg = path + ": not a directory";
497
std::string searchpath = path;
498
if (path.size() == 0 || searchpath[path.size()-1] == '/')
503
HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
504
if (h == INVALID_HANDLE_VALUE) {
505
if (GetLastError() == ERROR_FILE_NOT_FOUND)
506
return true; // not really an error, now is it?
507
MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
512
if (fd.cFileName[0] == '.')
515
aPath.appendComponent(&fd.cFileName[0]);
516
result.insert(aPath);
517
} while (FindNextFile(h, &fd));
519
DWORD err = GetLastError();
521
if (err != ERROR_NO_MORE_FILES) {
523
MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
530
Path::set(StringRef a_path) {
533
std::string save(path);
535
FlipBackSlashes(path);
544
Path::appendComponent(StringRef name) {
547
std::string save(path);
549
size_t last = path.size() - 1;
550
if (path[last] != '/')
562
Path::eraseComponent() {
563
size_t slashpos = path.rfind('/',path.size());
564
if (slashpos == path.size() - 1 || slashpos == std::string::npos)
566
std::string save(path);
567
path.erase(slashpos);
576
Path::eraseSuffix() {
577
size_t dotpos = path.rfind('.',path.size());
578
size_t slashpos = path.rfind('/',path.size());
579
if (dotpos != std::string::npos) {
580
if (slashpos == std::string::npos || dotpos > slashpos+1) {
581
std::string save(path);
582
path.erase(dotpos, path.size()-dotpos);
593
inline bool PathMsg(std::string* ErrMsg, const char* pathname, const char*msg) {
595
*ErrMsg = std::string(pathname) + ": " + std::string(msg);
600
Path::createDirectoryOnDisk(bool create_parents, std::string* ErrMsg) {
601
// Get a writeable copy of the path name
602
size_t len = path.length();
603
char *pathname = reinterpret_cast<char *>(_alloca(len+2));
604
path.copy(pathname, len);
607
// Make sure it ends with a slash.
608
if (len == 0 || pathname[len - 1] != '/') {
613
// Determine starting point for initial / search.
614
char *next = pathname;
615
if (pathname[0] == '/' && pathname[1] == '/') {
617
next = strchr(pathname+2, '/');
619
return PathMsg(ErrMsg, pathname, "badly formed remote directory");
622
next = strchr(next+1, '/');
624
return PathMsg(ErrMsg, pathname,"badly formed remote directory");
628
return PathMsg(ErrMsg, pathname, "badly formed remote directory");
631
if (pathname[1] == ':')
632
next += 2; // skip drive letter
634
next++; // skip root directory
637
// If we're supposed to create intermediate directories
638
if (create_parents) {
639
// Loop through the directory components until we're done
641
next = strchr(next, '/');
643
if (!CreateDirectory(pathname, NULL) &&
644
GetLastError() != ERROR_ALREADY_EXISTS)
645
return MakeErrMsg(ErrMsg,
646
std::string(pathname) + ": Can't create directory: ");
650
// Drop trailing slash.
652
if (!CreateDirectory(pathname, NULL) &&
653
GetLastError() != ERROR_ALREADY_EXISTS) {
654
return MakeErrMsg(ErrMsg, std::string(pathname) +
655
": Can't create directory: ");
662
Path::createFileOnDisk(std::string* ErrMsg) {
664
HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
665
FILE_ATTRIBUTE_NORMAL, NULL);
666
if (h == INVALID_HANDLE_VALUE)
667
return MakeErrMsg(ErrMsg, path + ": Can't create file: ");
674
Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
675
WIN32_FILE_ATTRIBUTE_DATA fi;
676
if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
679
if (fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
680
// If it doesn't exist, we're done.
682
if (fs::exists(path, Exists) || !Exists)
685
char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
686
int lastchar = path.length() - 1 ;
687
path.copy(pathname, lastchar+1);
689
// Make path end with '/*'.
690
if (pathname[lastchar] != '/')
691
pathname[++lastchar] = '/';
692
pathname[lastchar+1] = '*';
693
pathname[lastchar+2] = 0;
695
if (remove_contents) {
697
HANDLE h = FindFirstFile(pathname, &fd);
699
// It's a bad idea to alter the contents of a directory while enumerating
700
// its contents. So build a list of its contents first, then destroy them.
702
if (h != INVALID_HANDLE_VALUE) {
703
std::vector<Path> list;
706
if (strcmp(fd.cFileName, ".") == 0)
708
if (strcmp(fd.cFileName, "..") == 0)
712
aPath.appendComponent(&fd.cFileName[0]);
713
list.push_back(aPath);
714
} while (FindNextFile(h, &fd));
716
DWORD err = GetLastError();
718
if (err != ERROR_NO_MORE_FILES) {
720
return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
723
for (std::vector<Path>::iterator I = list.begin(); I != list.end();
726
aPath.eraseFromDisk(true);
729
if (GetLastError() != ERROR_FILE_NOT_FOUND)
730
return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
734
pathname[lastchar] = 0;
735
if (!RemoveDirectory(pathname))
736
return MakeErrMsg(ErrStr,
737
std::string(pathname) + ": Can't destroy directory: ");
740
// Read-only files cannot be deleted on Windows. Must remove the read-only
742
if (fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
743
if (!SetFileAttributes(path.c_str(),
744
fi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
745
return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
748
if (!DeleteFile(path.c_str()))
749
return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
754
bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
755
assert(len < 1024 && "Request for magic string too long");
756
char* buf = reinterpret_cast<char*>(alloca(len));
758
HANDLE h = CreateFile(path.c_str(),
763
FILE_ATTRIBUTE_NORMAL,
765
if (h == INVALID_HANDLE_VALUE)
769
BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
772
if (!ret || nRead != len)
775
Magic = std::string(buf, len);
780
Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
781
if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING))
782
return MakeErrMsg(ErrMsg, "Can't move '" + path + "' to '" + newName.path
788
Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrMsg) const {
789
// FIXME: should work on directories also.
794
HANDLE h = CreateFile(path.c_str(),
795
FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
796
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
799
FILE_ATTRIBUTE_NORMAL,
801
if (h == INVALID_HANDLE_VALUE)
804
BY_HANDLE_FILE_INFORMATION bhfi;
805
if (!GetFileInformationByHandle(h, &bhfi)) {
806
DWORD err = GetLastError();
809
return MakeErrMsg(ErrMsg, path + ": GetFileInformationByHandle: ");
813
ui.QuadPart = si.modTime.toWin32Time();
815
ft.dwLowDateTime = ui.LowPart;
816
ft.dwHighDateTime = ui.HighPart;
817
BOOL ret = SetFileTime(h, NULL, &ft, &ft);
818
DWORD err = GetLastError();
822
return MakeErrMsg(ErrMsg, path + ": SetFileTime: ");
825
// Best we can do with Unix permission bits is to interpret the owner
827
if (si.mode & 0200) {
828
if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
829
if (!SetFileAttributes(path.c_str(),
830
bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
831
return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
834
if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
835
if (!SetFileAttributes(path.c_str(),
836
bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
837
return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
845
CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg) {
846
// Can't use CopyFile macro defined in Windows.h because it would mess up the
847
// above line. We use the expansion it would have in a non-UNICODE build.
848
if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
849
return MakeErrMsg(ErrMsg, "Can't copy '" + Src.str() +
850
"' to '" + Dest.str() + "': ");
855
Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
857
if (reuse_current && (fs::exists(path, Exists) || !Exists))
858
return false; // File doesn't exist already, just use it!
860
// Reserve space for -XXXXXX at the end.
861
char *FNBuffer = (char*) alloca(path.size()+8);
862
unsigned offset = path.size();
863
path.copy(FNBuffer, offset);
865
// Find a numeric suffix that isn't used by an existing file. Assume there
866
// won't be more than 1 million files with the same prefix. Probably a safe
868
static int FCounter = -1;
870
// Give arbitrary initial seed.
871
// FIXME: We should use sys::fs::unique_file() in future.
873
DWORD x = GetCurrentProcessId();
874
x = (x << 16) | (x >> 16);
875
if (QueryPerformanceCounter(&cnt64)) // RDTSC
876
x ^= cnt64.HighPart ^ cnt64.LowPart;
877
FCounter = x % 1000000;
880
sprintf(FNBuffer+offset, "-%06u", FCounter);
881
if (++FCounter > 999999)
884
} while (!fs::exists(path, Exists) && Exists);
889
Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
890
// Make this into a unique file name
891
makeUnique(reuse_current, ErrMsg);
893
// Now go and create it
894
HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
895
FILE_ATTRIBUTE_NORMAL, NULL);
896
if (h == INVALID_HANDLE_VALUE)
897
return MakeErrMsg(ErrMsg, path + ": can't create file");
903
/// MapInFilePages - Not yet implemented on win32.
904
const char *Path::MapInFilePages(int FD, size_t FileSize, off_t Offset) {
908
/// MapInFilePages - Not yet implemented on win32.
909
void Path::UnMapFilePages(const char *Base, size_t FileSize) {
910
assert(0 && "NOT IMPLEMENTED");