d5db63f6d7c48e261899cdde647a87e1a7797f8b
[oota-llvm.git] / include / llvm / Support / FileUtilities.h
1 //===- Support/FileUtilities.h - File System Utilities ----------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a family of utility functions which are useful for doing
11 // various things with files.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef SUPPORT_FILEUTILITIES_H
16 #define SUPPORT_FILEUTILITIES_H
17
18 #include <string>
19
20 namespace llvm {
21
22 /// CheckMagic - Returns true IFF the file named FN begins with Magic. FN must
23 /// name a readable file.
24 ///
25 bool CheckMagic (const std::string &FN, const std::string &Magic);
26
27 /// IsArchive - Returns true IFF the file named FN appears to be a "ar" library
28 /// archive. The file named FN must exist.
29 ///
30 bool IsArchive (const std::string &FN);
31
32 /// IsBytecode - Returns true IFF the file named FN appears to be an LLVM
33 /// bytecode file. The file named FN must exist.
34 ///
35 bool IsBytecode (const std::string &FN);
36
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.
39 ///
40 bool IsSharedObject(const std::string &FN);
41
42 /// FileOpenable - Returns true IFF Filename names an existing regular file
43 /// which we can successfully open.
44 ///
45 bool FileOpenable(const std::string &Filename);
46
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
51 /// system error.
52 ///
53 bool DiffFiles(const std::string &FileA, const std::string &FileB,
54                std::string *Error = 0);
55
56
57 /// MoveFileOverIfUpdated - If the file specified by New is different than Old,
58 /// or if Old does not exist, move the New file over the Old file.  Otherwise,
59 /// remove the New file.
60 ///
61 void MoveFileOverIfUpdated(const std::string &New, const std::string &Old);
62  
63 /// removeFile - Delete the specified file
64 ///
65 void removeFile(const std::string &Filename);
66
67 /// getUniqueFilename - Return a filename with the specified prefix.  If the
68 /// file does not exist yet, return it, otherwise add a suffix to make it
69 /// unique.
70 ///
71 std::string getUniqueFilename(const std::string &FilenameBase);
72
73 /// MakeFileExecutable - This method turns on whatever access attributes are
74 /// needed to make the specified file executable.  It returns true on success.
75 /// In case of failure, the file's access attributes are unspecified.
76 ///
77 bool MakeFileExecutable(const std::string &Filename);
78
79 /// MakeFileReadable - This method turns on whatever access attributes are
80 /// needed to make the specified file readable.  It returns true on success.
81 /// In case of failure, the file's access attributes are unspecified.
82 ///
83 bool MakeFileReadable(const std::string &Filename);
84
85 /// getFileSize - Return the size of the specified file in bytes, or -1 if the
86 /// file cannot be read or does not exist.
87 long long getFileSize(const std::string &Filename);
88
89
90 /// getFileTimestamp - Get the last modified time for the specified file in an
91 /// unspecified format.  This is useful to allow checking to see if a file was
92 /// updated since that last time the timestampt was aquired.  If the file does
93 /// not exist or there is an error getting the time-stamp, zero is returned.
94 unsigned long long getFileTimestamp(const std::string &Filename);
95
96 /// ReadFileIntoAddressSpace - Attempt to map the specific file into the 
97 /// address space of the current process for reading.  If this succeeds, 
98 /// return the address of the buffer and the length of the file mapped.  On 
99 /// failure, return null.
100 void *ReadFileIntoAddressSpace(const std::string &Filename, unsigned &Length);
101
102 /// UnmapFileFromAddressSpace - Remove the specified file from the current
103 /// address space.
104 void UnmapFileFromAddressSpace(void *Buffer, unsigned Length);
105
106
107 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
108 /// when the object is destroyed.  This handle acts similarly to an
109 /// std::auto_ptr, in that the copy constructor and assignment operators
110 /// transfer ownership of the handle.  This means that FDHandle's do not have
111 /// value semantics.
112 ///
113 class FDHandle {
114   int FD;
115 public:
116   FDHandle() : FD(-1) {}
117   FDHandle(int fd) : FD(fd) {}
118   FDHandle(FDHandle &RHS) : FD(RHS.FD) {
119     RHS.FD = -1;       // Transfer ownership
120   }
121
122   ~FDHandle() throw();
123
124   /// get - Get the current file descriptor, without releasing ownership of it.
125   int get() const { return FD; }
126   operator int() const { return FD; }
127
128   FDHandle &operator=(int fd) throw();
129
130   FDHandle &operator=(FDHandle &RHS) {
131     int fd = RHS.FD;
132     RHS.FD = -1;       // Transfer ownership
133     return operator=(fd);
134   }
135
136   /// release - Take ownership of the file descriptor away from the FDHandle
137   /// object, so that the file is not closed when the FDHandle is destroyed.
138   int release() {
139     int Ret = FD;
140     FD = -1;
141     return Ret;
142   }
143 };
144
145   /// FileRemover - This class is a simple object meant to be stack allocated.
146   /// If an exception is thrown from a region, the object removes the filename
147   /// specified (if deleteIt is true).
148   ///
149   class FileRemover {
150     std::string Filename;
151     bool DeleteIt;
152   public:
153     FileRemover(const std::string &filename, bool deleteIt = true)
154       : Filename(filename), DeleteIt(deleteIt) {}
155     
156     ~FileRemover() {
157       if (DeleteIt) removeFile(Filename);
158     }
159
160     /// releaseFile - Take ownership of the file away from the FileRemover so it
161     /// will not be removed when the object is destroyed.
162     void releaseFile() { DeleteIt = false; }
163   };
164 } // End llvm namespace
165
166 #endif