FileManager.cpp 6.1 KB

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