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"
24 /// CheckMagic - Returns true IFF the file named FN begins with Magic. FN must
25 /// name a readable file.
27 bool llvm::CheckMagic(const std::string &FN, const std::string &Magic) {
28 char buf[1 + Magic.size ()];
29 std::ifstream f (FN.c_str ());
30 f.read (buf, Magic.size ());
31 buf[Magic.size ()] = '\0';
35 /// IsArchive - Returns true IFF the file named FN appears to be a "ar" library
36 /// archive. The file named FN must exist.
38 bool llvm::IsArchive(const std::string &FN) {
39 // Inspect the beginning of the file to see if it contains the "ar"
40 // library archive format magic string.
41 return CheckMagic (FN, "!<arch>\012");
44 /// IsBytecode - Returns true IFF the file named FN appears to be an LLVM
45 /// bytecode file. The file named FN must exist.
47 bool llvm::IsBytecode(const std::string &FN) {
48 // Inspect the beginning of the file to see if it contains the LLVM
49 // bytecode format magic string.
50 return CheckMagic (FN, "llvm");
53 /// IsSharedObject - Returns trus IFF the file named FN appears to be a shared
54 /// object with an ELF header. The file named FN must exist.
56 bool llvm::IsSharedObject(const std::string &FN) {
57 // Inspect the beginning of the file to see if it contains the ELF shared
58 // object magic string.
59 static const char elfMagic[] = { 0x7f, 'E', 'L', 'F', '\0' };
60 return CheckMagic(FN, elfMagic);
63 /// FileOpenable - Returns true IFF Filename names an existing regular
64 /// file which we can successfully open.
66 bool llvm::FileOpenable(const std::string &Filename) {
68 if (stat (Filename.c_str (), &s) == -1)
69 return false; // Cannot stat file
70 if (!S_ISREG (s.st_mode))
71 return false; // File is not a regular file
72 std::ifstream FileStream (Filename.c_str ());
74 return false; // File is not openable
78 /// DiffFiles - Compare the two files specified, returning true if they are
79 /// different or if there is a file error. If you specify a string to fill in
80 /// for the error option, it will set the string to an error message if an error
81 /// occurs, allowing the caller to distinguish between a failed diff and a file
84 bool llvm::DiffFiles(const std::string &FileA, const std::string &FileB,
86 std::ifstream FileAStream(FileA.c_str());
88 if (Error) *Error = "Couldn't open file '" + FileA + "'";
92 std::ifstream FileBStream(FileB.c_str());
94 if (Error) *Error = "Couldn't open file '" + FileB + "'";
98 // Compare the two files...
101 C1 = FileAStream.get();
102 C2 = FileBStream.get();
103 if (C1 != C2) return true;
110 /// MoveFileOverIfUpdated - If the file specified by New is different than Old,
111 /// or if Old does not exist, move the New file over the Old file. Otherwise,
112 /// remove the New file.
114 void llvm::MoveFileOverIfUpdated(const std::string &New,
115 const std::string &Old) {
116 if (DiffFiles(New, Old)) {
117 if (std::rename(New.c_str(), Old.c_str()))
118 std::cerr << "Error renaming '" << New << "' to '" << Old << "'!\n";
120 std::remove(New.c_str());
124 /// removeFile - Delete the specified file
126 void llvm::removeFile(const std::string &Filename) {
127 std::remove(Filename.c_str());
130 /// getUniqueFilename - Return a filename with the specified prefix. If the
131 /// file does not exist yet, return it, otherwise add a suffix to make it
134 std::string llvm::getUniqueFilename(const std::string &FilenameBase) {
135 if (!std::ifstream(FilenameBase.c_str()))
136 return FilenameBase; // Couldn't open the file? Use it!
138 // Create a pattern for mkstemp...
139 char *FNBuffer = new char[FilenameBase.size()+8];
140 strcpy(FNBuffer, FilenameBase.c_str());
141 strcpy(FNBuffer+FilenameBase.size(), "-XXXXXX");
143 // Agree on a temporary file name to use....
145 if ((TempFD = mkstemp(FNBuffer)) == -1) {
146 std::cerr << "bugpoint: ERROR: Cannot create temporary file in the current "
151 // We don't need to hold the temp file descriptor... we will trust that no one
152 // will overwrite/delete the file while we are working on it...
154 std::string Result(FNBuffer);
159 static bool AddPermissionsBits (const std::string &Filename, mode_t bits) {
160 // Get the umask value from the operating system. We want to use it
161 // when changing the file's permissions. Since calling umask() sets
162 // the umask and returns its old value, we must call it a second
163 // time to reset it to the user's preference.
164 mode_t mask = umask (0777); // The arg. to umask is arbitrary...
167 // Get the file's current mode.
169 if ((stat (Filename.c_str(), &st)) == -1)
172 // Change the file to have whichever permissions bits from 'bits'
173 // that the umask would not disable.
174 if ((chmod(Filename.c_str(), (st.st_mode | (bits & ~mask)))) == -1)
180 /// MakeFileExecutable - Make the file named Filename executable by
181 /// setting whichever execute permissions bits the process's current
182 /// umask would allow. Filename must name an existing file or
183 /// directory. Returns true on success, false on error.
185 bool llvm::MakeFileExecutable(const std::string &Filename) {
186 return AddPermissionsBits(Filename, 0111);
189 /// MakeFileReadable - Make the file named Filename readable by
190 /// setting whichever read permissions bits the process's current
191 /// umask would allow. Filename must name an existing file or
192 /// directory. Returns true on success, false on error.
194 bool llvm::MakeFileReadable(const std::string &Filename) {
195 return AddPermissionsBits(Filename, 0444);
198 /// getFileSize - Return the size of the specified file in bytes, or -1 if the
199 /// file cannot be read or does not exist.
200 long long llvm::getFileSize(const std::string &Filename) {
202 if (stat(Filename.c_str(), &StatBuf) == -1)
204 return StatBuf.st_size;
207 /// getFileTimestamp - Get the last modified time for the specified file in an
208 /// unspecified format. This is useful to allow checking to see if a file was
209 /// updated since that last time the timestampt was aquired. If the file does
210 /// not exist or there is an error getting the time-stamp, zero is returned.
211 unsigned long long llvm::getFileTimestamp(const std::string &Filename) {
213 if (stat(Filename.c_str(), &StatBuf) == -1)
215 return StatBuf.st_mtime;
221 //===----------------------------------------------------------------------===//
222 // FDHandle class implementation
225 FDHandle::~FDHandle() throw() {
226 if (FD != -1) close(FD);
229 FDHandle &FDHandle::operator=(int fd) throw() {
230 if (FD != -1) close(FD);