LLVM API Documentation
00001 //===- llvm/Support/FileUtilities.h - File System Utilities -----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines a family of utility functions which are useful for doing 00011 // various things with files. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_SUPPORT_FILEUTILITIES_H 00016 #define LLVM_SUPPORT_FILEUTILITIES_H 00017 00018 #include <string> 00019 00020 namespace llvm { 00021 00022 /// CheckMagic - Returns true IFF the file named FN begins with Magic. FN must 00023 /// name a readable file. 00024 /// 00025 bool CheckMagic (const std::string &FN, const std::string &Magic); 00026 00027 /// IsArchive - Returns true IFF the file named FN appears to be a "ar" library 00028 /// archive. The file named FN must exist. 00029 /// 00030 bool IsArchive (const std::string &FN); 00031 00032 /// IsBytecode - Returns true IFF the file named FN appears to be an LLVM 00033 /// bytecode file. The file named FN must exist. 00034 /// 00035 bool IsBytecode (const std::string &FN); 00036 00037 /// IsSharedObject - Returns trus IFF the file named FN appears to be a shared 00038 /// object with an ELF header. The file named FN must exist. 00039 /// 00040 bool IsSharedObject(const std::string &FN); 00041 00042 /// FileOpenable - Returns true IFF Filename names an existing regular file 00043 /// which we can successfully open. 00044 /// 00045 bool FileOpenable(const std::string &Filename); 00046 00047 /// DiffFiles - Compare the two files specified, returning true if they are 00048 /// different or if there is a file error. If you specify a string to fill in 00049 /// for the error option, it will set the string to an error message if an error 00050 /// occurs, allowing the caller to distinguish between a failed diff and a file 00051 /// system error. 00052 /// 00053 bool DiffFiles(const std::string &FileA, const std::string &FileB, 00054 std::string *Error = 0); 00055 00056 /// CopyFile - Copy the specified source file to the specified destination, 00057 /// overwriting destination if it exists. This returns true on failure. 00058 /// 00059 bool CopyFile(const std::string &Dest, const std::string &Src); 00060 00061 /// MoveFileOverIfUpdated - If the file specified by New is different than Old, 00062 /// or if Old does not exist, move the New file over the Old file. Otherwise, 00063 /// remove the New file. 00064 /// 00065 void MoveFileOverIfUpdated(const std::string &New, const std::string &Old); 00066 00067 /// removeFile - Delete the specified file. 00068 /// 00069 void removeFile(const std::string &Filename); 00070 00071 /// getUniqueFilename - Return a filename with the specified prefix. If the 00072 /// file does not exist yet, return it, otherwise add a suffix to make it 00073 /// unique. 00074 /// 00075 std::string getUniqueFilename(const std::string &FilenameBase); 00076 00077 /// MakeFileExecutable - This method turns on whatever access attributes are 00078 /// needed to make the specified file executable. It returns true on success. 00079 /// In case of failure, the file's access attributes are unspecified. 00080 /// 00081 bool MakeFileExecutable(const std::string &Filename); 00082 00083 /// MakeFileReadable - This method turns on whatever access attributes are 00084 /// needed to make the specified file readable. It returns true on success. 00085 /// In case of failure, the file's access attributes are unspecified. 00086 /// 00087 bool MakeFileReadable(const std::string &Filename); 00088 00089 /// getFileSize - Return the size of the specified file in bytes, or -1 if the 00090 /// file cannot be read or does not exist. 00091 long long getFileSize(const std::string &Filename); 00092 00093 00094 /// getFileTimestamp - Get the last modified time for the specified file in an 00095 /// unspecified format. This is useful to allow checking to see if a file was 00096 /// updated since that last time the timestampt was aquired. If the file does 00097 /// not exist or there is an error getting the time-stamp, zero is returned. 00098 unsigned long long getFileTimestamp(const std::string &Filename); 00099 00100 /// ReadFileIntoAddressSpace - Attempt to map the specific file into the 00101 /// address space of the current process for reading. If this succeeds, 00102 /// return the address of the buffer and the length of the file mapped. On 00103 /// failure, return null. 00104 void *ReadFileIntoAddressSpace(const std::string &Filename, unsigned &Length); 00105 00106 /// UnmapFileFromAddressSpace - Remove the specified file from the current 00107 /// address space. 00108 void UnmapFileFromAddressSpace(void *Buffer, unsigned Length); 00109 00110 00111 /// FDHandle - Simple handle class to make sure a file descriptor gets closed 00112 /// when the object is destroyed. This handle acts similarly to an 00113 /// std::auto_ptr, in that the copy constructor and assignment operators 00114 /// transfer ownership of the handle. This means that FDHandle's do not have 00115 /// value semantics. 00116 /// 00117 class FDHandle { 00118 int FD; 00119 public: 00120 FDHandle() : FD(-1) {} 00121 FDHandle(int fd) : FD(fd) {} 00122 FDHandle(FDHandle &RHS) : FD(RHS.FD) { 00123 RHS.FD = -1; // Transfer ownership 00124 } 00125 00126 ~FDHandle() throw(); 00127 00128 /// get - Get the current file descriptor, without releasing ownership of it. 00129 int get() const { return FD; } 00130 operator int() const { return FD; } 00131 00132 FDHandle &operator=(int fd) throw(); 00133 00134 FDHandle &operator=(FDHandle &RHS) { 00135 int fd = RHS.FD; 00136 RHS.FD = -1; // Transfer ownership 00137 return operator=(fd); 00138 } 00139 00140 /// release - Take ownership of the file descriptor away from the FDHandle 00141 /// object, so that the file is not closed when the FDHandle is destroyed. 00142 int release() { 00143 int Ret = FD; 00144 FD = -1; 00145 return Ret; 00146 } 00147 }; 00148 00149 /// FileRemover - This class is a simple object meant to be stack allocated. 00150 /// If an exception is thrown from a region, the object removes the filename 00151 /// specified (if deleteIt is true). 00152 /// 00153 class FileRemover { 00154 std::string Filename; 00155 bool DeleteIt; 00156 public: 00157 FileRemover(const std::string &filename, bool deleteIt = true) 00158 : Filename(filename), DeleteIt(deleteIt) {} 00159 00160 ~FileRemover() { 00161 if (DeleteIt) removeFile(Filename); 00162 } 00163 00164 /// releaseFile - Take ownership of the file away from the FileRemover so it 00165 /// will not be removed when the object is destroyed. 00166 void releaseFile() { DeleteIt = false; } 00167 }; 00168 } // End llvm namespace 00169 00170 #endif