FileManager.cpp revision 52e7108f51a4a9f4d6e84f33fb594d06e1d79560
17f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project///===--- FileManager.cpp - File System Probing and Caching ----------------===//
27f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//
37f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//                     The LLVM Compiler Infrastructure
47f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//
57f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project// This file is distributed under the University of Illinois Open Source
67f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project// License. See LICENSE.TXT for details.
77f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//
87f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//===----------------------------------------------------------------------===//
97f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//
107f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//  This file implements the FileManager interface.
117f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//
127f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//===----------------------------------------------------------------------===//
137f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//
147f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project// TODO: This should index all interesting directories with dirent calls.
157f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//  getdirentries ?
167f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//  opendir/readdir_r/closedir ?
177f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//
187f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//===----------------------------------------------------------------------===//
197f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
207f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#include "clang/Basic/FileManager.h"
217f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#include "llvm/ADT/SmallString.h"
227f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#include "llvm/Support/raw_ostream.h"
237f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#include "llvm/System/Path.h"
247f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#include "llvm/Config/config.h"
257f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#include <map>
267f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#include <set>
277f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#include <string>
287f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Projectusing namespace clang;
297f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
307f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project// FIXME: Enhance libsystem to support inode and other fields.
317f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#include <sys/stat.h>
327f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
337f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#if defined(_MSC_VER)
347f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#define S_ISDIR(s) (_S_IFDIR & s)
357f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#endif
367f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
377f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project/// NON_EXISTENT_DIR - A special value distinct from null that is used to
387f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project/// represent a dir name that doesn't exist on the disk.
397f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
407f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
417f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//===----------------------------------------------------------------------===//
427f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project// Windows.
437f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//===----------------------------------------------------------------------===//
447f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
457f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#ifdef LLVM_ON_WIN32
467f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
477f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
487f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
497f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Projectnamespace {
507f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  static std::string GetFullPath(const char *relPath) {
517f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    char *absPathStrPtr = _fullpath(NULL, relPath, 0);
527f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    assert(absPathStrPtr && "_fullpath() returned NULL!");
537f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
547f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    std::string absPath(absPathStrPtr);
557f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
567f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    free(absPathStrPtr);
577f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    return absPath;
587f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  }
597f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project}
607f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
617f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Projectclass FileManager::UniqueDirContainer {
627f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  /// UniqueDirs - Cache from full path to existing directories/files.
637f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  ///
647f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  llvm::StringMap<DirectoryEntry> UniqueDirs;
657f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
667f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Projectpublic:
677f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
687f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    std::string FullPath(GetFullPath(Name));
697f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    return UniqueDirs.GetOrCreateValue(
707f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project                              FullPath.c_str(),
717f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project                              FullPath.c_str() + FullPath.size()
727f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project                                                                ).getValue();
737f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  }
747f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
757f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  size_t size() { return UniqueDirs.size(); }
767f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project};
777f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
787f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Projectclass FileManager::UniqueFileContainer {
797f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  /// UniqueFiles - Cache from full path to existing directories/files.
807f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  ///
817f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
827f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
837f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Projectpublic:
847f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  FileEntry &getFile(const char *Name, struct stat &StatBuf) {
857f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    std::string FullPath(GetFullPath(Name));
867f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    return UniqueFiles.GetOrCreateValue(
877f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project                               FullPath.c_str(),
887f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project                               FullPath.c_str() + FullPath.size()
897f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project                                                                 ).getValue();
907f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  }
917f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
927f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  size_t size() { return UniqueFiles.size(); }
937f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project};
947f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
957f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//===----------------------------------------------------------------------===//
967f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project// Unix-like Systems.
977f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project//===----------------------------------------------------------------------===//
987f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
997f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#else
1007f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
1017f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
1027f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
1037f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Projectclass FileManager::UniqueDirContainer {
1047f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  /// UniqueDirs - Cache from ID's to existing directories/files.
1057f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  ///
1067f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
1077f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
1087f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Projectpublic:
1097f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
1107f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
1117f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project  }
112
113  size_t size() { return UniqueDirs.size(); }
114};
115
116class FileManager::UniqueFileContainer {
117  /// UniqueFiles - Cache from ID's to existing directories/files.
118  ///
119  std::set<FileEntry> UniqueFiles;
120
121public:
122  FileEntry &getFile(const char *Name, struct stat &StatBuf) {
123    return
124      const_cast<FileEntry&>(
125                    *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
126                                                  StatBuf.st_ino,
127                                                  StatBuf.st_mode)).first);
128  }
129
130  size_t size() { return UniqueFiles.size(); }
131};
132
133#endif
134
135//===----------------------------------------------------------------------===//
136// Common logic.
137//===----------------------------------------------------------------------===//
138
139FileManager::FileManager()
140  : UniqueDirs(*new UniqueDirContainer),
141    UniqueFiles(*new UniqueFileContainer),
142    DirEntries(64), FileEntries(64), NextFileUID(0) {
143  NumDirLookups = NumFileLookups = 0;
144  NumDirCacheMisses = NumFileCacheMisses = 0;
145}
146
147FileManager::~FileManager() {
148  delete &UniqueDirs;
149  delete &UniqueFiles;
150}
151
152void FileManager::addStatCache(StatSysCallCache *statCache, bool AtBeginning) {
153  assert(statCache && "No stat cache provided?");
154  if (AtBeginning || StatCache.get() == 0) {
155    statCache->setNextStatCache(StatCache.take());
156    StatCache.reset(statCache);
157    return;
158  }
159
160  StatSysCallCache *LastCache = StatCache.get();
161  while (LastCache->getNextStatCache())
162    LastCache = LastCache->getNextStatCache();
163
164  LastCache->setNextStatCache(statCache);
165}
166
167void FileManager::removeStatCache(StatSysCallCache *statCache) {
168  if (!statCache)
169    return;
170
171  if (StatCache.get() == statCache) {
172    // This is the first stat cache.
173    StatCache.reset(StatCache->takeNextStatCache());
174    return;
175  }
176
177  // Find the stat cache in the list.
178  StatSysCallCache *PrevCache = StatCache.get();
179  while (PrevCache && PrevCache->getNextStatCache() != statCache)
180    PrevCache = PrevCache->getNextStatCache();
181  if (PrevCache)
182    PrevCache->setNextStatCache(statCache->getNextStatCache());
183  else
184    assert(false && "Stat cache not found for removal");
185}
186
187/// getDirectory - Lookup, cache, and verify the specified directory.  This
188/// returns null if the directory doesn't exist.
189///
190const DirectoryEntry *FileManager::getDirectory(const char *NameStart,
191                                                const char *NameEnd) {
192  ++NumDirLookups;
193  llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
194    DirEntries.GetOrCreateValue(NameStart, NameEnd);
195
196  // See if there is already an entry in the map.
197  if (NamedDirEnt.getValue())
198    return NamedDirEnt.getValue() == NON_EXISTENT_DIR
199              ? 0 : NamedDirEnt.getValue();
200
201  ++NumDirCacheMisses;
202
203  // By default, initialize it to invalid.
204  NamedDirEnt.setValue(NON_EXISTENT_DIR);
205
206  // Get the null-terminated directory name as stored as the key of the
207  // DirEntries map.
208  const char *InterndDirName = NamedDirEnt.getKeyData();
209
210  // Check to see if the directory exists.
211  struct stat StatBuf;
212  if (stat_cached(InterndDirName, &StatBuf) ||   // Error stat'ing.
213      !S_ISDIR(StatBuf.st_mode))          // Not a directory?
214    return 0;
215
216  // It exists.  See if we have already opened a directory with the same inode.
217  // This occurs when one dir is symlinked to another, for example.
218  DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
219
220  NamedDirEnt.setValue(&UDE);
221  if (UDE.getName()) // Already have an entry with this inode, return it.
222    return &UDE;
223
224  // Otherwise, we don't have this directory yet, add it.  We use the string
225  // key from the DirEntries map as the string.
226  UDE.Name  = InterndDirName;
227  return &UDE;
228}
229
230/// NON_EXISTENT_FILE - A special value distinct from null that is used to
231/// represent a filename that doesn't exist on the disk.
232#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
233
234/// getFile - Lookup, cache, and verify the specified file.  This returns null
235/// if the file doesn't exist.
236///
237const FileEntry *FileManager::getFile(const char *NameStart,
238                                      const char *NameEnd) {
239  ++NumFileLookups;
240
241  // See if there is already an entry in the map.
242  llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
243    FileEntries.GetOrCreateValue(NameStart, NameEnd);
244
245  // See if there is already an entry in the map.
246  if (NamedFileEnt.getValue())
247    return NamedFileEnt.getValue() == NON_EXISTENT_FILE
248                 ? 0 : NamedFileEnt.getValue();
249
250  ++NumFileCacheMisses;
251
252  // By default, initialize it to invalid.
253  NamedFileEnt.setValue(NON_EXISTENT_FILE);
254
255  // Figure out what directory it is in.   If the string contains a / in it,
256  // strip off everything after it.
257  // FIXME: this logic should be in sys::Path.
258  const char *SlashPos = NameEnd-1;
259  while (SlashPos >= NameStart && !IS_DIR_SEPARATOR_CHAR(SlashPos[0]))
260    --SlashPos;
261  // Ignore duplicate //'s.
262  while (SlashPos > NameStart && IS_DIR_SEPARATOR_CHAR(SlashPos[-1]))
263    --SlashPos;
264
265  const DirectoryEntry *DirInfo;
266  if (SlashPos < NameStart) {
267    // Use the current directory if file has no path component.
268    const char *Name = ".";
269    DirInfo = getDirectory(Name, Name+1);
270  } else if (SlashPos == NameEnd-1)
271    return 0;       // If filename ends with a /, it's a directory.
272  else
273    DirInfo = getDirectory(NameStart, SlashPos);
274
275  if (DirInfo == 0)  // Directory doesn't exist, file can't exist.
276    return 0;
277
278  // Get the null-terminated file name as stored as the key of the
279  // FileEntries map.
280  const char *InterndFileName = NamedFileEnt.getKeyData();
281
282  // FIXME: Use the directory info to prune this, before doing the stat syscall.
283  // FIXME: This will reduce the # syscalls.
284
285  // Nope, there isn't.  Check to see if the file exists.
286  struct stat StatBuf;
287  //llvm::errs() << "STATING: " << Filename;
288  if (stat_cached(InterndFileName, &StatBuf) ||   // Error stat'ing.
289        S_ISDIR(StatBuf.st_mode)) {           // A directory?
290    // If this file doesn't exist, we leave a null in FileEntries for this path.
291    //llvm::errs() << ": Not existing\n";
292    return 0;
293  }
294  //llvm::errs() << ": exists\n";
295
296  // It exists.  See if we have already opened a file with the same inode.
297  // This occurs when one dir is symlinked to another, for example.
298  FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
299
300  NamedFileEnt.setValue(&UFE);
301  if (UFE.getName())  // Already have an entry with this inode, return it.
302    return &UFE;
303
304  // Otherwise, we don't have this directory yet, add it.
305  // FIXME: Change the name to be a char* that points back to the 'FileEntries'
306  // key.
307  UFE.Name    = InterndFileName;
308  UFE.Size    = StatBuf.st_size;
309  UFE.ModTime = StatBuf.st_mtime;
310  UFE.Dir     = DirInfo;
311  UFE.UID     = NextFileUID++;
312  return &UFE;
313}
314
315void FileManager::PrintStats() const {
316  llvm::errs() << "\n*** File Manager Stats:\n";
317  llvm::errs() << UniqueFiles.size() << " files found, "
318               << UniqueDirs.size() << " dirs found.\n";
319  llvm::errs() << NumDirLookups << " dir lookups, "
320               << NumDirCacheMisses << " dir cache misses.\n";
321  llvm::errs() << NumFileLookups << " file lookups, "
322               << NumFileCacheMisses << " file cache misses.\n";
323
324  //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
325}
326
327int MemorizeStatCalls::stat(const char *path, struct stat *buf) {
328  int result = StatSysCallCache::stat(path, buf);
329
330  if (result != 0) {
331    // Cache failed 'stat' results.
332    struct stat empty;
333    memset(&empty, 0, sizeof(empty));
334    StatCalls[path] = StatResult(result, empty);
335  }
336  else if (!S_ISDIR(buf->st_mode) || llvm::sys::Path(path).isAbsolute()) {
337    // Cache file 'stat' results and directories with absolutely
338    // paths.
339    StatCalls[path] = StatResult(result, *buf);
340  }
341
342  return result;
343}
344