FileSystemStatCache.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. }
  38. /// FileSystemStatCache::get - Get the 'stat' information for the specified
  39. /// path, using the cache to accelerate it if possible. This returns true if
  40. /// the path does not exist or false if it exists.
  41. ///
  42. /// If isFile is true, then this lookup should only return success for files
  43. /// (not directories). If it is false this lookup should only return
  44. /// success for directories (not files). On a successful file lookup, the
  45. /// implementation can optionally fill in FileDescriptor with a valid
  46. /// descriptor and the client guarantees that it will close it.
  47. bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile,
  48. vfs::File **F, FileSystemStatCache *Cache,
  49. vfs::FileSystem &FS) {
  50. LookupResult R;
  51. bool isForDir = !isFile;
  52. // If we have a cache, use it to resolve the stat query.
  53. if (Cache)
  54. R = Cache->getStat(Path, Data, isFile, F, FS);
  55. else if (isForDir || !F) {
  56. // If this is a directory or a file descriptor is not needed and we have
  57. // no cache, just go to the file system.
  58. llvm::ErrorOr<vfs::Status> Status = FS.status(Path);
  59. if (!Status) {
  60. R = CacheMissing;
  61. } else {
  62. R = CacheExists;
  63. copyStatusToFileData(*Status, Data);
  64. }
  65. } else {
  66. // Otherwise, we have to go to the filesystem. We can always just use
  67. // 'stat' here, but (for files) the client is asking whether the file exists
  68. // because it wants to turn around and *open* it. It is more efficient to
  69. // do "open+fstat" on success than it is to do "stat+open".
  70. //
  71. // Because of this, check to see if the file exists with 'open'. If the
  72. // open succeeds, use fstat to get the stat info.
  73. llvm::OwningPtr<vfs::File> OwnedFile;
  74. llvm::error_code EC = FS.openFileForRead(Path, OwnedFile);
  75. if (EC) {
  76. // If the open fails, our "stat" fails.
  77. R = CacheMissing;
  78. } else {
  79. // Otherwise, the open succeeded. Do an fstat to get the information
  80. // about the file. We'll end up returning the open file descriptor to the
  81. // client to do what they please with it.
  82. llvm::ErrorOr<vfs::Status> Status = OwnedFile->status();
  83. if (Status) {
  84. R = CacheExists;
  85. copyStatusToFileData(*Status, Data);
  86. *F = OwnedFile.take();
  87. } else {
  88. // fstat rarely fails. If it does, claim the initial open didn't
  89. // succeed.
  90. R = CacheMissing;
  91. *F = 0;
  92. }
  93. }
  94. }
  95. // If the path doesn't exist, return failure.
  96. if (R == CacheMissing) return true;
  97. // If the path exists, make sure that its "directoryness" matches the clients
  98. // demands.
  99. if (Data.IsDirectory != isForDir) {
  100. // If not, close the file if opened.
  101. if (F && *F) {
  102. (*F)->close();
  103. *F = 0;
  104. }
  105. return true;
  106. }
  107. return false;
  108. }
  109. MemorizeStatCalls::LookupResult
  110. MemorizeStatCalls::getStat(const char *Path, FileData &Data, bool isFile,
  111. vfs::File **F, vfs::FileSystem &FS) {
  112. LookupResult Result = statChained(Path, Data, isFile, F, FS);
  113. // Do not cache failed stats, it is easy to construct common inconsistent
  114. // situations if we do, and they are not important for PCH performance (which
  115. // currently only needs the stats to construct the initial FileManager
  116. // entries).
  117. if (Result == CacheMissing)
  118. return Result;
  119. // Cache file 'stat' results and directories with absolutely paths.
  120. if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path))
  121. StatCalls[Path] = Data;
  122. return Result;
  123. }