LLVM  14.0.0git
Classes | Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
llvm::vfs::RedirectingFileSystem Class Reference

A virtual file system parsed from a YAML file. More...

#include "llvm/Support/VirtualFileSystem.h"

Inheritance diagram for llvm::vfs::RedirectingFileSystem:
Inheritance graph
[legend]
Collaboration diagram for llvm::vfs::RedirectingFileSystem:
Collaboration graph
[legend]

Classes

class  DirectoryEntry
 A directory in the vfs with explicitly specified contents. More...
 
class  DirectoryRemapEntry
 A directory in the vfs that maps to a directory in the external file system. More...
 
class  Entry
 A single file or directory in the VFS. More...
 
class  FileEntry
 A file in the vfs that maps to a file in the external file system. More...
 
struct  LookupResult
 Represents the result of a path lookup into the RedirectingFileSystem. More...
 
class  RemapEntry
 A file or directory in the vfs that is mapped to a file or directory in the external filesystem. More...
 

Public Types

enum  EntryKind { EK_Directory, EK_DirectoryRemap, EK_File }
 
enum  NameKind { NK_NotSet, NK_External, NK_Virtual }
 

Public Member Functions

ErrorOr< LookupResultlookupPath (StringRef Path) const
 Looks up Path in Roots and returns a LookupResult giving the matched entry and, if the entry was a FileEntry or DirectoryRemapEntry, the path it redirects to in the external file system. More...
 
ErrorOr< Statusstatus (const Twine &Path) override
 Get the status of the entry at Path, if one exists. More...
 
ErrorOr< std::unique_ptr< File > > openFileForRead (const Twine &Path) override
 Get a File object for the file at Path, if one exists. More...
 
std::error_code getRealPath (const Twine &Path, SmallVectorImpl< char > &Output) const override
 Gets real path of Path e.g. More...
 
llvm::ErrorOr< std::string > getCurrentWorkingDirectory () const override
 Get the working directory of this file system. More...
 
std::error_code setCurrentWorkingDirectory (const Twine &Path) override
 Set the working directory. More...
 
std::error_code isLocal (const Twine &Path, bool &Result) override
 Is the file mounted on a local filesystem? More...
 
std::error_code makeAbsolute (SmallVectorImpl< char > &Path) const override
 Make Path an absolute path. More...
 
directory_iterator dir_begin (const Twine &Dir, std::error_code &EC) override
 Get a directory_iterator for Dir. More...
 
void setExternalContentsPrefixDir (StringRef PrefixDir)
 
StringRef getExternalContentsPrefixDir () const
 
void setFallthrough (bool Fallthrough)
 
std::vector< llvm::StringRefgetRoots () const
 
void dump (raw_ostream &OS) const
 
void dumpEntry (raw_ostream &OS, Entry *E, int NumSpaces=0) const
 
LLVM_DUMP_METHOD void dump () const
 
- Public Member Functions inherited from llvm::vfs::FileSystem
virtual ~FileSystem ()
 
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile (const Twine &Name, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
 This is a convenience method that opens a file, gets its content and then closes the file. More...
 
bool exists (const Twine &Path)
 Check whether a file exists. Provided for convenience. More...
 
- Public Member Functions inherited from llvm::ThreadSafeRefCountedBase< FileSystem >
void Retain () const
 
void Release () const
 

Static Public Member Functions

static std::unique_ptr< RedirectingFileSystemcreate (std::unique_ptr< MemoryBuffer > Buffer, SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath, void *DiagContext, IntrusiveRefCntPtr< FileSystem > ExternalFS)
 Parses Buffer, which is expected to be in YAML format and returns a virtual file system representing its contents. More...
 
static std::unique_ptr< RedirectingFileSystemcreate (ArrayRef< std::pair< std::string, std::string >> RemappedFiles, bool UseExternalNames, FileSystem &ExternalFS)
 Redirect each of the remapped files from first to second. More...
 

Friends

class RedirectingFSDirIterImpl
 
class RedirectingFileSystemParser
 

Additional Inherited Members

- Protected Member Functions inherited from llvm::ThreadSafeRefCountedBase< FileSystem >
 ThreadSafeRefCountedBase ()=default
 
 ThreadSafeRefCountedBase (const ThreadSafeRefCountedBase &)
 
ThreadSafeRefCountedBaseoperator= (const ThreadSafeRefCountedBase &)=delete
 
 ~ThreadSafeRefCountedBase ()
 

Detailed Description

A virtual file system parsed from a YAML file.

Currently, this class allows creating virtual files and directories. Virtual files map to existing external files in ExternalFS, and virtual directories may either map to existing directories in ExternalFS or list their contents in the form of other virtual directories and/or files.

The basic structure of the parsed file is:

/// {
///   'version': <version number>,
///   <optional configuration>
///   'roots': [
///              <directory entries>
///            ]
/// }
/// 
All configuration options are optional.
  'case-sensitive': <boolean, default=(true for Posix, false for Windows)>
  'use-external-names': <boolean, default=true>
  'overlay-relative': <boolean, default=false>
  'fallthrough': <boolean, default=true>

Virtual directories that list their contents are represented as
\verbatim

/// { /// 'type': 'directory', /// 'name': <string>, /// 'contents': [ <file or directory entries> ] /// } ///

The default attributes for such virtual directories are:
\verbatim

/// MTime = now() when created /// Perms = 0777 /// User = Group = 0 /// Size = 0 /// UniqueID = unspecified unique value ///

When a path prefix matches such a directory, the next component in the path
is matched against the entries in the 'contents' array.

Re-mapped directories, on the other hand, are represented as
/// \verbatim

/// { /// 'type': 'directory-remap', /// 'name': <string>, /// 'use-external-name': <boolean>, # Optional /// 'external-contents': <path to external directory> /// } ///

and inherit their attributes from the external directory. When a path
prefix matches such an entry, the unmatched components are appended to the
'external-contents' path, and the resulting path is looked up in the
external file system instead.

Re-mapped files are represented as
\verbatim

/// { /// 'type': 'file', /// 'name': <string>, /// 'use-external-name': <boolean>, # Optional /// 'external-contents': <path to external file> /// } ///

Their attributes and file contents are determined by looking up the file at
their 'external-contents' path in the external file system.

For 'file', 'directory' and 'directory-remap' entries the 'name' field may
contain multiple path components (e.g. /path/to/file). However, any
directory in such a path that contains more than one child must be uniquely
represented by a 'directory' entry.

When the 'use-external-name' field is set, calls to \a vfs::File::status()
give the external (remapped) filesystem name instead of the name the file
was accessed by. This is an intentional leak through the \a
RedirectingFileSystem abstraction layer. It enables clients to discover
(and use) the external file location when communicating with users or tools
that don't use the same VFS overlay.

FIXME: 'use-external-name' causes behaviour that's inconsistent with how
"real" filesystems behave. Maybe there should be a separate channel for
this information. 

Definition at line 610 of file VirtualFileSystem.h.

Member Enumeration Documentation

◆ EntryKind

Enumerator
EK_Directory 
EK_DirectoryRemap 
EK_File 

Definition at line 612 of file VirtualFileSystem.h.

◆ NameKind

Enumerator
NK_NotSet 
NK_External 
NK_Virtual 

Definition at line 613 of file VirtualFileSystem.h.

Member Function Documentation

◆ create() [1/2]

std::unique_ptr< RedirectingFileSystem > RedirectingFileSystem::create ( ArrayRef< std::pair< std::string, std::string >>  RemappedFiles,
bool  UseExternalNames,
FileSystem ExternalFS 
)
static

Redirect each of the remapped files from first to second.

Definition at line 1793 of file VirtualFileSystem.cpp.

References llvm::X86AS::FS.

◆ create() [2/2]

std::unique_ptr< RedirectingFileSystem > RedirectingFileSystem::create ( std::unique_ptr< MemoryBuffer Buffer,
SourceMgr::DiagHandlerTy  DiagHandler,
StringRef  YAMLFilePath,
void *  DiagContext,
IntrusiveRefCntPtr< FileSystem ExternalFS 
)
static

◆ dir_begin()

directory_iterator RedirectingFileSystem::dir_begin ( const Twine Dir,
std::error_code &  EC 
)
overridevirtual

Get a directory_iterator for Dir.

Note
The 'end' iterator is directory_iterator().

Implements llvm::vfs::FileSystem.

Definition at line 1192 of file VirtualFileSystem.cpp.

References lookupPath(), llvm::not_a_directory, S, and llvm::Twine::toVector().

◆ dump() [1/2]

LLVM_DUMP_METHOD void RedirectingFileSystem::dump ( ) const

Definition at line 1292 of file VirtualFileSystem.cpp.

References llvm::dbgs().

◆ dump() [2/2]

void RedirectingFileSystem::dump ( raw_ostream OS) const

Definition at line 1267 of file VirtualFileSystem.cpp.

References dumpEntry().

◆ dumpEntry()

void RedirectingFileSystem::dumpEntry ( raw_ostream OS,
RedirectingFileSystem::Entry E,
int  NumSpaces = 0 
) const

Definition at line 1272 of file VirtualFileSystem.cpp.

References assert(), llvm::numbers::e, E, EK_Directory, i, and llvm::make_range().

Referenced by dump().

◆ getCurrentWorkingDirectory()

llvm::ErrorOr< std::string > RedirectingFileSystem::getCurrentWorkingDirectory ( ) const
overridevirtual

Get the working directory of this file system.

Implements llvm::vfs::FileSystem.

Definition at line 1134 of file VirtualFileSystem.cpp.

Referenced by makeAbsolute().

◆ getExternalContentsPrefixDir()

StringRef RedirectingFileSystem::getExternalContentsPrefixDir ( ) const

Definition at line 1252 of file VirtualFileSystem.cpp.

◆ getRealPath()

std::error_code RedirectingFileSystem::getRealPath ( const Twine Path,
SmallVectorImpl< char > &  Output 
) const
overridevirtual

Gets real path of Path e.g.

collapse all . and .. patterns, resolve symlinks. For real file system, this uses llvm::sys::fs::real_path. This returns errc::operation_not_permitted if not implemented by subclass.

Reimplemented from llvm::vfs::FileSystem.

Definition at line 2045 of file VirtualFileSystem.cpp.

References llvm::invalid_argument, lookupPath(), P, and llvm::Twine::toVector().

◆ getRoots()

std::vector< StringRef > RedirectingFileSystem::getRoots ( ) const

Definition at line 1260 of file VirtualFileSystem.cpp.

◆ isLocal()

std::error_code RedirectingFileSystem::isLocal ( const Twine Path,
bool &  Result 
)
overridevirtual

Is the file mounted on a local filesystem?

Reimplemented from llvm::vfs::FileSystem.

Definition at line 1152 of file VirtualFileSystem.cpp.

References llvm::Twine::toVector().

◆ lookupPath()

ErrorOr< RedirectingFileSystem::LookupResult > RedirectingFileSystem::lookupPath ( StringRef  Path) const

Looks up Path in Roots and returns a LookupResult giving the matched entry and, if the entry was a FileEntry or DirectoryRemapEntry, the path it redirects to in the external file system.

Definition at line 1883 of file VirtualFileSystem.cpp.

Referenced by dir_begin(), getRealPath(), openFileForRead(), and status().

◆ makeAbsolute()

std::error_code RedirectingFileSystem::makeAbsolute ( SmallVectorImpl< char > &  Path) const
overridevirtual

Make Path an absolute path.

Makes Path absolute using the current directory if it is not already. An empty Path will result in the current directory.

/absolute/path => /absolute/path relative/../path => <current-directory>/relative/../path

Parameters
PathA path that is modified to be an absolute path.
Returns
success if path has been made absolute, otherwise a platform-specific error_code.

Reimplemented from llvm::vfs::FileSystem.

Definition at line 1163 of file VirtualFileSystem.cpp.

References llvm::SmallVectorImpl< T >::assign(), llvm::StringRef::endswith(), llvm::sys::path::get_separator(), getCurrentWorkingDirectory(), llvm::sys::path::is_absolute(), llvm::sys::path::posix, and llvm::sys::path::windows.

Referenced by setCurrentWorkingDirectory().

◆ openFileForRead()

ErrorOr< std::unique_ptr< File > > RedirectingFileSystem::openFileForRead ( const Twine Path)
overridevirtual

Get a File object for the file at Path, if one exists.

Implements llvm::vfs::FileSystem.

Definition at line 2006 of file VirtualFileSystem.cpp.

References getRedirectedFileStatus(), llvm::invalid_argument, lookupPath(), llvm::make_error_code(), move, S, and llvm::Twine::toVector().

◆ setCurrentWorkingDirectory()

std::error_code RedirectingFileSystem::setCurrentWorkingDirectory ( const Twine Path)
overridevirtual

Set the working directory.

This will affect all following operations on this file system and may propagate down for nested file systems.

Implements llvm::vfs::FileSystem.

Definition at line 1139 of file VirtualFileSystem.cpp.

References llvm::vfs::FileSystem::exists(), makeAbsolute(), llvm::no_such_file_or_directory, llvm::SmallString< InternalLen >::str(), and llvm::Twine::toVector().

◆ setExternalContentsPrefixDir()

void RedirectingFileSystem::setExternalContentsPrefixDir ( StringRef  PrefixDir)

Definition at line 1248 of file VirtualFileSystem.cpp.

References llvm::StringRef::str().

◆ setFallthrough()

void RedirectingFileSystem::setFallthrough ( bool  Fallthrough)

Definition at line 1256 of file VirtualFileSystem.cpp.

◆ status()

ErrorOr< Status > RedirectingFileSystem::status ( const Twine Path)
overridevirtual

Get the status of the entry at Path, if one exists.

Implements llvm::vfs::FileSystem.

Definition at line 1960 of file VirtualFileSystem.cpp.

References lookupPath(), S, and llvm::Twine::toVector().

Friends And Related Function Documentation

◆ RedirectingFileSystemParser

friend class RedirectingFileSystemParser
friend

Definition at line 747 of file VirtualFileSystem.h.

◆ RedirectingFSDirIterImpl

friend class RedirectingFSDirIterImpl
friend

Definition at line 746 of file VirtualFileSystem.h.


The documentation for this class was generated from the following files: