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" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Path.h" |
Chris Lattner | 5cc1c73 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 16 | #include <fcntl.h> |
Chris Lattner | b2b93c1 | 2010-11-23 21:53:56 +0000 | [diff] [blame] | 17 | |
| 18 | // FIXME: This is terrible, we need this for ::close. |
| 19 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 20 | #include <unistd.h> |
| 21 | #include <sys/uio.h> |
| 22 | #else |
| 23 | #include <io.h> |
| 24 | #endif |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Chris Lattner | 72f6130 | 2010-11-23 20:07:39 +0000 | [diff] [blame] | 27 | #if defined(_MSC_VER) |
Francois Pichet | 0d4739ab | 2010-11-24 03:07:43 +0000 | [diff] [blame] | 28 | #define S_ISDIR(s) ((_S_IFDIR & s) !=0) |
Chris Lattner | 72f6130 | 2010-11-23 20:07:39 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 31 | void FileSystemStatCache::anchor() { } |
| 32 | |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 33 | /// FileSystemStatCache::get - Get the 'stat' information for the specified |
Chris Lattner | c0f31fd | 2010-12-02 04:27:29 +0000 | [diff] [blame] | 34 | /// path, using the cache to accelerate it if possible. This returns true if |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 35 | /// the path does not exist or false if it exists. |
| 36 | /// |
| 37 | /// If FileDescriptor is non-null, then this lookup should only return success |
| 38 | /// for files (not directories). If it is null this lookup should only return |
| 39 | /// success for directories (not files). On a successful file lookup, the |
| 40 | /// implementation can optionally fill in FileDescriptor with a valid |
| 41 | /// descriptor and the client guarantees that it will close it. |
| 42 | bool FileSystemStatCache::get(const char *Path, struct stat &StatBuf, |
| 43 | int *FileDescriptor, FileSystemStatCache *Cache) { |
| 44 | LookupResult R; |
Chris Lattner | 5cc1c73 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 45 | bool isForDir = FileDescriptor == 0; |
| 46 | |
| 47 | // If we have a cache, use it to resolve the stat query. |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 48 | if (Cache) |
| 49 | R = Cache->getStat(Path, StatBuf, FileDescriptor); |
Chris Lattner | 5cc1c73 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 50 | else if (isForDir) { |
| 51 | // If this is a directory and we have no cache, just go to the file system. |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 52 | R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists; |
Chris Lattner | 5cc1c73 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 53 | } else { |
| 54 | // Otherwise, we have to go to the filesystem. We can always just use |
| 55 | // 'stat' here, but (for files) the client is asking whether the file exists |
| 56 | // because it wants to turn around and *open* it. It is more efficient to |
| 57 | // do "open+fstat" on success than it is to do "stat+open". |
| 58 | // |
| 59 | // Because of this, check to see if the file exists with 'open'. If the |
| 60 | // open succeeds, use fstat to get the stat info. |
| 61 | int OpenFlags = O_RDONLY; |
| 62 | #ifdef O_BINARY |
| 63 | OpenFlags |= O_BINARY; // Open input file in binary mode on win32. |
| 64 | #endif |
| 65 | *FileDescriptor = ::open(Path, OpenFlags); |
| 66 | |
| 67 | if (*FileDescriptor == -1) { |
| 68 | // If the open fails, our "stat" fails. |
| 69 | R = CacheMissing; |
| 70 | } else { |
| 71 | // Otherwise, the open succeeded. Do an fstat to get the information |
| 72 | // about the file. We'll end up returning the open file descriptor to the |
| 73 | // client to do what they please with it. |
| 74 | if (::fstat(*FileDescriptor, &StatBuf) == 0) |
| 75 | R = CacheExists; |
| 76 | else { |
| 77 | // fstat rarely fails. If it does, claim the initial open didn't |
| 78 | // succeed. |
| 79 | R = CacheMissing; |
| 80 | ::close(*FileDescriptor); |
| 81 | *FileDescriptor = -1; |
| 82 | } |
| 83 | } |
| 84 | } |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 85 | |
Chris Lattner | b2b93c1 | 2010-11-23 21:53:56 +0000 | [diff] [blame] | 86 | // If the path doesn't exist, return failure. |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 87 | if (R == CacheMissing) return true; |
| 88 | |
Chris Lattner | b2b93c1 | 2010-11-23 21:53:56 +0000 | [diff] [blame] | 89 | // If the path exists, make sure that its "directoryness" matches the clients |
| 90 | // demands. |
Chris Lattner | b2b93c1 | 2010-11-23 21:53:56 +0000 | [diff] [blame] | 91 | if (S_ISDIR(StatBuf.st_mode) != isForDir) { |
| 92 | // If not, close the file if opened. |
| 93 | if (FileDescriptor && *FileDescriptor != -1) { |
| 94 | ::close(*FileDescriptor); |
| 95 | *FileDescriptor = -1; |
| 96 | } |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | return false; |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 105 | MemorizeStatCalls::LookupResult |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 106 | MemorizeStatCalls::getStat(const char *Path, struct stat &StatBuf, |
| 107 | int *FileDescriptor) { |
| 108 | LookupResult Result = statChained(Path, StatBuf, FileDescriptor); |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 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). |
Chris Lattner | d6f6111 | 2010-11-23 20:05:15 +0000 | [diff] [blame] | 114 | if (Result == CacheMissing) |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 115 | return Result; |
| 116 | |
| 117 | // Cache file 'stat' results and directories with absolutely paths. |
Michael J. Spencer | 256053b | 2010-12-17 21:22:22 +0000 | [diff] [blame] | 118 | if (!S_ISDIR(StatBuf.st_mode) || llvm::sys::path::is_absolute(Path)) |
Chris Lattner | 74e976b | 2010-11-23 19:28:12 +0000 | [diff] [blame] | 119 | StatCalls[Path] = StatBuf; |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 120 | |
| 121 | return Result; |
| 122 | } |