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