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