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