llvm-project
120 строк · 4.5 Кб
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
20using namespace clang;
21
22void 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.
33std::error_code
34FileSystemStatCache::get(StringRef Path, llvm::vfs::Status &Status,
35bool isFile, std::unique_ptr<llvm::vfs::File> *F,
36FileSystemStatCache *Cache,
37llvm::vfs::FileSystem &FS) {
38bool isForDir = !isFile;
39std::error_code RetCode;
40
41// If we have a cache, use it to resolve the stat query.
42if (Cache)
43RetCode = Cache->getStat(Path, Status, isFile, F, FS);
44else if (isForDir || !F) {
45// If this is a directory or a file descriptor is not needed and we have
46// no cache, just go to the file system.
47llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = FS.status(Path);
48if (!StatusOrErr) {
49RetCode = StatusOrErr.getError();
50} else {
51Status = *StatusOrErr;
52}
53} else {
54// Otherwise, we have to go to the filesystem. We can always just use
55// 'stat' here, but (for files) the client is asking whether the file exists
56// because it wants to turn around and *open* it. It is more efficient to
57// do "open+fstat" on success than it is to do "stat+open".
58//
59// Because of this, check to see if the file exists with 'open'. If the
60// open succeeds, use fstat to get the stat info.
61auto OwnedFile = FS.openFileForRead(Path);
62
63if (!OwnedFile) {
64// If the open fails, our "stat" fails.
65RetCode = OwnedFile.getError();
66} else {
67// Otherwise, the open succeeded. Do an fstat to get the information
68// about the file. We'll end up returning the open file descriptor to the
69// client to do what they please with it.
70llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = (*OwnedFile)->status();
71if (StatusOrErr) {
72Status = *StatusOrErr;
73*F = std::move(*OwnedFile);
74} else {
75// fstat rarely fails. If it does, claim the initial open didn't
76// succeed.
77*F = nullptr;
78RetCode = StatusOrErr.getError();
79}
80}
81}
82
83// If the path doesn't exist, return failure.
84if (RetCode)
85return RetCode;
86
87// If the path exists, make sure that its "directoryness" matches the clients
88// demands.
89if (Status.isDirectory() != isForDir) {
90// If not, close the file if opened.
91if (F)
92*F = nullptr;
93return std::make_error_code(
94Status.isDirectory() ?
95std::errc::is_a_directory : std::errc::not_a_directory);
96}
97
98return std::error_code();
99}
100
101std::error_code
102MemorizeStatCalls::getStat(StringRef Path, llvm::vfs::Status &Status,
103bool isFile,
104std::unique_ptr<llvm::vfs::File> *F,
105llvm::vfs::FileSystem &FS) {
106auto err = get(Path, Status, isFile, F, nullptr, FS);
107if (err) {
108// Do not cache failed stats, it is easy to construct common inconsistent
109// situations if we do, and they are not important for PCH performance
110// (which currently only needs the stats to construct the initial
111// FileManager entries).
112return err;
113}
114
115// Cache file 'stat' results and directories with absolutely paths.
116if (!Status.isDirectory() || llvm::sys::path::is_absolute(Path))
117StatCalls[Path] = Status;
118
119return std::error_code();
120}
121