blob: bb37c999d10f546563ecfedd37a0cc5419161ea8 [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"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000024#include "llvm/Support/FileSystem.h"
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +000025#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000026#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000027#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000028#include "llvm/Support/system_error.h"
Ted Kremenek6bb816a2008-02-24 03:15:25 +000029#include "llvm/Config/config.h"
Benjamin Kramer458fb102009-09-05 09:49:39 +000030#include <map>
31#include <set>
32#include <string>
Chris Lattner291fcf02010-11-23 21:53:15 +000033
34// FIXME: This is terrible, we need this for ::close.
35#if !defined(_MSC_VER) && !defined(__MINGW32__)
36#include <unistd.h>
37#include <sys/uio.h>
38#else
39#include <io.h>
40#endif
Reid Spencer5f016e22007-07-11 17:01:13 +000041using namespace clang;
42
43// FIXME: Enhance libsystem to support inode and other fields.
44#include <sys/stat.h>
45
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000046/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +000047/// represent a dir name that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000048#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +000049
Chris Lattnerf9f77662010-11-23 20:50:22 +000050/// NON_EXISTENT_FILE - A special value distinct from null that is used to
51/// represent a filename that doesn't exist on the disk.
52#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
53
54
55FileEntry::~FileEntry() {
56 // If this FileEntry owns an open file descriptor that never got used, close
57 // it.
58 if (FD != -1) ::close(FD);
59}
60
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000061//===----------------------------------------------------------------------===//
62// Windows.
63//===----------------------------------------------------------------------===//
64
Ted Kremenek6bb816a2008-02-24 03:15:25 +000065#ifdef LLVM_ON_WIN32
66
Benjamin Krameraa8b2d92010-11-21 11:32:22 +000067#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
Ted Kremenek6bb816a2008-02-24 03:15:25 +000068
69namespace {
Mike Stump1eb44332009-09-09 15:08:12 +000070 static std::string GetFullPath(const char *relPath) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +000071 char *absPathStrPtr = _fullpath(NULL, relPath, 0);
72 assert(absPathStrPtr && "_fullpath() returned NULL!");
73
74 std::string absPath(absPathStrPtr);
75
76 free(absPathStrPtr);
77 return absPath;
78 }
79}
80
81class FileManager::UniqueDirContainer {
82 /// UniqueDirs - Cache from full path to existing directories/files.
83 ///
Mike Stump1eb44332009-09-09 15:08:12 +000084 llvm::StringMap<DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000085
86public:
87 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
88 std::string FullPath(GetFullPath(Name));
Chris Lattnerf3e8a992010-11-23 20:30:42 +000089 return UniqueDirs.GetOrCreateValue(FullPath).getValue();
Ted Kremenek6bb816a2008-02-24 03:15:25 +000090 }
Mike Stump1eb44332009-09-09 15:08:12 +000091
Chris Lattnerf3e8a992010-11-23 20:30:42 +000092 size_t size() const { return UniqueDirs.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +000093};
94
95class FileManager::UniqueFileContainer {
96 /// UniqueFiles - Cache from full path to existing directories/files.
97 ///
Ted Kremenek75368892009-01-28 01:01:07 +000098 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000099
100public:
101 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
102 std::string FullPath(GetFullPath(Name));
Chris Lattnerc070da42010-08-23 23:50:42 +0000103
104 // LowercaseString because Windows filesystem is case insensitive.
105 FullPath = llvm::LowercaseString(FullPath);
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000106 return UniqueFiles.GetOrCreateValue(FullPath).getValue();
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000107 }
108
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000109 size_t size() const { return UniqueFiles.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000110};
111
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000112//===----------------------------------------------------------------------===//
113// Unix-like Systems.
114//===----------------------------------------------------------------------===//
115
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000116#else
117
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000118#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000119
120class FileManager::UniqueDirContainer {
121 /// UniqueDirs - Cache from ID's to existing directories/files.
Mike Stump1eb44332009-09-09 15:08:12 +0000122 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000123
124public:
125 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
126 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
127 }
128
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000129 size_t size() const { return UniqueDirs.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000130};
131
132class FileManager::UniqueFileContainer {
133 /// UniqueFiles - Cache from ID's to existing directories/files.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000134 std::set<FileEntry> UniqueFiles;
135
136public:
137 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
138 return
139 const_cast<FileEntry&>(
140 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
Ted Kremenek96438f32009-02-12 03:17:57 +0000141 StatBuf.st_ino,
142 StatBuf.st_mode)).first);
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000143 }
144
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000145 size_t size() const { return UniqueFiles.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000146};
147
148#endif
149
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000150//===----------------------------------------------------------------------===//
151// Common logic.
152//===----------------------------------------------------------------------===//
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000153
Chris Lattner7ad97ff2010-11-23 07:51:02 +0000154FileManager::FileManager(const FileSystemOptions &FSO)
155 : FileSystemOpts(FSO),
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000156 UniqueDirs(*new UniqueDirContainer()),
157 UniqueFiles(*new UniqueFileContainer()),
Ted Kremenek96438f32009-02-12 03:17:57 +0000158 DirEntries(64), FileEntries(64), NextFileUID(0) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000159 NumDirLookups = NumFileLookups = 0;
160 NumDirCacheMisses = NumFileCacheMisses = 0;
161}
162
163FileManager::~FileManager() {
164 delete &UniqueDirs;
165 delete &UniqueFiles;
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000166 for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i)
167 delete VirtualFileEntries[i];
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000168}
169
Chris Lattner10e286a2010-11-23 19:19:34 +0000170void FileManager::addStatCache(FileSystemStatCache *statCache,
171 bool AtBeginning) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000172 assert(statCache && "No stat cache provided?");
173 if (AtBeginning || StatCache.get() == 0) {
174 statCache->setNextStatCache(StatCache.take());
175 StatCache.reset(statCache);
176 return;
177 }
178
Chris Lattner10e286a2010-11-23 19:19:34 +0000179 FileSystemStatCache *LastCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +0000180 while (LastCache->getNextStatCache())
181 LastCache = LastCache->getNextStatCache();
182
183 LastCache->setNextStatCache(statCache);
184}
185
Chris Lattner10e286a2010-11-23 19:19:34 +0000186void FileManager::removeStatCache(FileSystemStatCache *statCache) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000187 if (!statCache)
188 return;
189
190 if (StatCache.get() == statCache) {
191 // This is the first stat cache.
192 StatCache.reset(StatCache->takeNextStatCache());
193 return;
194 }
195
196 // Find the stat cache in the list.
Chris Lattner10e286a2010-11-23 19:19:34 +0000197 FileSystemStatCache *PrevCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +0000198 while (PrevCache && PrevCache->getNextStatCache() != statCache)
199 PrevCache = PrevCache->getNextStatCache();
Chris Lattnerf9f77662010-11-23 20:50:22 +0000200
201 assert(PrevCache && "Stat cache not found for removal");
202 PrevCache->setNextStatCache(statCache->getNextStatCache());
Douglas Gregor52e71082009-10-16 18:18:30 +0000203}
204
Douglas Gregor057e5672009-12-02 18:12:28 +0000205/// \brief Retrieve the directory that the given file name resides in.
206static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000207 llvm::StringRef Filename) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000208 // Figure out what directory it is in. If the string contains a / in it,
209 // strip off everything after it.
210 // FIXME: this logic should be in sys::Path.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000211 size_t SlashPos = Filename.size();
212 while (SlashPos != 0 && !IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
213 --SlashPos;
214
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000215 // Use the current directory if file has no path component.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000216 if (SlashPos == 0)
Chris Lattner39b49bc2010-11-23 08:35:12 +0000217 return FileMgr.getDirectory(".");
Douglas Gregor057e5672009-12-02 18:12:28 +0000218
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000219 if (SlashPos == Filename.size()-1)
Douglas Gregor057e5672009-12-02 18:12:28 +0000220 return 0; // If filename ends with a /, it's a directory.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000221
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000222 // Ignore repeated //'s.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000223 while (SlashPos != 0 && IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000224 --SlashPos;
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000225
Chris Lattner39b49bc2010-11-23 08:35:12 +0000226 return FileMgr.getDirectory(Filename.substr(0, SlashPos));
Douglas Gregor057e5672009-12-02 18:12:28 +0000227}
228
Reid Spencer5f016e22007-07-11 17:01:13 +0000229/// getDirectory - Lookup, cache, and verify the specified directory. This
230/// returns null if the directory doesn't exist.
Mike Stump1eb44332009-09-09 15:08:12 +0000231///
Chris Lattner39b49bc2010-11-23 08:35:12 +0000232const DirectoryEntry *FileManager::getDirectory(llvm::StringRef Filename) {
John Thompson9a6ac542009-12-18 14:18:21 +0000233 // stat doesn't like trailing separators (at least on Windows).
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000234 if (Filename.size() > 1 && IS_DIR_SEPARATOR_CHAR(Filename.back()))
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000235 Filename = Filename.substr(0, Filename.size()-1);
John Thompson9a6ac542009-12-18 14:18:21 +0000236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 ++NumDirLookups;
238 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000239 DirEntries.GetOrCreateValue(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 // See if there is already an entry in the map.
242 if (NamedDirEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000243 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 ? 0 : NamedDirEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 ++NumDirCacheMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000249 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 // Get the null-terminated directory name as stored as the key of the
252 // DirEntries map.
253 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 // Check to see if the directory exists.
256 struct stat StatBuf;
Chris Lattner898a0612010-11-23 21:17:56 +0000257 if (getStatValue(InterndDirName, StatBuf, 0/*directory lookup*/))
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 return 0;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000259
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 // It exists. See if we have already opened a directory with the same inode.
Mike Stump1eb44332009-09-09 15:08:12 +0000261 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000262 DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 NamedDirEnt.setValue(&UDE);
265 if (UDE.getName()) // Already have an entry with this inode, return it.
266 return &UDE;
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 // Otherwise, we don't have this directory yet, add it. We use the string
269 // key from the DirEntries map as the string.
270 UDE.Name = InterndDirName;
271 return &UDE;
272}
273
Reid Spencer5f016e22007-07-11 17:01:13 +0000274/// getFile - Lookup, cache, and verify the specified file. This returns null
275/// if the file doesn't exist.
Mike Stump1eb44332009-09-09 15:08:12 +0000276///
Chris Lattner39b49bc2010-11-23 08:35:12 +0000277const FileEntry *FileManager::getFile(llvm::StringRef Filename) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 ++NumFileLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 // See if there is already an entry in the map.
281 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000282 FileEntries.GetOrCreateValue(Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000283
284 // See if there is already an entry in the map.
285 if (NamedFileEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000286 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 ? 0 : NamedFileEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 ++NumFileCacheMisses;
290
291 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000292 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000293
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 // Get the null-terminated file name as stored as the key of the
296 // FileEntries map.
297 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000299
300 // Look up the directory for the file. When looking up something like
301 // sys/foo.h we'll discover all of the search directories that have a 'sys'
302 // subdirectory. This will let us avoid having to waste time on known-to-fail
303 // searches when we go to find sys/bar.h, because all the search directories
304 // without a 'sys' subdir will get a cached failure result.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000305 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000306 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
307 return 0;
308
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 // FIXME: Use the directory info to prune this, before doing the stat syscall.
310 // FIXME: This will reduce the # syscalls.
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 // Nope, there isn't. Check to see if the file exists.
Chris Lattner898a0612010-11-23 21:17:56 +0000313 int FileDescriptor = -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 struct stat StatBuf;
Chris Lattner898a0612010-11-23 21:17:56 +0000315 if (getStatValue(InterndFileName, StatBuf, &FileDescriptor))
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremenekbca6d122007-12-18 22:29:39 +0000318 // It exists. See if we have already opened a file with the same inode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000320 FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 NamedFileEnt.setValue(&UFE);
Chris Lattner898a0612010-11-23 21:17:56 +0000323 if (UFE.getName()) { // Already have an entry with this inode, return it.
324 // If the stat process opened the file, close it to avoid a FD leak.
325 if (FileDescriptor != -1)
326 close(FileDescriptor);
327
Reid Spencer5f016e22007-07-11 17:01:13 +0000328 return &UFE;
Chris Lattner898a0612010-11-23 21:17:56 +0000329 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000330
331 // Otherwise, we don't have this directory yet, add it.
332 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
333 // key.
334 UFE.Name = InterndFileName;
335 UFE.Size = StatBuf.st_size;
336 UFE.ModTime = StatBuf.st_mtime;
337 UFE.Dir = DirInfo;
338 UFE.UID = NextFileUID++;
Chris Lattner898a0612010-11-23 21:17:56 +0000339 UFE.FD = FileDescriptor;
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 return &UFE;
341}
342
Douglas Gregor057e5672009-12-02 18:12:28 +0000343const FileEntry *
Benjamin Kramerec1b1cc2010-07-14 23:19:41 +0000344FileManager::getVirtualFile(llvm::StringRef Filename, off_t Size,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000345 time_t ModificationTime) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000346 ++NumFileLookups;
347
348 // See if there is already an entry in the map.
349 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000350 FileEntries.GetOrCreateValue(Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000351
352 // See if there is already an entry in the map.
353 if (NamedFileEnt.getValue())
354 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
355 ? 0 : NamedFileEnt.getValue();
356
357 ++NumFileCacheMisses;
358
359 // By default, initialize it to invalid.
360 NamedFileEnt.setValue(NON_EXISTENT_FILE);
361
Chris Lattner39b49bc2010-11-23 08:35:12 +0000362 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000363 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
364 return 0;
365
366 FileEntry *UFE = new FileEntry();
367 VirtualFileEntries.push_back(UFE);
368 NamedFileEnt.setValue(UFE);
369
Chris Lattnerf9f77662010-11-23 20:50:22 +0000370 // Get the null-terminated file name as stored as the key of the
371 // FileEntries map.
372 const char *InterndFileName = NamedFileEnt.getKeyData();
373
374 UFE->Name = InterndFileName;
Douglas Gregor057e5672009-12-02 18:12:28 +0000375 UFE->Size = Size;
376 UFE->ModTime = ModificationTime;
377 UFE->Dir = DirInfo;
378 UFE->UID = NextFileUID++;
Douglas Gregor3e15e0a2010-07-26 23:54:23 +0000379
380 // If this virtual file resolves to a file, also map that file to the
381 // newly-created file entry.
Chris Lattner898a0612010-11-23 21:17:56 +0000382 int FileDescriptor = -1;
Douglas Gregor3e15e0a2010-07-26 23:54:23 +0000383 struct stat StatBuf;
Chris Lattner898a0612010-11-23 21:17:56 +0000384 if (getStatValue(InterndFileName, StatBuf, &FileDescriptor))
Chris Lattnerf9f77662010-11-23 20:50:22 +0000385 return UFE;
Chris Lattner898a0612010-11-23 21:17:56 +0000386
387 UFE->FD = FileDescriptor;
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000388 llvm::SmallString<128> FilePath(UFE->Name);
389 llvm::sys::fs::make_absolute(FilePath);
390 FileEntries[FilePath] = UFE;
Douglas Gregor057e5672009-12-02 18:12:28 +0000391 return UFE;
392}
393
Chris Lattner67452f52010-11-23 04:45:28 +0000394void FileManager::FixupRelativePath(llvm::sys::Path &path,
395 const FileSystemOptions &FSOpts) {
Michael J. Spencer256053b2010-12-17 21:22:22 +0000396 if (FSOpts.WorkingDir.empty() || llvm::sys::path::is_absolute(path.str()))
397 return;
398
399 llvm::SmallString<128> NewPath(FSOpts.WorkingDir);
400 llvm::sys::path::append(NewPath, path.str());
Chris Lattner67452f52010-11-23 04:45:28 +0000401 path = NewPath;
402}
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000403
Chris Lattner67452f52010-11-23 04:45:28 +0000404llvm::MemoryBuffer *FileManager::
Chris Lattner75dfb652010-11-23 09:19:42 +0000405getBufferForFile(const FileEntry *Entry, std::string *ErrorStr) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000406 llvm::OwningPtr<llvm::MemoryBuffer> Result;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000407 llvm::error_code ec;
Chris Lattner5cc1c732010-11-23 22:32:37 +0000408 if (FileSystemOpts.WorkingDir.empty()) {
409 const char *Filename = Entry->getName();
410 // If the file is already open, use the open file descriptor.
411 if (Entry->FD != -1) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000412 ec = llvm::MemoryBuffer::getOpenFile(Entry->FD, Filename, Result,
413 Entry->getSize());
414 if (ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000415 *ErrorStr = ec.message();
Chris Lattner5cc1c732010-11-23 22:32:37 +0000416 // getOpenFile will have closed the file descriptor, don't reuse or
417 // reclose it.
418 Entry->FD = -1;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000419 return Result.take();
Chris Lattner5cc1c732010-11-23 22:32:37 +0000420 }
421
422 // Otherwise, open the file.
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000423 ec = llvm::MemoryBuffer::getFile(Filename, Result, Entry->getSize());
424 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000425 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000426 return Result.take();
Chris Lattner5cc1c732010-11-23 22:32:37 +0000427 }
Chris Lattner67452f52010-11-23 04:45:28 +0000428
Chris Lattner5cc1c732010-11-23 22:32:37 +0000429 llvm::sys::Path FilePath(Entry->getName());
Chris Lattner67452f52010-11-23 04:45:28 +0000430 FixupRelativePath(FilePath, FileSystemOpts);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000431 ec = llvm::MemoryBuffer::getFile(FilePath.c_str(), Result, Entry->getSize());
432 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000433 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000434 return Result.take();
Chris Lattner75dfb652010-11-23 09:19:42 +0000435}
436
437llvm::MemoryBuffer *FileManager::
438getBufferForFile(llvm::StringRef Filename, std::string *ErrorStr) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000439 llvm::OwningPtr<llvm::MemoryBuffer> Result;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000440 llvm::error_code ec;
441 if (FileSystemOpts.WorkingDir.empty()) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000442 ec = llvm::MemoryBuffer::getFile(Filename, Result);
443 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000444 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000445 return Result.take();
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000446 }
447
Chris Lattner75dfb652010-11-23 09:19:42 +0000448 llvm::sys::Path FilePath(Filename);
449 FixupRelativePath(FilePath, FileSystemOpts);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000450 ec = llvm::MemoryBuffer::getFile(FilePath.c_str(), Result);
451 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000452 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000453 return Result.take();
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000454}
455
Chris Lattner10e286a2010-11-23 19:19:34 +0000456/// getStatValue - Get the 'stat' information for the specified path, using the
Chris Lattnerc0f31fd2010-12-02 04:27:29 +0000457/// cache to accelerate it if possible. This returns true if the path does not
Chris Lattner10e286a2010-11-23 19:19:34 +0000458/// exist or false if it exists.
Chris Lattnerf9f77662010-11-23 20:50:22 +0000459///
460/// The isForDir member indicates whether this is a directory lookup or not.
461/// This will return failure if the lookup isn't the expected kind.
462bool FileManager::getStatValue(const char *Path, struct stat &StatBuf,
Chris Lattner898a0612010-11-23 21:17:56 +0000463 int *FileDescriptor) {
Chris Lattner10e286a2010-11-23 19:19:34 +0000464 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
465 // absolute!
Chris Lattner11aa4b02010-11-23 19:56:39 +0000466 if (FileSystemOpts.WorkingDir.empty())
Chris Lattner898a0612010-11-23 21:17:56 +0000467 return FileSystemStatCache::get(Path, StatBuf, FileDescriptor,
468 StatCache.get());
Chris Lattner10e286a2010-11-23 19:19:34 +0000469
470 llvm::sys::Path FilePath(Path);
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000471 FixupRelativePath(FilePath, FileSystemOpts);
Chris Lattner11aa4b02010-11-23 19:56:39 +0000472
Chris Lattner898a0612010-11-23 21:17:56 +0000473 return FileSystemStatCache::get(FilePath.c_str(), StatBuf, FileDescriptor,
474 StatCache.get());
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000475}
476
Chris Lattner10e286a2010-11-23 19:19:34 +0000477
478
Reid Spencer5f016e22007-07-11 17:01:13 +0000479void FileManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000480 llvm::errs() << "\n*** File Manager Stats:\n";
481 llvm::errs() << UniqueFiles.size() << " files found, "
482 << UniqueDirs.size() << " dirs found.\n";
483 llvm::errs() << NumDirLookups << " dir lookups, "
484 << NumDirCacheMisses << " dir cache misses.\n";
485 llvm::errs() << NumFileLookups << " file lookups, "
486 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000488 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000489}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000490