blob: 02b4edd557324260e9d56f15f2ff961f3884b934 [file] [log] [blame]
Chris Lattner7a6ff2b2003-08-01 21:16:14 +00001//===- Support/FileUtilities.cpp - File System Utilities ------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner7a6ff2b2003-08-01 21:16:14 +00009//
10// This file implements a family of utility functions which are useful for doing
11// various things with files.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Support/FileUtilities.h"
Misha Brukmanda81eca2003-08-07 21:35:41 +000016#include "Config/unistd.h"
John Criswell6991a032003-09-02 20:14:57 +000017#include "Config/sys/stat.h"
18#include "Config/sys/types.h"
Chris Lattner7a6ff2b2003-08-01 21:16:14 +000019#include <fstream>
20#include <iostream>
21#include <cstdio>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Brian Gaekea2302ff2003-11-11 21:53:50 +000024/// CheckMagic - Returns true IFF the file named FN begins with Magic. FN must
25/// name a readable file.
26///
Chris Lattner2cdd21c2003-12-14 21:35:53 +000027bool llvm::CheckMagic(const std::string &FN, const std::string &Magic) {
Brian Gaekea2302ff2003-11-11 21:53:50 +000028 char buf[1 + Magic.size ()];
29 std::ifstream f (FN.c_str ());
30 f.read (buf, Magic.size ());
31 buf[Magic.size ()] = '\0';
32 return Magic == buf;
33}
34
35/// IsArchive - Returns true IFF the file named FN appears to be a "ar" library
36/// archive. The file named FN must exist.
37///
Chris Lattner2cdd21c2003-12-14 21:35:53 +000038bool llvm::IsArchive(const std::string &FN) {
Brian Gaekea2302ff2003-11-11 21:53:50 +000039 // Inspect the beginning of the file to see if it contains the "ar"
40 // library archive format magic string.
41 return CheckMagic (FN, "!<arch>\012");
42}
43
44/// IsBytecode - Returns true IFF the file named FN appears to be an LLVM
45/// bytecode file. The file named FN must exist.
46///
Chris Lattner2cdd21c2003-12-14 21:35:53 +000047bool llvm::IsBytecode(const std::string &FN) {
Brian Gaekea2302ff2003-11-11 21:53:50 +000048 // Inspect the beginning of the file to see if it contains the LLVM
49 // bytecode format magic string.
50 return CheckMagic (FN, "llvm");
51}
52
Misha Brukman17cca962003-11-24 05:28:12 +000053/// IsSharedObject - Returns trus IFF the file named FN appears to be a shared
54/// object with an ELF header. The file named FN must exist.
55///
Chris Lattner2cdd21c2003-12-14 21:35:53 +000056bool llvm::IsSharedObject(const std::string &FN) {
Misha Brukman971a7b82003-11-24 05:36:38 +000057 // Inspect the beginning of the file to see if it contains the ELF shared
58 // object magic string.
Misha Brukman17cca962003-11-24 05:28:12 +000059 static const char elfMagic[] = { 0x7f, 'E', 'L', 'F', '\0' };
60 return CheckMagic(FN, elfMagic);
61}
62
Brian Gaeke56be7ff2003-11-11 18:27:21 +000063/// FileOpenable - Returns true IFF Filename names an existing regular
64/// file which we can successfully open.
65///
Chris Lattner2cdd21c2003-12-14 21:35:53 +000066bool llvm::FileOpenable(const std::string &Filename) {
Brian Gaeke56be7ff2003-11-11 18:27:21 +000067 struct stat s;
68 if (stat (Filename.c_str (), &s) == -1)
69 return false; // Cannot stat file
70 if (!S_ISREG (s.st_mode))
71 return false; // File is not a regular file
72 std::ifstream FileStream (Filename.c_str ());
73 if (!FileStream)
74 return false; // File is not openable
75 return true;
76}
77
Chris Lattner7a6ff2b2003-08-01 21:16:14 +000078/// DiffFiles - Compare the two files specified, returning true if they are
79/// different or if there is a file error. If you specify a string to fill in
80/// for the error option, it will set the string to an error message if an error
81/// occurs, allowing the caller to distinguish between a failed diff and a file
82/// system error.
83///
Chris Lattner2cdd21c2003-12-14 21:35:53 +000084bool llvm::DiffFiles(const std::string &FileA, const std::string &FileB,
85 std::string *Error) {
Chris Lattner7a6ff2b2003-08-01 21:16:14 +000086 std::ifstream FileAStream(FileA.c_str());
87 if (!FileAStream) {
88 if (Error) *Error = "Couldn't open file '" + FileA + "'";
89 return true;
90 }
91
92 std::ifstream FileBStream(FileB.c_str());
93 if (!FileBStream) {
94 if (Error) *Error = "Couldn't open file '" + FileB + "'";
95 return true;
96 }
97
98 // Compare the two files...
99 int C1, C2;
100 do {
101 C1 = FileAStream.get();
102 C2 = FileBStream.get();
103 if (C1 != C2) return true;
104 } while (C1 != EOF);
105
106 return false;
107}
108
109
110/// MoveFileOverIfUpdated - If the file specified by New is different than Old,
111/// or if Old does not exist, move the New file over the Old file. Otherwise,
112/// remove the New file.
113///
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000114void llvm::MoveFileOverIfUpdated(const std::string &New,
115 const std::string &Old) {
Chris Lattner7a6ff2b2003-08-01 21:16:14 +0000116 if (DiffFiles(New, Old)) {
117 if (std::rename(New.c_str(), Old.c_str()))
118 std::cerr << "Error renaming '" << New << "' to '" << Old << "'!\n";
119 } else {
120 std::remove(New.c_str());
121 }
122}
Misha Brukman3d1b0c72003-08-07 21:28:50 +0000123
124/// removeFile - Delete the specified file
125///
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000126void llvm::removeFile(const std::string &Filename) {
Misha Brukman3d1b0c72003-08-07 21:28:50 +0000127 std::remove(Filename.c_str());
128}
129
130/// getUniqueFilename - Return a filename with the specified prefix. If the
131/// file does not exist yet, return it, otherwise add a suffix to make it
132/// unique.
133///
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000134std::string llvm::getUniqueFilename(const std::string &FilenameBase) {
Misha Brukman3d1b0c72003-08-07 21:28:50 +0000135 if (!std::ifstream(FilenameBase.c_str()))
136 return FilenameBase; // Couldn't open the file? Use it!
137
138 // Create a pattern for mkstemp...
139 char *FNBuffer = new char[FilenameBase.size()+8];
140 strcpy(FNBuffer, FilenameBase.c_str());
141 strcpy(FNBuffer+FilenameBase.size(), "-XXXXXX");
142
143 // Agree on a temporary file name to use....
144 int TempFD;
145 if ((TempFD = mkstemp(FNBuffer)) == -1) {
146 std::cerr << "bugpoint: ERROR: Cannot create temporary file in the current "
147 << " directory!\n";
148 exit(1);
149 }
150
Misha Brukman950971d2003-09-16 15:31:46 +0000151 // We don't need to hold the temp file descriptor... we will trust that no one
Misha Brukman3d1b0c72003-08-07 21:28:50 +0000152 // will overwrite/delete the file while we are working on it...
153 close(TempFD);
154 std::string Result(FNBuffer);
155 delete[] FNBuffer;
156 return Result;
157}
John Criswell6991a032003-09-02 20:14:57 +0000158
Brian Gaeke56be7ff2003-11-11 18:27:21 +0000159static bool AddPermissionsBits (const std::string &Filename, mode_t bits) {
160 // Get the umask value from the operating system. We want to use it
161 // when changing the file's permissions. Since calling umask() sets
162 // the umask and returns its old value, we must call it a second
163 // time to reset it to the user's preference.
164 mode_t mask = umask (0777); // The arg. to umask is arbitrary...
John Criswell6991a032003-09-02 20:14:57 +0000165 umask (mask);
166
Brian Gaeke56be7ff2003-11-11 18:27:21 +0000167 // Get the file's current mode.
168 struct stat st;
169 if ((stat (Filename.c_str(), &st)) == -1)
John Criswell6991a032003-09-02 20:14:57 +0000170 return false;
John Criswell6991a032003-09-02 20:14:57 +0000171
Brian Gaeke56be7ff2003-11-11 18:27:21 +0000172 // Change the file to have whichever permissions bits from 'bits'
173 // that the umask would not disable.
174 if ((chmod(Filename.c_str(), (st.st_mode | (bits & ~mask)))) == -1)
John Criswell9adeccc2003-09-02 20:30:16 +0000175 return false;
John Criswell6991a032003-09-02 20:14:57 +0000176
177 return true;
178}
179
Brian Gaeke56be7ff2003-11-11 18:27:21 +0000180/// MakeFileExecutable - Make the file named Filename executable by
181/// setting whichever execute permissions bits the process's current
182/// umask would allow. Filename must name an existing file or
183/// directory. Returns true on success, false on error.
John Criswell66622be2003-09-02 21:09:30 +0000184///
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000185bool llvm::MakeFileExecutable(const std::string &Filename) {
186 return AddPermissionsBits(Filename, 0111);
John Criswell66622be2003-09-02 21:09:30 +0000187}
188
Brian Gaeke56be7ff2003-11-11 18:27:21 +0000189/// MakeFileReadable - Make the file named Filename readable by
190/// setting whichever read permissions bits the process's current
191/// umask would allow. Filename must name an existing file or
192/// directory. Returns true on success, false on error.
193///
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000194bool llvm::MakeFileReadable(const std::string &Filename) {
195 return AddPermissionsBits(Filename, 0444);
Brian Gaeke56be7ff2003-11-11 18:27:21 +0000196}
Chris Lattner2d6481c2003-12-29 21:35:05 +0000197
198//===----------------------------------------------------------------------===//
199// FDHandle class implementation
200//
201
202FDHandle::~FDHandle() {
203 if (FD != -1) close(FD);
204}
205
206FDHandle &FDHandle::operator=(int fd) {
207 if (FD != -1) close(FD);
208 FD = fd;
209 return *this;
210}
211
212
213/// take - Take ownership of the file descriptor away from the FDHandle
214/// object, so that the file is not closed when the FDHandle is destroyed.
215int FDHandle::take() {
216 int Ret = FD;
217 FD = -1;
218 return Ret;
219}