xref: /src/contrib/llvm-project/clang/lib/Basic/FileSystemStatCache.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
148675466SDimitry Andric //===- FileSystemStatCache.cpp - Caching for 'stat' calls -----------------===//
2bca07a45SDimitry Andric //
322989816SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
422989816SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
522989816SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bca07a45SDimitry Andric //
7bca07a45SDimitry Andric //===----------------------------------------------------------------------===//
8bca07a45SDimitry Andric //
9bca07a45SDimitry Andric //  This file defines the FileSystemStatCache interface.
10bca07a45SDimitry Andric //
11bca07a45SDimitry Andric //===----------------------------------------------------------------------===//
12bca07a45SDimitry Andric 
13bca07a45SDimitry Andric #include "clang/Basic/FileSystemStatCache.h"
1448675466SDimitry Andric #include "llvm/Support/Chrono.h"
1548675466SDimitry Andric #include "llvm/Support/ErrorOr.h"
16bca07a45SDimitry Andric #include "llvm/Support/Path.h"
17676fbe81SDimitry Andric #include "llvm/Support/VirtualFileSystem.h"
1848675466SDimitry Andric #include <utility>
19bca07a45SDimitry Andric 
20bca07a45SDimitry Andric using namespace clang;
21bca07a45SDimitry Andric 
anchor()22dbe13110SDimitry Andric void FileSystemStatCache::anchor() {}
23dbe13110SDimitry Andric 
24bca07a45SDimitry Andric /// FileSystemStatCache::get - Get the 'stat' information for the specified
25bca07a45SDimitry Andric /// path, using the cache to accelerate it if possible.  This returns true if
26bca07a45SDimitry Andric /// the path does not exist or false if it exists.
27bca07a45SDimitry Andric ///
28809500fcSDimitry Andric /// If isFile is true, then this lookup should only return success for files
29809500fcSDimitry Andric /// (not directories).  If it is false this lookup should only return
30bca07a45SDimitry Andric /// success for directories (not files).  On a successful file lookup, the
31bca07a45SDimitry Andric /// implementation can optionally fill in FileDescriptor with a valid
32bca07a45SDimitry Andric /// descriptor and the client guarantees that it will close it.
3322989816SDimitry 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)3422989816SDimitry Andric FileSystemStatCache::get(StringRef Path, llvm::vfs::Status &Status,
3522989816SDimitry Andric                          bool isFile, std::unique_ptr<llvm::vfs::File> *F,
36676fbe81SDimitry Andric                          FileSystemStatCache *Cache,
37676fbe81SDimitry Andric                          llvm::vfs::FileSystem &FS) {
38809500fcSDimitry Andric   bool isForDir = !isFile;
3922989816SDimitry Andric   std::error_code RetCode;
40bca07a45SDimitry Andric 
41bca07a45SDimitry Andric   // If we have a cache, use it to resolve the stat query.
42bca07a45SDimitry Andric   if (Cache)
4322989816SDimitry Andric     RetCode = Cache->getStat(Path, Status, isFile, F, FS);
449f4dbff6SDimitry Andric   else if (isForDir || !F) {
45809500fcSDimitry Andric     // If this is a directory or a file descriptor is not needed and we have
46809500fcSDimitry Andric     // no cache, just go to the file system.
4722989816SDimitry Andric     llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = FS.status(Path);
4822989816SDimitry Andric     if (!StatusOrErr) {
4922989816SDimitry Andric       RetCode = StatusOrErr.getError();
50bfef3995SDimitry Andric     } else {
5122989816SDimitry Andric       Status = *StatusOrErr;
52bfef3995SDimitry Andric     }
53bca07a45SDimitry Andric   } else {
54bca07a45SDimitry Andric     // Otherwise, we have to go to the filesystem.  We can always just use
55bca07a45SDimitry Andric     // 'stat' here, but (for files) the client is asking whether the file exists
56bca07a45SDimitry Andric     // because it wants to turn around and *open* it.  It is more efficient to
57bca07a45SDimitry Andric     // do "open+fstat" on success than it is to do "stat+open".
58bca07a45SDimitry Andric     //
59bca07a45SDimitry Andric     // Because of this, check to see if the file exists with 'open'.  If the
60bca07a45SDimitry Andric     // open succeeds, use fstat to get the stat info.
6106d4ba38SDimitry Andric     auto OwnedFile = FS.openFileForRead(Path);
62bca07a45SDimitry Andric 
6306d4ba38SDimitry Andric     if (!OwnedFile) {
64bca07a45SDimitry Andric       // If the open fails, our "stat" fails.
6522989816SDimitry Andric       RetCode = OwnedFile.getError();
66bca07a45SDimitry Andric     } else {
67bca07a45SDimitry Andric       // Otherwise, the open succeeded.  Do an fstat to get the information
68bca07a45SDimitry Andric       // about the file.  We'll end up returning the open file descriptor to the
69bca07a45SDimitry Andric       // client to do what they please with it.
7022989816SDimitry Andric       llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = (*OwnedFile)->status();
7122989816SDimitry Andric       if (StatusOrErr) {
7222989816SDimitry Andric         Status = *StatusOrErr;
7306d4ba38SDimitry Andric         *F = std::move(*OwnedFile);
74bfef3995SDimitry Andric       } else {
75bca07a45SDimitry Andric         // fstat rarely fails.  If it does, claim the initial open didn't
76bca07a45SDimitry Andric         // succeed.
779f4dbff6SDimitry Andric         *F = nullptr;
7822989816SDimitry Andric         RetCode = StatusOrErr.getError();
79bca07a45SDimitry Andric       }
80bca07a45SDimitry Andric     }
81bca07a45SDimitry Andric   }
82bca07a45SDimitry Andric 
83bca07a45SDimitry Andric   // If the path doesn't exist, return failure.
8422989816SDimitry Andric   if (RetCode)
8522989816SDimitry Andric     return RetCode;
86bca07a45SDimitry Andric 
87bca07a45SDimitry Andric   // If the path exists, make sure that its "directoryness" matches the clients
88bca07a45SDimitry Andric   // demands.
8922989816SDimitry Andric   if (Status.isDirectory() != isForDir) {
90bca07a45SDimitry Andric     // If not, close the file if opened.
919f4dbff6SDimitry Andric     if (F)
929f4dbff6SDimitry Andric       *F = nullptr;
9322989816SDimitry Andric     return std::make_error_code(
9422989816SDimitry Andric         Status.isDirectory() ?
9522989816SDimitry Andric             std::errc::is_a_directory : std::errc::not_a_directory);
96bca07a45SDimitry Andric   }
97bca07a45SDimitry Andric 
9822989816SDimitry Andric   return std::error_code();
99bca07a45SDimitry Andric }
100bca07a45SDimitry Andric 
10122989816SDimitry Andric std::error_code
getStat(StringRef Path,llvm::vfs::Status & Status,bool isFile,std::unique_ptr<llvm::vfs::File> * F,llvm::vfs::FileSystem & FS)10222989816SDimitry Andric MemorizeStatCalls::getStat(StringRef Path, llvm::vfs::Status &Status,
10322989816SDimitry Andric                            bool isFile,
104676fbe81SDimitry Andric                            std::unique_ptr<llvm::vfs::File> *F,
105676fbe81SDimitry Andric                            llvm::vfs::FileSystem &FS) {
10622989816SDimitry Andric   auto err = get(Path, Status, isFile, F, nullptr, FS);
10722989816SDimitry Andric   if (err) {
108bca07a45SDimitry Andric     // Do not cache failed stats, it is easy to construct common inconsistent
109676fbe81SDimitry Andric     // situations if we do, and they are not important for PCH performance
110676fbe81SDimitry Andric     // (which currently only needs the stats to construct the initial
111676fbe81SDimitry Andric     // FileManager entries).
11222989816SDimitry Andric     return err;
113676fbe81SDimitry Andric   }
114bca07a45SDimitry Andric 
115bca07a45SDimitry Andric   // Cache file 'stat' results and directories with absolutely paths.
11622989816SDimitry Andric   if (!Status.isDirectory() || llvm::sys::path::is_absolute(Path))
11722989816SDimitry Andric     StatCalls[Path] = Status;
118bca07a45SDimitry Andric 
11922989816SDimitry Andric   return std::error_code();
120bca07a45SDimitry Andric }
121