FileSystemStatCache.cpp 4.7 KB

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