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