1226efd35SChris Lattner //===--- FileManager.cpp - File System Probing and Caching ----------------===// 27a51313dSChris Lattner // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 67a51313dSChris Lattner // 77a51313dSChris Lattner //===----------------------------------------------------------------------===// 87a51313dSChris Lattner // 97a51313dSChris Lattner // This file implements the FileManager interface. 107a51313dSChris Lattner // 117a51313dSChris Lattner //===----------------------------------------------------------------------===// 127a51313dSChris Lattner // 137a51313dSChris Lattner // TODO: This should index all interesting directories with dirent calls. 147a51313dSChris Lattner // getdirentries ? 157a51313dSChris Lattner // opendir/readdir_r/closedir ? 167a51313dSChris Lattner // 177a51313dSChris Lattner //===----------------------------------------------------------------------===// 187a51313dSChris Lattner 197a51313dSChris Lattner #include "clang/Basic/FileManager.h" 20226efd35SChris Lattner #include "clang/Basic/FileSystemStatCache.h" 217a51313dSChris Lattner #include "llvm/ADT/SmallString.h" 223a02247dSChandler Carruth #include "llvm/Config/llvm-config.h" 23d2725a31SDavid Blaikie #include "llvm/ADT/STLExtras.h" 24740857faSMichael J. Spencer #include "llvm/Support/FileSystem.h" 2571731d6bSArgyrios Kyrtzidis #include "llvm/Support/MemoryBuffer.h" 268aaf4995SMichael J. Spencer #include "llvm/Support/Path.h" 273a02247dSChandler Carruth #include "llvm/Support/raw_ostream.h" 2835b79c24SEugene Zelenko #include <algorithm> 2935b79c24SEugene Zelenko #include <cassert> 3035b79c24SEugene Zelenko #include <climits> 3135b79c24SEugene Zelenko #include <cstdint> 3235b79c24SEugene Zelenko #include <cstdlib> 3326db6481SBenjamin Kramer #include <string> 3435b79c24SEugene Zelenko #include <utility> 35278038b4SChris Lattner 367a51313dSChris Lattner using namespace clang; 377a51313dSChris Lattner 385c04bd81STed Kremenek //===----------------------------------------------------------------------===// 395c04bd81STed Kremenek // Common logic. 405c04bd81STed Kremenek //===----------------------------------------------------------------------===// 417a51313dSChris Lattner 42c8130a74SBen Langmuir FileManager::FileManager(const FileSystemOptions &FSO, 43fc51490bSJonas Devlieghere IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) 44f6021ecdSBenjamin Kramer : FS(std::move(FS)), FileSystemOpts(FSO), SeenDirEntries(64), 45f6021ecdSBenjamin Kramer SeenFileEntries(64), NextFileUID(0) { 467a51313dSChris Lattner NumDirLookups = NumFileLookups = 0; 477a51313dSChris Lattner NumDirCacheMisses = NumFileCacheMisses = 0; 48c8130a74SBen Langmuir 49c8130a74SBen Langmuir // If the caller doesn't provide a virtual file system, just grab the real 50c8130a74SBen Langmuir // file system. 51f6021ecdSBenjamin Kramer if (!this->FS) 52fc51490bSJonas Devlieghere this->FS = llvm::vfs::getRealFileSystem(); 537a51313dSChris Lattner } 547a51313dSChris Lattner 55d2725a31SDavid Blaikie FileManager::~FileManager() = default; 567a51313dSChris Lattner 57d92b1ae1SAlex Lorenz void FileManager::setStatCache(std::unique_ptr<FileSystemStatCache> statCache) { 58d2eb58abSDouglas Gregor assert(statCache && "No stat cache provided?"); 5923430ccbSDavid Blaikie StatCache = std::move(statCache); 60d2eb58abSDouglas Gregor } 61d2eb58abSDouglas Gregor 62d92b1ae1SAlex Lorenz void FileManager::clearStatCache() { StatCache.reset(); } 633aad855aSManuel Klimek 649fc8faf9SAdrian Prantl /// Retrieve the directory that the given file name resides in. 65e1dd3e2cSZhanyong Wan /// Filename can point to either a real file or a virtual file. 66461f0722SHarlan Haskins static llvm::ErrorOr<const DirectoryEntry *> 67461f0722SHarlan Haskins getDirectoryFromFile(FileManager &FileMgr, StringRef Filename, 681735f4e7SDouglas Gregor bool CacheFailure) { 69f3c0ff73SZhanyong Wan if (Filename.empty()) 70461f0722SHarlan Haskins return std::errc::no_such_file_or_directory; 71e1dd3e2cSZhanyong Wan 72f3c0ff73SZhanyong Wan if (llvm::sys::path::is_separator(Filename[Filename.size() - 1])) 73461f0722SHarlan Haskins return std::errc::is_a_directory; 740c0e8040SChris Lattner 750e62c1ccSChris Lattner StringRef DirName = llvm::sys::path::parent_path(Filename); 760c0e8040SChris Lattner // Use the current directory if file has no path component. 77f3c0ff73SZhanyong Wan if (DirName.empty()) 78f3c0ff73SZhanyong Wan DirName = "."; 790c0e8040SChris Lattner 801735f4e7SDouglas Gregor return FileMgr.getDirectory(DirName, CacheFailure); 81407e2124SDouglas Gregor } 82407e2124SDouglas Gregor 83e1dd3e2cSZhanyong Wan /// Add all ancestors of the given path (pointing to either a file or 84e1dd3e2cSZhanyong Wan /// a directory) as virtual directories. 850e62c1ccSChris Lattner void FileManager::addAncestorsAsVirtualDirs(StringRef Path) { 860e62c1ccSChris Lattner StringRef DirName = llvm::sys::path::parent_path(Path); 87f3c0ff73SZhanyong Wan if (DirName.empty()) 881834dc75SDavid Majnemer DirName = "."; 89e1dd3e2cSZhanyong Wan 90461f0722SHarlan Haskins auto &NamedDirEnt = *SeenDirEntries.insert( 91461f0722SHarlan Haskins {DirName, std::errc::no_such_file_or_directory}).first; 92e1dd3e2cSZhanyong Wan 93e1dd3e2cSZhanyong Wan // When caching a virtual directory, we always cache its ancestors 94e1dd3e2cSZhanyong Wan // at the same time. Therefore, if DirName is already in the cache, 95e1dd3e2cSZhanyong Wan // we don't need to recurse as its ancestors must also already be in 96018ab5faSRichard Smith // the cache (or it's a known non-virtual directory). 97018ab5faSRichard Smith if (NamedDirEnt.second) 98e1dd3e2cSZhanyong Wan return; 99e1dd3e2cSZhanyong Wan 100e1dd3e2cSZhanyong Wan // Add the virtual directory to the cache. 101*2b3d49b6SJonas Devlieghere auto UDE = std::make_unique<DirectoryEntry>(); 1020df59d8cSMehdi Amini UDE->Name = NamedDirEnt.first(); 103461f0722SHarlan Haskins NamedDirEnt.second = *UDE.get(); 104d2725a31SDavid Blaikie VirtualDirectoryEntries.push_back(std::move(UDE)); 105e1dd3e2cSZhanyong Wan 106e1dd3e2cSZhanyong Wan // Recursively add the other ancestors. 107e1dd3e2cSZhanyong Wan addAncestorsAsVirtualDirs(DirName); 108e1dd3e2cSZhanyong Wan } 109e1dd3e2cSZhanyong Wan 110461f0722SHarlan Haskins /// Converts a llvm::ErrorOr<T &> to an llvm::ErrorOr<T *> by promoting 111461f0722SHarlan Haskins /// the address of the inner reference to a pointer or by propagating the error. 112461f0722SHarlan Haskins template <typename T> 113461f0722SHarlan Haskins static llvm::ErrorOr<T *> promoteInnerReference(llvm::ErrorOr<T &> value) { 114461f0722SHarlan Haskins if (value) return &*value; 115461f0722SHarlan Haskins return value.getError(); 116461f0722SHarlan Haskins } 117461f0722SHarlan Haskins 118461f0722SHarlan Haskins llvm::ErrorOr<const DirectoryEntry *> 119461f0722SHarlan Haskins FileManager::getDirectory(StringRef DirName, bool CacheFailure) { 1208bd8ee76SNAKAMURA Takumi // stat doesn't like trailing separators except for root directory. 12132f1acf1SNAKAMURA Takumi // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'. 12232f1acf1SNAKAMURA Takumi // (though it can strip '\\') 1238bd8ee76SNAKAMURA Takumi if (DirName.size() > 1 && 1248bd8ee76SNAKAMURA Takumi DirName != llvm::sys::path::root_path(DirName) && 1258bd8ee76SNAKAMURA Takumi llvm::sys::path::is_separator(DirName.back())) 12632f1acf1SNAKAMURA Takumi DirName = DirName.substr(0, DirName.size()-1); 1271865df49SNico Weber #ifdef _WIN32 128ee30546cSRafael Espindola // Fixing a problem with "clang C:test.c" on Windows. 129ee30546cSRafael Espindola // Stat("C:") does not recognize "C:" as a valid directory 130ee30546cSRafael Espindola std::string DirNameStr; 131ee30546cSRafael Espindola if (DirName.size() > 1 && DirName.back() == ':' && 132ee30546cSRafael Espindola DirName.equals_lower(llvm::sys::path::root_name(DirName))) { 133ee30546cSRafael Espindola DirNameStr = DirName.str() + '.'; 134ee30546cSRafael Espindola DirName = DirNameStr; 135ee30546cSRafael Espindola } 136ee30546cSRafael Espindola #endif 13732f1acf1SNAKAMURA Takumi 1387a51313dSChris Lattner ++NumDirLookups; 1397a51313dSChris Lattner 140e1dd3e2cSZhanyong Wan // See if there was already an entry in the map. Note that the map 141e1dd3e2cSZhanyong Wan // contains both virtual and real directories. 142461f0722SHarlan Haskins auto SeenDirInsertResult = 143461f0722SHarlan Haskins SeenDirEntries.insert({DirName, std::errc::no_such_file_or_directory}); 144018ab5faSRichard Smith if (!SeenDirInsertResult.second) 145461f0722SHarlan Haskins return promoteInnerReference(SeenDirInsertResult.first->second); 1467a51313dSChris Lattner 147018ab5faSRichard Smith // We've not seen this before. Fill it in. 1487a51313dSChris Lattner ++NumDirCacheMisses; 149018ab5faSRichard Smith auto &NamedDirEnt = *SeenDirInsertResult.first; 150018ab5faSRichard Smith assert(!NamedDirEnt.second && "should be newly-created"); 1517a51313dSChris Lattner 1527a51313dSChris Lattner // Get the null-terminated directory name as stored as the key of the 153e1dd3e2cSZhanyong Wan // SeenDirEntries map. 1540df59d8cSMehdi Amini StringRef InterndDirName = NamedDirEnt.first(); 1557a51313dSChris Lattner 1567a51313dSChris Lattner // Check to see if the directory exists. 15706f64d53SHarlan Haskins llvm::vfs::Status Status; 158461f0722SHarlan Haskins auto statError = getStatValue(InterndDirName, Status, false, 159461f0722SHarlan Haskins nullptr /*directory lookup*/); 160461f0722SHarlan Haskins if (statError) { 161e1dd3e2cSZhanyong Wan // There's no real directory at the given path. 162461f0722SHarlan Haskins if (CacheFailure) 163461f0722SHarlan Haskins NamedDirEnt.second = statError; 164461f0722SHarlan Haskins else 1651735f4e7SDouglas Gregor SeenDirEntries.erase(DirName); 166461f0722SHarlan Haskins return statError; 167e1dd3e2cSZhanyong Wan } 1687a51313dSChris Lattner 169e1dd3e2cSZhanyong Wan // It exists. See if we have already opened a directory with the 170e1dd3e2cSZhanyong Wan // same inode (this occurs on Unix-like systems when one dir is 171e1dd3e2cSZhanyong Wan // symlinked to another, for example) or the same path (on 172e1dd3e2cSZhanyong Wan // Windows). 17306f64d53SHarlan Haskins DirectoryEntry &UDE = UniqueRealDirs[Status.getUniqueID()]; 1747a51313dSChris Lattner 175461f0722SHarlan Haskins NamedDirEnt.second = UDE; 1760df59d8cSMehdi Amini if (UDE.getName().empty()) { 177e1dd3e2cSZhanyong Wan // We don't have this directory yet, add it. We use the string 178e1dd3e2cSZhanyong Wan // key from the SeenDirEntries map as the string. 1797a51313dSChris Lattner UDE.Name = InterndDirName; 180e1dd3e2cSZhanyong Wan } 181e1dd3e2cSZhanyong Wan 1827a51313dSChris Lattner return &UDE; 1837a51313dSChris Lattner } 1847a51313dSChris Lattner 185461f0722SHarlan Haskins llvm::ErrorOr<const FileEntry *> 186461f0722SHarlan Haskins FileManager::getFile(StringRef Filename, bool openFile, bool CacheFailure) { 1877a51313dSChris Lattner ++NumFileLookups; 1887a51313dSChris Lattner 1897a51313dSChris Lattner // See if there is already an entry in the map. 190461f0722SHarlan Haskins auto SeenFileInsertResult = 191461f0722SHarlan Haskins SeenFileEntries.insert({Filename, std::errc::no_such_file_or_directory}); 192018ab5faSRichard Smith if (!SeenFileInsertResult.second) 193461f0722SHarlan Haskins return promoteInnerReference(SeenFileInsertResult.first->second); 1947a51313dSChris Lattner 195018ab5faSRichard Smith // We've not seen this before. Fill it in. 196e84385feSSam McCall ++NumFileCacheMisses; 197018ab5faSRichard Smith auto &NamedFileEnt = *SeenFileInsertResult.first; 198018ab5faSRichard Smith assert(!NamedFileEnt.second && "should be newly-created"); 199fa361206SSam McCall 2007a51313dSChris Lattner // Get the null-terminated file name as stored as the key of the 201e1dd3e2cSZhanyong Wan // SeenFileEntries map. 2020df59d8cSMehdi Amini StringRef InterndFileName = NamedFileEnt.first(); 2037a51313dSChris Lattner 204966b25b9SChris Lattner // Look up the directory for the file. When looking up something like 205966b25b9SChris Lattner // sys/foo.h we'll discover all of the search directories that have a 'sys' 206966b25b9SChris Lattner // subdirectory. This will let us avoid having to waste time on known-to-fail 207966b25b9SChris Lattner // searches when we go to find sys/bar.h, because all the search directories 208966b25b9SChris Lattner // without a 'sys' subdir will get a cached failure result. 209461f0722SHarlan Haskins auto DirInfoOrErr = getDirectoryFromFile(*this, Filename, CacheFailure); 210461f0722SHarlan Haskins if (!DirInfoOrErr) { // Directory doesn't exist, file can't exist. 211461f0722SHarlan Haskins if (CacheFailure) 212461f0722SHarlan Haskins NamedFileEnt.second = DirInfoOrErr.getError(); 213461f0722SHarlan Haskins else 2141735f4e7SDouglas Gregor SeenFileEntries.erase(Filename); 2151735f4e7SDouglas Gregor 216461f0722SHarlan Haskins return DirInfoOrErr.getError(); 2171735f4e7SDouglas Gregor } 218461f0722SHarlan Haskins const DirectoryEntry *DirInfo = *DirInfoOrErr; 219407e2124SDouglas Gregor 2207a51313dSChris Lattner // FIXME: Use the directory info to prune this, before doing the stat syscall. 2217a51313dSChris Lattner // FIXME: This will reduce the # syscalls. 2227a51313dSChris Lattner 223018ab5faSRichard Smith // Check to see if the file exists. 224fc51490bSJonas Devlieghere std::unique_ptr<llvm::vfs::File> F; 22506f64d53SHarlan Haskins llvm::vfs::Status Status; 226461f0722SHarlan Haskins auto statError = getStatValue(InterndFileName, Status, true, 227461f0722SHarlan Haskins openFile ? &F : nullptr); 228461f0722SHarlan Haskins if (statError) { 229e1dd3e2cSZhanyong Wan // There's no real file at the given path. 230461f0722SHarlan Haskins if (CacheFailure) 231461f0722SHarlan Haskins NamedFileEnt.second = statError; 232461f0722SHarlan Haskins else 2331735f4e7SDouglas Gregor SeenFileEntries.erase(Filename); 2341735f4e7SDouglas Gregor 235461f0722SHarlan Haskins return statError; 236e1dd3e2cSZhanyong Wan } 2377a51313dSChris Lattner 238ab01d4bbSPatrik Hagglund assert((openFile || !F) && "undesired open file"); 239d6278e32SArgyrios Kyrtzidis 2407a51313dSChris Lattner // It exists. See if we have already opened a file with the same inode. 2417a51313dSChris Lattner // This occurs when one dir is symlinked to another, for example. 24206f64d53SHarlan Haskins FileEntry &UFE = UniqueRealFiles[Status.getUniqueID()]; 2437a51313dSChris Lattner 244461f0722SHarlan Haskins NamedFileEnt.second = UFE; 245ab86fbe4SBen Langmuir 246ab86fbe4SBen Langmuir // If the name returned by getStatValue is different than Filename, re-intern 247ab86fbe4SBen Langmuir // the name. 24806f64d53SHarlan Haskins if (Status.getName() != Filename) { 24906f64d53SHarlan Haskins auto &NamedFileEnt = 250461f0722SHarlan Haskins *SeenFileEntries.insert({Status.getName(), UFE}).first; 251461f0722SHarlan Haskins assert(&*NamedFileEnt.second == &UFE && 252ab86fbe4SBen Langmuir "filename from getStatValue() refers to wrong file"); 25313156b68SDavid Blaikie InterndFileName = NamedFileEnt.first().data(); 254ab86fbe4SBen Langmuir } 255ab86fbe4SBen Langmuir 256c8a71468SBen Langmuir if (UFE.isValid()) { // Already have an entry with this inode, return it. 2575de00f3bSBen Langmuir 2585de00f3bSBen Langmuir // FIXME: this hack ensures that if we look up a file by a virtual path in 2595de00f3bSBen Langmuir // the VFS that the getDir() will have the virtual path, even if we found 2605de00f3bSBen Langmuir // the file by a 'real' path first. This is required in order to find a 2615de00f3bSBen Langmuir // module's structure when its headers/module map are mapped in the VFS. 2625de00f3bSBen Langmuir // We should remove this as soon as we can properly support a file having 2635de00f3bSBen Langmuir // multiple names. 26406f64d53SHarlan Haskins if (DirInfo != UFE.Dir && Status.IsVFSMapped) 2655de00f3bSBen Langmuir UFE.Dir = DirInfo; 2665de00f3bSBen Langmuir 267c0ff9908SManuel Klimek // Always update the name to use the last name by which a file was accessed. 268c0ff9908SManuel Klimek // FIXME: Neither this nor always using the first name is correct; we want 269c0ff9908SManuel Klimek // to switch towards a design where we return a FileName object that 270c0ff9908SManuel Klimek // encapsulates both the name by which the file was accessed and the 271c0ff9908SManuel Klimek // corresponding FileEntry. 272ab86fbe4SBen Langmuir UFE.Name = InterndFileName; 273c0ff9908SManuel Klimek 2747a51313dSChris Lattner return &UFE; 275dd278430SChris Lattner } 2767a51313dSChris Lattner 277c9b7234eSBen Langmuir // Otherwise, we don't have this file yet, add it. 278ab86fbe4SBen Langmuir UFE.Name = InterndFileName; 27906f64d53SHarlan Haskins UFE.Size = Status.getSize(); 28006f64d53SHarlan Haskins UFE.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime()); 2817a51313dSChris Lattner UFE.Dir = DirInfo; 2827a51313dSChris Lattner UFE.UID = NextFileUID++; 28306f64d53SHarlan Haskins UFE.UniqueID = Status.getUniqueID(); 28406f64d53SHarlan Haskins UFE.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file; 285fa361206SSam McCall UFE.File = std::move(F); 286c8a71468SBen Langmuir UFE.IsValid = true; 287ddbabc6bSSimon Marchi 288fa361206SSam McCall if (UFE.File) { 289fa361206SSam McCall if (auto PathName = UFE.File->getName()) 290fa361206SSam McCall fillRealPathName(&UFE, *PathName); 291cd8607dbSJan Korous } else if (!openFile) { 292cd8607dbSJan Korous // We should still fill the path even if we aren't opening the file. 293cd8607dbSJan Korous fillRealPathName(&UFE, InterndFileName); 294fa361206SSam McCall } 2957a51313dSChris Lattner return &UFE; 2967a51313dSChris Lattner } 2977a51313dSChris Lattner 298407e2124SDouglas Gregor const FileEntry * 2990e62c1ccSChris Lattner FileManager::getVirtualFile(StringRef Filename, off_t Size, 3005159f616SChris Lattner time_t ModificationTime) { 301407e2124SDouglas Gregor ++NumFileLookups; 302407e2124SDouglas Gregor 303018ab5faSRichard Smith // See if there is already an entry in the map for an existing file. 304461f0722SHarlan Haskins auto &NamedFileEnt = *SeenFileEntries.insert( 305461f0722SHarlan Haskins {Filename, std::errc::no_such_file_or_directory}).first; 306018ab5faSRichard Smith if (NamedFileEnt.second) 307461f0722SHarlan Haskins return &*NamedFileEnt.second; 308407e2124SDouglas Gregor 309018ab5faSRichard Smith // We've not seen this before, or the file is cached as non-existent. 310407e2124SDouglas Gregor ++NumFileCacheMisses; 311e1dd3e2cSZhanyong Wan addAncestorsAsVirtualDirs(Filename); 312f1186c5aSCraig Topper FileEntry *UFE = nullptr; 313e1dd3e2cSZhanyong Wan 314e1dd3e2cSZhanyong Wan // Now that all ancestors of Filename are in the cache, the 315e1dd3e2cSZhanyong Wan // following call is guaranteed to find the DirectoryEntry from the 316e1dd3e2cSZhanyong Wan // cache. 317461f0722SHarlan Haskins auto DirInfo = getDirectoryFromFile(*this, Filename, /*CacheFailure=*/true); 318e1dd3e2cSZhanyong Wan assert(DirInfo && 319e1dd3e2cSZhanyong Wan "The directory of a virtual file should already be in the cache."); 320e1dd3e2cSZhanyong Wan 321606c4ac3SDouglas Gregor // Check to see if the file exists. If so, drop the virtual file 32206f64d53SHarlan Haskins llvm::vfs::Status Status; 32313156b68SDavid Blaikie const char *InterndFileName = NamedFileEnt.first().data(); 324461f0722SHarlan Haskins if (!getStatValue(InterndFileName, Status, true, nullptr)) { 32506f64d53SHarlan Haskins UFE = &UniqueRealFiles[Status.getUniqueID()]; 32606f64d53SHarlan Haskins Status = llvm::vfs::Status( 32706f64d53SHarlan Haskins Status.getName(), Status.getUniqueID(), 32806f64d53SHarlan Haskins llvm::sys::toTimePoint(ModificationTime), 32906f64d53SHarlan Haskins Status.getUser(), Status.getGroup(), Size, 33006f64d53SHarlan Haskins Status.getType(), Status.getPermissions()); 331606c4ac3SDouglas Gregor 332461f0722SHarlan Haskins NamedFileEnt.second = *UFE; 333606c4ac3SDouglas Gregor 334606c4ac3SDouglas Gregor // If we had already opened this file, close it now so we don't 335606c4ac3SDouglas Gregor // leak the descriptor. We're not going to use the file 336606c4ac3SDouglas Gregor // descriptor anyway, since this is a virtual file. 337c8130a74SBen Langmuir if (UFE->File) 338c8130a74SBen Langmuir UFE->closeFile(); 339606c4ac3SDouglas Gregor 340606c4ac3SDouglas Gregor // If we already have an entry with this inode, return it. 341c8a71468SBen Langmuir if (UFE->isValid()) 342606c4ac3SDouglas Gregor return UFE; 343c9b7234eSBen Langmuir 34406f64d53SHarlan Haskins UFE->UniqueID = Status.getUniqueID(); 34506f64d53SHarlan Haskins UFE->IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file; 34606f64d53SHarlan Haskins fillRealPathName(UFE, Status.getName()); 347018ab5faSRichard Smith } else { 348*2b3d49b6SJonas Devlieghere VirtualFileEntries.push_back(std::make_unique<FileEntry>()); 349d2725a31SDavid Blaikie UFE = VirtualFileEntries.back().get(); 350461f0722SHarlan Haskins NamedFileEnt.second = *UFE; 351606c4ac3SDouglas Gregor } 352407e2124SDouglas Gregor 3539624b695SChris Lattner UFE->Name = InterndFileName; 354407e2124SDouglas Gregor UFE->Size = Size; 355407e2124SDouglas Gregor UFE->ModTime = ModificationTime; 356461f0722SHarlan Haskins UFE->Dir = *DirInfo; 357407e2124SDouglas Gregor UFE->UID = NextFileUID++; 358dfffaf57SErik Verbruggen UFE->IsValid = true; 359c8130a74SBen Langmuir UFE->File.reset(); 360407e2124SDouglas Gregor return UFE; 361407e2124SDouglas Gregor } 362407e2124SDouglas Gregor 363c56419edSArgyrios Kyrtzidis bool FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const { 3640e62c1ccSChris Lattner StringRef pathRef(path.data(), path.size()); 365b5c356a4SAnders Carlsson 3669ba8fb1eSAnders Carlsson if (FileSystemOpts.WorkingDir.empty() 3679ba8fb1eSAnders Carlsson || llvm::sys::path::is_absolute(pathRef)) 368c56419edSArgyrios Kyrtzidis return false; 36971731d6bSArgyrios Kyrtzidis 3702c1dd271SDylan Noblesmith SmallString<128> NewPath(FileSystemOpts.WorkingDir); 371b5c356a4SAnders Carlsson llvm::sys::path::append(NewPath, pathRef); 3726e640998SChris Lattner path = NewPath; 373c56419edSArgyrios Kyrtzidis return true; 374c56419edSArgyrios Kyrtzidis } 375c56419edSArgyrios Kyrtzidis 376c56419edSArgyrios Kyrtzidis bool FileManager::makeAbsolutePath(SmallVectorImpl<char> &Path) const { 377c56419edSArgyrios Kyrtzidis bool Changed = FixupRelativePath(Path); 378c56419edSArgyrios Kyrtzidis 379c56419edSArgyrios Kyrtzidis if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) { 38047035c02SIlya Biryukov FS->makeAbsolute(Path); 381c56419edSArgyrios Kyrtzidis Changed = true; 382c56419edSArgyrios Kyrtzidis } 383c56419edSArgyrios Kyrtzidis 384c56419edSArgyrios Kyrtzidis return Changed; 3856e640998SChris Lattner } 3866e640998SChris Lattner 387e9870c0cSKadir Cetinkaya void FileManager::fillRealPathName(FileEntry *UFE, llvm::StringRef FileName) { 388e9870c0cSKadir Cetinkaya llvm::SmallString<128> AbsPath(FileName); 389e9870c0cSKadir Cetinkaya // This is not the same as `VFS::getRealPath()`, which resolves symlinks 390e9870c0cSKadir Cetinkaya // but can be very expensive on real file systems. 391e9870c0cSKadir Cetinkaya // FIXME: the semantic of RealPathName is unclear, and the name might be 392e9870c0cSKadir Cetinkaya // misleading. We need to clean up the interface here. 393e9870c0cSKadir Cetinkaya makeAbsolutePath(AbsPath); 394e9870c0cSKadir Cetinkaya llvm::sys::path::remove_dots(AbsPath, /*remove_dot_dot=*/true); 395e9870c0cSKadir Cetinkaya UFE->RealPathName = AbsPath.str(); 396e9870c0cSKadir Cetinkaya } 397e9870c0cSKadir Cetinkaya 398a885796dSBenjamin Kramer llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 399a885796dSBenjamin Kramer FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile, 400a885796dSBenjamin Kramer bool ShouldCloseOpenFile) { 4016d7833f1SArgyrios Kyrtzidis uint64_t FileSize = Entry->getSize(); 4026d7833f1SArgyrios Kyrtzidis // If there's a high enough chance that the file have changed since we 4036d7833f1SArgyrios Kyrtzidis // got its size, force a stat before opening it. 4046d7833f1SArgyrios Kyrtzidis if (isVolatile) 4056d7833f1SArgyrios Kyrtzidis FileSize = -1; 4066d7833f1SArgyrios Kyrtzidis 407004b9c7aSMehdi Amini StringRef Filename = Entry->getName(); 4085ea7d07dSChris Lattner // If the file is already open, use the open file descriptor. 409c8130a74SBen Langmuir if (Entry->File) { 410a885796dSBenjamin Kramer auto Result = 411a885796dSBenjamin Kramer Entry->File->getBuffer(Filename, FileSize, 41226d56393SArgyrios Kyrtzidis /*RequiresNullTerminator=*/true, isVolatile); 4139801b253SBen Langmuir // FIXME: we need a set of APIs that can make guarantees about whether a 4149801b253SBen Langmuir // FileEntry is open or not. 4159801b253SBen Langmuir if (ShouldCloseOpenFile) 416c8130a74SBen Langmuir Entry->closeFile(); 4176406f7b8SRafael Espindola return Result; 4185ea7d07dSChris Lattner } 4196e640998SChris Lattner 4205ea7d07dSChris Lattner // Otherwise, open the file. 421669b0b15SArgyrios Kyrtzidis 422a885796dSBenjamin Kramer if (FileSystemOpts.WorkingDir.empty()) 423a885796dSBenjamin Kramer return FS->getBufferForFile(Filename, FileSize, 42426d56393SArgyrios Kyrtzidis /*RequiresNullTerminator=*/true, isVolatile); 4255ea7d07dSChris Lattner 4262c1dd271SDylan Noblesmith SmallString<128> FilePath(Entry->getName()); 427878b3e2bSAnders Carlsson FixupRelativePath(FilePath); 42892e1b62dSYaron Keren return FS->getBufferForFile(FilePath, FileSize, 42926d56393SArgyrios Kyrtzidis /*RequiresNullTerminator=*/true, isVolatile); 43026b5c190SChris Lattner } 43126b5c190SChris Lattner 432a885796dSBenjamin Kramer llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 4332ebe3a02SIvan Donchevskii FileManager::getBufferForFile(StringRef Filename, bool isVolatile) { 434a885796dSBenjamin Kramer if (FileSystemOpts.WorkingDir.empty()) 4352ebe3a02SIvan Donchevskii return FS->getBufferForFile(Filename, -1, true, isVolatile); 43626b5c190SChris Lattner 4372c1dd271SDylan Noblesmith SmallString<128> FilePath(Filename); 438878b3e2bSAnders Carlsson FixupRelativePath(FilePath); 4392ebe3a02SIvan Donchevskii return FS->getBufferForFile(FilePath.c_str(), -1, true, isVolatile); 44071731d6bSArgyrios Kyrtzidis } 44171731d6bSArgyrios Kyrtzidis 442e1dd3e2cSZhanyong Wan /// getStatValue - Get the 'stat' information for the specified path, 443e1dd3e2cSZhanyong Wan /// using the cache to accelerate it if possible. This returns true 444e1dd3e2cSZhanyong Wan /// if the path points to a virtual file or does not exist, or returns 445e1dd3e2cSZhanyong Wan /// false if it's an existent real file. If FileDescriptor is NULL, 446e1dd3e2cSZhanyong Wan /// do directory look-up instead of file look-up. 447461f0722SHarlan Haskins std::error_code 448461f0722SHarlan Haskins FileManager::getStatValue(StringRef Path, llvm::vfs::Status &Status, 449461f0722SHarlan Haskins bool isFile, std::unique_ptr<llvm::vfs::File> *F) { 450226efd35SChris Lattner // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be 451226efd35SChris Lattner // absolute! 4525769c3dfSChris Lattner if (FileSystemOpts.WorkingDir.empty()) 453461f0722SHarlan Haskins return FileSystemStatCache::get(Path, Status, isFile, F, 454461f0722SHarlan Haskins StatCache.get(), *FS); 455226efd35SChris Lattner 4562c1dd271SDylan Noblesmith SmallString<128> FilePath(Path); 457878b3e2bSAnders Carlsson FixupRelativePath(FilePath); 45871731d6bSArgyrios Kyrtzidis 459461f0722SHarlan Haskins return FileSystemStatCache::get(FilePath.c_str(), Status, isFile, F, 460461f0722SHarlan Haskins StatCache.get(), *FS); 46171731d6bSArgyrios Kyrtzidis } 46271731d6bSArgyrios Kyrtzidis 463461f0722SHarlan Haskins std::error_code 464461f0722SHarlan Haskins FileManager::getNoncachedStatValue(StringRef Path, 465fc51490bSJonas Devlieghere llvm::vfs::Status &Result) { 4662c1dd271SDylan Noblesmith SmallString<128> FilePath(Path); 4675e368405SAnders Carlsson FixupRelativePath(FilePath); 4685e368405SAnders Carlsson 469fc51490bSJonas Devlieghere llvm::ErrorOr<llvm::vfs::Status> S = FS->status(FilePath.c_str()); 470c8130a74SBen Langmuir if (!S) 471461f0722SHarlan Haskins return S.getError(); 472c8130a74SBen Langmuir Result = *S; 473461f0722SHarlan Haskins return std::error_code(); 4745e368405SAnders Carlsson } 4755e368405SAnders Carlsson 476b3074003SAxel Naumann void FileManager::invalidateCache(const FileEntry *Entry) { 477b3074003SAxel Naumann assert(Entry && "Cannot invalidate a NULL FileEntry"); 47838179d96SAxel Naumann 47938179d96SAxel Naumann SeenFileEntries.erase(Entry->getName()); 480b3074003SAxel Naumann 481b3074003SAxel Naumann // FileEntry invalidation should not block future optimizations in the file 482b3074003SAxel Naumann // caches. Possible alternatives are cache truncation (invalidate last N) or 483b3074003SAxel Naumann // invalidation of the whole cache. 484018ab5faSRichard Smith // 485018ab5faSRichard Smith // FIXME: This is broken. We sometimes have the same FileEntry* shared 486018ab5faSRichard Smith // betweeen multiple SeenFileEntries, so this can leave dangling pointers. 487c9b7234eSBen Langmuir UniqueRealFiles.erase(Entry->getUniqueID()); 48838179d96SAxel Naumann } 48938179d96SAxel Naumann 49009b6989eSDouglas Gregor void FileManager::GetUniqueIDMapping( 4910e62c1ccSChris Lattner SmallVectorImpl<const FileEntry *> &UIDToFiles) const { 49209b6989eSDouglas Gregor UIDToFiles.clear(); 49309b6989eSDouglas Gregor UIDToFiles.resize(NextFileUID); 49409b6989eSDouglas Gregor 49509b6989eSDouglas Gregor // Map file entries 496461f0722SHarlan Haskins for (llvm::StringMap<llvm::ErrorOr<FileEntry &>, 497461f0722SHarlan Haskins llvm::BumpPtrAllocator>::const_iterator 498e1dd3e2cSZhanyong Wan FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end(); 49909b6989eSDouglas Gregor FE != FEEnd; ++FE) 500461f0722SHarlan Haskins if (auto Entry = FE->getValue()) { 501461f0722SHarlan Haskins UIDToFiles[Entry->getUID()] = &*Entry; 502461f0722SHarlan Haskins } 50309b6989eSDouglas Gregor 50409b6989eSDouglas Gregor // Map virtual file entries 505d2725a31SDavid Blaikie for (const auto &VFE : VirtualFileEntries) 506d2725a31SDavid Blaikie UIDToFiles[VFE->getUID()] = VFE.get(); 50709b6989eSDouglas Gregor } 508226efd35SChris Lattner 5096eec06d0SArgyrios Kyrtzidis void FileManager::modifyFileEntry(FileEntry *File, 5106eec06d0SArgyrios Kyrtzidis off_t Size, time_t ModificationTime) { 5116eec06d0SArgyrios Kyrtzidis File->Size = Size; 5126eec06d0SArgyrios Kyrtzidis File->ModTime = ModificationTime; 5136eec06d0SArgyrios Kyrtzidis } 5146eec06d0SArgyrios Kyrtzidis 515e00c8b20SDouglas Gregor StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) { 516e00c8b20SDouglas Gregor // FIXME: use llvm::sys::fs::canonical() when it gets implemented 517e00c8b20SDouglas Gregor llvm::DenseMap<const DirectoryEntry *, llvm::StringRef>::iterator Known 518e00c8b20SDouglas Gregor = CanonicalDirNames.find(Dir); 519e00c8b20SDouglas Gregor if (Known != CanonicalDirNames.end()) 520e00c8b20SDouglas Gregor return Known->second; 521e00c8b20SDouglas Gregor 522e00c8b20SDouglas Gregor StringRef CanonicalName(Dir->getName()); 52354cc3c2fSRichard Smith 5245fb18fecSEric Liu SmallString<4096> CanonicalNameBuf; 5255fb18fecSEric Liu if (!FS->getRealPath(Dir->getName(), CanonicalNameBuf)) 526da4690aeSBenjamin Kramer CanonicalName = StringRef(CanonicalNameBuf).copy(CanonicalNameStorage); 527e00c8b20SDouglas Gregor 528018ab5faSRichard Smith CanonicalDirNames.insert({Dir, CanonicalName}); 529e00c8b20SDouglas Gregor return CanonicalName; 530e00c8b20SDouglas Gregor } 531226efd35SChris Lattner 5327a51313dSChris Lattner void FileManager::PrintStats() const { 53389b422c1SBenjamin Kramer llvm::errs() << "\n*** File Manager Stats:\n"; 534e1dd3e2cSZhanyong Wan llvm::errs() << UniqueRealFiles.size() << " real files found, " 535e1dd3e2cSZhanyong Wan << UniqueRealDirs.size() << " real dirs found.\n"; 536e1dd3e2cSZhanyong Wan llvm::errs() << VirtualFileEntries.size() << " virtual files found, " 537e1dd3e2cSZhanyong Wan << VirtualDirectoryEntries.size() << " virtual dirs found.\n"; 53889b422c1SBenjamin Kramer llvm::errs() << NumDirLookups << " dir lookups, " 5397a51313dSChris Lattner << NumDirCacheMisses << " dir cache misses.\n"; 54089b422c1SBenjamin Kramer llvm::errs() << NumFileLookups << " file lookups, " 5417a51313dSChris Lattner << NumFileCacheMisses << " file cache misses.\n"; 5427a51313dSChris Lattner 54389b422c1SBenjamin Kramer //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups; 5447a51313dSChris Lattner } 545