blob: 138b54cc0865dd9bff7bd71861818b1f184532b4 [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.
Axel Naumann04331162011-01-27 10:55:51 +0000353 if (NamedFileEnt.getValue() && NamedFileEnt.getValue() != NON_EXISTENT_FILE)
354 return NamedFileEnt.getValue();
Douglas Gregor057e5672009-12-02 18:12:28 +0000355
356 ++NumFileCacheMisses;
357
358 // By default, initialize it to invalid.
359 NamedFileEnt.setValue(NON_EXISTENT_FILE);
360
Axel Naumann04331162011-01-27 10:55:51 +0000361 // We allow the directory to not exist. If it does exist we store it.
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000362 FileEntry *UFE = 0;
Chris Lattner39b49bc2010-11-23 08:35:12 +0000363 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000364 if (DirInfo) {
365 // Check to see if the file exists. If so, drop the virtual file
366 int FileDescriptor = -1;
367 struct stat StatBuf;
368 const char *InterndFileName = NamedFileEnt.getKeyData();
369 if (getStatValue(InterndFileName, StatBuf, &FileDescriptor) == 0) {
370 // If the stat process opened the file, close it to avoid a FD leak.
371 if (FileDescriptor != -1)
372 close(FileDescriptor);
Douglas Gregor057e5672009-12-02 18:12:28 +0000373
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000374 StatBuf.st_size = Size;
375 StatBuf.st_mtime = ModificationTime;
376 UFE = &UniqueFiles.getFile(InterndFileName, StatBuf);
377
378 NamedFileEnt.setValue(UFE);
379
380 // If we had already opened this file, close it now so we don't
381 // leak the descriptor. We're not going to use the file
382 // descriptor anyway, since this is a virtual file.
383 if (UFE->FD != -1) {
384 close(UFE->FD);
385 UFE->FD = -1;
386 }
387
388 // If we already have an entry with this inode, return it.
389 if (UFE->getName())
390 return UFE;
391 }
392 }
393
394 if (!UFE) {
395 UFE = new FileEntry();
396 VirtualFileEntries.push_back(UFE);
397 NamedFileEnt.setValue(UFE);
398 }
Douglas Gregor057e5672009-12-02 18:12:28 +0000399
Chris Lattnerf9f77662010-11-23 20:50:22 +0000400 // Get the null-terminated file name as stored as the key of the
401 // FileEntries map.
402 const char *InterndFileName = NamedFileEnt.getKeyData();
403
404 UFE->Name = InterndFileName;
Douglas Gregor057e5672009-12-02 18:12:28 +0000405 UFE->Size = Size;
406 UFE->ModTime = ModificationTime;
407 UFE->Dir = DirInfo;
408 UFE->UID = NextFileUID++;
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000409 UFE->FD = -1;
Douglas Gregor057e5672009-12-02 18:12:28 +0000410 return UFE;
411}
412
Chris Lattner67452f52010-11-23 04:45:28 +0000413void FileManager::FixupRelativePath(llvm::sys::Path &path,
414 const FileSystemOptions &FSOpts) {
Michael J. Spencer256053b2010-12-17 21:22:22 +0000415 if (FSOpts.WorkingDir.empty() || llvm::sys::path::is_absolute(path.str()))
416 return;
417
418 llvm::SmallString<128> NewPath(FSOpts.WorkingDir);
419 llvm::sys::path::append(NewPath, path.str());
Chris Lattner67452f52010-11-23 04:45:28 +0000420 path = NewPath;
421}
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000422
Chris Lattner67452f52010-11-23 04:45:28 +0000423llvm::MemoryBuffer *FileManager::
Chris Lattner75dfb652010-11-23 09:19:42 +0000424getBufferForFile(const FileEntry *Entry, std::string *ErrorStr) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000425 llvm::OwningPtr<llvm::MemoryBuffer> Result;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000426 llvm::error_code ec;
Chris Lattner5cc1c732010-11-23 22:32:37 +0000427 if (FileSystemOpts.WorkingDir.empty()) {
428 const char *Filename = Entry->getName();
429 // If the file is already open, use the open file descriptor.
430 if (Entry->FD != -1) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000431 ec = llvm::MemoryBuffer::getOpenFile(Entry->FD, Filename, Result,
432 Entry->getSize());
433 if (ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000434 *ErrorStr = ec.message();
Chris Lattner5cc1c732010-11-23 22:32:37 +0000435 // getOpenFile will have closed the file descriptor, don't reuse or
436 // reclose it.
437 Entry->FD = -1;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000438 return Result.take();
Chris Lattner5cc1c732010-11-23 22:32:37 +0000439 }
440
441 // Otherwise, open the file.
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000442 ec = llvm::MemoryBuffer::getFile(Filename, Result, Entry->getSize());
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();
Chris Lattner5cc1c732010-11-23 22:32:37 +0000446 }
Chris Lattner67452f52010-11-23 04:45:28 +0000447
Chris Lattner5cc1c732010-11-23 22:32:37 +0000448 llvm::sys::Path FilePath(Entry->getName());
Chris Lattner67452f52010-11-23 04:45:28 +0000449 FixupRelativePath(FilePath, FileSystemOpts);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000450 ec = llvm::MemoryBuffer::getFile(FilePath.c_str(), Result, Entry->getSize());
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();
Chris Lattner75dfb652010-11-23 09:19:42 +0000454}
455
456llvm::MemoryBuffer *FileManager::
457getBufferForFile(llvm::StringRef Filename, std::string *ErrorStr) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000458 llvm::OwningPtr<llvm::MemoryBuffer> Result;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000459 llvm::error_code ec;
460 if (FileSystemOpts.WorkingDir.empty()) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000461 ec = llvm::MemoryBuffer::getFile(Filename, Result);
462 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000463 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000464 return Result.take();
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000465 }
466
Chris Lattner75dfb652010-11-23 09:19:42 +0000467 llvm::sys::Path FilePath(Filename);
468 FixupRelativePath(FilePath, FileSystemOpts);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000469 ec = llvm::MemoryBuffer::getFile(FilePath.c_str(), Result);
470 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000471 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000472 return Result.take();
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000473}
474
Chris Lattner10e286a2010-11-23 19:19:34 +0000475/// getStatValue - Get the 'stat' information for the specified path, using the
Chris Lattnerc0f31fd2010-12-02 04:27:29 +0000476/// cache to accelerate it if possible. This returns true if the path does not
Chris Lattner10e286a2010-11-23 19:19:34 +0000477/// exist or false if it exists.
Chris Lattnerf9f77662010-11-23 20:50:22 +0000478///
479/// The isForDir member indicates whether this is a directory lookup or not.
480/// This will return failure if the lookup isn't the expected kind.
481bool FileManager::getStatValue(const char *Path, struct stat &StatBuf,
Chris Lattner898a0612010-11-23 21:17:56 +0000482 int *FileDescriptor) {
Chris Lattner10e286a2010-11-23 19:19:34 +0000483 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
484 // absolute!
Chris Lattner11aa4b02010-11-23 19:56:39 +0000485 if (FileSystemOpts.WorkingDir.empty())
Chris Lattner898a0612010-11-23 21:17:56 +0000486 return FileSystemStatCache::get(Path, StatBuf, FileDescriptor,
487 StatCache.get());
Chris Lattner10e286a2010-11-23 19:19:34 +0000488
489 llvm::sys::Path FilePath(Path);
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000490 FixupRelativePath(FilePath, FileSystemOpts);
Chris Lattner11aa4b02010-11-23 19:56:39 +0000491
Chris Lattner898a0612010-11-23 21:17:56 +0000492 return FileSystemStatCache::get(FilePath.c_str(), StatBuf, FileDescriptor,
493 StatCache.get());
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000494}
495
Chris Lattner10e286a2010-11-23 19:19:34 +0000496
497
Reid Spencer5f016e22007-07-11 17:01:13 +0000498void FileManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000499 llvm::errs() << "\n*** File Manager Stats:\n";
500 llvm::errs() << UniqueFiles.size() << " files found, "
501 << UniqueDirs.size() << " dirs found.\n";
502 llvm::errs() << NumDirLookups << " dir lookups, "
503 << NumDirCacheMisses << " dir cache misses.\n";
504 llvm::errs() << NumFileLookups << " file lookups, "
505 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000507 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000508}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000509