Chris Lattner | 10e286a | 2010-11-23 19:19:34 +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" |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 21 | #include "clang/Basic/FileSystemStatCache.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | c070da4 | 2010-08-23 23:50:42 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 27 | #include "llvm/Support/system_error.h" |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 28 | #include "llvm/Config/config.h" |
Benjamin Kramer | 458fb10 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 29 | #include <map> |
| 30 | #include <set> |
| 31 | #include <string> |
Chris Lattner | 291fcf0 | 2010-11-23 21:53:15 +0000 | [diff] [blame] | 32 | |
| 33 | // FIXME: This is terrible, we need this for ::close. |
| 34 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 35 | #include <unistd.h> |
| 36 | #include <sys/uio.h> |
| 37 | #else |
| 38 | #include <io.h> |
| 39 | #endif |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | using namespace clang; |
| 41 | |
| 42 | // FIXME: Enhance libsystem to support inode and other fields. |
| 43 | #include <sys/stat.h> |
| 44 | |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 45 | /// 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] | 46 | /// represent a dir name that doesn't exist on the disk. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 47 | #define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | |
Chris Lattner | f9f7766 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 49 | /// NON_EXISTENT_FILE - A special value distinct from null that is used to |
| 50 | /// represent a filename that doesn't exist on the disk. |
| 51 | #define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1) |
| 52 | |
| 53 | |
| 54 | FileEntry::~FileEntry() { |
| 55 | // If this FileEntry owns an open file descriptor that never got used, close |
| 56 | // it. |
| 57 | if (FD != -1) ::close(FD); |
| 58 | } |
| 59 | |
Ted Kremenek | cb8d58b | 2009-01-28 00:27:31 +0000 | [diff] [blame] | 60 | //===----------------------------------------------------------------------===// |
| 61 | // Windows. |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 64 | #ifdef LLVM_ON_WIN32 |
| 65 | |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 66 | #define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\') |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 67 | |
| 68 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | static std::string GetFullPath(const char *relPath) { |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 70 | char *absPathStrPtr = _fullpath(NULL, relPath, 0); |
| 71 | assert(absPathStrPtr && "_fullpath() returned NULL!"); |
| 72 | |
| 73 | std::string absPath(absPathStrPtr); |
| 74 | |
| 75 | free(absPathStrPtr); |
| 76 | return absPath; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | class FileManager::UniqueDirContainer { |
| 81 | /// UniqueDirs - Cache from full path to existing directories/files. |
| 82 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | llvm::StringMap<DirectoryEntry> UniqueDirs; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 84 | |
| 85 | public: |
| 86 | DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) { |
| 87 | std::string FullPath(GetFullPath(Name)); |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 88 | return UniqueDirs.GetOrCreateValue(FullPath).getValue(); |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 89 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 91 | size_t size() const { return UniqueDirs.size(); } |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | class FileManager::UniqueFileContainer { |
| 95 | /// UniqueFiles - Cache from full path to existing directories/files. |
| 96 | /// |
Ted Kremenek | 7536889 | 2009-01-28 01:01:07 +0000 | [diff] [blame] | 97 | llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 98 | |
| 99 | public: |
| 100 | FileEntry &getFile(const char *Name, struct stat &StatBuf) { |
| 101 | std::string FullPath(GetFullPath(Name)); |
Chris Lattner | c070da4 | 2010-08-23 23:50:42 +0000 | [diff] [blame] | 102 | |
| 103 | // LowercaseString because Windows filesystem is case insensitive. |
| 104 | FullPath = llvm::LowercaseString(FullPath); |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 105 | return UniqueFiles.GetOrCreateValue(FullPath).getValue(); |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 108 | size_t size() const { return UniqueFiles.size(); } |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Ted Kremenek | cb8d58b | 2009-01-28 00:27:31 +0000 | [diff] [blame] | 111 | //===----------------------------------------------------------------------===// |
| 112 | // Unix-like Systems. |
| 113 | //===----------------------------------------------------------------------===// |
| 114 | |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 115 | #else |
| 116 | |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 117 | #define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/') |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 118 | |
| 119 | class FileManager::UniqueDirContainer { |
| 120 | /// UniqueDirs - Cache from ID's to existing directories/files. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 122 | |
| 123 | public: |
| 124 | DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) { |
| 125 | return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)]; |
| 126 | } |
| 127 | |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 128 | size_t size() const { return UniqueDirs.size(); } |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | class FileManager::UniqueFileContainer { |
| 132 | /// UniqueFiles - Cache from ID's to existing directories/files. |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 133 | std::set<FileEntry> UniqueFiles; |
| 134 | |
| 135 | public: |
| 136 | FileEntry &getFile(const char *Name, struct stat &StatBuf) { |
| 137 | return |
| 138 | const_cast<FileEntry&>( |
| 139 | *UniqueFiles.insert(FileEntry(StatBuf.st_dev, |
Ted Kremenek | 96438f3 | 2009-02-12 03:17:57 +0000 | [diff] [blame] | 140 | StatBuf.st_ino, |
| 141 | StatBuf.st_mode)).first); |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 144 | size_t size() const { return UniqueFiles.size(); } |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | #endif |
| 148 | |
Ted Kremenek | cb8d58b | 2009-01-28 00:27:31 +0000 | [diff] [blame] | 149 | //===----------------------------------------------------------------------===// |
| 150 | // Common logic. |
| 151 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 7ad97ff | 2010-11-23 07:51:02 +0000 | [diff] [blame] | 153 | FileManager::FileManager(const FileSystemOptions &FSO) |
| 154 | : FileSystemOpts(FSO), |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 155 | UniqueDirs(*new UniqueDirContainer()), |
| 156 | UniqueFiles(*new UniqueFileContainer()), |
Ted Kremenek | 96438f3 | 2009-02-12 03:17:57 +0000 | [diff] [blame] | 157 | DirEntries(64), FileEntries(64), NextFileUID(0) { |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 158 | NumDirLookups = NumFileLookups = 0; |
| 159 | NumDirCacheMisses = NumFileCacheMisses = 0; |
| 160 | } |
| 161 | |
| 162 | FileManager::~FileManager() { |
| 163 | delete &UniqueDirs; |
| 164 | delete &UniqueFiles; |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 165 | for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i) |
| 166 | delete VirtualFileEntries[i]; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 169 | void FileManager::addStatCache(FileSystemStatCache *statCache, |
| 170 | bool AtBeginning) { |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 171 | assert(statCache && "No stat cache provided?"); |
| 172 | if (AtBeginning || StatCache.get() == 0) { |
| 173 | statCache->setNextStatCache(StatCache.take()); |
| 174 | StatCache.reset(statCache); |
| 175 | return; |
| 176 | } |
| 177 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 178 | FileSystemStatCache *LastCache = StatCache.get(); |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 179 | while (LastCache->getNextStatCache()) |
| 180 | LastCache = LastCache->getNextStatCache(); |
| 181 | |
| 182 | LastCache->setNextStatCache(statCache); |
| 183 | } |
| 184 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 185 | void FileManager::removeStatCache(FileSystemStatCache *statCache) { |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 186 | if (!statCache) |
| 187 | return; |
| 188 | |
| 189 | if (StatCache.get() == statCache) { |
| 190 | // This is the first stat cache. |
| 191 | StatCache.reset(StatCache->takeNextStatCache()); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | // Find the stat cache in the list. |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 196 | FileSystemStatCache *PrevCache = StatCache.get(); |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 197 | while (PrevCache && PrevCache->getNextStatCache() != statCache) |
| 198 | PrevCache = PrevCache->getNextStatCache(); |
Chris Lattner | f9f7766 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 199 | |
| 200 | assert(PrevCache && "Stat cache not found for removal"); |
| 201 | PrevCache->setNextStatCache(statCache->getNextStatCache()); |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 204 | /// \brief Retrieve the directory that the given file name resides in. |
| 205 | static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr, |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 206 | llvm::StringRef Filename) { |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 207 | // Figure out what directory it is in. If the string contains a / in it, |
| 208 | // strip off everything after it. |
| 209 | // FIXME: this logic should be in sys::Path. |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 210 | size_t SlashPos = Filename.size(); |
| 211 | while (SlashPos != 0 && !IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1])) |
| 212 | --SlashPos; |
| 213 | |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 214 | // Use the current directory if file has no path component. |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 215 | if (SlashPos == 0) |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 216 | return FileMgr.getDirectory("."); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 217 | |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 218 | if (SlashPos == Filename.size()-1) |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 219 | return 0; // If filename ends with a /, it's a directory. |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 220 | |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 221 | // Ignore repeated //'s. |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 222 | while (SlashPos != 0 && IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1])) |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 223 | --SlashPos; |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 225 | return FileMgr.getDirectory(Filename.substr(0, SlashPos)); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 228 | /// getDirectory - Lookup, cache, and verify the specified directory. This |
| 229 | /// returns null if the directory doesn't exist. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | /// |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 231 | const DirectoryEntry *FileManager::getDirectory(llvm::StringRef Filename) { |
John Thompson | 9a6ac54 | 2009-12-18 14:18:21 +0000 | [diff] [blame] | 232 | // stat doesn't like trailing separators (at least on Windows). |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 233 | if (Filename.size() > 1 && IS_DIR_SEPARATOR_CHAR(Filename.back())) |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 234 | Filename = Filename.substr(0, Filename.size()-1); |
John Thompson | 9a6ac54 | 2009-12-18 14:18:21 +0000 | [diff] [blame] | 235 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 236 | ++NumDirLookups; |
| 237 | llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt = |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 238 | DirEntries.GetOrCreateValue(Filename); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 240 | // See if there is already an entry in the map. |
| 241 | if (NamedDirEnt.getValue()) |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 242 | return NamedDirEnt.getValue() == NON_EXISTENT_DIR |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 243 | ? 0 : NamedDirEnt.getValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 245 | ++NumDirCacheMisses; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 247 | // By default, initialize it to invalid. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 248 | NamedDirEnt.setValue(NON_EXISTENT_DIR); |
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 | // Get the null-terminated directory name as stored as the key of the |
| 251 | // DirEntries map. |
| 252 | const char *InterndDirName = NamedDirEnt.getKeyData(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 254 | // Check to see if the directory exists. |
| 255 | struct stat StatBuf; |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 256 | if (getStatValue(InterndDirName, StatBuf, 0/*directory lookup*/)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 257 | return 0; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 258 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 259 | // 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] | 260 | // This occurs when one dir is symlinked to another, for example. |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 261 | DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 263 | NamedDirEnt.setValue(&UDE); |
| 264 | if (UDE.getName()) // Already have an entry with this inode, return it. |
| 265 | return &UDE; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 266 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 267 | // Otherwise, we don't have this directory yet, add it. We use the string |
| 268 | // key from the DirEntries map as the string. |
| 269 | UDE.Name = InterndDirName; |
| 270 | return &UDE; |
| 271 | } |
| 272 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 273 | /// getFile - Lookup, cache, and verify the specified file. This returns null |
| 274 | /// if the file doesn't exist. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 275 | /// |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 276 | const FileEntry *FileManager::getFile(llvm::StringRef Filename) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | ++NumFileLookups; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | // See if there is already an entry in the map. |
| 280 | llvm::StringMapEntry<FileEntry *> &NamedFileEnt = |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 281 | FileEntries.GetOrCreateValue(Filename); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 282 | |
| 283 | // See if there is already an entry in the map. |
| 284 | if (NamedFileEnt.getValue()) |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 285 | return NamedFileEnt.getValue() == NON_EXISTENT_FILE |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | ? 0 : NamedFileEnt.getValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | ++NumFileCacheMisses; |
| 289 | |
| 290 | // By default, initialize it to invalid. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 291 | NamedFileEnt.setValue(NON_EXISTENT_FILE); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 292 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 294 | // Get the null-terminated file name as stored as the key of the |
| 295 | // FileEntries map. |
| 296 | const char *InterndFileName = NamedFileEnt.getKeyData(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 298 | |
| 299 | // Look up the directory for the file. When looking up something like |
| 300 | // sys/foo.h we'll discover all of the search directories that have a 'sys' |
| 301 | // subdirectory. This will let us avoid having to waste time on known-to-fail |
| 302 | // searches when we go to find sys/bar.h, because all the search directories |
| 303 | // without a 'sys' subdir will get a cached failure result. |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 304 | const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 305 | if (DirInfo == 0) // Directory doesn't exist, file can't exist. |
| 306 | return 0; |
| 307 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 308 | // FIXME: Use the directory info to prune this, before doing the stat syscall. |
| 309 | // FIXME: This will reduce the # syscalls. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 310 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 311 | // Nope, there isn't. Check to see if the file exists. |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 312 | int FileDescriptor = -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 313 | struct stat StatBuf; |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 314 | if (getStatValue(InterndFileName, StatBuf, &FileDescriptor)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 315 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | |
Ted Kremenek | bca6d12 | 2007-12-18 22:29:39 +0000 | [diff] [blame] | 317 | // 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] | 318 | // This occurs when one dir is symlinked to another, for example. |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 319 | FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 321 | NamedFileEnt.setValue(&UFE); |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 322 | if (UFE.getName()) { // Already have an entry with this inode, return it. |
| 323 | // If the stat process opened the file, close it to avoid a FD leak. |
| 324 | if (FileDescriptor != -1) |
| 325 | close(FileDescriptor); |
| 326 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 327 | return &UFE; |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 328 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 329 | |
| 330 | // Otherwise, we don't have this directory yet, add it. |
| 331 | // FIXME: Change the name to be a char* that points back to the 'FileEntries' |
| 332 | // key. |
| 333 | UFE.Name = InterndFileName; |
| 334 | UFE.Size = StatBuf.st_size; |
| 335 | UFE.ModTime = StatBuf.st_mtime; |
| 336 | UFE.Dir = DirInfo; |
| 337 | UFE.UID = NextFileUID++; |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 338 | UFE.FD = FileDescriptor; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 339 | return &UFE; |
| 340 | } |
| 341 | |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 342 | const FileEntry * |
Benjamin Kramer | ec1b1cc | 2010-07-14 23:19:41 +0000 | [diff] [blame] | 343 | FileManager::getVirtualFile(llvm::StringRef Filename, off_t Size, |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 344 | time_t ModificationTime) { |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 345 | ++NumFileLookups; |
| 346 | |
| 347 | // See if there is already an entry in the map. |
| 348 | llvm::StringMapEntry<FileEntry *> &NamedFileEnt = |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 349 | FileEntries.GetOrCreateValue(Filename); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 350 | |
| 351 | // See if there is already an entry in the map. |
| 352 | if (NamedFileEnt.getValue()) |
| 353 | return NamedFileEnt.getValue() == NON_EXISTENT_FILE |
| 354 | ? 0 : NamedFileEnt.getValue(); |
| 355 | |
| 356 | ++NumFileCacheMisses; |
| 357 | |
| 358 | // By default, initialize it to invalid. |
| 359 | NamedFileEnt.setValue(NON_EXISTENT_FILE); |
| 360 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 361 | const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 362 | if (DirInfo == 0) // Directory doesn't exist, file can't exist. |
| 363 | return 0; |
| 364 | |
| 365 | FileEntry *UFE = new FileEntry(); |
| 366 | VirtualFileEntries.push_back(UFE); |
| 367 | NamedFileEnt.setValue(UFE); |
| 368 | |
Chris Lattner | f9f7766 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 369 | // Get the null-terminated file name as stored as the key of the |
| 370 | // FileEntries map. |
| 371 | const char *InterndFileName = NamedFileEnt.getKeyData(); |
| 372 | |
| 373 | UFE->Name = InterndFileName; |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 374 | UFE->Size = Size; |
| 375 | UFE->ModTime = ModificationTime; |
| 376 | UFE->Dir = DirInfo; |
| 377 | UFE->UID = NextFileUID++; |
Douglas Gregor | 3e15e0a | 2010-07-26 23:54:23 +0000 | [diff] [blame] | 378 | |
| 379 | // If this virtual file resolves to a file, also map that file to the |
| 380 | // newly-created file entry. |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 381 | int FileDescriptor = -1; |
Douglas Gregor | 3e15e0a | 2010-07-26 23:54:23 +0000 | [diff] [blame] | 382 | struct stat StatBuf; |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 383 | if (getStatValue(InterndFileName, StatBuf, &FileDescriptor)) |
Chris Lattner | f9f7766 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 384 | return UFE; |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 385 | |
| 386 | UFE->FD = FileDescriptor; |
Chris Lattner | f9f7766 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 387 | llvm::sys::Path FilePath(UFE->Name); |
| 388 | FilePath.makeAbsolute(); |
| 389 | FileEntries[FilePath.str()] = UFE; |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 390 | return UFE; |
| 391 | } |
| 392 | |
Chris Lattner | 67452f5 | 2010-11-23 04:45:28 +0000 | [diff] [blame] | 393 | void FileManager::FixupRelativePath(llvm::sys::Path &path, |
| 394 | const FileSystemOptions &FSOpts) { |
| 395 | if (FSOpts.WorkingDir.empty() || path.isAbsolute()) return; |
| 396 | |
| 397 | llvm::sys::Path NewPath(FSOpts.WorkingDir); |
| 398 | NewPath.appendComponent(path.str()); |
| 399 | path = NewPath; |
| 400 | } |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 401 | |
Chris Lattner | 67452f5 | 2010-11-23 04:45:28 +0000 | [diff] [blame] | 402 | llvm::MemoryBuffer *FileManager:: |
Chris Lattner | 75dfb65 | 2010-11-23 09:19:42 +0000 | [diff] [blame] | 403 | getBufferForFile(const FileEntry *Entry, std::string *ErrorStr) { |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 404 | llvm::OwningPtr<llvm::MemoryBuffer> Result; |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 405 | llvm::error_code ec; |
Chris Lattner | 5cc1c73 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 406 | if (FileSystemOpts.WorkingDir.empty()) { |
| 407 | const char *Filename = Entry->getName(); |
| 408 | // If the file is already open, use the open file descriptor. |
| 409 | if (Entry->FD != -1) { |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 410 | ec = llvm::MemoryBuffer::getOpenFile(Entry->FD, Filename, Result, |
| 411 | Entry->getSize()); |
| 412 | if (ErrorStr) |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 413 | *ErrorStr = ec.message(); |
Chris Lattner | 5cc1c73 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 414 | // getOpenFile will have closed the file descriptor, don't reuse or |
| 415 | // reclose it. |
| 416 | Entry->FD = -1; |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 417 | return Result.take(); |
Chris Lattner | 5cc1c73 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | // Otherwise, open the file. |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 421 | ec = llvm::MemoryBuffer::getFile(Filename, Result, Entry->getSize()); |
| 422 | if (ec && ErrorStr) |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 423 | *ErrorStr = ec.message(); |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 424 | return Result.take(); |
Chris Lattner | 5cc1c73 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 425 | } |
Chris Lattner | 67452f5 | 2010-11-23 04:45:28 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 5cc1c73 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 427 | llvm::sys::Path FilePath(Entry->getName()); |
Chris Lattner | 67452f5 | 2010-11-23 04:45:28 +0000 | [diff] [blame] | 428 | FixupRelativePath(FilePath, FileSystemOpts); |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 429 | ec = llvm::MemoryBuffer::getFile(FilePath.c_str(), Result, Entry->getSize()); |
| 430 | if (ec && ErrorStr) |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 431 | *ErrorStr = ec.message(); |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 432 | return Result.take(); |
Chris Lattner | 75dfb65 | 2010-11-23 09:19:42 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | llvm::MemoryBuffer *FileManager:: |
| 436 | getBufferForFile(llvm::StringRef Filename, std::string *ErrorStr) { |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 437 | llvm::OwningPtr<llvm::MemoryBuffer> Result; |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 438 | llvm::error_code ec; |
| 439 | if (FileSystemOpts.WorkingDir.empty()) { |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 440 | ec = llvm::MemoryBuffer::getFile(Filename, Result); |
| 441 | if (ec && ErrorStr) |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 442 | *ErrorStr = ec.message(); |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 443 | return Result.take(); |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Chris Lattner | 75dfb65 | 2010-11-23 09:19:42 +0000 | [diff] [blame] | 446 | llvm::sys::Path FilePath(Filename); |
| 447 | FixupRelativePath(FilePath, FileSystemOpts); |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 448 | ec = llvm::MemoryBuffer::getFile(FilePath.c_str(), Result); |
| 449 | if (ec && ErrorStr) |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 450 | *ErrorStr = ec.message(); |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 451 | return Result.take(); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 454 | /// getStatValue - Get the 'stat' information for the specified path, using the |
Chris Lattner | c0f31fd | 2010-12-02 04:27:29 +0000 | [diff] [blame] | 455 | /// cache to accelerate it if possible. This returns true if the path does not |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 456 | /// exist or false if it exists. |
Chris Lattner | f9f7766 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 457 | /// |
| 458 | /// The isForDir member indicates whether this is a directory lookup or not. |
| 459 | /// This will return failure if the lookup isn't the expected kind. |
| 460 | bool FileManager::getStatValue(const char *Path, struct stat &StatBuf, |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 461 | int *FileDescriptor) { |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 462 | // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be |
| 463 | // absolute! |
Chris Lattner | 11aa4b0 | 2010-11-23 19:56:39 +0000 | [diff] [blame] | 464 | if (FileSystemOpts.WorkingDir.empty()) |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 465 | return FileSystemStatCache::get(Path, StatBuf, FileDescriptor, |
| 466 | StatCache.get()); |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 467 | |
| 468 | llvm::sys::Path FilePath(Path); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 469 | FixupRelativePath(FilePath, FileSystemOpts); |
Chris Lattner | 11aa4b0 | 2010-11-23 19:56:39 +0000 | [diff] [blame] | 470 | |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 471 | return FileSystemStatCache::get(FilePath.c_str(), StatBuf, FileDescriptor, |
| 472 | StatCache.get()); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 475 | |
| 476 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 477 | void FileManager::PrintStats() const { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 478 | llvm::errs() << "\n*** File Manager Stats:\n"; |
| 479 | llvm::errs() << UniqueFiles.size() << " files found, " |
| 480 | << UniqueDirs.size() << " dirs found.\n"; |
| 481 | llvm::errs() << NumDirLookups << " dir lookups, " |
| 482 | << NumDirCacheMisses << " dir cache misses.\n"; |
| 483 | llvm::errs() << NumFileLookups << " file lookups, " |
| 484 | << NumFileCacheMisses << " file cache misses.\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 486 | //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 487 | } |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 488 | |