FileSystemStatCache.cpp 4.6 KB

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