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