blob: 579c8185b3d193c9de4a451415f254eb9d11e896 [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "llvm/Config/llvm-config.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"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000026#include "llvm/Support/Path.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000027#include "llvm/Support/raw_ostream.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000028#include "llvm/Support/system_error.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
Reid Spencer5f016e22007-07-11 17:01:13 +000033using namespace clang;
34
35// FIXME: Enhance libsystem to support inode and other fields.
36#include <sys/stat.h>
37
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000038/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +000039/// represent a dir name that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000040#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +000041
Chris Lattnerf9f77662010-11-23 20:50:22 +000042/// NON_EXISTENT_FILE - A special value distinct from null that is used to
43/// represent a filename that doesn't exist on the disk.
44#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
45
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000046//===----------------------------------------------------------------------===//
47// Common logic.
48//===----------------------------------------------------------------------===//
Ted Kremenek6bb816a2008-02-24 03:15:25 +000049
Stephen Hines651f13c2014-04-23 16:59:28 -070050FileManager::FileManager(const FileSystemOptions &FSO,
51 IntrusiveRefCntPtr<vfs::FileSystem> FS)
52 : FS(FS), FileSystemOpts(FSO),
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000053 SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +000054 NumDirLookups = NumFileLookups = 0;
55 NumDirCacheMisses = NumFileCacheMisses = 0;
Stephen Hines651f13c2014-04-23 16:59:28 -070056
57 // If the caller doesn't provide a virtual file system, just grab the real
58 // file system.
59 if (!FS)
60 this->FS = vfs::getRealFileSystem();
Ted Kremenek6bb816a2008-02-24 03:15:25 +000061}
62
63FileManager::~FileManager() {
Chris Lattnerf3e8a992010-11-23 20:30:42 +000064 for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i)
65 delete VirtualFileEntries[i];
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000066 for (unsigned i = 0, e = VirtualDirectoryEntries.size(); i != e; ++i)
67 delete VirtualDirectoryEntries[i];
Ted Kremenek6bb816a2008-02-24 03:15:25 +000068}
69
Chris Lattner10e286a2010-11-23 19:19:34 +000070void FileManager::addStatCache(FileSystemStatCache *statCache,
71 bool AtBeginning) {
Douglas Gregor52e71082009-10-16 18:18:30 +000072 assert(statCache && "No stat cache provided?");
73 if (AtBeginning || StatCache.get() == 0) {
Stephen Hines651f13c2014-04-23 16:59:28 -070074 statCache->setNextStatCache(StatCache.release());
Douglas Gregor52e71082009-10-16 18:18:30 +000075 StatCache.reset(statCache);
76 return;
77 }
78
Chris Lattner10e286a2010-11-23 19:19:34 +000079 FileSystemStatCache *LastCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +000080 while (LastCache->getNextStatCache())
81 LastCache = LastCache->getNextStatCache();
82
83 LastCache->setNextStatCache(statCache);
84}
85
Chris Lattner10e286a2010-11-23 19:19:34 +000086void FileManager::removeStatCache(FileSystemStatCache *statCache) {
Douglas Gregor52e71082009-10-16 18:18:30 +000087 if (!statCache)
88 return;
89
90 if (StatCache.get() == statCache) {
91 // This is the first stat cache.
92 StatCache.reset(StatCache->takeNextStatCache());
93 return;
94 }
95
96 // Find the stat cache in the list.
Chris Lattner10e286a2010-11-23 19:19:34 +000097 FileSystemStatCache *PrevCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +000098 while (PrevCache && PrevCache->getNextStatCache() != statCache)
99 PrevCache = PrevCache->getNextStatCache();
Chris Lattnerf9f77662010-11-23 20:50:22 +0000100
101 assert(PrevCache && "Stat cache not found for removal");
102 PrevCache->setNextStatCache(statCache->getNextStatCache());
Douglas Gregor52e71082009-10-16 18:18:30 +0000103}
104
Manuel Klimek98be8602012-07-31 13:56:54 +0000105void FileManager::clearStatCaches() {
106 StatCache.reset(0);
107}
108
Douglas Gregor057e5672009-12-02 18:12:28 +0000109/// \brief Retrieve the directory that the given file name resides in.
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000110/// Filename can point to either a real file or a virtual file.
Douglas Gregor057e5672009-12-02 18:12:28 +0000111static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
Douglas Gregor6e975c42011-09-13 23:15:45 +0000112 StringRef Filename,
113 bool CacheFailure) {
Zhanyong Wan21af8872011-02-11 21:25:35 +0000114 if (Filename.empty())
115 return NULL;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000116
Zhanyong Wan21af8872011-02-11 21:25:35 +0000117 if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
118 return NULL; // If Filename is a directory.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000119
Chris Lattner5f9e2722011-07-23 10:55:15 +0000120 StringRef DirName = llvm::sys::path::parent_path(Filename);
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000121 // Use the current directory if file has no path component.
Zhanyong Wan21af8872011-02-11 21:25:35 +0000122 if (DirName.empty())
123 DirName = ".";
Douglas Gregor057e5672009-12-02 18:12:28 +0000124
Douglas Gregor6e975c42011-09-13 23:15:45 +0000125 return FileMgr.getDirectory(DirName, CacheFailure);
Douglas Gregor057e5672009-12-02 18:12:28 +0000126}
127
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000128/// Add all ancestors of the given path (pointing to either a file or
129/// a directory) as virtual directories.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000130void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
131 StringRef DirName = llvm::sys::path::parent_path(Path);
Zhanyong Wan21af8872011-02-11 21:25:35 +0000132 if (DirName.empty())
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000133 return;
134
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000135 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
136 SeenDirEntries.GetOrCreateValue(DirName);
137
138 // When caching a virtual directory, we always cache its ancestors
139 // at the same time. Therefore, if DirName is already in the cache,
140 // we don't need to recurse as its ancestors must also already be in
141 // the cache.
142 if (NamedDirEnt.getValue())
143 return;
144
145 // Add the virtual directory to the cache.
146 DirectoryEntry *UDE = new DirectoryEntry;
147 UDE->Name = NamedDirEnt.getKeyData();
148 NamedDirEnt.setValue(UDE);
149 VirtualDirectoryEntries.push_back(UDE);
150
151 // Recursively add the other ancestors.
152 addAncestorsAsVirtualDirs(DirName);
153}
154
Douglas Gregor6e975c42011-09-13 23:15:45 +0000155const DirectoryEntry *FileManager::getDirectory(StringRef DirName,
156 bool CacheFailure) {
NAKAMURA Takumi759a4b42012-06-16 06:04:10 +0000157 // stat doesn't like trailing separators except for root directory.
NAKAMURA Takumi678a3ea2011-11-17 06:16:05 +0000158 // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
159 // (though it can strip '\\')
NAKAMURA Takumi759a4b42012-06-16 06:04:10 +0000160 if (DirName.size() > 1 &&
161 DirName != llvm::sys::path::root_path(DirName) &&
162 llvm::sys::path::is_separator(DirName.back()))
NAKAMURA Takumi678a3ea2011-11-17 06:16:05 +0000163 DirName = DirName.substr(0, DirName.size()-1);
Rafael Espindola146d57f2013-07-29 15:47:24 +0000164#ifdef LLVM_ON_WIN32
165 // Fixing a problem with "clang C:test.c" on Windows.
166 // Stat("C:") does not recognize "C:" as a valid directory
167 std::string DirNameStr;
168 if (DirName.size() > 1 && DirName.back() == ':' &&
169 DirName.equals_lower(llvm::sys::path::root_name(DirName))) {
170 DirNameStr = DirName.str() + '.';
171 DirName = DirNameStr;
172 }
173#endif
NAKAMURA Takumi678a3ea2011-11-17 06:16:05 +0000174
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 ++NumDirLookups;
176 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000177 SeenDirEntries.GetOrCreateValue(DirName);
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000179 // See if there was already an entry in the map. Note that the map
180 // contains both virtual and real directories.
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 if (NamedDirEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000182 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 ? 0 : NamedDirEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 ++NumDirCacheMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000188 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 // Get the null-terminated directory name as stored as the key of the
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000191 // SeenDirEntries map.
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 // Check to see if the directory exists.
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000195 FileData Data;
196 if (getStatValue(InterndDirName, Data, false, 0 /*directory lookup*/)) {
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000197 // There's no real directory at the given path.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000198 if (!CacheFailure)
199 SeenDirEntries.erase(DirName);
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 return 0;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000201 }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000202
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000203 // It exists. See if we have already opened a directory with the
204 // same inode (this occurs on Unix-like systems when one dir is
205 // symlinked to another, for example) or the same path (on
206 // Windows).
Stephen Hines651f13c2014-04-23 16:59:28 -0700207 DirectoryEntry &UDE = UniqueRealDirs[Data.UniqueID];
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 NamedDirEnt.setValue(&UDE);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000210 if (!UDE.getName()) {
211 // We don't have this directory yet, add it. We use the string
212 // key from the SeenDirEntries map as the string.
213 UDE.Name = InterndDirName;
214 }
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 return &UDE;
217}
218
Douglas Gregor6e975c42011-09-13 23:15:45 +0000219const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
220 bool CacheFailure) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 ++NumFileLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 // See if there is already an entry in the map.
224 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000225 SeenFileEntries.GetOrCreateValue(Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000226
227 // See if there is already an entry in the map.
228 if (NamedFileEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000229 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 ? 0 : NamedFileEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 ++NumFileCacheMisses;
233
234 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000235 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 // Get the null-terminated file name as stored as the key of the
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000238 // SeenFileEntries map.
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000241 // Look up the directory for the file. When looking up something like
242 // sys/foo.h we'll discover all of the search directories that have a 'sys'
243 // subdirectory. This will let us avoid having to waste time on known-to-fail
244 // searches when we go to find sys/bar.h, because all the search directories
245 // without a 'sys' subdir will get a cached failure result.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000246 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
247 CacheFailure);
248 if (DirInfo == 0) { // Directory doesn't exist, file can't exist.
249 if (!CacheFailure)
250 SeenFileEntries.erase(Filename);
251
Douglas Gregor057e5672009-12-02 18:12:28 +0000252 return 0;
Douglas Gregor6e975c42011-09-13 23:15:45 +0000253 }
254
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 // FIXME: Use the directory info to prune this, before doing the stat syscall.
256 // FIXME: This will reduce the # syscalls.
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 // Nope, there isn't. Check to see if the file exists.
Stephen Hines651f13c2014-04-23 16:59:28 -0700259 vfs::File *F = 0;
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000260 FileData Data;
Stephen Hines651f13c2014-04-23 16:59:28 -0700261 if (getStatValue(InterndFileName, Data, true, openFile ? &F : 0)) {
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000262 // There's no real file at the given path.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000263 if (!CacheFailure)
264 SeenFileEntries.erase(Filename);
265
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 return 0;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000267 }
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Stephen Hines651f13c2014-04-23 16:59:28 -0700269 assert((openFile || !F) && "undesired open file");
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000270
Ted Kremenekbca6d122007-12-18 22:29:39 +0000271 // It exists. See if we have already opened a file with the same inode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 // This occurs when one dir is symlinked to another, for example.
Stephen Hines651f13c2014-04-23 16:59:28 -0700273 FileEntry &UFE = UniqueRealFiles[Data.UniqueID];
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 NamedFileEnt.setValue(&UFE);
Stephen Hines651f13c2014-04-23 16:59:28 -0700276 if (UFE.isValid()) { // Already have an entry with this inode, return it.
Chris Lattner898a0612010-11-23 21:17:56 +0000277 // If the stat process opened the file, close it to avoid a FD leak.
Stephen Hines651f13c2014-04-23 16:59:28 -0700278 if (F)
279 delete F;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000280
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 return &UFE;
Chris Lattner898a0612010-11-23 21:17:56 +0000282 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000283
Stephen Hines651f13c2014-04-23 16:59:28 -0700284 // Otherwise, we don't have this file yet, add it.
285 UFE.Name = Data.Name;
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000286 UFE.Size = Data.Size;
287 UFE.ModTime = Data.ModTime;
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 UFE.Dir = DirInfo;
289 UFE.UID = NextFileUID++;
Stephen Hines651f13c2014-04-23 16:59:28 -0700290 UFE.UniqueID = Data.UniqueID;
291 UFE.IsNamedPipe = Data.IsNamedPipe;
292 UFE.InPCH = Data.InPCH;
293 UFE.File.reset(F);
294 UFE.IsValid = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 return &UFE;
296}
297
Douglas Gregor057e5672009-12-02 18:12:28 +0000298const FileEntry *
Chris Lattner5f9e2722011-07-23 10:55:15 +0000299FileManager::getVirtualFile(StringRef Filename, off_t Size,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000300 time_t ModificationTime) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000301 ++NumFileLookups;
302
303 // See if there is already an entry in the map.
304 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000305 SeenFileEntries.GetOrCreateValue(Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000306
307 // See if there is already an entry in the map.
Axel Naumann04331162011-01-27 10:55:51 +0000308 if (NamedFileEnt.getValue() && NamedFileEnt.getValue() != NON_EXISTENT_FILE)
309 return NamedFileEnt.getValue();
Douglas Gregor057e5672009-12-02 18:12:28 +0000310
311 ++NumFileCacheMisses;
312
313 // By default, initialize it to invalid.
314 NamedFileEnt.setValue(NON_EXISTENT_FILE);
315
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000316 addAncestorsAsVirtualDirs(Filename);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000317 FileEntry *UFE = 0;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000318
319 // Now that all ancestors of Filename are in the cache, the
320 // following call is guaranteed to find the DirectoryEntry from the
321 // cache.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000322 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
323 /*CacheFailure=*/true);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000324 assert(DirInfo &&
325 "The directory of a virtual file should already be in the cache.");
Douglas Gregor057e5672009-12-02 18:12:28 +0000326
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000327 // Check to see if the file exists. If so, drop the virtual file
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000328 FileData Data;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000329 const char *InterndFileName = NamedFileEnt.getKeyData();
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000330 if (getStatValue(InterndFileName, Data, true, 0) == 0) {
331 Data.Size = Size;
332 Data.ModTime = ModificationTime;
Stephen Hines651f13c2014-04-23 16:59:28 -0700333 UFE = &UniqueRealFiles[Data.UniqueID];
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000334
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000335 NamedFileEnt.setValue(UFE);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000336
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000337 // If we had already opened this file, close it now so we don't
338 // leak the descriptor. We're not going to use the file
339 // descriptor anyway, since this is a virtual file.
Stephen Hines651f13c2014-04-23 16:59:28 -0700340 if (UFE->File)
341 UFE->closeFile();
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000342
343 // If we already have an entry with this inode, return it.
Stephen Hines651f13c2014-04-23 16:59:28 -0700344 if (UFE->isValid())
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000345 return UFE;
Stephen Hines651f13c2014-04-23 16:59:28 -0700346
347 UFE->UniqueID = Data.UniqueID;
348 UFE->IsNamedPipe = Data.IsNamedPipe;
349 UFE->InPCH = Data.InPCH;
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000350 }
351
352 if (!UFE) {
353 UFE = new FileEntry();
354 VirtualFileEntries.push_back(UFE);
355 NamedFileEnt.setValue(UFE);
356 }
Douglas Gregor057e5672009-12-02 18:12:28 +0000357
Chris Lattnerf9f77662010-11-23 20:50:22 +0000358 UFE->Name = InterndFileName;
Douglas Gregor057e5672009-12-02 18:12:28 +0000359 UFE->Size = Size;
360 UFE->ModTime = ModificationTime;
361 UFE->Dir = DirInfo;
362 UFE->UID = NextFileUID++;
Stephen Hines651f13c2014-04-23 16:59:28 -0700363 UFE->File.reset();
Douglas Gregor057e5672009-12-02 18:12:28 +0000364 return UFE;
365}
366
Chris Lattner5f9e2722011-07-23 10:55:15 +0000367void FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
368 StringRef pathRef(path.data(), path.size());
Anders Carlssonaf036a62011-03-06 22:25:35 +0000369
Anders Carlsson2e2468e2011-03-14 01:13:54 +0000370 if (FileSystemOpts.WorkingDir.empty()
371 || llvm::sys::path::is_absolute(pathRef))
Michael J. Spencer256053b2010-12-17 21:22:22 +0000372 return;
373
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000374 SmallString<128> NewPath(FileSystemOpts.WorkingDir);
Anders Carlssonaf036a62011-03-06 22:25:35 +0000375 llvm::sys::path::append(NewPath, pathRef);
Chris Lattner67452f52010-11-23 04:45:28 +0000376 path = NewPath;
377}
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000378
Chris Lattner67452f52010-11-23 04:45:28 +0000379llvm::MemoryBuffer *FileManager::
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +0000380getBufferForFile(const FileEntry *Entry, std::string *ErrorStr,
381 bool isVolatile) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700382 std::unique_ptr<llvm::MemoryBuffer> Result;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000383 llvm::error_code ec;
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000384
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +0000385 uint64_t FileSize = Entry->getSize();
386 // If there's a high enough chance that the file have changed since we
387 // got its size, force a stat before opening it.
388 if (isVolatile)
389 FileSize = -1;
390
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000391 const char *Filename = Entry->getName();
392 // If the file is already open, use the open file descriptor.
Stephen Hines651f13c2014-04-23 16:59:28 -0700393 if (Entry->File) {
394 ec = Entry->File->getBuffer(Filename, Result, FileSize);
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000395 if (ErrorStr)
396 *ErrorStr = ec.message();
Stephen Hines651f13c2014-04-23 16:59:28 -0700397 Entry->closeFile();
398 return Result.release();
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000399 }
400
401 // Otherwise, open the file.
402
Chris Lattner5cc1c732010-11-23 22:32:37 +0000403 if (FileSystemOpts.WorkingDir.empty()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700404 ec = FS->getBufferForFile(Filename, Result, FileSize);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000405 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000406 *ErrorStr = ec.message();
Stephen Hines651f13c2014-04-23 16:59:28 -0700407 return Result.release();
Chris Lattner5cc1c732010-11-23 22:32:37 +0000408 }
Anders Carlssonaf036a62011-03-06 22:25:35 +0000409
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000410 SmallString<128> FilePath(Entry->getName());
Anders Carlsson03fd3622011-03-07 01:28:33 +0000411 FixupRelativePath(FilePath);
Stephen Hines651f13c2014-04-23 16:59:28 -0700412 ec = FS->getBufferForFile(FilePath.str(), Result, FileSize);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000413 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000414 *ErrorStr = ec.message();
Stephen Hines651f13c2014-04-23 16:59:28 -0700415 return Result.release();
Chris Lattner75dfb652010-11-23 09:19:42 +0000416}
417
418llvm::MemoryBuffer *FileManager::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000419getBufferForFile(StringRef Filename, std::string *ErrorStr) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700420 std::unique_ptr<llvm::MemoryBuffer> Result;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000421 llvm::error_code ec;
422 if (FileSystemOpts.WorkingDir.empty()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700423 ec = FS->getBufferForFile(Filename, Result);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000424 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000425 *ErrorStr = ec.message();
Stephen Hines651f13c2014-04-23 16:59:28 -0700426 return Result.release();
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000427 }
428
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000429 SmallString<128> FilePath(Filename);
Anders Carlsson03fd3622011-03-07 01:28:33 +0000430 FixupRelativePath(FilePath);
Stephen Hines651f13c2014-04-23 16:59:28 -0700431 ec = FS->getBufferForFile(FilePath.c_str(), Result);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000432 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000433 *ErrorStr = ec.message();
Stephen Hines651f13c2014-04-23 16:59:28 -0700434 return Result.release();
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000435}
436
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000437/// getStatValue - Get the 'stat' information for the specified path,
438/// using the cache to accelerate it if possible. This returns true
439/// if the path points to a virtual file or does not exist, or returns
440/// false if it's an existent real file. If FileDescriptor is NULL,
441/// do directory look-up instead of file look-up.
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000442bool FileManager::getStatValue(const char *Path, FileData &Data, bool isFile,
Stephen Hines651f13c2014-04-23 16:59:28 -0700443 vfs::File **F) {
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())
Stephen Hines651f13c2014-04-23 16:59:28 -0700447 return FileSystemStatCache::get(Path, Data, isFile, F,StatCache.get(), *FS);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000448
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000449 SmallString<128> FilePath(Path);
Anders Carlsson03fd3622011-03-07 01:28:33 +0000450 FixupRelativePath(FilePath);
Chris Lattner11aa4b02010-11-23 19:56:39 +0000451
Stephen Hines651f13c2014-04-23 16:59:28 -0700452 return FileSystemStatCache::get(FilePath.c_str(), Data, isFile, F,
453 StatCache.get(), *FS);
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000454}
455
Rafael Espindolaaefb1d32013-07-29 18:22:23 +0000456bool FileManager::getNoncachedStatValue(StringRef Path,
Stephen Hines651f13c2014-04-23 16:59:28 -0700457 vfs::Status &Result) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000458 SmallString<128> FilePath(Path);
Anders Carlsson7dbafb32011-03-18 19:23:19 +0000459 FixupRelativePath(FilePath);
460
Stephen Hines651f13c2014-04-23 16:59:28 -0700461 llvm::ErrorOr<vfs::Status> S = FS->status(FilePath.c_str());
462 if (!S)
463 return true;
464 Result = *S;
465 return false;
Anders Carlsson7dbafb32011-03-18 19:23:19 +0000466}
467
Axel Naumann5ba05592012-07-10 16:50:27 +0000468void FileManager::invalidateCache(const FileEntry *Entry) {
469 assert(Entry && "Cannot invalidate a NULL FileEntry");
Axel Naumann3ce42c32012-06-27 09:17:42 +0000470
471 SeenFileEntries.erase(Entry->getName());
Axel Naumann5ba05592012-07-10 16:50:27 +0000472
473 // FileEntry invalidation should not block future optimizations in the file
474 // caches. Possible alternatives are cache truncation (invalidate last N) or
475 // invalidation of the whole cache.
Stephen Hines651f13c2014-04-23 16:59:28 -0700476 UniqueRealFiles.erase(Entry->getUniqueID());
Axel Naumann3ce42c32012-06-27 09:17:42 +0000477}
478
479
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000480void FileManager::GetUniqueIDMapping(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000481 SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000482 UIDToFiles.clear();
483 UIDToFiles.resize(NextFileUID);
484
485 // Map file entries
486 for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000487 FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end();
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000488 FE != FEEnd; ++FE)
489 if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE)
490 UIDToFiles[FE->getValue()->getUID()] = FE->getValue();
491
492 // Map virtual file entries
Craig Topper09d19ef2013-07-04 03:08:24 +0000493 for (SmallVectorImpl<FileEntry *>::const_iterator
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000494 VFE = VirtualFileEntries.begin(), VFEEnd = VirtualFileEntries.end();
495 VFE != VFEEnd; ++VFE)
496 if (*VFE && *VFE != NON_EXISTENT_FILE)
497 UIDToFiles[(*VFE)->getUID()] = *VFE;
498}
Chris Lattner10e286a2010-11-23 19:19:34 +0000499
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +0000500void FileManager::modifyFileEntry(FileEntry *File,
501 off_t Size, time_t ModificationTime) {
502 File->Size = Size;
503 File->ModTime = ModificationTime;
504}
505
Douglas Gregor713b7c02013-01-26 00:55:12 +0000506StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
507 // FIXME: use llvm::sys::fs::canonical() when it gets implemented
508#ifdef LLVM_ON_UNIX
509 llvm::DenseMap<const DirectoryEntry *, llvm::StringRef>::iterator Known
510 = CanonicalDirNames.find(Dir);
511 if (Known != CanonicalDirNames.end())
512 return Known->second;
513
514 StringRef CanonicalName(Dir->getName());
515 char CanonicalNameBuf[PATH_MAX];
516 if (realpath(Dir->getName(), CanonicalNameBuf)) {
517 unsigned Len = strlen(CanonicalNameBuf);
518 char *Mem = static_cast<char *>(CanonicalNameStorage.Allocate(Len, 1));
519 memcpy(Mem, CanonicalNameBuf, Len);
520 CanonicalName = StringRef(Mem, Len);
521 }
522
523 CanonicalDirNames.insert(std::make_pair(Dir, CanonicalName));
524 return CanonicalName;
525#else
526 return StringRef(Dir->getName());
527#endif
528}
Chris Lattner10e286a2010-11-23 19:19:34 +0000529
Reid Spencer5f016e22007-07-11 17:01:13 +0000530void FileManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000531 llvm::errs() << "\n*** File Manager Stats:\n";
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000532 llvm::errs() << UniqueRealFiles.size() << " real files found, "
533 << UniqueRealDirs.size() << " real dirs found.\n";
534 llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
535 << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000536 llvm::errs() << NumDirLookups << " dir lookups, "
537 << NumDirCacheMisses << " dir cache misses.\n";
538 llvm::errs() << NumFileLookups << " file lookups, "
539 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000541 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000542}