1 //===- FileSystemStatCache.cpp - Caching for 'stat' calls -----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the FileSystemStatCache interface. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Basic/FileSystemStatCache.h" 14 #include "llvm/Support/Chrono.h" 15 #include "llvm/Support/ErrorOr.h" 16 #include "llvm/Support/Path.h" 17 #include "llvm/Support/VirtualFileSystem.h" 18 #include <utility> 19 20 using namespace clang; 21 22 void FileSystemStatCache::anchor() {} 23 24 /// FileSystemStatCache::get - Get the 'stat' information for the specified 25 /// path, using the cache to accelerate it if possible. This returns true if 26 /// the path does not exist or false if it exists. 27 /// 28 /// If isFile is true, then this lookup should only return success for files 29 /// (not directories). If it is false this lookup should only return 30 /// success for directories (not files). On a successful file lookup, the 31 /// implementation can optionally fill in FileDescriptor with a valid 32 /// descriptor and the client guarantees that it will close it. 33 std::error_code FileSystemStatCache::get(StringRef Path, 34 llvm::vfs::Status &Status, bool isFile, 35 std::unique_ptr<llvm::vfs::File> *F, 36 FileSystemStatCache *Cache, 37 llvm::vfs::FileSystem &FS, 38 bool IsText) { 39 bool isForDir = !isFile; 40 std::error_code RetCode; 41 42 // If we have a cache, use it to resolve the stat query. 43 if (Cache) 44 RetCode = Cache->getStat(Path, Status, isFile, F, FS); 45 else if (isForDir || !F) { 46 // If this is a directory or a file descriptor is not needed and we have 47 // no cache, just go to the file system. 48 llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = FS.status(Path); 49 if (!StatusOrErr) { 50 RetCode = StatusOrErr.getError(); 51 } else { 52 Status = *StatusOrErr; 53 } 54 } else { 55 // Otherwise, we have to go to the filesystem. We can always just use 56 // 'stat' here, but (for files) the client is asking whether the file exists 57 // because it wants to turn around and *open* it. It is more efficient to 58 // do "open+fstat" on success than it is to do "stat+open". 59 // 60 // Because of this, check to see if the file exists with 'open'. If the 61 // open succeeds, use fstat to get the stat info. 62 auto OwnedFile = 63 IsText ? FS.openFileForRead(Path) : FS.openFileForReadBinary(Path); 64 65 if (!OwnedFile) { 66 // If the open fails, our "stat" fails. 67 RetCode = OwnedFile.getError(); 68 } else { 69 // Otherwise, the open succeeded. Do an fstat to get the information 70 // about the file. We'll end up returning the open file descriptor to the 71 // client to do what they please with it. 72 llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = (*OwnedFile)->status(); 73 if (StatusOrErr) { 74 Status = *StatusOrErr; 75 *F = std::move(*OwnedFile); 76 } else { 77 // fstat rarely fails. If it does, claim the initial open didn't 78 // succeed. 79 *F = nullptr; 80 RetCode = StatusOrErr.getError(); 81 } 82 } 83 } 84 85 // If the path doesn't exist, return failure. 86 if (RetCode) 87 return RetCode; 88 89 // If the path exists, make sure that its "directoryness" matches the clients 90 // demands. 91 if (Status.isDirectory() != isForDir) { 92 // If not, close the file if opened. 93 if (F) 94 *F = nullptr; 95 return std::make_error_code( 96 Status.isDirectory() ? 97 std::errc::is_a_directory : std::errc::not_a_directory); 98 } 99 100 return std::error_code(); 101 } 102 103 std::error_code 104 MemorizeStatCalls::getStat(StringRef Path, llvm::vfs::Status &Status, 105 bool isFile, 106 std::unique_ptr<llvm::vfs::File> *F, 107 llvm::vfs::FileSystem &FS) { 108 auto err = get(Path, Status, isFile, F, nullptr, FS); 109 if (err) { 110 // Do not cache failed stats, it is easy to construct common inconsistent 111 // situations if we do, and they are not important for PCH performance 112 // (which currently only needs the stats to construct the initial 113 // FileManager entries). 114 return err; 115 } 116 117 // Cache file 'stat' results and directories with absolutely paths. 118 if (!Status.isDirectory() || llvm::sys::path::is_absolute(Path)) 119 StatCalls[Path] = Status; 120 121 return std::error_code(); 122 } 123