1 //===- llvm/Support/FileUtilities.h - File System Utilities -----*- C++ -*-===//
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 defines a family of utility functions which are useful for doing
11 // various things with files.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_FILEUTILITIES_H
16 #define LLVM_SUPPORT_FILEUTILITIES_H
22 /// CheckMagic - Returns true IFF the file named FN begins with Magic. FN must
23 /// name a readable file.
25 bool CheckMagic (const std::string &FN, const std::string &Magic);
27 /// IsArchive - Returns true IFF the file named FN appears to be a "ar" library
28 /// archive. The file named FN must exist.
30 bool IsArchive (const std::string &FN);
32 /// IsBytecode - Returns true IFF the file named FN appears to be an LLVM
33 /// bytecode file. The file named FN must exist.
35 bool IsBytecode (const std::string &FN);
37 /// IsSharedObject - Returns trus IFF the file named FN appears to be a shared
38 /// object with an ELF header. The file named FN must exist.
40 bool IsSharedObject(const std::string &FN);
42 /// FileOpenable - Returns true IFF Filename names an existing regular file
43 /// which we can successfully open.
45 bool FileOpenable(const std::string &Filename);
47 /// DiffFiles - Compare the two files specified, returning true if they are
48 /// different or if there is a file error. If you specify a string to fill in
49 /// for the error option, it will set the string to an error message if an error
50 /// occurs, allowing the caller to distinguish between a failed diff and a file
53 bool DiffFiles(const std::string &FileA, const std::string &FileB,
54 std::string *Error = 0);
56 /// CopyFile - Copy the specified source file to the specified destination,
57 /// overwriting destination if it exists. This returns true on failure.
59 bool CopyFile(const std::string &Dest, const std::string &Src);
61 /// MoveFileOverIfUpdated - If the file specified by New is different than Old,
62 /// or if Old does not exist, move the New file over the Old file. Otherwise,
63 /// remove the New file.
65 void MoveFileOverIfUpdated(const std::string &New, const std::string &Old);
67 /// removeFile - Delete the specified file.
69 void removeFile(const std::string &Filename);
71 /// getUniqueFilename - Return a filename with the specified prefix. If the
72 /// file does not exist yet, return it, otherwise add a suffix to make it
75 std::string getUniqueFilename(const std::string &FilenameBase);
77 /// MakeFileExecutable - This method turns on whatever access attributes are
78 /// needed to make the specified file executable. It returns true on success.
79 /// In case of failure, the file's access attributes are unspecified.
81 bool MakeFileExecutable(const std::string &Filename);
83 /// MakeFileReadable - This method turns on whatever access attributes are
84 /// needed to make the specified file readable. It returns true on success.
85 /// In case of failure, the file's access attributes are unspecified.
87 bool MakeFileReadable(const std::string &Filename);
89 /// getFileSize - Return the size of the specified file in bytes, or -1 if the
90 /// file cannot be read or does not exist.
91 long long getFileSize(const std::string &Filename);
94 /// getFileTimestamp - Get the last modified time for the specified file in an
95 /// unspecified format. This is useful to allow checking to see if a file was
96 /// updated since that last time the timestampt was aquired. If the file does
97 /// not exist or there is an error getting the time-stamp, zero is returned.
98 unsigned long long getFileTimestamp(const std::string &Filename);
100 /// ReadFileIntoAddressSpace - Attempt to map the specific file into the
101 /// address space of the current process for reading. If this succeeds,
102 /// return the address of the buffer and the length of the file mapped. On
103 /// failure, return null.
104 void *ReadFileIntoAddressSpace(const std::string &Filename, unsigned &Length);
106 /// UnmapFileFromAddressSpace - Remove the specified file from the current
108 void UnmapFileFromAddressSpace(void *Buffer, unsigned Length);
111 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
112 /// when the object is destroyed. This handle acts similarly to an
113 /// std::auto_ptr, in that the copy constructor and assignment operators
114 /// transfer ownership of the handle. This means that FDHandle's do not have
120 FDHandle() : FD(-1) {}
121 FDHandle(int fd) : FD(fd) {}
122 FDHandle(FDHandle &RHS) : FD(RHS.FD) {
123 RHS.FD = -1; // Transfer ownership
128 /// get - Get the current file descriptor, without releasing ownership of it.
129 int get() const { return FD; }
130 operator int() const { return FD; }
132 FDHandle &operator=(int fd) throw();
134 FDHandle &operator=(FDHandle &RHS) {
136 RHS.FD = -1; // Transfer ownership
137 return operator=(fd);
140 /// release - Take ownership of the file descriptor away from the FDHandle
141 /// object, so that the file is not closed when the FDHandle is destroyed.
149 /// FileRemover - This class is a simple object meant to be stack allocated.
150 /// If an exception is thrown from a region, the object removes the filename
151 /// specified (if deleteIt is true).
154 std::string Filename;
157 FileRemover(const std::string &filename, bool deleteIt = true)
158 : Filename(filename), DeleteIt(deleteIt) {}
161 if (DeleteIt) removeFile(Filename);
164 /// releaseFile - Take ownership of the file away from the FileRemover so it
165 /// will not be removed when the object is destroyed.
166 void releaseFile() { DeleteIt = false; }
168 } // End llvm namespace