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