blob: 7d9652916130015ce12d4f97c58ddbcba7f8c8e1 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- llvm/System/Linux/Path.cpp - Linux Path Implementation ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8// Modified by Henrik Bach to comply with at least MinGW.
9// Ported to Win32 by Jeff Cohen.
10//
11//===----------------------------------------------------------------------===//
12//
13// This file provides the Win32 specific implementation of the Path class.
14//
15//===----------------------------------------------------------------------===//
16
17//===----------------------------------------------------------------------===//
18//=== WARNING: Implementation here must contain only generic Win32 code that
19//=== is guaranteed to work on *all* Win32 variants.
20//===----------------------------------------------------------------------===//
21
22#include "Win32.h"
23#include <malloc.h>
24
25// We need to undo a macro defined in Windows.h, otherwise we won't compile:
26#undef CopyFile
27
28// Windows happily accepts either forward or backward slashes, though any path
29// returned by a Win32 API will have backward slashes. As LLVM code basically
30// assumes forward slashes are used, backward slashs are converted where they
31// can be introduced into a path.
32//
33// Another invariant is that a path ends with a slash if and only if the path
34// is a root directory. Any other use of a trailing slash is stripped. Unlike
35// in Unix, Windows has a rather complicated notion of a root path and this
36// invariant helps simply the code.
37
38static void FlipBackSlashes(std::string& s) {
39 for (size_t i = 0; i < s.size(); i++)
40 if (s[i] == '\\')
41 s[i] = '/';
42}
43
44namespace llvm {
45namespace sys {
46
47bool
48Path::isValid() const {
49 if (path.empty())
50 return false;
51
52 // If there is a colon, it must be the second character, preceded by a letter
53 // and followed by something.
54 size_t len = path.size();
55 size_t pos = path.rfind(':',len);
56 size_t rootslash = 0;
57 if (pos != std::string::npos) {
58 if (pos != 1 || !isalpha(path[0]) || len < 3)
59 return false;
60 rootslash = 2;
61 }
62
63 // Look for a UNC path, and if found adjust our notion of the root slash.
64 if (len > 3 && path[0] == '/' && path[1] == '/') {
65 rootslash = path.find('/', 2);
66 if (rootslash == std::string::npos)
67 rootslash = 0;
68 }
69
70 // Check for illegal characters.
71 if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012"
72 "\013\014\015\016\017\020\021\022\023\024\025\026"
73 "\027\030\031\032\033\034\035\036\037")
74 != std::string::npos)
75 return false;
76
77 // Remove trailing slash, unless it's a root slash.
78 if (len > rootslash+1 && path[len-1] == '/')
79 path.erase(--len);
80
81 // Check each component for legality.
82 for (pos = 0; pos < len; ++pos) {
83 // A component may not end in a space.
84 if (path[pos] == ' ') {
85 if (path[pos+1] == '/' || path[pos+1] == '\0')
86 return false;
87 }
88
89 // A component may not end in a period.
90 if (path[pos] == '.') {
91 if (path[pos+1] == '/' || path[pos+1] == '\0') {
92 // Unless it is the pseudo-directory "."...
93 if (pos == 0 || path[pos-1] == '/' || path[pos-1] == ':')
94 return true;
95 // or "..".
96 if (pos > 0 && path[pos-1] == '.') {
97 if (pos == 1 || path[pos-2] == '/' || path[pos-2] == ':')
98 return true;
99 }
100 return false;
101 }
102 }
103 }
104
105 return true;
106}
107
108bool
109Path::isAbsolute() const {
110 switch (path.length()) {
111 case 0:
112 return false;
113 case 1:
114 case 2:
115 return path[0] == '/';
116 default:
117 return path[0] == '/' || (path[1] == ':' && path[2] == '/');
118 }
119}
120
121static Path *TempDirectory = NULL;
122
123Path
124Path::GetTemporaryDirectory(std::string* ErrMsg) {
125 if (TempDirectory)
126 return *TempDirectory;
127
128 char pathname[MAX_PATH];
129 if (!GetTempPath(MAX_PATH, pathname)) {
130 if (ErrMsg)
131 *ErrMsg = "Can't determine temporary directory";
132 return Path();
133 }
134
135 Path result;
136 result.set(pathname);
137
138 // Append a subdirectory passed on our process id so multiple LLVMs don't
139 // step on each other's toes.
140#ifdef __MINGW32__
141 // Mingw's Win32 header files are broken.
142 sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId()));
143#else
144 sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
145#endif
146 result.appendComponent(pathname);
147
148 // If there's a directory left over from a previous LLVM execution that
149 // happened to have the same process id, get rid of it.
150 result.eraseFromDisk(true);
151
152 // And finally (re-)create the empty directory.
153 result.createDirectoryOnDisk(false);
154 TempDirectory = new Path(result);
155 return *TempDirectory;
156}
157
158// FIXME: the following set of functions don't map to Windows very well.
159Path
160Path::GetRootDirectory() {
161 Path result;
162 result.set("C:/");
163 return result;
164}
165
166static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
167 const char* at = path;
168 const char* delim = strchr(at, ';');
169 Path tmpPath;
170 while (delim != 0) {
171 std::string tmp(at, size_t(delim-at));
172 if (tmpPath.set(tmp))
173 if (tmpPath.canRead())
174 Paths.push_back(tmpPath);
175 at = delim + 1;
176 delim = strchr(at, ';');
177 }
178
179 if (*at != 0)
180 if (tmpPath.set(std::string(at)))
181 if (tmpPath.canRead())
182 Paths.push_back(tmpPath);
183}
184
185void
186Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
187 Paths.push_back(sys::Path("C:/WINDOWS/SYSTEM32"));
188 Paths.push_back(sys::Path("C:/WINDOWS"));
189}
190
191void
192Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) {
193 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
194 if (env_var != 0) {
195 getPathList(env_var,Paths);
196 }
197#ifdef LLVM_LIBDIR
198 {
199 Path tmpPath;
200 if (tmpPath.set(LLVM_LIBDIR))
201 if (tmpPath.canRead())
202 Paths.push_back(tmpPath);
203 }
204#endif
205 GetSystemLibraryPaths(Paths);
206}
207
208Path
209Path::GetLLVMDefaultConfigDir() {
210 // TODO: this isn't going to fly on Windows
211 return Path("/etc/llvm");
212}
213
214Path
215Path::GetUserHomeDirectory() {
216 // TODO: Typical Windows setup doesn't define HOME.
217 const char* home = getenv("HOME");
218 if (home) {
219 Path result;
220 if (result.set(home))
221 return result;
222 }
223 return GetRootDirectory();
224}
Ted Kremenekb05c9352007-12-18 22:07:33 +0000225
226Path
227Path::GetCurrentDirectory() {
228 char pathname[MAX_PATH];
229 GetCurrentDirectory(pathname,MAX_PATH);
230 return Path(pathname);
231}
232
233
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234// FIXME: the above set of functions don't map to Windows very well.
235
236
237bool
238Path::isRootDirectory() const {
239 size_t len = path.size();
240 return len > 0 && path[len-1] == '/';
241}
242
243std::string
244Path::getBasename() const {
245 // Find the last slash
246 size_t slash = path.rfind('/');
247 if (slash == std::string::npos)
248 slash = 0;
249 else
250 slash++;
251
252 size_t dot = path.rfind('.');
253 if (dot == std::string::npos || dot < slash)
254 return path.substr(slash);
255 else
256 return path.substr(slash, dot - slash);
257}
258
259bool
260Path::exists() const {
261 DWORD attr = GetFileAttributes(path.c_str());
262 return attr != INVALID_FILE_ATTRIBUTES;
263}
264
265bool
Ted Kremenek65149be2007-12-18 19:46:22 +0000266Path::isDirectory() const {
267 DWORD attr = GetFileAttributes(path.c_str());
268 return (attr != INVALID_FILE_ATTRIBUTES) &&
269 (attr & FILE_ATTRIBUTE_DIRECTORY);
270}
271
272bool
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273Path::canRead() const {
274 // FIXME: take security attributes into account.
275 DWORD attr = GetFileAttributes(path.c_str());
276 return attr != INVALID_FILE_ATTRIBUTES;
277}
278
279bool
280Path::canWrite() const {
281 // FIXME: take security attributes into account.
282 DWORD attr = GetFileAttributes(path.c_str());
283 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
284}
285
286bool
287Path::canExecute() const {
288 // FIXME: take security attributes into account.
289 DWORD attr = GetFileAttributes(path.c_str());
290 return attr != INVALID_FILE_ATTRIBUTES;
291}
292
293std::string
294Path::getLast() const {
295 // Find the last slash
296 size_t pos = path.rfind('/');
297
298 // Handle the corner cases
299 if (pos == std::string::npos)
300 return path;
301
302 // If the last character is a slash, we have a root directory
303 if (pos == path.length()-1)
304 return path;
305
306 // Return everything after the last slash
307 return path.substr(pos+1);
308}
309
310const FileStatus *
311PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const {
312 if (!fsIsValid || update) {
313 WIN32_FILE_ATTRIBUTE_DATA fi;
314 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
315 MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) +
316 ": Can't get status: ");
317 return 0;
318 }
319
320 status.fileSize = fi.nFileSizeHigh;
321 status.fileSize <<= sizeof(fi.nFileSizeHigh)*8;
322 status.fileSize += fi.nFileSizeLow;
323
324 status.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
325 status.user = 9999; // Not applicable to Windows, so...
326 status.group = 9999; // Not applicable to Windows, so...
327
328 // FIXME: this is only unique if the file is accessed by the same file path.
329 // How do we do this for C:\dir\file and ..\dir\file ? Unix has inode
330 // numbers, but the concept doesn't exist in Windows.
331 status.uniqueID = 0;
332 for (unsigned i = 0; i < path.length(); ++i)
333 status.uniqueID += path[i];
334
335 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
336 status.modTime.fromWin32Time(ft);
337
338 status.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
339 fsIsValid = true;
340 }
341 return &status;
342}
343
344bool Path::makeReadableOnDisk(std::string* ErrMsg) {
345 // All files are readable on Windows (ignoring security attributes).
346 return false;
347}
348
349bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
350 DWORD attr = GetFileAttributes(path.c_str());
351
352 // If it doesn't exist, we're done.
353 if (attr == INVALID_FILE_ATTRIBUTES)
354 return false;
355
356 if (attr & FILE_ATTRIBUTE_READONLY) {
357 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) {
358 MakeErrMsg(ErrMsg, std::string(path) + ": Can't make file writable: ");
359 return true;
360 }
361 }
362 return false;
363}
364
365bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
366 // All files are executable on Windows (ignoring security attributes).
367 return false;
368}
369
370bool
371Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
372 WIN32_FILE_ATTRIBUTE_DATA fi;
373 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
374 MakeErrMsg(ErrMsg, path + ": can't get status of file");
375 return true;
376 }
377
378 if (!(fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
379 if (ErrMsg)
380 *ErrMsg = path + ": not a directory";
381 return true;
382 }
383
384 result.clear();
385 WIN32_FIND_DATA fd;
386 std::string searchpath = path;
387 if (path.size() == 0 || searchpath[path.size()-1] == '/')
388 searchpath += "*";
389 else
390 searchpath += "/*";
391
392 HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
393 if (h == INVALID_HANDLE_VALUE) {
394 if (GetLastError() == ERROR_FILE_NOT_FOUND)
395 return true; // not really an error, now is it?
396 MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
397 return true;
398 }
399
400 do {
401 if (fd.cFileName[0] == '.')
402 continue;
403 Path aPath(path);
404 aPath.appendComponent(&fd.cFileName[0]);
405 result.insert(aPath);
406 } while (FindNextFile(h, &fd));
407
408 DWORD err = GetLastError();
409 FindClose(h);
410 if (err != ERROR_NO_MORE_FILES) {
411 SetLastError(err);
412 MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
413 return true;
414 }
415 return false;
416}
417
418bool
419Path::set(const std::string& a_path) {
420 if (a_path.size() == 0)
421 return false;
422 std::string save(path);
423 path = a_path;
424 FlipBackSlashes(path);
425 if (!isValid()) {
426 path = save;
427 return false;
428 }
429 return true;
430}
431
432bool
433Path::appendComponent(const std::string& name) {
434 if (name.empty())
435 return false;
436 std::string save(path);
437 if (!path.empty()) {
438 size_t last = path.size() - 1;
439 if (path[last] != '/')
440 path += '/';
441 }
442 path += name;
443 if (!isValid()) {
444 path = save;
445 return false;
446 }
447 return true;
448}
449
450bool
451Path::eraseComponent() {
452 size_t slashpos = path.rfind('/',path.size());
453 if (slashpos == path.size() - 1 || slashpos == std::string::npos)
454 return false;
455 std::string save(path);
456 path.erase(slashpos);
457 if (!isValid()) {
458 path = save;
459 return false;
460 }
461 return true;
462}
463
464bool
465Path::appendSuffix(const std::string& suffix) {
466 std::string save(path);
467 path.append(".");
468 path.append(suffix);
469 if (!isValid()) {
470 path = save;
471 return false;
472 }
473 return true;
474}
475
476bool
477Path::eraseSuffix() {
478 size_t dotpos = path.rfind('.',path.size());
479 size_t slashpos = path.rfind('/',path.size());
480 if (dotpos != std::string::npos) {
481 if (slashpos == std::string::npos || dotpos > slashpos+1) {
482 std::string save(path);
483 path.erase(dotpos, path.size()-dotpos);
484 if (!isValid()) {
485 path = save;
486 return false;
487 }
488 return true;
489 }
490 }
491 return false;
492}
493
494inline bool PathMsg(std::string* ErrMsg, const char* pathname, const char*msg) {
495 if (ErrMsg)
496 *ErrMsg = std::string(pathname) + ": " + std::string(msg);
497 return true;
498}
499
500bool
501Path::createDirectoryOnDisk(bool create_parents, std::string* ErrMsg) {
502 // Get a writeable copy of the path name
503 size_t len = path.length();
504 char *pathname = reinterpret_cast<char *>(_alloca(len+2));
505 path.copy(pathname, len);
506 pathname[len] = 0;
507
508 // Make sure it ends with a slash.
509 if (len == 0 || pathname[len - 1] != '/') {
510 pathname[len] = '/';
511 pathname[++len] = 0;
512 }
513
514 // Determine starting point for initial / search.
515 char *next = pathname;
516 if (pathname[0] == '/' && pathname[1] == '/') {
517 // Skip host name.
518 next = strchr(pathname+2, '/');
519 if (next == NULL)
520 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
521
522 // Skip share name.
523 next = strchr(next+1, '/');
524 if (next == NULL)
525 return PathMsg(ErrMsg, pathname,"badly formed remote directory");
526
527 next++;
528 if (*next == 0)
529 return PathMsg(ErrMsg, pathname, "badly formed remote directory");
530
531 } else {
532 if (pathname[1] == ':')
533 next += 2; // skip drive letter
534 if (*next == '/')
535 next++; // skip root directory
536 }
537
538 // If we're supposed to create intermediate directories
539 if (create_parents) {
540 // Loop through the directory components until we're done
541 while (*next) {
542 next = strchr(next, '/');
543 *next = 0;
544 if (!CreateDirectory(pathname, NULL))
545 return MakeErrMsg(ErrMsg,
546 std::string(pathname) + ": Can't create directory: ");
547 *next++ = '/';
548 }
549 } else {
550 // Drop trailing slash.
551 pathname[len-1] = 0;
552 if (!CreateDirectory(pathname, NULL)) {
553 return MakeErrMsg(ErrMsg, std::string(pathname) + ": Can't create directory: ");
554 }
555 }
556 return false;
557}
558
559bool
560Path::createFileOnDisk(std::string* ErrMsg) {
561 // Create the file
562 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
563 FILE_ATTRIBUTE_NORMAL, NULL);
564 if (h == INVALID_HANDLE_VALUE)
565 return MakeErrMsg(ErrMsg, path + ": Can't create file: ");
566
567 CloseHandle(h);
568 return false;
569}
570
571bool
572Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
573 WIN32_FILE_ATTRIBUTE_DATA fi;
574 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
575 return true;
576
577 if (fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
578 // If it doesn't exist, we're done.
579 if (!exists())
580 return false;
581
582 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
583 int lastchar = path.length() - 1 ;
584 path.copy(pathname, lastchar+1);
585
586 // Make path end with '/*'.
587 if (pathname[lastchar] != '/')
588 pathname[++lastchar] = '/';
589 pathname[lastchar+1] = '*';
590 pathname[lastchar+2] = 0;
591
592 if (remove_contents) {
593 WIN32_FIND_DATA fd;
594 HANDLE h = FindFirstFile(pathname, &fd);
595
596 // It's a bad idea to alter the contents of a directory while enumerating
597 // its contents. So build a list of its contents first, then destroy them.
598
599 if (h != INVALID_HANDLE_VALUE) {
600 std::vector<Path> list;
601
602 do {
603 if (strcmp(fd.cFileName, ".") == 0)
604 continue;
605 if (strcmp(fd.cFileName, "..") == 0)
606 continue;
607
608 Path aPath(path);
609 aPath.appendComponent(&fd.cFileName[0]);
610 list.push_back(aPath);
611 } while (FindNextFile(h, &fd));
612
613 DWORD err = GetLastError();
614 FindClose(h);
615 if (err != ERROR_NO_MORE_FILES) {
616 SetLastError(err);
617 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
618 }
619
620 for (std::vector<Path>::iterator I = list.begin(); I != list.end();
621 ++I) {
622 Path &aPath = *I;
623 aPath.eraseFromDisk(true);
624 }
625 } else {
626 if (GetLastError() != ERROR_FILE_NOT_FOUND)
627 return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
628 }
629 }
630
631 pathname[lastchar] = 0;
632 if (!RemoveDirectory(pathname))
633 return MakeErrMsg(ErrStr,
634 std::string(pathname) + ": Can't destroy directory: ");
635 return false;
636 } else {
637 // Read-only files cannot be deleted on Windows. Must remove the read-only
638 // attribute first.
639 if (fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
640 if (!SetFileAttributes(path.c_str(),
641 fi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
642 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
643 }
644
645 if (!DeleteFile(path.c_str()))
646 return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
647 return false;
648 }
649}
650
651bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
652 assert(len < 1024 && "Request for magic string too long");
653 char* buf = (char*) alloca(1 + len);
654
655 HANDLE h = CreateFile(path.c_str(),
656 GENERIC_READ,
657 FILE_SHARE_READ,
658 NULL,
659 OPEN_EXISTING,
660 FILE_ATTRIBUTE_NORMAL,
661 NULL);
662 if (h == INVALID_HANDLE_VALUE)
663 return false;
664
665 DWORD nRead = 0;
666 BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
667 CloseHandle(h);
668
669 if (!ret || nRead != len)
670 return false;
671
672 buf[len] = '\0';
673 Magic = buf;
674 return true;
675}
676
677bool
678Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
679 if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING))
680 return MakeErrMsg(ErrMsg, "Can't move '" + path + "' to '" + newName.path
681 + "': ");
682 return true;
683}
684
685bool
686Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrMsg) const {
687 // FIXME: should work on directories also.
688 if (!si.isFile) {
689 return true;
690 }
691
692 HANDLE h = CreateFile(path.c_str(),
693 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
694 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
695 NULL,
696 OPEN_EXISTING,
697 FILE_ATTRIBUTE_NORMAL,
698 NULL);
699 if (h == INVALID_HANDLE_VALUE)
700 return true;
701
702 BY_HANDLE_FILE_INFORMATION bhfi;
703 if (!GetFileInformationByHandle(h, &bhfi)) {
704 DWORD err = GetLastError();
705 CloseHandle(h);
706 SetLastError(err);
707 return MakeErrMsg(ErrMsg, path + ": GetFileInformationByHandle: ");
708 }
709
710 FILETIME ft;
711 (uint64_t&)ft = si.modTime.toWin32Time();
712 BOOL ret = SetFileTime(h, NULL, &ft, &ft);
713 DWORD err = GetLastError();
714 CloseHandle(h);
715 if (!ret) {
716 SetLastError(err);
717 return MakeErrMsg(ErrMsg, path + ": SetFileTime: ");
718 }
719
720 // Best we can do with Unix permission bits is to interpret the owner
721 // writable bit.
722 if (si.mode & 0200) {
723 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
724 if (!SetFileAttributes(path.c_str(),
725 bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
726 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
727 }
728 } else {
729 if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
730 if (!SetFileAttributes(path.c_str(),
731 bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
732 return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
733 }
734 }
735
736 return false;
737}
738
739bool
740CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg) {
741 // Can't use CopyFile macro defined in Windows.h because it would mess up the
742 // above line. We use the expansion it would have in a non-UNICODE build.
743 if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
744 return MakeErrMsg(ErrMsg, "Can't copy '" + Src.toString() +
745 "' to '" + Dest.toString() + "': ");
746 return false;
747}
748
749bool
750Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
751 if (reuse_current && !exists())
752 return false; // File doesn't exist already, just use it!
753
754 // Reserve space for -XXXXXX at the end.
755 char *FNBuffer = (char*) alloca(path.size()+8);
756 unsigned offset = path.size();
757 path.copy(FNBuffer, offset);
758
759 // Find a numeric suffix that isn't used by an existing file. Assume there
760 // won't be more than 1 million files with the same prefix. Probably a safe
761 // bet.
762 static unsigned FCounter = 0;
763 do {
764 sprintf(FNBuffer+offset, "-%06u", FCounter);
765 if (++FCounter > 999999)
766 FCounter = 0;
767 path = FNBuffer;
768 } while (exists());
769 return false;
770}
771
772bool
773Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
774 // Make this into a unique file name
775 makeUnique(reuse_current, ErrMsg);
776
777 // Now go and create it
778 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
779 FILE_ATTRIBUTE_NORMAL, NULL);
780 if (h == INVALID_HANDLE_VALUE)
781 return MakeErrMsg(ErrMsg, path + ": can't create file");
782
783 CloseHandle(h);
784 return false;
785}
786
787}
788}