blob: a80fae7bfaec743c3518e041133e35439685bfee [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"
Douglas Gregor4fed3f42009-04-27 18:38:38 +000026#include "llvm/System/Path.h"
Ted Kremenek6bb816a2008-02-24 03:15:25 +000027#include "llvm/Config/config.h"
Benjamin Kramer458fb102009-09-05 09:49:39 +000028#include <map>
29#include <set>
30#include <string>
Chris Lattner291fcf02010-11-23 21:53:15 +000031
32// FIXME: This is terrible, we need this for ::close.
33#if !defined(_MSC_VER) && !defined(__MINGW32__)
34#include <unistd.h>
35#include <sys/uio.h>
36#else
37#include <io.h>
38#endif
Reid Spencer5f016e22007-07-11 17:01:13 +000039using namespace clang;
40
41// FIXME: Enhance libsystem to support inode and other fields.
42#include <sys/stat.h>
43
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000044/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +000045/// represent a dir name that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000046#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +000047
Chris Lattnerf9f77662010-11-23 20:50:22 +000048/// NON_EXISTENT_FILE - A special value distinct from null that is used to
49/// represent a filename that doesn't exist on the disk.
50#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
51
52
53FileEntry::~FileEntry() {
54 // If this FileEntry owns an open file descriptor that never got used, close
55 // it.
56 if (FD != -1) ::close(FD);
57}
58
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000059//===----------------------------------------------------------------------===//
60// Windows.
61//===----------------------------------------------------------------------===//
62
Ted Kremenek6bb816a2008-02-24 03:15:25 +000063#ifdef LLVM_ON_WIN32
64
Benjamin Krameraa8b2d92010-11-21 11:32:22 +000065#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
Ted Kremenek6bb816a2008-02-24 03:15:25 +000066
67namespace {
Mike Stump1eb44332009-09-09 15:08:12 +000068 static std::string GetFullPath(const char *relPath) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +000069 char *absPathStrPtr = _fullpath(NULL, relPath, 0);
70 assert(absPathStrPtr && "_fullpath() returned NULL!");
71
72 std::string absPath(absPathStrPtr);
73
74 free(absPathStrPtr);
75 return absPath;
76 }
77}
78
79class FileManager::UniqueDirContainer {
80 /// UniqueDirs - Cache from full path to existing directories/files.
81 ///
Mike Stump1eb44332009-09-09 15:08:12 +000082 llvm::StringMap<DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000083
84public:
85 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
86 std::string FullPath(GetFullPath(Name));
Chris Lattnerf3e8a992010-11-23 20:30:42 +000087 return UniqueDirs.GetOrCreateValue(FullPath).getValue();
Ted Kremenek6bb816a2008-02-24 03:15:25 +000088 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattnerf3e8a992010-11-23 20:30:42 +000090 size_t size() const { return UniqueDirs.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +000091};
92
93class FileManager::UniqueFileContainer {
94 /// UniqueFiles - Cache from full path to existing directories/files.
95 ///
Ted Kremenek75368892009-01-28 01:01:07 +000096 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000097
98public:
99 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
100 std::string FullPath(GetFullPath(Name));
Chris Lattnerc070da42010-08-23 23:50:42 +0000101
102 // LowercaseString because Windows filesystem is case insensitive.
103 FullPath = llvm::LowercaseString(FullPath);
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000104 return UniqueFiles.GetOrCreateValue(FullPath).getValue();
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000105 }
106
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000107 size_t size() const { return UniqueFiles.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000108};
109
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000110//===----------------------------------------------------------------------===//
111// Unix-like Systems.
112//===----------------------------------------------------------------------===//
113
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000114#else
115
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000116#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000117
118class FileManager::UniqueDirContainer {
119 /// UniqueDirs - Cache from ID's to existing directories/files.
Mike Stump1eb44332009-09-09 15:08:12 +0000120 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000121
122public:
123 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
124 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
125 }
126
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000127 size_t size() const { return UniqueDirs.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000128};
129
130class FileManager::UniqueFileContainer {
131 /// UniqueFiles - Cache from ID's to existing directories/files.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000132 std::set<FileEntry> UniqueFiles;
133
134public:
135 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
136 return
137 const_cast<FileEntry&>(
138 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
Ted Kremenek96438f32009-02-12 03:17:57 +0000139 StatBuf.st_ino,
140 StatBuf.st_mode)).first);
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000141 }
142
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000143 size_t size() const { return UniqueFiles.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000144};
145
146#endif
147
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000148//===----------------------------------------------------------------------===//
149// Common logic.
150//===----------------------------------------------------------------------===//
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000151
Chris Lattner7ad97ff2010-11-23 07:51:02 +0000152FileManager::FileManager(const FileSystemOptions &FSO)
153 : FileSystemOpts(FSO),
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000154 UniqueDirs(*new UniqueDirContainer()),
155 UniqueFiles(*new UniqueFileContainer()),
Ted Kremenek96438f32009-02-12 03:17:57 +0000156 DirEntries(64), FileEntries(64), NextFileUID(0) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000157 NumDirLookups = NumFileLookups = 0;
158 NumDirCacheMisses = NumFileCacheMisses = 0;
159}
160
161FileManager::~FileManager() {
162 delete &UniqueDirs;
163 delete &UniqueFiles;
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000164 for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i)
165 delete VirtualFileEntries[i];
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000166}
167
Chris Lattner10e286a2010-11-23 19:19:34 +0000168void FileManager::addStatCache(FileSystemStatCache *statCache,
169 bool AtBeginning) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000170 assert(statCache && "No stat cache provided?");
171 if (AtBeginning || StatCache.get() == 0) {
172 statCache->setNextStatCache(StatCache.take());
173 StatCache.reset(statCache);
174 return;
175 }
176
Chris Lattner10e286a2010-11-23 19:19:34 +0000177 FileSystemStatCache *LastCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +0000178 while (LastCache->getNextStatCache())
179 LastCache = LastCache->getNextStatCache();
180
181 LastCache->setNextStatCache(statCache);
182}
183
Chris Lattner10e286a2010-11-23 19:19:34 +0000184void FileManager::removeStatCache(FileSystemStatCache *statCache) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000185 if (!statCache)
186 return;
187
188 if (StatCache.get() == statCache) {
189 // This is the first stat cache.
190 StatCache.reset(StatCache->takeNextStatCache());
191 return;
192 }
193
194 // Find the stat cache in the list.
Chris Lattner10e286a2010-11-23 19:19:34 +0000195 FileSystemStatCache *PrevCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +0000196 while (PrevCache && PrevCache->getNextStatCache() != statCache)
197 PrevCache = PrevCache->getNextStatCache();
Chris Lattnerf9f77662010-11-23 20:50:22 +0000198
199 assert(PrevCache && "Stat cache not found for removal");
200 PrevCache->setNextStatCache(statCache->getNextStatCache());
Douglas Gregor52e71082009-10-16 18:18:30 +0000201}
202
Douglas Gregor057e5672009-12-02 18:12:28 +0000203/// \brief Retrieve the directory that the given file name resides in.
204static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000205 llvm::StringRef Filename) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000206 // Figure out what directory it is in. If the string contains a / in it,
207 // strip off everything after it.
208 // FIXME: this logic should be in sys::Path.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000209 size_t SlashPos = Filename.size();
210 while (SlashPos != 0 && !IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
211 --SlashPos;
212
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000213 // Use the current directory if file has no path component.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000214 if (SlashPos == 0)
Chris Lattner39b49bc2010-11-23 08:35:12 +0000215 return FileMgr.getDirectory(".");
Douglas Gregor057e5672009-12-02 18:12:28 +0000216
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000217 if (SlashPos == Filename.size()-1)
Douglas Gregor057e5672009-12-02 18:12:28 +0000218 return 0; // If filename ends with a /, it's a directory.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000219
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000220 // Ignore repeated //'s.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000221 while (SlashPos != 0 && IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000222 --SlashPos;
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000223
Chris Lattner39b49bc2010-11-23 08:35:12 +0000224 return FileMgr.getDirectory(Filename.substr(0, SlashPos));
Douglas Gregor057e5672009-12-02 18:12:28 +0000225}
226
Reid Spencer5f016e22007-07-11 17:01:13 +0000227/// getDirectory - Lookup, cache, and verify the specified directory. This
228/// returns null if the directory doesn't exist.
Mike Stump1eb44332009-09-09 15:08:12 +0000229///
Chris Lattner39b49bc2010-11-23 08:35:12 +0000230const DirectoryEntry *FileManager::getDirectory(llvm::StringRef Filename) {
John Thompson9a6ac542009-12-18 14:18:21 +0000231 // stat doesn't like trailing separators (at least on Windows).
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000232 if (Filename.size() > 1 && IS_DIR_SEPARATOR_CHAR(Filename.back()))
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000233 Filename = Filename.substr(0, Filename.size()-1);
John Thompson9a6ac542009-12-18 14:18:21 +0000234
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 ++NumDirLookups;
236 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000237 DirEntries.GetOrCreateValue(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 // See if there is already an entry in the map.
240 if (NamedDirEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000241 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 ? 0 : NamedDirEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 ++NumDirCacheMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000247 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 // Get the null-terminated directory name as stored as the key of the
250 // DirEntries map.
251 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 // Check to see if the directory exists.
254 struct stat StatBuf;
Chris Lattner898a0612010-11-23 21:17:56 +0000255 if (getStatValue(InterndDirName, StatBuf, 0/*directory lookup*/))
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 return 0;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000257
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 // It exists. See if we have already opened a directory with the same inode.
Mike Stump1eb44332009-09-09 15:08:12 +0000259 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000260 DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 NamedDirEnt.setValue(&UDE);
263 if (UDE.getName()) // Already have an entry with this inode, return it.
264 return &UDE;
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 // Otherwise, we don't have this directory yet, add it. We use the string
267 // key from the DirEntries map as the string.
268 UDE.Name = InterndDirName;
269 return &UDE;
270}
271
Reid Spencer5f016e22007-07-11 17:01:13 +0000272/// getFile - Lookup, cache, and verify the specified file. This returns null
273/// if the file doesn't exist.
Mike Stump1eb44332009-09-09 15:08:12 +0000274///
Chris Lattner39b49bc2010-11-23 08:35:12 +0000275const FileEntry *FileManager::getFile(llvm::StringRef Filename) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 ++NumFileLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 // See if there is already an entry in the map.
279 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000280 FileEntries.GetOrCreateValue(Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000281
282 // See if there is already an entry in the map.
283 if (NamedFileEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000284 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 ? 0 : NamedFileEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 ++NumFileCacheMisses;
288
289 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000290 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000291
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 // Get the null-terminated file name as stored as the key of the
294 // FileEntries map.
295 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000297
298 // Look up the directory for the file. When looking up something like
299 // sys/foo.h we'll discover all of the search directories that have a 'sys'
300 // subdirectory. This will let us avoid having to waste time on known-to-fail
301 // searches when we go to find sys/bar.h, because all the search directories
302 // without a 'sys' subdir will get a cached failure result.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000303 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000304 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
305 return 0;
306
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 // FIXME: Use the directory info to prune this, before doing the stat syscall.
308 // FIXME: This will reduce the # syscalls.
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 // Nope, there isn't. Check to see if the file exists.
Chris Lattner898a0612010-11-23 21:17:56 +0000311 int FileDescriptor = -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 struct stat StatBuf;
Chris Lattner898a0612010-11-23 21:17:56 +0000313 if (getStatValue(InterndFileName, StatBuf, &FileDescriptor))
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Ted Kremenekbca6d122007-12-18 22:29:39 +0000316 // It exists. See if we have already opened a file with the same inode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000318 FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 NamedFileEnt.setValue(&UFE);
Chris Lattner898a0612010-11-23 21:17:56 +0000321 if (UFE.getName()) { // Already have an entry with this inode, return it.
322 // If the stat process opened the file, close it to avoid a FD leak.
323 if (FileDescriptor != -1)
324 close(FileDescriptor);
325
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 return &UFE;
Chris Lattner898a0612010-11-23 21:17:56 +0000327 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000328
329 // Otherwise, we don't have this directory yet, add it.
330 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
331 // key.
332 UFE.Name = InterndFileName;
333 UFE.Size = StatBuf.st_size;
334 UFE.ModTime = StatBuf.st_mtime;
335 UFE.Dir = DirInfo;
336 UFE.UID = NextFileUID++;
Chris Lattner898a0612010-11-23 21:17:56 +0000337 UFE.FD = FileDescriptor;
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 return &UFE;
339}
340
Douglas Gregor057e5672009-12-02 18:12:28 +0000341const FileEntry *
Benjamin Kramerec1b1cc2010-07-14 23:19:41 +0000342FileManager::getVirtualFile(llvm::StringRef Filename, off_t Size,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000343 time_t ModificationTime) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000344 ++NumFileLookups;
345
346 // See if there is already an entry in the map.
347 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000348 FileEntries.GetOrCreateValue(Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000349
350 // See if there is already an entry in the map.
351 if (NamedFileEnt.getValue())
352 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
353 ? 0 : NamedFileEnt.getValue();
354
355 ++NumFileCacheMisses;
356
357 // By default, initialize it to invalid.
358 NamedFileEnt.setValue(NON_EXISTENT_FILE);
359
Chris Lattner39b49bc2010-11-23 08:35:12 +0000360 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000361 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
362 return 0;
363
364 FileEntry *UFE = new FileEntry();
365 VirtualFileEntries.push_back(UFE);
366 NamedFileEnt.setValue(UFE);
367
Chris Lattnerf9f77662010-11-23 20:50:22 +0000368 // Get the null-terminated file name as stored as the key of the
369 // FileEntries map.
370 const char *InterndFileName = NamedFileEnt.getKeyData();
371
372 UFE->Name = InterndFileName;
Douglas Gregor057e5672009-12-02 18:12:28 +0000373 UFE->Size = Size;
374 UFE->ModTime = ModificationTime;
375 UFE->Dir = DirInfo;
376 UFE->UID = NextFileUID++;
Douglas Gregor3e15e0a2010-07-26 23:54:23 +0000377
378 // If this virtual file resolves to a file, also map that file to the
379 // newly-created file entry.
Chris Lattner898a0612010-11-23 21:17:56 +0000380 int FileDescriptor = -1;
Douglas Gregor3e15e0a2010-07-26 23:54:23 +0000381 struct stat StatBuf;
Chris Lattner898a0612010-11-23 21:17:56 +0000382 if (getStatValue(InterndFileName, StatBuf, &FileDescriptor))
Chris Lattnerf9f77662010-11-23 20:50:22 +0000383 return UFE;
Chris Lattner898a0612010-11-23 21:17:56 +0000384
385 UFE->FD = FileDescriptor;
Chris Lattnerf9f77662010-11-23 20:50:22 +0000386 llvm::sys::Path FilePath(UFE->Name);
387 FilePath.makeAbsolute();
388 FileEntries[FilePath.str()] = UFE;
Douglas Gregor057e5672009-12-02 18:12:28 +0000389 return UFE;
390}
391
Chris Lattner67452f52010-11-23 04:45:28 +0000392void FileManager::FixupRelativePath(llvm::sys::Path &path,
393 const FileSystemOptions &FSOpts) {
394 if (FSOpts.WorkingDir.empty() || path.isAbsolute()) return;
395
396 llvm::sys::Path NewPath(FSOpts.WorkingDir);
397 NewPath.appendComponent(path.str());
398 path = NewPath;
399}
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000400
Chris Lattner67452f52010-11-23 04:45:28 +0000401llvm::MemoryBuffer *FileManager::
Chris Lattner75dfb652010-11-23 09:19:42 +0000402getBufferForFile(const FileEntry *Entry, std::string *ErrorStr) {
Chris Lattner5cc1c732010-11-23 22:32:37 +0000403 if (FileSystemOpts.WorkingDir.empty()) {
404 const char *Filename = Entry->getName();
405 // If the file is already open, use the open file descriptor.
406 if (Entry->FD != -1) {
407 llvm::MemoryBuffer *Buf =
408 llvm::MemoryBuffer::getOpenFile(Entry->FD, Filename, ErrorStr,
409 Entry->getSize());
410 // getOpenFile will have closed the file descriptor, don't reuse or
411 // reclose it.
412 Entry->FD = -1;
413 return Buf;
414 }
415
416 // Otherwise, open the file.
Chris Lattnerf8f61292010-11-23 19:38:22 +0000417 return llvm::MemoryBuffer::getFile(Filename, ErrorStr, Entry->getSize());
Chris Lattner5cc1c732010-11-23 22:32:37 +0000418 }
Chris Lattner67452f52010-11-23 04:45:28 +0000419
Chris Lattner5cc1c732010-11-23 22:32:37 +0000420 llvm::sys::Path FilePath(Entry->getName());
Chris Lattner67452f52010-11-23 04:45:28 +0000421 FixupRelativePath(FilePath, FileSystemOpts);
Chris Lattnerf8f61292010-11-23 19:38:22 +0000422 return llvm::MemoryBuffer::getFile(FilePath.c_str(), ErrorStr,
423 Entry->getSize());
Chris Lattner75dfb652010-11-23 09:19:42 +0000424}
425
426llvm::MemoryBuffer *FileManager::
427getBufferForFile(llvm::StringRef Filename, std::string *ErrorStr) {
428 if (FileSystemOpts.WorkingDir.empty())
429 return llvm::MemoryBuffer::getFile(Filename, ErrorStr);
430
431 llvm::sys::Path FilePath(Filename);
432 FixupRelativePath(FilePath, FileSystemOpts);
433 return llvm::MemoryBuffer::getFile(FilePath.c_str(), ErrorStr);
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000434}
435
Chris Lattner10e286a2010-11-23 19:19:34 +0000436/// getStatValue - Get the 'stat' information for the specified path, using the
437/// cache to accellerate it if possible. This returns true if the path does not
438/// exist or false if it exists.
Chris Lattnerf9f77662010-11-23 20:50:22 +0000439///
440/// The isForDir member indicates whether this is a directory lookup or not.
441/// This will return failure if the lookup isn't the expected kind.
442bool FileManager::getStatValue(const char *Path, struct stat &StatBuf,
Chris Lattner898a0612010-11-23 21:17:56 +0000443 int *FileDescriptor) {
Chris Lattner10e286a2010-11-23 19:19:34 +0000444 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
445 // absolute!
Chris Lattner11aa4b02010-11-23 19:56:39 +0000446 if (FileSystemOpts.WorkingDir.empty())
Chris Lattner898a0612010-11-23 21:17:56 +0000447 return FileSystemStatCache::get(Path, StatBuf, FileDescriptor,
448 StatCache.get());
Chris Lattner10e286a2010-11-23 19:19:34 +0000449
450 llvm::sys::Path FilePath(Path);
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000451 FixupRelativePath(FilePath, FileSystemOpts);
Chris Lattner11aa4b02010-11-23 19:56:39 +0000452
Chris Lattner898a0612010-11-23 21:17:56 +0000453 return FileSystemStatCache::get(FilePath.c_str(), StatBuf, FileDescriptor,
454 StatCache.get());
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000455}
456
Chris Lattner10e286a2010-11-23 19:19:34 +0000457
458
Reid Spencer5f016e22007-07-11 17:01:13 +0000459void FileManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000460 llvm::errs() << "\n*** File Manager Stats:\n";
461 llvm::errs() << UniqueFiles.size() << " files found, "
462 << UniqueDirs.size() << " dirs found.\n";
463 llvm::errs() << NumDirLookups << " dir lookups, "
464 << NumDirCacheMisses << " dir cache misses.\n";
465 llvm::errs() << NumFileLookups << " file lookups, "
466 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000468 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000469}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000470