b155213ec627edbeca10fb11f6c2c0b161009544
[oota-llvm.git] / lib / System / Unix / Path.inc
1 //===- llvm/System/Unix/Path.cpp - Unix 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 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Unix specific portion of the Path class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic UNIX code that
16 //===          is guaranteed to work on *all* UNIX variants.
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/Config/alloca.h"
20 #include "Unix.h"
21 #if HAVE_SYS_STAT_H
22 #include <sys/stat.h>
23 #endif
24 #if HAVE_FCNTL_H
25 #include <fcntl.h>
26 #endif
27 #if HAVE_UTIME_H
28 #include <utime.h>
29 #endif
30 #if HAVE_TIME_H
31 #include <time.h>
32 #endif
33 #if HAVE_DIRENT_H
34 # include <dirent.h>
35 # define NAMLEN(dirent) strlen((dirent)->d_name)
36 #else
37 # define dirent direct
38 # define NAMLEN(dirent) (dirent)->d_namlen
39 # if HAVE_SYS_NDIR_H
40 #  include <sys/ndir.h>
41 # endif
42 # if HAVE_SYS_DIR_H
43 #  include <sys/dir.h>
44 # endif
45 # if HAVE_NDIR_H
46 #  include <ndir.h>
47 # endif
48 #endif
49
50 // Put in a hack for Cygwin which falsely reports that the mkdtemp function
51 // is available when it is not.
52 #ifdef __CYGWIN__
53 # undef HAVE_MKDTEMP
54 #endif
55
56 namespace {
57 inline bool lastIsSlash(const std::string& path) {
58   return !path.empty() && path[path.length() - 1] == '/';
59 }
60
61 }
62
63 namespace llvm {
64 using namespace sys;
65
66 bool 
67 Path::isValid() const {
68   // Check some obvious things
69   if (path.empty()) 
70     return false;
71   else if (path.length() >= MAXPATHLEN)
72     return false;
73
74   // Check that the characters are ascii chars
75   size_t len = path.length();
76   unsigned i = 0;
77   while (i < len && isascii(path[i])) 
78     ++i;
79   return i >= len; 
80 }
81
82 bool 
83 Path::isAbsolute() const {
84   if (path.empty())
85     return false;
86   return path[0] == '/';
87
88 Path
89 Path::GetRootDirectory() {
90   Path result;
91   result.set("/");
92   return result;
93 }
94
95 Path
96 Path::GetTemporaryDirectory(std::string* ErrMsg ) {
97 #if defined(HAVE_MKDTEMP)
98   // The best way is with mkdtemp but that's not available on many systems, 
99   // Linux and FreeBSD have it. Others probably won't.
100   char pathname[MAXPATHLEN];
101   strcpy(pathname,"/tmp/llvm_XXXXXX");
102   if (0 == mkdtemp(pathname)) {
103     MakeErrMsg(ErrMsg, 
104       std::string(pathname) + ": can't create temporary directory");
105     return Path();
106   }
107   Path result;
108   result.set(pathname);
109   assert(result.isValid() && "mkdtemp didn't create a valid pathname!");
110   return result;
111 #elif defined(HAVE_MKSTEMP)
112   // If no mkdtemp is available, mkstemp can be used to create a temporary file
113   // which is then removed and created as a directory. We prefer this over
114   // mktemp because of mktemp's inherent security and threading risks. We still
115   // have a slight race condition from the time the temporary file is created to
116   // the time it is re-created as a directoy. 
117   char pathname[MAXPATHLEN];
118   strcpy(pathname, "/tmp/llvm_XXXXXX");
119   int fd = 0;
120   if (-1 == (fd = mkstemp(pathname))) {
121     MakeErrMsg(ErrMsg, 
122       std::string(pathname) + ": can't create temporary directory");
123     return Path();
124   }
125   ::close(fd);
126   ::unlink(pathname); // start race condition, ignore errors
127   if (-1 == ::mkdir(pathname, S_IRWXU)) { // end race condition
128     MakeErrMsg(ErrMsg, 
129       std::string(pathname) + ": can't create temporary directory");
130     return Path();
131   }
132   Path result;
133   result.set(pathname);
134   assert(result.isValid() && "mkstemp didn't create a valid pathname!");
135   return result;
136 #elif defined(HAVE_MKTEMP)
137   // If a system doesn't have mkdtemp(3) or mkstemp(3) but it does have
138   // mktemp(3) then we'll assume that system (e.g. AIX) has a reasonable
139   // implementation of mktemp(3) and doesn't follow BSD 4.3's lead of replacing
140   // the XXXXXX with the pid of the process and a letter. That leads to only
141   // twenty six temporary files that can be generated.
142   char pathname[MAXPATHLEN];
143   strcpy(pathname, "/tmp/llvm_XXXXXX");
144   char *TmpName = ::mktemp(pathname);
145   if (TmpName == 0) {
146     MakeErrMsg(ErrMsg, 
147       std::string(TmpName) + ": can't create unique directory name");
148     return Path();
149   }
150   if (-1 == ::mkdir(TmpName, S_IRWXU)) {
151     MakeErrMsg(ErrMsg, 
152         std::string(TmpName) + ": can't create temporary directory");
153     return Path();
154   }
155   Path result;
156   result.set(TmpName);
157   assert(result.isValid() && "mktemp didn't create a valid pathname!");
158   return result;
159 #else
160   // This is the worst case implementation. tempnam(3) leaks memory unless its
161   // on an SVID2 (or later) system. On BSD 4.3 it leaks. tmpnam(3) has thread
162   // issues. The mktemp(3) function doesn't have enough variability in the
163   // temporary name generated. So, we provide our own implementation that 
164   // increments an integer from a random number seeded by the current time. This
165   // should be sufficiently unique that we don't have many collisions between
166   // processes. Generally LLVM processes don't run very long and don't use very
167   // many temporary files so this shouldn't be a big issue for LLVM.
168   static time_t num = ::time(0);
169   char pathname[MAXPATHLEN];
170   do {
171     num++;
172     sprintf(pathname, "/tmp/llvm_%010u", unsigned(num));
173   } while ( 0 == access(pathname, F_OK ) );
174   if (-1 == ::mkdir(pathname, S_IRWXU)) {
175     MakeErrMsg(ErrMsg, 
176       std::string(pathname) + ": can't create temporary directory");
177     return Path();
178   }
179   Path result;
180   result.set(pathname);
181   assert(result.isValid() && "mkstemp didn't create a valid pathname!");
182   return result;
183 #endif
184 }
185
186 static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
187   const char* at = path;
188   const char* delim = strchr(at, ':');
189   Path tmpPath;
190   while( delim != 0 ) {
191     std::string tmp(at, size_t(delim-at));
192     if (tmpPath.set(tmp))
193       if (tmpPath.canRead())
194         Paths.push_back(tmpPath);
195     at = delim + 1;
196     delim = strchr(at, ':');
197   }
198   if (*at != 0)
199     if (tmpPath.set(std::string(at)))
200       if (tmpPath.canRead())
201         Paths.push_back(tmpPath);
202
203 }
204
205 void 
206 Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
207 #ifdef LTDL_SHLIBPATH_VAR
208   char* env_var = getenv(LTDL_SHLIBPATH_VAR);
209   if (env_var != 0) {
210     getPathList(env_var,Paths);
211   }
212 #endif
213   // FIXME: Should this look at LD_LIBRARY_PATH too?
214   Paths.push_back(sys::Path("/usr/local/lib/"));
215   Paths.push_back(sys::Path("/usr/X11R6/lib/"));
216   Paths.push_back(sys::Path("/usr/lib/"));
217   Paths.push_back(sys::Path("/lib/"));
218 }
219
220 void
221 Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
222   char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
223   if (env_var != 0) {
224     getPathList(env_var,Paths);
225   }
226 #ifdef LLVM_LIBDIR
227   {
228     Path tmpPath;
229     if (tmpPath.set(LLVM_LIBDIR))
230       if (tmpPath.canRead())
231         Paths.push_back(tmpPath);
232   }
233 #endif
234   GetSystemLibraryPaths(Paths);
235 }
236
237 Path 
238 Path::GetLLVMDefaultConfigDir() {
239   return Path("/etc/llvm/");
240 }
241
242 Path
243 Path::GetUserHomeDirectory() {
244   const char* home = getenv("HOME");
245   if (home) {
246     Path result;
247     if (result.set(home))
248       return result;
249   }
250   return GetRootDirectory();
251 }
252
253
254 std::string
255 Path::getBasename() const {
256   // Find the last slash
257   size_t slash = path.rfind('/');
258   if (slash == std::string::npos)
259     slash = 0;
260   else
261     slash++;
262
263   size_t dot = path.rfind('.');
264   if (dot == std::string::npos || dot < slash)
265     return path.substr(slash);
266   else
267     return path.substr(slash, dot - slash);
268 }
269
270 bool Path::hasMagicNumber(const std::string &Magic) const {
271   size_t len = Magic.size();
272   assert(len < 1024 && "Request for magic string too long");
273   char* buf = (char*) alloca(1 + len);
274   int fd = ::open(path.c_str(), O_RDONLY);
275   if (fd < 0)
276     return false;
277   size_t read_len = ::read(fd, buf, len);
278   close(fd);
279   if (len != read_len)
280     return false;
281   buf[len] = '\0';
282   return Magic == buf;
283 }
284
285 bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
286   assert(len < 1024 && "Request for magic string too long");
287   char* buf = (char*) alloca(1 + len);
288   int fd = ::open(path.c_str(), O_RDONLY);
289   if (fd < 0)
290     return false;
291   ssize_t bytes_read = ::read(fd, buf, len);
292   ::close(fd);
293   if (ssize_t(len) != bytes_read) {
294     Magic.clear();
295     return false;
296   }
297   Magic.assign(buf,len);
298   return true;
299 }
300
301 bool 
302 Path::isBytecodeFile() const {
303   char buffer[4];
304   buffer[0] = 0;
305   int fd = ::open(path.c_str(), O_RDONLY);
306   if (fd < 0)
307     return false;
308   ssize_t bytes_read = ::read(fd, buffer, 4);
309   ::close(fd);
310   if (4 != bytes_read) 
311     return false;
312
313   return (buffer[0] == 'l' && buffer[1] == 'l' && buffer[2] == 'v' &&
314          (buffer[3] == 'c' || buffer[3] == 'm'));
315 }
316
317 bool
318 Path::exists() const {
319   return 0 == access(path.c_str(), F_OK );
320 }
321
322 bool
323 Path::canRead() const {
324   return 0 == access(path.c_str(), F_OK | R_OK );
325 }
326
327 bool
328 Path::canWrite() const {
329   return 0 == access(path.c_str(), F_OK | W_OK );
330 }
331
332 bool
333 Path::canExecute() const {
334   if (0 != access(path.c_str(), R_OK | X_OK ))
335     return false;
336   struct stat st;
337   int r = stat(path.c_str(), &st);
338   if (r != 0 || !S_ISREG(st.st_mode))
339     return false;
340   return true;
341 }
342
343 std::string 
344 Path::getLast() const {
345   // Find the last slash
346   size_t pos = path.rfind('/');
347
348   // Handle the corner cases
349   if (pos == std::string::npos)
350     return path;
351
352   // If the last character is a slash
353   if (pos == path.length()-1) {
354     // Find the second to last slash
355     size_t pos2 = path.rfind('/', pos-1);
356     if (pos2 == std::string::npos)
357       return path.substr(0,pos);
358     else
359       return path.substr(pos2+1,pos-pos2-1);
360   }
361   // Return everything after the last slash
362   return path.substr(pos+1);
363 }
364
365 bool
366 Path::getFileStatus(FileStatus &info, bool update, std::string *ErrStr) const {
367   if (status == 0 || update) {
368     struct stat buf;
369     if (0 != stat(path.c_str(), &buf))
370       return MakeErrMsg(ErrStr, path + ": can't get status of file");
371     if (status == 0)
372       status = new FileStatus;
373     status->fileSize = buf.st_size;
374     status->modTime.fromEpochTime(buf.st_mtime);
375     status->mode = buf.st_mode;
376     status->user = buf.st_uid;
377     status->group = buf.st_gid;
378     status->isDir  = S_ISDIR(buf.st_mode);
379     status->isFile = S_ISREG(buf.st_mode);
380   }
381   info = *status;
382   return false;
383 }
384
385 static bool AddPermissionBits(const Path &File, int bits) {
386   // Get the umask value from the operating system.  We want to use it
387   // when changing the file's permissions. Since calling umask() sets
388   // the umask and returns its old value, we must call it a second
389   // time to reset it to the user's preference.
390   int mask = umask(0777); // The arg. to umask is arbitrary.
391   umask(mask);            // Restore the umask.
392
393   // Get the file's current mode.
394   FileStatus Stat;
395   if (File.getFileStatus(Stat)) return false;
396
397   // Change the file to have whichever permissions bits from 'bits'
398   // that the umask would not disable.
399   if ((chmod(File.c_str(), (Stat.getMode() | (bits & ~mask)))) == -1)
400     return false;
401
402   return true;
403 }
404
405 bool Path::makeReadableOnDisk(std::string* ErrMsg) {
406   if (!AddPermissionBits(*this, 0444)) 
407     return MakeErrMsg(ErrMsg, path + ": can't make file readable");
408   return false;
409 }
410
411 bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
412   if (!AddPermissionBits(*this, 0222))
413     return MakeErrMsg(ErrMsg, path + ": can't make file writable");
414   return false;
415 }
416
417 bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
418   if (!AddPermissionBits(*this, 0111))
419     return MakeErrMsg(ErrMsg, path + ": can't make file executable");
420   return false;
421 }
422
423 bool
424 Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
425   DIR* direntries = ::opendir(path.c_str());
426   if (direntries == 0)
427     return MakeErrMsg(ErrMsg, path + ": can't open directory");
428
429   std::string dirPath = path;
430   if (!lastIsSlash(dirPath))
431     dirPath += '/';
432
433   result.clear();
434   struct dirent* de = ::readdir(direntries);
435   for ( ; de != 0; de = ::readdir(direntries)) {
436     if (de->d_name[0] != '.') {
437       Path aPath(dirPath + (const char*)de->d_name);
438       struct stat st;
439       if (0 != lstat(aPath.path.c_str(), &st)) {
440         if (S_ISLNK(st.st_mode))
441           continue; // dangling symlink -- ignore
442         return MakeErrMsg(ErrMsg, 
443                           aPath.path +  ": can't determine file object type");
444       }
445       result.insert(aPath);
446     }
447   }
448   
449   closedir(direntries);
450   return false;
451 }
452
453 bool
454 Path::set(const std::string& a_path) {
455   if (a_path.empty())
456     return false;
457   std::string save(path);
458   path = a_path;
459   if (!isValid()) {
460     path = save;
461     return false;
462   }
463   return true;
464 }
465
466 bool
467 Path::appendComponent(const std::string& name) {
468   if (name.empty())
469     return false;
470   std::string save(path);
471   if (!lastIsSlash(path))
472     path += '/';
473   path += name;
474   if (!isValid()) {
475     path = save;
476     return false;
477   }
478   return true;
479 }
480
481 bool
482 Path::eraseComponent() {
483   size_t slashpos = path.rfind('/',path.size());
484   if (slashpos == 0 || slashpos == std::string::npos) {
485     path.erase();
486     return true;
487   }
488   if (slashpos == path.size() - 1)
489     slashpos = path.rfind('/',slashpos-1);
490   if (slashpos == std::string::npos) {
491     path.erase();
492     return true;
493   }
494   path.erase(slashpos);
495   return true;
496 }
497
498 bool
499 Path::appendSuffix(const std::string& suffix) {
500   std::string save(path);
501   path.append(".");
502   path.append(suffix);
503   if (!isValid()) {
504     path = save;
505     return false;
506   }
507   return true;
508 }
509
510 bool
511 Path::eraseSuffix() {
512   std::string save = path;
513   size_t dotpos = path.rfind('.',path.size());
514   size_t slashpos = path.rfind('/',path.size());
515   if (dotpos != std::string::npos) {
516     if (slashpos == std::string::npos || dotpos > slashpos+1) {
517       path.erase(dotpos, path.size()-dotpos);
518       return true;
519     }
520   }
521   if (!isValid())
522     path = save;
523   return false;
524 }
525
526 bool
527 Path::createDirectoryOnDisk( bool create_parents, std::string* ErrMsg ) {
528   // Get a writeable copy of the path name
529   char pathname[MAXPATHLEN];
530   path.copy(pathname,MAXPATHLEN);
531
532   // Null-terminate the last component
533   int lastchar = path.length() - 1 ; 
534   if (pathname[lastchar] == '/') 
535     pathname[lastchar] = 0;
536   else 
537     pathname[lastchar+1] = 0;
538
539   // If we're supposed to create intermediate directories
540   if ( create_parents ) {
541     // Find the end of the initial name component
542     char * next = strchr(pathname,'/');
543     if ( pathname[0] == '/') 
544       next = strchr(&pathname[1],'/');
545
546     // Loop through the directory components until we're done 
547     while ( next != 0 ) {
548       *next = 0;
549       if (0 != access(pathname, F_OK | R_OK | W_OK))
550         if (0 != mkdir(pathname, S_IRWXU | S_IRWXG)) {
551           return MakeErrMsg(ErrMsg, 
552                             std::string(pathname) + ": can't create directory");
553         }
554       char* save = next;
555       next = strchr(next+1,'/');
556       *save = '/';
557     }
558   } 
559
560   if (0 != access(pathname, F_OK | R_OK))
561     if (0 != mkdir(pathname, S_IRWXU | S_IRWXG)) {
562       return MakeErrMsg(ErrMsg, 
563                         std::string(pathname) + ": can't create directory");
564     }
565   return false;
566 }
567
568 bool
569 Path::createFileOnDisk(std::string* ErrMsg) {
570   // Create the file
571   int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR);
572   if (fd < 0)
573     return MakeErrMsg(ErrMsg, path + ": can't create file");
574   ::close(fd);
575   return false;
576 }
577
578 bool
579 Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
580   // Make this into a unique file name
581   if (makeUnique( reuse_current, ErrMsg ))
582     return true;
583
584   // create the file
585   int fd = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
586   if (fd < 0) 
587     return MakeErrMsg(ErrMsg, path + ": can't create temporary file");
588   ::close(fd);
589   return false;
590 }
591
592 bool
593 Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
594   FileStatus Status;
595   if (getFileStatus(Status, ErrStr))
596     return true;
597     
598   // Note: this check catches strange situations. In all cases, LLVM should only
599   // be involved in the creation and deletion of regular files.  This check 
600   // ensures that what we're trying to erase is a regular file. It effectively
601   // prevents LLVM from erasing things like /dev/null, any block special file,
602   // or other things that aren't "regular" files. 
603   if (Status.isFile) {
604     if (unlink(path.c_str()) != 0)
605       return MakeErrMsg(ErrStr, path + ": can't destroy file");
606     return false;
607   }
608   
609   if (!Status.isDir) {
610     if (ErrStr) *ErrStr = "not a file or directory";
611     return true;
612   }
613   if (remove_contents) {
614     // Recursively descend the directory to remove its contents.
615     std::string cmd = "/bin/rm -rf " + path;
616     system(cmd.c_str());
617     return false;
618   }
619
620   // Otherwise, try to just remove the one directory.
621   char pathname[MAXPATHLEN];
622   path.copy(pathname, MAXPATHLEN);
623   int lastchar = path.length() - 1 ; 
624   if (pathname[lastchar] == '/') 
625     pathname[lastchar] = 0;
626   else
627     pathname[lastchar+1] = 0;
628     
629   if (rmdir(pathname) != 0)
630     return MakeErrMsg(ErrStr, 
631       std::string(pathname) + ": can't destroy directory");
632   return false;
633 }
634
635 bool
636 Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
637   if (0 != ::rename(path.c_str(), newName.c_str()))
638     return MakeErrMsg(ErrMsg, std::string("can't rename '") + path + "' as '" + 
639                newName.toString() + "' ");
640   return false;
641 }
642
643 bool
644 Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrStr) const {
645   struct utimbuf utb;
646   utb.actime = si.modTime.toPosixTime();
647   utb.modtime = utb.actime;
648   if (0 != ::utime(path.c_str(),&utb))
649     return MakeErrMsg(ErrStr, path + ": can't set file modification time");
650   if (0 != ::chmod(path.c_str(),si.mode))
651     return MakeErrMsg(ErrStr, path + ": can't set mode");
652   return false;
653 }
654
655 bool 
656 sys::CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg){
657   int inFile = -1;
658   int outFile = -1;
659   inFile = ::open(Src.c_str(), O_RDONLY);
660   if (inFile == -1)
661     return MakeErrMsg(ErrMsg, Src.toString() + 
662       ": can't open source file to copy");
663
664   outFile = ::open(Dest.c_str(), O_WRONLY|O_CREAT, 0666);
665   if (outFile == -1) {
666     ::close(inFile);
667     return MakeErrMsg(ErrMsg, Dest.toString() +
668       ": can't create destination file for copy");
669   }
670
671   char Buffer[16*1024];
672   while (ssize_t Amt = ::read(inFile, Buffer, 16*1024)) {
673     if (Amt == -1) {
674       if (errno != EINTR && errno != EAGAIN) {
675         ::close(inFile);
676         ::close(outFile);
677         return MakeErrMsg(ErrMsg, Src.toString()+": can't read source file: ");
678       }
679     } else {
680       char *BufPtr = Buffer;
681       while (Amt) {
682         ssize_t AmtWritten = ::write(outFile, BufPtr, Amt);
683         if (AmtWritten == -1) {
684           if (errno != EINTR && errno != EAGAIN) {
685             ::close(inFile);
686             ::close(outFile);
687             return MakeErrMsg(ErrMsg, Dest.toString() + 
688               ": can't write destination file: ");
689           }
690         } else {
691           Amt -= AmtWritten;
692           BufPtr += AmtWritten;
693         }
694       }
695     }
696   }
697   ::close(inFile);
698   ::close(outFile);
699   return false;
700 }
701
702 bool 
703 Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
704   if (reuse_current && !exists())
705     return false; // File doesn't exist already, just use it!
706
707   // Append an XXXXXX pattern to the end of the file for use with mkstemp, 
708   // mktemp or our own implementation.
709   char *FNBuffer = (char*) alloca(path.size()+8);
710   path.copy(FNBuffer,path.size());
711   strcpy(FNBuffer+path.size(), "-XXXXXX");
712
713 #if defined(HAVE_MKSTEMP)
714   int TempFD;
715   if ((TempFD = mkstemp(FNBuffer)) == -1)
716     return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
717
718   // We don't need to hold the temp file descriptor... we will trust that no one
719   // will overwrite/delete the file before we can open it again.
720   close(TempFD);
721
722   // Save the name
723   path = FNBuffer;
724 #elif defined(HAVE_MKTEMP)
725   // If we don't have mkstemp, use the old and obsolete mktemp function.
726   if (mktemp(FNBuffer) == 0)
727     return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
728
729   // Save the name
730   path = FNBuffer;
731 #else
732   // Okay, looks like we have to do it all by our lonesome.
733   static unsigned FCounter = 0;
734   unsigned offset = path.size() + 1;
735   while ( FCounter < 999999 && exists()) {
736     sprintf(FNBuffer+offset,"%06u",++FCounter);
737     path = FNBuffer;
738   }
739   if (FCounter > 999999)
740     return MakeErrMsg(ErrMsg, 
741       path + ": can't make unique filename: too many files");
742 #endif
743   return false;
744 }
745
746 } // end llvm namespace
747