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
19#include "clang/Basic/FileManager.h"
20#include "clang/Basic/FileSystemStatCache.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/IOSandbox.h"
26#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/Support/Path.h"
28#include "llvm/Support/raw_ostream.h"
29#include <cassert>
30#include <climits>
31#include <cstdint>
32#include <cstdlib>
33#include <optional>
34#include <string>
35#include <utility>
36
37using namespace clang;
38
39#define DEBUG_TYPE "file-search"
40
41static void normalizeCacheKey(StringRef &Path,
42 std::optional<std::string> &Storage) {
43 using namespace llvm::sys::path;
44
45 // Drop trailing separators for non-root paths so that cache keys and `stat`
46 // queries use a single spelling. Keep root paths (`/`, `[A-Z]:\`) unchanged.
47 if (Path.size() > 1 && root_path(path: Path) != Path && is_separator(value: Path.back()))
48 Path = Path.drop_back();
49
50 // A bare drive path like "[A-Z]:" is drive-relative (current directory on the
51 // drive). As `[A-Z]:` is not a path specification, we must canonicalise it
52 // to `[A-Z]:.`.
53 if (is_style_windows(S: Style::native)) {
54 if (Path.size() > 1 && Path.back() == ':' &&
55 Path.equals_insensitive(RHS: root_name(path: Path))) {
56 Storage = Path.str() + ".";
57 Path = *Storage;
58 }
59 }
60}
61
62//===----------------------------------------------------------------------===//
63// Common logic.
64//===----------------------------------------------------------------------===//
65
66FileManager::FileManager(const FileSystemOptions &FSO,
67 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
68 : FS(std::move(FS)), FileSystemOpts(FSO), SeenDirEntries(64),
69 SeenFileEntries(64), NextFileUID(0) {
70 // If the caller doesn't provide a virtual file system, just grab the real
71 // file system.
72 if (!this->FS)
73 this->FS = llvm::vfs::getRealFileSystem();
74}
75
76FileManager::~FileManager() = default;
77
78void FileManager::setStatCache(std::unique_ptr<FileSystemStatCache> statCache) {
79 assert(statCache && "No stat cache provided?");
80 StatCache = std::move(statCache);
81}
82
83void FileManager::clearStatCache() { StatCache.reset(); }
84
85/// Retrieve the directory that the given file name resides in.
86/// Filename can point to either a real file or a virtual file.
87static llvm::Expected<DirectoryEntryRef>
88getDirectoryFromFile(FileManager &FileMgr, StringRef Filename,
89 bool CacheFailure) {
90 if (Filename.empty())
91 return llvm::errorCodeToError(
92 EC: make_error_code(e: std::errc::no_such_file_or_directory));
93
94 if (llvm::sys::path::is_separator(value: Filename[Filename.size() - 1]))
95 return llvm::errorCodeToError(EC: make_error_code(e: std::errc::is_a_directory));
96
97 StringRef DirName = llvm::sys::path::parent_path(path: Filename);
98 // Use the current directory if file has no path component.
99 if (DirName.empty())
100 DirName = ".";
101
102 return FileMgr.getDirectoryRef(DirName, CacheFailure);
103}
104
105DirectoryEntry *&FileManager::getRealDirEntry(const llvm::vfs::Status &Status) {
106 assert(Status.isDirectory() && "The directory should exist!");
107 // See if we have already opened a directory with the
108 // same inode (this occurs on Unix-like systems when one dir is
109 // symlinked to another, for example) or the same path (on
110 // Windows).
111 DirectoryEntry *&UDE = UniqueRealDirs[Status.getUniqueID()];
112
113 if (!UDE) {
114 // We don't have this directory yet, add it. We use the string
115 // key from the SeenDirEntries map as the string.
116 UDE = new (DirsAlloc.Allocate()) DirectoryEntry();
117 }
118 return UDE;
119}
120
121/// Add all ancestors of the given path (pointing to either a file or
122/// a directory) as virtual directories.
123void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
124 StringRef DirName = llvm::sys::path::parent_path(path: Path);
125 if (DirName.empty())
126 DirName = ".";
127
128 // Normalize the key for cache lookup/insert, but keep the original DirName
129 // for recursive processing since normalization can create paths that don't
130 // work well with parent_path() (e.g., "C:" -> "C:.").
131 std::optional<std::string> Storage;
132 StringRef OriginalDirName = DirName;
133 normalizeCacheKey(Path&: DirName, Storage);
134
135 auto &NamedDirEnt = *SeenDirEntries.insert(
136 KV: {DirName, std::errc::no_such_file_or_directory}).first;
137
138 // When caching a virtual directory, we always cache its ancestors
139 // at the same time. Therefore, if DirName is already in the cache,
140 // we don't need to recurse as its ancestors must also already be in
141 // the cache (or it's a known non-virtual directory).
142 if (NamedDirEnt.second)
143 return;
144
145 // Check to see if the directory exists.
146 llvm::vfs::Status Status;
147 auto statError =
148 getStatValue(Path: DirName, Status, isFile: false, F: nullptr /*directory lookup*/);
149 if (statError) {
150 // There's no real directory at the given path.
151 // Add the virtual directory to the cache.
152 auto *UDE = new (DirsAlloc.Allocate()) DirectoryEntry();
153 NamedDirEnt.second = *UDE;
154 VirtualDirectoryEntries.push_back(Elt: UDE);
155 } else {
156 // There is the real directory
157 DirectoryEntry *&UDE = getRealDirEntry(Status);
158 NamedDirEnt.second = *UDE;
159 }
160
161 // Recursively add the other ancestors.
162 addAncestorsAsVirtualDirs(Path: OriginalDirName);
163}
164
165llvm::Expected<DirectoryEntryRef>
166FileManager::getDirectoryRef(StringRef DirName, bool CacheFailure) {
167 std::optional<std::string> DirNameStr;
168 normalizeCacheKey(Path&: DirName, Storage&: DirNameStr);
169
170 ++NumDirLookups;
171
172 // See if there was already an entry in the map. Note that the map
173 // contains both virtual and real directories.
174 auto SeenDirInsertResult =
175 SeenDirEntries.insert(KV: {DirName, std::errc::no_such_file_or_directory});
176 if (!SeenDirInsertResult.second) {
177 if (SeenDirInsertResult.first->second)
178 return DirectoryEntryRef(*SeenDirInsertResult.first);
179 return llvm::errorCodeToError(EC: SeenDirInsertResult.first->second.getError());
180 }
181
182 // We've not seen this before. Fill it in.
183 ++NumDirCacheMisses;
184 auto &NamedDirEnt = *SeenDirInsertResult.first;
185 assert(!NamedDirEnt.second && "should be newly-created");
186
187 // Get the null-terminated directory name as stored as the key of the
188 // SeenDirEntries map.
189 StringRef InterndDirName = NamedDirEnt.first();
190
191 // Check to see if the directory exists.
192 llvm::vfs::Status Status;
193 auto statError = getStatValue(Path: InterndDirName, Status, isFile: false,
194 F: nullptr /*directory lookup*/);
195 if (statError) {
196 // There's no real directory at the given path.
197 if (CacheFailure)
198 NamedDirEnt.second = statError;
199 else
200 SeenDirEntries.erase(Key: DirName);
201 return llvm::errorCodeToError(EC: statError);
202 }
203
204 // It exists.
205 DirectoryEntry *&UDE = getRealDirEntry(Status);
206 NamedDirEnt.second = *UDE;
207
208 return DirectoryEntryRef(NamedDirEnt);
209}
210
211llvm::Expected<FileEntryRef> FileManager::getFileRef(StringRef Filename,
212 bool openFile,
213 bool CacheFailure,
214 bool IsText) {
215 ++NumFileLookups;
216
217 // See if there is already an entry in the map.
218 auto SeenFileInsertResult =
219 SeenFileEntries.insert(KV: {Filename, std::errc::no_such_file_or_directory});
220 if (!SeenFileInsertResult.second) {
221 if (!SeenFileInsertResult.first->second)
222 return llvm::errorCodeToError(
223 EC: SeenFileInsertResult.first->second.getError());
224 return FileEntryRef(*SeenFileInsertResult.first);
225 }
226
227 // We've not seen this before. Fill it in.
228 ++NumFileCacheMisses;
229 auto *NamedFileEnt = &*SeenFileInsertResult.first;
230 assert(!NamedFileEnt->second && "should be newly-created");
231
232 // Get the null-terminated file name as stored as the key of the
233 // SeenFileEntries map.
234 StringRef InterndFileName = NamedFileEnt->first();
235
236 // Look up the directory for the file. When looking up something like
237 // sys/foo.h we'll discover all of the search directories that have a 'sys'
238 // subdirectory. This will let us avoid having to waste time on known-to-fail
239 // searches when we go to find sys/bar.h, because all the search directories
240 // without a 'sys' subdir will get a cached failure result.
241 auto DirInfoOrErr = getDirectoryFromFile(FileMgr&: *this, Filename, CacheFailure);
242 if (!DirInfoOrErr) { // Directory doesn't exist, file can't exist.
243 std::error_code Err = errorToErrorCode(Err: DirInfoOrErr.takeError());
244 if (CacheFailure)
245 NamedFileEnt->second = Err;
246 else
247 SeenFileEntries.erase(Key: Filename);
248
249 return llvm::errorCodeToError(EC: Err);
250 }
251 DirectoryEntryRef DirInfo = *DirInfoOrErr;
252
253 // FIXME: Use the directory info to prune this, before doing the stat syscall.
254 // FIXME: This will reduce the # syscalls.
255
256 // Check to see if the file exists.
257 std::unique_ptr<llvm::vfs::File> F;
258 llvm::vfs::Status Status;
259 auto statError = getStatValue(Path: InterndFileName, Status, isFile: true,
260 F: openFile ? &F : nullptr, IsText);
261 if (statError) {
262 // There's no real file at the given path.
263 if (CacheFailure)
264 NamedFileEnt->second = statError;
265 else
266 SeenFileEntries.erase(Key: Filename);
267
268 return llvm::errorCodeToError(EC: statError);
269 }
270
271 assert((openFile || !F) && "undesired open file");
272
273 // It exists. See if we have already opened a file with the same inode.
274 // This occurs when one dir is symlinked to another, for example.
275 FileEntry *&UFE = UniqueRealFiles[Status.getUniqueID()];
276 bool ReusingEntry = UFE != nullptr;
277 if (!UFE)
278 UFE = new (FilesAlloc.Allocate()) FileEntry();
279
280 if (!Status.ExposesExternalVFSPath || Status.getName() == Filename) {
281 // Use the requested name. Set the FileEntry.
282 NamedFileEnt->second = FileEntryRef::MapValue(*UFE, DirInfo);
283 } else {
284 // Name mismatch. We need a redirect. First grab the actual entry we want
285 // to return.
286 //
287 // This redirection logic intentionally leaks the external name of a
288 // redirected file that uses 'use-external-name' in \a
289 // vfs::RedirectionFileSystem. This allows clang to report the external
290 // name to users (in diagnostics) and to tools that don't have access to
291 // the VFS (in debug info and dependency '.d' files).
292 //
293 // FIXME: This is pretty complex and has some very complicated interactions
294 // with the rest of clang. It's also inconsistent with how "real"
295 // filesystems behave and confuses parts of clang expect to see the
296 // name-as-accessed on the \a FileEntryRef.
297 //
298 // A potential plan to remove this is as follows -
299 // - Update callers such as `HeaderSearch::findUsableModuleForHeader()`
300 // to explicitly use the `getNameAsRequested()` rather than just using
301 // `getName()`.
302 // - Add a `FileManager::getExternalPath` API for explicitly getting the
303 // remapped external filename when there is one available. Adopt it in
304 // callers like diagnostics/deps reporting instead of calling
305 // `getName()` directly.
306 // - Switch the meaning of `FileEntryRef::getName()` to get the requested
307 // name, not the external name. Once that sticks, revert callers that
308 // want the requested name back to calling `getName()`.
309 // - Update the VFS to always return the requested name. This could also
310 // return the external name, or just have an API to request it
311 // lazily. The latter has the benefit of making accesses of the
312 // external path easily tracked, but may also require extra work than
313 // just returning up front.
314 // - (Optionally) Add an API to VFS to get the external filename lazily
315 // and update `FileManager::getExternalPath()` to use it instead. This
316 // has the benefit of making such accesses easily tracked, though isn't
317 // necessarily required (and could cause extra work than just adding to
318 // eg. `vfs::Status` up front).
319 auto &Redirection =
320 *SeenFileEntries
321 .insert(KV: {Status.getName(), FileEntryRef::MapValue(*UFE, DirInfo)})
322 .first;
323 assert(isa<FileEntry *>(Redirection.second->V) &&
324 "filename redirected to a non-canonical filename?");
325 assert(cast<FileEntry *>(Redirection.second->V) == UFE &&
326 "filename from getStatValue() refers to wrong file");
327
328 // Cache the redirection in the previously-inserted entry, still available
329 // in the tentative return value.
330 NamedFileEnt->second = FileEntryRef::MapValue(Redirection, DirInfo);
331 }
332
333 FileEntryRef ReturnedRef(*NamedFileEnt);
334 if (ReusingEntry) { // Already have an entry with this inode, return it.
335 return ReturnedRef;
336 }
337
338 // Otherwise, we don't have this file yet, add it.
339 UFE->Size = Status.getSize();
340 UFE->ModTime = llvm::sys::toTimeT(TP: Status.getLastModificationTime());
341 UFE->Dir = &DirInfo.getDirEntry();
342 UFE->UID = NextFileUID++;
343 UFE->UniqueID = Status.getUniqueID();
344 UFE->IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
345 UFE->IsDeviceFile =
346 Status.getType() == llvm::sys::fs::file_type::character_file;
347 UFE->File = std::move(F);
348
349 if (UFE->File) {
350 if (auto PathName = UFE->File->getName())
351 fillRealPathName(UFE, FileName: *PathName);
352 } else if (!openFile) {
353 // We should still fill the path even if we aren't opening the file.
354 fillRealPathName(UFE, FileName: InterndFileName);
355 }
356 return ReturnedRef;
357}
358
359llvm::Expected<FileEntryRef> FileManager::getSTDIN() {
360 // Only read stdin once.
361 if (STDIN)
362 return *STDIN;
363
364 auto ContentOrError = [] {
365 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
366 return llvm::MemoryBuffer::getSTDIN();
367 }();
368
369 if (!ContentOrError)
370 return llvm::errorCodeToError(EC: ContentOrError.getError());
371
372 auto Content = std::move(*ContentOrError);
373 STDIN = getVirtualFileRef(Filename: Content->getBufferIdentifier(),
374 Size: Content->getBufferSize(), ModificationTime: 0);
375 FileEntry &FE = const_cast<FileEntry &>(STDIN->getFileEntry());
376 FE.Content = std::move(Content);
377 FE.IsNamedPipe = true;
378 return *STDIN;
379}
380
381void FileManager::trackVFSUsage(bool Active) {
382 FS->visit(Callback: [Active](llvm::vfs::FileSystem &FileSys) {
383 if (auto *RFS = dyn_cast<llvm::vfs::RedirectingFileSystem>(Val: &FileSys))
384 RFS->setUsageTrackingActive(Active);
385 });
386}
387
388FileEntryRef FileManager::getVirtualFileRef(StringRef Filename, off_t Size,
389 time_t ModificationTime) {
390 ++NumFileLookups;
391
392 // See if there is already an entry in the map for an existing file.
393 auto &NamedFileEnt = *SeenFileEntries.insert(
394 KV: {Filename, std::errc::no_such_file_or_directory}).first;
395 if (NamedFileEnt.second) {
396 FileEntryRef::MapValue Value = *NamedFileEnt.second;
397 if (LLVM_LIKELY(isa<FileEntry *>(Value.V)))
398 return FileEntryRef(NamedFileEnt);
399 return FileEntryRef(*cast<const FileEntryRef::MapEntry *>(Val&: Value.V));
400 }
401
402 // We've not seen this before, or the file is cached as non-existent.
403 ++NumFileCacheMisses;
404 addAncestorsAsVirtualDirs(Path: Filename);
405 FileEntry *UFE = nullptr;
406
407 // Now that all ancestors of Filename are in the cache, the
408 // following call is guaranteed to find the DirectoryEntry from the
409 // cache. A virtual file can also have an empty filename, that could come
410 // from a source location preprocessor directive with an empty filename as
411 // an example, so we need to pretend it has a name to ensure a valid directory
412 // entry can be returned.
413 auto DirInfo = expectedToOptional(E: getDirectoryFromFile(
414 FileMgr&: *this, Filename: Filename.empty() ? "." : Filename, /*CacheFailure=*/true));
415 assert(DirInfo &&
416 "The directory of a virtual file should already be in the cache.");
417
418 // Check to see if the file exists. If so, drop the virtual file
419 llvm::vfs::Status Status;
420 const char *InterndFileName = NamedFileEnt.first().data();
421 if (!getStatValue(Path: InterndFileName, Status, isFile: true, F: nullptr)) {
422 Status = llvm::vfs::Status(
423 Status.getName(), Status.getUniqueID(),
424 llvm::sys::toTimePoint(T: ModificationTime),
425 Status.getUser(), Status.getGroup(), Size,
426 Status.getType(), Status.getPermissions());
427
428 auto &RealFE = UniqueRealFiles[Status.getUniqueID()];
429 if (RealFE) {
430 // If we had already opened this file, close it now so we don't
431 // leak the descriptor. We're not going to use the file
432 // descriptor anyway, since this is a virtual file.
433 if (RealFE->File)
434 RealFE->closeFile();
435 // If we already have an entry with this inode, return it.
436 //
437 // FIXME: Surely this should add a reference by the new name, and return
438 // it instead...
439 NamedFileEnt.second = FileEntryRef::MapValue(*RealFE, *DirInfo);
440 return FileEntryRef(NamedFileEnt);
441 }
442 // File exists, but no entry - create it.
443 RealFE = new (FilesAlloc.Allocate()) FileEntry();
444 RealFE->UniqueID = Status.getUniqueID();
445 RealFE->IsNamedPipe =
446 Status.getType() == llvm::sys::fs::file_type::fifo_file;
447 fillRealPathName(UFE: RealFE, FileName: Status.getName());
448
449 UFE = RealFE;
450 } else {
451 // File does not exist, create a virtual entry.
452 UFE = new (FilesAlloc.Allocate()) FileEntry();
453 VirtualFileEntries.push_back(Elt: UFE);
454 }
455
456 NamedFileEnt.second = FileEntryRef::MapValue(*UFE, *DirInfo);
457 UFE->Size = Size;
458 UFE->ModTime = ModificationTime;
459 UFE->Dir = &DirInfo->getDirEntry();
460 UFE->UID = NextFileUID++;
461 UFE->File.reset();
462 return FileEntryRef(NamedFileEnt);
463}
464
465OptionalFileEntryRef FileManager::getBypassFile(FileEntryRef VF) {
466 // Stat of the file and return nullptr if it doesn't exist.
467 llvm::vfs::Status Status;
468 if (getStatValue(Path: VF.getName(), Status, /*isFile=*/true, /*F=*/nullptr))
469 return std::nullopt;
470
471 if (!SeenBypassFileEntries)
472 SeenBypassFileEntries = std::make_unique<
473 llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>>>();
474
475 // If we've already bypassed just use the existing one.
476 auto Insertion = SeenBypassFileEntries->insert(
477 KV: {VF.getName(), std::errc::no_such_file_or_directory});
478 if (!Insertion.second)
479 return FileEntryRef(*Insertion.first);
480
481 // Fill in the new entry from the stat.
482 FileEntry *BFE = new (FilesAlloc.Allocate()) FileEntry();
483 BypassFileEntries.push_back(Elt: BFE);
484 Insertion.first->second = FileEntryRef::MapValue(*BFE, VF.getDir());
485 BFE->Size = Status.getSize();
486 BFE->Dir = VF.getFileEntry().Dir;
487 BFE->ModTime = llvm::sys::toTimeT(TP: Status.getLastModificationTime());
488 BFE->UID = NextFileUID++;
489
490 // Save the entry in the bypass table and return.
491 return FileEntryRef(*Insertion.first);
492}
493
494bool FileManager::fixupRelativePath(const FileSystemOptions &FileSystemOpts,
495 SmallVectorImpl<char> &Path) {
496 StringRef pathRef(Path.data(), Path.size());
497
498 if (FileSystemOpts.WorkingDir.empty()
499 || llvm::sys::path::is_absolute(path: pathRef))
500 return false;
501
502 SmallString<128> NewPath(FileSystemOpts.WorkingDir);
503 llvm::sys::path::append(path&: NewPath, a: pathRef);
504 Path = NewPath;
505 return true;
506}
507
508bool FileManager::makeAbsolutePath(SmallVectorImpl<char> &Path,
509 bool Canonicalize) const {
510 bool Changed = FixupRelativePath(Path);
511
512 if (!llvm::sys::path::is_absolute(path: StringRef(Path.data(), Path.size()))) {
513 FS->makeAbsolute(Path);
514 Changed = true;
515 }
516
517 if (Canonicalize)
518 Changed |= llvm::sys::path::remove_dots(path&: Path);
519
520 return Changed;
521}
522
523void FileManager::fillRealPathName(FileEntry *UFE, llvm::StringRef FileName) {
524 llvm::SmallString<128> AbsPath(FileName);
525 // This is not the same as `VFS::getRealPath()`, which resolves symlinks
526 // but can be very expensive on real file systems.
527 // FIXME: the semantic of RealPathName is unclear, and the name might be
528 // misleading. We need to clean up the interface here.
529 makeAbsolutePath(Path&: AbsPath);
530 llvm::sys::path::remove_dots(path&: AbsPath, /*remove_dot_dot=*/true);
531 UFE->RealPathName = std::string(AbsPath);
532}
533
534llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
535FileManager::getBufferForFile(FileEntryRef FE, bool isVolatile,
536 bool RequiresNullTerminator,
537 std::optional<int64_t> MaybeLimit, bool IsText) {
538 const FileEntry *Entry = &FE.getFileEntry();
539 // If the content is living on the file entry, return a reference to it.
540 if (Entry->Content)
541 return llvm::MemoryBuffer::getMemBuffer(Ref: Entry->Content->getMemBufferRef());
542
543 uint64_t FileSize = Entry->getSize();
544
545 if (MaybeLimit)
546 FileSize = *MaybeLimit;
547
548 // If there's a high enough chance that the file have changed since we
549 // got its size, force a stat before opening it.
550 if (isVolatile || Entry->isNamedPipe())
551 FileSize = -1;
552
553 StringRef Filename = FE.getName();
554 // If the file is already open, use the open file descriptor.
555 if (Entry->File) {
556 auto Result = Entry->File->getBuffer(Name: Filename, FileSize,
557 RequiresNullTerminator, IsVolatile: isVolatile);
558 Entry->closeFile();
559 return Result;
560 }
561
562 // Otherwise, open the file.
563 return getBufferForFileImpl(Filename, FileSize, isVolatile,
564 RequiresNullTerminator, IsText);
565}
566
567llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
568FileManager::getBufferForFileImpl(StringRef Filename, int64_t FileSize,
569 bool isVolatile, bool RequiresNullTerminator,
570 bool IsText) const {
571 if (FileSystemOpts.WorkingDir.empty())
572 return FS->getBufferForFile(Name: Filename, FileSize, RequiresNullTerminator,
573 IsVolatile: isVolatile, IsText);
574
575 SmallString<128> FilePath(Filename);
576 FixupRelativePath(Path&: FilePath);
577 return FS->getBufferForFile(Name: FilePath, FileSize, RequiresNullTerminator,
578 IsVolatile: isVolatile, IsText);
579}
580
581/// getStatValue - Get the 'stat' information for the specified path,
582/// using the cache to accelerate it if possible. This returns true
583/// if the path points to a virtual file or does not exist, or returns
584/// false if it's an existent real file. If FileDescriptor is NULL,
585/// do directory look-up instead of file look-up.
586std::error_code FileManager::getStatValue(StringRef Path,
587 llvm::vfs::Status &Status,
588 bool isFile,
589 std::unique_ptr<llvm::vfs::File> *F,
590 bool IsText) {
591 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
592 // absolute!
593 if (FileSystemOpts.WorkingDir.empty())
594 return FileSystemStatCache::get(Path, Status, isFile, F, Cache: StatCache.get(),
595 FS&: *FS, IsText);
596
597 SmallString<128> FilePath(Path);
598 FixupRelativePath(Path&: FilePath);
599
600 return FileSystemStatCache::get(Path: FilePath.c_str(), Status, isFile, F,
601 Cache: StatCache.get(), FS&: *FS, IsText);
602}
603
604std::error_code
605FileManager::getNoncachedStatValue(StringRef Path,
606 llvm::vfs::Status &Result) {
607 SmallString<128> FilePath(Path);
608 FixupRelativePath(Path&: FilePath);
609
610 llvm::ErrorOr<llvm::vfs::Status> S = FS->status(Path: FilePath.c_str());
611 if (!S)
612 return S.getError();
613 Result = *S;
614 return std::error_code();
615}
616
617void FileManager::GetUniqueIDMapping(
618 SmallVectorImpl<OptionalFileEntryRef> &UIDToFiles) const {
619 UIDToFiles.clear();
620 UIDToFiles.resize(N: NextFileUID);
621
622 for (const auto &Entry : SeenFileEntries) {
623 // Only return files that exist and are not redirected.
624 if (!Entry.getValue() || !isa<FileEntry *>(Val: Entry.getValue()->V))
625 continue;
626 FileEntryRef FE(Entry);
627 // Add this file if it's the first one with the UID, or if its name is
628 // better than the existing one.
629 OptionalFileEntryRef &ExistingFE = UIDToFiles[FE.getUID()];
630 if (!ExistingFE || FE.getName() < ExistingFE->getName())
631 ExistingFE = FE;
632 }
633}
634
635StringRef FileManager::getCanonicalName(DirectoryEntryRef Dir) {
636 return getCanonicalName(Entry: Dir, Name: Dir.getName());
637}
638
639StringRef FileManager::getCanonicalName(FileEntryRef File) {
640 return getCanonicalName(Entry: File, Name: File.getName());
641}
642
643StringRef FileManager::getCanonicalName(const void *Entry, StringRef Name) {
644 llvm::DenseMap<const void *, llvm::StringRef>::iterator Known =
645 CanonicalNames.find(Val: Entry);
646 if (Known != CanonicalNames.end())
647 return Known->second;
648
649 // Name comes from FileEntry/DirectoryEntry::getName(), so it is safe to
650 // store it in the DenseMap below.
651 StringRef CanonicalName(Name);
652
653 SmallString<256> AbsPathBuf;
654 SmallString<256> RealPathBuf;
655 if (!FS->getRealPath(Path: Name, Output&: RealPathBuf)) {
656 if (is_style_windows(S: llvm::sys::path::Style::native)) {
657 // For Windows paths, only use the real path if it doesn't resolve
658 // a substitute drive, as those are used to avoid MAX_PATH issues.
659 AbsPathBuf = Name;
660 if (!FS->makeAbsolute(Path&: AbsPathBuf)) {
661 if (llvm::sys::path::root_name(path: RealPathBuf) ==
662 llvm::sys::path::root_name(path: AbsPathBuf)) {
663 CanonicalName = RealPathBuf.str().copy(A&: CanonicalNameStorage);
664 } else {
665 // Fallback to using the absolute path.
666 // Simplifying /../ is semantically valid on Windows even in the
667 // presence of symbolic links.
668 llvm::sys::path::remove_dots(path&: AbsPathBuf, /*remove_dot_dot=*/true);
669 CanonicalName = AbsPathBuf.str().copy(A&: CanonicalNameStorage);
670 }
671 }
672 } else {
673 CanonicalName = RealPathBuf.str().copy(A&: CanonicalNameStorage);
674 }
675 }
676
677 CanonicalNames.insert(KV: {Entry, CanonicalName});
678 return CanonicalName;
679}
680
681void FileManager::AddStats(const FileManager &Other) {
682 assert(&Other != this && "Collecting stats into the same FileManager");
683 NumDirLookups += Other.NumDirLookups;
684 NumFileLookups += Other.NumFileLookups;
685 NumDirCacheMisses += Other.NumDirCacheMisses;
686 NumFileCacheMisses += Other.NumFileCacheMisses;
687}
688
689void FileManager::PrintStats() const {
690 llvm::errs() << "\n*** File Manager Stats:\n";
691 llvm::errs() << UniqueRealFiles.size() << " real files found, "
692 << UniqueRealDirs.size() << " real dirs found.\n";
693 llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
694 << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
695 llvm::errs() << NumDirLookups << " dir lookups, "
696 << NumDirCacheMisses << " dir cache misses.\n";
697 llvm::errs() << NumFileLookups << " file lookups, "
698 << NumFileCacheMisses << " file cache misses.\n";
699
700 getVirtualFileSystem().visit(Callback: [](llvm::vfs::FileSystem &VFS) {
701 if (auto *T = dyn_cast_or_null<llvm::vfs::TracingFileSystem>(Val: &VFS))
702 llvm::errs() << "\n*** Virtual File System Stats:\n"
703 << T->NumStatusCalls << " status() calls\n"
704 << T->NumOpenFileForReadCalls << " openFileForRead() calls\n"
705 << T->NumDirBeginCalls << " dir_begin() calls\n"
706 << T->NumGetRealPathCalls << " getRealPath() calls\n"
707 << T->NumExistsCalls << " exists() calls\n"
708 << T->NumIsLocalCalls << " isLocal() calls\n";
709 });
710
711 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
712}
713