clang API Documentation
00001 //===--- FileSystemStatCache.cpp - Caching for 'stat' calls ---------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the FileSystemStatCache interface. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/Basic/FileSystemStatCache.h" 00015 #include "clang/Basic/VirtualFileSystem.h" 00016 #include "llvm/Support/Path.h" 00017 00018 // FIXME: This is terrible, we need this for ::close. 00019 #if !defined(_MSC_VER) && !defined(__MINGW32__) 00020 #include <unistd.h> 00021 #include <sys/uio.h> 00022 #else 00023 #include <io.h> 00024 #endif 00025 using namespace clang; 00026 00027 #if defined(_MSC_VER) 00028 #define S_ISDIR(s) ((_S_IFDIR & s) !=0) 00029 #endif 00030 00031 void FileSystemStatCache::anchor() { } 00032 00033 static void copyStatusToFileData(const vfs::Status &Status, 00034 FileData &Data) { 00035 Data.Name = Status.getName(); 00036 Data.Size = Status.getSize(); 00037 Data.ModTime = Status.getLastModificationTime().toEpochTime(); 00038 Data.UniqueID = Status.getUniqueID(); 00039 Data.IsDirectory = Status.isDirectory(); 00040 Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file; 00041 Data.InPCH = false; 00042 Data.IsVFSMapped = Status.IsVFSMapped; 00043 } 00044 00045 /// FileSystemStatCache::get - Get the 'stat' information for the specified 00046 /// path, using the cache to accelerate it if possible. This returns true if 00047 /// the path does not exist or false if it exists. 00048 /// 00049 /// If isFile is true, then this lookup should only return success for files 00050 /// (not directories). If it is false this lookup should only return 00051 /// success for directories (not files). On a successful file lookup, the 00052 /// implementation can optionally fill in FileDescriptor with a valid 00053 /// descriptor and the client guarantees that it will close it. 00054 bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile, 00055 std::unique_ptr<vfs::File> *F, 00056 FileSystemStatCache *Cache, vfs::FileSystem &FS) { 00057 LookupResult R; 00058 bool isForDir = !isFile; 00059 00060 // If we have a cache, use it to resolve the stat query. 00061 if (Cache) 00062 R = Cache->getStat(Path, Data, isFile, F, FS); 00063 else if (isForDir || !F) { 00064 // If this is a directory or a file descriptor is not needed and we have 00065 // no cache, just go to the file system. 00066 llvm::ErrorOr<vfs::Status> Status = FS.status(Path); 00067 if (!Status) { 00068 R = CacheMissing; 00069 } else { 00070 R = CacheExists; 00071 copyStatusToFileData(*Status, Data); 00072 } 00073 } else { 00074 // Otherwise, we have to go to the filesystem. We can always just use 00075 // 'stat' here, but (for files) the client is asking whether the file exists 00076 // because it wants to turn around and *open* it. It is more efficient to 00077 // do "open+fstat" on success than it is to do "stat+open". 00078 // 00079 // Because of this, check to see if the file exists with 'open'. If the 00080 // open succeeds, use fstat to get the stat info. 00081 auto OwnedFile = FS.openFileForRead(Path); 00082 00083 if (!OwnedFile) { 00084 // If the open fails, our "stat" fails. 00085 R = CacheMissing; 00086 } else { 00087 // Otherwise, the open succeeded. Do an fstat to get the information 00088 // about the file. We'll end up returning the open file descriptor to the 00089 // client to do what they please with it. 00090 llvm::ErrorOr<vfs::Status> Status = (*OwnedFile)->status(); 00091 if (Status) { 00092 R = CacheExists; 00093 copyStatusToFileData(*Status, Data); 00094 *F = std::move(*OwnedFile); 00095 } else { 00096 // fstat rarely fails. If it does, claim the initial open didn't 00097 // succeed. 00098 R = CacheMissing; 00099 *F = nullptr; 00100 } 00101 } 00102 } 00103 00104 // If the path doesn't exist, return failure. 00105 if (R == CacheMissing) return true; 00106 00107 // If the path exists, make sure that its "directoryness" matches the clients 00108 // demands. 00109 if (Data.IsDirectory != isForDir) { 00110 // If not, close the file if opened. 00111 if (F) 00112 *F = nullptr; 00113 00114 return true; 00115 } 00116 00117 return false; 00118 } 00119 00120 MemorizeStatCalls::LookupResult 00121 MemorizeStatCalls::getStat(const char *Path, FileData &Data, bool isFile, 00122 std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) { 00123 LookupResult Result = statChained(Path, Data, isFile, F, FS); 00124 00125 // Do not cache failed stats, it is easy to construct common inconsistent 00126 // situations if we do, and they are not important for PCH performance (which 00127 // currently only needs the stats to construct the initial FileManager 00128 // entries). 00129 if (Result == CacheMissing) 00130 return Result; 00131 00132 // Cache file 'stat' results and directories with absolutely paths. 00133 if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path)) 00134 StatCalls[Path] = Data; 00135 00136 return Result; 00137 }