Ted Kremenek | 8fbc88e | 2007-12-04 22:42:20 +0000 | [diff] [blame] | 1 | ///===--- FileManager.cpp - File System Probing and Caching ----------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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" |
Chris Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 23 | #include "llvm/System/Path.h" |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 24 | #include "llvm/Config/config.h" |
Benjamin Kramer | 458fb10 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 25 | #include <map> |
| 26 | #include <set> |
| 27 | #include <string> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | |
| 30 | // FIXME: Enhance libsystem to support inode and other fields. |
| 31 | #include <sys/stat.h> |
| 32 | |
Chris Lattner | a8c11c6 | 2007-09-03 18:37:14 +0000 | [diff] [blame] | 33 | #if defined(_MSC_VER) |
Chris Lattner | 3102c83 | 2009-02-12 01:37:35 +0000 | [diff] [blame] | 34 | #define S_ISDIR(s) (_S_IFDIR & s) |
Chris Lattner | a8c11c6 | 2007-09-03 18:37:14 +0000 | [diff] [blame] | 35 | #endif |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 37 | /// NON_EXISTENT_DIR - A special value distinct from null that is used to |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | /// represent a dir name that doesn't exist on the disk. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 39 | #define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | |
Ted Kremenek | cb8d58b | 2009-01-28 00:27:31 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | // Windows. |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 45 | #ifdef LLVM_ON_WIN32 |
| 46 | |
| 47 | #define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\') |
| 48 | |
| 49 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | static std::string GetFullPath(const char *relPath) { |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 51 | char *absPathStrPtr = _fullpath(NULL, relPath, 0); |
| 52 | assert(absPathStrPtr && "_fullpath() returned NULL!"); |
| 53 | |
| 54 | std::string absPath(absPathStrPtr); |
| 55 | |
| 56 | free(absPathStrPtr); |
| 57 | return absPath; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | class FileManager::UniqueDirContainer { |
| 62 | /// UniqueDirs - Cache from full path to existing directories/files. |
| 63 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | llvm::StringMap<DirectoryEntry> UniqueDirs; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 65 | |
| 66 | public: |
| 67 | DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) { |
| 68 | std::string FullPath(GetFullPath(Name)); |
| 69 | return UniqueDirs.GetOrCreateValue( |
| 70 | FullPath.c_str(), |
| 71 | FullPath.c_str() + FullPath.size() |
| 72 | ).getValue(); |
| 73 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 75 | size_t size() { return UniqueDirs.size(); } |
| 76 | }; |
| 77 | |
| 78 | class FileManager::UniqueFileContainer { |
| 79 | /// UniqueFiles - Cache from full path to existing directories/files. |
| 80 | /// |
Ted Kremenek | 7536889 | 2009-01-28 01:01:07 +0000 | [diff] [blame] | 81 | llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 82 | |
| 83 | public: |
| 84 | FileEntry &getFile(const char *Name, struct stat &StatBuf) { |
| 85 | std::string FullPath(GetFullPath(Name)); |
| 86 | return UniqueFiles.GetOrCreateValue( |
| 87 | FullPath.c_str(), |
| 88 | FullPath.c_str() + FullPath.size() |
| 89 | ).getValue(); |
| 90 | } |
| 91 | |
| 92 | size_t size() { return UniqueFiles.size(); } |
| 93 | }; |
| 94 | |
Ted Kremenek | cb8d58b | 2009-01-28 00:27:31 +0000 | [diff] [blame] | 95 | //===----------------------------------------------------------------------===// |
| 96 | // Unix-like Systems. |
| 97 | //===----------------------------------------------------------------------===// |
| 98 | |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 99 | #else |
| 100 | |
| 101 | #define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/') |
| 102 | |
| 103 | class FileManager::UniqueDirContainer { |
| 104 | /// UniqueDirs - Cache from ID's to existing directories/files. |
| 105 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 107 | |
| 108 | public: |
| 109 | DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) { |
| 110 | return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)]; |
| 111 | } |
| 112 | |
| 113 | size_t size() { return UniqueDirs.size(); } |
| 114 | }; |
| 115 | |
| 116 | class FileManager::UniqueFileContainer { |
| 117 | /// UniqueFiles - Cache from ID's to existing directories/files. |
| 118 | /// |
| 119 | std::set<FileEntry> UniqueFiles; |
| 120 | |
| 121 | public: |
| 122 | FileEntry &getFile(const char *Name, struct stat &StatBuf) { |
| 123 | return |
| 124 | const_cast<FileEntry&>( |
| 125 | *UniqueFiles.insert(FileEntry(StatBuf.st_dev, |
Ted Kremenek | 96438f3 | 2009-02-12 03:17:57 +0000 | [diff] [blame] | 126 | StatBuf.st_ino, |
| 127 | StatBuf.st_mode)).first); |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | size_t size() { return UniqueFiles.size(); } |
| 131 | }; |
| 132 | |
| 133 | #endif |
| 134 | |
Ted Kremenek | cb8d58b | 2009-01-28 00:27:31 +0000 | [diff] [blame] | 135 | //===----------------------------------------------------------------------===// |
| 136 | // Common logic. |
| 137 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | 96438f3 | 2009-02-12 03:17:57 +0000 | [diff] [blame] | 139 | FileManager::FileManager() |
Ted Kremenek | fc7052d | 2009-02-12 00:39:05 +0000 | [diff] [blame] | 140 | : UniqueDirs(*new UniqueDirContainer), |
| 141 | UniqueFiles(*new UniqueFileContainer), |
Ted Kremenek | 96438f3 | 2009-02-12 03:17:57 +0000 | [diff] [blame] | 142 | DirEntries(64), FileEntries(64), NextFileUID(0) { |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 143 | NumDirLookups = NumFileLookups = 0; |
| 144 | NumDirCacheMisses = NumFileCacheMisses = 0; |
| 145 | } |
| 146 | |
| 147 | FileManager::~FileManager() { |
| 148 | delete &UniqueDirs; |
| 149 | delete &UniqueFiles; |
| 150 | } |
| 151 | |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 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); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | StatSysCallCache *LastCache = StatCache.get(); |
| 161 | while (LastCache->getNextStatCache()) |
| 162 | LastCache = LastCache->getNextStatCache(); |
| 163 | |
| 164 | LastCache->setNextStatCache(statCache); |
| 165 | } |
| 166 | |
| 167 | void FileManager::removeStatCache(StatSysCallCache *statCache) { |
| 168 | if (!statCache) |
| 169 | return; |
| 170 | |
| 171 | if (StatCache.get() == statCache) { |
| 172 | // This is the first stat cache. |
| 173 | StatCache.reset(StatCache->takeNextStatCache()); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | // Find the stat cache in the list. |
| 178 | StatSysCallCache *PrevCache = StatCache.get(); |
| 179 | while (PrevCache && PrevCache->getNextStatCache() != statCache) |
| 180 | PrevCache = PrevCache->getNextStatCache(); |
| 181 | if (PrevCache) |
| 182 | PrevCache->setNextStatCache(statCache->getNextStatCache()); |
| 183 | else |
| 184 | assert(false && "Stat cache not found for removal"); |
| 185 | } |
| 186 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 187 | /// getDirectory - Lookup, cache, and verify the specified directory. This |
| 188 | /// returns null if the directory doesn't exist. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 189 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | const DirectoryEntry *FileManager::getDirectory(const char *NameStart, |
| 191 | const char *NameEnd) { |
| 192 | ++NumDirLookups; |
| 193 | llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt = |
| 194 | DirEntries.GetOrCreateValue(NameStart, NameEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 196 | // See if there is already an entry in the map. |
| 197 | if (NamedDirEnt.getValue()) |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 198 | return NamedDirEnt.getValue() == NON_EXISTENT_DIR |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 199 | ? 0 : NamedDirEnt.getValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 201 | ++NumDirCacheMisses; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 203 | // By default, initialize it to invalid. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 204 | NamedDirEnt.setValue(NON_EXISTENT_DIR); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 206 | // Get the null-terminated directory name as stored as the key of the |
| 207 | // DirEntries map. |
| 208 | const char *InterndDirName = NamedDirEnt.getKeyData(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 210 | // Check to see if the directory exists. |
| 211 | struct stat StatBuf; |
Ted Kremenek | fc7052d | 2009-02-12 00:39:05 +0000 | [diff] [blame] | 212 | if (stat_cached(InterndDirName, &StatBuf) || // Error stat'ing. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 213 | !S_ISDIR(StatBuf.st_mode)) // Not a directory? |
| 214 | return 0; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 215 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 216 | // It exists. See if we have already opened a directory with the same inode. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | // This occurs when one dir is symlinked to another, for example. |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 218 | DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 220 | NamedDirEnt.setValue(&UDE); |
| 221 | if (UDE.getName()) // Already have an entry with this inode, return it. |
| 222 | return &UDE; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 223 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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; |
| 227 | return &UDE; |
| 228 | } |
| 229 | |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 230 | /// NON_EXISTENT_FILE - A special value distinct from null that is used to |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 231 | /// represent a filename that doesn't exist on the disk. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 232 | #define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 233 | |
| 234 | /// getFile - Lookup, cache, and verify the specified file. This returns null |
| 235 | /// if the file doesn't exist. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 237 | const FileEntry *FileManager::getFile(const char *NameStart, |
| 238 | const char *NameEnd) { |
| 239 | ++NumFileLookups; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 241 | // See if there is already an entry in the map. |
| 242 | llvm::StringMapEntry<FileEntry *> &NamedFileEnt = |
| 243 | FileEntries.GetOrCreateValue(NameStart, NameEnd); |
| 244 | |
| 245 | // See if there is already an entry in the map. |
| 246 | if (NamedFileEnt.getValue()) |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 247 | return NamedFileEnt.getValue() == NON_EXISTENT_FILE |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 248 | ? 0 : NamedFileEnt.getValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 250 | ++NumFileCacheMisses; |
| 251 | |
| 252 | // By default, initialize it to invalid. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 253 | NamedFileEnt.setValue(NON_EXISTENT_FILE); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 254 | |
| 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; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 259 | while (SlashPos >= NameStart && !IS_DIR_SEPARATOR_CHAR(SlashPos[0])) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | --SlashPos; |
Chris Lattner | 46730b2 | 2009-08-12 17:50:39 +0000 | [diff] [blame] | 261 | // Ignore duplicate //'s. |
| 262 | while (SlashPos > NameStart && IS_DIR_SEPARATOR_CHAR(SlashPos[-1])) |
| 263 | --SlashPos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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. |
| 272 | else |
| 273 | DirInfo = getDirectory(NameStart, SlashPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 275 | if (DirInfo == 0) // Directory doesn't exist, file can't exist. |
| 276 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 278 | // Get the null-terminated file name as stored as the key of the |
| 279 | // FileEntries map. |
| 280 | const char *InterndFileName = NamedFileEnt.getKeyData(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 282 | // FIXME: Use the directory info to prune this, before doing the stat syscall. |
| 283 | // FIXME: This will reduce the # syscalls. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 285 | // Nope, there isn't. Check to see if the file exists. |
| 286 | struct stat StatBuf; |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 287 | //llvm::errs() << "STATING: " << Filename; |
Ted Kremenek | fc7052d | 2009-02-12 00:39:05 +0000 | [diff] [blame] | 288 | if (stat_cached(InterndFileName, &StatBuf) || // Error stat'ing. |
| 289 | S_ISDIR(StatBuf.st_mode)) { // A directory? |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 290 | // If this file doesn't exist, we leave a null in FileEntries for this path. |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 291 | //llvm::errs() << ": Not existing\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 292 | return 0; |
| 293 | } |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 294 | //llvm::errs() << ": exists\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | |
Ted Kremenek | bca6d12 | 2007-12-18 22:29:39 +0000 | [diff] [blame] | 296 | // It exists. See if we have already opened a file with the same inode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 297 | // This occurs when one dir is symlinked to another, for example. |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 298 | FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 300 | NamedFileEnt.setValue(&UFE); |
| 301 | if (UFE.getName()) // Already have an entry with this inode, return it. |
| 302 | return &UFE; |
| 303 | |
| 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' |
| 306 | // key. |
| 307 | UFE.Name = InterndFileName; |
| 308 | UFE.Size = StatBuf.st_size; |
| 309 | UFE.ModTime = StatBuf.st_mtime; |
| 310 | UFE.Dir = DirInfo; |
| 311 | UFE.UID = NextFileUID++; |
| 312 | return &UFE; |
| 313 | } |
| 314 | |
| 315 | void FileManager::PrintStats() const { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 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"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 324 | //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 325 | } |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 326 | |
| 327 | int MemorizeStatCalls::stat(const char *path, struct stat *buf) { |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 328 | int result = StatSysCallCache::stat(path, buf); |
| 329 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | if (result != 0) { |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 331 | // Cache failed 'stat' results. |
| 332 | struct stat empty; |
Chris Lattner | f1affe6 | 2009-09-18 04:51:01 +0000 | [diff] [blame] | 333 | memset(&empty, 0, sizeof(empty)); |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 334 | StatCalls[path] = StatResult(result, empty); |
| 335 | } |
| 336 | else if (!S_ISDIR(buf->st_mode) || llvm::sys::Path(path).isAbsolute()) { |
| 337 | // Cache file 'stat' results and directories with absolutely |
| 338 | // paths. |
| 339 | StatCalls[path] = StatResult(result, *buf); |
| 340 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | |
| 342 | return result; |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 343 | } |