15a52011cSEugene Zelenko //===- FileSystemStatCache.cpp - Caching for 'stat' calls -----------------===// 2226efd35SChris Lattner // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler 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 24dd278430SChris Lattner /// FileSystemStatCache::get - Get the 'stat' information for the specified 254fc8fb09SChris Lattner /// path, using the cache to accelerate it if possible. This returns true if 26dd278430SChris Lattner /// the path does not exist or false if it exists. 27dd278430SChris Lattner /// 283b779379SArgyrios Kyrtzidis /// If isFile is true, then this lookup should only return success for files 293b779379SArgyrios Kyrtzidis /// (not directories). If it is false this lookup should only return 30dd278430SChris Lattner /// success for directories (not files). On a successful file lookup, the 31dd278430SChris Lattner /// implementation can optionally fill in FileDescriptor with a valid 32dd278430SChris Lattner /// descriptor and the client guarantees that it will close it. 33*06f64d53SHarlan Haskins bool FileSystemStatCache::get(StringRef Path, llvm::vfs::Status &Status, 34*06f64d53SHarlan Haskins bool isFile, 35fc51490bSJonas Devlieghere std::unique_ptr<llvm::vfs::File> *F, 36fc51490bSJonas Devlieghere FileSystemStatCache *Cache, 37fc51490bSJonas Devlieghere llvm::vfs::FileSystem &FS) { 38dd278430SChris Lattner LookupResult R; 393b779379SArgyrios Kyrtzidis bool isForDir = !isFile; 40dd278430SChris Lattner 415ea7d07dSChris Lattner // If we have a cache, use it to resolve the stat query. 42dd278430SChris Lattner if (Cache) 43*06f64d53SHarlan Haskins R = Cache->getStat(Path, Status, isFile, F, FS); 44c8130a74SBen Langmuir else if (isForDir || !F) { 453b779379SArgyrios Kyrtzidis // If this is a directory or a file descriptor is not needed and we have 463b779379SArgyrios Kyrtzidis // no cache, just go to the file system. 47*06f64d53SHarlan Haskins llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = FS.status(Path); 48*06f64d53SHarlan Haskins if (!StatusOrErr) { 49f8f91b89SRafael Espindola R = CacheMissing; 50f8f91b89SRafael Espindola } else { 51f8f91b89SRafael Espindola R = CacheExists; 52*06f64d53SHarlan Haskins Status = *StatusOrErr; 53f8f91b89SRafael Espindola } 545ea7d07dSChris Lattner } else { 555ea7d07dSChris Lattner // Otherwise, we have to go to the filesystem. We can always just use 565ea7d07dSChris Lattner // 'stat' here, but (for files) the client is asking whether the file exists 575ea7d07dSChris Lattner // because it wants to turn around and *open* it. It is more efficient to 585ea7d07dSChris Lattner // do "open+fstat" on success than it is to do "stat+open". 595ea7d07dSChris Lattner // 605ea7d07dSChris Lattner // Because of this, check to see if the file exists with 'open'. If the 615ea7d07dSChris Lattner // open succeeds, use fstat to get the stat info. 62a885796dSBenjamin Kramer auto OwnedFile = FS.openFileForRead(Path); 635ea7d07dSChris Lattner 64a885796dSBenjamin Kramer if (!OwnedFile) { 655ea7d07dSChris Lattner // If the open fails, our "stat" fails. 665ea7d07dSChris Lattner R = CacheMissing; 675ea7d07dSChris Lattner } else { 685ea7d07dSChris Lattner // Otherwise, the open succeeded. Do an fstat to get the information 695ea7d07dSChris Lattner // about the file. We'll end up returning the open file descriptor to the 705ea7d07dSChris Lattner // client to do what they please with it. 71*06f64d53SHarlan Haskins llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = (*OwnedFile)->status(); 72*06f64d53SHarlan Haskins if (StatusOrErr) { 735ea7d07dSChris Lattner R = CacheExists; 74*06f64d53SHarlan Haskins Status = *StatusOrErr; 75a885796dSBenjamin Kramer *F = std::move(*OwnedFile); 76f8f91b89SRafael Espindola } else { 775ea7d07dSChris Lattner // fstat rarely fails. If it does, claim the initial open didn't 785ea7d07dSChris Lattner // succeed. 795ea7d07dSChris Lattner R = CacheMissing; 80f1186c5aSCraig Topper *F = nullptr; 815ea7d07dSChris Lattner } 825ea7d07dSChris Lattner } 835ea7d07dSChris Lattner } 84dd278430SChris Lattner 85f77e11baSChris Lattner // If the path doesn't exist, return failure. 86dd278430SChris Lattner if (R == CacheMissing) return true; 87dd278430SChris Lattner 88f77e11baSChris Lattner // If the path exists, make sure that its "directoryness" matches the clients 89f77e11baSChris Lattner // demands. 90*06f64d53SHarlan Haskins if (Status.isDirectory() != isForDir) { 91f77e11baSChris Lattner // If not, close the file if opened. 92326ffb36SDavid Blaikie if (F) 93f1186c5aSCraig Topper *F = nullptr; 94f77e11baSChris Lattner 95f77e11baSChris Lattner return true; 96f77e11baSChris Lattner } 97f77e11baSChris Lattner 98f77e11baSChris Lattner return false; 99dd278430SChris Lattner } 100dd278430SChris Lattner 101226efd35SChris Lattner MemorizeStatCalls::LookupResult 102*06f64d53SHarlan Haskins MemorizeStatCalls::getStat(StringRef Path, llvm::vfs::Status &Status, 103*06f64d53SHarlan Haskins bool isFile, 104fc51490bSJonas Devlieghere std::unique_ptr<llvm::vfs::File> *F, 105fc51490bSJonas Devlieghere llvm::vfs::FileSystem &FS) { 106*06f64d53SHarlan Haskins if (get(Path, Status, isFile, F, nullptr, FS)) { 107226efd35SChris Lattner // Do not cache failed stats, it is easy to construct common inconsistent 108d92b1ae1SAlex Lorenz // situations if we do, and they are not important for PCH performance 109d92b1ae1SAlex Lorenz // (which currently only needs the stats to construct the initial 110d92b1ae1SAlex Lorenz // FileManager entries). 111d92b1ae1SAlex Lorenz return CacheMissing; 112d92b1ae1SAlex Lorenz } 113226efd35SChris Lattner 114226efd35SChris Lattner // Cache file 'stat' results and directories with absolutely paths. 115*06f64d53SHarlan Haskins if (!Status.isDirectory() || llvm::sys::path::is_absolute(Path)) 116*06f64d53SHarlan Haskins StatCalls[Path] = Status; 117226efd35SChris Lattner 118d92b1ae1SAlex Lorenz return CacheExists; 119226efd35SChris Lattner } 120