3e4c270da564d83dad0d055e00bc63464fd09422
[oota-llvm.git] / support / lib / Support / FileUtilities.cpp
1 //===- Support/FileUtilities.cpp - File System Utilities ------------------===//
2 //
3 // This file implements a family of utility functions which are useful for doing
4 // various things with files.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "Support/FileUtilities.h"
9 #include "Config/unistd.h"
10 #include <fstream>
11 #include <iostream>
12 #include <cstdio>
13
14 /// DiffFiles - Compare the two files specified, returning true if they are
15 /// different or if there is a file error.  If you specify a string to fill in
16 /// for the error option, it will set the string to an error message if an error
17 /// occurs, allowing the caller to distinguish between a failed diff and a file
18 /// system error.
19 ///
20 bool DiffFiles(const std::string &FileA, const std::string &FileB,
21                std::string *Error) {
22   std::ifstream FileAStream(FileA.c_str());
23   if (!FileAStream) {
24     if (Error) *Error = "Couldn't open file '" + FileA + "'";
25     return true;
26   }
27
28   std::ifstream FileBStream(FileB.c_str());
29   if (!FileBStream) {
30     if (Error) *Error = "Couldn't open file '" + FileB + "'";
31     return true;
32   }
33
34   // Compare the two files...
35   int C1, C2;
36   do {
37     C1 = FileAStream.get();
38     C2 = FileBStream.get();
39     if (C1 != C2) return true;
40   } while (C1 != EOF);
41
42   return false;
43 }
44
45
46 /// MoveFileOverIfUpdated - If the file specified by New is different than Old,
47 /// or if Old does not exist, move the New file over the Old file.  Otherwise,
48 /// remove the New file.
49 ///
50 void MoveFileOverIfUpdated(const std::string &New, const std::string &Old) {
51   if (DiffFiles(New, Old)) {
52     if (std::rename(New.c_str(), Old.c_str()))
53       std::cerr << "Error renaming '" << New << "' to '" << Old << "'!\n";
54   } else {
55     std::remove(New.c_str());
56   }  
57 }
58
59 /// removeFile - Delete the specified file
60 ///
61 void removeFile(const std::string &Filename) {
62   std::remove(Filename.c_str());
63 }
64
65 /// getUniqueFilename - Return a filename with the specified prefix.  If the
66 /// file does not exist yet, return it, otherwise add a suffix to make it
67 /// unique.
68 ///
69 std::string getUniqueFilename(const std::string &FilenameBase) {
70   if (!std::ifstream(FilenameBase.c_str()))
71     return FilenameBase;    // Couldn't open the file? Use it!
72
73   // Create a pattern for mkstemp...
74   char *FNBuffer = new char[FilenameBase.size()+8];
75   strcpy(FNBuffer, FilenameBase.c_str());
76   strcpy(FNBuffer+FilenameBase.size(), "-XXXXXX");
77
78   // Agree on a temporary file name to use....
79   int TempFD;
80   if ((TempFD = mkstemp(FNBuffer)) == -1) {
81     std::cerr << "bugpoint: ERROR: Cannot create temporary file in the current "
82               << " directory!\n";
83     exit(1);
84   }
85
86   // We don't need to hold the temp file descriptor... we will trust that noone
87   // will overwrite/delete the file while we are working on it...
88   close(TempFD);
89   std::string Result(FNBuffer);
90   delete[] FNBuffer;
91   return Result;
92 }