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