xref: /llvm-project/clang/lib/Basic/FileManager.cpp (revision 5769c3dffd6d7b1d7684c93eaea71e1277846e26)
1 //===--- FileManager.cpp - File System Probing and Caching ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the FileManager interface.
11 //
12 //===----------------------------------------------------------------------===//
13 //
14 // TODO: This should index all interesting directories with dirent calls.
15 //  getdirentries ?
16 //  opendir/readdir_r/closedir ?
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #include "clang/Basic/FileManager.h"
21 #include "clang/Basic/FileSystemStatCache.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/System/Path.h"
27 #include "llvm/Config/config.h"
28 #include <map>
29 #include <set>
30 #include <string>
31 using namespace clang;
32 
33 // FIXME: Enhance libsystem to support inode and other fields.
34 #include <sys/stat.h>
35 
36 #if defined(_MSC_VER)
37 #define S_ISDIR(s) (_S_IFDIR & s)
38 #endif
39 
40 /// NON_EXISTENT_DIR - A special value distinct from null that is used to
41 /// represent a dir name that doesn't exist on the disk.
42 #define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
43 
44 //===----------------------------------------------------------------------===//
45 // Windows.
46 //===----------------------------------------------------------------------===//
47 
48 #ifdef LLVM_ON_WIN32
49 
50 #define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
51 
52 namespace {
53   static std::string GetFullPath(const char *relPath) {
54     char *absPathStrPtr = _fullpath(NULL, relPath, 0);
55     assert(absPathStrPtr && "_fullpath() returned NULL!");
56 
57     std::string absPath(absPathStrPtr);
58 
59     free(absPathStrPtr);
60     return absPath;
61   }
62 }
63 
64 class FileManager::UniqueDirContainer {
65   /// UniqueDirs - Cache from full path to existing directories/files.
66   ///
67   llvm::StringMap<DirectoryEntry> UniqueDirs;
68 
69 public:
70   DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
71     std::string FullPath(GetFullPath(Name));
72     return UniqueDirs.GetOrCreateValue(
73                               FullPath.c_str(),
74                               FullPath.c_str() + FullPath.size()
75                                                                 ).getValue();
76   }
77 
78   size_t size() { return UniqueDirs.size(); }
79 };
80 
81 class FileManager::UniqueFileContainer {
82   /// UniqueFiles - Cache from full path to existing directories/files.
83   ///
84   llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
85 
86 public:
87   FileEntry &getFile(const char *Name, struct stat &StatBuf) {
88     std::string FullPath(GetFullPath(Name));
89 
90     // LowercaseString because Windows filesystem is case insensitive.
91     FullPath = llvm::LowercaseString(FullPath);
92     return UniqueFiles.GetOrCreateValue(
93                                FullPath.c_str(),
94                                FullPath.c_str() + FullPath.size()
95                                                                  ).getValue();
96   }
97 
98   size_t size() { return UniqueFiles.size(); }
99 };
100 
101 //===----------------------------------------------------------------------===//
102 // Unix-like Systems.
103 //===----------------------------------------------------------------------===//
104 
105 #else
106 
107 #define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
108 
109 class FileManager::UniqueDirContainer {
110   /// UniqueDirs - Cache from ID's to existing directories/files.
111   ///
112   std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
113 
114 public:
115   DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
116     return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
117   }
118 
119   size_t size() { return UniqueDirs.size(); }
120 };
121 
122 class FileManager::UniqueFileContainer {
123   /// UniqueFiles - Cache from ID's to existing directories/files.
124   ///
125   std::set<FileEntry> UniqueFiles;
126 
127 public:
128   FileEntry &getFile(const char *Name, struct stat &StatBuf) {
129     return
130       const_cast<FileEntry&>(
131                     *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
132                                                   StatBuf.st_ino,
133                                                   StatBuf.st_mode)).first);
134   }
135 
136   size_t size() { return UniqueFiles.size(); }
137 };
138 
139 #endif
140 
141 //===----------------------------------------------------------------------===//
142 // Common logic.
143 //===----------------------------------------------------------------------===//
144 
145 FileManager::FileManager(const FileSystemOptions &FSO)
146   : FileSystemOpts(FSO),
147     UniqueDirs(*new UniqueDirContainer),
148     UniqueFiles(*new UniqueFileContainer),
149     DirEntries(64), FileEntries(64), NextFileUID(0) {
150   NumDirLookups = NumFileLookups = 0;
151   NumDirCacheMisses = NumFileCacheMisses = 0;
152 }
153 
154 FileManager::~FileManager() {
155   delete &UniqueDirs;
156   delete &UniqueFiles;
157   for (llvm::SmallVectorImpl<FileEntry *>::iterator
158          V = VirtualFileEntries.begin(),
159          VEnd = VirtualFileEntries.end();
160        V != VEnd;
161        ++V)
162     delete *V;
163 }
164 
165 void FileManager::addStatCache(FileSystemStatCache *statCache,
166                                bool AtBeginning) {
167   assert(statCache && "No stat cache provided?");
168   if (AtBeginning || StatCache.get() == 0) {
169     statCache->setNextStatCache(StatCache.take());
170     StatCache.reset(statCache);
171     return;
172   }
173 
174   FileSystemStatCache *LastCache = StatCache.get();
175   while (LastCache->getNextStatCache())
176     LastCache = LastCache->getNextStatCache();
177 
178   LastCache->setNextStatCache(statCache);
179 }
180 
181 void FileManager::removeStatCache(FileSystemStatCache *statCache) {
182   if (!statCache)
183     return;
184 
185   if (StatCache.get() == statCache) {
186     // This is the first stat cache.
187     StatCache.reset(StatCache->takeNextStatCache());
188     return;
189   }
190 
191   // Find the stat cache in the list.
192   FileSystemStatCache *PrevCache = StatCache.get();
193   while (PrevCache && PrevCache->getNextStatCache() != statCache)
194     PrevCache = PrevCache->getNextStatCache();
195   if (PrevCache)
196     PrevCache->setNextStatCache(statCache->getNextStatCache());
197   else
198     assert(false && "Stat cache not found for removal");
199 }
200 
201 /// \brief Retrieve the directory that the given file name resides in.
202 static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
203                                                   llvm::StringRef Filename) {
204   // Figure out what directory it is in.   If the string contains a / in it,
205   // strip off everything after it.
206   // FIXME: this logic should be in sys::Path.
207   size_t SlashPos = Filename.size();
208   while (SlashPos != 0 && !IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
209     --SlashPos;
210 
211   // Use the current directory if file has no path component.
212   if (SlashPos == 0)
213     return FileMgr.getDirectory(".");
214 
215   if (SlashPos == Filename.size()-1)
216     return 0;       // If filename ends with a /, it's a directory.
217 
218   // Ignore repeated //'s.
219   while (SlashPos != 0 && IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
220     --SlashPos;
221 
222   return FileMgr.getDirectory(Filename.substr(0, SlashPos));
223 }
224 
225 /// getDirectory - Lookup, cache, and verify the specified directory.  This
226 /// returns null if the directory doesn't exist.
227 ///
228 const DirectoryEntry *FileManager::getDirectory(llvm::StringRef Filename) {
229   // stat doesn't like trailing separators (at least on Windows).
230   if (Filename.size() > 1 && IS_DIR_SEPARATOR_CHAR(Filename.back()))
231     Filename = Filename.substr(0, Filename.size()-1);
232 
233   ++NumDirLookups;
234   llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
235     DirEntries.GetOrCreateValue(Filename);
236 
237   // See if there is already an entry in the map.
238   if (NamedDirEnt.getValue())
239     return NamedDirEnt.getValue() == NON_EXISTENT_DIR
240               ? 0 : NamedDirEnt.getValue();
241 
242   ++NumDirCacheMisses;
243 
244   // By default, initialize it to invalid.
245   NamedDirEnt.setValue(NON_EXISTENT_DIR);
246 
247   // Get the null-terminated directory name as stored as the key of the
248   // DirEntries map.
249   const char *InterndDirName = NamedDirEnt.getKeyData();
250 
251   // Check to see if the directory exists.
252   struct stat StatBuf;
253   if (getStatValue(InterndDirName, StatBuf) ||    // Error stat'ing.
254       !S_ISDIR(StatBuf.st_mode))                  // Not a directory?
255     return 0;
256 
257   // It exists.  See if we have already opened a directory with the same inode.
258   // This occurs when one dir is symlinked to another, for example.
259   DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
260 
261   NamedDirEnt.setValue(&UDE);
262   if (UDE.getName()) // Already have an entry with this inode, return it.
263     return &UDE;
264 
265   // Otherwise, we don't have this directory yet, add it.  We use the string
266   // key from the DirEntries map as the string.
267   UDE.Name  = InterndDirName;
268   return &UDE;
269 }
270 
271 /// NON_EXISTENT_FILE - A special value distinct from null that is used to
272 /// represent a filename that doesn't exist on the disk.
273 #define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
274 
275 /// getFile - Lookup, cache, and verify the specified file.  This returns null
276 /// if the file doesn't exist.
277 ///
278 const FileEntry *FileManager::getFile(llvm::StringRef Filename) {
279   ++NumFileLookups;
280 
281   // See if there is already an entry in the map.
282   llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
283     FileEntries.GetOrCreateValue(Filename);
284 
285   // See if there is already an entry in the map.
286   if (NamedFileEnt.getValue())
287     return NamedFileEnt.getValue() == NON_EXISTENT_FILE
288                  ? 0 : NamedFileEnt.getValue();
289 
290   ++NumFileCacheMisses;
291 
292   // By default, initialize it to invalid.
293   NamedFileEnt.setValue(NON_EXISTENT_FILE);
294 
295 
296   // Get the null-terminated file name as stored as the key of the
297   // FileEntries map.
298   const char *InterndFileName = NamedFileEnt.getKeyData();
299 
300   const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
301   if (DirInfo == 0)  // Directory doesn't exist, file can't exist.
302     return 0;
303 
304   // FIXME: Use the directory info to prune this, before doing the stat syscall.
305   // FIXME: This will reduce the # syscalls.
306 
307   // Nope, there isn't.  Check to see if the file exists.
308   struct stat StatBuf;
309   if (getStatValue(InterndFileName, StatBuf) ||    // Error stat'ing.
310       S_ISDIR(StatBuf.st_mode)) {                  // A directory?
311     // If this file doesn't exist, we leave NON_EXISTENT_FILE in FileEntries for
312     // this path so subsequent queries get the negative result.
313     return 0;
314   }
315 
316   // It exists.  See if we have already opened a file with the same inode.
317   // This occurs when one dir is symlinked to another, for example.
318   FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
319 
320   NamedFileEnt.setValue(&UFE);
321   if (UFE.getName())  // Already have an entry with this inode, return it.
322     return &UFE;
323 
324   // Otherwise, we don't have this directory yet, add it.
325   // FIXME: Change the name to be a char* that points back to the 'FileEntries'
326   // key.
327   UFE.Name    = InterndFileName;
328   UFE.Size    = StatBuf.st_size;
329   UFE.ModTime = StatBuf.st_mtime;
330   UFE.Dir     = DirInfo;
331   UFE.UID     = NextFileUID++;
332   return &UFE;
333 }
334 
335 const FileEntry *
336 FileManager::getVirtualFile(llvm::StringRef Filename, off_t Size,
337                             time_t ModificationTime) {
338   ++NumFileLookups;
339 
340   // See if there is already an entry in the map.
341   llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
342     FileEntries.GetOrCreateValue(Filename);
343 
344   // See if there is already an entry in the map.
345   if (NamedFileEnt.getValue())
346     return NamedFileEnt.getValue() == NON_EXISTENT_FILE
347                  ? 0 : NamedFileEnt.getValue();
348 
349   ++NumFileCacheMisses;
350 
351   // By default, initialize it to invalid.
352   NamedFileEnt.setValue(NON_EXISTENT_FILE);
353 
354   const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
355   if (DirInfo == 0)  // Directory doesn't exist, file can't exist.
356     return 0;
357 
358   FileEntry *UFE = new FileEntry();
359   VirtualFileEntries.push_back(UFE);
360   NamedFileEnt.setValue(UFE);
361 
362   UFE->Name    = NamedFileEnt.getKeyData();
363   UFE->Size    = Size;
364   UFE->ModTime = ModificationTime;
365   UFE->Dir     = DirInfo;
366   UFE->UID     = NextFileUID++;
367 
368   // If this virtual file resolves to a file, also map that file to the
369   // newly-created file entry.
370   const char *InterndFileName = NamedFileEnt.getKeyData();
371   struct stat StatBuf;
372   if (!getStatValue(InterndFileName, StatBuf) &&
373       !S_ISDIR(StatBuf.st_mode)) {
374     llvm::sys::Path FilePath(InterndFileName);
375     FilePath.makeAbsolute();
376     FileEntries[FilePath.str()] = UFE;
377   }
378 
379   return UFE;
380 }
381 
382 void FileManager::FixupRelativePath(llvm::sys::Path &path,
383                                     const FileSystemOptions &FSOpts) {
384   if (FSOpts.WorkingDir.empty() || path.isAbsolute()) return;
385 
386   llvm::sys::Path NewPath(FSOpts.WorkingDir);
387   NewPath.appendComponent(path.str());
388   path = NewPath;
389 }
390 
391 llvm::MemoryBuffer *FileManager::
392 getBufferForFile(const FileEntry *Entry, std::string *ErrorStr) {
393   llvm::StringRef Filename = Entry->getName();
394   if (FileSystemOpts.WorkingDir.empty())
395     return llvm::MemoryBuffer::getFile(Filename, ErrorStr, Entry->getSize());
396 
397   llvm::sys::Path FilePath(Filename);
398   FixupRelativePath(FilePath, FileSystemOpts);
399   return llvm::MemoryBuffer::getFile(FilePath.c_str(), ErrorStr,
400                                      Entry->getSize());
401 }
402 
403 llvm::MemoryBuffer *FileManager::
404 getBufferForFile(llvm::StringRef Filename, std::string *ErrorStr) {
405   if (FileSystemOpts.WorkingDir.empty())
406     return llvm::MemoryBuffer::getFile(Filename, ErrorStr);
407 
408   llvm::sys::Path FilePath(Filename);
409   FixupRelativePath(FilePath, FileSystemOpts);
410   return llvm::MemoryBuffer::getFile(FilePath.c_str(), ErrorStr);
411 }
412 
413 /// getStatValue - Get the 'stat' information for the specified path, using the
414 /// cache to accellerate it if possible.  This returns true if the path does not
415 /// exist or false if it exists.
416 bool FileManager::getStatValue(const char *Path, struct stat &StatBuf) {
417   // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
418   // absolute!
419   if (FileSystemOpts.WorkingDir.empty())
420     return FileSystemStatCache::get(Path, StatBuf, StatCache.get());
421 
422   llvm::sys::Path FilePath(Path);
423   FixupRelativePath(FilePath, FileSystemOpts);
424 
425   return FileSystemStatCache::get(FilePath.c_str(), StatBuf, StatCache.get());
426 }
427 
428 
429 
430 void FileManager::PrintStats() const {
431   llvm::errs() << "\n*** File Manager Stats:\n";
432   llvm::errs() << UniqueFiles.size() << " files found, "
433                << UniqueDirs.size() << " dirs found.\n";
434   llvm::errs() << NumDirLookups << " dir lookups, "
435                << NumDirCacheMisses << " dir cache misses.\n";
436   llvm::errs() << NumFileLookups << " file lookups, "
437                << NumFileCacheMisses << " file cache misses.\n";
438 
439   //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
440 }
441 
442