1 //===--- FileManager.cpp - File System Probing and Caching ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file implements the FileManager interface.
10 //
11 //===----------------------------------------------------------------------===//
12 //
13 // TODO: This should index all interesting directories with dirent calls.
14 //  getdirentries ?
15 //  opendir/readdir_r/closedir ?
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #include "clang/Basic/FileManager.h"
20 #include "clang/Basic/FileSystemStatCache.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Config/llvm-config.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include <algorithm>
30 #include <cassert>
31 #include <climits>
32 #include <cstdint>
33 #include <cstdlib>
34 #include <string>
35 #include <utility>
36 
37 using namespace clang;
38 
39 #define DEBUG_TYPE "file-search"
40 
41 ALWAYS_ENABLED_STATISTIC(NumDirLookups, "Number of directory lookups.");
42 ALWAYS_ENABLED_STATISTIC(NumFileLookups, "Number of file lookups.");
43 ALWAYS_ENABLED_STATISTIC(NumDirCacheMisses,
44                          "Number of directory cache misses.");
45 ALWAYS_ENABLED_STATISTIC(NumFileCacheMisses, "Number of file cache misses.");
46 
47 //===----------------------------------------------------------------------===//
48 // Common logic.
49 //===----------------------------------------------------------------------===//
50 
51 FileManager::FileManager(const FileSystemOptions &FSO,
52                          IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
53     : FS(std::move(FS)), FileSystemOpts(FSO), SeenDirEntries(64),
54       SeenFileEntries(64), NextFileUID(0) {
55   // If the caller doesn't provide a virtual file system, just grab the real
56   // file system.
57   if (!this->FS)
58     this->FS = llvm::vfs::getRealFileSystem();
59 }
60 
61 FileManager::~FileManager() = default;
62 
63 void FileManager::setStatCache(std::unique_ptr<FileSystemStatCache> statCache) {
64   assert(statCache && "No stat cache provided?");
65   StatCache = std::move(statCache);
66 }
67 
68 void FileManager::clearStatCache() { StatCache.reset(); }
69 
70 /// Retrieve the directory that the given file name resides in.
71 /// Filename can point to either a real file or a virtual file.
72 static llvm::Expected<DirectoryEntryRef>
73 getDirectoryFromFile(FileManager &FileMgr, StringRef Filename,
74                      bool CacheFailure) {
75   if (Filename.empty())
76     return llvm::errorCodeToError(
77         make_error_code(std::errc::no_such_file_or_directory));
78 
79   if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
80     return llvm::errorCodeToError(make_error_code(std::errc::is_a_directory));
81 
82   StringRef DirName = llvm::sys::path::parent_path(Filename);
83   // Use the current directory if file has no path component.
84   if (DirName.empty())
85     DirName = ".";
86 
87   return FileMgr.getDirectoryRef(DirName, CacheFailure);
88 }
89 
90 /// Add all ancestors of the given path (pointing to either a file or
91 /// a directory) as virtual directories.
92 void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
93   StringRef DirName = llvm::sys::path::parent_path(Path);
94   if (DirName.empty())
95     DirName = ".";
96 
97   auto &NamedDirEnt = *SeenDirEntries.insert(
98         {DirName, std::errc::no_such_file_or_directory}).first;
99 
100   // When caching a virtual directory, we always cache its ancestors
101   // at the same time.  Therefore, if DirName is already in the cache,
102   // we don't need to recurse as its ancestors must also already be in
103   // the cache (or it's a known non-virtual directory).
104   if (NamedDirEnt.second)
105     return;
106 
107   // Add the virtual directory to the cache.
108   auto *UDE = new (DirsAlloc.Allocate()) DirectoryEntry();
109   UDE->Name = NamedDirEnt.first();
110   NamedDirEnt.second = *UDE;
111   VirtualDirectoryEntries.push_back(UDE);
112 
113   // Recursively add the other ancestors.
114   addAncestorsAsVirtualDirs(DirName);
115 }
116 
117 llvm::Expected<DirectoryEntryRef>
118 FileManager::getDirectoryRef(StringRef DirName, bool CacheFailure) {
119   // stat doesn't like trailing separators except for root directory.
120   // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
121   // (though it can strip '\\')
122   if (DirName.size() > 1 &&
123       DirName != llvm::sys::path::root_path(DirName) &&
124       llvm::sys::path::is_separator(DirName.back()))
125     DirName = DirName.substr(0, DirName.size()-1);
126   Optional<std::string> DirNameStr;
127   if (is_style_windows(llvm::sys::path::Style::native)) {
128     // Fixing a problem with "clang C:test.c" on Windows.
129     // Stat("C:") does not recognize "C:" as a valid directory
130     if (DirName.size() > 1 && DirName.back() == ':' &&
131         DirName.equals_insensitive(llvm::sys::path::root_name(DirName))) {
132       DirNameStr = DirName.str() + '.';
133       DirName = *DirNameStr;
134     }
135   }
136 
137   ++NumDirLookups;
138 
139   // See if there was already an entry in the map.  Note that the map
140   // contains both virtual and real directories.
141   auto SeenDirInsertResult =
142       SeenDirEntries.insert({DirName, std::errc::no_such_file_or_directory});
143   if (!SeenDirInsertResult.second) {
144     if (SeenDirInsertResult.first->second)
145       return DirectoryEntryRef(*SeenDirInsertResult.first);
146     return llvm::errorCodeToError(SeenDirInsertResult.first->second.getError());
147   }
148 
149   // We've not seen this before. Fill it in.
150   ++NumDirCacheMisses;
151   auto &NamedDirEnt = *SeenDirInsertResult.first;
152   assert(!NamedDirEnt.second && "should be newly-created");
153 
154   // Get the null-terminated directory name as stored as the key of the
155   // SeenDirEntries map.
156   StringRef InterndDirName = NamedDirEnt.first();
157 
158   // Check to see if the directory exists.
159   llvm::vfs::Status Status;
160   auto statError = getStatValue(InterndDirName, Status, false,
161                                 nullptr /*directory lookup*/);
162   if (statError) {
163     // There's no real directory at the given path.
164     if (CacheFailure)
165       NamedDirEnt.second = statError;
166     else
167       SeenDirEntries.erase(DirName);
168     return llvm::errorCodeToError(statError);
169   }
170 
171   // It exists.  See if we have already opened a directory with the
172   // same inode (this occurs on Unix-like systems when one dir is
173   // symlinked to another, for example) or the same path (on
174   // Windows).
175   DirectoryEntry *&UDE = UniqueRealDirs[Status.getUniqueID()];
176 
177   if (!UDE) {
178     // We don't have this directory yet, add it.  We use the string
179     // key from the SeenDirEntries map as the string.
180     UDE = new (DirsAlloc.Allocate()) DirectoryEntry();
181     UDE->Name = InterndDirName;
182   }
183   NamedDirEnt.second = *UDE;
184 
185   return DirectoryEntryRef(NamedDirEnt);
186 }
187 
188 llvm::ErrorOr<const DirectoryEntry *>
189 FileManager::getDirectory(StringRef DirName, bool CacheFailure) {
190   auto Result = getDirectoryRef(DirName, CacheFailure);
191   if (Result)
192     return &Result->getDirEntry();
193   return llvm::errorToErrorCode(Result.takeError());
194 }
195 
196 llvm::ErrorOr<const FileEntry *>
197 FileManager::getFile(StringRef Filename, bool openFile, bool CacheFailure) {
198   auto Result = getFileRef(Filename, openFile, CacheFailure);
199   if (Result)
200     return &Result->getFileEntry();
201   return llvm::errorToErrorCode(Result.takeError());
202 }
203 
204 llvm::Expected<FileEntryRef>
205 FileManager::getFileRef(StringRef Filename, bool openFile, bool CacheFailure) {
206   ++NumFileLookups;
207 
208   // See if there is already an entry in the map.
209   auto SeenFileInsertResult =
210       SeenFileEntries.insert({Filename, std::errc::no_such_file_or_directory});
211   if (!SeenFileInsertResult.second) {
212     if (!SeenFileInsertResult.first->second)
213       return llvm::errorCodeToError(
214           SeenFileInsertResult.first->second.getError());
215     // Construct and return and FileEntryRef, unless it's a redirect to another
216     // filename.
217     FileEntryRef::MapValue Value = *SeenFileInsertResult.first->second;
218     if (LLVM_LIKELY(Value.V.is<FileEntry *>()))
219       return FileEntryRef(*SeenFileInsertResult.first);
220     return FileEntryRef(*reinterpret_cast<const FileEntryRef::MapEntry *>(
221         Value.V.get<const void *>()));
222   }
223 
224   // We've not seen this before. Fill it in.
225   ++NumFileCacheMisses;
226   auto *NamedFileEnt = &*SeenFileInsertResult.first;
227   assert(!NamedFileEnt->second && "should be newly-created");
228 
229   // Get the null-terminated file name as stored as the key of the
230   // SeenFileEntries map.
231   StringRef InterndFileName = NamedFileEnt->first();
232 
233   // Look up the directory for the file.  When looking up something like
234   // sys/foo.h we'll discover all of the search directories that have a 'sys'
235   // subdirectory.  This will let us avoid having to waste time on known-to-fail
236   // searches when we go to find sys/bar.h, because all the search directories
237   // without a 'sys' subdir will get a cached failure result.
238   auto DirInfoOrErr = getDirectoryFromFile(*this, Filename, CacheFailure);
239   if (!DirInfoOrErr) { // Directory doesn't exist, file can't exist.
240     std::error_code Err = errorToErrorCode(DirInfoOrErr.takeError());
241     if (CacheFailure)
242       NamedFileEnt->second = Err;
243     else
244       SeenFileEntries.erase(Filename);
245 
246     return llvm::errorCodeToError(Err);
247   }
248   DirectoryEntryRef DirInfo = *DirInfoOrErr;
249 
250   // FIXME: Use the directory info to prune this, before doing the stat syscall.
251   // FIXME: This will reduce the # syscalls.
252 
253   // Check to see if the file exists.
254   std::unique_ptr<llvm::vfs::File> F;
255   llvm::vfs::Status Status;
256   auto statError = getStatValue(InterndFileName, Status, true,
257                                 openFile ? &F : nullptr);
258   if (statError) {
259     // There's no real file at the given path.
260     if (CacheFailure)
261       NamedFileEnt->second = statError;
262     else
263       SeenFileEntries.erase(Filename);
264 
265     return llvm::errorCodeToError(statError);
266   }
267 
268   assert((openFile || !F) && "undesired open file");
269 
270   // It exists.  See if we have already opened a file with the same inode.
271   // This occurs when one dir is symlinked to another, for example.
272   FileEntry *&UFE = UniqueRealFiles[Status.getUniqueID()];
273   bool ReusingEntry = UFE != nullptr;
274   if (!UFE)
275     UFE = new (FilesAlloc.Allocate()) FileEntry();
276 
277   if (Status.getName() == Filename) {
278     // The name matches. Set the FileEntry.
279     NamedFileEnt->second = FileEntryRef::MapValue(*UFE, DirInfo);
280   } else {
281     // Name mismatch. We need a redirect. First grab the actual entry we want
282     // to return.
283     //
284     // This redirection logic intentionally leaks the external name of a
285     // redirected file that uses 'use-external-name' in \a
286     // vfs::RedirectionFileSystem. This allows clang to report the external
287     // name to users (in diagnostics) and to tools that don't have access to
288     // the VFS (in debug info and dependency '.d' files).
289     //
290     // FIXME: This is pretty complex and has some very complicated interactions
291     // with the rest of clang. It's also inconsistent with how "real"
292     // filesystems behave and confuses parts of clang expect to see the
293     // name-as-accessed on the \a FileEntryRef.
294     //
295     // Further, it isn't *just* external names, but will also give back absolute
296     // paths when a relative path was requested - the check is comparing the
297     // name from the status, which is passed an absolute path resolved from the
298     // current working directory. `clang-apply-replacements` appears to depend
299     // on this behaviour, though it's adjusting the working directory, which is
300     // definitely not supported. Once that's fixed this hack should be able to
301     // be narrowed to only when there's an externally mapped name given back.
302     //
303     // A potential plan to remove this is as follows -
304     //   - Add API to determine if the name has been rewritten by the VFS.
305     //   - Fix `clang-apply-replacements` to pass down the absolute path rather
306     //     than changing the CWD. Narrow this hack down to just externally
307     //     mapped paths.
308     //   - Expose the requested filename. One possibility would be to allow
309     //     redirection-FileEntryRefs to be returned, rather than returning
310     //     the pointed-at-FileEntryRef, and customizing `getName()` to look
311     //     through the indirection.
312     //   - Update callers such as `HeaderSearch::findUsableModuleForHeader()`
313     //     to explicitly use the requested filename rather than just using
314     //     `getName()`.
315     //   - Add a `FileManager::getExternalPath` API for explicitly getting the
316     //     remapped external filename when there is one available. Adopt it in
317     //     callers like diagnostics/deps reporting instead of calling
318     //     `getName()` directly.
319     //   - Switch the meaning of `FileEntryRef::getName()` to get the requested
320     //     name, not the external name. Once that sticks, revert callers that
321     //     want the requested name back to calling `getName()`.
322     //   - Update the VFS to always return the requested name. This could also
323     //     return the external name, or just have an API to request it
324     //     lazily. The latter has the benefit of making accesses of the
325     //     external path easily tracked, but may also require extra work than
326     //     just returning up front.
327     //   - (Optionally) Add an API to VFS to get the external filename lazily
328     //     and update `FileManager::getExternalPath()` to use it instead. This
329     //     has the benefit of making such accesses easily tracked, though isn't
330     //     necessarily required (and could cause extra work than just adding to
331     //     eg. `vfs::Status` up front).
332     auto &Redirection =
333         *SeenFileEntries
334              .insert({Status.getName(), FileEntryRef::MapValue(*UFE, DirInfo)})
335              .first;
336     assert(Redirection.second->V.is<FileEntry *>() &&
337            "filename redirected to a non-canonical filename?");
338     assert(Redirection.second->V.get<FileEntry *>() == UFE &&
339            "filename from getStatValue() refers to wrong file");
340 
341     // Cache the redirection in the previously-inserted entry, still available
342     // in the tentative return value.
343     NamedFileEnt->second = FileEntryRef::MapValue(Redirection);
344 
345     // Fix the tentative return value.
346     NamedFileEnt = &Redirection;
347   }
348 
349   FileEntryRef ReturnedRef(*NamedFileEnt);
350   if (ReusingEntry) { // Already have an entry with this inode, return it.
351 
352     // FIXME: This hack ensures that `getDir()` will use the path that was
353     // used to lookup this file, even if we found a file by different path
354     // first. This is required in order to find a module's structure when its
355     // headers/module map are mapped in the VFS.
356     //
357     // See above for how this will eventually be removed. `IsVFSMapped`
358     // *cannot* be narrowed to `ExposesExternalVFSPath` as crash reproducers
359     // also depend on this logic and they have `use-external-paths: false`.
360     if (&DirInfo.getDirEntry() != UFE->Dir && Status.IsVFSMapped)
361       UFE->Dir = &DirInfo.getDirEntry();
362 
363     // Always update LastRef to the last name by which a file was accessed.
364     // FIXME: Neither this nor always using the first reference is correct; we
365     // want to switch towards a design where we return a FileName object that
366     // encapsulates both the name by which the file was accessed and the
367     // corresponding FileEntry.
368     // FIXME: LastRef should be removed from FileEntry once all clients adopt
369     // FileEntryRef.
370     UFE->LastRef = ReturnedRef;
371 
372     return ReturnedRef;
373   }
374 
375   // Otherwise, we don't have this file yet, add it.
376   UFE->LastRef = ReturnedRef;
377   UFE->Size = Status.getSize();
378   UFE->ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
379   UFE->Dir = &DirInfo.getDirEntry();
380   UFE->UID = NextFileUID++;
381   UFE->UniqueID = Status.getUniqueID();
382   UFE->IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
383   UFE->File = std::move(F);
384 
385   if (UFE->File) {
386     if (auto PathName = UFE->File->getName())
387       fillRealPathName(UFE, *PathName);
388   } else if (!openFile) {
389     // We should still fill the path even if we aren't opening the file.
390     fillRealPathName(UFE, InterndFileName);
391   }
392   return ReturnedRef;
393 }
394 
395 llvm::Expected<FileEntryRef> FileManager::getSTDIN() {
396   // Only read stdin once.
397   if (STDIN)
398     return *STDIN;
399 
400   std::unique_ptr<llvm::MemoryBuffer> Content;
401   if (auto ContentOrError = llvm::MemoryBuffer::getSTDIN())
402     Content = std::move(*ContentOrError);
403   else
404     return llvm::errorCodeToError(ContentOrError.getError());
405 
406   STDIN = getVirtualFileRef(Content->getBufferIdentifier(),
407                             Content->getBufferSize(), 0);
408   FileEntry &FE = const_cast<FileEntry &>(STDIN->getFileEntry());
409   FE.Content = std::move(Content);
410   FE.IsNamedPipe = true;
411   return *STDIN;
412 }
413 
414 const FileEntry *FileManager::getVirtualFile(StringRef Filename, off_t Size,
415                                              time_t ModificationTime) {
416   return &getVirtualFileRef(Filename, Size, ModificationTime).getFileEntry();
417 }
418 
419 FileEntryRef FileManager::getVirtualFileRef(StringRef Filename, off_t Size,
420                                             time_t ModificationTime) {
421   ++NumFileLookups;
422 
423   // See if there is already an entry in the map for an existing file.
424   auto &NamedFileEnt = *SeenFileEntries.insert(
425       {Filename, std::errc::no_such_file_or_directory}).first;
426   if (NamedFileEnt.second) {
427     FileEntryRef::MapValue Value = *NamedFileEnt.second;
428     if (LLVM_LIKELY(Value.V.is<FileEntry *>()))
429       return FileEntryRef(NamedFileEnt);
430     return FileEntryRef(*reinterpret_cast<const FileEntryRef::MapEntry *>(
431         Value.V.get<const void *>()));
432   }
433 
434   // We've not seen this before, or the file is cached as non-existent.
435   ++NumFileCacheMisses;
436   addAncestorsAsVirtualDirs(Filename);
437   FileEntry *UFE = nullptr;
438 
439   // Now that all ancestors of Filename are in the cache, the
440   // following call is guaranteed to find the DirectoryEntry from the
441   // cache. A virtual file can also have an empty filename, that could come
442   // from a source location preprocessor directive with an empty filename as
443   // an example, so we need to pretend it has a name to ensure a valid directory
444   // entry can be returned.
445   auto DirInfo = expectedToOptional(getDirectoryFromFile(
446       *this, Filename.empty() ? "." : Filename, /*CacheFailure=*/true));
447   assert(DirInfo &&
448          "The directory of a virtual file should already be in the cache.");
449 
450   // Check to see if the file exists. If so, drop the virtual file
451   llvm::vfs::Status Status;
452   const char *InterndFileName = NamedFileEnt.first().data();
453   if (!getStatValue(InterndFileName, Status, true, nullptr)) {
454     Status = llvm::vfs::Status(
455       Status.getName(), Status.getUniqueID(),
456       llvm::sys::toTimePoint(ModificationTime),
457       Status.getUser(), Status.getGroup(), Size,
458       Status.getType(), Status.getPermissions());
459 
460     auto &RealFE = UniqueRealFiles[Status.getUniqueID()];
461     if (RealFE) {
462       // If we had already opened this file, close it now so we don't
463       // leak the descriptor. We're not going to use the file
464       // descriptor anyway, since this is a virtual file.
465       if (RealFE->File)
466         RealFE->closeFile();
467       // If we already have an entry with this inode, return it.
468       //
469       // FIXME: Surely this should add a reference by the new name, and return
470       // it instead...
471       NamedFileEnt.second = FileEntryRef::MapValue(*RealFE, *DirInfo);
472       return FileEntryRef(NamedFileEnt);
473     }
474     // File exists, but no entry - create it.
475     RealFE = new (FilesAlloc.Allocate()) FileEntry();
476     RealFE->UniqueID = Status.getUniqueID();
477     RealFE->IsNamedPipe =
478         Status.getType() == llvm::sys::fs::file_type::fifo_file;
479     fillRealPathName(RealFE, Status.getName());
480 
481     UFE = RealFE;
482   } else {
483     // File does not exist, create a virtual entry.
484     UFE = new (FilesAlloc.Allocate()) FileEntry();
485     VirtualFileEntries.push_back(UFE);
486   }
487 
488   NamedFileEnt.second = FileEntryRef::MapValue(*UFE, *DirInfo);
489   UFE->LastRef = FileEntryRef(NamedFileEnt);
490   UFE->Size    = Size;
491   UFE->ModTime = ModificationTime;
492   UFE->Dir     = &DirInfo->getDirEntry();
493   UFE->UID     = NextFileUID++;
494   UFE->File.reset();
495   return FileEntryRef(NamedFileEnt);
496 }
497 
498 llvm::Optional<FileEntryRef> FileManager::getBypassFile(FileEntryRef VF) {
499   // Stat of the file and return nullptr if it doesn't exist.
500   llvm::vfs::Status Status;
501   if (getStatValue(VF.getName(), Status, /*isFile=*/true, /*F=*/nullptr))
502     return None;
503 
504   if (!SeenBypassFileEntries)
505     SeenBypassFileEntries = std::make_unique<
506         llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>>>();
507 
508   // If we've already bypassed just use the existing one.
509   auto Insertion = SeenBypassFileEntries->insert(
510       {VF.getName(), std::errc::no_such_file_or_directory});
511   if (!Insertion.second)
512     return FileEntryRef(*Insertion.first);
513 
514   // Fill in the new entry from the stat.
515   FileEntry *BFE = new (FilesAlloc.Allocate()) FileEntry();
516   BypassFileEntries.push_back(BFE);
517   Insertion.first->second = FileEntryRef::MapValue(*BFE, VF.getDir());
518   BFE->LastRef = FileEntryRef(*Insertion.first);
519   BFE->Size = Status.getSize();
520   BFE->Dir = VF.getFileEntry().Dir;
521   BFE->ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
522   BFE->UID = NextFileUID++;
523 
524   // Save the entry in the bypass table and return.
525   return FileEntryRef(*Insertion.first);
526 }
527 
528 bool FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
529   StringRef pathRef(path.data(), path.size());
530 
531   if (FileSystemOpts.WorkingDir.empty()
532       || llvm::sys::path::is_absolute(pathRef))
533     return false;
534 
535   SmallString<128> NewPath(FileSystemOpts.WorkingDir);
536   llvm::sys::path::append(NewPath, pathRef);
537   path = NewPath;
538   return true;
539 }
540 
541 bool FileManager::makeAbsolutePath(SmallVectorImpl<char> &Path) const {
542   bool Changed = FixupRelativePath(Path);
543 
544   if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
545     FS->makeAbsolute(Path);
546     Changed = true;
547   }
548 
549   return Changed;
550 }
551 
552 void FileManager::fillRealPathName(FileEntry *UFE, llvm::StringRef FileName) {
553   llvm::SmallString<128> AbsPath(FileName);
554   // This is not the same as `VFS::getRealPath()`, which resolves symlinks
555   // but can be very expensive on real file systems.
556   // FIXME: the semantic of RealPathName is unclear, and the name might be
557   // misleading. We need to clean up the interface here.
558   makeAbsolutePath(AbsPath);
559   llvm::sys::path::remove_dots(AbsPath, /*remove_dot_dot=*/true);
560   UFE->RealPathName = std::string(AbsPath.str());
561 }
562 
563 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
564 FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile,
565                               bool RequiresNullTerminator) {
566   // If the content is living on the file entry, return a reference to it.
567   if (Entry->Content)
568     return llvm::MemoryBuffer::getMemBuffer(Entry->Content->getMemBufferRef());
569 
570   uint64_t FileSize = Entry->getSize();
571   // If there's a high enough chance that the file have changed since we
572   // got its size, force a stat before opening it.
573   if (isVolatile || Entry->isNamedPipe())
574     FileSize = -1;
575 
576   StringRef Filename = Entry->getName();
577   // If the file is already open, use the open file descriptor.
578   if (Entry->File) {
579     auto Result = Entry->File->getBuffer(Filename, FileSize,
580                                          RequiresNullTerminator, isVolatile);
581     Entry->closeFile();
582     return Result;
583   }
584 
585   // Otherwise, open the file.
586   return getBufferForFileImpl(Filename, FileSize, isVolatile,
587                               RequiresNullTerminator);
588 }
589 
590 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
591 FileManager::getBufferForFileImpl(StringRef Filename, int64_t FileSize,
592                                   bool isVolatile,
593                                   bool RequiresNullTerminator) {
594   if (FileSystemOpts.WorkingDir.empty())
595     return FS->getBufferForFile(Filename, FileSize, RequiresNullTerminator,
596                                 isVolatile);
597 
598   SmallString<128> FilePath(Filename);
599   FixupRelativePath(FilePath);
600   return FS->getBufferForFile(FilePath, FileSize, RequiresNullTerminator,
601                               isVolatile);
602 }
603 
604 /// getStatValue - Get the 'stat' information for the specified path,
605 /// using the cache to accelerate it if possible.  This returns true
606 /// if the path points to a virtual file or does not exist, or returns
607 /// false if it's an existent real file.  If FileDescriptor is NULL,
608 /// do directory look-up instead of file look-up.
609 std::error_code
610 FileManager::getStatValue(StringRef Path, llvm::vfs::Status &Status,
611                           bool isFile, std::unique_ptr<llvm::vfs::File> *F) {
612   // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
613   // absolute!
614   if (FileSystemOpts.WorkingDir.empty())
615     return FileSystemStatCache::get(Path, Status, isFile, F,
616                                     StatCache.get(), *FS);
617 
618   SmallString<128> FilePath(Path);
619   FixupRelativePath(FilePath);
620 
621   return FileSystemStatCache::get(FilePath.c_str(), Status, isFile, F,
622                                   StatCache.get(), *FS);
623 }
624 
625 std::error_code
626 FileManager::getNoncachedStatValue(StringRef Path,
627                                    llvm::vfs::Status &Result) {
628   SmallString<128> FilePath(Path);
629   FixupRelativePath(FilePath);
630 
631   llvm::ErrorOr<llvm::vfs::Status> S = FS->status(FilePath.c_str());
632   if (!S)
633     return S.getError();
634   Result = *S;
635   return std::error_code();
636 }
637 
638 void FileManager::GetUniqueIDMapping(
639     SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
640   UIDToFiles.clear();
641   UIDToFiles.resize(NextFileUID);
642 
643   // Map file entries
644   for (llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>,
645                        llvm::BumpPtrAllocator>::const_iterator
646            FE = SeenFileEntries.begin(),
647            FEEnd = SeenFileEntries.end();
648        FE != FEEnd; ++FE)
649     if (llvm::ErrorOr<FileEntryRef::MapValue> Entry = FE->getValue()) {
650       if (const auto *FE = Entry->V.dyn_cast<FileEntry *>())
651         UIDToFiles[FE->getUID()] = FE;
652     }
653 
654   // Map virtual file entries
655   for (const auto &VFE : VirtualFileEntries)
656     UIDToFiles[VFE->getUID()] = VFE;
657 }
658 
659 StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
660   llvm::DenseMap<const void *, llvm::StringRef>::iterator Known
661     = CanonicalNames.find(Dir);
662   if (Known != CanonicalNames.end())
663     return Known->second;
664 
665   StringRef CanonicalName(Dir->getName());
666 
667   SmallString<4096> CanonicalNameBuf;
668   if (!FS->getRealPath(Dir->getName(), CanonicalNameBuf))
669     CanonicalName = CanonicalNameBuf.str().copy(CanonicalNameStorage);
670 
671   CanonicalNames.insert({Dir, CanonicalName});
672   return CanonicalName;
673 }
674 
675 StringRef FileManager::getCanonicalName(const FileEntry *File) {
676   llvm::DenseMap<const void *, llvm::StringRef>::iterator Known
677     = CanonicalNames.find(File);
678   if (Known != CanonicalNames.end())
679     return Known->second;
680 
681   StringRef CanonicalName(File->getName());
682 
683   SmallString<4096> CanonicalNameBuf;
684   if (!FS->getRealPath(File->getName(), CanonicalNameBuf))
685     CanonicalName = CanonicalNameBuf.str().copy(CanonicalNameStorage);
686 
687   CanonicalNames.insert({File, CanonicalName});
688   return CanonicalName;
689 }
690 
691 void FileManager::PrintStats() const {
692   llvm::errs() << "\n*** File Manager Stats:\n";
693   llvm::errs() << UniqueRealFiles.size() << " real files found, "
694                << UniqueRealDirs.size() << " real dirs found.\n";
695   llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
696                << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
697   llvm::errs() << NumDirLookups << " dir lookups, "
698                << NumDirCacheMisses << " dir cache misses.\n";
699   llvm::errs() << NumFileLookups << " file lookups, "
700                << NumFileCacheMisses << " file cache misses.\n";
701 
702   //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
703 }
704