blob: 891b81d106f311c87dcae40677bb7341ab2b3305 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- FileManager.cpp - File System Probing and Caching ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the FileManager interface.
11//
12//===----------------------------------------------------------------------===//
13//
14// TODO: This should index all interesting directories with dirent calls.
15// getdirentries ?
16// opendir/readdir_r/closedir ?
17//
18//===----------------------------------------------------------------------===//
19
20#include "clang/Basic/FileManager.h"
21#include <iostream>
22using namespace llvm;
23using namespace clang;
24
25// FIXME: Enhance libsystem to support inode and other fields.
26#include <sys/stat.h>
27
Chris Lattneraf653752006-10-30 03:06:54 +000028
29/// NON_EXISTANT_DIR - A special value distinct from null that is used to
30/// represent a dir name that doesn't exist on the disk.
31#define NON_EXISTANT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
32
Chris Lattner22eb9722006-06-18 05:43:12 +000033/// getDirectory - Lookup, cache, and verify the specified directory. This
34/// returns null if the directory doesn't exist.
35///
Chris Lattner43fd42e2006-10-30 03:40:58 +000036const DirectoryEntry *FileManager::getDirectory(const char *FileStart,
37 const char *FileEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +000038 ++NumDirLookups;
Chris Lattner43fd42e2006-10-30 03:40:58 +000039 DirectoryEntry *&NamedDirEnt =DirEntries.GetOrCreateValue(FileStart, FileEnd);
Chris Lattneraf653752006-10-30 03:06:54 +000040
Chris Lattner22eb9722006-06-18 05:43:12 +000041 // See if there is already an entry in the map.
Chris Lattneraf653752006-10-30 03:06:54 +000042 if (NamedDirEnt)
43 return NamedDirEnt == NON_EXISTANT_DIR ? 0 : NamedDirEnt;
Chris Lattner22eb9722006-06-18 05:43:12 +000044
45 ++NumDirCacheMisses;
46
Chris Lattneraf653752006-10-30 03:06:54 +000047 // By default, initialize it to invalid.
48 NamedDirEnt = NON_EXISTANT_DIR;
Chris Lattner22eb9722006-06-18 05:43:12 +000049
Chris Lattner43fd42e2006-10-30 03:40:58 +000050 // Get the null-terminated directory name as stored as the key of the
51 // DirEntries map.
52 const char *InterndDirName = DirEntries.GetKeyForValueInMap(NamedDirEnt);
53
Chris Lattneraf653752006-10-30 03:06:54 +000054 // Check to see if the directory exists.
Chris Lattner22eb9722006-06-18 05:43:12 +000055 struct stat StatBuf;
Chris Lattner43fd42e2006-10-30 03:40:58 +000056 if (stat(InterndDirName, &StatBuf) || // Error stat'ing.
57 !S_ISDIR(StatBuf.st_mode)) // Not a directory?
Chris Lattner22eb9722006-06-18 05:43:12 +000058 return 0;
59
60 // It exists. See if we have already opened a directory with the same inode.
61 // This occurs when one dir is symlinked to another, for example.
Chris Lattner8b1e8482006-10-30 02:45:16 +000062 DirectoryEntry &UDE =
Chris Lattner22eb9722006-06-18 05:43:12 +000063 UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
64
Chris Lattnera85cbe22006-10-30 03:11:40 +000065 if (UDE.getName()) // Already have an entry with this inode, return it.
Chris Lattneraf653752006-10-30 03:06:54 +000066 return NamedDirEnt = &UDE;
Chris Lattner22eb9722006-06-18 05:43:12 +000067
Chris Lattnera85cbe22006-10-30 03:11:40 +000068 // Otherwise, we don't have this directory yet, add it. We use the string
69 // key from the DirEntries map as the string.
Chris Lattner43fd42e2006-10-30 03:40:58 +000070 UDE.Name = InterndDirName;
Chris Lattneraf653752006-10-30 03:06:54 +000071 return NamedDirEnt = &UDE;
Chris Lattner22eb9722006-06-18 05:43:12 +000072}
73
74/// getFile - Lookup, cache, and verify the specified file. This returns null
75/// if the file doesn't exist.
76///
77const FileEntry *FileManager::getFile(const std::string &Filename) {
78 ++NumFileLookups;
79
80 // See if there is already an entry in the map.
81 std::map<std::string, FileEntry*>::iterator I =
82 FileEntries.lower_bound(Filename);
83 if (I != FileEntries.end() && I->first == Filename)
84 return I->second;
85
86 ++NumFileCacheMisses;
87
88 // By default, zero initialize it.
89 FileEntry *&Ent =
90 FileEntries.insert(I, std::make_pair(Filename, (FileEntry*)0))->second;
91
92 // Figure out what directory it is in.
93 std::string DirName;
94
95 // If the string contains a / in it, strip off everything after it.
96 // FIXME: this logic should be in sys::Path.
97 std::string::size_type SlashPos = Filename.find_last_of('/');
98 if (SlashPos == std::string::npos)
99 DirName = "."; // Use the current directory if file has no path component.
100 else if (SlashPos == Filename.size()-1)
101 return 0; // If filename ends with a /, it's a directory.
102 else
103 DirName = std::string(Filename.begin(), Filename.begin()+SlashPos);
104
105 const DirectoryEntry *DirInfo = getDirectory(DirName);
106 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
107 return 0;
108
109 // FIXME: Use the directory info to prune this, before doing the stat syscall.
110 // FIXME: This will reduce the # syscalls.
111
112 // Nope, there isn't. Check to see if the file exists.
113 struct stat StatBuf;
Chris Lattner81500bc2006-07-19 03:40:07 +0000114 //std::cerr << "STATING: " << Filename;
Chris Lattner22eb9722006-06-18 05:43:12 +0000115 if (stat(Filename.c_str(), &StatBuf) || // Error stat'ing.
Chris Lattner81500bc2006-07-19 03:40:07 +0000116 S_ISDIR(StatBuf.st_mode)) { // A directory?
117 // If this file doesn't exist, we leave a null in FileEntries for this path.
118 //std::cerr << ": Not existing\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000119 return 0;
Chris Lattner81500bc2006-07-19 03:40:07 +0000120 }
121 //std::cerr << ": exists\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000122
123 // It exists. See if we have already opened a directory with the same inode.
124 // This occurs when one dir is symlinked to another, for example.
Chris Lattner8b1e8482006-10-30 02:45:16 +0000125 FileEntry &UFE = UniqueFiles[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
Chris Lattner22eb9722006-06-18 05:43:12 +0000126
Chris Lattner8b1e8482006-10-30 02:45:16 +0000127 if (UFE.getUID() != ~0U) // Already have an entry with this inode, return it.
128 return Ent = &UFE;
Chris Lattner269c2322006-06-25 06:23:00 +0000129
Chris Lattner22eb9722006-06-18 05:43:12 +0000130 // Otherwise, we don't have this directory yet, add it.
Chris Lattner8b1e8482006-10-30 02:45:16 +0000131 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
132 // key.
133 UFE.Name = Filename;
134 UFE.Size = StatBuf.st_size;
135 UFE.ModTime = StatBuf.st_mtime;
136 UFE.Dir = DirInfo;
137 UFE.UID = NextFileUID++;
138 return Ent = &UFE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000139}
140
141void FileManager::PrintStats() const {
142 std::cerr << "\n*** File Manager Stats:\n";
143 std::cerr << UniqueFiles.size() << " files found, "
144 << UniqueDirs.size() << " dirs found.\n";
145 std::cerr << NumDirLookups << " dir lookups, "
146 << NumDirCacheMisses << " dir cache misses.\n";
147 std::cerr << NumFileLookups << " file lookups, "
148 << NumFileCacheMisses << " file cache misses.\n";
149
150 //std::cerr << PagesMapped << BytesOfPagesMapped << FSLookups;
151}