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