FileManager.cpp revision 4fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57
1///===--- FileManager.cpp - File System Probing and Caching ----------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// 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 "llvm/ADT/SmallString.h" 22#include "llvm/System/Path.h" 23#include "llvm/Support/Streams.h" 24#include "llvm/Config/config.h" 25using namespace clang; 26 27// FIXME: Enhance libsystem to support inode and other fields. 28#include <sys/stat.h> 29 30#if defined(_MSC_VER) 31#define S_ISDIR(s) (_S_IFDIR & s) 32#endif 33 34/// NON_EXISTENT_DIR - A special value distinct from null that is used to 35/// represent a dir name that doesn't exist on the disk. 36#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1) 37 38//===----------------------------------------------------------------------===// 39// Windows. 40//===----------------------------------------------------------------------===// 41 42#ifdef LLVM_ON_WIN32 43 44#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\') 45 46namespace { 47 static std::string GetFullPath(const char *relPath) 48 { 49 char *absPathStrPtr = _fullpath(NULL, relPath, 0); 50 assert(absPathStrPtr && "_fullpath() returned NULL!"); 51 52 std::string absPath(absPathStrPtr); 53 54 free(absPathStrPtr); 55 return absPath; 56 } 57} 58 59class FileManager::UniqueDirContainer { 60 /// UniqueDirs - Cache from full path to existing directories/files. 61 /// 62 llvm::StringMap<DirectoryEntry> UniqueDirs; 63 64public: 65 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) { 66 std::string FullPath(GetFullPath(Name)); 67 return UniqueDirs.GetOrCreateValue( 68 FullPath.c_str(), 69 FullPath.c_str() + FullPath.size() 70 ).getValue(); 71 } 72 73 size_t size() { return UniqueDirs.size(); } 74}; 75 76class FileManager::UniqueFileContainer { 77 /// UniqueFiles - Cache from full path to existing directories/files. 78 /// 79 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles; 80 81public: 82 FileEntry &getFile(const char *Name, struct stat &StatBuf) { 83 std::string FullPath(GetFullPath(Name)); 84 return UniqueFiles.GetOrCreateValue( 85 FullPath.c_str(), 86 FullPath.c_str() + FullPath.size() 87 ).getValue(); 88 } 89 90 size_t size() { return UniqueFiles.size(); } 91}; 92 93//===----------------------------------------------------------------------===// 94// Unix-like Systems. 95//===----------------------------------------------------------------------===// 96 97#else 98 99#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/') 100 101class FileManager::UniqueDirContainer { 102 /// UniqueDirs - Cache from ID's to existing directories/files. 103 /// 104 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs; 105 106public: 107 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) { 108 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)]; 109 } 110 111 size_t size() { return UniqueDirs.size(); } 112}; 113 114class FileManager::UniqueFileContainer { 115 /// UniqueFiles - Cache from ID's to existing directories/files. 116 /// 117 std::set<FileEntry> UniqueFiles; 118 119public: 120 FileEntry &getFile(const char *Name, struct stat &StatBuf) { 121 return 122 const_cast<FileEntry&>( 123 *UniqueFiles.insert(FileEntry(StatBuf.st_dev, 124 StatBuf.st_ino, 125 StatBuf.st_mode)).first); 126 } 127 128 size_t size() { return UniqueFiles.size(); } 129}; 130 131#endif 132 133//===----------------------------------------------------------------------===// 134// Common logic. 135//===----------------------------------------------------------------------===// 136 137FileManager::FileManager() 138 : UniqueDirs(*new UniqueDirContainer), 139 UniqueFiles(*new UniqueFileContainer), 140 DirEntries(64), FileEntries(64), NextFileUID(0) { 141 NumDirLookups = NumFileLookups = 0; 142 NumDirCacheMisses = NumFileCacheMisses = 0; 143} 144 145FileManager::~FileManager() { 146 delete &UniqueDirs; 147 delete &UniqueFiles; 148} 149 150/// getDirectory - Lookup, cache, and verify the specified directory. This 151/// returns null if the directory doesn't exist. 152/// 153const DirectoryEntry *FileManager::getDirectory(const char *NameStart, 154 const char *NameEnd) { 155 ++NumDirLookups; 156 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt = 157 DirEntries.GetOrCreateValue(NameStart, NameEnd); 158 159 // See if there is already an entry in the map. 160 if (NamedDirEnt.getValue()) 161 return NamedDirEnt.getValue() == NON_EXISTENT_DIR 162 ? 0 : NamedDirEnt.getValue(); 163 164 ++NumDirCacheMisses; 165 166 // By default, initialize it to invalid. 167 NamedDirEnt.setValue(NON_EXISTENT_DIR); 168 169 // Get the null-terminated directory name as stored as the key of the 170 // DirEntries map. 171 const char *InterndDirName = NamedDirEnt.getKeyData(); 172 173 // Check to see if the directory exists. 174 struct stat StatBuf; 175 if (stat_cached(InterndDirName, &StatBuf) || // Error stat'ing. 176 !S_ISDIR(StatBuf.st_mode)) // Not a directory? 177 return 0; 178 179 // It exists. See if we have already opened a directory with the same inode. 180 // This occurs when one dir is symlinked to another, for example. 181 DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf); 182 183 NamedDirEnt.setValue(&UDE); 184 if (UDE.getName()) // Already have an entry with this inode, return it. 185 return &UDE; 186 187 // Otherwise, we don't have this directory yet, add it. We use the string 188 // key from the DirEntries map as the string. 189 UDE.Name = InterndDirName; 190 return &UDE; 191} 192 193/// NON_EXISTENT_FILE - A special value distinct from null that is used to 194/// represent a filename that doesn't exist on the disk. 195#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1) 196 197/// getFile - Lookup, cache, and verify the specified file. This returns null 198/// if the file doesn't exist. 199/// 200const FileEntry *FileManager::getFile(const char *NameStart, 201 const char *NameEnd) { 202 ++NumFileLookups; 203 204 // See if there is already an entry in the map. 205 llvm::StringMapEntry<FileEntry *> &NamedFileEnt = 206 FileEntries.GetOrCreateValue(NameStart, NameEnd); 207 208 // See if there is already an entry in the map. 209 if (NamedFileEnt.getValue()) 210 return NamedFileEnt.getValue() == NON_EXISTENT_FILE 211 ? 0 : NamedFileEnt.getValue(); 212 213 ++NumFileCacheMisses; 214 215 // By default, initialize it to invalid. 216 NamedFileEnt.setValue(NON_EXISTENT_FILE); 217 218 // Figure out what directory it is in. If the string contains a / in it, 219 // strip off everything after it. 220 // FIXME: this logic should be in sys::Path. 221 const char *SlashPos = NameEnd-1; 222 while (SlashPos >= NameStart && !IS_DIR_SEPARATOR_CHAR(SlashPos[0])) 223 --SlashPos; 224 225 const DirectoryEntry *DirInfo; 226 if (SlashPos < NameStart) { 227 // Use the current directory if file has no path component. 228 const char *Name = "."; 229 DirInfo = getDirectory(Name, Name+1); 230 } else if (SlashPos == NameEnd-1) 231 return 0; // If filename ends with a /, it's a directory. 232 else 233 DirInfo = getDirectory(NameStart, SlashPos); 234 235 if (DirInfo == 0) // Directory doesn't exist, file can't exist. 236 return 0; 237 238 // Get the null-terminated file name as stored as the key of the 239 // FileEntries map. 240 const char *InterndFileName = NamedFileEnt.getKeyData(); 241 242 // FIXME: Use the directory info to prune this, before doing the stat syscall. 243 // FIXME: This will reduce the # syscalls. 244 245 // Nope, there isn't. Check to see if the file exists. 246 struct stat StatBuf; 247 //llvm::cerr << "STATING: " << Filename; 248 if (stat_cached(InterndFileName, &StatBuf) || // Error stat'ing. 249 S_ISDIR(StatBuf.st_mode)) { // A directory? 250 // If this file doesn't exist, we leave a null in FileEntries for this path. 251 //llvm::cerr << ": Not existing\n"; 252 return 0; 253 } 254 //llvm::cerr << ": exists\n"; 255 256 // It exists. See if we have already opened a file with the same inode. 257 // This occurs when one dir is symlinked to another, for example. 258 FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf); 259 260 NamedFileEnt.setValue(&UFE); 261 if (UFE.getName()) // Already have an entry with this inode, return it. 262 return &UFE; 263 264 // Otherwise, we don't have this directory yet, add it. 265 // FIXME: Change the name to be a char* that points back to the 'FileEntries' 266 // key. 267 UFE.Name = InterndFileName; 268 UFE.Size = StatBuf.st_size; 269 UFE.ModTime = StatBuf.st_mtime; 270 UFE.Dir = DirInfo; 271 UFE.UID = NextFileUID++; 272 return &UFE; 273} 274 275void FileManager::PrintStats() const { 276 llvm::cerr << "\n*** File Manager Stats:\n"; 277 llvm::cerr << UniqueFiles.size() << " files found, " 278 << UniqueDirs.size() << " dirs found.\n"; 279 llvm::cerr << NumDirLookups << " dir lookups, " 280 << NumDirCacheMisses << " dir cache misses.\n"; 281 llvm::cerr << NumFileLookups << " file lookups, " 282 << NumFileCacheMisses << " file cache misses.\n"; 283 284 //llvm::cerr << PagesMapped << BytesOfPagesMapped << FSLookups; 285} 286 287int MemorizeStatCalls::stat(const char *path, struct stat *buf) { 288 int result = ::stat(path, buf); 289 290 if (result != 0) { 291 // Cache failed 'stat' results. 292 struct stat empty; 293 StatCalls[path] = StatResult(result, empty); 294 } 295 else if (!S_ISDIR(buf->st_mode) || llvm::sys::Path(path).isAbsolute()) { 296 // Cache file 'stat' results and directories with absolutely 297 // paths. 298 StatCalls[path] = StatResult(result, *buf); 299 } 300 301 return result; 302} 303