FileSystemStatCache.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. std::error_code
  30. FileSystemStatCache::get(StringRef Path, llvm::vfs::Status &Status,
  31. bool isFile, std::unique_ptr<llvm::vfs::File> *F,
  32. FileSystemStatCache *Cache,
  33. llvm::vfs::FileSystem &FS) {
  34. bool isForDir = !isFile;
  35. std::error_code RetCode;
  36. // If we have a cache, use it to resolve the stat query.
  37. if (Cache)
  38. RetCode = 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. RetCode = StatusOrErr.getError();
  45. } else {
  46. Status = *StatusOrErr;
  47. }
  48. } else {
  49. // Otherwise, we have to go to the filesystem. We can always just use
  50. // 'stat' here, but (for files) the client is asking whether the file exists
  51. // because it wants to turn around and *open* it. It is more efficient to
  52. // do "open+fstat" on success than it is to do "stat+open".
  53. //
  54. // Because of this, check to see if the file exists with 'open'. If the
  55. // open succeeds, use fstat to get the stat info.
  56. auto OwnedFile = FS.openFileForRead(Path);
  57. if (!OwnedFile) {
  58. // If the open fails, our "stat" fails.
  59. RetCode = OwnedFile.getError();
  60. } else {
  61. // Otherwise, the open succeeded. Do an fstat to get the information
  62. // about the file. We'll end up returning the open file descriptor to the
  63. // client to do what they please with it.
  64. llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = (*OwnedFile)->status();
  65. if (StatusOrErr) {
  66. Status = *StatusOrErr;
  67. *F = std::move(*OwnedFile);
  68. } else {
  69. // fstat rarely fails. If it does, claim the initial open didn't
  70. // succeed.
  71. *F = nullptr;
  72. RetCode = StatusOrErr.getError();
  73. }
  74. }
  75. }
  76. // If the path doesn't exist, return failure.
  77. if (RetCode)
  78. return RetCode;
  79. // If the path exists, make sure that its "directoryness" matches the clients
  80. // demands.
  81. if (Status.isDirectory() != isForDir) {
  82. // If not, close the file if opened.
  83. if (F)
  84. *F = nullptr;
  85. return std::make_error_code(
  86. Status.isDirectory() ?
  87. std::errc::is_a_directory : std::errc::not_a_directory);
  88. }
  89. return std::error_code();
  90. }
  91. std::error_code
  92. MemorizeStatCalls::getStat(StringRef Path, llvm::vfs::Status &Status,
  93. bool isFile,
  94. std::unique_ptr<llvm::vfs::File> *F,
  95. llvm::vfs::FileSystem &FS) {
  96. auto err = get(Path, Status, isFile, F, nullptr, FS);
  97. if (err) {
  98. // Do not cache failed stats, it is easy to construct common inconsistent
  99. // situations if we do, and they are not important for PCH performance
  100. // (which currently only needs the stats to construct the initial
  101. // FileManager entries).
  102. return err;
  103. }
  104. // Cache file 'stat' results and directories with absolutely paths.
  105. if (!Status.isDirectory() || llvm::sys::path::is_absolute(Path))
  106. StatCalls[Path] = Status;
  107. return std::error_code();
  108. }