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