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