1*5a52011cSEugene Zelenko //===- FileSystemStatCache.cpp - Caching for 'stat' calls -----------------===//
2226efd35SChris Lattner //
3226efd35SChris Lattner //                     The LLVM Compiler Infrastructure
4226efd35SChris Lattner //
5226efd35SChris Lattner // This file is distributed under the University of Illinois Open Source
6226efd35SChris Lattner // License. See LICENSE.TXT for details.
7226efd35SChris Lattner //
8226efd35SChris Lattner //===----------------------------------------------------------------------===//
9226efd35SChris Lattner //
10226efd35SChris Lattner //  This file defines the FileSystemStatCache interface.
11226efd35SChris Lattner //
12226efd35SChris Lattner //===----------------------------------------------------------------------===//
13226efd35SChris Lattner 
14226efd35SChris Lattner #include "clang/Basic/FileSystemStatCache.h"
15c8130a74SBen Langmuir #include "clang/Basic/VirtualFileSystem.h"
16*5a52011cSEugene Zelenko #include "llvm/Support/Chrono.h"
17*5a52011cSEugene Zelenko #include "llvm/Support/ErrorOr.h"
188aaf4995SMichael J. Spencer #include "llvm/Support/Path.h"
19*5a52011cSEugene Zelenko #include <utility>
20f77e11baSChris Lattner 
21226efd35SChris Lattner using namespace clang;
22226efd35SChris Lattner 
2368e081d6SDavid Blaikie void FileSystemStatCache::anchor() {}
2468e081d6SDavid Blaikie 
25c8130a74SBen Langmuir static void copyStatusToFileData(const vfs::Status &Status,
26f8f91b89SRafael Espindola                                  FileData &Data) {
27d066d4c8SBen Langmuir   Data.Name = Status.getName();
28f8f91b89SRafael Espindola   Data.Size = Status.getSize();
29ac71c8e2SPavel Labath   Data.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
30f8f91b89SRafael Espindola   Data.UniqueID = Status.getUniqueID();
31c8130a74SBen Langmuir   Data.IsDirectory = Status.isDirectory();
32c8130a74SBen Langmuir   Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
33f8f91b89SRafael Espindola   Data.InPCH = false;
345de00f3bSBen Langmuir   Data.IsVFSMapped = Status.IsVFSMapped;
35f8f91b89SRafael Espindola }
36f8f91b89SRafael Espindola 
37dd278430SChris Lattner /// FileSystemStatCache::get - Get the 'stat' information for the specified
384fc8fb09SChris Lattner /// path, using the cache to accelerate it if possible.  This returns true if
39dd278430SChris Lattner /// the path does not exist or false if it exists.
40dd278430SChris Lattner ///
413b779379SArgyrios Kyrtzidis /// If isFile is true, then this lookup should only return success for files
423b779379SArgyrios Kyrtzidis /// (not directories).  If it is false this lookup should only return
43dd278430SChris Lattner /// success for directories (not files).  On a successful file lookup, the
44dd278430SChris Lattner /// implementation can optionally fill in FileDescriptor with a valid
45dd278430SChris Lattner /// descriptor and the client guarantees that it will close it.
460df59d8cSMehdi Amini bool FileSystemStatCache::get(StringRef Path, FileData &Data, bool isFile,
47326ffb36SDavid Blaikie                               std::unique_ptr<vfs::File> *F,
48326ffb36SDavid Blaikie                               FileSystemStatCache *Cache, vfs::FileSystem &FS) {
49dd278430SChris Lattner   LookupResult R;
503b779379SArgyrios Kyrtzidis   bool isForDir = !isFile;
51dd278430SChris Lattner 
525ea7d07dSChris Lattner   // If we have a cache, use it to resolve the stat query.
53dd278430SChris Lattner   if (Cache)
54c8130a74SBen Langmuir     R = Cache->getStat(Path, Data, isFile, F, FS);
55c8130a74SBen Langmuir   else if (isForDir || !F) {
563b779379SArgyrios Kyrtzidis     // If this is a directory or a file descriptor is not needed and we have
573b779379SArgyrios Kyrtzidis     // no cache, just go to the file system.
58c8130a74SBen Langmuir     llvm::ErrorOr<vfs::Status> Status = FS.status(Path);
59c8130a74SBen Langmuir     if (!Status) {
60f8f91b89SRafael Espindola       R = CacheMissing;
61f8f91b89SRafael Espindola     } else {
62f8f91b89SRafael Espindola       R = CacheExists;
63c8130a74SBen Langmuir       copyStatusToFileData(*Status, Data);
64f8f91b89SRafael Espindola     }
655ea7d07dSChris Lattner   } else {
665ea7d07dSChris Lattner     // Otherwise, we have to go to the filesystem.  We can always just use
675ea7d07dSChris Lattner     // 'stat' here, but (for files) the client is asking whether the file exists
685ea7d07dSChris Lattner     // because it wants to turn around and *open* it.  It is more efficient to
695ea7d07dSChris Lattner     // do "open+fstat" on success than it is to do "stat+open".
705ea7d07dSChris Lattner     //
715ea7d07dSChris Lattner     // Because of this, check to see if the file exists with 'open'.  If the
725ea7d07dSChris Lattner     // open succeeds, use fstat to get the stat info.
73a885796dSBenjamin Kramer     auto OwnedFile = FS.openFileForRead(Path);
745ea7d07dSChris Lattner 
75a885796dSBenjamin Kramer     if (!OwnedFile) {
765ea7d07dSChris Lattner       // If the open fails, our "stat" fails.
775ea7d07dSChris Lattner       R = CacheMissing;
785ea7d07dSChris Lattner     } else {
795ea7d07dSChris Lattner       // Otherwise, the open succeeded.  Do an fstat to get the information
805ea7d07dSChris Lattner       // about the file.  We'll end up returning the open file descriptor to the
815ea7d07dSChris Lattner       // client to do what they please with it.
82a885796dSBenjamin Kramer       llvm::ErrorOr<vfs::Status> Status = (*OwnedFile)->status();
83c8130a74SBen Langmuir       if (Status) {
845ea7d07dSChris Lattner         R = CacheExists;
85c8130a74SBen Langmuir         copyStatusToFileData(*Status, Data);
86a885796dSBenjamin Kramer         *F = std::move(*OwnedFile);
87f8f91b89SRafael Espindola       } else {
885ea7d07dSChris Lattner         // fstat rarely fails.  If it does, claim the initial open didn't
895ea7d07dSChris Lattner         // succeed.
905ea7d07dSChris Lattner         R = CacheMissing;
91f1186c5aSCraig Topper         *F = nullptr;
925ea7d07dSChris Lattner       }
935ea7d07dSChris Lattner     }
945ea7d07dSChris Lattner   }
95dd278430SChris Lattner 
96f77e11baSChris Lattner   // If the path doesn't exist, return failure.
97dd278430SChris Lattner   if (R == CacheMissing) return true;
98dd278430SChris Lattner 
99f77e11baSChris Lattner   // If the path exists, make sure that its "directoryness" matches the clients
100f77e11baSChris Lattner   // demands.
101f8f91b89SRafael Espindola   if (Data.IsDirectory != isForDir) {
102f77e11baSChris Lattner     // If not, close the file if opened.
103326ffb36SDavid Blaikie     if (F)
104f1186c5aSCraig Topper       *F = nullptr;
105f77e11baSChris Lattner 
106f77e11baSChris Lattner     return true;
107f77e11baSChris Lattner   }
108f77e11baSChris Lattner 
109f77e11baSChris Lattner   return false;
110dd278430SChris Lattner }
111dd278430SChris Lattner 
112226efd35SChris Lattner MemorizeStatCalls::LookupResult
1130df59d8cSMehdi Amini MemorizeStatCalls::getStat(StringRef Path, FileData &Data, bool isFile,
114326ffb36SDavid Blaikie                            std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) {
115c8130a74SBen Langmuir   LookupResult Result = statChained(Path, Data, isFile, F, FS);
116226efd35SChris Lattner 
117226efd35SChris Lattner   // Do not cache failed stats, it is easy to construct common inconsistent
118226efd35SChris Lattner   // situations if we do, and they are not important for PCH performance (which
119226efd35SChris Lattner   // currently only needs the stats to construct the initial FileManager
120226efd35SChris Lattner   // entries).
1218f0583daSChris Lattner   if (Result == CacheMissing)
122226efd35SChris Lattner     return Result;
123226efd35SChris Lattner 
124226efd35SChris Lattner   // Cache file 'stat' results and directories with absolutely paths.
125f8f91b89SRafael Espindola   if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path))
126f8f91b89SRafael Espindola     StatCalls[Path] = Data;
127226efd35SChris Lattner 
128226efd35SChris Lattner   return Result;
129226efd35SChris Lattner }
130