1 //===- Support/FileUtilities.cpp - File System Utilities ------------------===//
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 implements a family of utility functions which are useful for doing
11 // various things with files.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/FileUtilities.h"
16 #include "llvm/Support/DataTypes.h"
17 #include "llvm/System/Path.h"
18 #include "llvm/Config/unistd.h"
19 #include "llvm/Config/fcntl.h"
20 #include "llvm/Config/sys/types.h"
21 #include "llvm/Config/sys/stat.h"
22 #include "llvm/Config/sys/mman.h"
23 #include "llvm/Config/alloca.h"
30 /// DiffFiles - Compare the two files specified, returning true if they are
31 /// different or if there is a file error. If you specify a string to fill in
32 /// for the error option, it will set the string to an error message if an error
33 /// occurs, allowing the caller to distinguish between a failed diff and a file
36 bool llvm::DiffFiles(const std::string &FileA, const std::string &FileB,
38 std::ifstream FileAStream(FileA.c_str());
40 if (Error) *Error = "Couldn't open file '" + FileA + "'";
44 std::ifstream FileBStream(FileB.c_str());
46 if (Error) *Error = "Couldn't open file '" + FileB + "'";
50 // Compare the two files...
53 C1 = FileAStream.get();
54 C2 = FileBStream.get();
55 if (C1 != C2) return true;
62 /// CopyFile - Copy the specified source file to the specified destination,
63 /// overwriting destination if it exists. This returns true on failure.
65 bool llvm::CopyFile(const std::string &Dest, const std::string &Src) {
66 FDHandle InFD(open(Src.c_str(), O_RDONLY));
67 if (InFD == -1) return true;
71 FDHandle OutFD(open(Dest.c_str(), O_WRONLY|O_CREAT, 0666));
72 if (OutFD == -1) return true;
75 while (ssize_t Amt = read(InFD, Buffer, 16*1024)) {
77 if (errno != EINTR) return true; // Error reading the file.
79 char *BufPtr = Buffer;
81 ssize_t AmtWritten = write(OutFD, BufPtr, Amt);
82 if (AmtWritten == -1) {
83 if (errno != EINTR) return true; // Error writing the file.
92 FR.releaseFile(); // Success!
97 /// MoveFileOverIfUpdated - If the file specified by New is different than Old,
98 /// or if Old does not exist, move the New file over the Old file. Otherwise,
99 /// remove the New file.
101 void llvm::MoveFileOverIfUpdated(const std::string &New,
102 const std::string &Old) {
103 if (DiffFiles(New, Old)) {
104 if (std::rename(New.c_str(), Old.c_str()))
105 std::cerr << "Error renaming '" << New << "' to '" << Old << "'!\n";
107 std::remove(New.c_str());
111 /// removeFile - Delete the specified file
113 void llvm::removeFile(const std::string &Filename) {
114 std::remove(Filename.c_str());
117 /// getUniqueFilename - Return a filename with the specified prefix. If the
118 /// file does not exist yet, return it, otherwise add a suffix to make it
121 std::string llvm::getUniqueFilename(const std::string &FilenameBase) {
122 if (!std::ifstream(FilenameBase.c_str()))
123 return FilenameBase; // Couldn't open the file? Use it!
125 // Create a pattern for mkstemp...
126 char *FNBuffer = new char[FilenameBase.size()+8];
127 strcpy(FNBuffer, FilenameBase.c_str());
128 strcpy(FNBuffer+FilenameBase.size(), "-XXXXXX");
130 // Agree on a temporary file name to use....
131 #if defined(HAVE_MKSTEMP) && !defined(_MSC_VER)
133 if ((TempFD = mkstemp(FNBuffer)) == -1) {
134 // FIXME: this should return an emtpy string or something and allow the
135 // caller to deal with the error!
136 std::cerr << "bugpoint: ERROR: Cannot create temporary file in the current "
141 // We don't need to hold the temp file descriptor... we will trust that no one
142 // will overwrite/delete the file while we are working on it...
145 // If we don't have mkstemp, use the old and obsolete mktemp function.
146 if (mktemp(FNBuffer) == 0) {
147 // FIXME: this should return an emtpy string or something and allow the
148 // caller to deal with the error!
149 std::cerr << "bugpoint: ERROR: Cannot create temporary file in the current "
155 std::string Result(FNBuffer);
160 //===----------------------------------------------------------------------===//
161 // FDHandle class implementation
164 FDHandle::~FDHandle() throw() {
165 if (FD != -1) close(FD);
168 FDHandle &FDHandle::operator=(int fd) throw() {
169 if (FD != -1) close(FD);