FileManager.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. //===--- FileManager.cpp - File System Probing and Caching ----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the FileManager interface.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. //
  13. // TODO: This should index all interesting directories with dirent calls.
  14. // getdirentries ?
  15. // opendir/readdir_r/closedir ?
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #include "clang/Basic/FileManager.h"
  19. #include "clang/Basic/FileSystemStatCache.h"
  20. #include "llvm/ADT/STLExtras.h"
  21. #include "llvm/ADT/SmallString.h"
  22. #include "llvm/ADT/Statistic.h"
  23. #include "llvm/Config/llvm-config.h"
  24. #include "llvm/Support/FileSystem.h"
  25. #include "llvm/Support/MemoryBuffer.h"
  26. #include "llvm/Support/Path.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include <algorithm>
  29. #include <cassert>
  30. #include <climits>
  31. #include <cstdint>
  32. #include <cstdlib>
  33. #include <optional>
  34. #include <string>
  35. #include <utility>
  36. using namespace clang;
  37. #define DEBUG_TYPE "file-search"
  38. ALWAYS_ENABLED_STATISTIC(NumDirLookups, "Number of directory lookups.");
  39. ALWAYS_ENABLED_STATISTIC(NumFileLookups, "Number of file lookups.");
  40. ALWAYS_ENABLED_STATISTIC(NumDirCacheMisses,
  41. "Number of directory cache misses.");
  42. ALWAYS_ENABLED_STATISTIC(NumFileCacheMisses, "Number of file cache misses.");
  43. //===----------------------------------------------------------------------===//
  44. // Common logic.
  45. //===----------------------------------------------------------------------===//
  46. FileManager::FileManager(const FileSystemOptions &FSO,
  47. IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
  48. : FS(std::move(FS)), FileSystemOpts(FSO), SeenDirEntries(64),
  49. SeenFileEntries(64), NextFileUID(0) {
  50. // If the caller doesn't provide a virtual file system, just grab the real
  51. // file system.
  52. if (!this->FS)
  53. this->FS = llvm::vfs::getRealFileSystem();
  54. }
  55. FileManager::~FileManager() = default;
  56. void FileManager::setStatCache(std::unique_ptr<FileSystemStatCache> statCache) {
  57. assert(statCache && "No stat cache provided?");
  58. StatCache = std::move(statCache);
  59. }
  60. void FileManager::clearStatCache() { StatCache.reset(); }
  61. /// Retrieve the directory that the given file name resides in.
  62. /// Filename can point to either a real file or a virtual file.
  63. static llvm::Expected<DirectoryEntryRef>
  64. getDirectoryFromFile(FileManager &FileMgr, StringRef Filename,
  65. bool CacheFailure) {
  66. if (Filename.empty())
  67. return llvm::errorCodeToError(
  68. make_error_code(std::errc::no_such_file_or_directory));
  69. if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
  70. return llvm::errorCodeToError(make_error_code(std::errc::is_a_directory));
  71. StringRef DirName = llvm::sys::path::parent_path(Filename);
  72. // Use the current directory if file has no path component.
  73. if (DirName.empty())
  74. DirName = ".";
  75. return FileMgr.getDirectoryRef(DirName, CacheFailure);
  76. }
  77. /// Add all ancestors of the given path (pointing to either a file or
  78. /// a directory) as virtual directories.
  79. void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
  80. StringRef DirName = llvm::sys::path::parent_path(Path);
  81. if (DirName.empty())
  82. DirName = ".";
  83. auto &NamedDirEnt = *SeenDirEntries.insert(
  84. {DirName, std::errc::no_such_file_or_directory}).first;
  85. // When caching a virtual directory, we always cache its ancestors
  86. // at the same time. Therefore, if DirName is already in the cache,
  87. // we don't need to recurse as its ancestors must also already be in
  88. // the cache (or it's a known non-virtual directory).
  89. if (NamedDirEnt.second)
  90. return;
  91. // Add the virtual directory to the cache.
  92. auto *UDE = new (DirsAlloc.Allocate()) DirectoryEntry();
  93. UDE->Name = NamedDirEnt.first();
  94. NamedDirEnt.second = *UDE;
  95. VirtualDirectoryEntries.push_back(UDE);
  96. // Recursively add the other ancestors.
  97. addAncestorsAsVirtualDirs(DirName);
  98. }
  99. llvm::Expected<DirectoryEntryRef>
  100. FileManager::getDirectoryRef(StringRef DirName, bool CacheFailure) {
  101. // stat doesn't like trailing separators except for root directory.
  102. // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
  103. // (though it can strip '\\')
  104. if (DirName.size() > 1 &&
  105. DirName != llvm::sys::path::root_path(DirName) &&
  106. llvm::sys::path::is_separator(DirName.back()))
  107. DirName = DirName.substr(0, DirName.size()-1);
  108. std::optional<std::string> DirNameStr;
  109. if (is_style_windows(llvm::sys::path::Style::native)) {
  110. // Fixing a problem with "clang C:test.c" on Windows.
  111. // Stat("C:") does not recognize "C:" as a valid directory
  112. if (DirName.size() > 1 && DirName.back() == ':' &&
  113. DirName.equals_insensitive(llvm::sys::path::root_name(DirName))) {
  114. DirNameStr = DirName.str() + '.';
  115. DirName = *DirNameStr;
  116. }
  117. }
  118. ++NumDirLookups;
  119. // See if there was already an entry in the map. Note that the map
  120. // contains both virtual and real directories.
  121. auto SeenDirInsertResult =
  122. SeenDirEntries.insert({DirName, std::errc::no_such_file_or_directory});
  123. if (!SeenDirInsertResult.second) {
  124. if (SeenDirInsertResult.first->second)
  125. return DirectoryEntryRef(*SeenDirInsertResult.first);
  126. return llvm::errorCodeToError(SeenDirInsertResult.first->second.getError());
  127. }
  128. // We've not seen this before. Fill it in.
  129. ++NumDirCacheMisses;
  130. auto &NamedDirEnt = *SeenDirInsertResult.first;
  131. assert(!NamedDirEnt.second && "should be newly-created");
  132. // Get the null-terminated directory name as stored as the key of the
  133. // SeenDirEntries map.
  134. StringRef InterndDirName = NamedDirEnt.first();
  135. // Check to see if the directory exists.
  136. llvm::vfs::Status Status;
  137. auto statError = getStatValue(InterndDirName, Status, false,
  138. nullptr /*directory lookup*/);
  139. if (statError) {
  140. // There's no real directory at the given path.
  141. if (CacheFailure)
  142. NamedDirEnt.second = statError;
  143. else
  144. SeenDirEntries.erase(DirName);
  145. return llvm::errorCodeToError(statError);
  146. }
  147. // It exists. See if we have already opened a directory with the
  148. // same inode (this occurs on Unix-like systems when one dir is
  149. // symlinked to another, for example) or the same path (on
  150. // Windows).
  151. DirectoryEntry *&UDE = UniqueRealDirs[Status.getUniqueID()];
  152. if (!UDE) {
  153. // We don't have this directory yet, add it. We use the string
  154. // key from the SeenDirEntries map as the string.
  155. UDE = new (DirsAlloc.Allocate()) DirectoryEntry();
  156. UDE->Name = InterndDirName;
  157. }
  158. NamedDirEnt.second = *UDE;
  159. return DirectoryEntryRef(NamedDirEnt);
  160. }
  161. llvm::ErrorOr<const DirectoryEntry *>
  162. FileManager::getDirectory(StringRef DirName, bool CacheFailure) {
  163. auto Result = getDirectoryRef(DirName, CacheFailure);
  164. if (Result)
  165. return &Result->getDirEntry();
  166. return llvm::errorToErrorCode(Result.takeError());
  167. }
  168. llvm::ErrorOr<const FileEntry *>
  169. FileManager::getFile(StringRef Filename, bool openFile, bool CacheFailure) {
  170. auto Result = getFileRef(Filename, openFile, CacheFailure);
  171. if (Result)
  172. return &Result->getFileEntry();
  173. return llvm::errorToErrorCode(Result.takeError());
  174. }
  175. llvm::Expected<FileEntryRef>
  176. FileManager::getFileRef(StringRef Filename, bool openFile, bool CacheFailure) {
  177. ++NumFileLookups;
  178. // See if there is already an entry in the map.
  179. auto SeenFileInsertResult =
  180. SeenFileEntries.insert({Filename, std::errc::no_such_file_or_directory});
  181. if (!SeenFileInsertResult.second) {
  182. if (!SeenFileInsertResult.first->second)
  183. return llvm::errorCodeToError(
  184. SeenFileInsertResult.first->second.getError());
  185. return FileEntryRef(*SeenFileInsertResult.first);
  186. }
  187. // We've not seen this before. Fill it in.
  188. ++NumFileCacheMisses;
  189. auto *NamedFileEnt = &*SeenFileInsertResult.first;
  190. assert(!NamedFileEnt->second && "should be newly-created");
  191. // Get the null-terminated file name as stored as the key of the
  192. // SeenFileEntries map.
  193. StringRef InterndFileName = NamedFileEnt->first();
  194. // Look up the directory for the file. When looking up something like
  195. // sys/foo.h we'll discover all of the search directories that have a 'sys'
  196. // subdirectory. This will let us avoid having to waste time on known-to-fail
  197. // searches when we go to find sys/bar.h, because all the search directories
  198. // without a 'sys' subdir will get a cached failure result.
  199. auto DirInfoOrErr = getDirectoryFromFile(*this, Filename, CacheFailure);
  200. if (!DirInfoOrErr) { // Directory doesn't exist, file can't exist.
  201. std::error_code Err = errorToErrorCode(DirInfoOrErr.takeError());
  202. if (CacheFailure)
  203. NamedFileEnt->second = Err;
  204. else
  205. SeenFileEntries.erase(Filename);
  206. return llvm::errorCodeToError(Err);
  207. }
  208. DirectoryEntryRef DirInfo = *DirInfoOrErr;
  209. // FIXME: Use the directory info to prune this, before doing the stat syscall.
  210. // FIXME: This will reduce the # syscalls.
  211. // Check to see if the file exists.
  212. std::unique_ptr<llvm::vfs::File> F;
  213. llvm::vfs::Status Status;
  214. auto statError = getStatValue(InterndFileName, Status, true,
  215. openFile ? &F : nullptr);
  216. if (statError) {
  217. // There's no real file at the given path.
  218. if (CacheFailure)
  219. NamedFileEnt->second = statError;
  220. else
  221. SeenFileEntries.erase(Filename);
  222. return llvm::errorCodeToError(statError);
  223. }
  224. assert((openFile || !F) && "undesired open file");
  225. // It exists. See if we have already opened a file with the same inode.
  226. // This occurs when one dir is symlinked to another, for example.
  227. FileEntry *&UFE = UniqueRealFiles[Status.getUniqueID()];
  228. bool ReusingEntry = UFE != nullptr;
  229. if (!UFE)
  230. UFE = new (FilesAlloc.Allocate()) FileEntry();
  231. if (!Status.ExposesExternalVFSPath || Status.getName() == Filename) {
  232. // Use the requested name. Set the FileEntry.
  233. NamedFileEnt->second = FileEntryRef::MapValue(*UFE, DirInfo);
  234. } else {
  235. // Name mismatch. We need a redirect. First grab the actual entry we want
  236. // to return.
  237. //
  238. // This redirection logic intentionally leaks the external name of a
  239. // redirected file that uses 'use-external-name' in \a
  240. // vfs::RedirectionFileSystem. This allows clang to report the external
  241. // name to users (in diagnostics) and to tools that don't have access to
  242. // the VFS (in debug info and dependency '.d' files).
  243. //
  244. // FIXME: This is pretty complex and has some very complicated interactions
  245. // with the rest of clang. It's also inconsistent with how "real"
  246. // filesystems behave and confuses parts of clang expect to see the
  247. // name-as-accessed on the \a FileEntryRef.
  248. //
  249. // A potential plan to remove this is as follows -
  250. // - Update callers such as `HeaderSearch::findUsableModuleForHeader()`
  251. // to explicitly use the `getNameAsRequested()` rather than just using
  252. // `getName()`.
  253. // - Add a `FileManager::getExternalPath` API for explicitly getting the
  254. // remapped external filename when there is one available. Adopt it in
  255. // callers like diagnostics/deps reporting instead of calling
  256. // `getName()` directly.
  257. // - Switch the meaning of `FileEntryRef::getName()` to get the requested
  258. // name, not the external name. Once that sticks, revert callers that
  259. // want the requested name back to calling `getName()`.
  260. // - Update the VFS to always return the requested name. This could also
  261. // return the external name, or just have an API to request it
  262. // lazily. The latter has the benefit of making accesses of the
  263. // external path easily tracked, but may also require extra work than
  264. // just returning up front.
  265. // - (Optionally) Add an API to VFS to get the external filename lazily
  266. // and update `FileManager::getExternalPath()` to use it instead. This
  267. // has the benefit of making such accesses easily tracked, though isn't
  268. // necessarily required (and could cause extra work than just adding to
  269. // eg. `vfs::Status` up front).
  270. auto &Redirection =
  271. *SeenFileEntries
  272. .insert({Status.getName(), FileEntryRef::MapValue(*UFE, DirInfo)})
  273. .first;
  274. assert(Redirection.second->V.is<FileEntry *>() &&
  275. "filename redirected to a non-canonical filename?");
  276. assert(Redirection.second->V.get<FileEntry *>() == UFE &&
  277. "filename from getStatValue() refers to wrong file");
  278. // Cache the redirection in the previously-inserted entry, still available
  279. // in the tentative return value.
  280. NamedFileEnt->second = FileEntryRef::MapValue(Redirection);
  281. }
  282. FileEntryRef ReturnedRef(*NamedFileEnt);
  283. if (ReusingEntry) { // Already have an entry with this inode, return it.
  284. // FIXME: This hack ensures that `getDir()` will use the path that was
  285. // used to lookup this file, even if we found a file by different path
  286. // first. This is required in order to find a module's structure when its
  287. // headers/module map are mapped in the VFS.
  288. //
  289. // See above for how this will eventually be removed. `IsVFSMapped`
  290. // *cannot* be narrowed to `ExposesExternalVFSPath` as crash reproducers
  291. // also depend on this logic and they have `use-external-paths: false`.
  292. if (&DirInfo.getDirEntry() != UFE->Dir && Status.IsVFSMapped)
  293. UFE->Dir = &DirInfo.getDirEntry();
  294. // Always update LastRef to the last name by which a file was accessed.
  295. // FIXME: Neither this nor always using the first reference is correct; we
  296. // want to switch towards a design where we return a FileName object that
  297. // encapsulates both the name by which the file was accessed and the
  298. // corresponding FileEntry.
  299. // FIXME: LastRef should be removed from FileEntry once all clients adopt
  300. // FileEntryRef.
  301. UFE->LastRef = ReturnedRef;
  302. return ReturnedRef;
  303. }
  304. // Otherwise, we don't have this file yet, add it.
  305. UFE->LastRef = ReturnedRef;
  306. UFE->Size = Status.getSize();
  307. UFE->ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
  308. UFE->Dir = &DirInfo.getDirEntry();
  309. UFE->UID = NextFileUID++;
  310. UFE->UniqueID = Status.getUniqueID();
  311. UFE->IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
  312. UFE->File = std::move(F);
  313. if (UFE->File) {
  314. if (auto PathName = UFE->File->getName())
  315. fillRealPathName(UFE, *PathName);
  316. } else if (!openFile) {
  317. // We should still fill the path even if we aren't opening the file.
  318. fillRealPathName(UFE, InterndFileName);
  319. }
  320. return ReturnedRef;
  321. }
  322. llvm::Expected<FileEntryRef> FileManager::getSTDIN() {
  323. // Only read stdin once.
  324. if (STDIN)
  325. return *STDIN;
  326. std::unique_ptr<llvm::MemoryBuffer> Content;
  327. if (auto ContentOrError = llvm::MemoryBuffer::getSTDIN())
  328. Content = std::move(*ContentOrError);
  329. else
  330. return llvm::errorCodeToError(ContentOrError.getError());
  331. STDIN = getVirtualFileRef(Content->getBufferIdentifier(),
  332. Content->getBufferSize(), 0);
  333. FileEntry &FE = const_cast<FileEntry &>(STDIN->getFileEntry());
  334. FE.Content = std::move(Content);
  335. FE.IsNamedPipe = true;
  336. return *STDIN;
  337. }
  338. const FileEntry *FileManager::getVirtualFile(StringRef Filename, off_t Size,
  339. time_t ModificationTime) {
  340. return &getVirtualFileRef(Filename, Size, ModificationTime).getFileEntry();
  341. }
  342. FileEntryRef FileManager::getVirtualFileRef(StringRef Filename, off_t Size,
  343. time_t ModificationTime) {
  344. ++NumFileLookups;
  345. // See if there is already an entry in the map for an existing file.
  346. auto &NamedFileEnt = *SeenFileEntries.insert(
  347. {Filename, std::errc::no_such_file_or_directory}).first;
  348. if (NamedFileEnt.second) {
  349. FileEntryRef::MapValue Value = *NamedFileEnt.second;
  350. if (LLVM_LIKELY(Value.V.is<FileEntry *>()))
  351. return FileEntryRef(NamedFileEnt);
  352. return FileEntryRef(*reinterpret_cast<const FileEntryRef::MapEntry *>(
  353. Value.V.get<const void *>()));
  354. }
  355. // We've not seen this before, or the file is cached as non-existent.
  356. ++NumFileCacheMisses;
  357. addAncestorsAsVirtualDirs(Filename);
  358. FileEntry *UFE = nullptr;
  359. // Now that all ancestors of Filename are in the cache, the
  360. // following call is guaranteed to find the DirectoryEntry from the
  361. // cache. A virtual file can also have an empty filename, that could come
  362. // from a source location preprocessor directive with an empty filename as
  363. // an example, so we need to pretend it has a name to ensure a valid directory
  364. // entry can be returned.
  365. auto DirInfo = expectedToOptional(getDirectoryFromFile(
  366. *this, Filename.empty() ? "." : Filename, /*CacheFailure=*/true));
  367. assert(DirInfo &&
  368. "The directory of a virtual file should already be in the cache.");
  369. // Check to see if the file exists. If so, drop the virtual file
  370. llvm::vfs::Status Status;
  371. const char *InterndFileName = NamedFileEnt.first().data();
  372. if (!getStatValue(InterndFileName, Status, true, nullptr)) {
  373. Status = llvm::vfs::Status(
  374. Status.getName(), Status.getUniqueID(),
  375. llvm::sys::toTimePoint(ModificationTime),
  376. Status.getUser(), Status.getGroup(), Size,
  377. Status.getType(), Status.getPermissions());
  378. auto &RealFE = UniqueRealFiles[Status.getUniqueID()];
  379. if (RealFE) {
  380. // If we had already opened this file, close it now so we don't
  381. // leak the descriptor. We're not going to use the file
  382. // descriptor anyway, since this is a virtual file.
  383. if (RealFE->File)
  384. RealFE->closeFile();
  385. // If we already have an entry with this inode, return it.
  386. //
  387. // FIXME: Surely this should add a reference by the new name, and return
  388. // it instead...
  389. NamedFileEnt.second = FileEntryRef::MapValue(*RealFE, *DirInfo);
  390. return FileEntryRef(NamedFileEnt);
  391. }
  392. // File exists, but no entry - create it.
  393. RealFE = new (FilesAlloc.Allocate()) FileEntry();
  394. RealFE->UniqueID = Status.getUniqueID();
  395. RealFE->IsNamedPipe =
  396. Status.getType() == llvm::sys::fs::file_type::fifo_file;
  397. fillRealPathName(RealFE, Status.getName());
  398. UFE = RealFE;
  399. } else {
  400. // File does not exist, create a virtual entry.
  401. UFE = new (FilesAlloc.Allocate()) FileEntry();
  402. VirtualFileEntries.push_back(UFE);
  403. }
  404. NamedFileEnt.second = FileEntryRef::MapValue(*UFE, *DirInfo);
  405. UFE->LastRef = FileEntryRef(NamedFileEnt);
  406. UFE->Size = Size;
  407. UFE->ModTime = ModificationTime;
  408. UFE->Dir = &DirInfo->getDirEntry();
  409. UFE->UID = NextFileUID++;
  410. UFE->File.reset();
  411. return FileEntryRef(NamedFileEnt);
  412. }
  413. OptionalFileEntryRef FileManager::getBypassFile(FileEntryRef VF) {
  414. // Stat of the file and return nullptr if it doesn't exist.
  415. llvm::vfs::Status Status;
  416. if (getStatValue(VF.getName(), Status, /*isFile=*/true, /*F=*/nullptr))
  417. return std::nullopt;
  418. if (!SeenBypassFileEntries)
  419. SeenBypassFileEntries = std::make_unique<
  420. llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>>>();
  421. // If we've already bypassed just use the existing one.
  422. auto Insertion = SeenBypassFileEntries->insert(
  423. {VF.getName(), std::errc::no_such_file_or_directory});
  424. if (!Insertion.second)
  425. return FileEntryRef(*Insertion.first);
  426. // Fill in the new entry from the stat.
  427. FileEntry *BFE = new (FilesAlloc.Allocate()) FileEntry();
  428. BypassFileEntries.push_back(BFE);
  429. Insertion.first->second = FileEntryRef::MapValue(*BFE, VF.getDir());
  430. BFE->LastRef = FileEntryRef(*Insertion.first);
  431. BFE->Size = Status.getSize();
  432. BFE->Dir = VF.getFileEntry().Dir;
  433. BFE->ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
  434. BFE->UID = NextFileUID++;
  435. // Save the entry in the bypass table and return.
  436. return FileEntryRef(*Insertion.first);
  437. }
  438. bool FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
  439. StringRef pathRef(path.data(), path.size());
  440. if (FileSystemOpts.WorkingDir.empty()
  441. || llvm::sys::path::is_absolute(pathRef))
  442. return false;
  443. SmallString<128> NewPath(FileSystemOpts.WorkingDir);
  444. llvm::sys::path::append(NewPath, pathRef);
  445. path = NewPath;
  446. return true;
  447. }
  448. bool FileManager::makeAbsolutePath(SmallVectorImpl<char> &Path) const {
  449. bool Changed = FixupRelativePath(Path);
  450. if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
  451. FS->makeAbsolute(Path);
  452. Changed = true;
  453. }
  454. return Changed;
  455. }
  456. void FileManager::fillRealPathName(FileEntry *UFE, llvm::StringRef FileName) {
  457. llvm::SmallString<128> AbsPath(FileName);
  458. // This is not the same as `VFS::getRealPath()`, which resolves symlinks
  459. // but can be very expensive on real file systems.
  460. // FIXME: the semantic of RealPathName is unclear, and the name might be
  461. // misleading. We need to clean up the interface here.
  462. makeAbsolutePath(AbsPath);
  463. llvm::sys::path::remove_dots(AbsPath, /*remove_dot_dot=*/true);
  464. UFE->RealPathName = std::string(AbsPath.str());
  465. }
  466. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
  467. FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile,
  468. bool RequiresNullTerminator) {
  469. // If the content is living on the file entry, return a reference to it.
  470. if (Entry->Content)
  471. return llvm::MemoryBuffer::getMemBuffer(Entry->Content->getMemBufferRef());
  472. uint64_t FileSize = Entry->getSize();
  473. // If there's a high enough chance that the file have changed since we
  474. // got its size, force a stat before opening it.
  475. if (isVolatile || Entry->isNamedPipe())
  476. FileSize = -1;
  477. StringRef Filename = Entry->getName();
  478. // If the file is already open, use the open file descriptor.
  479. if (Entry->File) {
  480. auto Result = Entry->File->getBuffer(Filename, FileSize,
  481. RequiresNullTerminator, isVolatile);
  482. Entry->closeFile();
  483. return Result;
  484. }
  485. // Otherwise, open the file.
  486. return getBufferForFileImpl(Filename, FileSize, isVolatile,
  487. RequiresNullTerminator);
  488. }
  489. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
  490. FileManager::getBufferForFileImpl(StringRef Filename, int64_t FileSize,
  491. bool isVolatile,
  492. bool RequiresNullTerminator) {
  493. if (FileSystemOpts.WorkingDir.empty())
  494. return FS->getBufferForFile(Filename, FileSize, RequiresNullTerminator,
  495. isVolatile);
  496. SmallString<128> FilePath(Filename);
  497. FixupRelativePath(FilePath);
  498. return FS->getBufferForFile(FilePath, FileSize, RequiresNullTerminator,
  499. isVolatile);
  500. }
  501. /// getStatValue - Get the 'stat' information for the specified path,
  502. /// using the cache to accelerate it if possible. This returns true
  503. /// if the path points to a virtual file or does not exist, or returns
  504. /// false if it's an existent real file. If FileDescriptor is NULL,
  505. /// do directory look-up instead of file look-up.
  506. std::error_code
  507. FileManager::getStatValue(StringRef Path, llvm::vfs::Status &Status,
  508. bool isFile, std::unique_ptr<llvm::vfs::File> *F) {
  509. // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
  510. // absolute!
  511. if (FileSystemOpts.WorkingDir.empty())
  512. return FileSystemStatCache::get(Path, Status, isFile, F,
  513. StatCache.get(), *FS);
  514. SmallString<128> FilePath(Path);
  515. FixupRelativePath(FilePath);
  516. return FileSystemStatCache::get(FilePath.c_str(), Status, isFile, F,
  517. StatCache.get(), *FS);
  518. }
  519. std::error_code
  520. FileManager::getNoncachedStatValue(StringRef Path,
  521. llvm::vfs::Status &Result) {
  522. SmallString<128> FilePath(Path);
  523. FixupRelativePath(FilePath);
  524. llvm::ErrorOr<llvm::vfs::Status> S = FS->status(FilePath.c_str());
  525. if (!S)
  526. return S.getError();
  527. Result = *S;
  528. return std::error_code();
  529. }
  530. void FileManager::GetUniqueIDMapping(
  531. SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
  532. UIDToFiles.clear();
  533. UIDToFiles.resize(NextFileUID);
  534. // Map file entries
  535. for (llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>,
  536. llvm::BumpPtrAllocator>::const_iterator
  537. FE = SeenFileEntries.begin(),
  538. FEEnd = SeenFileEntries.end();
  539. FE != FEEnd; ++FE)
  540. if (llvm::ErrorOr<FileEntryRef::MapValue> Entry = FE->getValue()) {
  541. if (const auto *FE = Entry->V.dyn_cast<FileEntry *>())
  542. UIDToFiles[FE->getUID()] = FE;
  543. }
  544. // Map virtual file entries
  545. for (const auto &VFE : VirtualFileEntries)
  546. UIDToFiles[VFE->getUID()] = VFE;
  547. }
  548. StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
  549. llvm::DenseMap<const void *, llvm::StringRef>::iterator Known
  550. = CanonicalNames.find(Dir);
  551. if (Known != CanonicalNames.end())
  552. return Known->second;
  553. StringRef CanonicalName(Dir->getName());
  554. SmallString<4096> CanonicalNameBuf;
  555. if (!FS->getRealPath(Dir->getName(), CanonicalNameBuf))
  556. CanonicalName = CanonicalNameBuf.str().copy(CanonicalNameStorage);
  557. CanonicalNames.insert({Dir, CanonicalName});
  558. return CanonicalName;
  559. }
  560. StringRef FileManager::getCanonicalName(const FileEntry *File) {
  561. llvm::DenseMap<const void *, llvm::StringRef>::iterator Known
  562. = CanonicalNames.find(File);
  563. if (Known != CanonicalNames.end())
  564. return Known->second;
  565. StringRef CanonicalName(File->getName());
  566. SmallString<4096> CanonicalNameBuf;
  567. if (!FS->getRealPath(File->getName(), CanonicalNameBuf))
  568. CanonicalName = CanonicalNameBuf.str().copy(CanonicalNameStorage);
  569. CanonicalNames.insert({File, CanonicalName});
  570. return CanonicalName;
  571. }
  572. void FileManager::PrintStats() const {
  573. llvm::errs() << "\n*** File Manager Stats:\n";
  574. llvm::errs() << UniqueRealFiles.size() << " real files found, "
  575. << UniqueRealDirs.size() << " real dirs found.\n";
  576. llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
  577. << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
  578. llvm::errs() << NumDirLookups << " dir lookups, "
  579. << NumDirCacheMisses << " dir cache misses.\n";
  580. llvm::errs() << NumFileLookups << " file lookups, "
  581. << NumFileCacheMisses << " file cache misses.\n";
  582. //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
  583. }