Path: blob/main/contrib/llvm-project/clang/lib/Basic/FileSystemStatCache.cpp
35232 views
//===- FileSystemStatCache.cpp - Caching for 'stat' calls -----------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines the FileSystemStatCache interface.9//10//===----------------------------------------------------------------------===//1112#include "clang/Basic/FileSystemStatCache.h"13#include "llvm/Support/Chrono.h"14#include "llvm/Support/ErrorOr.h"15#include "llvm/Support/Path.h"16#include "llvm/Support/VirtualFileSystem.h"17#include <utility>1819using namespace clang;2021void FileSystemStatCache::anchor() {}2223/// FileSystemStatCache::get - Get the 'stat' information for the specified24/// path, using the cache to accelerate it if possible. This returns true if25/// the path does not exist or false if it exists.26///27/// If isFile is true, then this lookup should only return success for files28/// (not directories). If it is false this lookup should only return29/// success for directories (not files). On a successful file lookup, the30/// implementation can optionally fill in FileDescriptor with a valid31/// descriptor and the client guarantees that it will close it.32std::error_code33FileSystemStatCache::get(StringRef Path, llvm::vfs::Status &Status,34bool isFile, std::unique_ptr<llvm::vfs::File> *F,35FileSystemStatCache *Cache,36llvm::vfs::FileSystem &FS) {37bool isForDir = !isFile;38std::error_code RetCode;3940// If we have a cache, use it to resolve the stat query.41if (Cache)42RetCode = Cache->getStat(Path, Status, isFile, F, FS);43else if (isForDir || !F) {44// If this is a directory or a file descriptor is not needed and we have45// no cache, just go to the file system.46llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = FS.status(Path);47if (!StatusOrErr) {48RetCode = StatusOrErr.getError();49} else {50Status = *StatusOrErr;51}52} else {53// Otherwise, we have to go to the filesystem. We can always just use54// 'stat' here, but (for files) the client is asking whether the file exists55// because it wants to turn around and *open* it. It is more efficient to56// do "open+fstat" on success than it is to do "stat+open".57//58// Because of this, check to see if the file exists with 'open'. If the59// open succeeds, use fstat to get the stat info.60auto OwnedFile = FS.openFileForRead(Path);6162if (!OwnedFile) {63// If the open fails, our "stat" fails.64RetCode = OwnedFile.getError();65} else {66// Otherwise, the open succeeded. Do an fstat to get the information67// about the file. We'll end up returning the open file descriptor to the68// client to do what they please with it.69llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = (*OwnedFile)->status();70if (StatusOrErr) {71Status = *StatusOrErr;72*F = std::move(*OwnedFile);73} else {74// fstat rarely fails. If it does, claim the initial open didn't75// succeed.76*F = nullptr;77RetCode = StatusOrErr.getError();78}79}80}8182// If the path doesn't exist, return failure.83if (RetCode)84return RetCode;8586// If the path exists, make sure that its "directoryness" matches the clients87// demands.88if (Status.isDirectory() != isForDir) {89// If not, close the file if opened.90if (F)91*F = nullptr;92return std::make_error_code(93Status.isDirectory() ?94std::errc::is_a_directory : std::errc::not_a_directory);95}9697return std::error_code();98}99100std::error_code101MemorizeStatCalls::getStat(StringRef Path, llvm::vfs::Status &Status,102bool isFile,103std::unique_ptr<llvm::vfs::File> *F,104llvm::vfs::FileSystem &FS) {105auto err = get(Path, Status, isFile, F, nullptr, FS);106if (err) {107// Do not cache failed stats, it is easy to construct common inconsistent108// situations if we do, and they are not important for PCH performance109// (which currently only needs the stats to construct the initial110// FileManager entries).111return err;112}113114// Cache file 'stat' results and directories with absolutely paths.115if (!Status.isDirectory() || llvm::sys::path::is_absolute(Path))116StatCalls[Path] = Status;117118return std::error_code();119}120121122