1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
#ifndef MONGOCRYPT_PATH_PRIVATE_H
#define MONGOCRYPT_PATH_PRIVATE_H
#include "./user-check.h"
#include "./str.h"
#include <inttypes.h>
#ifndef _WIN32
#include <unistd.h>
#else
#include "./windows-lean.h"
#endif
typedef enum mpath_format {
/// The POSIX path format
MPATH_POSIX = 'p',
/// The Win32 path format
MPATH_WIN32 = 'w',
/// The native path format for the current platform
MPATH_NATIVE =
#ifdef _WIN32
MPATH_WIN32,
#else
MPATH_POSIX,
#endif
} mpath_format;
/**
* @brief Determine if the given character is a path separator for the given
* path format
*
* @param c A path character
* @param f The path format to use
*/
static inline bool mpath_is_sep(char c, mpath_format f) {
if (f == MPATH_WIN32) {
return c == '/' || c == '\\';
} else {
return c == '/';
}
}
/**
* @brief Obtain the preferred path separator character for the given format
*/
static inline char mpath_preferred_sep(mpath_format f) {
if (f == MPATH_WIN32) {
return '\\';
} else {
return '/';
}
}
/**
* @brief Obtain the path string denoting the application's current working
* directory
*
* @return mstr A new string which must be freed with mstr_free()
*/
static inline mstr mpath_current_path(void) {
#if _WIN32
while (1) {
DWORD len = GetCurrentDirectoryW(0, NULL);
wchar_t *wstr = calloc(sizeof(wchar_t), len);
DWORD got_len = GetCurrentDirectoryW(len, wstr);
if (got_len > len) {
free(wstr);
continue;
}
mstr_narrow_result nar = mstr_win32_narrow(wstr);
free(wstr);
assert(nar.error == 0);
return nar.string;
}
#else
mstr_mut mut = mstr_new(8096);
char *p = getcwd(mut.data, mut.len);
if (p == NULL) {
mstr_free(mut.mstr);
return MSTR_NULL;
}
mstr ret = mstr_copy_cstr(mut.data);
mstr_free(mut.mstr);
return ret;
#endif
}
/**
* @brief Determine whether the given path string has a trailing path separator
*/
static inline bool mpath_has_trailing_sep(mstr_view path, mpath_format f) {
return path.len && mpath_is_sep(path.data[path.len - 1], f);
}
/**
* @brief Obtain the parent path of the given path.
*/
static inline mstr_view mpath_parent(mstr_view path, mpath_format f) {
if (mpath_has_trailing_sep(path, f)) {
// Remove trailing separators:
while (mpath_has_trailing_sep(path, f)) {
path = mstrv_remove_suffix(path, 1);
}
return path;
}
// Remove everything that isn't a path separator:
while (path.len != 0 && !mpath_has_trailing_sep(path, f)) {
path = mstrv_remove_suffix(path, 1);
}
// Remove trailing separators again
while (path.len > 1 && mpath_has_trailing_sep(path, f)) {
path = mstrv_remove_suffix(path, 1);
}
// The result is the parent path.
return path;
}
/**
* @brief Obtain the filename denoted by the given path.
*
* The returned path will include no directory separators. If the given path
* ends with a directory separator, the single-dot '.' path is returned instead.
*/
static inline mstr_view mpath_filename(mstr_view path, mpath_format f) {
if (!path.len) {
return path;
}
const char *it = path.data + path.len;
while (it != path.data && !mpath_is_sep(it[-1], f)) {
--it;
}
size_t off = (size_t)(it - path.data);
mstr_view fname = mstrv_subview(path, off, path.len);
if (fname.len == 0) {
return mstrv_lit(".");
}
return fname;
}
/**
* @brief Join the two given paths into a single path
*
* The two strings will be combined into a single string with a path separator
* between them. If either string is empty, the other string will be copied
* without modification.
*
* @param base The left-hand of the join
* @param suffix The right-hand of the join
* @param f The path format to use
* @return mstr A new string resulting from the join
*/
static inline mstr mpath_join(mstr_view base, mstr_view suffix, mpath_format f) {
if (!base.len) {
return mstr_copy(suffix);
}
if (mpath_has_trailing_sep(base, f)) {
return mstr_append(base, suffix);
}
if (!suffix.len) {
return mstr_copy(base);
}
if (mpath_is_sep(suffix.data[0], f)) {
return mstr_append(base, suffix);
}
// We must insert a path separator between the two strings
assert(base.len <= SIZE_MAX - suffix.len - 1u);
mstr_mut r = mstr_new(base.len + suffix.len + 1);
char *p = r.data;
memcpy(p, base.data, base.len);
p += base.len;
*p++ = mpath_preferred_sep(f);
memcpy(p, suffix.data, suffix.len);
return r.mstr;
}
/**
* @brief Obtain the root name for the given path.
*
* For the Windows format, this will return the drive letter, if present.
* Otherwise, this will return an empty string.
*/
static inline mstr_view mpath_root_name(mstr_view path, mpath_format f) {
if (f == MPATH_WIN32 && path.len > 1) {
char c = path.data[0];
if (path.len > 2 && path.data[1] == ':' && ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) {
return mstrv_subview(path, 0, 2);
}
}
return mstrv_subview(path, 0, 0);
}
/**
* @brief Return the root directory of the given path, if present.
*
* @note This will not include the drive letter of a Win32 path.
*/
static inline mstr_view mpath_root_directory(mstr_view path, mpath_format f) {
mstr_view rname = mpath_root_name(path, f);
path = mstrv_subview(path, rname.len, path.len);
if (path.len && mpath_is_sep(path.data[0], f)) {
return mstrv_subview(path, 0, 1);
} else {
return mstrv_subview(path, 0, 0);
}
}
/**
* @brief Obtain the root filepath of the given path.
*
* This will include both the root name and the root filepath, if present.
*/
static inline mstr_view mpath_root_path(mstr_view path, mpath_format f) {
mstr_view rname = mpath_root_name(path, f);
mstr_view rdir = mpath_root_directory(path, f);
assert(rname.len <= SIZE_MAX - rdir.len);
return mstrv_subview(path, 0, rname.len + rdir.len);
}
/**
* @brief Determine whether the given filepath designates a single unambiguous
* filesystem location.
*
* @note A Win32 filepath without a drive letter is not absolute!
*/
static inline bool mpath_is_absolute(mstr_view path, mpath_format f) {
if (f == MPATH_WIN32) {
// Win32 requires both a root name and a root directory for an absolute
// path
return mpath_root_name(path, f).len && mpath_root_directory(path, f).len;
} else {
// POSIX doesn't have "root names"
return mpath_root_directory(path, f).len;
}
}
/**
* @brief Obtain a relative path from the given filepath
*
* If the path has a root path, returns the content of the path following that
* root path, otherwise returns the same path itself.
*/
static inline mstr_view mpath_relative_path(mstr_view path, mpath_format f) {
mstr_view root = mpath_root_path(path, f);
return mstrv_subview(path, root.len, path.len);
}
/**
* @brief Convert the filepath from one format to a preferred form in another
* format.
*
* @note The return value must be freed with mstr_free()
*/
static inline mstr mpath_to_format(mpath_format from, mstr_view path, mpath_format to) {
mstr_mut ret = mstr_new(path.len);
const char *p = path.data;
char *out = ret.data;
const char *stop = path.data + path.len;
for (; p != stop; ++p, ++out) {
if (mpath_is_sep(*p, from)) {
*out = mpath_preferred_sep(to);
} else {
*out = *p;
}
}
return ret.mstr;
}
/**
* @brief Determine whether the given path is relative (not absolute)
*/
static inline bool mpath_is_relative(mstr_view path, mpath_format f) {
return !mpath_is_absolute(path, f);
}
/**
* @brief Convert the given path to an absolute path, if it is not already.
*
* @note The return value must be freed with mstr_free()
*/
static inline mstr mpath_absolute(mstr_view path, mpath_format f);
/**
* @brief Resolve a path to an absolute path from the given base path.
*
* @note This is not the same as mpath_join(): If the given path is already
* absolute, returns that path unchanged. Otherwise, resolves that path as being
* relative to `base`.
*
* @note If `base` is also a relative path, it will also be given to
* mpath_absolute() to resolve it.
*/
static inline mstr mpath_absolute_from(mstr_view path, mstr_view base, mpath_format f) {
mstr_view rname = mpath_root_name(path, f);
mstr_view rdir = mpath_root_directory(path, f);
if (rname.len) {
if (rdir.len) {
// The path is already fully absolute
return mstr_copy(path);
} else {
mstr abs_base = mpath_absolute(base, f);
mstr_view base_rdir = mpath_root_directory(abs_base.view, f);
mstr_view base_relpath = mpath_relative_path(abs_base.view, f);
mstr_view relpath = mpath_relative_path(path, f);
mstr ret = mstr_copy(rname);
mstr_assign(&ret, mpath_join(ret.view, base_rdir, f));
mstr_assign(&ret, mpath_join(ret.view, base_relpath, f));
mstr_assign(&ret, mpath_join(ret.view, relpath, f));
mstr_free(abs_base);
return ret;
}
} else {
// No root name
if (rdir.len) {
if (f == MPATH_POSIX) {
// No root name, but a root directory on a POSIX path indicates an
// absolute path
return mstr_copy(path);
}
mstr abs_base = mpath_absolute(base, f);
mstr_view base_rname = mpath_root_name(abs_base.view, f);
mstr ret = mpath_join(base_rname, path, f);
mstr_free(abs_base);
return ret;
} else {
mstr abs_base = mpath_absolute(base, f);
mstr r = mpath_join(abs_base.view, path, f);
mstr_free(abs_base);
return r;
}
}
}
static inline mstr mpath_absolute(mstr_view path, mpath_format f) {
if (mpath_is_absolute(path, f)) {
return mstr_copy(path);
}
mstr cur = mpath_current_path();
mstr ret = mpath_absolute_from(path, cur.view, MPATH_NATIVE);
mstr_assign(&ret, mpath_to_format(MPATH_NATIVE, ret.view, f));
mstr_free(cur);
return ret;
}
#endif // MONGOCRYPT_PATH_PRIVATE_H
|