MySQL 9.3.0
Source Code Documentation
utils_path.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017, 2025, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0,
6 * as published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU General Public License, version 2.0, for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#ifndef MYSQLSHDK_LIBS_UTILS_UTILS_PATH_H_
27#define MYSQLSHDK_LIBS_UTILS_UTILS_PATH_H_
28
29#include <string>
30#include <tuple>
31#include <utility>
32#include <vector>
33// #include "scripting/common.h"
34
35namespace shcore {
36namespace path {
37namespace detail {
38std::string expand_user(const std::string &path, const std::string &sep);
39std::tuple<std::string, std::string> split_extension(const std::string &path,
40 const std::string &sep);
41size_t span_dirname(const std::string &path);
42} // namespace detail
43
44std::string join_path(const std::vector<std::string> &components);
45
46inline std::string join_path(const std::string &a, const std::string &b) {
47 return join_path({a, b});
48}
49
50template <typename... Args>
51std::string join_path(const std::string &a, const std::string &b,
52 Args... args) {
53 return join_path(a, join_path(b, args...));
54}
55
56std::pair<std::string, std::string> splitdrive(const std::string &path);
57
58std::string dirname(const std::string &path);
59std::string basename(const std::string &path);
60
61#ifdef WIN32
62const char path_separator = '\\';
63const char pathlist_separator = ';';
64#else
65const char path_separator = '/';
66const char pathlist_separator = ':';
67#endif
69
70extern const char *k_valid_path_separators;
71
72/**
73 * Get home directory path of the user executing the shell.
74 *
75 * On Unix: `HOME` environment variable is returned if is set, otherwise current
76 * user home directory is looked up in password directory.
77 *
78 * On Windows: `%HOME%` or `%USERPROFILE%` or `%HOMEDRIVE%` + `%HOMEPATH%`
79 * environment variable is returned if is set, otherwise user home
80 * directory is obtained from system KnownFolder ID
81 * `FOLDERID_Profile`.
82 * If all of above fail, empty string is returned.
83 *
84 * @return User home directory path.
85 */
86std::string home();
87
88/**
89 * Get home directory path of user associated with the specified login name.
90 *
91 * On Unix: User home directory path associated with `loginname` is looked up
92 * directly in the password directory.
93 *
94 * On Windows: Retrieving home directory of another user on Windows platform
95 * is *NOT SUPPORTED*.
96 *
97 * @param loginname Login name of existing user in system.
98 *
99 * @return User home directory associated with `loginname` or empty string
100 * if such user does not exist.
101 */
102std::string home(const std::string &loginname);
103
104/**
105 * `expand_user` expand paths beginning with `~` or `~user` (also known as
106 * "tilde expansion").
107 *
108 * Home directory for `~` prefix is obtained by `home()` function.
109 * Home directory for `~user` prefix is obtained by `home(user)` function.
110 *
111 * @param path Path string.
112 *
113 * @return Return `path` with prefix `~` or `~user` replaced by that user's home
114 * directory. @see home() and @see home(user).
115 * If the expansion fails or if the path does not begin with `~`, the
116 * path is returned unchanged.
117 */
118std::string expand_user(const std::string &path);
119
120/**
121 * Unix:
122 * Normalize a path collapsing redundant separators and relative references.
123 * This string path manipulation, might affect paths containing
124 * symbolic links.
125 *
126 * Windows:
127 * Retrieves the full path and file name of the specified file. If error
128 * occur path isn't altered.
129 *
130 * @param path Input path string to normalize.
131 * @return Normalized string path.
132 */
133std::string normalize(const std::string &path);
134
135/**
136 * Split path to (root, extension) tuple such that [root + extenstion == path].
137 *
138 * @param path Original file name
139 * @return Tuple (root, extension). Leading periods on basename are ignored,
140 * i.e. split_extension(".dotfile") returns (".dotfile", "").
141 */
142std::tuple<std::string, std::string> split_extension(const std::string &path);
143
144/**
145 * Returns true if the path exists.
146 */
147bool exists(const std::string &path);
148
149/**
150 * Returns path to the given executable name searched in PATH.
151 */
152std::string search_stdpath(const std::string &name);
153
154/**
155 * Returns path to the given executable name searched in the given path list
156 * string, separated by the given separator.
157 *
158 * @param name name of the executable. .exe is automatically appended in Win32
159 * @param pathlist string with list of paths to search
160 * @param separator separator character used in pathlist. If 0, the default
161 * PATH separator for the platform is used.
162 */
163std::string search_path_list(const std::string &name,
164 const std::string &pathlist,
165 const char separator = 0);
166
167/**
168 * Checks if character is a path separator.
169 *
170 * @param c - character to be checked.
171 *
172 * @returns true if the given character is a path separator.
173 */
174bool is_path_separator(char c);
175
176/**
177 * Checks if path is absolute.
178 *
179 * @param path - path to be checked.
180 *
181 * @returns true if the given path is absolute.
182 */
183bool is_absolute(const std::string &path);
184
185/**
186 * Provides path to the current working directory.
187 */
188std::string getcwd();
189
190} // namespace path
191} // namespace shcore
192
193#endif // MYSQLSHDK_LIBS_UTILS_UTILS_PATH_H_
static const char separator
Definition: item_func.cc:4454
static char * path
Definition: mysqldump.cc:150
Definition: components.cc:43
Definition: fts0fts.cc:238
size_t span_dirname(const std::string &path)
Definition: utils_path.cc:86
std::string expand_user(const std::string &path, const std::string &sep)
Definition: utils_path.cc:40
std::tuple< std::string, std::string > split_extension(const std::string &path, const std::string &sep)
Definition: utils_path.cc:64
std::string home()
Get home directory path of the user executing the shell.
Definition: utils_path_unix.cc:95
std::string normalize(const std::string &path)
Unix: Normalize a path collapsing redundant separators and relative references.
Definition: utils_path_unix.cc:130
std::string search_path_list(const std::string &name, const std::string &pathlist, const char separator)
Returns path to the given executable name searched in the given path list string, separated by the gi...
Definition: utils_path.cc:129
bool is_path_separator(char c)
Checks if character is a path separator.
Definition: utils_path.cc:152
std::tuple< std::string, std::string > split_extension(const std::string &path)
Split path to (root, extension) tuple such that [root + extenstion == path].
Definition: utils_path_unix.cc:124
std::string getcwd()
Provides path to the current working directory.
Definition: utils_path_unix.cc:227
std::pair< std::string, std::string > splitdrive(const std::string &path)
Definition: utils_path_unix.cc:91
std::string basename(const std::string &path)
Definition: utils_path_unix.cc:179
const char pathlist_separator_s[]
Definition: utils_path.h:68
bool exists(const std::string &path)
Returns true if the path exists.
Definition: utils_path_unix.cc:192
std::string expand_user(const std::string &path)
expand_user expand paths beginning with ~ or ~user (also known as "tilde expansion").
Definition: utils_path_unix.cc:119
const char * k_valid_path_separators
Definition: utils_path_unix.cc:44
const char pathlist_separator
Definition: utils_path.h:66
const char path_separator
Definition: utils_path.h:65
std::string search_stdpath(const std::string &name)
Returns path to the given executable name searched in PATH.
Definition: utils_path.cc:121
std::string dirname(const std::string &path)
Definition: utils_path_unix.cc:173
bool is_absolute(const std::string &path)
Checks if path is absolute.
Definition: utils_path_unix.cc:204
std::string join_path(const std::vector< std::string > &components)
Definition: utils_path_unix.cc:57
Definition: file_system_exceptions.h:34