1 ///===--- FileManager.cpp - File System Probing and Caching ----------------===//
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 implements the FileManager interface.
12 //===----------------------------------------------------------------------===//
14 // TODO: This should index all interesting directories with dirent calls.
16 // opendir/readdir_r/closedir ?
18 //===----------------------------------------------------------------------===//
20 #include "clang/Basic/FileManager.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/System/Path.h"
24 #include "llvm/Config/config.h"
28 using namespace clang;
30 // FIXME: Enhance libsystem to support inode and other fields.
34 #define S_ISDIR(s) (_S_IFDIR & s)
37 /// NON_EXISTENT_DIR - A special value distinct from null that is used to
38 /// represent a dir name that doesn't exist on the disk.
39 #define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
41 //===----------------------------------------------------------------------===//
43 //===----------------------------------------------------------------------===//
47 #define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
50 static std::string GetFullPath(const char *relPath) {
51 char *absPathStrPtr = _fullpath(NULL, relPath, 0);
52 assert(absPathStrPtr && "_fullpath() returned NULL!");
54 std::string absPath(absPathStrPtr);
61 class FileManager::UniqueDirContainer {
62 /// UniqueDirs - Cache from full path to existing directories/files.
64 llvm::StringMap<DirectoryEntry> UniqueDirs;
67 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
68 std::string FullPath(GetFullPath(Name));
69 return UniqueDirs.GetOrCreateValue(
71 FullPath.c_str() + FullPath.size()
75 size_t size() { return UniqueDirs.size(); }
78 class FileManager::UniqueFileContainer {
79 /// UniqueFiles - Cache from full path to existing directories/files.
81 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
84 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
85 std::string FullPath(GetFullPath(Name));
86 return UniqueFiles.GetOrCreateValue(
88 FullPath.c_str() + FullPath.size()
92 size_t size() { return UniqueFiles.size(); }
95 //===----------------------------------------------------------------------===//
97 //===----------------------------------------------------------------------===//
101 #define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
103 class FileManager::UniqueDirContainer {
104 /// UniqueDirs - Cache from ID's to existing directories/files.
106 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
109 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
110 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
113 size_t size() { return UniqueDirs.size(); }
116 class FileManager::UniqueFileContainer {
117 /// UniqueFiles - Cache from ID's to existing directories/files.
119 std::set<FileEntry> UniqueFiles;
122 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
124 const_cast<FileEntry&>(
125 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
127 StatBuf.st_mode)).first);
130 size_t size() { return UniqueFiles.size(); }
135 //===----------------------------------------------------------------------===//
137 //===----------------------------------------------------------------------===//
139 FileManager::FileManager()
140 : UniqueDirs(*new UniqueDirContainer),
141 UniqueFiles(*new UniqueFileContainer),
142 DirEntries(64), FileEntries(64), NextFileUID(0) {
143 NumDirLookups = NumFileLookups = 0;
144 NumDirCacheMisses = NumFileCacheMisses = 0;
147 FileManager::~FileManager() {
152 void FileManager::addStatCache(StatSysCallCache *statCache, bool AtBeginning) {
153 assert(statCache && "No stat cache provided?");
154 if (AtBeginning || StatCache.get() == 0) {
155 statCache->setNextStatCache(StatCache.take());
156 StatCache.reset(statCache);
160 StatSysCallCache *LastCache = StatCache.get();
161 while (LastCache->getNextStatCache())
162 LastCache = LastCache->getNextStatCache();
164 LastCache->setNextStatCache(statCache);
167 void FileManager::removeStatCache(StatSysCallCache *statCache) {
171 if (StatCache.get() == statCache) {
172 // This is the first stat cache.
173 StatCache.reset(StatCache->takeNextStatCache());
177 // Find the stat cache in the list.
178 StatSysCallCache *PrevCache = StatCache.get();
179 while (PrevCache && PrevCache->getNextStatCache() != statCache)
180 PrevCache = PrevCache->getNextStatCache();
182 PrevCache->setNextStatCache(statCache->getNextStatCache());
184 assert(false && "Stat cache not found for removal");
187 /// getDirectory - Lookup, cache, and verify the specified directory. This
188 /// returns null if the directory doesn't exist.
190 const DirectoryEntry *FileManager::getDirectory(const char *NameStart,
191 const char *NameEnd) {
193 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
194 DirEntries.GetOrCreateValue(NameStart, NameEnd);
196 // See if there is already an entry in the map.
197 if (NamedDirEnt.getValue())
198 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
199 ? 0 : NamedDirEnt.getValue();
203 // By default, initialize it to invalid.
204 NamedDirEnt.setValue(NON_EXISTENT_DIR);
206 // Get the null-terminated directory name as stored as the key of the
208 const char *InterndDirName = NamedDirEnt.getKeyData();
210 // Check to see if the directory exists.
212 if (stat_cached(InterndDirName, &StatBuf) || // Error stat'ing.
213 !S_ISDIR(StatBuf.st_mode)) // Not a directory?
216 // It exists. See if we have already opened a directory with the same inode.
217 // This occurs when one dir is symlinked to another, for example.
218 DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
220 NamedDirEnt.setValue(&UDE);
221 if (UDE.getName()) // Already have an entry with this inode, return it.
224 // Otherwise, we don't have this directory yet, add it. We use the string
225 // key from the DirEntries map as the string.
226 UDE.Name = InterndDirName;
230 /// NON_EXISTENT_FILE - A special value distinct from null that is used to
231 /// represent a filename that doesn't exist on the disk.
232 #define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
234 /// getFile - Lookup, cache, and verify the specified file. This returns null
235 /// if the file doesn't exist.
237 const FileEntry *FileManager::getFile(const char *NameStart,
238 const char *NameEnd) {
241 // See if there is already an entry in the map.
242 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
243 FileEntries.GetOrCreateValue(NameStart, NameEnd);
245 // See if there is already an entry in the map.
246 if (NamedFileEnt.getValue())
247 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
248 ? 0 : NamedFileEnt.getValue();
250 ++NumFileCacheMisses;
252 // By default, initialize it to invalid.
253 NamedFileEnt.setValue(NON_EXISTENT_FILE);
255 // Figure out what directory it is in. If the string contains a / in it,
256 // strip off everything after it.
257 // FIXME: this logic should be in sys::Path.
258 const char *SlashPos = NameEnd-1;
259 while (SlashPos >= NameStart && !IS_DIR_SEPARATOR_CHAR(SlashPos[0]))
261 // Ignore duplicate //'s.
262 while (SlashPos > NameStart && IS_DIR_SEPARATOR_CHAR(SlashPos[-1]))
265 const DirectoryEntry *DirInfo;
266 if (SlashPos < NameStart) {
267 // Use the current directory if file has no path component.
268 const char *Name = ".";
269 DirInfo = getDirectory(Name, Name+1);
270 } else if (SlashPos == NameEnd-1)
271 return 0; // If filename ends with a /, it's a directory.
273 DirInfo = getDirectory(NameStart, SlashPos);
275 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
278 // Get the null-terminated file name as stored as the key of the
280 const char *InterndFileName = NamedFileEnt.getKeyData();
282 // FIXME: Use the directory info to prune this, before doing the stat syscall.
283 // FIXME: This will reduce the # syscalls.
285 // Nope, there isn't. Check to see if the file exists.
287 //llvm::errs() << "STATING: " << Filename;
288 if (stat_cached(InterndFileName, &StatBuf) || // Error stat'ing.
289 S_ISDIR(StatBuf.st_mode)) { // A directory?
290 // If this file doesn't exist, we leave a null in FileEntries for this path.
291 //llvm::errs() << ": Not existing\n";
294 //llvm::errs() << ": exists\n";
296 // It exists. See if we have already opened a file with the same inode.
297 // This occurs when one dir is symlinked to another, for example.
298 FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
300 NamedFileEnt.setValue(&UFE);
301 if (UFE.getName()) // Already have an entry with this inode, return it.
304 // Otherwise, we don't have this directory yet, add it.
305 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
307 UFE.Name = InterndFileName;
308 UFE.Size = StatBuf.st_size;
309 UFE.ModTime = StatBuf.st_mtime;
311 UFE.UID = NextFileUID++;
315 void FileManager::PrintStats() const {
316 llvm::errs() << "\n*** File Manager Stats:\n";
317 llvm::errs() << UniqueFiles.size() << " files found, "
318 << UniqueDirs.size() << " dirs found.\n";
319 llvm::errs() << NumDirLookups << " dir lookups, "
320 << NumDirCacheMisses << " dir cache misses.\n";
321 llvm::errs() << NumFileLookups << " file lookups, "
322 << NumFileCacheMisses << " file cache misses.\n";
324 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
327 int MemorizeStatCalls::stat(const char *path, struct stat *buf) {
328 int result = StatSysCallCache::stat(path, buf);
331 // Cache failed 'stat' results.
333 memset(&empty, 0, sizeof(empty));
334 StatCalls[path] = StatResult(result, empty);
336 else if (!S_ISDIR(buf->st_mode) || llvm::sys::Path(path).isAbsolute()) {
337 // Cache file 'stat' results and directories with absolutely
339 StatCalls[path] = StatResult(result, *buf);