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 /// DiffFiles - Compare the two files specified, returning true if they are
23 /// different or if there is a file error. If you specify a string to fill in
24 /// for the error option, it will set the string to an error message if an error
25 /// occurs, allowing the caller to distinguish between a failed diff and a file
28 bool DiffFiles(const std::string &FileA, const std::string &FileB,
29 std::string *Error = 0);
31 /// CopyFile - Copy the specified source file to the specified destination,
32 /// overwriting destination if it exists. This returns true on failure.
34 bool CopyFile(const std::string &Dest, const std::string &Src);
36 /// MoveFileOverIfUpdated - If the file specified by New is different than Old,
37 /// or if Old does not exist, move the New file over the Old file. Otherwise,
38 /// remove the New file.
40 void MoveFileOverIfUpdated(const std::string &New, const std::string &Old);
42 /// removeFile - Delete the specified file.
44 void removeFile(const std::string &Filename);
46 /// getUniqueFilename - Return a filename with the specified prefix. If the
47 /// file does not exist yet, return it, otherwise add a suffix to make it
50 std::string getUniqueFilename(const std::string &FilenameBase);
52 /// MakeFileExecutable - This method turns on whatever access attributes are
53 /// needed to make the specified file executable. It returns true on success.
54 /// In case of failure, the file's access attributes are unspecified.
56 bool MakeFileExecutable(const std::string &Filename);
58 /// MakeFileReadable - This method turns on whatever access attributes are
59 /// needed to make the specified file readable. It returns true on success.
60 /// In case of failure, the file's access attributes are unspecified.
62 bool MakeFileReadable(const std::string &Filename);
64 /// getFileSize - Return the size of the specified file in bytes, or -1 if the
65 /// file cannot be read or does not exist.
66 long long getFileSize(const std::string &Filename);
69 /// getFileTimestamp - Get the last modified time for the specified file in an
70 /// unspecified format. This is useful to allow checking to see if a file was
71 /// updated since that last time the timestampt was aquired. If the file does
72 /// not exist or there is an error getting the time-stamp, zero is returned.
73 unsigned long long getFileTimestamp(const std::string &Filename);
75 /// ReadFileIntoAddressSpace - Attempt to map the specific file into the
76 /// address space of the current process for reading. If this succeeds,
77 /// return the address of the buffer and the length of the file mapped. On
78 /// failure, return null.
79 void *ReadFileIntoAddressSpace(const std::string &Filename, unsigned &Length);
81 /// UnmapFileFromAddressSpace - Remove the specified file from the current
83 void UnmapFileFromAddressSpace(void *Buffer, unsigned Length);
86 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
87 /// when the object is destroyed. This handle acts similarly to an
88 /// std::auto_ptr, in that the copy constructor and assignment operators
89 /// transfer ownership of the handle. This means that FDHandle's do not have
95 FDHandle() : FD(-1) {}
96 FDHandle(int fd) : FD(fd) {}
97 FDHandle(FDHandle &RHS) : FD(RHS.FD) {
98 RHS.FD = -1; // Transfer ownership
103 /// get - Get the current file descriptor, without releasing ownership of it.
104 int get() const { return FD; }
105 operator int() const { return FD; }
107 FDHandle &operator=(int fd) throw();
109 FDHandle &operator=(FDHandle &RHS) {
111 RHS.FD = -1; // Transfer ownership
112 return operator=(fd);
115 /// release - Take ownership of the file descriptor away from the FDHandle
116 /// object, so that the file is not closed when the FDHandle is destroyed.
124 /// FileRemover - This class is a simple object meant to be stack allocated.
125 /// If an exception is thrown from a region, the object removes the filename
126 /// specified (if deleteIt is true).
129 std::string Filename;
132 FileRemover(const std::string &filename, bool deleteIt = true)
133 : Filename(filename), DeleteIt(deleteIt) {}
136 if (DeleteIt) removeFile(Filename);
139 /// releaseFile - Take ownership of the file away from the FileRemover so it
140 /// will not be removed when the object is destroyed.
141 void releaseFile() { DeleteIt = false; }
143 } // End llvm namespace