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/Support/FileSystem.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/system_error.h"
28 #include "llvm/Config/llvm-config.h"
29 #include <map>
30 #include <set>
31 #include <string>
32 
33 // FIXME: This is terrible, we need this for ::close.
34 #if !defined(_MSC_VER) && !defined(__MINGW32__)
35 #include <unistd.h>
36 #include <sys/uio.h>
37 #else
38 #include <io.h>
39 #endif
40 using namespace clang;
41 
42 // FIXME: Enhance libsystem to support inode and other fields.
43 #include <sys/stat.h>
44 
45 /// NON_EXISTENT_DIR - A special value distinct from null that is used to
46 /// represent a dir name that doesn't exist on the disk.
47 #define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
48 
49 /// NON_EXISTENT_FILE - A special value distinct from null that is used to
50 /// represent a filename that doesn't exist on the disk.
51 #define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
52 
53 
54 FileEntry::~FileEntry() {
55   // If this FileEntry owns an open file descriptor that never got used, close
56   // it.
57   if (FD != -1) ::close(FD);
58 }
59 
60 //===----------------------------------------------------------------------===//
61 // Windows.
62 //===----------------------------------------------------------------------===//
63 
64 #ifdef LLVM_ON_WIN32
65 
66 namespace {
67   static std::string GetFullPath(const char *relPath) {
68     char *absPathStrPtr = _fullpath(NULL, relPath, 0);
69     assert(absPathStrPtr && "_fullpath() returned NULL!");
70 
71     std::string absPath(absPathStrPtr);
72 
73     free(absPathStrPtr);
74     return absPath;
75   }
76 }
77 
78 class FileManager::UniqueDirContainer {
79   /// UniqueDirs - Cache from full path to existing directories/files.
80   ///
81   llvm::StringMap<DirectoryEntry> UniqueDirs;
82 
83 public:
84   /// getDirectory - Return an existing DirectoryEntry with the given
85   /// name if there is already one; otherwise create and return a
86   /// default-constructed DirectoryEntry.
87   DirectoryEntry &getDirectory(const char *Name,
88                                const struct stat & /*StatBuf*/) {
89     std::string FullPath(GetFullPath(Name));
90     return UniqueDirs.GetOrCreateValue(FullPath).getValue();
91   }
92 
93   size_t size() const { return UniqueDirs.size(); }
94 };
95 
96 class FileManager::UniqueFileContainer {
97   /// UniqueFiles - Cache from full path to existing directories/files.
98   ///
99   llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
100 
101 public:
102   /// getFile - Return an existing FileEntry with the given name if
103   /// there is already one; otherwise create and return a
104   /// default-constructed FileEntry.
105   FileEntry &getFile(const char *Name, const struct stat & /*StatBuf*/) {
106     std::string FullPath(GetFullPath(Name));
107 
108     // Lowercase string because Windows filesystem is case insensitive.
109     FullPath = StringRef(FullPath).lower();
110     return UniqueFiles.GetOrCreateValue(FullPath).getValue();
111   }
112 
113   size_t size() const { return UniqueFiles.size(); }
114 
115   void erase(const FileEntry *Entry) {
116     std::string FullPath(GetFullPath(Entry->getName()));
117 
118     // Lowercase string because Windows filesystem is case insensitive.
119     FullPath = StringRef(FullPath).lower();
120     UniqueFiles.erase(FullPath);
121   }
122 };
123 
124 //===----------------------------------------------------------------------===//
125 // Unix-like Systems.
126 //===----------------------------------------------------------------------===//
127 
128 #else
129 
130 class FileManager::UniqueDirContainer {
131   /// UniqueDirs - Cache from ID's to existing directories/files.
132   std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
133 
134 public:
135   /// getDirectory - Return an existing DirectoryEntry with the given
136   /// ID's if there is already one; otherwise create and return a
137   /// default-constructed DirectoryEntry.
138   DirectoryEntry &getDirectory(const char * /*Name*/,
139                                const struct stat &StatBuf) {
140     return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
141   }
142 
143   size_t size() const { return UniqueDirs.size(); }
144 };
145 
146 class FileManager::UniqueFileContainer {
147   /// UniqueFiles - Cache from ID's to existing directories/files.
148   std::set<FileEntry> UniqueFiles;
149 
150 public:
151   /// getFile - Return an existing FileEntry with the given ID's if
152   /// there is already one; otherwise create and return a
153   /// default-constructed FileEntry.
154   FileEntry &getFile(const char * /*Name*/, const struct stat &StatBuf) {
155     return
156       const_cast<FileEntry&>(
157                     *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
158                                                   StatBuf.st_ino,
159                                                   StatBuf.st_mode)).first);
160   }
161 
162   size_t size() const { return UniqueFiles.size(); }
163 
164   void erase(const FileEntry *Entry) { UniqueFiles.erase(*Entry); }
165 };
166 
167 #endif
168 
169 //===----------------------------------------------------------------------===//
170 // Common logic.
171 //===----------------------------------------------------------------------===//
172 
173 FileManager::FileManager(const FileSystemOptions &FSO)
174   : FileSystemOpts(FSO),
175     UniqueRealDirs(*new UniqueDirContainer()),
176     UniqueRealFiles(*new UniqueFileContainer()),
177     SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) {
178   NumDirLookups = NumFileLookups = 0;
179   NumDirCacheMisses = NumFileCacheMisses = 0;
180 }
181 
182 FileManager::~FileManager() {
183   delete &UniqueRealDirs;
184   delete &UniqueRealFiles;
185   for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i)
186     delete VirtualFileEntries[i];
187   for (unsigned i = 0, e = VirtualDirectoryEntries.size(); i != e; ++i)
188     delete VirtualDirectoryEntries[i];
189 }
190 
191 void FileManager::addStatCache(FileSystemStatCache *statCache,
192                                bool AtBeginning) {
193   assert(statCache && "No stat cache provided?");
194   if (AtBeginning || StatCache.get() == 0) {
195     statCache->setNextStatCache(StatCache.take());
196     StatCache.reset(statCache);
197     return;
198   }
199 
200   FileSystemStatCache *LastCache = StatCache.get();
201   while (LastCache->getNextStatCache())
202     LastCache = LastCache->getNextStatCache();
203 
204   LastCache->setNextStatCache(statCache);
205 }
206 
207 void FileManager::removeStatCache(FileSystemStatCache *statCache) {
208   if (!statCache)
209     return;
210 
211   if (StatCache.get() == statCache) {
212     // This is the first stat cache.
213     StatCache.reset(StatCache->takeNextStatCache());
214     return;
215   }
216 
217   // Find the stat cache in the list.
218   FileSystemStatCache *PrevCache = StatCache.get();
219   while (PrevCache && PrevCache->getNextStatCache() != statCache)
220     PrevCache = PrevCache->getNextStatCache();
221 
222   assert(PrevCache && "Stat cache not found for removal");
223   PrevCache->setNextStatCache(statCache->getNextStatCache());
224 }
225 
226 /// \brief Retrieve the directory that the given file name resides in.
227 /// Filename can point to either a real file or a virtual file.
228 static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
229                                                   StringRef Filename,
230                                                   bool CacheFailure) {
231   if (Filename.empty())
232     return NULL;
233 
234   if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
235     return NULL;  // If Filename is a directory.
236 
237   StringRef DirName = llvm::sys::path::parent_path(Filename);
238   // Use the current directory if file has no path component.
239   if (DirName.empty())
240     DirName = ".";
241 
242   return FileMgr.getDirectory(DirName, CacheFailure);
243 }
244 
245 /// Add all ancestors of the given path (pointing to either a file or
246 /// a directory) as virtual directories.
247 void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
248   StringRef DirName = llvm::sys::path::parent_path(Path);
249   if (DirName.empty())
250     return;
251 
252   llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
253     SeenDirEntries.GetOrCreateValue(DirName);
254 
255   // When caching a virtual directory, we always cache its ancestors
256   // at the same time.  Therefore, if DirName is already in the cache,
257   // we don't need to recurse as its ancestors must also already be in
258   // the cache.
259   if (NamedDirEnt.getValue())
260     return;
261 
262   // Add the virtual directory to the cache.
263   DirectoryEntry *UDE = new DirectoryEntry;
264   UDE->Name = NamedDirEnt.getKeyData();
265   NamedDirEnt.setValue(UDE);
266   VirtualDirectoryEntries.push_back(UDE);
267 
268   // Recursively add the other ancestors.
269   addAncestorsAsVirtualDirs(DirName);
270 }
271 
272 const DirectoryEntry *FileManager::getDirectory(StringRef DirName,
273                                                 bool CacheFailure) {
274   // stat doesn't like trailing separators except for root directory.
275   // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
276   // (though it can strip '\\')
277   if (DirName.size() > 1 &&
278       DirName != llvm::sys::path::root_path(DirName) &&
279       llvm::sys::path::is_separator(DirName.back()))
280     DirName = DirName.substr(0, DirName.size()-1);
281 
282   ++NumDirLookups;
283   llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
284     SeenDirEntries.GetOrCreateValue(DirName);
285 
286   // See if there was already an entry in the map.  Note that the map
287   // contains both virtual and real directories.
288   if (NamedDirEnt.getValue())
289     return NamedDirEnt.getValue() == NON_EXISTENT_DIR
290               ? 0 : NamedDirEnt.getValue();
291 
292   ++NumDirCacheMisses;
293 
294   // By default, initialize it to invalid.
295   NamedDirEnt.setValue(NON_EXISTENT_DIR);
296 
297   // Get the null-terminated directory name as stored as the key of the
298   // SeenDirEntries map.
299   const char *InterndDirName = NamedDirEnt.getKeyData();
300 
301   // Check to see if the directory exists.
302   struct stat StatBuf;
303   if (getStatValue(InterndDirName, StatBuf, 0/*directory lookup*/)) {
304     // There's no real directory at the given path.
305     if (!CacheFailure)
306       SeenDirEntries.erase(DirName);
307     return 0;
308   }
309 
310   // It exists.  See if we have already opened a directory with the
311   // same inode (this occurs on Unix-like systems when one dir is
312   // symlinked to another, for example) or the same path (on
313   // Windows).
314   DirectoryEntry &UDE = UniqueRealDirs.getDirectory(InterndDirName, StatBuf);
315 
316   NamedDirEnt.setValue(&UDE);
317   if (!UDE.getName()) {
318     // We don't have this directory yet, add it.  We use the string
319     // key from the SeenDirEntries map as the string.
320     UDE.Name  = InterndDirName;
321   }
322 
323   return &UDE;
324 }
325 
326 const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
327                                       bool CacheFailure) {
328   ++NumFileLookups;
329 
330   // See if there is already an entry in the map.
331   llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
332     SeenFileEntries.GetOrCreateValue(Filename);
333 
334   // See if there is already an entry in the map.
335   if (NamedFileEnt.getValue())
336     return NamedFileEnt.getValue() == NON_EXISTENT_FILE
337                  ? 0 : NamedFileEnt.getValue();
338 
339   ++NumFileCacheMisses;
340 
341   // By default, initialize it to invalid.
342   NamedFileEnt.setValue(NON_EXISTENT_FILE);
343 
344   // Get the null-terminated file name as stored as the key of the
345   // SeenFileEntries map.
346   const char *InterndFileName = NamedFileEnt.getKeyData();
347 
348   // Look up the directory for the file.  When looking up something like
349   // sys/foo.h we'll discover all of the search directories that have a 'sys'
350   // subdirectory.  This will let us avoid having to waste time on known-to-fail
351   // searches when we go to find sys/bar.h, because all the search directories
352   // without a 'sys' subdir will get a cached failure result.
353   const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
354                                                        CacheFailure);
355   if (DirInfo == 0) {  // Directory doesn't exist, file can't exist.
356     if (!CacheFailure)
357       SeenFileEntries.erase(Filename);
358 
359     return 0;
360   }
361 
362   // FIXME: Use the directory info to prune this, before doing the stat syscall.
363   // FIXME: This will reduce the # syscalls.
364 
365   // Nope, there isn't.  Check to see if the file exists.
366   int FileDescriptor = -1;
367   struct stat StatBuf;
368   if (getStatValue(InterndFileName, StatBuf, &FileDescriptor)) {
369     // There's no real file at the given path.
370     if (!CacheFailure)
371       SeenFileEntries.erase(Filename);
372 
373     return 0;
374   }
375 
376   if (FileDescriptor != -1 && !openFile) {
377     close(FileDescriptor);
378     FileDescriptor = -1;
379   }
380 
381   // It exists.  See if we have already opened a file with the same inode.
382   // This occurs when one dir is symlinked to another, for example.
383   FileEntry &UFE = UniqueRealFiles.getFile(InterndFileName, StatBuf);
384 
385   NamedFileEnt.setValue(&UFE);
386   if (UFE.getName()) { // Already have an entry with this inode, return it.
387     // If the stat process opened the file, close it to avoid a FD leak.
388     if (FileDescriptor != -1)
389       close(FileDescriptor);
390 
391     return &UFE;
392   }
393 
394   // Otherwise, we don't have this directory yet, add it.
395   // FIXME: Change the name to be a char* that points back to the
396   // 'SeenFileEntries' key.
397   UFE.Name    = InterndFileName;
398   UFE.Size    = StatBuf.st_size;
399   UFE.ModTime = StatBuf.st_mtime;
400   UFE.Dir     = DirInfo;
401   UFE.UID     = NextFileUID++;
402   UFE.FD      = FileDescriptor;
403   return &UFE;
404 }
405 
406 const FileEntry *
407 FileManager::getVirtualFile(StringRef Filename, off_t Size,
408                             time_t ModificationTime) {
409   ++NumFileLookups;
410 
411   // See if there is already an entry in the map.
412   llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
413     SeenFileEntries.GetOrCreateValue(Filename);
414 
415   // See if there is already an entry in the map.
416   if (NamedFileEnt.getValue() && NamedFileEnt.getValue() != NON_EXISTENT_FILE)
417     return NamedFileEnt.getValue();
418 
419   ++NumFileCacheMisses;
420 
421   // By default, initialize it to invalid.
422   NamedFileEnt.setValue(NON_EXISTENT_FILE);
423 
424   addAncestorsAsVirtualDirs(Filename);
425   FileEntry *UFE = 0;
426 
427   // Now that all ancestors of Filename are in the cache, the
428   // following call is guaranteed to find the DirectoryEntry from the
429   // cache.
430   const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
431                                                        /*CacheFailure=*/true);
432   assert(DirInfo &&
433          "The directory of a virtual file should already be in the cache.");
434 
435   // Check to see if the file exists. If so, drop the virtual file
436   int FileDescriptor = -1;
437   struct stat StatBuf;
438   const char *InterndFileName = NamedFileEnt.getKeyData();
439   if (getStatValue(InterndFileName, StatBuf, &FileDescriptor) == 0) {
440     // If the stat process opened the file, close it to avoid a FD leak.
441     if (FileDescriptor != -1)
442       close(FileDescriptor);
443 
444     StatBuf.st_size = Size;
445     StatBuf.st_mtime = ModificationTime;
446     UFE = &UniqueRealFiles.getFile(InterndFileName, StatBuf);
447 
448     NamedFileEnt.setValue(UFE);
449 
450     // If we had already opened this file, close it now so we don't
451     // leak the descriptor. We're not going to use the file
452     // descriptor anyway, since this is a virtual file.
453     if (UFE->FD != -1) {
454       close(UFE->FD);
455       UFE->FD = -1;
456     }
457 
458     // If we already have an entry with this inode, return it.
459     if (UFE->getName())
460       return UFE;
461   }
462 
463   if (!UFE) {
464     UFE = new FileEntry();
465     VirtualFileEntries.push_back(UFE);
466     NamedFileEnt.setValue(UFE);
467   }
468 
469   UFE->Name    = InterndFileName;
470   UFE->Size    = Size;
471   UFE->ModTime = ModificationTime;
472   UFE->Dir     = DirInfo;
473   UFE->UID     = NextFileUID++;
474   UFE->FD      = -1;
475   return UFE;
476 }
477 
478 void FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
479   StringRef pathRef(path.data(), path.size());
480 
481   if (FileSystemOpts.WorkingDir.empty()
482       || llvm::sys::path::is_absolute(pathRef))
483     return;
484 
485   SmallString<128> NewPath(FileSystemOpts.WorkingDir);
486   llvm::sys::path::append(NewPath, pathRef);
487   path = NewPath;
488 }
489 
490 llvm::MemoryBuffer *FileManager::
491 getBufferForFile(const FileEntry *Entry, std::string *ErrorStr,
492                  bool isVolatile) {
493   OwningPtr<llvm::MemoryBuffer> Result;
494   llvm::error_code ec;
495 
496   uint64_t FileSize = Entry->getSize();
497   // If there's a high enough chance that the file have changed since we
498   // got its size, force a stat before opening it.
499   if (isVolatile)
500     FileSize = -1;
501 
502   const char *Filename = Entry->getName();
503   // If the file is already open, use the open file descriptor.
504   if (Entry->FD != -1) {
505     ec = llvm::MemoryBuffer::getOpenFile(Entry->FD, Filename, Result, FileSize);
506     if (ErrorStr)
507       *ErrorStr = ec.message();
508 
509     close(Entry->FD);
510     Entry->FD = -1;
511     return Result.take();
512   }
513 
514   // Otherwise, open the file.
515 
516   if (FileSystemOpts.WorkingDir.empty()) {
517     ec = llvm::MemoryBuffer::getFile(Filename, Result, FileSize);
518     if (ec && ErrorStr)
519       *ErrorStr = ec.message();
520     return Result.take();
521   }
522 
523   SmallString<128> FilePath(Entry->getName());
524   FixupRelativePath(FilePath);
525   ec = llvm::MemoryBuffer::getFile(FilePath.str(), Result, FileSize);
526   if (ec && ErrorStr)
527     *ErrorStr = ec.message();
528   return Result.take();
529 }
530 
531 llvm::MemoryBuffer *FileManager::
532 getBufferForFile(StringRef Filename, std::string *ErrorStr) {
533   OwningPtr<llvm::MemoryBuffer> Result;
534   llvm::error_code ec;
535   if (FileSystemOpts.WorkingDir.empty()) {
536     ec = llvm::MemoryBuffer::getFile(Filename, Result);
537     if (ec && ErrorStr)
538       *ErrorStr = ec.message();
539     return Result.take();
540   }
541 
542   SmallString<128> FilePath(Filename);
543   FixupRelativePath(FilePath);
544   ec = llvm::MemoryBuffer::getFile(FilePath.c_str(), Result);
545   if (ec && ErrorStr)
546     *ErrorStr = ec.message();
547   return Result.take();
548 }
549 
550 /// getStatValue - Get the 'stat' information for the specified path,
551 /// using the cache to accelerate it if possible.  This returns true
552 /// if the path points to a virtual file or does not exist, or returns
553 /// false if it's an existent real file.  If FileDescriptor is NULL,
554 /// do directory look-up instead of file look-up.
555 bool FileManager::getStatValue(const char *Path, struct stat &StatBuf,
556                                int *FileDescriptor) {
557   // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
558   // absolute!
559   if (FileSystemOpts.WorkingDir.empty())
560     return FileSystemStatCache::get(Path, StatBuf, FileDescriptor,
561                                     StatCache.get());
562 
563   SmallString<128> FilePath(Path);
564   FixupRelativePath(FilePath);
565 
566   return FileSystemStatCache::get(FilePath.c_str(), StatBuf, FileDescriptor,
567                                   StatCache.get());
568 }
569 
570 bool FileManager::getNoncachedStatValue(StringRef Path,
571                                         struct stat &StatBuf) {
572   SmallString<128> FilePath(Path);
573   FixupRelativePath(FilePath);
574 
575   return ::stat(FilePath.c_str(), &StatBuf) != 0;
576 }
577 
578 void FileManager::invalidateCache(const FileEntry *Entry) {
579   assert(Entry && "Cannot invalidate a NULL FileEntry");
580 
581   SeenFileEntries.erase(Entry->getName());
582 
583   // FileEntry invalidation should not block future optimizations in the file
584   // caches. Possible alternatives are cache truncation (invalidate last N) or
585   // invalidation of the whole cache.
586   UniqueRealFiles.erase(Entry);
587 }
588 
589 
590 void FileManager::GetUniqueIDMapping(
591                    SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
592   UIDToFiles.clear();
593   UIDToFiles.resize(NextFileUID);
594 
595   // Map file entries
596   for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator
597          FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end();
598        FE != FEEnd; ++FE)
599     if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE)
600       UIDToFiles[FE->getValue()->getUID()] = FE->getValue();
601 
602   // Map virtual file entries
603   for (SmallVector<FileEntry*, 4>::const_iterator
604          VFE = VirtualFileEntries.begin(), VFEEnd = VirtualFileEntries.end();
605        VFE != VFEEnd; ++VFE)
606     if (*VFE && *VFE != NON_EXISTENT_FILE)
607       UIDToFiles[(*VFE)->getUID()] = *VFE;
608 }
609 
610 void FileManager::modifyFileEntry(FileEntry *File,
611                                   off_t Size, time_t ModificationTime) {
612   File->Size = Size;
613   File->ModTime = ModificationTime;
614 }
615 
616 
617 void FileManager::PrintStats() const {
618   llvm::errs() << "\n*** File Manager Stats:\n";
619   llvm::errs() << UniqueRealFiles.size() << " real files found, "
620                << UniqueRealDirs.size() << " real dirs found.\n";
621   llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
622                << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
623   llvm::errs() << NumDirLookups << " dir lookups, "
624                << NumDirCacheMisses << " dir cache misses.\n";
625   llvm::errs() << NumFileLookups << " file lookups, "
626                << NumFileCacheMisses << " file cache misses.\n";
627 
628   //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
629 }
630