FileSystemStatCache.cpp 4.8 KB

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