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" 15c8130a74SBen Langmuir #include "clang/Basic/VirtualFileSystem.h" 168aaf4995SMichael J. Spencer #include "llvm/Support/Path.h" 17f77e11baSChris Lattner 18226efd35SChris Lattner using namespace clang; 19226efd35SChris Lattner 2068e081d6SDavid Blaikie void FileSystemStatCache::anchor() { } 2168e081d6SDavid Blaikie 22c8130a74SBen Langmuir static void copyStatusToFileData(const vfs::Status &Status, 23f8f91b89SRafael Espindola FileData &Data) { 24d066d4c8SBen Langmuir Data.Name = Status.getName(); 25f8f91b89SRafael Espindola Data.Size = Status.getSize(); 26*ac71c8e2SPavel Labath Data.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime()); 27f8f91b89SRafael Espindola Data.UniqueID = Status.getUniqueID(); 28c8130a74SBen Langmuir Data.IsDirectory = Status.isDirectory(); 29c8130a74SBen Langmuir Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file; 30f8f91b89SRafael Espindola Data.InPCH = false; 315de00f3bSBen Langmuir Data.IsVFSMapped = Status.IsVFSMapped; 32f8f91b89SRafael Espindola } 33f8f91b89SRafael Espindola 34dd278430SChris Lattner /// FileSystemStatCache::get - Get the 'stat' information for the specified 354fc8fb09SChris Lattner /// path, using the cache to accelerate it if possible. This returns true if 36dd278430SChris Lattner /// the path does not exist or false if it exists. 37dd278430SChris Lattner /// 383b779379SArgyrios Kyrtzidis /// If isFile is true, then this lookup should only return success for files 393b779379SArgyrios Kyrtzidis /// (not directories). If it is false this lookup should only return 40dd278430SChris Lattner /// success for directories (not files). On a successful file lookup, the 41dd278430SChris Lattner /// implementation can optionally fill in FileDescriptor with a valid 42dd278430SChris Lattner /// descriptor and the client guarantees that it will close it. 430df59d8cSMehdi Amini bool FileSystemStatCache::get(StringRef Path, FileData &Data, bool isFile, 44326ffb36SDavid Blaikie std::unique_ptr<vfs::File> *F, 45326ffb36SDavid Blaikie FileSystemStatCache *Cache, vfs::FileSystem &FS) { 46dd278430SChris Lattner LookupResult R; 473b779379SArgyrios Kyrtzidis bool isForDir = !isFile; 48dd278430SChris Lattner 495ea7d07dSChris Lattner // If we have a cache, use it to resolve the stat query. 50dd278430SChris Lattner if (Cache) 51c8130a74SBen Langmuir R = Cache->getStat(Path, Data, isFile, F, FS); 52c8130a74SBen Langmuir else if (isForDir || !F) { 533b779379SArgyrios Kyrtzidis // If this is a directory or a file descriptor is not needed and we have 543b779379SArgyrios Kyrtzidis // no cache, just go to the file system. 55c8130a74SBen Langmuir llvm::ErrorOr<vfs::Status> Status = FS.status(Path); 56c8130a74SBen Langmuir if (!Status) { 57f8f91b89SRafael Espindola R = CacheMissing; 58f8f91b89SRafael Espindola } else { 59f8f91b89SRafael Espindola R = CacheExists; 60c8130a74SBen Langmuir copyStatusToFileData(*Status, Data); 61f8f91b89SRafael Espindola } 625ea7d07dSChris Lattner } else { 635ea7d07dSChris Lattner // Otherwise, we have to go to the filesystem. We can always just use 645ea7d07dSChris Lattner // 'stat' here, but (for files) the client is asking whether the file exists 655ea7d07dSChris Lattner // because it wants to turn around and *open* it. It is more efficient to 665ea7d07dSChris Lattner // do "open+fstat" on success than it is to do "stat+open". 675ea7d07dSChris Lattner // 685ea7d07dSChris Lattner // Because of this, check to see if the file exists with 'open'. If the 695ea7d07dSChris Lattner // open succeeds, use fstat to get the stat info. 70a885796dSBenjamin Kramer auto OwnedFile = FS.openFileForRead(Path); 715ea7d07dSChris Lattner 72a885796dSBenjamin Kramer if (!OwnedFile) { 735ea7d07dSChris Lattner // If the open fails, our "stat" fails. 745ea7d07dSChris Lattner R = CacheMissing; 755ea7d07dSChris Lattner } else { 765ea7d07dSChris Lattner // Otherwise, the open succeeded. Do an fstat to get the information 775ea7d07dSChris Lattner // about the file. We'll end up returning the open file descriptor to the 785ea7d07dSChris Lattner // client to do what they please with it. 79a885796dSBenjamin Kramer llvm::ErrorOr<vfs::Status> Status = (*OwnedFile)->status(); 80c8130a74SBen Langmuir if (Status) { 815ea7d07dSChris Lattner R = CacheExists; 82c8130a74SBen Langmuir copyStatusToFileData(*Status, Data); 83a885796dSBenjamin Kramer *F = std::move(*OwnedFile); 84f8f91b89SRafael Espindola } else { 855ea7d07dSChris Lattner // fstat rarely fails. If it does, claim the initial open didn't 865ea7d07dSChris Lattner // succeed. 875ea7d07dSChris Lattner R = CacheMissing; 88f1186c5aSCraig Topper *F = nullptr; 895ea7d07dSChris Lattner } 905ea7d07dSChris Lattner } 915ea7d07dSChris Lattner } 92dd278430SChris Lattner 93f77e11baSChris Lattner // If the path doesn't exist, return failure. 94dd278430SChris Lattner if (R == CacheMissing) return true; 95dd278430SChris Lattner 96f77e11baSChris Lattner // If the path exists, make sure that its "directoryness" matches the clients 97f77e11baSChris Lattner // demands. 98f8f91b89SRafael Espindola if (Data.IsDirectory != isForDir) { 99f77e11baSChris Lattner // If not, close the file if opened. 100326ffb36SDavid Blaikie if (F) 101f1186c5aSCraig Topper *F = nullptr; 102f77e11baSChris Lattner 103f77e11baSChris Lattner return true; 104f77e11baSChris Lattner } 105f77e11baSChris Lattner 106f77e11baSChris Lattner return false; 107dd278430SChris Lattner } 108dd278430SChris Lattner 109226efd35SChris Lattner MemorizeStatCalls::LookupResult 1100df59d8cSMehdi Amini MemorizeStatCalls::getStat(StringRef Path, FileData &Data, bool isFile, 111326ffb36SDavid Blaikie std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) { 112c8130a74SBen Langmuir LookupResult Result = statChained(Path, Data, isFile, F, FS); 113226efd35SChris Lattner 114226efd35SChris Lattner // Do not cache failed stats, it is easy to construct common inconsistent 115226efd35SChris Lattner // situations if we do, and they are not important for PCH performance (which 116226efd35SChris Lattner // currently only needs the stats to construct the initial FileManager 117226efd35SChris Lattner // entries). 1188f0583daSChris Lattner if (Result == CacheMissing) 119226efd35SChris Lattner return Result; 120226efd35SChris Lattner 121226efd35SChris Lattner // Cache file 'stat' results and directories with absolutely paths. 122f8f91b89SRafael Espindola if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path)) 123f8f91b89SRafael Espindola StatCalls[Path] = Data; 124226efd35SChris Lattner 125226efd35SChris Lattner return Result; 126226efd35SChris Lattner } 127