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