FileSystemStatCache.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //===- FileSystemStatCache.cpp - Caching for 'stat' calls -----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the FileSystemStatCache interface.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Basic/FileSystemStatCache.h"
  13. #include "llvm/Support/Chrono.h"
  14. #include "llvm/Support/ErrorOr.h"
  15. #include "llvm/Support/Path.h"
  16. #include "llvm/Support/VirtualFileSystem.h"
  17. #include <utility>
  18. using namespace clang;
  19. void FileSystemStatCache::anchor() {}
  20. /// FileSystemStatCache::get - Get the 'stat' information for the specified
  21. /// path, using the cache to accelerate it if possible. This returns true if
  22. /// the path does not exist or false if it exists.
  23. ///
  24. /// If isFile is true, then this lookup should only return success for files
  25. /// (not directories). If it is false this lookup should only return
  26. /// success for directories (not files). On a successful file lookup, the
  27. /// implementation can optionally fill in FileDescriptor with a valid
  28. /// descriptor and the client guarantees that it will close it.
  29. bool FileSystemStatCache::get(StringRef Path, llvm::vfs::Status &Status,
  30. bool isFile,
  31. std::unique_ptr<llvm::vfs::File> *F,
  32. FileSystemStatCache *Cache,
  33. llvm::vfs::FileSystem &FS) {
  34. LookupResult R;
  35. bool isForDir = !isFile;
  36. // If we have a cache, use it to resolve the stat query.
  37. if (Cache)
  38. R = Cache->getStat(Path, Status, isFile, F, FS);
  39. else if (isForDir || !F) {
  40. // If this is a directory or a file descriptor is not needed and we have
  41. // no cache, just go to the file system.
  42. llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = FS.status(Path);
  43. if (!StatusOrErr) {
  44. R = CacheMissing;
  45. } else {
  46. R = CacheExists;
  47. Status = *StatusOrErr;
  48. }
  49. } else {
  50. // Otherwise, we have to go to the filesystem. We can always just use
  51. // 'stat' here, but (for files) the client is asking whether the file exists
  52. // because it wants to turn around and *open* it. It is more efficient to
  53. // do "open+fstat" on success than it is to do "stat+open".
  54. //
  55. // Because of this, check to see if the file exists with 'open'. If the
  56. // open succeeds, use fstat to get the stat info.
  57. auto OwnedFile = FS.openFileForRead(Path);
  58. if (!OwnedFile) {
  59. // If the open fails, our "stat" fails.
  60. R = CacheMissing;
  61. } else {
  62. // Otherwise, the open succeeded. Do an fstat to get the information
  63. // about the file. We'll end up returning the open file descriptor to the
  64. // client to do what they please with it.
  65. llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = (*OwnedFile)->status();
  66. if (StatusOrErr) {
  67. R = CacheExists;
  68. Status = *StatusOrErr;
  69. *F = std::move(*OwnedFile);
  70. } else {
  71. // fstat rarely fails. If it does, claim the initial open didn't
  72. // succeed.
  73. R = CacheMissing;
  74. *F = nullptr;
  75. }
  76. }
  77. }
  78. // If the path doesn't exist, return failure.
  79. if (R == CacheMissing) return true;
  80. // If the path exists, make sure that its "directoryness" matches the clients
  81. // demands.
  82. if (Status.isDirectory() != isForDir) {
  83. // If not, close the file if opened.
  84. if (F)
  85. *F = nullptr;
  86. return true;
  87. }
  88. return false;
  89. }
  90. MemorizeStatCalls::LookupResult
  91. MemorizeStatCalls::getStat(StringRef Path, llvm::vfs::Status &Status,
  92. bool isFile,
  93. std::unique_ptr<llvm::vfs::File> *F,
  94. llvm::vfs::FileSystem &FS) {
  95. if (get(Path, Status, isFile, F, nullptr, FS)) {
  96. // Do not cache failed stats, it is easy to construct common inconsistent
  97. // situations if we do, and they are not important for PCH performance
  98. // (which currently only needs the stats to construct the initial
  99. // FileManager entries).
  100. return CacheMissing;
  101. }
  102. // Cache file 'stat' results and directories with absolutely paths.
  103. if (!Status.isDirectory() || llvm::sys::path::is_absolute(Path))
  104. StatCalls[Path] = Status;
  105. return CacheExists;
  106. }