1 //===--- FileManager.h - File System Probing and Caching --------*- C++ -*-===// 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 /// \file 11 /// \brief Defines the clang::FileManager interface and associated types. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CLANG_FILEMANAGER_H 16 #define LLVM_CLANG_FILEMANAGER_H 17 18 #include "clang/Basic/FileSystemOptions.h" 19 #include "clang/Basic/LLVM.h" 20 #include "llvm/ADT/IntrusiveRefCntPtr.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/ADT/StringMap.h" 23 #include "llvm/ADT/StringRef.h" 24 #include "llvm/ADT/OwningPtr.h" 25 #include "llvm/Support/Allocator.h" 26 // FIXME: Enhance libsystem to support inode and other fields in stat. 27 #include <sys/types.h> 28 29 #ifdef _MSC_VER 30 typedef unsigned short mode_t; 31 #endif 32 33 struct stat; 34 35 namespace llvm { 36 class MemoryBuffer; 37 namespace sys { class Path; } 38 } 39 40 namespace clang { 41 class FileManager; 42 class FileSystemStatCache; 43 44 /// \brief Cached information about one directory (either on disk or in 45 /// the virtual file system). 46 class DirectoryEntry { 47 const char *Name; // Name of the directory. 48 friend class FileManager; 49 public: DirectoryEntry()50 DirectoryEntry() : Name(0) {} getName()51 const char *getName() const { return Name; } 52 }; 53 54 /// \brief Cached information about one file (either on disk 55 /// or in the virtual file system). 56 /// 57 /// If the 'FD' member is valid, then this FileEntry has an open file 58 /// descriptor for the file. 59 class FileEntry { 60 const char *Name; // Name of the file. 61 off_t Size; // File size in bytes. 62 time_t ModTime; // Modification time of file. 63 const DirectoryEntry *Dir; // Directory file lives in. 64 unsigned UID; // A unique (small) ID for the file. 65 dev_t Device; // ID for the device containing the file. 66 ino_t Inode; // Inode number for the file. 67 mode_t FileMode; // The file mode as returned by 'stat'. 68 69 /// FD - The file descriptor for the file entry if it is opened and owned 70 /// by the FileEntry. If not, this is set to -1. 71 mutable int FD; 72 friend class FileManager; 73 74 public: FileEntry(dev_t device,ino_t inode,mode_t m)75 FileEntry(dev_t device, ino_t inode, mode_t m) 76 : Name(0), Device(device), Inode(inode), FileMode(m), FD(-1) {} 77 // Add a default constructor for use with llvm::StringMap FileEntry()78 FileEntry() : Name(0), Device(0), Inode(0), FileMode(0), FD(-1) {} 79 FileEntry(const FileEntry & FE)80 FileEntry(const FileEntry &FE) { 81 memcpy(this, &FE, sizeof(FE)); 82 assert(FD == -1 && "Cannot copy a file-owning FileEntry"); 83 } 84 85 void operator=(const FileEntry &FE) { 86 memcpy(this, &FE, sizeof(FE)); 87 assert(FD == -1 && "Cannot assign a file-owning FileEntry"); 88 } 89 90 ~FileEntry(); 91 getName()92 const char *getName() const { return Name; } getSize()93 off_t getSize() const { return Size; } getUID()94 unsigned getUID() const { return UID; } getInode()95 ino_t getInode() const { return Inode; } getDevice()96 dev_t getDevice() const { return Device; } getModificationTime()97 time_t getModificationTime() const { return ModTime; } getFileMode()98 mode_t getFileMode() const { return FileMode; } 99 100 /// \brief Return the directory the file lives in. getDir()101 const DirectoryEntry *getDir() const { return Dir; } 102 103 bool operator<(const FileEntry &RHS) const { 104 return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode); 105 } 106 }; 107 108 /// \brief Implements support for file system lookup, file system caching, 109 /// and directory search management. 110 /// 111 /// This also handles more advanced properties, such as uniquing files based 112 /// on "inode", so that a file with two names (e.g. symlinked) will be treated 113 /// as a single file. 114 /// 115 class FileManager : public RefCountedBase<FileManager> { 116 FileSystemOptions FileSystemOpts; 117 118 class UniqueDirContainer; 119 class UniqueFileContainer; 120 121 /// \brief Cache for existing real directories. 122 UniqueDirContainer &UniqueRealDirs; 123 124 /// \brief Cache for existing real files. 125 UniqueFileContainer &UniqueRealFiles; 126 127 /// \brief The virtual directories that we have allocated. 128 /// 129 /// For each virtual file (e.g. foo/bar/baz.cpp), we add all of its parent 130 /// directories (foo/ and foo/bar/) here. 131 SmallVector<DirectoryEntry*, 4> VirtualDirectoryEntries; 132 /// \brief The virtual files that we have allocated. 133 SmallVector<FileEntry*, 4> VirtualFileEntries; 134 135 /// \brief A cache that maps paths to directory entries (either real or 136 /// virtual) we have looked up 137 /// 138 /// The actual Entries for real directories/files are 139 /// owned by UniqueRealDirs/UniqueRealFiles above, while the Entries 140 /// for virtual directories/files are owned by 141 /// VirtualDirectoryEntries/VirtualFileEntries above. 142 /// 143 llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> SeenDirEntries; 144 145 /// \brief A cache that maps paths to file entries (either real or 146 /// virtual) we have looked up. 147 /// 148 /// \see SeenDirEntries 149 llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> SeenFileEntries; 150 151 /// \brief Each FileEntry we create is assigned a unique ID #. 152 /// 153 unsigned NextFileUID; 154 155 // Statistics. 156 unsigned NumDirLookups, NumFileLookups; 157 unsigned NumDirCacheMisses, NumFileCacheMisses; 158 159 // Caching. 160 OwningPtr<FileSystemStatCache> StatCache; 161 162 bool getStatValue(const char *Path, struct stat &StatBuf, 163 int *FileDescriptor); 164 165 /// Add all ancestors of the given path (pointing to either a file 166 /// or a directory) as virtual directories. 167 void addAncestorsAsVirtualDirs(StringRef Path); 168 169 public: 170 FileManager(const FileSystemOptions &FileSystemOpts); 171 ~FileManager(); 172 173 /// \brief Installs the provided FileSystemStatCache object within 174 /// the FileManager. 175 /// 176 /// Ownership of this object is transferred to the FileManager. 177 /// 178 /// \param statCache the new stat cache to install. Ownership of this 179 /// object is transferred to the FileManager. 180 /// 181 /// \param AtBeginning whether this new stat cache must be installed at the 182 /// beginning of the chain of stat caches. Otherwise, it will be added to 183 /// the end of the chain. 184 void addStatCache(FileSystemStatCache *statCache, bool AtBeginning = false); 185 186 /// \brief Removes the specified FileSystemStatCache object from the manager. 187 void removeStatCache(FileSystemStatCache *statCache); 188 189 /// \brief Removes all FileSystemStatCache objects from the manager. 190 void clearStatCaches(); 191 192 /// \brief Lookup, cache, and verify the specified directory (real or 193 /// virtual). 194 /// 195 /// This returns NULL if the directory doesn't exist. 196 /// 197 /// \param CacheFailure If true and the file does not exist, we'll cache 198 /// the failure to find this file. 199 const DirectoryEntry *getDirectory(StringRef DirName, 200 bool CacheFailure = true); 201 202 /// \brief Lookup, cache, and verify the specified file (real or 203 /// virtual). 204 /// 205 /// This returns NULL if the file doesn't exist. 206 /// 207 /// \param OpenFile if true and the file exists, it will be opened. 208 /// 209 /// \param CacheFailure If true and the file does not exist, we'll cache 210 /// the failure to find this file. 211 const FileEntry *getFile(StringRef Filename, bool OpenFile = false, 212 bool CacheFailure = true); 213 214 /// \brief Returns the current file system options getFileSystemOptions()215 const FileSystemOptions &getFileSystemOptions() { return FileSystemOpts; } 216 217 /// \brief Retrieve a file entry for a "virtual" file that acts as 218 /// if there were a file with the given name on disk. 219 /// 220 /// The file itself is not accessed. 221 const FileEntry *getVirtualFile(StringRef Filename, off_t Size, 222 time_t ModificationTime); 223 224 /// \brief Open the specified file as a MemoryBuffer, returning a new 225 /// MemoryBuffer if successful, otherwise returning null. 226 llvm::MemoryBuffer *getBufferForFile(const FileEntry *Entry, 227 std::string *ErrorStr = 0, 228 bool isVolatile = false); 229 llvm::MemoryBuffer *getBufferForFile(StringRef Filename, 230 std::string *ErrorStr = 0); 231 232 /// \brief Get the 'stat' information for the given \p Path. 233 /// 234 /// If the path is relative, it will be resolved against the WorkingDir of the 235 /// FileManager's FileSystemOptions. 236 bool getNoncachedStatValue(StringRef Path, struct stat &StatBuf); 237 238 /// \brief Remove the real file \p Entry from the cache. 239 void invalidateCache(const FileEntry *Entry); 240 241 /// \brief If path is not absolute and FileSystemOptions set the working 242 /// directory, the path is modified to be relative to the given 243 /// working directory. 244 void FixupRelativePath(SmallVectorImpl<char> &path) const; 245 246 /// \brief Produce an array mapping from the unique IDs assigned to each 247 /// file to the corresponding FileEntry pointer. 248 void GetUniqueIDMapping( 249 SmallVectorImpl<const FileEntry *> &UIDToFiles) const; 250 251 /// \brief Modifies the size and modification time of a previously created 252 /// FileEntry. Use with caution. 253 static void modifyFileEntry(FileEntry *File, off_t Size, 254 time_t ModificationTime); 255 256 void PrintStats() const; 257 }; 258 259 } // end namespace clang 260 261 #endif 262