Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- FileManager.cpp - File System Probing and Caching ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source 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 <iostream> |
| 23 | using namespace clang; |
| 24 | |
| 25 | // FIXME: Enhance libsystem to support inode and other fields. |
| 26 | #include <sys/stat.h> |
| 27 | |
Chris Lattner | a8c11c6 | 2007-09-03 18:37:14 +0000 | [diff] [blame] | 28 | #if defined(_MSC_VER) |
| 29 | #define S_ISDIR(s) (_S_IFDIR & s) |
| 30 | #endif |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | |
| 32 | /// NON_EXISTANT_DIR - A special value distinct from null that is used to |
| 33 | /// represent a dir name that doesn't exist on the disk. |
| 34 | #define NON_EXISTANT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1) |
| 35 | |
| 36 | /// getDirectory - Lookup, cache, and verify the specified directory. This |
| 37 | /// returns null if the directory doesn't exist. |
| 38 | /// |
| 39 | const DirectoryEntry *FileManager::getDirectory(const char *NameStart, |
| 40 | const char *NameEnd) { |
| 41 | ++NumDirLookups; |
| 42 | llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt = |
| 43 | DirEntries.GetOrCreateValue(NameStart, NameEnd); |
| 44 | |
| 45 | // See if there is already an entry in the map. |
| 46 | if (NamedDirEnt.getValue()) |
| 47 | return NamedDirEnt.getValue() == NON_EXISTANT_DIR |
| 48 | ? 0 : NamedDirEnt.getValue(); |
| 49 | |
| 50 | ++NumDirCacheMisses; |
| 51 | |
| 52 | // By default, initialize it to invalid. |
| 53 | NamedDirEnt.setValue(NON_EXISTANT_DIR); |
| 54 | |
| 55 | // Get the null-terminated directory name as stored as the key of the |
| 56 | // DirEntries map. |
| 57 | const char *InterndDirName = NamedDirEnt.getKeyData(); |
| 58 | |
| 59 | // Check to see if the directory exists. |
| 60 | struct stat StatBuf; |
| 61 | if (stat(InterndDirName, &StatBuf) || // Error stat'ing. |
| 62 | !S_ISDIR(StatBuf.st_mode)) // Not a directory? |
| 63 | return 0; |
| 64 | |
| 65 | // It exists. See if we have already opened a directory with the same inode. |
| 66 | // This occurs when one dir is symlinked to another, for example. |
| 67 | DirectoryEntry &UDE = |
| 68 | UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)]; |
| 69 | |
| 70 | NamedDirEnt.setValue(&UDE); |
| 71 | if (UDE.getName()) // Already have an entry with this inode, return it. |
| 72 | return &UDE; |
| 73 | |
| 74 | // Otherwise, we don't have this directory yet, add it. We use the string |
| 75 | // key from the DirEntries map as the string. |
| 76 | UDE.Name = InterndDirName; |
| 77 | return &UDE; |
| 78 | } |
| 79 | |
| 80 | /// NON_EXISTANT_FILE - A special value distinct from null that is used to |
| 81 | /// represent a filename that doesn't exist on the disk. |
| 82 | #define NON_EXISTANT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1) |
| 83 | |
| 84 | /// getFile - Lookup, cache, and verify the specified file. This returns null |
| 85 | /// if the file doesn't exist. |
| 86 | /// |
| 87 | const FileEntry *FileManager::getFile(const char *NameStart, |
| 88 | const char *NameEnd) { |
| 89 | ++NumFileLookups; |
| 90 | |
| 91 | // See if there is already an entry in the map. |
| 92 | llvm::StringMapEntry<FileEntry *> &NamedFileEnt = |
| 93 | FileEntries.GetOrCreateValue(NameStart, NameEnd); |
| 94 | |
| 95 | // See if there is already an entry in the map. |
| 96 | if (NamedFileEnt.getValue()) |
| 97 | return NamedFileEnt.getValue() == NON_EXISTANT_FILE |
| 98 | ? 0 : NamedFileEnt.getValue(); |
| 99 | |
| 100 | ++NumFileCacheMisses; |
| 101 | |
| 102 | // By default, initialize it to invalid. |
| 103 | NamedFileEnt.setValue(NON_EXISTANT_FILE); |
| 104 | |
| 105 | // Figure out what directory it is in. If the string contains a / in it, |
| 106 | // strip off everything after it. |
| 107 | // FIXME: this logic should be in sys::Path. |
| 108 | const char *SlashPos = NameEnd-1; |
| 109 | while (SlashPos >= NameStart && SlashPos[0] != '/') |
| 110 | --SlashPos; |
| 111 | |
| 112 | const DirectoryEntry *DirInfo; |
| 113 | if (SlashPos < NameStart) { |
| 114 | // Use the current directory if file has no path component. |
| 115 | const char *Name = "."; |
| 116 | DirInfo = getDirectory(Name, Name+1); |
| 117 | } else if (SlashPos == NameEnd-1) |
| 118 | return 0; // If filename ends with a /, it's a directory. |
| 119 | else |
| 120 | DirInfo = getDirectory(NameStart, SlashPos); |
| 121 | |
| 122 | if (DirInfo == 0) // Directory doesn't exist, file can't exist. |
| 123 | return 0; |
| 124 | |
| 125 | // Get the null-terminated file name as stored as the key of the |
| 126 | // FileEntries map. |
| 127 | const char *InterndFileName = NamedFileEnt.getKeyData(); |
| 128 | |
| 129 | // FIXME: Use the directory info to prune this, before doing the stat syscall. |
| 130 | // FIXME: This will reduce the # syscalls. |
| 131 | |
| 132 | // Nope, there isn't. Check to see if the file exists. |
| 133 | struct stat StatBuf; |
| 134 | //std::cerr << "STATING: " << Filename; |
| 135 | if (stat(InterndFileName, &StatBuf) || // Error stat'ing. |
| 136 | S_ISDIR(StatBuf.st_mode)) { // A directory? |
| 137 | // If this file doesn't exist, we leave a null in FileEntries for this path. |
| 138 | //std::cerr << ": Not existing\n"; |
| 139 | return 0; |
| 140 | } |
| 141 | //std::cerr << ": exists\n"; |
| 142 | |
| 143 | // It exists. See if we have already opened a directory with the same inode. |
| 144 | // This occurs when one dir is symlinked to another, for example. |
| 145 | FileEntry &UFE = UniqueFiles[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)]; |
| 146 | |
| 147 | NamedFileEnt.setValue(&UFE); |
| 148 | if (UFE.getName()) // Already have an entry with this inode, return it. |
| 149 | return &UFE; |
| 150 | |
| 151 | // Otherwise, we don't have this directory yet, add it. |
| 152 | // FIXME: Change the name to be a char* that points back to the 'FileEntries' |
| 153 | // key. |
| 154 | UFE.Name = InterndFileName; |
| 155 | UFE.Size = StatBuf.st_size; |
| 156 | UFE.ModTime = StatBuf.st_mtime; |
| 157 | UFE.Dir = DirInfo; |
| 158 | UFE.UID = NextFileUID++; |
| 159 | return &UFE; |
| 160 | } |
| 161 | |
| 162 | void FileManager::PrintStats() const { |
| 163 | std::cerr << "\n*** File Manager Stats:\n"; |
| 164 | std::cerr << UniqueFiles.size() << " files found, " |
| 165 | << UniqueDirs.size() << " dirs found.\n"; |
| 166 | std::cerr << NumDirLookups << " dir lookups, " |
| 167 | << NumDirCacheMisses << " dir cache misses.\n"; |
| 168 | std::cerr << NumFileLookups << " file lookups, " |
| 169 | << NumFileCacheMisses << " file cache misses.\n"; |
| 170 | |
| 171 | //std::cerr << PagesMapped << BytesOfPagesMapped << FSLookups; |
| 172 | } |