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