1 //===--- FileManager.h - File System Probing and Caching --------*- C++ -*-===//
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 FileManager interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_FILEMANAGER_H
15 #define LLVM_CLANG_FILEMANAGER_H
17 #include "clang/Basic/FileSystemOptions.h"
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/IntrusiveRefCntPtr.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ADT/OwningPtr.h"
24 #include "llvm/Support/Allocator.h"
25 // FIXME: Enhance libsystem to support inode and other fields in stat.
26 #include <sys/types.h>
29 typedef unsigned short mode_t;
36 namespace sys { class Path; }
41 class FileSystemStatCache;
43 /// DirectoryEntry - Cached information about one directory (either on
44 /// the disk or in the virtual file system).
46 class DirectoryEntry {
47 const char *Name; // Name of the directory.
48 friend class FileManager;
50 DirectoryEntry() : Name(0) {}
51 const char *getName() const { return Name; }
54 /// FileEntry - Cached information about one file (either on the disk
55 /// or in the virtual file system). If the 'FD' member is valid, then
56 /// this FileEntry has an open file descriptor for the file.
59 const char *Name; // Name of the file.
60 off_t Size; // File size in bytes.
61 time_t ModTime; // Modification time of file.
62 const DirectoryEntry *Dir; // Directory file lives in.
63 unsigned UID; // A unique (small) ID for the file.
64 dev_t Device; // ID for the device containing the file.
65 ino_t Inode; // Inode number for the file.
66 mode_t FileMode; // The file mode as returned by 'stat'.
68 /// FD - The file descriptor for the file entry if it is opened and owned
69 /// by the FileEntry. If not, this is set to -1.
71 friend class FileManager;
74 FileEntry(dev_t device, ino_t inode, mode_t m)
75 : Name(0), Device(device), Inode(inode), FileMode(m), FD(-1) {}
76 // Add a default constructor for use with llvm::StringMap
77 FileEntry() : Name(0), Device(0), Inode(0), FileMode(0), FD(-1) {}
79 FileEntry(const FileEntry &FE) {
80 memcpy(this, &FE, sizeof(FE));
81 assert(FD == -1 && "Cannot copy a file-owning FileEntry");
84 void operator=(const FileEntry &FE) {
85 memcpy(this, &FE, sizeof(FE));
86 assert(FD == -1 && "Cannot assign a file-owning FileEntry");
91 const char *getName() const { return Name; }
92 off_t getSize() const { return Size; }
93 unsigned getUID() const { return UID; }
94 ino_t getInode() const { return Inode; }
95 dev_t getDevice() const { return Device; }
96 time_t getModificationTime() const { return ModTime; }
97 mode_t getFileMode() const { return FileMode; }
99 /// getDir - Return the directory the file lives in.
101 const DirectoryEntry *getDir() const { return Dir; }
103 bool operator<(const FileEntry &RHS) const {
104 return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode);
108 /// FileManager - Implements support for file system lookup, file system
109 /// caching, and directory search management. This also handles more advanced
110 /// properties, such as uniquing files based on "inode", so that a file with two
111 /// names (e.g. symlinked) will be treated as a single file.
113 class FileManager : public llvm::RefCountedBase<FileManager> {
114 FileSystemOptions FileSystemOpts;
116 class UniqueDirContainer;
117 class UniqueFileContainer;
119 /// UniqueRealDirs/UniqueRealFiles - Cache for existing real directories/files.
121 UniqueDirContainer &UniqueRealDirs;
122 UniqueFileContainer &UniqueRealFiles;
124 /// \brief The virtual directories that we have allocated. For each
125 /// virtual file (e.g. foo/bar/baz.cpp), we add all of its parent
126 /// directories (foo/ and foo/bar/) here.
127 SmallVector<DirectoryEntry*, 4> VirtualDirectoryEntries;
128 /// \brief The virtual files that we have allocated.
129 SmallVector<FileEntry*, 4> VirtualFileEntries;
131 /// SeenDirEntries/SeenFileEntries - This is a cache that maps paths
132 /// to directory/file entries (either real or virtual) we have
133 /// looked up. The actual Entries for real directories/files are
134 /// owned by UniqueRealDirs/UniqueRealFiles above, while the Entries
135 /// for virtual directories/files are owned by
136 /// VirtualDirectoryEntries/VirtualFileEntries above.
138 llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> SeenDirEntries;
139 llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> SeenFileEntries;
141 /// NextFileUID - Each FileEntry we create is assigned a unique ID #.
143 unsigned NextFileUID;
146 unsigned NumDirLookups, NumFileLookups;
147 unsigned NumDirCacheMisses, NumFileCacheMisses;
150 llvm::OwningPtr<FileSystemStatCache> StatCache;
152 bool getStatValue(const char *Path, struct stat &StatBuf,
153 int *FileDescriptor);
155 /// Add all ancestors of the given path (pointing to either a file
156 /// or a directory) as virtual directories.
157 void addAncestorsAsVirtualDirs(StringRef Path);
160 FileManager(const FileSystemOptions &FileSystemOpts);
163 /// \brief Installs the provided FileSystemStatCache object within
166 /// Ownership of this object is transferred to the FileManager.
168 /// \param statCache the new stat cache to install. Ownership of this
169 /// object is transferred to the FileManager.
171 /// \param AtBeginning whether this new stat cache must be installed at the
172 /// beginning of the chain of stat caches. Otherwise, it will be added to
173 /// the end of the chain.
174 void addStatCache(FileSystemStatCache *statCache, bool AtBeginning = false);
176 /// \brief Removes the specified FileSystemStatCache object from the manager.
177 void removeStatCache(FileSystemStatCache *statCache);
179 /// getDirectory - Lookup, cache, and verify the specified directory
180 /// (real or virtual). This returns NULL if the directory doesn't exist.
182 /// \param CacheFailure If true and the file does not exist, we'll cache
183 /// the failure to find this file.
184 const DirectoryEntry *getDirectory(StringRef DirName,
185 bool CacheFailure = true);
187 /// \brief Lookup, cache, and verify the specified file (real or
188 /// virtual). This returns NULL if the file doesn't exist.
190 /// \param OpenFile if true and the file exists, it will be opened.
192 /// \param CacheFailure If true and the file does not exist, we'll cache
193 /// the failure to find this file.
194 const FileEntry *getFile(StringRef Filename, bool OpenFile = false,
195 bool CacheFailure = true);
197 /// \brief Returns the current file system options
198 const FileSystemOptions &getFileSystemOptions() { return FileSystemOpts; }
200 /// \brief Retrieve a file entry for a "virtual" file that acts as
201 /// if there were a file with the given name on disk. The file
202 /// itself is not accessed.
203 const FileEntry *getVirtualFile(StringRef Filename, off_t Size,
204 time_t ModificationTime);
206 /// \brief Open the specified file as a MemoryBuffer, returning a new
207 /// MemoryBuffer if successful, otherwise returning null.
208 llvm::MemoryBuffer *getBufferForFile(const FileEntry *Entry,
209 std::string *ErrorStr = 0);
210 llvm::MemoryBuffer *getBufferForFile(StringRef Filename,
211 std::string *ErrorStr = 0);
213 // getNoncachedStatValue - Will get the 'stat' information for the given path.
214 // If the path is relative, it will be resolved against the WorkingDir of the
215 // FileManager's FileSystemOptions.
216 bool getNoncachedStatValue(StringRef Path, struct stat &StatBuf);
218 /// \brief If path is not absolute and FileSystemOptions set the working
219 /// directory, the path is modified to be relative to the given
220 /// working directory.
221 void FixupRelativePath(SmallVectorImpl<char> &path) const;
223 /// \brief Produce an array mapping from the unique IDs assigned to each
224 /// file to the corresponding FileEntry pointer.
225 void GetUniqueIDMapping(
226 SmallVectorImpl<const FileEntry *> &UIDToFiles) const;
228 void PrintStats() const;
231 } // end namespace clang