Chris Lattner | 7a6ff2b | 2003-08-01 21:16:14 +0000 | [diff] [blame] | 1 | //===- Support/FileUtilities.cpp - File System Utilities ------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7a6ff2b | 2003-08-01 21:16:14 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements a family of utility functions which are useful for doing |
| 11 | // various things with files. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Support/FileUtilities.h" |
Misha Brukman | da81eca | 2003-08-07 21:35:41 +0000 | [diff] [blame] | 16 | #include "Config/unistd.h" |
Chris Lattner | eb08299 | 2004-05-28 00:23:48 +0000 | [diff] [blame] | 17 | #include "Config/fcntl.h" |
John Criswell | 6991a03 | 2003-09-02 20:14:57 +0000 | [diff] [blame] | 18 | #include "Config/sys/stat.h" |
| 19 | #include "Config/sys/types.h" |
Chris Lattner | eb08299 | 2004-05-28 00:23:48 +0000 | [diff] [blame] | 20 | #include "Config/sys/mman.h" |
Chris Lattner | 7a6ff2b | 2003-08-01 21:16:14 +0000 | [diff] [blame] | 21 | #include <fstream> |
| 22 | #include <iostream> |
| 23 | #include <cstdio> |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Brian Gaeke | a2302ff | 2003-11-11 21:53:50 +0000 | [diff] [blame] | 26 | /// CheckMagic - Returns true IFF the file named FN begins with Magic. FN must |
| 27 | /// name a readable file. |
| 28 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 29 | bool llvm::CheckMagic(const std::string &FN, const std::string &Magic) { |
Brian Gaeke | a2302ff | 2003-11-11 21:53:50 +0000 | [diff] [blame] | 30 | char buf[1 + Magic.size ()]; |
| 31 | std::ifstream f (FN.c_str ()); |
| 32 | f.read (buf, Magic.size ()); |
| 33 | buf[Magic.size ()] = '\0'; |
| 34 | return Magic == buf; |
| 35 | } |
| 36 | |
| 37 | /// IsArchive - Returns true IFF the file named FN appears to be a "ar" library |
| 38 | /// archive. The file named FN must exist. |
| 39 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 40 | bool llvm::IsArchive(const std::string &FN) { |
Brian Gaeke | a2302ff | 2003-11-11 21:53:50 +0000 | [diff] [blame] | 41 | // Inspect the beginning of the file to see if it contains the "ar" |
| 42 | // library archive format magic string. |
| 43 | return CheckMagic (FN, "!<arch>\012"); |
| 44 | } |
| 45 | |
| 46 | /// IsBytecode - Returns true IFF the file named FN appears to be an LLVM |
| 47 | /// bytecode file. The file named FN must exist. |
| 48 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 49 | bool llvm::IsBytecode(const std::string &FN) { |
Brian Gaeke | a2302ff | 2003-11-11 21:53:50 +0000 | [diff] [blame] | 50 | // Inspect the beginning of the file to see if it contains the LLVM |
| 51 | // bytecode format magic string. |
| 52 | return CheckMagic (FN, "llvm"); |
| 53 | } |
| 54 | |
Misha Brukman | 17cca96 | 2003-11-24 05:28:12 +0000 | [diff] [blame] | 55 | /// IsSharedObject - Returns trus IFF the file named FN appears to be a shared |
| 56 | /// object with an ELF header. The file named FN must exist. |
| 57 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 58 | bool llvm::IsSharedObject(const std::string &FN) { |
Misha Brukman | 971a7b8 | 2003-11-24 05:36:38 +0000 | [diff] [blame] | 59 | // Inspect the beginning of the file to see if it contains the ELF shared |
| 60 | // object magic string. |
Misha Brukman | 17cca96 | 2003-11-24 05:28:12 +0000 | [diff] [blame] | 61 | static const char elfMagic[] = { 0x7f, 'E', 'L', 'F', '\0' }; |
| 62 | return CheckMagic(FN, elfMagic); |
| 63 | } |
| 64 | |
Brian Gaeke | 56be7ff | 2003-11-11 18:27:21 +0000 | [diff] [blame] | 65 | /// FileOpenable - Returns true IFF Filename names an existing regular |
| 66 | /// file which we can successfully open. |
| 67 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 68 | bool llvm::FileOpenable(const std::string &Filename) { |
Brian Gaeke | 56be7ff | 2003-11-11 18:27:21 +0000 | [diff] [blame] | 69 | struct stat s; |
| 70 | if (stat (Filename.c_str (), &s) == -1) |
| 71 | return false; // Cannot stat file |
| 72 | if (!S_ISREG (s.st_mode)) |
| 73 | return false; // File is not a regular file |
| 74 | std::ifstream FileStream (Filename.c_str ()); |
| 75 | if (!FileStream) |
| 76 | return false; // File is not openable |
| 77 | return true; |
| 78 | } |
| 79 | |
Chris Lattner | 7a6ff2b | 2003-08-01 21:16:14 +0000 | [diff] [blame] | 80 | /// DiffFiles - Compare the two files specified, returning true if they are |
| 81 | /// different or if there is a file error. If you specify a string to fill in |
| 82 | /// for the error option, it will set the string to an error message if an error |
| 83 | /// occurs, allowing the caller to distinguish between a failed diff and a file |
| 84 | /// system error. |
| 85 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 86 | bool llvm::DiffFiles(const std::string &FileA, const std::string &FileB, |
| 87 | std::string *Error) { |
Chris Lattner | 7a6ff2b | 2003-08-01 21:16:14 +0000 | [diff] [blame] | 88 | std::ifstream FileAStream(FileA.c_str()); |
| 89 | if (!FileAStream) { |
| 90 | if (Error) *Error = "Couldn't open file '" + FileA + "'"; |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | std::ifstream FileBStream(FileB.c_str()); |
| 95 | if (!FileBStream) { |
| 96 | if (Error) *Error = "Couldn't open file '" + FileB + "'"; |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | // Compare the two files... |
| 101 | int C1, C2; |
| 102 | do { |
| 103 | C1 = FileAStream.get(); |
| 104 | C2 = FileBStream.get(); |
| 105 | if (C1 != C2) return true; |
| 106 | } while (C1 != EOF); |
| 107 | |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /// MoveFileOverIfUpdated - If the file specified by New is different than Old, |
| 113 | /// or if Old does not exist, move the New file over the Old file. Otherwise, |
| 114 | /// remove the New file. |
| 115 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 116 | void llvm::MoveFileOverIfUpdated(const std::string &New, |
| 117 | const std::string &Old) { |
Chris Lattner | 7a6ff2b | 2003-08-01 21:16:14 +0000 | [diff] [blame] | 118 | if (DiffFiles(New, Old)) { |
| 119 | if (std::rename(New.c_str(), Old.c_str())) |
| 120 | std::cerr << "Error renaming '" << New << "' to '" << Old << "'!\n"; |
| 121 | } else { |
| 122 | std::remove(New.c_str()); |
| 123 | } |
| 124 | } |
Misha Brukman | 3d1b0c7 | 2003-08-07 21:28:50 +0000 | [diff] [blame] | 125 | |
| 126 | /// removeFile - Delete the specified file |
| 127 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 128 | void llvm::removeFile(const std::string &Filename) { |
Misha Brukman | 3d1b0c7 | 2003-08-07 21:28:50 +0000 | [diff] [blame] | 129 | std::remove(Filename.c_str()); |
| 130 | } |
| 131 | |
| 132 | /// getUniqueFilename - Return a filename with the specified prefix. If the |
| 133 | /// file does not exist yet, return it, otherwise add a suffix to make it |
| 134 | /// unique. |
| 135 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 136 | std::string llvm::getUniqueFilename(const std::string &FilenameBase) { |
Misha Brukman | 3d1b0c7 | 2003-08-07 21:28:50 +0000 | [diff] [blame] | 137 | if (!std::ifstream(FilenameBase.c_str())) |
| 138 | return FilenameBase; // Couldn't open the file? Use it! |
| 139 | |
| 140 | // Create a pattern for mkstemp... |
| 141 | char *FNBuffer = new char[FilenameBase.size()+8]; |
| 142 | strcpy(FNBuffer, FilenameBase.c_str()); |
| 143 | strcpy(FNBuffer+FilenameBase.size(), "-XXXXXX"); |
| 144 | |
| 145 | // Agree on a temporary file name to use.... |
| 146 | int TempFD; |
| 147 | if ((TempFD = mkstemp(FNBuffer)) == -1) { |
| 148 | std::cerr << "bugpoint: ERROR: Cannot create temporary file in the current " |
| 149 | << " directory!\n"; |
| 150 | exit(1); |
| 151 | } |
| 152 | |
Misha Brukman | 950971d | 2003-09-16 15:31:46 +0000 | [diff] [blame] | 153 | // We don't need to hold the temp file descriptor... we will trust that no one |
Misha Brukman | 3d1b0c7 | 2003-08-07 21:28:50 +0000 | [diff] [blame] | 154 | // will overwrite/delete the file while we are working on it... |
| 155 | close(TempFD); |
| 156 | std::string Result(FNBuffer); |
| 157 | delete[] FNBuffer; |
| 158 | return Result; |
| 159 | } |
John Criswell | 6991a03 | 2003-09-02 20:14:57 +0000 | [diff] [blame] | 160 | |
Brian Gaeke | 56be7ff | 2003-11-11 18:27:21 +0000 | [diff] [blame] | 161 | static bool AddPermissionsBits (const std::string &Filename, mode_t bits) { |
| 162 | // Get the umask value from the operating system. We want to use it |
| 163 | // when changing the file's permissions. Since calling umask() sets |
| 164 | // the umask and returns its old value, we must call it a second |
| 165 | // time to reset it to the user's preference. |
| 166 | mode_t mask = umask (0777); // The arg. to umask is arbitrary... |
John Criswell | 6991a03 | 2003-09-02 20:14:57 +0000 | [diff] [blame] | 167 | umask (mask); |
| 168 | |
Brian Gaeke | 56be7ff | 2003-11-11 18:27:21 +0000 | [diff] [blame] | 169 | // Get the file's current mode. |
| 170 | struct stat st; |
| 171 | if ((stat (Filename.c_str(), &st)) == -1) |
John Criswell | 6991a03 | 2003-09-02 20:14:57 +0000 | [diff] [blame] | 172 | return false; |
John Criswell | 6991a03 | 2003-09-02 20:14:57 +0000 | [diff] [blame] | 173 | |
Brian Gaeke | 56be7ff | 2003-11-11 18:27:21 +0000 | [diff] [blame] | 174 | // Change the file to have whichever permissions bits from 'bits' |
| 175 | // that the umask would not disable. |
| 176 | if ((chmod(Filename.c_str(), (st.st_mode | (bits & ~mask)))) == -1) |
John Criswell | 9adeccc | 2003-09-02 20:30:16 +0000 | [diff] [blame] | 177 | return false; |
John Criswell | 6991a03 | 2003-09-02 20:14:57 +0000 | [diff] [blame] | 178 | |
| 179 | return true; |
| 180 | } |
| 181 | |
Brian Gaeke | 56be7ff | 2003-11-11 18:27:21 +0000 | [diff] [blame] | 182 | /// MakeFileExecutable - Make the file named Filename executable by |
| 183 | /// setting whichever execute permissions bits the process's current |
| 184 | /// umask would allow. Filename must name an existing file or |
| 185 | /// directory. Returns true on success, false on error. |
John Criswell | 66622be | 2003-09-02 21:09:30 +0000 | [diff] [blame] | 186 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 187 | bool llvm::MakeFileExecutable(const std::string &Filename) { |
| 188 | return AddPermissionsBits(Filename, 0111); |
John Criswell | 66622be | 2003-09-02 21:09:30 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Brian Gaeke | 56be7ff | 2003-11-11 18:27:21 +0000 | [diff] [blame] | 191 | /// MakeFileReadable - Make the file named Filename readable by |
| 192 | /// setting whichever read permissions bits the process's current |
| 193 | /// umask would allow. Filename must name an existing file or |
| 194 | /// directory. Returns true on success, false on error. |
| 195 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 196 | bool llvm::MakeFileReadable(const std::string &Filename) { |
| 197 | return AddPermissionsBits(Filename, 0444); |
Brian Gaeke | 56be7ff | 2003-11-11 18:27:21 +0000 | [diff] [blame] | 198 | } |
Chris Lattner | 2d6481c | 2003-12-29 21:35:05 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 316cb08 | 2003-12-30 07:36:14 +0000 | [diff] [blame] | 200 | /// getFileSize - Return the size of the specified file in bytes, or -1 if the |
| 201 | /// file cannot be read or does not exist. |
| 202 | long long llvm::getFileSize(const std::string &Filename) { |
| 203 | struct stat StatBuf; |
| 204 | if (stat(Filename.c_str(), &StatBuf) == -1) |
| 205 | return -1; |
| 206 | return StatBuf.st_size; |
| 207 | } |
| 208 | |
Chris Lattner | 81a085a | 2003-12-31 06:15:37 +0000 | [diff] [blame] | 209 | /// getFileTimestamp - Get the last modified time for the specified file in an |
| 210 | /// unspecified format. This is useful to allow checking to see if a file was |
| 211 | /// updated since that last time the timestampt was aquired. If the file does |
| 212 | /// not exist or there is an error getting the time-stamp, zero is returned. |
| 213 | unsigned long long llvm::getFileTimestamp(const std::string &Filename) { |
| 214 | struct stat StatBuf; |
| 215 | if (stat(Filename.c_str(), &StatBuf) == -1) |
| 216 | return 0; |
| 217 | return StatBuf.st_mtime; |
| 218 | } |
| 219 | |
Chris Lattner | eb08299 | 2004-05-28 00:23:48 +0000 | [diff] [blame] | 220 | /// ReadFileIntoAddressSpace - Attempt to map the specific file into the |
| 221 | /// address space of the current process for reading. If this succeeds, |
| 222 | /// return the address of the buffer and the length of the file mapped. On |
| 223 | /// failure, return null. |
| 224 | void *llvm::ReadFileIntoAddressSpace(const std::string &Filename, |
| 225 | unsigned &Length) { |
| 226 | #ifdef HAVE_MMAP_FILE |
| 227 | Length = getFileSize(Filename); |
| 228 | if ((int)Length == -1) return 0; |
Chris Lattner | 81a085a | 2003-12-31 06:15:37 +0000 | [diff] [blame] | 229 | |
Chris Lattner | eb08299 | 2004-05-28 00:23:48 +0000 | [diff] [blame] | 230 | FDHandle FD(open(Filename.c_str(), O_RDONLY)); |
| 231 | if (FD == -1) return 0; |
Chris Lattner | 81a085a | 2003-12-31 06:15:37 +0000 | [diff] [blame] | 232 | |
Chris Lattner | e53477e | 2004-05-28 00:34:42 +0000 | [diff] [blame] | 233 | // If the file has a length of zero, mmap might return a null pointer. In |
| 234 | // this case, allocate a single byte of memory and return it instead. |
| 235 | if (Length == 0) |
| 236 | return malloc(1); |
| 237 | |
Chris Lattner | eb08299 | 2004-05-28 00:23:48 +0000 | [diff] [blame] | 238 | // mmap in the file all at once... |
| 239 | void *Buffer = (void*)mmap(0, Length, PROT_READ, MAP_PRIVATE, FD, 0); |
| 240 | |
| 241 | if (Buffer == (void*)MAP_FAILED) |
| 242 | return 0; |
Chris Lattner | e53477e | 2004-05-28 00:34:42 +0000 | [diff] [blame] | 243 | |
Chris Lattner | eb08299 | 2004-05-28 00:23:48 +0000 | [diff] [blame] | 244 | return Buffer; |
| 245 | #else |
| 246 | // FIXME: implement with read/write |
| 247 | return 0; |
| 248 | #endif |
| 249 | } |
| 250 | |
| 251 | /// UnmapFileFromAddressSpace - Remove the specified file from the current |
| 252 | /// address space. |
| 253 | void llvm::UnmapFileFromAddressSpace(void *Buffer, unsigned Length) { |
| 254 | #ifdef HAVE_MMAP_FILE |
Chris Lattner | e53477e | 2004-05-28 00:34:42 +0000 | [diff] [blame] | 255 | if (Length) |
| 256 | munmap((char*)Buffer, Length); |
| 257 | else |
| 258 | free(Buffer); // Zero byte files are malloc(1)'s. |
Chris Lattner | eb08299 | 2004-05-28 00:23:48 +0000 | [diff] [blame] | 259 | #else |
| 260 | free(Buffer); |
| 261 | #endif |
| 262 | } |
Chris Lattner | 316cb08 | 2003-12-30 07:36:14 +0000 | [diff] [blame] | 263 | |
Chris Lattner | 2d6481c | 2003-12-29 21:35:05 +0000 | [diff] [blame] | 264 | //===----------------------------------------------------------------------===// |
| 265 | // FDHandle class implementation |
| 266 | // |
| 267 | |
Chris Lattner | 9b448b7 | 2003-12-29 21:43:58 +0000 | [diff] [blame] | 268 | FDHandle::~FDHandle() throw() { |
Chris Lattner | 2d6481c | 2003-12-29 21:35:05 +0000 | [diff] [blame] | 269 | if (FD != -1) close(FD); |
| 270 | } |
| 271 | |
Chris Lattner | 9b448b7 | 2003-12-29 21:43:58 +0000 | [diff] [blame] | 272 | FDHandle &FDHandle::operator=(int fd) throw() { |
Chris Lattner | 2d6481c | 2003-12-29 21:35:05 +0000 | [diff] [blame] | 273 | if (FD != -1) close(FD); |
| 274 | FD = fd; |
| 275 | return *this; |
| 276 | } |
| 277 | |