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" 158aaf4995SMichael J. Spencer #include "llvm/Support/Path.h" 165ea7d07dSChris 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) 2831ee8bfbSFrancois Pichet #define S_ISDIR(s) ((_S_IFDIR & s) !=0) 29ea61b32cSChris Lattner #endif 30ea61b32cSChris Lattner 3168e081d6SDavid Blaikie void FileSystemStatCache::anchor() { } 3268e081d6SDavid Blaikie 33dd278430SChris Lattner /// FileSystemStatCache::get - Get the 'stat' information for the specified 344fc8fb09SChris Lattner /// path, using the cache to accelerate it if possible. This returns true if 35dd278430SChris Lattner /// the path does not exist or false if it exists. 36dd278430SChris Lattner /// 37*3b779379SArgyrios Kyrtzidis /// If isFile is true, then this lookup should only return success for files 38*3b779379SArgyrios Kyrtzidis /// (not directories). If it is false this lookup should only return 39dd278430SChris Lattner /// success for directories (not files). On a successful file lookup, the 40dd278430SChris Lattner /// implementation can optionally fill in FileDescriptor with a valid 41dd278430SChris Lattner /// descriptor and the client guarantees that it will close it. 42dd278430SChris Lattner bool FileSystemStatCache::get(const char *Path, struct stat &StatBuf, 43*3b779379SArgyrios Kyrtzidis bool isFile, int *FileDescriptor, 44*3b779379SArgyrios Kyrtzidis FileSystemStatCache *Cache) { 45dd278430SChris Lattner LookupResult R; 46*3b779379SArgyrios Kyrtzidis bool isForDir = !isFile; 47dd278430SChris Lattner 485ea7d07dSChris Lattner // If we have a cache, use it to resolve the stat query. 49dd278430SChris Lattner if (Cache) 50*3b779379SArgyrios Kyrtzidis R = Cache->getStat(Path, StatBuf, isFile, FileDescriptor); 51*3b779379SArgyrios Kyrtzidis else if (isForDir || !FileDescriptor) { 52*3b779379SArgyrios Kyrtzidis // If this is a directory or a file descriptor is not needed and we have 53*3b779379SArgyrios Kyrtzidis // no cache, just go to the file system. 54dd278430SChris Lattner R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists; 555ea7d07dSChris Lattner } else { 565ea7d07dSChris Lattner // Otherwise, we have to go to the filesystem. We can always just use 575ea7d07dSChris Lattner // 'stat' here, but (for files) the client is asking whether the file exists 585ea7d07dSChris Lattner // because it wants to turn around and *open* it. It is more efficient to 595ea7d07dSChris Lattner // do "open+fstat" on success than it is to do "stat+open". 605ea7d07dSChris Lattner // 615ea7d07dSChris Lattner // Because of this, check to see if the file exists with 'open'. If the 625ea7d07dSChris Lattner // open succeeds, use fstat to get the stat info. 635ea7d07dSChris Lattner int OpenFlags = O_RDONLY; 645ea7d07dSChris Lattner #ifdef O_BINARY 655ea7d07dSChris Lattner OpenFlags |= O_BINARY; // Open input file in binary mode on win32. 665ea7d07dSChris Lattner #endif 675ea7d07dSChris Lattner *FileDescriptor = ::open(Path, OpenFlags); 685ea7d07dSChris Lattner 695ea7d07dSChris Lattner if (*FileDescriptor == -1) { 705ea7d07dSChris Lattner // If the open fails, our "stat" fails. 715ea7d07dSChris Lattner R = CacheMissing; 725ea7d07dSChris Lattner } else { 735ea7d07dSChris Lattner // Otherwise, the open succeeded. Do an fstat to get the information 745ea7d07dSChris Lattner // about the file. We'll end up returning the open file descriptor to the 755ea7d07dSChris Lattner // client to do what they please with it. 765ea7d07dSChris Lattner if (::fstat(*FileDescriptor, &StatBuf) == 0) 775ea7d07dSChris Lattner R = CacheExists; 785ea7d07dSChris Lattner else { 795ea7d07dSChris Lattner // fstat rarely fails. If it does, claim the initial open didn't 805ea7d07dSChris Lattner // succeed. 815ea7d07dSChris Lattner R = CacheMissing; 825ea7d07dSChris Lattner ::close(*FileDescriptor); 835ea7d07dSChris Lattner *FileDescriptor = -1; 845ea7d07dSChris Lattner } 855ea7d07dSChris Lattner } 865ea7d07dSChris Lattner } 87dd278430SChris Lattner 88f77e11baSChris Lattner // If the path doesn't exist, return failure. 89dd278430SChris Lattner if (R == CacheMissing) return true; 90dd278430SChris Lattner 91f77e11baSChris Lattner // If the path exists, make sure that its "directoryness" matches the clients 92f77e11baSChris Lattner // demands. 93f77e11baSChris Lattner if (S_ISDIR(StatBuf.st_mode) != isForDir) { 94f77e11baSChris Lattner // If not, close the file if opened. 95f77e11baSChris Lattner if (FileDescriptor && *FileDescriptor != -1) { 96f77e11baSChris Lattner ::close(*FileDescriptor); 97f77e11baSChris Lattner *FileDescriptor = -1; 98f77e11baSChris Lattner } 99f77e11baSChris Lattner 100f77e11baSChris Lattner return true; 101f77e11baSChris Lattner } 102f77e11baSChris Lattner 103f77e11baSChris Lattner return false; 104dd278430SChris Lattner } 105dd278430SChris Lattner 106dd278430SChris Lattner 107226efd35SChris Lattner MemorizeStatCalls::LookupResult 108dd278430SChris Lattner MemorizeStatCalls::getStat(const char *Path, struct stat &StatBuf, 109*3b779379SArgyrios Kyrtzidis bool isFile, int *FileDescriptor) { 110*3b779379SArgyrios Kyrtzidis LookupResult Result = statChained(Path, StatBuf, isFile, FileDescriptor); 111226efd35SChris Lattner 112226efd35SChris Lattner // Do not cache failed stats, it is easy to construct common inconsistent 113226efd35SChris Lattner // situations if we do, and they are not important for PCH performance (which 114226efd35SChris Lattner // currently only needs the stats to construct the initial FileManager 115226efd35SChris Lattner // entries). 1168f0583daSChris Lattner if (Result == CacheMissing) 117226efd35SChris Lattner return Result; 118226efd35SChris Lattner 119226efd35SChris Lattner // Cache file 'stat' results and directories with absolutely paths. 120f28df4cdSMichael J. Spencer if (!S_ISDIR(StatBuf.st_mode) || llvm::sys::path::is_absolute(Path)) 1212a6fa47bSChris Lattner StatCalls[Path] = StatBuf; 122226efd35SChris Lattner 123226efd35SChris Lattner return Result; 124226efd35SChris Lattner } 125