| 1 | /* Copyright (c) 2020, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #ifndef UNIT_TEST_COMMON |
| 25 | #define UNIT_TEST_COMMON |
| 26 | |
| 27 | #include "unit_test_common.h" |
| 28 | #include <string.h> |
| 29 | #ifdef _WIN32 |
| 30 | #include <Windows.h> |
| 31 | #include <direct.h> // getcwd |
| 32 | #else |
| 33 | #include <unistd.h> // getcwd |
| 34 | #endif |
| 35 | |
| 36 | #define FN_REFLEN 2048 |
| 37 | bool make_absolute_urn(const char *input_urn, std::string *out_path) { |
| 38 | char component_dir[FN_REFLEN]; |
| 39 | if (getcwd(component_dir, FN_REFLEN) == nullptr) { |
| 40 | return true; |
| 41 | } |
| 42 | /* Omit scheme prefix to get filename. */ |
| 43 | const char *file = strstr(input_urn, "://"); |
| 44 | if (file == nullptr) { |
| 45 | return true; |
| 46 | } |
| 47 | /* Offset by "://" */ |
| 48 | file += 3; |
| 49 | |
| 50 | /* Compile library path */ |
| 51 | std::string path = component_dir; |
| 52 | path += "/"; |
| 53 | path += file; |
| 54 | |
| 55 | char path_buff[FN_REFLEN + 1]; |
| 56 | strcpy(path_buff, path.c_str()); |
| 57 | |
| 58 | *out_path = "file://"; |
| 59 | *out_path += path_buff; |
| 60 | |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | char *make_str(char *dst, const char *src, size_t length) { |
| 65 | while (length--) |
| 66 | if (!(*dst++ = *src++)) return dst - 1; |
| 67 | *dst = 0; |
| 68 | return dst; |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | Resolve all symbolic links in path |
| 73 | 'to' may be equal to 'filename' |
| 74 | */ |
| 75 | int make_realpath(char *to, const char *filename) { |
| 76 | #ifndef _WIN32 |
| 77 | int result = 0; |
| 78 | |
| 79 | unique_ptr_free<char> ptr(realpath(filename, nullptr)); |
| 80 | if (ptr) { |
| 81 | make_str(to, ptr.get(), FN_REFLEN - 1); |
| 82 | } else { |
| 83 | result = -1; |
| 84 | } |
| 85 | return result; |
| 86 | #else |
| 87 | int ret = GetFullPathName(filename, FN_REFLEN, to, NULL); |
| 88 | if (ret == 0) { |
| 89 | return -1; |
| 90 | } |
| 91 | return 0; |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | size_t make_dirname_length(const char *name) { |
| 96 | const char *pos = name - 1; |
| 97 | const char *gpos = pos++; |
| 98 | for (; *pos; pos++) /* Find last FN_LIBCHAR */ |
| 99 | { |
| 100 | if (directory_separator(*pos)) gpos = pos; |
| 101 | } |
| 102 | return gpos + 1 - name; |
| 103 | } |
| 104 | |
| 105 | char *make_convert_dirname(char *to, const char *from, const char *from_end) { |
| 106 | char *to_org = to; |
| 107 | |
| 108 | /* We use -2 here, becasue we need place for the last FN_LIBCHAR */ |
| 109 | if (!from_end || (from_end - from) > FN_REFLEN - 2) |
| 110 | from_end = from + FN_REFLEN - 2; |
| 111 | |
| 112 | #if FN_LIBCHAR != '/' |
| 113 | { |
| 114 | for (; from < from_end && *from; from++) { |
| 115 | if (*from == '/') |
| 116 | *to++ = FN_LIBCHAR; |
| 117 | else { |
| 118 | *to++ = *from; |
| 119 | } |
| 120 | } |
| 121 | *to = 0; |
| 122 | } |
| 123 | #else |
| 124 | /* This is ok even if to == from, becasue we need to cut the string */ |
| 125 | to = make_str(to, from, (size_t)(from_end - from)); |
| 126 | #endif |
| 127 | |
| 128 | /* Add FN_LIBCHAR to the end of directory path */ |
| 129 | if (to != to_org && (to[-1] != FN_LIBCHAR && to[-1] != FN_DEV_CHAR)) { |
| 130 | *to++ = FN_LIBCHAR; |
| 131 | *to = 0; |
| 132 | } |
| 133 | return to; /* Pointer to end of dir */ |
| 134 | } |
| 135 | |
| 136 | size_t make_dirname_part(char *to, const char *name, size_t *to_res_length) { |
| 137 | size_t length; |
| 138 | length = make_dirname_length(name); |
| 139 | *to_res_length = (size_t)(make_convert_dirname(to, name, name + length) - to); |
| 140 | return length; |
| 141 | } |
| 142 | |
| 143 | /* Set new working directory */ |
| 144 | |
| 145 | int make_setwd(const char *dir) { |
| 146 | int res; |
| 147 | |
| 148 | if (!dir[0] || (dir[0] == FN_LIBCHAR && dir[1] == 0)) dir = FN_ROOTDIR; |
| 149 | if ((res = chdir(dir)) != 0) { |
| 150 | return res; |
| 151 | } |
| 152 | return res; |
| 153 | } /* my_setwd */ |
| 154 | |
| 155 | #endif |