FileSystemStatCache.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/Path.h"
  15. #include <fcntl.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. /// FileSystemStatCache::get - Get the 'stat' information for the specified
  28. /// path, using the cache to accelerate it if possible. This returns true if
  29. /// the path does not exist or false if it exists.
  30. ///
  31. /// If FileDescriptor is non-null, then this lookup should only return success
  32. /// for files (not directories). If it is null this lookup should only return
  33. /// success for directories (not files). On a successful file lookup, the
  34. /// implementation can optionally fill in FileDescriptor with a valid
  35. /// descriptor and the client guarantees that it will close it.
  36. bool FileSystemStatCache::get(const char *Path, struct stat &StatBuf,
  37. int *FileDescriptor, FileSystemStatCache *Cache) {
  38. LookupResult R;
  39. bool isForDir = FileDescriptor == 0;
  40. // If we have a cache, use it to resolve the stat query.
  41. if (Cache)
  42. R = Cache->getStat(Path, StatBuf, FileDescriptor);
  43. else if (isForDir) {
  44. // If this is a directory and we have no cache, just go to the file system.
  45. R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists;
  46. } else {
  47. // Otherwise, we have to go to the filesystem. We can always just use
  48. // 'stat' here, but (for files) the client is asking whether the file exists
  49. // because it wants to turn around and *open* it. It is more efficient to
  50. // do "open+fstat" on success than it is to do "stat+open".
  51. //
  52. // Because of this, check to see if the file exists with 'open'. If the
  53. // open succeeds, use fstat to get the stat info.
  54. int OpenFlags = O_RDONLY;
  55. #ifdef O_BINARY
  56. OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
  57. #endif
  58. *FileDescriptor = ::open(Path, OpenFlags);
  59. if (*FileDescriptor == -1) {
  60. // If the open fails, our "stat" fails.
  61. R = CacheMissing;
  62. } else {
  63. // Otherwise, the open succeeded. Do an fstat to get the information
  64. // about the file. We'll end up returning the open file descriptor to the
  65. // client to do what they please with it.
  66. if (::fstat(*FileDescriptor, &StatBuf) == 0)
  67. R = CacheExists;
  68. else {
  69. // fstat rarely fails. If it does, claim the initial open didn't
  70. // succeed.
  71. R = CacheMissing;
  72. ::close(*FileDescriptor);
  73. *FileDescriptor = -1;
  74. }
  75. }
  76. }
  77. // If the path doesn't exist, return failure.
  78. if (R == CacheMissing) return true;
  79. // If the path exists, make sure that its "directoryness" matches the clients
  80. // demands.
  81. if (S_ISDIR(StatBuf.st_mode) != isForDir) {
  82. // If not, close the file if opened.
  83. if (FileDescriptor && *FileDescriptor != -1) {
  84. ::close(*FileDescriptor);
  85. *FileDescriptor = -1;
  86. }
  87. return true;
  88. }
  89. return false;
  90. }
  91. MemorizeStatCalls::LookupResult
  92. MemorizeStatCalls::getStat(const char *Path, struct stat &StatBuf,
  93. int *FileDescriptor) {
  94. LookupResult Result = statChained(Path, StatBuf, FileDescriptor);
  95. // Do not cache failed stats, it is easy to construct common inconsistent
  96. // situations if we do, and they are not important for PCH performance (which
  97. // currently only needs the stats to construct the initial FileManager
  98. // entries).
  99. if (Result == CacheMissing)
  100. return Result;
  101. // Cache file 'stat' results and directories with absolutely paths.
  102. if (!S_ISDIR(StatBuf.st_mode) || llvm::sys::Path(Path).isAbsolute())
  103. StatCalls[Path] = StatBuf;
  104. return Result;
  105. }