15a52011cSEugene Zelenko //===- 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" 155a52011cSEugene Zelenko #include "llvm/Support/Chrono.h" 165a52011cSEugene Zelenko #include "llvm/Support/ErrorOr.h" 178aaf4995SMichael J. Spencer #include "llvm/Support/Path.h" 18*fc51490bSJonas Devlieghere #include "llvm/Support/VirtualFileSystem.h" 195a52011cSEugene Zelenko #include <utility> 20f77e11baSChris Lattner 21226efd35SChris Lattner using namespace clang; 22226efd35SChris Lattner 2368e081d6SDavid Blaikie void FileSystemStatCache::anchor() {} 2468e081d6SDavid Blaikie 25*fc51490bSJonas Devlieghere static void copyStatusToFileData(const llvm::vfs::Status &Status, 26f8f91b89SRafael Espindola FileData &Data) { 27d066d4c8SBen Langmuir Data.Name = Status.getName(); 28f8f91b89SRafael Espindola Data.Size = Status.getSize(); 29ac71c8e2SPavel Labath Data.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime()); 30f8f91b89SRafael Espindola Data.UniqueID = Status.getUniqueID(); 31c8130a74SBen Langmuir Data.IsDirectory = Status.isDirectory(); 32c8130a74SBen Langmuir Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file; 33f8f91b89SRafael Espindola Data.InPCH = false; 345de00f3bSBen Langmuir Data.IsVFSMapped = Status.IsVFSMapped; 35f8f91b89SRafael Espindola } 36f8f91b89SRafael Espindola 37dd278430SChris Lattner /// FileSystemStatCache::get - Get the 'stat' information for the specified 384fc8fb09SChris Lattner /// path, using the cache to accelerate it if possible. This returns true if 39dd278430SChris Lattner /// the path does not exist or false if it exists. 40dd278430SChris Lattner /// 413b779379SArgyrios Kyrtzidis /// If isFile is true, then this lookup should only return success for files 423b779379SArgyrios Kyrtzidis /// (not directories). If it is false this lookup should only return 43dd278430SChris Lattner /// success for directories (not files). On a successful file lookup, the 44dd278430SChris Lattner /// implementation can optionally fill in FileDescriptor with a valid 45dd278430SChris Lattner /// descriptor and the client guarantees that it will close it. 460df59d8cSMehdi Amini bool FileSystemStatCache::get(StringRef Path, FileData &Data, bool isFile, 47*fc51490bSJonas Devlieghere std::unique_ptr<llvm::vfs::File> *F, 48*fc51490bSJonas Devlieghere FileSystemStatCache *Cache, 49*fc51490bSJonas Devlieghere llvm::vfs::FileSystem &FS) { 50dd278430SChris Lattner LookupResult R; 513b779379SArgyrios Kyrtzidis bool isForDir = !isFile; 52dd278430SChris Lattner 535ea7d07dSChris Lattner // If we have a cache, use it to resolve the stat query. 54dd278430SChris Lattner if (Cache) 55c8130a74SBen Langmuir R = Cache->getStat(Path, Data, isFile, F, FS); 56c8130a74SBen Langmuir else if (isForDir || !F) { 573b779379SArgyrios Kyrtzidis // If this is a directory or a file descriptor is not needed and we have 583b779379SArgyrios Kyrtzidis // no cache, just go to the file system. 59*fc51490bSJonas Devlieghere llvm::ErrorOr<llvm::vfs::Status> Status = FS.status(Path); 60c8130a74SBen Langmuir if (!Status) { 61f8f91b89SRafael Espindola R = CacheMissing; 62f8f91b89SRafael Espindola } else { 63f8f91b89SRafael Espindola R = CacheExists; 64c8130a74SBen Langmuir copyStatusToFileData(*Status, Data); 65f8f91b89SRafael Espindola } 665ea7d07dSChris Lattner } else { 675ea7d07dSChris Lattner // Otherwise, we have to go to the filesystem. We can always just use 685ea7d07dSChris Lattner // 'stat' here, but (for files) the client is asking whether the file exists 695ea7d07dSChris Lattner // because it wants to turn around and *open* it. It is more efficient to 705ea7d07dSChris Lattner // do "open+fstat" on success than it is to do "stat+open". 715ea7d07dSChris Lattner // 725ea7d07dSChris Lattner // Because of this, check to see if the file exists with 'open'. If the 735ea7d07dSChris Lattner // open succeeds, use fstat to get the stat info. 74a885796dSBenjamin Kramer auto OwnedFile = FS.openFileForRead(Path); 755ea7d07dSChris Lattner 76a885796dSBenjamin Kramer if (!OwnedFile) { 775ea7d07dSChris Lattner // If the open fails, our "stat" fails. 785ea7d07dSChris Lattner R = CacheMissing; 795ea7d07dSChris Lattner } else { 805ea7d07dSChris Lattner // Otherwise, the open succeeded. Do an fstat to get the information 815ea7d07dSChris Lattner // about the file. We'll end up returning the open file descriptor to the 825ea7d07dSChris Lattner // client to do what they please with it. 83*fc51490bSJonas Devlieghere llvm::ErrorOr<llvm::vfs::Status> Status = (*OwnedFile)->status(); 84c8130a74SBen Langmuir if (Status) { 855ea7d07dSChris Lattner R = CacheExists; 86c8130a74SBen Langmuir copyStatusToFileData(*Status, Data); 87a885796dSBenjamin Kramer *F = std::move(*OwnedFile); 88f8f91b89SRafael Espindola } else { 895ea7d07dSChris Lattner // fstat rarely fails. If it does, claim the initial open didn't 905ea7d07dSChris Lattner // succeed. 915ea7d07dSChris Lattner R = CacheMissing; 92f1186c5aSCraig Topper *F = nullptr; 935ea7d07dSChris Lattner } 945ea7d07dSChris Lattner } 955ea7d07dSChris Lattner } 96dd278430SChris Lattner 97f77e11baSChris Lattner // If the path doesn't exist, return failure. 98dd278430SChris Lattner if (R == CacheMissing) return true; 99dd278430SChris Lattner 100f77e11baSChris Lattner // If the path exists, make sure that its "directoryness" matches the clients 101f77e11baSChris Lattner // demands. 102f8f91b89SRafael Espindola if (Data.IsDirectory != isForDir) { 103f77e11baSChris Lattner // If not, close the file if opened. 104326ffb36SDavid Blaikie if (F) 105f1186c5aSCraig Topper *F = nullptr; 106f77e11baSChris Lattner 107f77e11baSChris Lattner return true; 108f77e11baSChris Lattner } 109f77e11baSChris Lattner 110f77e11baSChris Lattner return false; 111dd278430SChris Lattner } 112dd278430SChris Lattner 113226efd35SChris Lattner MemorizeStatCalls::LookupResult 1140df59d8cSMehdi Amini MemorizeStatCalls::getStat(StringRef Path, FileData &Data, bool isFile, 115*fc51490bSJonas Devlieghere std::unique_ptr<llvm::vfs::File> *F, 116*fc51490bSJonas Devlieghere llvm::vfs::FileSystem &FS) { 117c8130a74SBen Langmuir LookupResult Result = statChained(Path, Data, isFile, F, FS); 118226efd35SChris Lattner 119226efd35SChris Lattner // Do not cache failed stats, it is easy to construct common inconsistent 120226efd35SChris Lattner // situations if we do, and they are not important for PCH performance (which 121226efd35SChris Lattner // currently only needs the stats to construct the initial FileManager 122226efd35SChris Lattner // entries). 1238f0583daSChris Lattner if (Result == CacheMissing) 124226efd35SChris Lattner return Result; 125226efd35SChris Lattner 126226efd35SChris Lattner // Cache file 'stat' results and directories with absolutely paths. 127f8f91b89SRafael Espindola if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path)) 128f8f91b89SRafael Espindola StatCalls[Path] = Data; 129226efd35SChris Lattner 130226efd35SChris Lattner return Result; 131226efd35SChris Lattner } 132