xref: /aosp_15_r20/external/clang/lib/Basic/FileManager.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li //===--- FileManager.cpp - File System Probing and Caching ----------------===//
2*67e74705SXin Li //
3*67e74705SXin Li //                     The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li //  This file implements the FileManager interface.
11*67e74705SXin Li //
12*67e74705SXin Li //===----------------------------------------------------------------------===//
13*67e74705SXin Li //
14*67e74705SXin Li // TODO: This should index all interesting directories with dirent calls.
15*67e74705SXin Li //  getdirentries ?
16*67e74705SXin Li //  opendir/readdir_r/closedir ?
17*67e74705SXin Li //
18*67e74705SXin Li //===----------------------------------------------------------------------===//
19*67e74705SXin Li 
20*67e74705SXin Li #include "clang/Basic/FileManager.h"
21*67e74705SXin Li #include "clang/Basic/FileSystemStatCache.h"
22*67e74705SXin Li #include "llvm/ADT/SmallString.h"
23*67e74705SXin Li #include "llvm/Config/llvm-config.h"
24*67e74705SXin Li #include "llvm/ADT/STLExtras.h"
25*67e74705SXin Li #include "llvm/Support/FileSystem.h"
26*67e74705SXin Li #include "llvm/Support/MemoryBuffer.h"
27*67e74705SXin Li #include "llvm/Support/Path.h"
28*67e74705SXin Li #include "llvm/Support/raw_ostream.h"
29*67e74705SXin Li #include <map>
30*67e74705SXin Li #include <set>
31*67e74705SXin Li #include <string>
32*67e74705SXin Li #include <system_error>
33*67e74705SXin Li 
34*67e74705SXin Li using namespace clang;
35*67e74705SXin Li 
36*67e74705SXin Li /// NON_EXISTENT_DIR - A special value distinct from null that is used to
37*67e74705SXin Li /// represent a dir name that doesn't exist on the disk.
38*67e74705SXin Li #define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
39*67e74705SXin Li 
40*67e74705SXin Li /// NON_EXISTENT_FILE - A special value distinct from null that is used to
41*67e74705SXin Li /// represent a filename that doesn't exist on the disk.
42*67e74705SXin Li #define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
43*67e74705SXin Li 
44*67e74705SXin Li //===----------------------------------------------------------------------===//
45*67e74705SXin Li // Common logic.
46*67e74705SXin Li //===----------------------------------------------------------------------===//
47*67e74705SXin Li 
FileManager(const FileSystemOptions & FSO,IntrusiveRefCntPtr<vfs::FileSystem> FS)48*67e74705SXin Li FileManager::FileManager(const FileSystemOptions &FSO,
49*67e74705SXin Li                          IntrusiveRefCntPtr<vfs::FileSystem> FS)
50*67e74705SXin Li   : FS(FS), FileSystemOpts(FSO),
51*67e74705SXin Li     SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) {
52*67e74705SXin Li   NumDirLookups = NumFileLookups = 0;
53*67e74705SXin Li   NumDirCacheMisses = NumFileCacheMisses = 0;
54*67e74705SXin Li 
55*67e74705SXin Li   // If the caller doesn't provide a virtual file system, just grab the real
56*67e74705SXin Li   // file system.
57*67e74705SXin Li   if (!FS)
58*67e74705SXin Li     this->FS = vfs::getRealFileSystem();
59*67e74705SXin Li }
60*67e74705SXin Li 
61*67e74705SXin Li FileManager::~FileManager() = default;
62*67e74705SXin Li 
addStatCache(std::unique_ptr<FileSystemStatCache> statCache,bool AtBeginning)63*67e74705SXin Li void FileManager::addStatCache(std::unique_ptr<FileSystemStatCache> statCache,
64*67e74705SXin Li                                bool AtBeginning) {
65*67e74705SXin Li   assert(statCache && "No stat cache provided?");
66*67e74705SXin Li   if (AtBeginning || !StatCache.get()) {
67*67e74705SXin Li     statCache->setNextStatCache(std::move(StatCache));
68*67e74705SXin Li     StatCache = std::move(statCache);
69*67e74705SXin Li     return;
70*67e74705SXin Li   }
71*67e74705SXin Li 
72*67e74705SXin Li   FileSystemStatCache *LastCache = StatCache.get();
73*67e74705SXin Li   while (LastCache->getNextStatCache())
74*67e74705SXin Li     LastCache = LastCache->getNextStatCache();
75*67e74705SXin Li 
76*67e74705SXin Li   LastCache->setNextStatCache(std::move(statCache));
77*67e74705SXin Li }
78*67e74705SXin Li 
removeStatCache(FileSystemStatCache * statCache)79*67e74705SXin Li void FileManager::removeStatCache(FileSystemStatCache *statCache) {
80*67e74705SXin Li   if (!statCache)
81*67e74705SXin Li     return;
82*67e74705SXin Li 
83*67e74705SXin Li   if (StatCache.get() == statCache) {
84*67e74705SXin Li     // This is the first stat cache.
85*67e74705SXin Li     StatCache = StatCache->takeNextStatCache();
86*67e74705SXin Li     return;
87*67e74705SXin Li   }
88*67e74705SXin Li 
89*67e74705SXin Li   // Find the stat cache in the list.
90*67e74705SXin Li   FileSystemStatCache *PrevCache = StatCache.get();
91*67e74705SXin Li   while (PrevCache && PrevCache->getNextStatCache() != statCache)
92*67e74705SXin Li     PrevCache = PrevCache->getNextStatCache();
93*67e74705SXin Li 
94*67e74705SXin Li   assert(PrevCache && "Stat cache not found for removal");
95*67e74705SXin Li   PrevCache->setNextStatCache(statCache->takeNextStatCache());
96*67e74705SXin Li }
97*67e74705SXin Li 
clearStatCaches()98*67e74705SXin Li void FileManager::clearStatCaches() {
99*67e74705SXin Li   StatCache.reset();
100*67e74705SXin Li }
101*67e74705SXin Li 
102*67e74705SXin Li /// \brief Retrieve the directory that the given file name resides in.
103*67e74705SXin Li /// Filename can point to either a real file or a virtual file.
getDirectoryFromFile(FileManager & FileMgr,StringRef Filename,bool CacheFailure)104*67e74705SXin Li static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
105*67e74705SXin Li                                                   StringRef Filename,
106*67e74705SXin Li                                                   bool CacheFailure) {
107*67e74705SXin Li   if (Filename.empty())
108*67e74705SXin Li     return nullptr;
109*67e74705SXin Li 
110*67e74705SXin Li   if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
111*67e74705SXin Li     return nullptr; // If Filename is a directory.
112*67e74705SXin Li 
113*67e74705SXin Li   StringRef DirName = llvm::sys::path::parent_path(Filename);
114*67e74705SXin Li   // Use the current directory if file has no path component.
115*67e74705SXin Li   if (DirName.empty())
116*67e74705SXin Li     DirName = ".";
117*67e74705SXin Li 
118*67e74705SXin Li   return FileMgr.getDirectory(DirName, CacheFailure);
119*67e74705SXin Li }
120*67e74705SXin Li 
121*67e74705SXin Li /// Add all ancestors of the given path (pointing to either a file or
122*67e74705SXin Li /// a directory) as virtual directories.
addAncestorsAsVirtualDirs(StringRef Path)123*67e74705SXin Li void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
124*67e74705SXin Li   StringRef DirName = llvm::sys::path::parent_path(Path);
125*67e74705SXin Li   if (DirName.empty())
126*67e74705SXin Li     DirName = ".";
127*67e74705SXin Li 
128*67e74705SXin Li   auto &NamedDirEnt =
129*67e74705SXin Li       *SeenDirEntries.insert(std::make_pair(DirName, nullptr)).first;
130*67e74705SXin Li 
131*67e74705SXin Li   // When caching a virtual directory, we always cache its ancestors
132*67e74705SXin Li   // at the same time.  Therefore, if DirName is already in the cache,
133*67e74705SXin Li   // we don't need to recurse as its ancestors must also already be in
134*67e74705SXin Li   // the cache.
135*67e74705SXin Li   if (NamedDirEnt.second && NamedDirEnt.second != NON_EXISTENT_DIR)
136*67e74705SXin Li     return;
137*67e74705SXin Li 
138*67e74705SXin Li   // Add the virtual directory to the cache.
139*67e74705SXin Li   auto UDE = llvm::make_unique<DirectoryEntry>();
140*67e74705SXin Li   UDE->Name = NamedDirEnt.first().data();
141*67e74705SXin Li   NamedDirEnt.second = UDE.get();
142*67e74705SXin Li   VirtualDirectoryEntries.push_back(std::move(UDE));
143*67e74705SXin Li 
144*67e74705SXin Li   // Recursively add the other ancestors.
145*67e74705SXin Li   addAncestorsAsVirtualDirs(DirName);
146*67e74705SXin Li }
147*67e74705SXin Li 
getDirectory(StringRef DirName,bool CacheFailure)148*67e74705SXin Li const DirectoryEntry *FileManager::getDirectory(StringRef DirName,
149*67e74705SXin Li                                                 bool CacheFailure) {
150*67e74705SXin Li   // stat doesn't like trailing separators except for root directory.
151*67e74705SXin Li   // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
152*67e74705SXin Li   // (though it can strip '\\')
153*67e74705SXin Li   if (DirName.size() > 1 &&
154*67e74705SXin Li       DirName != llvm::sys::path::root_path(DirName) &&
155*67e74705SXin Li       llvm::sys::path::is_separator(DirName.back()))
156*67e74705SXin Li     DirName = DirName.substr(0, DirName.size()-1);
157*67e74705SXin Li #ifdef LLVM_ON_WIN32
158*67e74705SXin Li   // Fixing a problem with "clang C:test.c" on Windows.
159*67e74705SXin Li   // Stat("C:") does not recognize "C:" as a valid directory
160*67e74705SXin Li   std::string DirNameStr;
161*67e74705SXin Li   if (DirName.size() > 1 && DirName.back() == ':' &&
162*67e74705SXin Li       DirName.equals_lower(llvm::sys::path::root_name(DirName))) {
163*67e74705SXin Li     DirNameStr = DirName.str() + '.';
164*67e74705SXin Li     DirName = DirNameStr;
165*67e74705SXin Li   }
166*67e74705SXin Li #endif
167*67e74705SXin Li 
168*67e74705SXin Li   ++NumDirLookups;
169*67e74705SXin Li   auto &NamedDirEnt =
170*67e74705SXin Li       *SeenDirEntries.insert(std::make_pair(DirName, nullptr)).first;
171*67e74705SXin Li 
172*67e74705SXin Li   // See if there was already an entry in the map.  Note that the map
173*67e74705SXin Li   // contains both virtual and real directories.
174*67e74705SXin Li   if (NamedDirEnt.second)
175*67e74705SXin Li     return NamedDirEnt.second == NON_EXISTENT_DIR ? nullptr
176*67e74705SXin Li                                                   : NamedDirEnt.second;
177*67e74705SXin Li 
178*67e74705SXin Li   ++NumDirCacheMisses;
179*67e74705SXin Li 
180*67e74705SXin Li   // By default, initialize it to invalid.
181*67e74705SXin Li   NamedDirEnt.second = NON_EXISTENT_DIR;
182*67e74705SXin Li 
183*67e74705SXin Li   // Get the null-terminated directory name as stored as the key of the
184*67e74705SXin Li   // SeenDirEntries map.
185*67e74705SXin Li   const char *InterndDirName = NamedDirEnt.first().data();
186*67e74705SXin Li 
187*67e74705SXin Li   // Check to see if the directory exists.
188*67e74705SXin Li   FileData Data;
189*67e74705SXin Li   if (getStatValue(InterndDirName, Data, false, nullptr /*directory lookup*/)) {
190*67e74705SXin Li     // There's no real directory at the given path.
191*67e74705SXin Li     if (!CacheFailure)
192*67e74705SXin Li       SeenDirEntries.erase(DirName);
193*67e74705SXin Li     return nullptr;
194*67e74705SXin Li   }
195*67e74705SXin Li 
196*67e74705SXin Li   // It exists.  See if we have already opened a directory with the
197*67e74705SXin Li   // same inode (this occurs on Unix-like systems when one dir is
198*67e74705SXin Li   // symlinked to another, for example) or the same path (on
199*67e74705SXin Li   // Windows).
200*67e74705SXin Li   DirectoryEntry &UDE = UniqueRealDirs[Data.UniqueID];
201*67e74705SXin Li 
202*67e74705SXin Li   NamedDirEnt.second = &UDE;
203*67e74705SXin Li   if (!UDE.getName()) {
204*67e74705SXin Li     // We don't have this directory yet, add it.  We use the string
205*67e74705SXin Li     // key from the SeenDirEntries map as the string.
206*67e74705SXin Li     UDE.Name  = InterndDirName;
207*67e74705SXin Li   }
208*67e74705SXin Li 
209*67e74705SXin Li   return &UDE;
210*67e74705SXin Li }
211*67e74705SXin Li 
getFile(StringRef Filename,bool openFile,bool CacheFailure)212*67e74705SXin Li const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
213*67e74705SXin Li                                       bool CacheFailure) {
214*67e74705SXin Li   ++NumFileLookups;
215*67e74705SXin Li 
216*67e74705SXin Li   // See if there is already an entry in the map.
217*67e74705SXin Li   auto &NamedFileEnt =
218*67e74705SXin Li       *SeenFileEntries.insert(std::make_pair(Filename, nullptr)).first;
219*67e74705SXin Li 
220*67e74705SXin Li   // See if there is already an entry in the map.
221*67e74705SXin Li   if (NamedFileEnt.second)
222*67e74705SXin Li     return NamedFileEnt.second == NON_EXISTENT_FILE ? nullptr
223*67e74705SXin Li                                                     : NamedFileEnt.second;
224*67e74705SXin Li 
225*67e74705SXin Li   ++NumFileCacheMisses;
226*67e74705SXin Li 
227*67e74705SXin Li   // By default, initialize it to invalid.
228*67e74705SXin Li   NamedFileEnt.second = NON_EXISTENT_FILE;
229*67e74705SXin Li 
230*67e74705SXin Li   // Get the null-terminated file name as stored as the key of the
231*67e74705SXin Li   // SeenFileEntries map.
232*67e74705SXin Li   const char *InterndFileName = NamedFileEnt.first().data();
233*67e74705SXin Li 
234*67e74705SXin Li   // Look up the directory for the file.  When looking up something like
235*67e74705SXin Li   // sys/foo.h we'll discover all of the search directories that have a 'sys'
236*67e74705SXin Li   // subdirectory.  This will let us avoid having to waste time on known-to-fail
237*67e74705SXin Li   // searches when we go to find sys/bar.h, because all the search directories
238*67e74705SXin Li   // without a 'sys' subdir will get a cached failure result.
239*67e74705SXin Li   const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
240*67e74705SXin Li                                                        CacheFailure);
241*67e74705SXin Li   if (DirInfo == nullptr) { // Directory doesn't exist, file can't exist.
242*67e74705SXin Li     if (!CacheFailure)
243*67e74705SXin Li       SeenFileEntries.erase(Filename);
244*67e74705SXin Li 
245*67e74705SXin Li     return nullptr;
246*67e74705SXin Li   }
247*67e74705SXin Li 
248*67e74705SXin Li   // FIXME: Use the directory info to prune this, before doing the stat syscall.
249*67e74705SXin Li   // FIXME: This will reduce the # syscalls.
250*67e74705SXin Li 
251*67e74705SXin Li   // Nope, there isn't.  Check to see if the file exists.
252*67e74705SXin Li   std::unique_ptr<vfs::File> F;
253*67e74705SXin Li   FileData Data;
254*67e74705SXin Li   if (getStatValue(InterndFileName, Data, true, openFile ? &F : nullptr)) {
255*67e74705SXin Li     // There's no real file at the given path.
256*67e74705SXin Li     if (!CacheFailure)
257*67e74705SXin Li       SeenFileEntries.erase(Filename);
258*67e74705SXin Li 
259*67e74705SXin Li     return nullptr;
260*67e74705SXin Li   }
261*67e74705SXin Li 
262*67e74705SXin Li   assert((openFile || !F) && "undesired open file");
263*67e74705SXin Li 
264*67e74705SXin Li   // It exists.  See if we have already opened a file with the same inode.
265*67e74705SXin Li   // This occurs when one dir is symlinked to another, for example.
266*67e74705SXin Li   FileEntry &UFE = UniqueRealFiles[Data.UniqueID];
267*67e74705SXin Li 
268*67e74705SXin Li   NamedFileEnt.second = &UFE;
269*67e74705SXin Li 
270*67e74705SXin Li   // If the name returned by getStatValue is different than Filename, re-intern
271*67e74705SXin Li   // the name.
272*67e74705SXin Li   if (Data.Name != Filename) {
273*67e74705SXin Li     auto &NamedFileEnt =
274*67e74705SXin Li         *SeenFileEntries.insert(std::make_pair(Data.Name, nullptr)).first;
275*67e74705SXin Li     if (!NamedFileEnt.second)
276*67e74705SXin Li       NamedFileEnt.second = &UFE;
277*67e74705SXin Li     else
278*67e74705SXin Li       assert(NamedFileEnt.second == &UFE &&
279*67e74705SXin Li              "filename from getStatValue() refers to wrong file");
280*67e74705SXin Li     InterndFileName = NamedFileEnt.first().data();
281*67e74705SXin Li   }
282*67e74705SXin Li 
283*67e74705SXin Li   if (UFE.isValid()) { // Already have an entry with this inode, return it.
284*67e74705SXin Li 
285*67e74705SXin Li     // FIXME: this hack ensures that if we look up a file by a virtual path in
286*67e74705SXin Li     // the VFS that the getDir() will have the virtual path, even if we found
287*67e74705SXin Li     // the file by a 'real' path first. This is required in order to find a
288*67e74705SXin Li     // module's structure when its headers/module map are mapped in the VFS.
289*67e74705SXin Li     // We should remove this as soon as we can properly support a file having
290*67e74705SXin Li     // multiple names.
291*67e74705SXin Li     if (DirInfo != UFE.Dir && Data.IsVFSMapped)
292*67e74705SXin Li       UFE.Dir = DirInfo;
293*67e74705SXin Li 
294*67e74705SXin Li     // Always update the name to use the last name by which a file was accessed.
295*67e74705SXin Li     // FIXME: Neither this nor always using the first name is correct; we want
296*67e74705SXin Li     // to switch towards a design where we return a FileName object that
297*67e74705SXin Li     // encapsulates both the name by which the file was accessed and the
298*67e74705SXin Li     // corresponding FileEntry.
299*67e74705SXin Li     UFE.Name = InterndFileName;
300*67e74705SXin Li 
301*67e74705SXin Li     return &UFE;
302*67e74705SXin Li   }
303*67e74705SXin Li 
304*67e74705SXin Li   // Otherwise, we don't have this file yet, add it.
305*67e74705SXin Li   UFE.Name    = InterndFileName;
306*67e74705SXin Li   UFE.Size = Data.Size;
307*67e74705SXin Li   UFE.ModTime = Data.ModTime;
308*67e74705SXin Li   UFE.Dir     = DirInfo;
309*67e74705SXin Li   UFE.UID     = NextFileUID++;
310*67e74705SXin Li   UFE.UniqueID = Data.UniqueID;
311*67e74705SXin Li   UFE.IsNamedPipe = Data.IsNamedPipe;
312*67e74705SXin Li   UFE.InPCH = Data.InPCH;
313*67e74705SXin Li   UFE.File = std::move(F);
314*67e74705SXin Li   UFE.IsValid = true;
315*67e74705SXin Li   if (UFE.File)
316*67e74705SXin Li     if (auto RealPathName = UFE.File->getName())
317*67e74705SXin Li       UFE.RealPathName = *RealPathName;
318*67e74705SXin Li   return &UFE;
319*67e74705SXin Li }
320*67e74705SXin Li 
321*67e74705SXin Li const FileEntry *
getVirtualFile(StringRef Filename,off_t Size,time_t ModificationTime)322*67e74705SXin Li FileManager::getVirtualFile(StringRef Filename, off_t Size,
323*67e74705SXin Li                             time_t ModificationTime) {
324*67e74705SXin Li   ++NumFileLookups;
325*67e74705SXin Li 
326*67e74705SXin Li   // See if there is already an entry in the map.
327*67e74705SXin Li   auto &NamedFileEnt =
328*67e74705SXin Li       *SeenFileEntries.insert(std::make_pair(Filename, nullptr)).first;
329*67e74705SXin Li 
330*67e74705SXin Li   // See if there is already an entry in the map.
331*67e74705SXin Li   if (NamedFileEnt.second && NamedFileEnt.second != NON_EXISTENT_FILE)
332*67e74705SXin Li     return NamedFileEnt.second;
333*67e74705SXin Li 
334*67e74705SXin Li   ++NumFileCacheMisses;
335*67e74705SXin Li 
336*67e74705SXin Li   // By default, initialize it to invalid.
337*67e74705SXin Li   NamedFileEnt.second = NON_EXISTENT_FILE;
338*67e74705SXin Li 
339*67e74705SXin Li   addAncestorsAsVirtualDirs(Filename);
340*67e74705SXin Li   FileEntry *UFE = nullptr;
341*67e74705SXin Li 
342*67e74705SXin Li   // Now that all ancestors of Filename are in the cache, the
343*67e74705SXin Li   // following call is guaranteed to find the DirectoryEntry from the
344*67e74705SXin Li   // cache.
345*67e74705SXin Li   const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
346*67e74705SXin Li                                                        /*CacheFailure=*/true);
347*67e74705SXin Li   assert(DirInfo &&
348*67e74705SXin Li          "The directory of a virtual file should already be in the cache.");
349*67e74705SXin Li 
350*67e74705SXin Li   // Check to see if the file exists. If so, drop the virtual file
351*67e74705SXin Li   FileData Data;
352*67e74705SXin Li   const char *InterndFileName = NamedFileEnt.first().data();
353*67e74705SXin Li   if (getStatValue(InterndFileName, Data, true, nullptr) == 0) {
354*67e74705SXin Li     Data.Size = Size;
355*67e74705SXin Li     Data.ModTime = ModificationTime;
356*67e74705SXin Li     UFE = &UniqueRealFiles[Data.UniqueID];
357*67e74705SXin Li 
358*67e74705SXin Li     NamedFileEnt.second = UFE;
359*67e74705SXin Li 
360*67e74705SXin Li     // If we had already opened this file, close it now so we don't
361*67e74705SXin Li     // leak the descriptor. We're not going to use the file
362*67e74705SXin Li     // descriptor anyway, since this is a virtual file.
363*67e74705SXin Li     if (UFE->File)
364*67e74705SXin Li       UFE->closeFile();
365*67e74705SXin Li 
366*67e74705SXin Li     // If we already have an entry with this inode, return it.
367*67e74705SXin Li     if (UFE->isValid())
368*67e74705SXin Li       return UFE;
369*67e74705SXin Li 
370*67e74705SXin Li     UFE->UniqueID = Data.UniqueID;
371*67e74705SXin Li     UFE->IsNamedPipe = Data.IsNamedPipe;
372*67e74705SXin Li     UFE->InPCH = Data.InPCH;
373*67e74705SXin Li   }
374*67e74705SXin Li 
375*67e74705SXin Li   if (!UFE) {
376*67e74705SXin Li     VirtualFileEntries.push_back(llvm::make_unique<FileEntry>());
377*67e74705SXin Li     UFE = VirtualFileEntries.back().get();
378*67e74705SXin Li     NamedFileEnt.second = UFE;
379*67e74705SXin Li   }
380*67e74705SXin Li 
381*67e74705SXin Li   UFE->Name    = InterndFileName;
382*67e74705SXin Li   UFE->Size    = Size;
383*67e74705SXin Li   UFE->ModTime = ModificationTime;
384*67e74705SXin Li   UFE->Dir     = DirInfo;
385*67e74705SXin Li   UFE->UID     = NextFileUID++;
386*67e74705SXin Li   UFE->File.reset();
387*67e74705SXin Li   return UFE;
388*67e74705SXin Li }
389*67e74705SXin Li 
FixupRelativePath(SmallVectorImpl<char> & path) const390*67e74705SXin Li bool FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
391*67e74705SXin Li   StringRef pathRef(path.data(), path.size());
392*67e74705SXin Li 
393*67e74705SXin Li   if (FileSystemOpts.WorkingDir.empty()
394*67e74705SXin Li       || llvm::sys::path::is_absolute(pathRef))
395*67e74705SXin Li     return false;
396*67e74705SXin Li 
397*67e74705SXin Li   SmallString<128> NewPath(FileSystemOpts.WorkingDir);
398*67e74705SXin Li   llvm::sys::path::append(NewPath, pathRef);
399*67e74705SXin Li   path = NewPath;
400*67e74705SXin Li   return true;
401*67e74705SXin Li }
402*67e74705SXin Li 
makeAbsolutePath(SmallVectorImpl<char> & Path) const403*67e74705SXin Li bool FileManager::makeAbsolutePath(SmallVectorImpl<char> &Path) const {
404*67e74705SXin Li   bool Changed = FixupRelativePath(Path);
405*67e74705SXin Li 
406*67e74705SXin Li   if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
407*67e74705SXin Li     llvm::sys::fs::make_absolute(Path);
408*67e74705SXin Li     Changed = true;
409*67e74705SXin Li   }
410*67e74705SXin Li 
411*67e74705SXin Li   return Changed;
412*67e74705SXin Li }
413*67e74705SXin Li 
414*67e74705SXin Li llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
getBufferForFile(const FileEntry * Entry,bool isVolatile,bool ShouldCloseOpenFile)415*67e74705SXin Li FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile,
416*67e74705SXin Li                               bool ShouldCloseOpenFile) {
417*67e74705SXin Li   uint64_t FileSize = Entry->getSize();
418*67e74705SXin Li   // If there's a high enough chance that the file have changed since we
419*67e74705SXin Li   // got its size, force a stat before opening it.
420*67e74705SXin Li   if (isVolatile)
421*67e74705SXin Li     FileSize = -1;
422*67e74705SXin Li 
423*67e74705SXin Li   const char *Filename = Entry->getName();
424*67e74705SXin Li   // If the file is already open, use the open file descriptor.
425*67e74705SXin Li   if (Entry->File) {
426*67e74705SXin Li     auto Result =
427*67e74705SXin Li         Entry->File->getBuffer(Filename, FileSize,
428*67e74705SXin Li                                /*RequiresNullTerminator=*/true, isVolatile);
429*67e74705SXin Li     // FIXME: we need a set of APIs that can make guarantees about whether a
430*67e74705SXin Li     // FileEntry is open or not.
431*67e74705SXin Li     if (ShouldCloseOpenFile)
432*67e74705SXin Li       Entry->closeFile();
433*67e74705SXin Li     return Result;
434*67e74705SXin Li   }
435*67e74705SXin Li 
436*67e74705SXin Li   // Otherwise, open the file.
437*67e74705SXin Li 
438*67e74705SXin Li   if (FileSystemOpts.WorkingDir.empty())
439*67e74705SXin Li     return FS->getBufferForFile(Filename, FileSize,
440*67e74705SXin Li                                 /*RequiresNullTerminator=*/true, isVolatile);
441*67e74705SXin Li 
442*67e74705SXin Li   SmallString<128> FilePath(Entry->getName());
443*67e74705SXin Li   FixupRelativePath(FilePath);
444*67e74705SXin Li   return FS->getBufferForFile(FilePath, FileSize,
445*67e74705SXin Li                               /*RequiresNullTerminator=*/true, isVolatile);
446*67e74705SXin Li }
447*67e74705SXin Li 
448*67e74705SXin Li llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
getBufferForFile(StringRef Filename)449*67e74705SXin Li FileManager::getBufferForFile(StringRef Filename) {
450*67e74705SXin Li   if (FileSystemOpts.WorkingDir.empty())
451*67e74705SXin Li     return FS->getBufferForFile(Filename);
452*67e74705SXin Li 
453*67e74705SXin Li   SmallString<128> FilePath(Filename);
454*67e74705SXin Li   FixupRelativePath(FilePath);
455*67e74705SXin Li   return FS->getBufferForFile(FilePath.c_str());
456*67e74705SXin Li }
457*67e74705SXin Li 
458*67e74705SXin Li /// getStatValue - Get the 'stat' information for the specified path,
459*67e74705SXin Li /// using the cache to accelerate it if possible.  This returns true
460*67e74705SXin Li /// if the path points to a virtual file or does not exist, or returns
461*67e74705SXin Li /// false if it's an existent real file.  If FileDescriptor is NULL,
462*67e74705SXin Li /// do directory look-up instead of file look-up.
getStatValue(const char * Path,FileData & Data,bool isFile,std::unique_ptr<vfs::File> * F)463*67e74705SXin Li bool FileManager::getStatValue(const char *Path, FileData &Data, bool isFile,
464*67e74705SXin Li                                std::unique_ptr<vfs::File> *F) {
465*67e74705SXin Li   // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
466*67e74705SXin Li   // absolute!
467*67e74705SXin Li   if (FileSystemOpts.WorkingDir.empty())
468*67e74705SXin Li     return FileSystemStatCache::get(Path, Data, isFile, F,StatCache.get(), *FS);
469*67e74705SXin Li 
470*67e74705SXin Li   SmallString<128> FilePath(Path);
471*67e74705SXin Li   FixupRelativePath(FilePath);
472*67e74705SXin Li 
473*67e74705SXin Li   return FileSystemStatCache::get(FilePath.c_str(), Data, isFile, F,
474*67e74705SXin Li                                   StatCache.get(), *FS);
475*67e74705SXin Li }
476*67e74705SXin Li 
getNoncachedStatValue(StringRef Path,vfs::Status & Result)477*67e74705SXin Li bool FileManager::getNoncachedStatValue(StringRef Path,
478*67e74705SXin Li                                         vfs::Status &Result) {
479*67e74705SXin Li   SmallString<128> FilePath(Path);
480*67e74705SXin Li   FixupRelativePath(FilePath);
481*67e74705SXin Li 
482*67e74705SXin Li   llvm::ErrorOr<vfs::Status> S = FS->status(FilePath.c_str());
483*67e74705SXin Li   if (!S)
484*67e74705SXin Li     return true;
485*67e74705SXin Li   Result = *S;
486*67e74705SXin Li   return false;
487*67e74705SXin Li }
488*67e74705SXin Li 
invalidateCache(const FileEntry * Entry)489*67e74705SXin Li void FileManager::invalidateCache(const FileEntry *Entry) {
490*67e74705SXin Li   assert(Entry && "Cannot invalidate a NULL FileEntry");
491*67e74705SXin Li 
492*67e74705SXin Li   SeenFileEntries.erase(Entry->getName());
493*67e74705SXin Li 
494*67e74705SXin Li   // FileEntry invalidation should not block future optimizations in the file
495*67e74705SXin Li   // caches. Possible alternatives are cache truncation (invalidate last N) or
496*67e74705SXin Li   // invalidation of the whole cache.
497*67e74705SXin Li   UniqueRealFiles.erase(Entry->getUniqueID());
498*67e74705SXin Li }
499*67e74705SXin Li 
500*67e74705SXin Li 
GetUniqueIDMapping(SmallVectorImpl<const FileEntry * > & UIDToFiles) const501*67e74705SXin Li void FileManager::GetUniqueIDMapping(
502*67e74705SXin Li                    SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
503*67e74705SXin Li   UIDToFiles.clear();
504*67e74705SXin Li   UIDToFiles.resize(NextFileUID);
505*67e74705SXin Li 
506*67e74705SXin Li   // Map file entries
507*67e74705SXin Li   for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator
508*67e74705SXin Li          FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end();
509*67e74705SXin Li        FE != FEEnd; ++FE)
510*67e74705SXin Li     if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE)
511*67e74705SXin Li       UIDToFiles[FE->getValue()->getUID()] = FE->getValue();
512*67e74705SXin Li 
513*67e74705SXin Li   // Map virtual file entries
514*67e74705SXin Li   for (const auto &VFE : VirtualFileEntries)
515*67e74705SXin Li     if (VFE && VFE.get() != NON_EXISTENT_FILE)
516*67e74705SXin Li       UIDToFiles[VFE->getUID()] = VFE.get();
517*67e74705SXin Li }
518*67e74705SXin Li 
modifyFileEntry(FileEntry * File,off_t Size,time_t ModificationTime)519*67e74705SXin Li void FileManager::modifyFileEntry(FileEntry *File,
520*67e74705SXin Li                                   off_t Size, time_t ModificationTime) {
521*67e74705SXin Li   File->Size = Size;
522*67e74705SXin Li   File->ModTime = ModificationTime;
523*67e74705SXin Li }
524*67e74705SXin Li 
getCanonicalName(const DirectoryEntry * Dir)525*67e74705SXin Li StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
526*67e74705SXin Li   // FIXME: use llvm::sys::fs::canonical() when it gets implemented
527*67e74705SXin Li   llvm::DenseMap<const DirectoryEntry *, llvm::StringRef>::iterator Known
528*67e74705SXin Li     = CanonicalDirNames.find(Dir);
529*67e74705SXin Li   if (Known != CanonicalDirNames.end())
530*67e74705SXin Li     return Known->second;
531*67e74705SXin Li 
532*67e74705SXin Li   StringRef CanonicalName(Dir->getName());
533*67e74705SXin Li 
534*67e74705SXin Li #ifdef LLVM_ON_UNIX
535*67e74705SXin Li   char CanonicalNameBuf[PATH_MAX];
536*67e74705SXin Li   if (realpath(Dir->getName(), CanonicalNameBuf))
537*67e74705SXin Li     CanonicalName = StringRef(CanonicalNameBuf).copy(CanonicalNameStorage);
538*67e74705SXin Li #else
539*67e74705SXin Li   SmallString<256> CanonicalNameBuf(CanonicalName);
540*67e74705SXin Li   llvm::sys::fs::make_absolute(CanonicalNameBuf);
541*67e74705SXin Li   llvm::sys::path::native(CanonicalNameBuf);
542*67e74705SXin Li   // We've run into needing to remove '..' here in the wild though, so
543*67e74705SXin Li   // remove it.
544*67e74705SXin Li   // On Windows, symlinks are significantly less prevalent, so removing
545*67e74705SXin Li   // '..' is pretty safe.
546*67e74705SXin Li   // Ideally we'd have an equivalent of `realpath` and could implement
547*67e74705SXin Li   // sys::fs::canonical across all the platforms.
548*67e74705SXin Li   llvm::sys::path::remove_dots(CanonicalNameBuf, /* remove_dot_dot */ true);
549*67e74705SXin Li   CanonicalName = StringRef(CanonicalNameBuf).copy(CanonicalNameStorage);
550*67e74705SXin Li #endif
551*67e74705SXin Li 
552*67e74705SXin Li   CanonicalDirNames.insert(std::make_pair(Dir, CanonicalName));
553*67e74705SXin Li   return CanonicalName;
554*67e74705SXin Li }
555*67e74705SXin Li 
PrintStats() const556*67e74705SXin Li void FileManager::PrintStats() const {
557*67e74705SXin Li   llvm::errs() << "\n*** File Manager Stats:\n";
558*67e74705SXin Li   llvm::errs() << UniqueRealFiles.size() << " real files found, "
559*67e74705SXin Li                << UniqueRealDirs.size() << " real dirs found.\n";
560*67e74705SXin Li   llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
561*67e74705SXin Li                << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
562*67e74705SXin Li   llvm::errs() << NumDirLookups << " dir lookups, "
563*67e74705SXin Li                << NumDirCacheMisses << " dir cache misses.\n";
564*67e74705SXin Li   llvm::errs() << NumFileLookups << " file lookups, "
565*67e74705SXin Li                << NumFileCacheMisses << " file cache misses.\n";
566*67e74705SXin Li 
567*67e74705SXin Li   //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
568*67e74705SXin Li }
569