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