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" 1516125fb6SRafael Espindola #include "llvm/Support/FileSystem.h" 168aaf4995SMichael J. Spencer #include "llvm/Support/Path.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 33*f8f91b89SRafael Espindola static void copyStatusToFileData(const llvm::sys::fs::file_status &Status, 34*f8f91b89SRafael Espindola FileData &Data) { 35*f8f91b89SRafael Espindola Data.Size = Status.getSize(); 36*f8f91b89SRafael Espindola Data.ModTime = Status.getLastModificationTime().toEpochTime(); 37*f8f91b89SRafael Espindola Data.UniqueID = Status.getUniqueID(); 38*f8f91b89SRafael Espindola Data.IsDirectory = is_directory(Status); 39*f8f91b89SRafael Espindola Data.IsNamedPipe = Status.type() == llvm::sys::fs::file_type::fifo_file; 40*f8f91b89SRafael Espindola Data.InPCH = false; 41*f8f91b89SRafael Espindola } 42*f8f91b89SRafael Espindola 43dd278430SChris Lattner /// FileSystemStatCache::get - Get the 'stat' information for the specified 444fc8fb09SChris Lattner /// path, using the cache to accelerate it if possible. This returns true if 45dd278430SChris Lattner /// the path does not exist or false if it exists. 46dd278430SChris Lattner /// 473b779379SArgyrios Kyrtzidis /// If isFile is true, then this lookup should only return success for files 483b779379SArgyrios Kyrtzidis /// (not directories). If it is false this lookup should only return 49dd278430SChris Lattner /// success for directories (not files). On a successful file lookup, the 50dd278430SChris Lattner /// implementation can optionally fill in FileDescriptor with a valid 51dd278430SChris Lattner /// descriptor and the client guarantees that it will close it. 52*f8f91b89SRafael Espindola bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile, 53*f8f91b89SRafael Espindola int *FileDescriptor, FileSystemStatCache *Cache) { 54dd278430SChris Lattner LookupResult R; 553b779379SArgyrios Kyrtzidis bool isForDir = !isFile; 56dd278430SChris Lattner 575ea7d07dSChris Lattner // If we have a cache, use it to resolve the stat query. 58dd278430SChris Lattner if (Cache) 59*f8f91b89SRafael Espindola R = Cache->getStat(Path, Data, isFile, FileDescriptor); 603b779379SArgyrios Kyrtzidis else if (isForDir || !FileDescriptor) { 613b779379SArgyrios Kyrtzidis // If this is a directory or a file descriptor is not needed and we have 623b779379SArgyrios Kyrtzidis // no cache, just go to the file system. 63*f8f91b89SRafael Espindola llvm::sys::fs::file_status Status; 64*f8f91b89SRafael Espindola if (llvm::sys::fs::status(Path, Status)) { 65*f8f91b89SRafael Espindola R = CacheMissing; 66*f8f91b89SRafael Espindola } else { 67*f8f91b89SRafael Espindola R = CacheExists; 68*f8f91b89SRafael Espindola copyStatusToFileData(Status, Data); 69*f8f91b89SRafael Espindola } 705ea7d07dSChris Lattner } else { 715ea7d07dSChris Lattner // Otherwise, we have to go to the filesystem. We can always just use 725ea7d07dSChris Lattner // 'stat' here, but (for files) the client is asking whether the file exists 735ea7d07dSChris Lattner // because it wants to turn around and *open* it. It is more efficient to 745ea7d07dSChris Lattner // do "open+fstat" on success than it is to do "stat+open". 755ea7d07dSChris Lattner // 765ea7d07dSChris Lattner // Because of this, check to see if the file exists with 'open'. If the 775ea7d07dSChris Lattner // open succeeds, use fstat to get the stat info. 7816125fb6SRafael Espindola llvm::error_code EC = llvm::sys::fs::openFileForRead(Path, *FileDescriptor); 795ea7d07dSChris Lattner 8016125fb6SRafael Espindola if (EC) { 815ea7d07dSChris Lattner // If the open fails, our "stat" fails. 825ea7d07dSChris Lattner R = CacheMissing; 835ea7d07dSChris Lattner } else { 845ea7d07dSChris Lattner // Otherwise, the open succeeded. Do an fstat to get the information 855ea7d07dSChris Lattner // about the file. We'll end up returning the open file descriptor to the 865ea7d07dSChris Lattner // client to do what they please with it. 87*f8f91b89SRafael Espindola llvm::sys::fs::file_status Status; 88*f8f91b89SRafael Espindola if (!llvm::sys::fs::status(*FileDescriptor, Status)) { 895ea7d07dSChris Lattner R = CacheExists; 90*f8f91b89SRafael Espindola copyStatusToFileData(Status, Data); 91*f8f91b89SRafael Espindola } else { 925ea7d07dSChris Lattner // fstat rarely fails. If it does, claim the initial open didn't 935ea7d07dSChris Lattner // succeed. 945ea7d07dSChris Lattner R = CacheMissing; 955ea7d07dSChris Lattner ::close(*FileDescriptor); 965ea7d07dSChris Lattner *FileDescriptor = -1; 975ea7d07dSChris Lattner } 985ea7d07dSChris Lattner } 995ea7d07dSChris Lattner } 100dd278430SChris Lattner 101f77e11baSChris Lattner // If the path doesn't exist, return failure. 102dd278430SChris Lattner if (R == CacheMissing) return true; 103dd278430SChris Lattner 104f77e11baSChris Lattner // If the path exists, make sure that its "directoryness" matches the clients 105f77e11baSChris Lattner // demands. 106*f8f91b89SRafael Espindola if (Data.IsDirectory != isForDir) { 107f77e11baSChris Lattner // If not, close the file if opened. 108f77e11baSChris Lattner if (FileDescriptor && *FileDescriptor != -1) { 109f77e11baSChris Lattner ::close(*FileDescriptor); 110f77e11baSChris Lattner *FileDescriptor = -1; 111f77e11baSChris Lattner } 112f77e11baSChris Lattner 113f77e11baSChris Lattner return true; 114f77e11baSChris Lattner } 115f77e11baSChris Lattner 116f77e11baSChris Lattner return false; 117dd278430SChris Lattner } 118dd278430SChris Lattner 119226efd35SChris Lattner MemorizeStatCalls::LookupResult 120*f8f91b89SRafael Espindola MemorizeStatCalls::getStat(const char *Path, FileData &Data, bool isFile, 121*f8f91b89SRafael Espindola int *FileDescriptor) { 122*f8f91b89SRafael Espindola LookupResult Result = statChained(Path, Data, isFile, FileDescriptor); 123226efd35SChris Lattner 124226efd35SChris Lattner // Do not cache failed stats, it is easy to construct common inconsistent 125226efd35SChris Lattner // situations if we do, and they are not important for PCH performance (which 126226efd35SChris Lattner // currently only needs the stats to construct the initial FileManager 127226efd35SChris Lattner // entries). 1288f0583daSChris Lattner if (Result == CacheMissing) 129226efd35SChris Lattner return Result; 130226efd35SChris Lattner 131226efd35SChris Lattner // Cache file 'stat' results and directories with absolutely paths. 132*f8f91b89SRafael Espindola if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path)) 133*f8f91b89SRafael Espindola StatCalls[Path] = Data; 134226efd35SChris Lattner 135226efd35SChris Lattner return Result; 136226efd35SChris Lattner } 137