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 "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/OwningPtr.h"
22 #include "llvm/Support/Allocator.h"
23 #include "llvm/Config/config.h" // for mode_t
24 // FIXME: Enhance libsystem to support inode and other fields in stat.
25 #include <sys/types.h>
31 namespace sys { class Path; }
36 class FileSystemStatCache;
38 /// DirectoryEntry - Cached information about one directory on the disk.
40 class DirectoryEntry {
41 const char *Name; // Name of the directory.
42 friend class FileManager;
44 DirectoryEntry() : Name(0) {}
45 const char *getName() const { return Name; }
48 /// FileEntry - Cached information about one file on the disk. If the 'FD'
49 /// member is valid, then this FileEntry has an open file descriptor for the
53 const char *Name; // Name of the file.
54 off_t Size; // File size in bytes.
55 time_t ModTime; // Modification time of file.
56 const DirectoryEntry *Dir; // Directory file lives in.
57 unsigned UID; // A unique (small) ID for the file.
58 dev_t Device; // ID for the device containing the file.
59 ino_t Inode; // Inode number for the file.
60 mode_t FileMode; // The file mode as returned by 'stat'.
62 /// FD - The file descriptor for the file entry if it is opened and owned
63 /// by the FileEntry. If not, this is set to -1.
65 friend class FileManager;
68 FileEntry(dev_t device, ino_t inode, mode_t m)
69 : Name(0), Device(device), Inode(inode), FileMode(m), FD(-1) {}
70 // Add a default constructor for use with llvm::StringMap
71 FileEntry() : Name(0), Device(0), Inode(0), FileMode(0), FD(-1) {}
73 FileEntry(const FileEntry &FE) {
74 memcpy(this, &FE, sizeof(FE));
75 assert(FD == -1 && "Cannot copy an file-owning FileEntry");
80 const char *getName() const { return Name; }
81 off_t getSize() const { return Size; }
82 unsigned getUID() const { return UID; }
83 ino_t getInode() const { return Inode; }
84 dev_t getDevice() const { return Device; }
85 time_t getModificationTime() const { return ModTime; }
86 mode_t getFileMode() const { return FileMode; }
88 /// getDir - Return the directory the file lives in.
90 const DirectoryEntry *getDir() const { return Dir; }
92 bool operator<(const FileEntry &RHS) const {
93 return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode);
97 /// FileManager - Implements support for file system lookup, file system
98 /// caching, and directory search management. This also handles more advanced
99 /// properties, such as uniquing files based on "inode", so that a file with two
100 /// names (e.g. symlinked) will be treated as a single file.
103 FileSystemOptions FileSystemOpts;
105 class UniqueDirContainer;
106 class UniqueFileContainer;
108 /// UniqueDirs/UniqueFiles - Cache for existing directories/files.
110 UniqueDirContainer &UniqueDirs;
111 UniqueFileContainer &UniqueFiles;
113 /// DirEntries/FileEntries - This is a cache of directory/file entries we have
114 /// looked up. The actual Entry is owned by UniqueFiles/UniqueDirs above.
116 llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> DirEntries;
117 llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> FileEntries;
119 /// NextFileUID - Each FileEntry we create is assigned a unique ID #.
121 unsigned NextFileUID;
123 /// \brief The virtual files that we have allocated.
124 llvm::SmallVector<FileEntry*, 4> VirtualFileEntries;
127 unsigned NumDirLookups, NumFileLookups;
128 unsigned NumDirCacheMisses, NumFileCacheMisses;
131 llvm::OwningPtr<FileSystemStatCache> StatCache;
133 bool getStatValue(const char *Path, struct stat &StatBuf,
134 int *FileDescriptor);
136 FileManager(const FileSystemOptions &FileSystemOpts);
139 /// \brief Installs the provided FileSystemStatCache object within
142 /// Ownership of this object is transferred to the FileManager.
144 /// \param statCache the new stat cache to install. Ownership of this
145 /// object is transferred to the FileManager.
147 /// \param AtBeginning whether this new stat cache must be installed at the
148 /// beginning of the chain of stat caches. Otherwise, it will be added to
149 /// the end of the chain.
150 void addStatCache(FileSystemStatCache *statCache, bool AtBeginning = false);
152 /// \brief Removes the specified FileSystemStatCache object from the manager.
153 void removeStatCache(FileSystemStatCache *statCache);
155 /// getDirectory - Lookup, cache, and verify the specified directory. This
156 /// returns null if the directory doesn't exist.
158 const DirectoryEntry *getDirectory(llvm::StringRef Filename);
160 /// getFile - Lookup, cache, and verify the specified file. This returns null
161 /// if the file doesn't exist.
163 const FileEntry *getFile(llvm::StringRef Filename);
165 /// \brief Retrieve a file entry for a "virtual" file that acts as
166 /// if there were a file with the given name on disk. The file
167 /// itself is not accessed.
168 const FileEntry *getVirtualFile(llvm::StringRef Filename, off_t Size,
169 time_t ModificationTime);
171 /// \brief Open the specified file as a MemoryBuffer, returning a new
172 /// MemoryBuffer if successful, otherwise returning null.
173 llvm::MemoryBuffer *getBufferForFile(const FileEntry *Entry,
174 std::string *ErrorStr = 0);
175 llvm::MemoryBuffer *getBufferForFile(llvm::StringRef Filename,
176 std::string *ErrorStr = 0);
178 /// \brief If path is not absolute and FileSystemOptions set the working
179 /// directory, the path is modified to be relative to the given
180 /// working directory.
181 static void FixupRelativePath(llvm::sys::Path &path,
182 const FileSystemOptions &FSOpts);
184 void PrintStats() const;
187 } // end namespace clang