15a52011cSEugene Zelenko //===- FileSystemStatCache.cpp - Caching for 'stat' calls -----------------===//
2226efd35SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6226efd35SChris Lattner //
7226efd35SChris Lattner //===----------------------------------------------------------------------===//
8226efd35SChris Lattner //
9226efd35SChris Lattner // This file defines the FileSystemStatCache interface.
10226efd35SChris Lattner //
11226efd35SChris Lattner //===----------------------------------------------------------------------===//
12226efd35SChris Lattner
13226efd35SChris Lattner #include "clang/Basic/FileSystemStatCache.h"
145a52011cSEugene Zelenko #include "llvm/Support/Chrono.h"
155a52011cSEugene Zelenko #include "llvm/Support/ErrorOr.h"
168aaf4995SMichael J. Spencer #include "llvm/Support/Path.h"
17fc51490bSJonas Devlieghere #include "llvm/Support/VirtualFileSystem.h"
185a52011cSEugene Zelenko #include <utility>
19f77e11baSChris Lattner
20226efd35SChris Lattner using namespace clang;
21226efd35SChris Lattner
anchor()2268e081d6SDavid Blaikie void FileSystemStatCache::anchor() {}
2368e081d6SDavid Blaikie
24dd278430SChris Lattner /// FileSystemStatCache::get - Get the 'stat' information for the specified
254fc8fb09SChris Lattner /// path, using the cache to accelerate it if possible. This returns true if
26dd278430SChris Lattner /// the path does not exist or false if it exists.
27dd278430SChris Lattner ///
283b779379SArgyrios Kyrtzidis /// If isFile is true, then this lookup should only return success for files
293b779379SArgyrios Kyrtzidis /// (not directories). If it is false this lookup should only return
30dd278430SChris Lattner /// success for directories (not files). On a successful file lookup, the
31dd278430SChris Lattner /// implementation can optionally fill in FileDescriptor with a valid
32dd278430SChris Lattner /// descriptor and the client guarantees that it will close it.
33*eec3c0f9SHarlan Haskins 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*eec3c0f9SHarlan Haskins FileSystemStatCache::get(StringRef Path, llvm::vfs::Status &Status,
35*eec3c0f9SHarlan Haskins bool isFile, std::unique_ptr<llvm::vfs::File> *F,
36fc51490bSJonas Devlieghere FileSystemStatCache *Cache,
37fc51490bSJonas Devlieghere llvm::vfs::FileSystem &FS) {
383b779379SArgyrios Kyrtzidis bool isForDir = !isFile;
39*eec3c0f9SHarlan Haskins std::error_code RetCode;
40dd278430SChris Lattner
415ea7d07dSChris Lattner // If we have a cache, use it to resolve the stat query.
42dd278430SChris Lattner if (Cache)
43*eec3c0f9SHarlan Haskins RetCode = Cache->getStat(Path, Status, isFile, F, FS);
44c8130a74SBen Langmuir else if (isForDir || !F) {
453b779379SArgyrios Kyrtzidis // If this is a directory or a file descriptor is not needed and we have
463b779379SArgyrios Kyrtzidis // no cache, just go to the file system.
4706f64d53SHarlan Haskins llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = FS.status(Path);
4806f64d53SHarlan Haskins if (!StatusOrErr) {
49*eec3c0f9SHarlan Haskins RetCode = StatusOrErr.getError();
50f8f91b89SRafael Espindola } else {
5106f64d53SHarlan Haskins Status = *StatusOrErr;
52f8f91b89SRafael Espindola }
535ea7d07dSChris Lattner } else {
545ea7d07dSChris Lattner // Otherwise, we have to go to the filesystem. We can always just use
555ea7d07dSChris Lattner // 'stat' here, but (for files) the client is asking whether the file exists
565ea7d07dSChris Lattner // because it wants to turn around and *open* it. It is more efficient to
575ea7d07dSChris Lattner // do "open+fstat" on success than it is to do "stat+open".
585ea7d07dSChris Lattner //
595ea7d07dSChris Lattner // Because of this, check to see if the file exists with 'open'. If the
605ea7d07dSChris Lattner // open succeeds, use fstat to get the stat info.
61a885796dSBenjamin Kramer auto OwnedFile = FS.openFileForRead(Path);
625ea7d07dSChris Lattner
63a885796dSBenjamin Kramer if (!OwnedFile) {
645ea7d07dSChris Lattner // If the open fails, our "stat" fails.
65*eec3c0f9SHarlan Haskins RetCode = OwnedFile.getError();
665ea7d07dSChris Lattner } else {
675ea7d07dSChris Lattner // Otherwise, the open succeeded. Do an fstat to get the information
685ea7d07dSChris Lattner // about the file. We'll end up returning the open file descriptor to the
695ea7d07dSChris Lattner // client to do what they please with it.
7006f64d53SHarlan Haskins llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = (*OwnedFile)->status();
7106f64d53SHarlan Haskins if (StatusOrErr) {
7206f64d53SHarlan Haskins Status = *StatusOrErr;
73a885796dSBenjamin Kramer *F = std::move(*OwnedFile);
74f8f91b89SRafael Espindola } else {
755ea7d07dSChris Lattner // fstat rarely fails. If it does, claim the initial open didn't
765ea7d07dSChris Lattner // succeed.
77f1186c5aSCraig Topper *F = nullptr;
78*eec3c0f9SHarlan Haskins RetCode = StatusOrErr.getError();
795ea7d07dSChris Lattner }
805ea7d07dSChris Lattner }
815ea7d07dSChris Lattner }
82dd278430SChris Lattner
83f77e11baSChris Lattner // If the path doesn't exist, return failure.
84*eec3c0f9SHarlan Haskins if (RetCode)
85*eec3c0f9SHarlan Haskins return RetCode;
86dd278430SChris Lattner
87f77e11baSChris Lattner // If the path exists, make sure that its "directoryness" matches the clients
88f77e11baSChris Lattner // demands.
8906f64d53SHarlan Haskins if (Status.isDirectory() != isForDir) {
90f77e11baSChris Lattner // If not, close the file if opened.
91326ffb36SDavid Blaikie if (F)
92f1186c5aSCraig Topper *F = nullptr;
93*eec3c0f9SHarlan Haskins return std::make_error_code(
94*eec3c0f9SHarlan Haskins Status.isDirectory() ?
95*eec3c0f9SHarlan Haskins std::errc::is_a_directory : std::errc::not_a_directory);
96f77e11baSChris Lattner }
97f77e11baSChris Lattner
98*eec3c0f9SHarlan Haskins return std::error_code();
99dd278430SChris Lattner }
100dd278430SChris Lattner
101*eec3c0f9SHarlan Haskins std::error_code
getStat(StringRef Path,llvm::vfs::Status & Status,bool isFile,std::unique_ptr<llvm::vfs::File> * F,llvm::vfs::FileSystem & FS)10206f64d53SHarlan Haskins MemorizeStatCalls::getStat(StringRef Path, llvm::vfs::Status &Status,
10306f64d53SHarlan Haskins bool isFile,
104fc51490bSJonas Devlieghere std::unique_ptr<llvm::vfs::File> *F,
105fc51490bSJonas Devlieghere llvm::vfs::FileSystem &FS) {
106*eec3c0f9SHarlan Haskins auto err = get(Path, Status, isFile, F, nullptr, FS);
107*eec3c0f9SHarlan Haskins if (err) {
108226efd35SChris Lattner // Do not cache failed stats, it is easy to construct common inconsistent
109d92b1ae1SAlex Lorenz // situations if we do, and they are not important for PCH performance
110d92b1ae1SAlex Lorenz // (which currently only needs the stats to construct the initial
111d92b1ae1SAlex Lorenz // FileManager entries).
112*eec3c0f9SHarlan Haskins return err;
113d92b1ae1SAlex Lorenz }
114226efd35SChris Lattner
115226efd35SChris Lattner // Cache file 'stat' results and directories with absolutely paths.
11606f64d53SHarlan Haskins if (!Status.isDirectory() || llvm::sys::path::is_absolute(Path))
11706f64d53SHarlan Haskins StatCalls[Path] = Status;
118226efd35SChris Lattner
119*eec3c0f9SHarlan Haskins return std::error_code();
120226efd35SChris Lattner }
121