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