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