blob: 14731f6b09cd82c56494be7a84b7aebdf4da9df7 [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?");
Stephen Hines6bcf27b2014-05-29 04:14:42 -070073 if (AtBeginning || !StatCache.get()) {
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() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700106 StatCache.reset(nullptr);
Manuel Klimek98be8602012-07-31 13:56:54 +0000107}
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())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700115 return nullptr;
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]))
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700118 return nullptr; // 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())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700182 return NamedDirEnt.getValue() == NON_EXISTENT_DIR ? nullptr
183 : 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;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700196 if (getStatValue(InterndDirName, Data, false, nullptr /*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);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700200 return nullptr;
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
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700230 ? nullptr : 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);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700248 if (DirInfo == nullptr) { // Directory doesn't exist, file can't exist.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000249 if (!CacheFailure)
250 SeenFileEntries.erase(Filename);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700251
252 return nullptr;
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 Hines6bcf27b2014-05-29 04:14:42 -0700259 vfs::File *F = nullptr;
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000260 FileData Data;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700261 if (getStatValue(InterndFileName, Data, true, openFile ? &F : nullptr)) {
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);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700265
266 return nullptr;
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.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700277
278 // FIXME: this hack ensures that if we look up a file by a virtual path in
279 // the VFS that the getDir() will have the virtual path, even if we found
280 // the file by a 'real' path first. This is required in order to find a
281 // module's structure when its headers/module map are mapped in the VFS.
282 // We should remove this as soon as we can properly support a file having
283 // multiple names.
284 if (DirInfo != UFE.Dir && Data.IsVFSMapped)
285 UFE.Dir = DirInfo;
286
Chris Lattner898a0612010-11-23 21:17:56 +0000287 // If the stat process opened the file, close it to avoid a FD leak.
Stephen Hines651f13c2014-04-23 16:59:28 -0700288 if (F)
289 delete F;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000290
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 return &UFE;
Chris Lattner898a0612010-11-23 21:17:56 +0000292 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000293
Stephen Hines651f13c2014-04-23 16:59:28 -0700294 // Otherwise, we don't have this file yet, add it.
295 UFE.Name = Data.Name;
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000296 UFE.Size = Data.Size;
297 UFE.ModTime = Data.ModTime;
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 UFE.Dir = DirInfo;
299 UFE.UID = NextFileUID++;
Stephen Hines651f13c2014-04-23 16:59:28 -0700300 UFE.UniqueID = Data.UniqueID;
301 UFE.IsNamedPipe = Data.IsNamedPipe;
302 UFE.InPCH = Data.InPCH;
303 UFE.File.reset(F);
304 UFE.IsValid = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 return &UFE;
306}
307
Douglas Gregor057e5672009-12-02 18:12:28 +0000308const FileEntry *
Chris Lattner5f9e2722011-07-23 10:55:15 +0000309FileManager::getVirtualFile(StringRef Filename, off_t Size,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000310 time_t ModificationTime) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000311 ++NumFileLookups;
312
313 // See if there is already an entry in the map.
314 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000315 SeenFileEntries.GetOrCreateValue(Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000316
317 // See if there is already an entry in the map.
Axel Naumann04331162011-01-27 10:55:51 +0000318 if (NamedFileEnt.getValue() && NamedFileEnt.getValue() != NON_EXISTENT_FILE)
319 return NamedFileEnt.getValue();
Douglas Gregor057e5672009-12-02 18:12:28 +0000320
321 ++NumFileCacheMisses;
322
323 // By default, initialize it to invalid.
324 NamedFileEnt.setValue(NON_EXISTENT_FILE);
325
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000326 addAncestorsAsVirtualDirs(Filename);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700327 FileEntry *UFE = nullptr;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000328
329 // Now that all ancestors of Filename are in the cache, the
330 // following call is guaranteed to find the DirectoryEntry from the
331 // cache.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000332 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
333 /*CacheFailure=*/true);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000334 assert(DirInfo &&
335 "The directory of a virtual file should already be in the cache.");
Douglas Gregor057e5672009-12-02 18:12:28 +0000336
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000337 // Check to see if the file exists. If so, drop the virtual file
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000338 FileData Data;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000339 const char *InterndFileName = NamedFileEnt.getKeyData();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700340 if (getStatValue(InterndFileName, Data, true, nullptr) == 0) {
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000341 Data.Size = Size;
342 Data.ModTime = ModificationTime;
Stephen Hines651f13c2014-04-23 16:59:28 -0700343 UFE = &UniqueRealFiles[Data.UniqueID];
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000344
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000345 NamedFileEnt.setValue(UFE);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000346
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000347 // If we had already opened this file, close it now so we don't
348 // leak the descriptor. We're not going to use the file
349 // descriptor anyway, since this is a virtual file.
Stephen Hines651f13c2014-04-23 16:59:28 -0700350 if (UFE->File)
351 UFE->closeFile();
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000352
353 // If we already have an entry with this inode, return it.
Stephen Hines651f13c2014-04-23 16:59:28 -0700354 if (UFE->isValid())
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000355 return UFE;
Stephen Hines651f13c2014-04-23 16:59:28 -0700356
357 UFE->UniqueID = Data.UniqueID;
358 UFE->IsNamedPipe = Data.IsNamedPipe;
359 UFE->InPCH = Data.InPCH;
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000360 }
361
362 if (!UFE) {
363 UFE = new FileEntry();
364 VirtualFileEntries.push_back(UFE);
365 NamedFileEnt.setValue(UFE);
366 }
Douglas Gregor057e5672009-12-02 18:12:28 +0000367
Chris Lattnerf9f77662010-11-23 20:50:22 +0000368 UFE->Name = InterndFileName;
Douglas Gregor057e5672009-12-02 18:12:28 +0000369 UFE->Size = Size;
370 UFE->ModTime = ModificationTime;
371 UFE->Dir = DirInfo;
372 UFE->UID = NextFileUID++;
Stephen Hines651f13c2014-04-23 16:59:28 -0700373 UFE->File.reset();
Douglas Gregor057e5672009-12-02 18:12:28 +0000374 return UFE;
375}
376
Chris Lattner5f9e2722011-07-23 10:55:15 +0000377void FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
378 StringRef pathRef(path.data(), path.size());
Anders Carlssonaf036a62011-03-06 22:25:35 +0000379
Anders Carlsson2e2468e2011-03-14 01:13:54 +0000380 if (FileSystemOpts.WorkingDir.empty()
381 || llvm::sys::path::is_absolute(pathRef))
Michael J. Spencer256053b2010-12-17 21:22:22 +0000382 return;
383
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000384 SmallString<128> NewPath(FileSystemOpts.WorkingDir);
Anders Carlssonaf036a62011-03-06 22:25:35 +0000385 llvm::sys::path::append(NewPath, pathRef);
Chris Lattner67452f52010-11-23 04:45:28 +0000386 path = NewPath;
387}
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000388
Chris Lattner67452f52010-11-23 04:45:28 +0000389llvm::MemoryBuffer *FileManager::
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +0000390getBufferForFile(const FileEntry *Entry, std::string *ErrorStr,
391 bool isVolatile) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700392 std::unique_ptr<llvm::MemoryBuffer> Result;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000393 llvm::error_code ec;
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000394
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +0000395 uint64_t FileSize = Entry->getSize();
396 // If there's a high enough chance that the file have changed since we
397 // got its size, force a stat before opening it.
398 if (isVolatile)
399 FileSize = -1;
400
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000401 const char *Filename = Entry->getName();
402 // If the file is already open, use the open file descriptor.
Stephen Hines651f13c2014-04-23 16:59:28 -0700403 if (Entry->File) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700404 ec = Entry->File->getBuffer(Filename, Result, FileSize,
405 /*RequiresNullTerminator=*/true, isVolatile);
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000406 if (ErrorStr)
407 *ErrorStr = ec.message();
Stephen Hines651f13c2014-04-23 16:59:28 -0700408 Entry->closeFile();
409 return Result.release();
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000410 }
411
412 // Otherwise, open the file.
413
Chris Lattner5cc1c732010-11-23 22:32:37 +0000414 if (FileSystemOpts.WorkingDir.empty()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700415 ec = FS->getBufferForFile(Filename, Result, FileSize,
416 /*RequiresNullTerminator=*/true, isVolatile);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000417 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000418 *ErrorStr = ec.message();
Stephen Hines651f13c2014-04-23 16:59:28 -0700419 return Result.release();
Chris Lattner5cc1c732010-11-23 22:32:37 +0000420 }
Anders Carlssonaf036a62011-03-06 22:25:35 +0000421
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000422 SmallString<128> FilePath(Entry->getName());
Anders Carlsson03fd3622011-03-07 01:28:33 +0000423 FixupRelativePath(FilePath);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700424 ec = FS->getBufferForFile(FilePath.str(), Result, FileSize,
425 /*RequiresNullTerminator=*/true, isVolatile);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000426 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000427 *ErrorStr = ec.message();
Stephen Hines651f13c2014-04-23 16:59:28 -0700428 return Result.release();
Chris Lattner75dfb652010-11-23 09:19:42 +0000429}
430
431llvm::MemoryBuffer *FileManager::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000432getBufferForFile(StringRef Filename, std::string *ErrorStr) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700433 std::unique_ptr<llvm::MemoryBuffer> Result;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000434 llvm::error_code ec;
435 if (FileSystemOpts.WorkingDir.empty()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700436 ec = FS->getBufferForFile(Filename, Result);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000437 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000438 *ErrorStr = ec.message();
Stephen Hines651f13c2014-04-23 16:59:28 -0700439 return Result.release();
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000440 }
441
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000442 SmallString<128> FilePath(Filename);
Anders Carlsson03fd3622011-03-07 01:28:33 +0000443 FixupRelativePath(FilePath);
Stephen Hines651f13c2014-04-23 16:59:28 -0700444 ec = FS->getBufferForFile(FilePath.c_str(), Result);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000445 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000446 *ErrorStr = ec.message();
Stephen Hines651f13c2014-04-23 16:59:28 -0700447 return Result.release();
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000448}
449
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000450/// getStatValue - Get the 'stat' information for the specified path,
451/// using the cache to accelerate it if possible. This returns true
452/// if the path points to a virtual file or does not exist, or returns
453/// false if it's an existent real file. If FileDescriptor is NULL,
454/// do directory look-up instead of file look-up.
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000455bool FileManager::getStatValue(const char *Path, FileData &Data, bool isFile,
Stephen Hines651f13c2014-04-23 16:59:28 -0700456 vfs::File **F) {
Chris Lattner10e286a2010-11-23 19:19:34 +0000457 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
458 // absolute!
Chris Lattner11aa4b02010-11-23 19:56:39 +0000459 if (FileSystemOpts.WorkingDir.empty())
Stephen Hines651f13c2014-04-23 16:59:28 -0700460 return FileSystemStatCache::get(Path, Data, isFile, F,StatCache.get(), *FS);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000461
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000462 SmallString<128> FilePath(Path);
Anders Carlsson03fd3622011-03-07 01:28:33 +0000463 FixupRelativePath(FilePath);
Chris Lattner11aa4b02010-11-23 19:56:39 +0000464
Stephen Hines651f13c2014-04-23 16:59:28 -0700465 return FileSystemStatCache::get(FilePath.c_str(), Data, isFile, F,
466 StatCache.get(), *FS);
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000467}
468
Rafael Espindolaaefb1d32013-07-29 18:22:23 +0000469bool FileManager::getNoncachedStatValue(StringRef Path,
Stephen Hines651f13c2014-04-23 16:59:28 -0700470 vfs::Status &Result) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000471 SmallString<128> FilePath(Path);
Anders Carlsson7dbafb32011-03-18 19:23:19 +0000472 FixupRelativePath(FilePath);
473
Stephen Hines651f13c2014-04-23 16:59:28 -0700474 llvm::ErrorOr<vfs::Status> S = FS->status(FilePath.c_str());
475 if (!S)
476 return true;
477 Result = *S;
478 return false;
Anders Carlsson7dbafb32011-03-18 19:23:19 +0000479}
480
Axel Naumann5ba05592012-07-10 16:50:27 +0000481void FileManager::invalidateCache(const FileEntry *Entry) {
482 assert(Entry && "Cannot invalidate a NULL FileEntry");
Axel Naumann3ce42c32012-06-27 09:17:42 +0000483
484 SeenFileEntries.erase(Entry->getName());
Axel Naumann5ba05592012-07-10 16:50:27 +0000485
486 // FileEntry invalidation should not block future optimizations in the file
487 // caches. Possible alternatives are cache truncation (invalidate last N) or
488 // invalidation of the whole cache.
Stephen Hines651f13c2014-04-23 16:59:28 -0700489 UniqueRealFiles.erase(Entry->getUniqueID());
Axel Naumann3ce42c32012-06-27 09:17:42 +0000490}
491
492
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000493void FileManager::GetUniqueIDMapping(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000494 SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000495 UIDToFiles.clear();
496 UIDToFiles.resize(NextFileUID);
497
498 // Map file entries
499 for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000500 FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end();
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000501 FE != FEEnd; ++FE)
502 if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE)
503 UIDToFiles[FE->getValue()->getUID()] = FE->getValue();
504
505 // Map virtual file entries
Craig Topper09d19ef2013-07-04 03:08:24 +0000506 for (SmallVectorImpl<FileEntry *>::const_iterator
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000507 VFE = VirtualFileEntries.begin(), VFEEnd = VirtualFileEntries.end();
508 VFE != VFEEnd; ++VFE)
509 if (*VFE && *VFE != NON_EXISTENT_FILE)
510 UIDToFiles[(*VFE)->getUID()] = *VFE;
511}
Chris Lattner10e286a2010-11-23 19:19:34 +0000512
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +0000513void FileManager::modifyFileEntry(FileEntry *File,
514 off_t Size, time_t ModificationTime) {
515 File->Size = Size;
516 File->ModTime = ModificationTime;
517}
518
Douglas Gregor713b7c02013-01-26 00:55:12 +0000519StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
520 // FIXME: use llvm::sys::fs::canonical() when it gets implemented
521#ifdef LLVM_ON_UNIX
522 llvm::DenseMap<const DirectoryEntry *, llvm::StringRef>::iterator Known
523 = CanonicalDirNames.find(Dir);
524 if (Known != CanonicalDirNames.end())
525 return Known->second;
526
527 StringRef CanonicalName(Dir->getName());
528 char CanonicalNameBuf[PATH_MAX];
529 if (realpath(Dir->getName(), CanonicalNameBuf)) {
530 unsigned Len = strlen(CanonicalNameBuf);
531 char *Mem = static_cast<char *>(CanonicalNameStorage.Allocate(Len, 1));
532 memcpy(Mem, CanonicalNameBuf, Len);
533 CanonicalName = StringRef(Mem, Len);
534 }
535
536 CanonicalDirNames.insert(std::make_pair(Dir, CanonicalName));
537 return CanonicalName;
538#else
539 return StringRef(Dir->getName());
540#endif
541}
Chris Lattner10e286a2010-11-23 19:19:34 +0000542
Reid Spencer5f016e22007-07-11 17:01:13 +0000543void FileManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000544 llvm::errs() << "\n*** File Manager Stats:\n";
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000545 llvm::errs() << UniqueRealFiles.size() << " real files found, "
546 << UniqueRealDirs.size() << " real dirs found.\n";
547 llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
548 << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000549 llvm::errs() << NumDirLookups << " dir lookups, "
550 << NumDirCacheMisses << " dir cache misses.\n";
551 llvm::errs() << NumFileLookups << " file lookups, "
552 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000554 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000555}