1226efd35SChris Lattner //===--- FileSystemStatCache.cpp - Caching for 'stat' calls ---------------===// 2226efd35SChris Lattner // 3226efd35SChris Lattner // The LLVM Compiler Infrastructure 4226efd35SChris Lattner // 5226efd35SChris Lattner // This file is distributed under the University of Illinois Open Source 6226efd35SChris Lattner // License. See LICENSE.TXT for details. 7226efd35SChris Lattner // 8226efd35SChris Lattner //===----------------------------------------------------------------------===// 9226efd35SChris Lattner // 10226efd35SChris Lattner // This file defines the FileSystemStatCache interface. 11226efd35SChris Lattner // 12226efd35SChris Lattner //===----------------------------------------------------------------------===// 13226efd35SChris Lattner 14226efd35SChris Lattner #include "clang/Basic/FileSystemStatCache.h" 15226efd35SChris Lattner #include "llvm/System/Path.h" 16*5ea7d07dSChris Lattner #include <fcntl.h> 17f77e11baSChris Lattner 18f77e11baSChris Lattner // FIXME: This is terrible, we need this for ::close. 19f77e11baSChris Lattner #if !defined(_MSC_VER) && !defined(__MINGW32__) 20f77e11baSChris Lattner #include <unistd.h> 21f77e11baSChris Lattner #include <sys/uio.h> 22f77e11baSChris Lattner #else 23f77e11baSChris Lattner #include <io.h> 24f77e11baSChris Lattner #endif 25226efd35SChris Lattner using namespace clang; 26226efd35SChris Lattner 27ea61b32cSChris Lattner #if defined(_MSC_VER) 28ea61b32cSChris Lattner #define S_ISDIR(s) (_S_IFDIR & s) 29ea61b32cSChris Lattner #endif 30ea61b32cSChris Lattner 31dd278430SChris Lattner /// FileSystemStatCache::get - Get the 'stat' information for the specified 32dd278430SChris Lattner /// path, using the cache to accellerate it if possible. This returns true if 33dd278430SChris Lattner /// the path does not exist or false if it exists. 34dd278430SChris Lattner /// 35dd278430SChris Lattner /// If FileDescriptor is non-null, then this lookup should only return success 36dd278430SChris Lattner /// for files (not directories). If it is null this lookup should only return 37dd278430SChris Lattner /// success for directories (not files). On a successful file lookup, the 38dd278430SChris Lattner /// implementation can optionally fill in FileDescriptor with a valid 39dd278430SChris Lattner /// descriptor and the client guarantees that it will close it. 40dd278430SChris Lattner bool FileSystemStatCache::get(const char *Path, struct stat &StatBuf, 41dd278430SChris Lattner int *FileDescriptor, FileSystemStatCache *Cache) { 42dd278430SChris Lattner LookupResult R; 43*5ea7d07dSChris Lattner bool isForDir = FileDescriptor == 0; 44dd278430SChris Lattner 45*5ea7d07dSChris Lattner // If we have a cache, use it to resolve the stat query. 46dd278430SChris Lattner if (Cache) 47dd278430SChris Lattner R = Cache->getStat(Path, StatBuf, FileDescriptor); 48*5ea7d07dSChris Lattner else if (isForDir) { 49*5ea7d07dSChris Lattner // If this is a directory and we have no cache, just go to the file system. 50dd278430SChris Lattner R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists; 51*5ea7d07dSChris Lattner } else { 52*5ea7d07dSChris Lattner // Otherwise, we have to go to the filesystem. We can always just use 53*5ea7d07dSChris Lattner // 'stat' here, but (for files) the client is asking whether the file exists 54*5ea7d07dSChris Lattner // because it wants to turn around and *open* it. It is more efficient to 55*5ea7d07dSChris Lattner // do "open+fstat" on success than it is to do "stat+open". 56*5ea7d07dSChris Lattner // 57*5ea7d07dSChris Lattner // Because of this, check to see if the file exists with 'open'. If the 58*5ea7d07dSChris Lattner // open succeeds, use fstat to get the stat info. 59*5ea7d07dSChris Lattner int OpenFlags = O_RDONLY; 60*5ea7d07dSChris Lattner #ifdef O_BINARY 61*5ea7d07dSChris Lattner OpenFlags |= O_BINARY; // Open input file in binary mode on win32. 62*5ea7d07dSChris Lattner #endif 63*5ea7d07dSChris Lattner *FileDescriptor = ::open(Path, OpenFlags); 64*5ea7d07dSChris Lattner 65*5ea7d07dSChris Lattner if (*FileDescriptor == -1) { 66*5ea7d07dSChris Lattner // If the open fails, our "stat" fails. 67*5ea7d07dSChris Lattner R = CacheMissing; 68*5ea7d07dSChris Lattner } else { 69*5ea7d07dSChris Lattner // Otherwise, the open succeeded. Do an fstat to get the information 70*5ea7d07dSChris Lattner // about the file. We'll end up returning the open file descriptor to the 71*5ea7d07dSChris Lattner // client to do what they please with it. 72*5ea7d07dSChris Lattner if (::fstat(*FileDescriptor, &StatBuf) == 0) 73*5ea7d07dSChris Lattner R = CacheExists; 74*5ea7d07dSChris Lattner else { 75*5ea7d07dSChris Lattner // fstat rarely fails. If it does, claim the initial open didn't 76*5ea7d07dSChris Lattner // succeed. 77*5ea7d07dSChris Lattner R = CacheMissing; 78*5ea7d07dSChris Lattner ::close(*FileDescriptor); 79*5ea7d07dSChris Lattner *FileDescriptor = -1; 80*5ea7d07dSChris Lattner } 81*5ea7d07dSChris Lattner } 82*5ea7d07dSChris Lattner } 83dd278430SChris Lattner 84f77e11baSChris Lattner // If the path doesn't exist, return failure. 85dd278430SChris Lattner if (R == CacheMissing) return true; 86dd278430SChris Lattner 87f77e11baSChris Lattner // If the path exists, make sure that its "directoryness" matches the clients 88f77e11baSChris Lattner // demands. 89f77e11baSChris Lattner if (S_ISDIR(StatBuf.st_mode) != isForDir) { 90f77e11baSChris Lattner // If not, close the file if opened. 91f77e11baSChris Lattner if (FileDescriptor && *FileDescriptor != -1) { 92f77e11baSChris Lattner ::close(*FileDescriptor); 93f77e11baSChris Lattner *FileDescriptor = -1; 94f77e11baSChris Lattner } 95f77e11baSChris Lattner 96f77e11baSChris Lattner return true; 97f77e11baSChris Lattner } 98f77e11baSChris Lattner 99f77e11baSChris Lattner return false; 100dd278430SChris Lattner } 101dd278430SChris Lattner 102dd278430SChris Lattner 103226efd35SChris Lattner MemorizeStatCalls::LookupResult 104dd278430SChris Lattner MemorizeStatCalls::getStat(const char *Path, struct stat &StatBuf, 105dd278430SChris Lattner int *FileDescriptor) { 106dd278430SChris Lattner LookupResult Result = statChained(Path, StatBuf, FileDescriptor); 107226efd35SChris Lattner 108226efd35SChris Lattner // Do not cache failed stats, it is easy to construct common inconsistent 109226efd35SChris Lattner // situations if we do, and they are not important for PCH performance (which 110226efd35SChris Lattner // currently only needs the stats to construct the initial FileManager 111226efd35SChris Lattner // entries). 1128f0583daSChris Lattner if (Result == CacheMissing) 113226efd35SChris Lattner return Result; 114226efd35SChris Lattner 115226efd35SChris Lattner // Cache file 'stat' results and directories with absolutely paths. 116226efd35SChris Lattner if (!S_ISDIR(StatBuf.st_mode) || llvm::sys::Path(Path).isAbsolute()) 1172a6fa47bSChris Lattner StatCalls[Path] = StatBuf; 118226efd35SChris Lattner 119226efd35SChris Lattner return Result; 120226efd35SChris Lattner } 121