blob: 3262ecce316546ba8dc3f8a7e7695f460782df23 [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>
22
Brian Gaekea2302ff2003-11-11 21:53:50 +000023/// CheckMagic - Returns true IFF the file named FN begins with Magic. FN must
24/// name a readable file.
25///
26bool CheckMagic (const std::string &FN, const std::string &Magic) {
27 char buf[1 + Magic.size ()];
28 std::ifstream f (FN.c_str ());
29 f.read (buf, Magic.size ());
30 buf[Magic.size ()] = '\0';
31 return Magic == buf;
32}
33
34/// IsArchive - Returns true IFF the file named FN appears to be a "ar" library
35/// archive. The file named FN must exist.
36///
37bool IsArchive(const std::string &FN) {
38 // Inspect the beginning of the file to see if it contains the "ar"
39 // library archive format magic string.
40 return CheckMagic (FN, "!<arch>\012");
41}
42
43/// IsBytecode - Returns true IFF the file named FN appears to be an LLVM
44/// bytecode file. The file named FN must exist.
45///
46bool IsBytecode(const std::string &FN) {
47 // Inspect the beginning of the file to see if it contains the LLVM
48 // bytecode format magic string.
49 return CheckMagic (FN, "llvm");
50}
51
Brian Gaeke56be7ff2003-11-11 18:27:21 +000052/// FileOpenable - Returns true IFF Filename names an existing regular
53/// file which we can successfully open.
54///
55bool FileOpenable (const std::string &Filename) {
56 struct stat s;
57 if (stat (Filename.c_str (), &s) == -1)
58 return false; // Cannot stat file
59 if (!S_ISREG (s.st_mode))
60 return false; // File is not a regular file
61 std::ifstream FileStream (Filename.c_str ());
62 if (!FileStream)
63 return false; // File is not openable
64 return true;
65}
66
Chris Lattner7a6ff2b2003-08-01 21:16:14 +000067/// DiffFiles - Compare the two files specified, returning true if they are
68/// different or if there is a file error. If you specify a string to fill in
69/// for the error option, it will set the string to an error message if an error
70/// occurs, allowing the caller to distinguish between a failed diff and a file
71/// system error.
72///
73bool DiffFiles(const std::string &FileA, const std::string &FileB,
74 std::string *Error) {
75 std::ifstream FileAStream(FileA.c_str());
76 if (!FileAStream) {
77 if (Error) *Error = "Couldn't open file '" + FileA + "'";
78 return true;
79 }
80
81 std::ifstream FileBStream(FileB.c_str());
82 if (!FileBStream) {
83 if (Error) *Error = "Couldn't open file '" + FileB + "'";
84 return true;
85 }
86
87 // Compare the two files...
88 int C1, C2;
89 do {
90 C1 = FileAStream.get();
91 C2 = FileBStream.get();
92 if (C1 != C2) return true;
93 } while (C1 != EOF);
94
95 return false;
96}
97
98
99/// MoveFileOverIfUpdated - If the file specified by New is different than Old,
100/// or if Old does not exist, move the New file over the Old file. Otherwise,
101/// remove the New file.
102///
103void MoveFileOverIfUpdated(const std::string &New, const std::string &Old) {
104 if (DiffFiles(New, Old)) {
105 if (std::rename(New.c_str(), Old.c_str()))
106 std::cerr << "Error renaming '" << New << "' to '" << Old << "'!\n";
107 } else {
108 std::remove(New.c_str());
109 }
110}
Misha Brukman3d1b0c72003-08-07 21:28:50 +0000111
112/// removeFile - Delete the specified file
113///
114void removeFile(const std::string &Filename) {
115 std::remove(Filename.c_str());
116}
117
118/// getUniqueFilename - Return a filename with the specified prefix. If the
119/// file does not exist yet, return it, otherwise add a suffix to make it
120/// unique.
121///
122std::string getUniqueFilename(const std::string &FilenameBase) {
123 if (!std::ifstream(FilenameBase.c_str()))
124 return FilenameBase; // Couldn't open the file? Use it!
125
126 // Create a pattern for mkstemp...
127 char *FNBuffer = new char[FilenameBase.size()+8];
128 strcpy(FNBuffer, FilenameBase.c_str());
129 strcpy(FNBuffer+FilenameBase.size(), "-XXXXXX");
130
131 // Agree on a temporary file name to use....
132 int TempFD;
133 if ((TempFD = mkstemp(FNBuffer)) == -1) {
134 std::cerr << "bugpoint: ERROR: Cannot create temporary file in the current "
135 << " directory!\n";
136 exit(1);
137 }
138
Misha Brukman950971d2003-09-16 15:31:46 +0000139 // We don't need to hold the temp file descriptor... we will trust that no one
Misha Brukman3d1b0c72003-08-07 21:28:50 +0000140 // will overwrite/delete the file while we are working on it...
141 close(TempFD);
142 std::string Result(FNBuffer);
143 delete[] FNBuffer;
144 return Result;
145}
John Criswell6991a032003-09-02 20:14:57 +0000146
Brian Gaeke56be7ff2003-11-11 18:27:21 +0000147static bool AddPermissionsBits (const std::string &Filename, mode_t bits) {
148 // Get the umask value from the operating system. We want to use it
149 // when changing the file's permissions. Since calling umask() sets
150 // the umask and returns its old value, we must call it a second
151 // time to reset it to the user's preference.
152 mode_t mask = umask (0777); // The arg. to umask is arbitrary...
John Criswell6991a032003-09-02 20:14:57 +0000153 umask (mask);
154
Brian Gaeke56be7ff2003-11-11 18:27:21 +0000155 // Get the file's current mode.
156 struct stat st;
157 if ((stat (Filename.c_str(), &st)) == -1)
John Criswell6991a032003-09-02 20:14:57 +0000158 return false;
John Criswell6991a032003-09-02 20:14:57 +0000159
Brian Gaeke56be7ff2003-11-11 18:27:21 +0000160 // Change the file to have whichever permissions bits from 'bits'
161 // that the umask would not disable.
162 if ((chmod(Filename.c_str(), (st.st_mode | (bits & ~mask)))) == -1)
John Criswell9adeccc2003-09-02 20:30:16 +0000163 return false;
John Criswell6991a032003-09-02 20:14:57 +0000164
165 return true;
166}
167
Brian Gaeke56be7ff2003-11-11 18:27:21 +0000168/// MakeFileExecutable - Make the file named Filename executable by
169/// setting whichever execute permissions bits the process's current
170/// umask would allow. Filename must name an existing file or
171/// directory. Returns true on success, false on error.
John Criswell66622be2003-09-02 21:09:30 +0000172///
Brian Gaeke56be7ff2003-11-11 18:27:21 +0000173bool MakeFileExecutable (const std::string &Filename) {
174 return AddPermissionsBits (Filename, 0111);
John Criswell66622be2003-09-02 21:09:30 +0000175}
176
Brian Gaeke56be7ff2003-11-11 18:27:21 +0000177/// MakeFileReadable - Make the file named Filename readable by
178/// setting whichever read permissions bits the process's current
179/// umask would allow. Filename must name an existing file or
180/// directory. Returns true on success, false on error.
181///
182bool MakeFileReadable (const std::string &Filename) {
183 return AddPermissionsBits (Filename, 0444);
184}