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 //===----------------------------------------------------------------------===//
11 /// \brief Defines the clang::FileManager interface and associated types.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_FILEMANAGER_H
16 #define LLVM_CLANG_FILEMANAGER_H
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>
30 typedef unsigned short mode_t;
37 namespace sys { class Path; }
42 class FileSystemStatCache;
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;
50 DirectoryEntry() : Name(0) {}
51 const char *getName() const { return Name; }
54 /// \brief Cached information about one file (either on disk
55 /// or in the virtual file system).
57 /// If the 'FD' member is valid, then this FileEntry has an open file
58 /// descriptor for the file.
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'.
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.
72 friend class FileManager;
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
78 FileEntry() : Name(0), Device(0), Inode(0), FileMode(0), FD(-1) {}
80 FileEntry(const FileEntry &FE) {
81 memcpy(this, &FE, sizeof(FE));
82 assert(FD == -1 && "Cannot copy a file-owning FileEntry");
85 void operator=(const FileEntry &FE) {
86 memcpy(this, &FE, sizeof(FE));
87 assert(FD == -1 && "Cannot assign a file-owning FileEntry");
92 const char *getName() const { return Name; }
93 off_t getSize() const { return Size; }
94 unsigned getUID() const { return UID; }
95 ino_t getInode() const { return Inode; }
96 dev_t getDevice() const { return Device; }
97 time_t getModificationTime() const { return ModTime; }
98 mode_t getFileMode() const { return FileMode; }
100 /// \brief 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);
107 /// \brief Check whether the file is a named pipe (and thus can't be opened by
108 /// the native FileManager methods).
109 bool isNamedPipe() const;
112 /// \brief Implements support for file system lookup, file system caching,
113 /// and directory search management.
115 /// This also handles more advanced properties, such as uniquing files based
116 /// on "inode", so that a file with two names (e.g. symlinked) will be treated
117 /// as a single file.
119 class FileManager : public RefCountedBase<FileManager> {
120 FileSystemOptions FileSystemOpts;
122 class UniqueDirContainer;
123 class UniqueFileContainer;
125 /// \brief Cache for existing real directories.
126 UniqueDirContainer &UniqueRealDirs;
128 /// \brief Cache for existing real files.
129 UniqueFileContainer &UniqueRealFiles;
131 /// \brief The virtual directories that we have allocated.
133 /// For each virtual file (e.g. foo/bar/baz.cpp), we add all of its parent
134 /// directories (foo/ and foo/bar/) here.
135 SmallVector<DirectoryEntry*, 4> VirtualDirectoryEntries;
136 /// \brief The virtual files that we have allocated.
137 SmallVector<FileEntry*, 4> VirtualFileEntries;
139 /// \brief A cache that maps paths to directory entries (either real or
140 /// virtual) we have looked up
142 /// The actual Entries for real directories/files are
143 /// owned by UniqueRealDirs/UniqueRealFiles above, while the Entries
144 /// for virtual directories/files are owned by
145 /// VirtualDirectoryEntries/VirtualFileEntries above.
147 llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> SeenDirEntries;
149 /// \brief A cache that maps paths to file entries (either real or
150 /// virtual) we have looked up.
152 /// \see SeenDirEntries
153 llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> SeenFileEntries;
155 /// \brief Each FileEntry we create is assigned a unique ID #.
157 unsigned NextFileUID;
160 unsigned NumDirLookups, NumFileLookups;
161 unsigned NumDirCacheMisses, NumFileCacheMisses;
164 OwningPtr<FileSystemStatCache> StatCache;
166 bool getStatValue(const char *Path, struct stat &StatBuf,
167 int *FileDescriptor);
169 /// Add all ancestors of the given path (pointing to either a file
170 /// or a directory) as virtual directories.
171 void addAncestorsAsVirtualDirs(StringRef Path);
174 FileManager(const FileSystemOptions &FileSystemOpts);
177 /// \brief Installs the provided FileSystemStatCache object within
180 /// Ownership of this object is transferred to the FileManager.
182 /// \param statCache the new stat cache to install. Ownership of this
183 /// object is transferred to the FileManager.
185 /// \param AtBeginning whether this new stat cache must be installed at the
186 /// beginning of the chain of stat caches. Otherwise, it will be added to
187 /// the end of the chain.
188 void addStatCache(FileSystemStatCache *statCache, bool AtBeginning = false);
190 /// \brief Removes the specified FileSystemStatCache object from the manager.
191 void removeStatCache(FileSystemStatCache *statCache);
193 /// \brief Removes all FileSystemStatCache objects from the manager.
194 void clearStatCaches();
196 /// \brief Lookup, cache, and verify the specified directory (real or
199 /// This returns NULL if the directory doesn't exist.
201 /// \param CacheFailure If true and the file does not exist, we'll cache
202 /// the failure to find this file.
203 const DirectoryEntry *getDirectory(StringRef DirName,
204 bool CacheFailure = true);
206 /// \brief Lookup, cache, and verify the specified file (real or
209 /// This returns NULL if the file doesn't exist.
211 /// \param OpenFile if true and the file exists, it will be opened.
213 /// \param CacheFailure If true and the file does not exist, we'll cache
214 /// the failure to find this file.
215 const FileEntry *getFile(StringRef Filename, bool OpenFile = false,
216 bool CacheFailure = true);
218 /// \brief Returns the current file system options
219 const FileSystemOptions &getFileSystemOptions() { return FileSystemOpts; }
221 /// \brief Retrieve a file entry for a "virtual" file that acts as
222 /// if there were a file with the given name on disk.
224 /// The file itself is not accessed.
225 const FileEntry *getVirtualFile(StringRef Filename, off_t Size,
226 time_t ModificationTime);
228 /// \brief Open the specified file as a MemoryBuffer, returning a new
229 /// MemoryBuffer if successful, otherwise returning null.
230 llvm::MemoryBuffer *getBufferForFile(const FileEntry *Entry,
231 std::string *ErrorStr = 0,
232 bool isVolatile = false);
233 llvm::MemoryBuffer *getBufferForFile(StringRef Filename,
234 std::string *ErrorStr = 0);
236 /// \brief Get the 'stat' information for the given \p Path.
238 /// If the path is relative, it will be resolved against the WorkingDir of the
239 /// FileManager's FileSystemOptions.
240 bool getNoncachedStatValue(StringRef Path, struct stat &StatBuf);
242 /// \brief Remove the real file \p Entry from the cache.
243 void invalidateCache(const FileEntry *Entry);
245 /// \brief If path is not absolute and FileSystemOptions set the working
246 /// directory, the path is modified to be relative to the given
247 /// working directory.
248 void FixupRelativePath(SmallVectorImpl<char> &path) const;
250 /// \brief Produce an array mapping from the unique IDs assigned to each
251 /// file to the corresponding FileEntry pointer.
252 void GetUniqueIDMapping(
253 SmallVectorImpl<const FileEntry *> &UIDToFiles) const;
255 /// \brief Modifies the size and modification time of a previously created
256 /// FileEntry. Use with caution.
257 static void modifyFileEntry(FileEntry *File, off_t Size,
258 time_t ModificationTime);
260 void PrintStats() const;
263 } // end namespace clang