1 //===- Support/FileUtilities.cpp - File System Utilities ------------------===//
3 // This file implements a family of utility functions which are useful for doing
4 // various things with files.
6 //===----------------------------------------------------------------------===//
8 #include "Support/FileUtilities.h"
9 #include "Config/unistd.h"
10 #include "Config/sys/stat.h"
11 #include "Config/sys/types.h"
16 /// DiffFiles - Compare the two files specified, returning true if they are
17 /// different or if there is a file error. If you specify a string to fill in
18 /// for the error option, it will set the string to an error message if an error
19 /// occurs, allowing the caller to distinguish between a failed diff and a file
22 bool DiffFiles(const std::string &FileA, const std::string &FileB,
24 std::ifstream FileAStream(FileA.c_str());
26 if (Error) *Error = "Couldn't open file '" + FileA + "'";
30 std::ifstream FileBStream(FileB.c_str());
32 if (Error) *Error = "Couldn't open file '" + FileB + "'";
36 // Compare the two files...
39 C1 = FileAStream.get();
40 C2 = FileBStream.get();
41 if (C1 != C2) return true;
48 /// MoveFileOverIfUpdated - If the file specified by New is different than Old,
49 /// or if Old does not exist, move the New file over the Old file. Otherwise,
50 /// remove the New file.
52 void MoveFileOverIfUpdated(const std::string &New, const std::string &Old) {
53 if (DiffFiles(New, Old)) {
54 if (std::rename(New.c_str(), Old.c_str()))
55 std::cerr << "Error renaming '" << New << "' to '" << Old << "'!\n";
57 std::remove(New.c_str());
61 /// removeFile - Delete the specified file
63 void removeFile(const std::string &Filename) {
64 std::remove(Filename.c_str());
67 /// getUniqueFilename - Return a filename with the specified prefix. If the
68 /// file does not exist yet, return it, otherwise add a suffix to make it
71 std::string getUniqueFilename(const std::string &FilenameBase) {
72 if (!std::ifstream(FilenameBase.c_str()))
73 return FilenameBase; // Couldn't open the file? Use it!
75 // Create a pattern for mkstemp...
76 char *FNBuffer = new char[FilenameBase.size()+8];
77 strcpy(FNBuffer, FilenameBase.c_str());
78 strcpy(FNBuffer+FilenameBase.size(), "-XXXXXX");
80 // Agree on a temporary file name to use....
82 if ((TempFD = mkstemp(FNBuffer)) == -1) {
83 std::cerr << "bugpoint: ERROR: Cannot create temporary file in the current "
88 // We don't need to hold the temp file descriptor... we will trust that noone
89 // will overwrite/delete the file while we are working on it...
91 std::string Result(FNBuffer);
97 /// Method: MakeFileExecutable ()
100 /// This method makes the specified filename executable by giving it
101 /// execute permission. It respects the umask value of the process, and it
102 /// does not enable any unnecessary access bits.
105 /// o Get file's current permissions.
106 /// o Get the process's current umask.
107 /// o Take the set of all execute bits and disable those found in the umask.
108 /// o Add the remaining permissions to the file's permissions.
111 MakeFileExecutable (const std::string & Filename)
113 // Permissions masking value of the user
116 // Permissions currently enabled on the file
120 // Grab the umask value from the operating system. We want to use it when
121 // changing the file's permissions.
124 // Umask() is one of those annoying system calls. You have to call it
125 // to get the current value and then set it back.
127 mask = umask (0x777);
131 // Go fetch the file's current permission bits. We want to *add* execute
132 // access to the file.
134 if ((stat (Filename.c_str(), &fstat)) == -1)
140 // Make the file executable...
142 if ((chmod(Filename.c_str(), (fstat.st_mode | (0111 & ~mask)))) == -1)
151 /// Method: MakeFileReadable ()
154 /// This method makes the specified filename readable by giving it
155 /// read permission. It respects the umask value of the process, and it
156 /// does not enable any unnecessary access bits.
159 /// o Get file's current permissions.
160 /// o Get the process's current umask.
161 /// o Take the set of all read bits and disable those found in the umask.
162 /// o Add the remaining permissions to the file's permissions.
165 MakeFileReadable (const std::string & Filename)
167 // Permissions masking value of the user
170 // Permissions currently enabled on the file
174 // Grab the umask value from the operating system. We want to use it when
175 // changing the file's permissions.
178 // Umask() is one of those annoying system calls. You have to call it
179 // to get the current value and then set it back.
181 mask = umask (0x777);
185 // Go fetch the file's current permission bits. We want to *add* execute
186 // access to the file.
188 if ((stat (Filename.c_str(), &fstat)) == -1)
194 // Make the file executable...
196 if ((chmod(Filename.c_str(), (fstat.st_mode | (0444 & ~mask)))) == -1)