15a52011cSEugene Zelenko //===- FileSystemStatCache.cpp - Caching for 'stat' calls -----------------===// 2226efd35SChris Lattner // 3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6226efd35SChris Lattner // 7226efd35SChris Lattner //===----------------------------------------------------------------------===// 8226efd35SChris Lattner // 9226efd35SChris Lattner // This file defines the FileSystemStatCache interface. 10226efd35SChris Lattner // 11226efd35SChris Lattner //===----------------------------------------------------------------------===// 12226efd35SChris Lattner 13226efd35SChris Lattner #include "clang/Basic/FileSystemStatCache.h" 145a52011cSEugene Zelenko #include "llvm/Support/Chrono.h" 155a52011cSEugene Zelenko #include "llvm/Support/ErrorOr.h" 168aaf4995SMichael J. Spencer #include "llvm/Support/Path.h" 17fc51490bSJonas Devlieghere #include "llvm/Support/VirtualFileSystem.h" 185a52011cSEugene Zelenko #include <utility> 19f77e11baSChris Lattner 20226efd35SChris Lattner using namespace clang; 21226efd35SChris Lattner 2268e081d6SDavid Blaikie void FileSystemStatCache::anchor() {} 2368e081d6SDavid Blaikie 24fc51490bSJonas Devlieghere static void copyStatusToFileData(const llvm::vfs::Status &Status, 25f8f91b89SRafael Espindola FileData &Data) { 26d066d4c8SBen Langmuir Data.Name = Status.getName(); 27f8f91b89SRafael Espindola Data.Size = Status.getSize(); 28ac71c8e2SPavel Labath Data.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime()); 29f8f91b89SRafael Espindola Data.UniqueID = Status.getUniqueID(); 30c8130a74SBen Langmuir Data.IsDirectory = Status.isDirectory(); 31c8130a74SBen Langmuir Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file; 32f8f91b89SRafael Espindola Data.InPCH = false; 335de00f3bSBen Langmuir Data.IsVFSMapped = Status.IsVFSMapped; 34f8f91b89SRafael Espindola } 35f8f91b89SRafael Espindola 36dd278430SChris Lattner /// FileSystemStatCache::get - Get the 'stat' information for the specified 374fc8fb09SChris Lattner /// path, using the cache to accelerate it if possible. This returns true if 38dd278430SChris Lattner /// the path does not exist or false if it exists. 39dd278430SChris Lattner /// 403b779379SArgyrios Kyrtzidis /// If isFile is true, then this lookup should only return success for files 413b779379SArgyrios Kyrtzidis /// (not directories). If it is false this lookup should only return 42dd278430SChris Lattner /// success for directories (not files). On a successful file lookup, the 43dd278430SChris Lattner /// implementation can optionally fill in FileDescriptor with a valid 44dd278430SChris Lattner /// descriptor and the client guarantees that it will close it. 450df59d8cSMehdi Amini bool FileSystemStatCache::get(StringRef Path, FileData &Data, bool isFile, 46fc51490bSJonas Devlieghere std::unique_ptr<llvm::vfs::File> *F, 47fc51490bSJonas Devlieghere FileSystemStatCache *Cache, 48fc51490bSJonas Devlieghere llvm::vfs::FileSystem &FS) { 49dd278430SChris Lattner LookupResult R; 503b779379SArgyrios Kyrtzidis bool isForDir = !isFile; 51dd278430SChris Lattner 525ea7d07dSChris Lattner // If we have a cache, use it to resolve the stat query. 53dd278430SChris Lattner if (Cache) 54c8130a74SBen Langmuir R = Cache->getStat(Path, Data, isFile, F, FS); 55c8130a74SBen Langmuir else if (isForDir || !F) { 563b779379SArgyrios Kyrtzidis // If this is a directory or a file descriptor is not needed and we have 573b779379SArgyrios Kyrtzidis // no cache, just go to the file system. 58fc51490bSJonas Devlieghere llvm::ErrorOr<llvm::vfs::Status> Status = FS.status(Path); 59c8130a74SBen Langmuir if (!Status) { 60f8f91b89SRafael Espindola R = CacheMissing; 61f8f91b89SRafael Espindola } else { 62f8f91b89SRafael Espindola R = CacheExists; 63c8130a74SBen Langmuir copyStatusToFileData(*Status, Data); 64f8f91b89SRafael Espindola } 655ea7d07dSChris Lattner } else { 665ea7d07dSChris Lattner // Otherwise, we have to go to the filesystem. We can always just use 675ea7d07dSChris Lattner // 'stat' here, but (for files) the client is asking whether the file exists 685ea7d07dSChris Lattner // because it wants to turn around and *open* it. It is more efficient to 695ea7d07dSChris Lattner // do "open+fstat" on success than it is to do "stat+open". 705ea7d07dSChris Lattner // 715ea7d07dSChris Lattner // Because of this, check to see if the file exists with 'open'. If the 725ea7d07dSChris Lattner // open succeeds, use fstat to get the stat info. 73a885796dSBenjamin Kramer auto OwnedFile = FS.openFileForRead(Path); 745ea7d07dSChris Lattner 75a885796dSBenjamin Kramer if (!OwnedFile) { 765ea7d07dSChris Lattner // If the open fails, our "stat" fails. 775ea7d07dSChris Lattner R = CacheMissing; 785ea7d07dSChris Lattner } else { 795ea7d07dSChris Lattner // Otherwise, the open succeeded. Do an fstat to get the information 805ea7d07dSChris Lattner // about the file. We'll end up returning the open file descriptor to the 815ea7d07dSChris Lattner // client to do what they please with it. 82fc51490bSJonas Devlieghere llvm::ErrorOr<llvm::vfs::Status> Status = (*OwnedFile)->status(); 83c8130a74SBen Langmuir if (Status) { 845ea7d07dSChris Lattner R = CacheExists; 85c8130a74SBen Langmuir copyStatusToFileData(*Status, Data); 86a885796dSBenjamin Kramer *F = std::move(*OwnedFile); 87f8f91b89SRafael Espindola } else { 885ea7d07dSChris Lattner // fstat rarely fails. If it does, claim the initial open didn't 895ea7d07dSChris Lattner // succeed. 905ea7d07dSChris Lattner R = CacheMissing; 91f1186c5aSCraig Topper *F = nullptr; 925ea7d07dSChris Lattner } 935ea7d07dSChris Lattner } 945ea7d07dSChris Lattner } 95dd278430SChris Lattner 96f77e11baSChris Lattner // If the path doesn't exist, return failure. 97dd278430SChris Lattner if (R == CacheMissing) return true; 98dd278430SChris Lattner 99f77e11baSChris Lattner // If the path exists, make sure that its "directoryness" matches the clients 100f77e11baSChris Lattner // demands. 101f8f91b89SRafael Espindola if (Data.IsDirectory != isForDir) { 102f77e11baSChris Lattner // If not, close the file if opened. 103326ffb36SDavid Blaikie if (F) 104f1186c5aSCraig Topper *F = nullptr; 105f77e11baSChris Lattner 106f77e11baSChris Lattner return true; 107f77e11baSChris Lattner } 108f77e11baSChris Lattner 109f77e11baSChris Lattner return false; 110dd278430SChris Lattner } 111dd278430SChris Lattner 112226efd35SChris Lattner MemorizeStatCalls::LookupResult 1130df59d8cSMehdi Amini MemorizeStatCalls::getStat(StringRef Path, FileData &Data, bool isFile, 114fc51490bSJonas Devlieghere std::unique_ptr<llvm::vfs::File> *F, 115fc51490bSJonas Devlieghere llvm::vfs::FileSystem &FS) { 116d92b1ae1SAlex Lorenz if (get(Path, Data, isFile, F, nullptr, FS)) { 117226efd35SChris Lattner // Do not cache failed stats, it is easy to construct common inconsistent 118d92b1ae1SAlex Lorenz // situations if we do, and they are not important for PCH performance 119d92b1ae1SAlex Lorenz // (which currently only needs the stats to construct the initial 120d92b1ae1SAlex Lorenz // FileManager entries). 121d92b1ae1SAlex Lorenz return CacheMissing; 122d92b1ae1SAlex Lorenz } 123226efd35SChris Lattner 124226efd35SChris Lattner // Cache file 'stat' results and directories with absolutely paths. 125f8f91b89SRafael Espindola if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path)) 126f8f91b89SRafael Espindola StatCalls[Path] = Data; 127226efd35SChris Lattner 128d92b1ae1SAlex Lorenz return CacheExists; 129226efd35SChris Lattner } 130