blob: 488d4c3b8dfeb489a4f8f928d70291907f8d3059 [file] [log] [blame]
Chris Lattner10e286a2010-11-23 19:19:34 +00001//===--- FileManager.cpp - File System Probing and Caching ----------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the FileManager interface.
11//
12//===----------------------------------------------------------------------===//
13//
14// TODO: This should index all interesting directories with dirent calls.
15// getdirentries ?
16// opendir/readdir_r/closedir ?
17//
18//===----------------------------------------------------------------------===//
19
20#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000021#include "clang/Basic/FileSystemStatCache.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "llvm/ADT/SmallString.h"
Chris Lattnerc070da42010-08-23 23:50:42 +000023#include "llvm/ADT/StringExtras.h"
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000025#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000026#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000027#include "llvm/Support/system_error.h"
Ted Kremenek6bb816a2008-02-24 03:15:25 +000028#include "llvm/Config/config.h"
Benjamin Kramer458fb102009-09-05 09:49:39 +000029#include <map>
30#include <set>
31#include <string>
Chris Lattner291fcf02010-11-23 21:53:15 +000032
33// FIXME: This is terrible, we need this for ::close.
34#if !defined(_MSC_VER) && !defined(__MINGW32__)
35#include <unistd.h>
36#include <sys/uio.h>
37#else
38#include <io.h>
39#endif
Reid Spencer5f016e22007-07-11 17:01:13 +000040using namespace clang;
41
42// FIXME: Enhance libsystem to support inode and other fields.
43#include <sys/stat.h>
44
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000045/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +000046/// represent a dir name that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000047#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +000048
Chris Lattnerf9f77662010-11-23 20:50:22 +000049/// NON_EXISTENT_FILE - A special value distinct from null that is used to
50/// represent a filename that doesn't exist on the disk.
51#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
52
53
54FileEntry::~FileEntry() {
55 // If this FileEntry owns an open file descriptor that never got used, close
56 // it.
57 if (FD != -1) ::close(FD);
58}
59
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000060//===----------------------------------------------------------------------===//
61// Windows.
62//===----------------------------------------------------------------------===//
63
Ted Kremenek6bb816a2008-02-24 03:15:25 +000064#ifdef LLVM_ON_WIN32
65
Benjamin Krameraa8b2d92010-11-21 11:32:22 +000066#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
Ted Kremenek6bb816a2008-02-24 03:15:25 +000067
68namespace {
Mike Stump1eb44332009-09-09 15:08:12 +000069 static std::string GetFullPath(const char *relPath) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +000070 char *absPathStrPtr = _fullpath(NULL, relPath, 0);
71 assert(absPathStrPtr && "_fullpath() returned NULL!");
72
73 std::string absPath(absPathStrPtr);
74
75 free(absPathStrPtr);
76 return absPath;
77 }
78}
79
80class FileManager::UniqueDirContainer {
81 /// UniqueDirs - Cache from full path to existing directories/files.
82 ///
Mike Stump1eb44332009-09-09 15:08:12 +000083 llvm::StringMap<DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000084
85public:
86 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
87 std::string FullPath(GetFullPath(Name));
Chris Lattnerf3e8a992010-11-23 20:30:42 +000088 return UniqueDirs.GetOrCreateValue(FullPath).getValue();
Ted Kremenek6bb816a2008-02-24 03:15:25 +000089 }
Mike Stump1eb44332009-09-09 15:08:12 +000090
Chris Lattnerf3e8a992010-11-23 20:30:42 +000091 size_t size() const { return UniqueDirs.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +000092};
93
94class FileManager::UniqueFileContainer {
95 /// UniqueFiles - Cache from full path to existing directories/files.
96 ///
Ted Kremenek75368892009-01-28 01:01:07 +000097 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000098
99public:
100 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
101 std::string FullPath(GetFullPath(Name));
Chris Lattnerc070da42010-08-23 23:50:42 +0000102
103 // LowercaseString because Windows filesystem is case insensitive.
104 FullPath = llvm::LowercaseString(FullPath);
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000105 return UniqueFiles.GetOrCreateValue(FullPath).getValue();
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000106 }
107
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000108 size_t size() const { return UniqueFiles.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000109};
110
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000111//===----------------------------------------------------------------------===//
112// Unix-like Systems.
113//===----------------------------------------------------------------------===//
114
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000115#else
116
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000117#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000118
119class FileManager::UniqueDirContainer {
120 /// UniqueDirs - Cache from ID's to existing directories/files.
Mike Stump1eb44332009-09-09 15:08:12 +0000121 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000122
123public:
124 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
125 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
126 }
127
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000128 size_t size() const { return UniqueDirs.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000129};
130
131class FileManager::UniqueFileContainer {
132 /// UniqueFiles - Cache from ID's to existing directories/files.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000133 std::set<FileEntry> UniqueFiles;
134
135public:
136 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
137 return
138 const_cast<FileEntry&>(
139 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
Ted Kremenek96438f32009-02-12 03:17:57 +0000140 StatBuf.st_ino,
141 StatBuf.st_mode)).first);
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000142 }
143
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000144 size_t size() const { return UniqueFiles.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000145};
146
147#endif
148
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000149//===----------------------------------------------------------------------===//
150// Common logic.
151//===----------------------------------------------------------------------===//
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000152
Chris Lattner7ad97ff2010-11-23 07:51:02 +0000153FileManager::FileManager(const FileSystemOptions &FSO)
154 : FileSystemOpts(FSO),
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000155 UniqueDirs(*new UniqueDirContainer()),
156 UniqueFiles(*new UniqueFileContainer()),
Ted Kremenek96438f32009-02-12 03:17:57 +0000157 DirEntries(64), FileEntries(64), NextFileUID(0) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000158 NumDirLookups = NumFileLookups = 0;
159 NumDirCacheMisses = NumFileCacheMisses = 0;
160}
161
162FileManager::~FileManager() {
163 delete &UniqueDirs;
164 delete &UniqueFiles;
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000165 for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i)
166 delete VirtualFileEntries[i];
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000167}
168
Chris Lattner10e286a2010-11-23 19:19:34 +0000169void FileManager::addStatCache(FileSystemStatCache *statCache,
170 bool AtBeginning) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000171 assert(statCache && "No stat cache provided?");
172 if (AtBeginning || StatCache.get() == 0) {
173 statCache->setNextStatCache(StatCache.take());
174 StatCache.reset(statCache);
175 return;
176 }
177
Chris Lattner10e286a2010-11-23 19:19:34 +0000178 FileSystemStatCache *LastCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +0000179 while (LastCache->getNextStatCache())
180 LastCache = LastCache->getNextStatCache();
181
182 LastCache->setNextStatCache(statCache);
183}
184
Chris Lattner10e286a2010-11-23 19:19:34 +0000185void FileManager::removeStatCache(FileSystemStatCache *statCache) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000186 if (!statCache)
187 return;
188
189 if (StatCache.get() == statCache) {
190 // This is the first stat cache.
191 StatCache.reset(StatCache->takeNextStatCache());
192 return;
193 }
194
195 // Find the stat cache in the list.
Chris Lattner10e286a2010-11-23 19:19:34 +0000196 FileSystemStatCache *PrevCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +0000197 while (PrevCache && PrevCache->getNextStatCache() != statCache)
198 PrevCache = PrevCache->getNextStatCache();
Chris Lattnerf9f77662010-11-23 20:50:22 +0000199
200 assert(PrevCache && "Stat cache not found for removal");
201 PrevCache->setNextStatCache(statCache->getNextStatCache());
Douglas Gregor52e71082009-10-16 18:18:30 +0000202}
203
Douglas Gregor057e5672009-12-02 18:12:28 +0000204/// \brief Retrieve the directory that the given file name resides in.
205static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000206 llvm::StringRef Filename) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000207 // Figure out what directory it is in. If the string contains a / in it,
208 // strip off everything after it.
209 // FIXME: this logic should be in sys::Path.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000210 size_t SlashPos = Filename.size();
211 while (SlashPos != 0 && !IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
212 --SlashPos;
213
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000214 // Use the current directory if file has no path component.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000215 if (SlashPos == 0)
Chris Lattner39b49bc2010-11-23 08:35:12 +0000216 return FileMgr.getDirectory(".");
Douglas Gregor057e5672009-12-02 18:12:28 +0000217
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000218 if (SlashPos == Filename.size()-1)
Douglas Gregor057e5672009-12-02 18:12:28 +0000219 return 0; // If filename ends with a /, it's a directory.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000220
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000221 // Ignore repeated //'s.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000222 while (SlashPos != 0 && IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000223 --SlashPos;
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000224
Chris Lattner39b49bc2010-11-23 08:35:12 +0000225 return FileMgr.getDirectory(Filename.substr(0, SlashPos));
Douglas Gregor057e5672009-12-02 18:12:28 +0000226}
227
Reid Spencer5f016e22007-07-11 17:01:13 +0000228/// getDirectory - Lookup, cache, and verify the specified directory. This
229/// returns null if the directory doesn't exist.
Mike Stump1eb44332009-09-09 15:08:12 +0000230///
Chris Lattner39b49bc2010-11-23 08:35:12 +0000231const DirectoryEntry *FileManager::getDirectory(llvm::StringRef Filename) {
John Thompson9a6ac542009-12-18 14:18:21 +0000232 // stat doesn't like trailing separators (at least on Windows).
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000233 if (Filename.size() > 1 && IS_DIR_SEPARATOR_CHAR(Filename.back()))
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000234 Filename = Filename.substr(0, Filename.size()-1);
John Thompson9a6ac542009-12-18 14:18:21 +0000235
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 ++NumDirLookups;
237 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000238 DirEntries.GetOrCreateValue(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 // See if there is already an entry in the map.
241 if (NamedDirEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000242 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 ? 0 : NamedDirEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Reid Spencer5f016e22007-07-11 17:01:13 +0000245 ++NumDirCacheMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000248 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 // Get the null-terminated directory name as stored as the key of the
251 // DirEntries map.
252 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 // Check to see if the directory exists.
255 struct stat StatBuf;
Chris Lattner898a0612010-11-23 21:17:56 +0000256 if (getStatValue(InterndDirName, StatBuf, 0/*directory lookup*/))
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 return 0;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000258
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 // It exists. See if we have already opened a directory with the same inode.
Mike Stump1eb44332009-09-09 15:08:12 +0000260 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000261 DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 NamedDirEnt.setValue(&UDE);
264 if (UDE.getName()) // Already have an entry with this inode, return it.
265 return &UDE;
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 // Otherwise, we don't have this directory yet, add it. We use the string
268 // key from the DirEntries map as the string.
269 UDE.Name = InterndDirName;
270 return &UDE;
271}
272
Reid Spencer5f016e22007-07-11 17:01:13 +0000273/// getFile - Lookup, cache, and verify the specified file. This returns null
274/// if the file doesn't exist.
Mike Stump1eb44332009-09-09 15:08:12 +0000275///
Chris Lattner39b49bc2010-11-23 08:35:12 +0000276const FileEntry *FileManager::getFile(llvm::StringRef Filename) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 ++NumFileLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 // See if there is already an entry in the map.
280 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000281 FileEntries.GetOrCreateValue(Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000282
283 // See if there is already an entry in the map.
284 if (NamedFileEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000285 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 ? 0 : NamedFileEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 ++NumFileCacheMisses;
289
290 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000291 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000292
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 // Get the null-terminated file name as stored as the key of the
295 // FileEntries map.
296 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000298
299 // Look up the directory for the file. When looking up something like
300 // sys/foo.h we'll discover all of the search directories that have a 'sys'
301 // subdirectory. This will let us avoid having to waste time on known-to-fail
302 // searches when we go to find sys/bar.h, because all the search directories
303 // without a 'sys' subdir will get a cached failure result.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000304 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000305 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
306 return 0;
307
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 // FIXME: Use the directory info to prune this, before doing the stat syscall.
309 // FIXME: This will reduce the # syscalls.
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 // Nope, there isn't. Check to see if the file exists.
Chris Lattner898a0612010-11-23 21:17:56 +0000312 int FileDescriptor = -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 struct stat StatBuf;
Chris Lattner898a0612010-11-23 21:17:56 +0000314 if (getStatValue(InterndFileName, StatBuf, &FileDescriptor))
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Ted Kremenekbca6d122007-12-18 22:29:39 +0000317 // It exists. See if we have already opened a file with the same inode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000319 FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 NamedFileEnt.setValue(&UFE);
Chris Lattner898a0612010-11-23 21:17:56 +0000322 if (UFE.getName()) { // Already have an entry with this inode, return it.
323 // If the stat process opened the file, close it to avoid a FD leak.
324 if (FileDescriptor != -1)
325 close(FileDescriptor);
326
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 return &UFE;
Chris Lattner898a0612010-11-23 21:17:56 +0000328 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000329
330 // Otherwise, we don't have this directory yet, add it.
331 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
332 // key.
333 UFE.Name = InterndFileName;
334 UFE.Size = StatBuf.st_size;
335 UFE.ModTime = StatBuf.st_mtime;
336 UFE.Dir = DirInfo;
337 UFE.UID = NextFileUID++;
Chris Lattner898a0612010-11-23 21:17:56 +0000338 UFE.FD = FileDescriptor;
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 return &UFE;
340}
341
Douglas Gregor057e5672009-12-02 18:12:28 +0000342const FileEntry *
Benjamin Kramerec1b1cc2010-07-14 23:19:41 +0000343FileManager::getVirtualFile(llvm::StringRef Filename, off_t Size,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000344 time_t ModificationTime) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000345 ++NumFileLookups;
346
347 // See if there is already an entry in the map.
348 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000349 FileEntries.GetOrCreateValue(Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000350
351 // See if there is already an entry in the map.
352 if (NamedFileEnt.getValue())
353 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
354 ? 0 : NamedFileEnt.getValue();
355
356 ++NumFileCacheMisses;
357
358 // By default, initialize it to invalid.
359 NamedFileEnt.setValue(NON_EXISTENT_FILE);
360
Chris Lattner39b49bc2010-11-23 08:35:12 +0000361 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000362 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
363 return 0;
364
365 FileEntry *UFE = new FileEntry();
366 VirtualFileEntries.push_back(UFE);
367 NamedFileEnt.setValue(UFE);
368
Chris Lattnerf9f77662010-11-23 20:50:22 +0000369 // Get the null-terminated file name as stored as the key of the
370 // FileEntries map.
371 const char *InterndFileName = NamedFileEnt.getKeyData();
372
373 UFE->Name = InterndFileName;
Douglas Gregor057e5672009-12-02 18:12:28 +0000374 UFE->Size = Size;
375 UFE->ModTime = ModificationTime;
376 UFE->Dir = DirInfo;
377 UFE->UID = NextFileUID++;
Douglas Gregor3e15e0a2010-07-26 23:54:23 +0000378
379 // If this virtual file resolves to a file, also map that file to the
380 // newly-created file entry.
Chris Lattner898a0612010-11-23 21:17:56 +0000381 int FileDescriptor = -1;
Douglas Gregor3e15e0a2010-07-26 23:54:23 +0000382 struct stat StatBuf;
Chris Lattner898a0612010-11-23 21:17:56 +0000383 if (getStatValue(InterndFileName, StatBuf, &FileDescriptor))
Chris Lattnerf9f77662010-11-23 20:50:22 +0000384 return UFE;
Chris Lattner898a0612010-11-23 21:17:56 +0000385
386 UFE->FD = FileDescriptor;
Chris Lattnerf9f77662010-11-23 20:50:22 +0000387 llvm::sys::Path FilePath(UFE->Name);
388 FilePath.makeAbsolute();
389 FileEntries[FilePath.str()] = UFE;
Douglas Gregor057e5672009-12-02 18:12:28 +0000390 return UFE;
391}
392
Chris Lattner67452f52010-11-23 04:45:28 +0000393void FileManager::FixupRelativePath(llvm::sys::Path &path,
394 const FileSystemOptions &FSOpts) {
395 if (FSOpts.WorkingDir.empty() || path.isAbsolute()) return;
396
397 llvm::sys::Path NewPath(FSOpts.WorkingDir);
398 NewPath.appendComponent(path.str());
399 path = NewPath;
400}
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000401
Chris Lattner67452f52010-11-23 04:45:28 +0000402llvm::MemoryBuffer *FileManager::
Chris Lattner75dfb652010-11-23 09:19:42 +0000403getBufferForFile(const FileEntry *Entry, std::string *ErrorStr) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000404 llvm::OwningPtr<llvm::MemoryBuffer> Result;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000405 llvm::error_code ec;
Chris Lattner5cc1c732010-11-23 22:32:37 +0000406 if (FileSystemOpts.WorkingDir.empty()) {
407 const char *Filename = Entry->getName();
408 // If the file is already open, use the open file descriptor.
409 if (Entry->FD != -1) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000410 ec = llvm::MemoryBuffer::getOpenFile(Entry->FD, Filename, Result,
411 Entry->getSize());
412 if (ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000413 *ErrorStr = ec.message();
Chris Lattner5cc1c732010-11-23 22:32:37 +0000414 // getOpenFile will have closed the file descriptor, don't reuse or
415 // reclose it.
416 Entry->FD = -1;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000417 return Result.take();
Chris Lattner5cc1c732010-11-23 22:32:37 +0000418 }
419
420 // Otherwise, open the file.
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000421 ec = llvm::MemoryBuffer::getFile(Filename, Result, Entry->getSize());
422 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000423 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000424 return Result.take();
Chris Lattner5cc1c732010-11-23 22:32:37 +0000425 }
Chris Lattner67452f52010-11-23 04:45:28 +0000426
Chris Lattner5cc1c732010-11-23 22:32:37 +0000427 llvm::sys::Path FilePath(Entry->getName());
Chris Lattner67452f52010-11-23 04:45:28 +0000428 FixupRelativePath(FilePath, FileSystemOpts);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000429 ec = llvm::MemoryBuffer::getFile(FilePath.c_str(), Result, Entry->getSize());
430 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000431 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000432 return Result.take();
Chris Lattner75dfb652010-11-23 09:19:42 +0000433}
434
435llvm::MemoryBuffer *FileManager::
436getBufferForFile(llvm::StringRef Filename, std::string *ErrorStr) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000437 llvm::OwningPtr<llvm::MemoryBuffer> Result;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000438 llvm::error_code ec;
439 if (FileSystemOpts.WorkingDir.empty()) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000440 ec = llvm::MemoryBuffer::getFile(Filename, Result);
441 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000442 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000443 return Result.take();
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000444 }
445
Chris Lattner75dfb652010-11-23 09:19:42 +0000446 llvm::sys::Path FilePath(Filename);
447 FixupRelativePath(FilePath, FileSystemOpts);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000448 ec = llvm::MemoryBuffer::getFile(FilePath.c_str(), Result);
449 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000450 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000451 return Result.take();
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000452}
453
Chris Lattner10e286a2010-11-23 19:19:34 +0000454/// getStatValue - Get the 'stat' information for the specified path, using the
Chris Lattnerc0f31fd2010-12-02 04:27:29 +0000455/// cache to accelerate it if possible. This returns true if the path does not
Chris Lattner10e286a2010-11-23 19:19:34 +0000456/// exist or false if it exists.
Chris Lattnerf9f77662010-11-23 20:50:22 +0000457///
458/// The isForDir member indicates whether this is a directory lookup or not.
459/// This will return failure if the lookup isn't the expected kind.
460bool FileManager::getStatValue(const char *Path, struct stat &StatBuf,
Chris Lattner898a0612010-11-23 21:17:56 +0000461 int *FileDescriptor) {
Chris Lattner10e286a2010-11-23 19:19:34 +0000462 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
463 // absolute!
Chris Lattner11aa4b02010-11-23 19:56:39 +0000464 if (FileSystemOpts.WorkingDir.empty())
Chris Lattner898a0612010-11-23 21:17:56 +0000465 return FileSystemStatCache::get(Path, StatBuf, FileDescriptor,
466 StatCache.get());
Chris Lattner10e286a2010-11-23 19:19:34 +0000467
468 llvm::sys::Path FilePath(Path);
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000469 FixupRelativePath(FilePath, FileSystemOpts);
Chris Lattner11aa4b02010-11-23 19:56:39 +0000470
Chris Lattner898a0612010-11-23 21:17:56 +0000471 return FileSystemStatCache::get(FilePath.c_str(), StatBuf, FileDescriptor,
472 StatCache.get());
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000473}
474
Chris Lattner10e286a2010-11-23 19:19:34 +0000475
476
Reid Spencer5f016e22007-07-11 17:01:13 +0000477void FileManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000478 llvm::errs() << "\n*** File Manager Stats:\n";
479 llvm::errs() << UniqueFiles.size() << " files found, "
480 << UniqueDirs.size() << " dirs found.\n";
481 llvm::errs() << NumDirLookups << " dir lookups, "
482 << NumDirCacheMisses << " dir cache misses.\n";
483 llvm::errs() << NumFileLookups << " file lookups, "
484 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000486 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000487}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000488