1 //===- Support/FileUtilities.cpp - File System Utilities ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a family of utility functions which are useful for doing
11 // various things with files.
13 //===----------------------------------------------------------------------===//
15 #include "Support/FileUtilities.h"
16 #include "Config/unistd.h"
17 #include "Config/sys/stat.h"
18 #include "Config/sys/types.h"
23 /// FileOpenable - Returns true IFF Filename names an existing regular
24 /// file which we can successfully open.
26 bool FileOpenable (const std::string &Filename) {
28 if (stat (Filename.c_str (), &s) == -1)
29 return false; // Cannot stat file
30 if (!S_ISREG (s.st_mode))
31 return false; // File is not a regular file
32 std::ifstream FileStream (Filename.c_str ());
34 return false; // File is not openable
38 /// DiffFiles - Compare the two files specified, returning true if they are
39 /// different or if there is a file error. If you specify a string to fill in
40 /// for the error option, it will set the string to an error message if an error
41 /// occurs, allowing the caller to distinguish between a failed diff and a file
44 bool DiffFiles(const std::string &FileA, const std::string &FileB,
46 std::ifstream FileAStream(FileA.c_str());
48 if (Error) *Error = "Couldn't open file '" + FileA + "'";
52 std::ifstream FileBStream(FileB.c_str());
54 if (Error) *Error = "Couldn't open file '" + FileB + "'";
58 // Compare the two files...
61 C1 = FileAStream.get();
62 C2 = FileBStream.get();
63 if (C1 != C2) return true;
70 /// MoveFileOverIfUpdated - If the file specified by New is different than Old,
71 /// or if Old does not exist, move the New file over the Old file. Otherwise,
72 /// remove the New file.
74 void MoveFileOverIfUpdated(const std::string &New, const std::string &Old) {
75 if (DiffFiles(New, Old)) {
76 if (std::rename(New.c_str(), Old.c_str()))
77 std::cerr << "Error renaming '" << New << "' to '" << Old << "'!\n";
79 std::remove(New.c_str());
83 /// removeFile - Delete the specified file
85 void removeFile(const std::string &Filename) {
86 std::remove(Filename.c_str());
89 /// getUniqueFilename - Return a filename with the specified prefix. If the
90 /// file does not exist yet, return it, otherwise add a suffix to make it
93 std::string getUniqueFilename(const std::string &FilenameBase) {
94 if (!std::ifstream(FilenameBase.c_str()))
95 return FilenameBase; // Couldn't open the file? Use it!
97 // Create a pattern for mkstemp...
98 char *FNBuffer = new char[FilenameBase.size()+8];
99 strcpy(FNBuffer, FilenameBase.c_str());
100 strcpy(FNBuffer+FilenameBase.size(), "-XXXXXX");
102 // Agree on a temporary file name to use....
104 if ((TempFD = mkstemp(FNBuffer)) == -1) {
105 std::cerr << "bugpoint: ERROR: Cannot create temporary file in the current "
110 // We don't need to hold the temp file descriptor... we will trust that no one
111 // will overwrite/delete the file while we are working on it...
113 std::string Result(FNBuffer);
118 static bool AddPermissionsBits (const std::string &Filename, mode_t bits) {
119 // Get the umask value from the operating system. We want to use it
120 // when changing the file's permissions. Since calling umask() sets
121 // the umask and returns its old value, we must call it a second
122 // time to reset it to the user's preference.
123 mode_t mask = umask (0777); // The arg. to umask is arbitrary...
126 // Get the file's current mode.
128 if ((stat (Filename.c_str(), &st)) == -1)
131 // Change the file to have whichever permissions bits from 'bits'
132 // that the umask would not disable.
133 if ((chmod(Filename.c_str(), (st.st_mode | (bits & ~mask)))) == -1)
139 /// MakeFileExecutable - Make the file named Filename executable by
140 /// setting whichever execute permissions bits the process's current
141 /// umask would allow. Filename must name an existing file or
142 /// directory. Returns true on success, false on error.
144 bool MakeFileExecutable (const std::string &Filename) {
145 return AddPermissionsBits (Filename, 0111);
148 /// MakeFileReadable - Make the file named Filename readable by
149 /// setting whichever read permissions bits the process's current
150 /// umask would allow. Filename must name an existing file or
151 /// directory. Returns true on success, false on error.
153 bool MakeFileReadable (const std::string &Filename) {
154 return AddPermissionsBits (Filename, 0444);