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