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.Name = Status.getName();
  31. Data.Size = Status.getSize();
  32. Data.ModTime = Status.getLastModificationTime().toEpochTime();
  33. Data.UniqueID = Status.getUniqueID();
  34. Data.IsDirectory = Status.isDirectory();
  35. Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
  36. Data.InPCH = false;
  37. Data.IsVFSMapped = Status.IsVFSMapped;
  38. }
  39. /// FileSystemStatCache::get - Get the 'stat' information for the specified
  40. /// path, using the cache to accelerate it if possible. This returns true if
  41. /// the path does not exist or false if it exists.
  42. ///
  43. /// If isFile is true, then this lookup should only return success for files
  44. /// (not directories). If it is false this lookup should only return
  45. /// success for directories (not files). On a successful file lookup, the
  46. /// implementation can optionally fill in FileDescriptor with a valid
  47. /// descriptor and the client guarantees that it will close it.
  48. bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile,
  49. std::unique_ptr<vfs::File> *F,
  50. FileSystemStatCache *Cache, vfs::FileSystem &FS) {
  51. LookupResult R;
  52. bool isForDir = !isFile;
  53. // If we have a cache, use it to resolve the stat query.
  54. if (Cache)
  55. R = Cache->getStat(Path, Data, isFile, F, FS);
  56. else if (isForDir || !F) {
  57. // If this is a directory or a file descriptor is not needed and we have
  58. // no cache, just go to the file system.
  59. llvm::ErrorOr<vfs::Status> Status = FS.status(Path);
  60. if (!Status) {
  61. R = CacheMissing;
  62. } else {
  63. R = CacheExists;
  64. copyStatusToFileData(*Status, Data);
  65. }
  66. } else {
  67. // Otherwise, we have to go to the filesystem. We can always just use
  68. // 'stat' here, but (for files) the client is asking whether the file exists
  69. // because it wants to turn around and *open* it. It is more efficient to
  70. // do "open+fstat" on success than it is to do "stat+open".
  71. //
  72. // Because of this, check to see if the file exists with 'open'. If the
  73. // open succeeds, use fstat to get the stat info.
  74. std::unique_ptr<vfs::File> OwnedFile;
  75. std::error_code EC = FS.openFileForRead(Path, OwnedFile);
  76. if (EC) {
  77. // If the open fails, our "stat" fails.
  78. R = CacheMissing;
  79. } else {
  80. // Otherwise, the open succeeded. Do an fstat to get the information
  81. // about the file. We'll end up returning the open file descriptor to the
  82. // client to do what they please with it.
  83. llvm::ErrorOr<vfs::Status> Status = OwnedFile->status();
  84. if (Status) {
  85. R = CacheExists;
  86. copyStatusToFileData(*Status, Data);
  87. *F = std::move(OwnedFile);
  88. } else {
  89. // fstat rarely fails. If it does, claim the initial open didn't
  90. // succeed.
  91. R = CacheMissing;
  92. *F = nullptr;
  93. }
  94. }
  95. }
  96. // If the path doesn't exist, return failure.
  97. if (R == CacheMissing) return true;
  98. // If the path exists, make sure that its "directoryness" matches the clients
  99. // demands.
  100. if (Data.IsDirectory != isForDir) {
  101. // If not, close the file if opened.
  102. if (F)
  103. *F = nullptr;
  104. return true;
  105. }
  106. return false;
  107. }
  108. MemorizeStatCalls::LookupResult
  109. MemorizeStatCalls::getStat(const char *Path, FileData &Data, bool isFile,
  110. std::unique_ptr<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. }