MySQL 8.0.31
Source Code Documentation
Fil_path Class Reference

Wrapper for a path to a directory that may or may not exist. More...

#include <fil0fil.h>

Public Types

enum  path_type { absolute , relative , file_name_only , invalid }
 Various types of file paths. More...
 

Public Member Functions

 Fil_path ()
 Default constructor. More...
 
 Fil_path (const char *path, size_t len, bool normalize_path=false)
 Constructor. More...
 
 Fil_path (const char *path, bool normalize_path=false)
 Constructor. More...
 
 Fil_path (const std::string &path, bool normalize_path=false)
 Constructor. More...
 
 operator const char * () const
 Implicit type conversion. More...
 
const char * operator() () const
 Explicit type conversion. More...
 
const std::string & path () const
 
size_t len () const
 
const std::string abs_path () const
 Return the absolute path by value. More...
 
size_t abs_len () const
 
bool operator== (const Fil_path &other) const
 Determine if this path is equal to the other path. More...
 
bool is_same_as (const Fil_path &other) const
 Check if m_path is the same as this other path. More...
 
bool is_same_as (const std::string &other) const
 Check if this path is the same as the other path. More...
 
bool is_ancestor (const Fil_path &other) const
 Check if m_path is the parent of the other path. More...
 
bool is_ancestor (const std::string &other) const
 Check if this Fil_path is an ancestor of the other path. More...
 
bool is_file_and_exists () const
 
bool is_directory_and_exists () const
 
bool is_valid () const
 This validation is only for ':'. More...
 
bool is_circular () const
 Determine if m_path contains a circular section like "/anydir/../" Fil_path::normalize() must be run before this. More...
 
bool is_absolute_path () const
 

Static Public Member Functions

static bool is_same_as (const std::string &first, const std::string &second)
 Check if two path strings are equal. More...
 
static std::pair< std::string, std::string > split (const std::string &path)
 Splits the path into directory and file name parts. More...
 
static bool is_ancestor (const std::string &first, const std::string &second)
 Check if the first path is an ancestor of the second. More...
 
static bool is_hidden (std::string path)
 Determine if the file or directory is considered HIDDEN. More...
 
static std::string remove_quotes (const char *pathspec)
 Remove quotes e.g., 'a;b' or "a;b" -> a;b. More...
 
static bool is_relative_path (const std::string &path)
 Determine if a path is a relative path or not. More...
 
static bool is_absolute_path (const std::string &path)
 Determine if a path is an absolute path or not. More...
 
static path_type type_of_path (const std::string &path)
 Determine what type of path is provided. More...
 
static bool has_prefix (const std::string &path, const std::string prefix)
 
static void normalize (std::string &path)
 Normalize a directory path for the current OS: On Windows, we convert '/' to '\', else we convert '\' to '/'. More...
 
static void normalize (char *path)
 Normalize a directory path for the current OS: On Windows, we convert '/' to '\', else we convert '\' to '/'. More...
 
static void to_lower (std::string &path)
 Convert a path string to lower case using the CHARSET my_charset_filename. More...
 
static os_file_type_t get_file_type (const std::string &path)
 
static const char * get_file_type_string (const std::string &path)
 Return a string to display the file type of a path. More...
 
static const char * get_file_type_string (os_file_type_t type)
 Return a string to display the file type of a path. More...
 
static std::string get_real_path (const std::string &path, bool force=true)
 Get the real path for a directory or a file name. More...
 
static std::string get_basename (const std::string &filepath)
 Get the basename of the file path. More...
 
static std::string get_existing_path (const std::string &path, std::string &ghost)
 Separate the portion of a directory path that exists and the portion that does not exist. More...
 
static bool is_undo_tablespace_name (const std::string &name)
 Check if the name is an undo tablespace name. More...
 
static bool has_suffix (ib_file_suffix sfx, const std::string &path)
 Check if the file has the the specified suffix. More...
 
static bool truncate_suffix (ib_file_suffix sfx, std::string &path)
 Check if the file has the the specified suffix and truncate. More...
 
static bool is_separator (char c)
 Check if a character is a path separator ('\' or '/') More...
 
static void trim_separator (std::string &path)
 If the last character of a directory path is a separator ('\' or '/') trim it off the string. More...
 
static void append_separator (std::string &path)
 If the last character of a directory path is NOT a separator, append a separator to the path. More...
 
static char * make (const std::string &path_in, const std::string &name_in, ib_file_suffix ext, bool trim=false)
 Allocate and build a file name from a path, a table or tablespace name and a suffix. More...
 
static char * make_cfg (const std::string &path_in)
 Allocate and build a CFG file name from a path. More...
 
static char * make_cfp (const std::string &path_in)
 Allocate and build a CFP file name from a path. More...
 
static char * make_ibd (const std::string &path_in, const std::string &name_in)
 Allocate and build a file name from a path, a table or tablespace name and a suffix. More...
 
static char * make_ibd_from_table_name (const std::string &name_in)
 Allocate and build a file name from a path, a table or tablespace name and a suffix. More...
 
static std::string make_new_path (const std::string &path_in, const std::string &name_in, ib_file_suffix extn)
 Create an IBD path name after replacing the basename in an old path with a new basename. More...
 
static bool parse_file_path (const std::string &file_path, ib_file_suffix extn, std::string &dict_name)
 Parse file-per-table file name and build Innodb dictionary table name. More...
 
static void make_data_dir_path (char *data_dir_path)
 This function reduces a null-terminated full remote path name into the path that is sent by MySQL for DATA DIRECTORY clause. More...
 
static bool is_valid_location (const char *space_name, space_id_t space_id, uint32_t fsp_flags, const std::string &path)
 Check if the filepath provided is in a valid placement. More...
 
static bool is_valid_location_within_db (const char *space_name, const std::string &path)
 Check if the implicit filepath is immediately within a dir named for the schema. More...
 
static void convert_to_filename_charset (std::string &name)
 Convert filename to the file system charset format. More...
 
static void convert_to_lower_case (std::string &path)
 Convert to lower case using the file system charset. More...
 

Static Public Attributes

static constexpr auto DB_SEPARATOR = '/'
 schema '/' table separator More...
 
static constexpr auto OS_SEPARATOR = OS_PATH_SEPARATOR
 OS specific path separator. More...
 
static constexpr auto SEPARATOR = "\\/"
 Directory separators that are supported. More...
 
static constexpr auto DOT_SLASH = "./"
 
static constexpr auto DOT_DOT_SLASH = "../"
 
static constexpr auto SLASH_DOT_DOT_SLASH = "/../"
 

Protected Attributes

std::string m_path
 Path to a file or directory. More...
 
std::string m_abs_path
 A full absolute path to the same file. More...
 

Detailed Description

Wrapper for a path to a directory that may or may not exist.

Member Enumeration Documentation

◆ path_type

Various types of file paths.

Enumerator
absolute 
relative 
file_name_only 
invalid 

Constructor & Destructor Documentation

◆ Fil_path() [1/4]

Fil_path::Fil_path ( )

Default constructor.

Defaults to MySQL_datadir_path.

◆ Fil_path() [2/4]

Fil_path::Fil_path ( const char *  path,
size_t  len,
bool  normalize_path = false 
)
explicit

Constructor.

Parameters
[in]pathPath, not necessarily NUL terminated
[in]lenLength of path
[in]normalize_pathIf false, it's the callers responsibility to ensure that the path is normalized.

◆ Fil_path() [3/4]

Fil_path::Fil_path ( const char *  path,
bool  normalize_path = false 
)
explicit

Constructor.

Parameters
[in]pathPath, not necessarily NUL terminated
[in]normalize_pathIf false, it's the callers responsibility to ensure that the path is normalized.

◆ Fil_path() [4/4]

Fil_path::Fil_path ( const std::string &  path,
bool  normalize_path = false 
)
explicit

Constructor.

Parameters
[in]pathpathname (may also include the file basename)
[in]normalize_pathIf false, it's the callers responsibility to ensure that the path is normalized.

Member Function Documentation

◆ abs_len()

size_t Fil_path::abs_len ( ) const
inline
Returns
the length of m_abs_path

◆ abs_path()

const std::string Fil_path::abs_path ( ) const
inline

Return the absolute path by value.

If m_abs_path is null, calculate it and return it by value without trying to reset this const object. m_abs_path can be empty if the path did not exist when this object was constructed.

Returns
the absolute path by value.

◆ append_separator()

static void Fil_path::append_separator ( std::string &  path)
inlinestatic

If the last character of a directory path is NOT a separator, append a separator to the path.

NOTE: We leave it up to the caller to assure that the path is a directory and not a file since if that directory does not yet exist, this function cannot tell the difference.

Parameters
[in]pathfile system path

◆ convert_to_filename_charset()

void Fil_path::convert_to_filename_charset ( std::string &  name)
static

Convert filename to the file system charset format.

Parameters
[in,out]nameFilename to convert

◆ convert_to_lower_case()

void Fil_path::convert_to_lower_case ( std::string &  path)
static

Convert to lower case using the file system charset.

Parameters
[in,out]pathFilepath to convert

◆ get_basename()

std::string Fil_path::get_basename ( const std::string &  filepath)
static

Get the basename of the file path.

This is the file name without any directory separators. In other words, the file name after the last separator.

Parameters
[in]filepathThe name of a file, optionally with a path.

◆ get_existing_path()

std::string Fil_path::get_existing_path ( const std::string &  path,
std::string &  ghost 
)
static

Separate the portion of a directory path that exists and the portion that does not exist.

Parameters
[in]pathPath to evaluate
[in,out]ghostThe portion of the path that does not exist.
Returns
the existing portion of a path.

◆ get_file_type()

os_file_type_t Fil_path::get_file_type ( const std::string &  path)
static
Returns
true if the path exists and is a file .

◆ get_file_type_string() [1/2]

const char * Fil_path::get_file_type_string ( const std::string &  path)
static

Return a string to display the file type of a path.

Parameters
[in]pathpath name
Returns
true if the path exists and is a file .

◆ get_file_type_string() [2/2]

const char * Fil_path::get_file_type_string ( os_file_type_t  type)
static

Return a string to display the file type of a path.

Parameters
[in]typeOS file type
Returns
true if the path exists and is a file .

◆ get_real_path()

std::string Fil_path::get_real_path ( const std::string &  path,
bool  force = true 
)
static

Get the real path for a directory or a file name.

This path can be used to compare with another absolute path. It will be converted to lower case on case insensitive file systems and if it is a directory, it will end with a directory separator. The call to my_realpath() may fail on non-Windows platforms if the path does not exist. If so, the parameter 'force' determines what to return.

Parameters
[in]pathdirectory or filename to convert to a real path
[in]forceif true and my_realpath() fails, use the path provided. if false and my_realpath() fails, return a null string.
Returns
the absolute path prepared for making comparisons with other real paths.

◆ has_prefix()

static bool Fil_path::has_prefix ( const std::string &  path,
const std::string  prefix 
)
inlinestatic

◆ has_suffix()

static bool Fil_path::has_suffix ( ib_file_suffix  sfx,
const std::string &  path 
)
inlinestatic

Check if the file has the the specified suffix.

Parameters
[in]sfxsuffix to look for
[in]pathFilename to check
Returns
true if it has the the ".ibd" suffix.

◆ is_absolute_path() [1/2]

bool Fil_path::is_absolute_path ( ) const
inline
Returns
true if the path is an absolute path.

◆ is_absolute_path() [2/2]

static bool Fil_path::is_absolute_path ( const std::string &  path)
inlinestatic

Determine if a path is an absolute path or not.

Parameters
[in]pathOS directory or file path to evaluate
Return values
trueif the path is absolute
falseif the path is relative or file_name_only

◆ is_ancestor() [1/3]

bool Fil_path::is_ancestor ( const Fil_path other) const

Check if m_path is the parent of the other path.

Parameters
[in]otherpath to compare to
Returns
true if m_path is an ancestor of name

◆ is_ancestor() [2/3]

static bool Fil_path::is_ancestor ( const std::string &  first,
const std::string &  second 
)
inlinestatic

Check if the first path is an ancestor of the second.

Do not assume that these paths have been converted to real paths and are ready to compare. If the two paths are the same we will return false.

Parameters
[in]firstParent path to check
[in]secondDescendent path to check
Returns
true if the first path is an ancestor of the second

◆ is_ancestor() [3/3]

bool Fil_path::is_ancestor ( const std::string &  other) const

Check if this Fil_path is an ancestor of the other path.

Parameters
[in]otherpath to compare to
Returns
true if this Fil_path is an ancestor of the other path

◆ is_circular()

bool Fil_path::is_circular ( ) const

Determine if m_path contains a circular section like "/anydir/../" Fil_path::normalize() must be run before this.

Returns
true if a circular section if found, false if not

◆ is_directory_and_exists()

bool Fil_path::is_directory_and_exists ( ) const
Returns
true if m_path exists and is a directory.
true if the path exists and is a directory.

◆ is_file_and_exists()

bool Fil_path::is_file_and_exists ( ) const
Returns
true if m_path exists and is a file.
true if the path exists and is a file .

◆ is_hidden()

bool Fil_path::is_hidden ( std::string  path)
static

Determine if the file or directory is considered HIDDEN.

Most file systems identify the HIDDEN attribute by a '.' preceding the basename. On Windows, a HIDDEN path is identified by a file attribute. We will use the preceding '.' to indicate a HIDDEN attribute on ALL file systems so that InnoDB tablespaces and their directory structure remain portable.

Parameters
[in]pathThe full or relative path of a file or directory.
Returns
true if the directory or path is HIDDEN.

◆ is_relative_path()

static bool Fil_path::is_relative_path ( const std::string &  path)
inlinestatic

Determine if a path is a relative path or not.

Parameters
[in]pathOS directory or file path to evaluate
Return values
trueif the path is relative
falseif the path is absolute or file_name_only

◆ is_same_as() [1/3]

bool Fil_path::is_same_as ( const Fil_path other) const

Check if m_path is the same as this other path.

Parameters
[in]otherdirectory path to compare to
Returns
true if m_path is the same as path

◆ is_same_as() [2/3]

static bool Fil_path::is_same_as ( const std::string &  first,
const std::string &  second 
)
inlinestatic

Check if two path strings are equal.

Put them into Fil_path objects so that they can be compared correctly.

Parameters
[in]firstfirst path to check
[in]secondsocond path to check
Returns
true if these two paths are the same

◆ is_same_as() [3/3]

bool Fil_path::is_same_as ( const std::string &  other) const

Check if this path is the same as the other path.

Parameters
[in]otherdirectory path to compare to
Returns
true if this path is the same as the other path

◆ is_separator()

static bool Fil_path::is_separator ( char  c)
inlinestatic

Check if a character is a path separator ('\' or '/')

Parameters
[in]cCharacter to check
Returns
true if it is a separator

◆ is_undo_tablespace_name()

bool Fil_path::is_undo_tablespace_name ( const std::string &  name)
static

Check if the name is an undo tablespace name.

Check if the basename of a filepath is an undo tablespace name.

Parameters
[in]nameTablespace name
Returns
true if it is an undo tablespace name

◆ is_valid()

bool Fil_path::is_valid ( void  ) const

This validation is only for ':'.

Returns
true if the path is valid.

◆ is_valid_location()

bool Fil_path::is_valid_location ( const char *  space_name,
space_id_t  space_id,
uint32_t  fsp_flags,
const std::string &  path 
)
static

Check if the filepath provided is in a valid placement.

This routine is run during file discovery at startup. 1) File-per-table must be in a dir named for the schema. 2) File-per-table must not be in the datadir. 3) General tablespace must not be under the datadir.

Parameters
[in]space_nametablespace name
[in]space_idtablespace ID
[in]fsp_flagstablespace flags
[in]pathscanned realpath to an existing file to validate
Return values
trueif the filepath is a valid datafile location

◆ is_valid_location_within_db()

bool Fil_path::is_valid_location_within_db ( const char *  space_name,
const std::string &  path 
)
static

Check if the implicit filepath is immediately within a dir named for the schema.

Parameters
[in]space_nametablespace name
[in]pathscanned realpath to an existing file to validate
Return values
trueif the filepath is valid

◆ len()

size_t Fil_path::len ( ) const
inline
Returns
the length of m_path

◆ make()

char * Fil_path::make ( const std::string &  path_in,
const std::string &  name_in,
ib_file_suffix  ext,
bool  trim = false 
)
static

Allocate and build a file name from a path, a table or tablespace name and a suffix.

Parameters
[in]path_innullptr or the directory path or the full path and filename
[in]name_innullptr if path is full, or Table/Tablespace name
[in]extthe file extension to use
[in]trimwhether last name on the path should be trimmed
Returns
own: file name; must be freed by ut::free()

◆ make_cfg()

static char * Fil_path::make_cfg ( const std::string &  path_in)
inlinestatic

Allocate and build a CFG file name from a path.

Parameters
[in]path_inFull path to the filename
Returns
own: file name; must be freed by ut::free()

◆ make_cfp()

static char * Fil_path::make_cfp ( const std::string &  path_in)
inlinestatic

Allocate and build a CFP file name from a path.

Parameters
[in]path_inFull path to the filename
Returns
own: file name; must be freed by ut::free()

◆ make_data_dir_path()

void Fil_path::make_data_dir_path ( char *  data_dir_path)
static

This function reduces a null-terminated full remote path name into the path that is sent by MySQL for DATA DIRECTORY clause.

It replaces the 'databasename/tablename.ibd' found at the end of the path with just 'tablename'.

Since the result is always smaller than the path sent in, no new memory is allocated. The caller should allocate memory for the path sent in. This function manipulates that path in place. If the path format is not as expected, set data_dir_path to "" and return.

The result is used to inform a SHOW CREATE TABLE command.

Parameters
[in,out]data_dir_pathFull path/data_dir_path

◆ make_ibd()

static char * Fil_path::make_ibd ( const std::string &  path_in,
const std::string &  name_in 
)
inlinestatic

Allocate and build a file name from a path, a table or tablespace name and a suffix.

Parameters
[in]path_innullptr or the directory path or the full path and filename
[in]name_innullptr if path is full, or Table/Tablespace name
Returns
own: file name; must be freed by ut::free()

◆ make_ibd_from_table_name()

static char * Fil_path::make_ibd_from_table_name ( const std::string &  name_in)
inlinestatic

Allocate and build a file name from a path, a table or tablespace name and a suffix.

Parameters
[in]name_inTable/Tablespace name
Returns
own: file name; must be freed by ut::free()

◆ make_new_path()

std::string Fil_path::make_new_path ( const std::string &  path_in,
const std::string &  name_in,
ib_file_suffix  extn 
)
static

Create an IBD path name after replacing the basename in an old path with a new basename.

The old_path is a full path name including the extension. The tablename is in the normal form "schema/tablename".

Parameters
[in]path_inPathname
[in]name_inContains new base name
[in]extnFile extension
Returns
new full pathname

◆ normalize() [1/2]

static void Fil_path::normalize ( char *  path)
inlinestatic

Normalize a directory path for the current OS: On Windows, we convert '/' to '\', else we convert '\' to '/'.

Parameters
[in,out]pathA NUL terminated path

◆ normalize() [2/2]

static void Fil_path::normalize ( std::string &  path)
inlinestatic

Normalize a directory path for the current OS: On Windows, we convert '/' to '\', else we convert '\' to '/'.

Parameters
[in,out]pathDirectory and file path

◆ operator const char *()

Fil_path::operator const char * ( ) const
inline

Implicit type conversion.

Returns
pointer to m_path.c_str()

◆ operator()()

const char * Fil_path::operator() ( void  ) const
inline

Explicit type conversion.

Returns
pointer to m_path.c_str()

◆ operator==()

bool Fil_path::operator== ( const Fil_path other) const
inline

Determine if this path is equal to the other path.

Parameters
[in]otherpath to compare to
Returns
true if the paths are the same

◆ parse_file_path()

bool Fil_path::parse_file_path ( const std::string &  file_path,
ib_file_suffix  extn,
std::string &  dict_name 
)
static

Parse file-per-table file name and build Innodb dictionary table name.

Parameters
[in]file_pathFile name with complete path
[in]extnFile extension
[out]dict_nameInnodb dictionary table name
Returns
true, if successful.

◆ path()

const std::string & Fil_path::path ( ) const
inline
Returns
the value of m_path

◆ remove_quotes()

static std::string Fil_path::remove_quotes ( const char *  pathspec)
inlinestatic

Remove quotes e.g., 'a;b' or "a;b" -> a;b.

This will only remove the quotes if they are matching on the whole string. This will not work if each delimited string is quoted since this is called before the string is parsed.

Returns
pathspec with the quotes stripped

◆ split()

std::pair< std::string, std::string > Fil_path::split ( const std::string &  path)
static

Splits the path into directory and file name parts.

Parameters
[in]pathpath to split
Returns
[directory, file] for the path

◆ to_lower()

static void Fil_path::to_lower ( std::string &  path)
inlinestatic

Convert a path string to lower case using the CHARSET my_charset_filename.

Parameters
[in,out]pathDirectory and file path

◆ trim_separator()

static void Fil_path::trim_separator ( std::string &  path)
inlinestatic

If the last character of a directory path is a separator ('\' or '/') trim it off the string.

Parameters
[in]pathfile system path

◆ truncate_suffix()

static bool Fil_path::truncate_suffix ( ib_file_suffix  sfx,
std::string &  path 
)
inlinestatic

Check if the file has the the specified suffix and truncate.

Parameters
[in]sfxsuffix to look for
[in,out]pathFilename to check
Returns
true if the suffix is found and truncated.

◆ type_of_path()

static path_type Fil_path::type_of_path ( const std::string &  path)
inlinestatic

Determine what type of path is provided.

Parameters
[in]pathOS directory or file path to evaluate
Returns
the type of filepath; 'absolute', 'relative', 'file_name_only', or 'invalid' if the path is empty.

Member Data Documentation

◆ DB_SEPARATOR

constexpr char Fil_path::DB_SEPARATOR = '/'
staticconstexpr

schema '/' table separator

◆ DOT_DOT_SLASH

constexpr const char * Fil_path::DOT_DOT_SLASH = "../"
staticconstexpr

◆ DOT_SLASH

constexpr const char * Fil_path::DOT_SLASH = "./"
staticconstexpr

◆ m_abs_path

std::string Fil_path::m_abs_path
protected

A full absolute path to the same file.

◆ m_path

std::string Fil_path::m_path
protected

Path to a file or directory.

◆ OS_SEPARATOR

constexpr char Fil_path::OS_SEPARATOR = OS_PATH_SEPARATOR
staticconstexpr

OS specific path separator.

◆ SEPARATOR

constexpr const char * Fil_path::SEPARATOR = "\\/"
staticconstexpr

Directory separators that are supported.

◆ SLASH_DOT_DOT_SLASH

constexpr const char * Fil_path::SLASH_DOT_DOT_SLASH = "/../"
staticconstexpr

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