Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 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 | |
| 14 | #include "clang/Basic/FileSystemStatCache.h" |
| 15 | #include "llvm/System/Path.h" |
Chris Lattner | b2b93c1 | 2010-11-23 21:53:56 +0000 | [diff] [blame^] | 16 | |
| 17 | // FIXME: This is terrible, we need this for ::close. |
| 18 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 19 | #include <unistd.h> |
| 20 | #include <sys/uio.h> |
| 21 | #else |
| 22 | #include <io.h> |
| 23 | #endif |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
Chris Lattner | 72f6130 | 2010-11-23 20:07:39 +0000 | [diff] [blame] | 26 | #if defined(_MSC_VER) |
| 27 | #define S_ISDIR(s) (_S_IFDIR & s) |
| 28 | #endif |
| 29 | |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 30 | /// FileSystemStatCache::get - Get the 'stat' information for the specified |
| 31 | /// path, using the cache to accellerate it if possible. This returns true if |
| 32 | /// the path does not exist or false if it exists. |
| 33 | /// |
| 34 | /// If FileDescriptor is non-null, then this lookup should only return success |
| 35 | /// for files (not directories). If it is null this lookup should only return |
| 36 | /// success for directories (not files). On a successful file lookup, the |
| 37 | /// implementation can optionally fill in FileDescriptor with a valid |
| 38 | /// descriptor and the client guarantees that it will close it. |
| 39 | bool FileSystemStatCache::get(const char *Path, struct stat &StatBuf, |
| 40 | int *FileDescriptor, FileSystemStatCache *Cache) { |
| 41 | LookupResult R; |
| 42 | |
| 43 | if (Cache) |
| 44 | R = Cache->getStat(Path, StatBuf, FileDescriptor); |
| 45 | else |
| 46 | R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists; |
| 47 | |
Chris Lattner | b2b93c1 | 2010-11-23 21:53:56 +0000 | [diff] [blame^] | 48 | // If the path doesn't exist, return failure. |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 49 | if (R == CacheMissing) return true; |
| 50 | |
Chris Lattner | b2b93c1 | 2010-11-23 21:53:56 +0000 | [diff] [blame^] | 51 | // If the path exists, make sure that its "directoryness" matches the clients |
| 52 | // demands. |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 53 | bool isForDir = FileDescriptor == 0; |
Chris Lattner | b2b93c1 | 2010-11-23 21:53:56 +0000 | [diff] [blame^] | 54 | if (S_ISDIR(StatBuf.st_mode) != isForDir) { |
| 55 | // If not, close the file if opened. |
| 56 | if (FileDescriptor && *FileDescriptor != -1) { |
| 57 | ::close(*FileDescriptor); |
| 58 | *FileDescriptor = -1; |
| 59 | } |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | return false; |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 68 | MemorizeStatCalls::LookupResult |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 69 | MemorizeStatCalls::getStat(const char *Path, struct stat &StatBuf, |
| 70 | int *FileDescriptor) { |
| 71 | LookupResult Result = statChained(Path, StatBuf, FileDescriptor); |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 73 | // Do not cache failed stats, it is easy to construct common inconsistent |
| 74 | // situations if we do, and they are not important for PCH performance (which |
| 75 | // currently only needs the stats to construct the initial FileManager |
| 76 | // entries). |
Chris Lattner | d6f6111 | 2010-11-23 20:05:15 +0000 | [diff] [blame] | 77 | if (Result == CacheMissing) |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 78 | return Result; |
| 79 | |
| 80 | // Cache file 'stat' results and directories with absolutely paths. |
| 81 | if (!S_ISDIR(StatBuf.st_mode) || llvm::sys::Path(Path).isAbsolute()) |
Chris Lattner | 74e976b | 2010-11-23 19:28:12 +0000 | [diff] [blame] | 82 | StatCalls[Path] = StatBuf; |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 83 | |
| 84 | return Result; |
| 85 | } |