FileSystemStatCache.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //===--- FileSystemStatCache.cpp - Caching for 'stat' calls ---------------===//
  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 defines the FileSystemStatCache interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Basic/FileSystemStatCache.h"
  14. #include "clang/Basic/VirtualFileSystem.h"
  15. #include "llvm/Support/Path.h"
  16. // FIXME: This is terrible, we need this for ::close.
  17. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  18. #include <unistd.h>
  19. #include <sys/uio.h>
  20. #else
  21. #include <io.h>
  22. #endif
  23. using namespace clang;
  24. #if defined(_MSC_VER)
  25. #define S_ISDIR(s) ((_S_IFDIR & s) !=0)
  26. #endif
  27. void FileSystemStatCache::anchor() { }
  28. static void copyStatusToFileData(const vfs::Status &Status,
  29. FileData &Data) {
  30. Data.Size = Status.getSize();
  31. Data.ModTime = Status.getLastModificationTime().toEpochTime();
  32. Data.UniqueID = Status.getUniqueID();
  33. Data.IsDirectory = Status.isDirectory();
  34. Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
  35. Data.InPCH = false;
  36. }
  37. /// FileSystemStatCache::get - Get the 'stat' information for the specified
  38. /// path, using the cache to accelerate it if possible. This returns true if
  39. /// the path does not exist or false if it exists.
  40. ///
  41. /// If isFile is true, then this lookup should only return success for files
  42. /// (not directories). If it is false this lookup should only return
  43. /// success for directories (not files). On a successful file lookup, the
  44. /// implementation can optionally fill in FileDescriptor with a valid
  45. /// descriptor and the client guarantees that it will close it.
  46. bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile,
  47. vfs::File **F, FileSystemStatCache *Cache,
  48. vfs::FileSystem &FS) {
  49. LookupResult R;
  50. bool isForDir = !isFile;
  51. // If we have a cache, use it to resolve the stat query.
  52. if (Cache)
  53. R = Cache->getStat(Path, Data, isFile, F, FS);
  54. else if (isForDir || !F) {
  55. // If this is a directory or a file descriptor is not needed and we have
  56. // no cache, just go to the file system.
  57. llvm::ErrorOr<vfs::Status> Status = FS.status(Path);
  58. if (!Status) {
  59. R = CacheMissing;
  60. } else {
  61. R = CacheExists;
  62. copyStatusToFileData(*Status, Data);
  63. }
  64. } else {
  65. // Otherwise, we have to go to the filesystem. We can always just use
  66. // 'stat' here, but (for files) the client is asking whether the file exists
  67. // because it wants to turn around and *open* it. It is more efficient to
  68. // do "open+fstat" on success than it is to do "stat+open".
  69. //
  70. // Because of this, check to see if the file exists with 'open'. If the
  71. // open succeeds, use fstat to get the stat info.
  72. llvm::OwningPtr<vfs::File> OwnedFile;
  73. llvm::error_code EC = FS.openFileForRead(Path, OwnedFile);
  74. if (EC) {
  75. // If the open fails, our "stat" fails.
  76. R = CacheMissing;
  77. } else {
  78. // Otherwise, the open succeeded. Do an fstat to get the information
  79. // about the file. We'll end up returning the open file descriptor to the
  80. // client to do what they please with it.
  81. llvm::ErrorOr<vfs::Status> Status = OwnedFile->status();
  82. if (Status) {
  83. R = CacheExists;
  84. copyStatusToFileData(*Status, Data);
  85. *F = OwnedFile.take();
  86. } else {
  87. // fstat rarely fails. If it does, claim the initial open didn't
  88. // succeed.
  89. R = CacheMissing;
  90. *F = 0;
  91. }
  92. }
  93. }
  94. // If the path doesn't exist, return failure.
  95. if (R == CacheMissing) return true;
  96. // If the path exists, make sure that its "directoryness" matches the clients
  97. // demands.
  98. if (Data.IsDirectory != isForDir) {
  99. // If not, close the file if opened.
  100. if (F && *F) {
  101. (*F)->close();
  102. *F = 0;
  103. }
  104. return true;
  105. }
  106. return false;
  107. }
  108. MemorizeStatCalls::LookupResult
  109. MemorizeStatCalls::getStat(const char *Path, FileData &Data, bool isFile,
  110. vfs::File **F, vfs::FileSystem &FS) {
  111. LookupResult Result = statChained(Path, Data, isFile, F, FS);
  112. // Do not cache failed stats, it is easy to construct common inconsistent
  113. // situations if we do, and they are not important for PCH performance (which
  114. // currently only needs the stats to construct the initial FileManager
  115. // entries).
  116. if (Result == CacheMissing)
  117. return Result;
  118. // Cache file 'stat' results and directories with absolutely paths.
  119. if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path))
  120. StatCalls[Path] = Data;
  121. return Result;
  122. }