-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathclone_handler.cc
239 lines (189 loc) · 6.83 KB
/
clone_handler.cc
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
/* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file clone_handler.cc
Clone handler implementation
*/
#include "sql/clone_handler.h"
#include <string.h>
#include "my_dbug.h"
#include "my_dir.h"
#include "my_inttypes.h"
#include "my_sys.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/plugin.h"
#include "mysql/plugin_clone.h"
#include "mysql/psi/mysql_file.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysqld_error.h"
#include "sql/mysqld.h"
#include "sql/sql_parse.h"
#include "sql/sql_plugin.h" // plugin_unlock
#include "sql_string.h" // to_lex_cstring
class THD;
/** Clone handler global */
Clone_handler *clone_handle = nullptr;
/** Clone plugin name */
const char *clone_plugin_nm = "clone";
/** Clone handler interface for local clone.
@param[in] thd server thread handle
@param[in] data_dir cloned data directory
@return error code */
int Clone_handler::clone_local(THD *thd, const char *data_dir) {
int error;
char dir_name[FN_REFLEN];
error = validate_dir(data_dir, dir_name);
if (error == 0) {
error = m_plugin_handle->clone_local(thd, dir_name);
}
return error;
}
/** Clone handler interface for remote clone client.
TODO: Create remote connection: wl#9210
@param[in] thd server thread handle
@param[in] data_dir cloned data directory
@return error code */
int Clone_handler::clone_remote_client(THD *thd, const char *data_dir) {
return m_plugin_handle->clone_client(thd, data_dir, 0);
}
/** Clone handler interface for remote clone server.
@param[in] thd server thread handle
@param[in] socket network socket to remote client
@return error code */
int Clone_handler::clone_remote_server(THD *thd, my_socket socket) {
return m_plugin_handle->clone_server(thd, socket);
}
/** Initialize plugin handle
@return error code */
int Clone_handler::init() {
plugin_ref plugin;
plugin = my_plugin_lock_by_name(0, to_lex_cstring(m_plugin_name.c_str()),
MYSQL_CLONE_PLUGIN);
if (plugin == nullptr) {
m_plugin_handle = nullptr;
LogErr(ERROR_LEVEL, ER_CLONE_PLUGIN_NOT_LOADED);
return 1;
}
m_plugin_handle = (Mysql_clone *)plugin_decl(plugin)->info;
plugin_unlock(0, plugin);
return 0;
}
/** Validate clone data directory and convert to os format
@param[in] in_dir user specified clone directory
@param[out] out_dir data directory in native os format
@return error code */
int Clone_handler::validate_dir(const char *in_dir, char *out_dir) {
MY_STAT stat_info;
/* Verify that it is absolute path. */
if (test_if_hard_path(in_dir) == 0) {
my_error(ER_WRONG_VALUE, MYF(0), "path", in_dir);
return ER_WRONG_VALUE;
}
/* Verify that the length is not too long. */
if (strlen(in_dir) >= FN_REFLEN - 1) {
my_error(ER_PATH_LENGTH, MYF(0), "DATA DIRECTORY");
return ER_PATH_LENGTH;
}
/* Convert the path to native os format. */
convert_dirname(out_dir, in_dir, nullptr);
/* Check if the data directory exists already. */
if (mysql_file_stat(key_file_misc, out_dir, &stat_info, MYF(0)) != nullptr) {
my_error(ER_DB_CREATE_EXISTS, MYF(0), in_dir);
return ER_DB_CREATE_EXISTS;
}
/* Check if path is within current data directory */
char tmp_dir[FN_REFLEN];
size_t length;
strncpy(tmp_dir, out_dir, FN_REFLEN);
length = strlen(out_dir);
/* Loop and remove all non-existent directories from the tail */
while (length != 0) {
/* Check if directory exists. */
if (mysql_file_stat(key_file_misc, tmp_dir, &stat_info, MYF(0)) !=
nullptr) {
/* Check if the path is not within data directory. */
if (test_if_data_home_dir(tmp_dir)) {
my_error(ER_PATH_IN_DATADIR, MYF(0), in_dir);
return ER_PATH_IN_DATADIR;
}
break;
}
size_t new_length;
/* Remove the last directory separator from string */
tmp_dir[length - 1] = '\0';
dirname_part(tmp_dir, tmp_dir, &new_length);
/* length must always decrease for the loop to terminate */
if (length <= new_length) {
DBUG_ASSERT(false);
break;
}
length = new_length;
}
return 0;
}
/** Create clone handle to access the clone interfaces from server.
Called when Clone plugin is installed.
@param[in] plugin_name clone plugin name
@return error code */
int clone_handle_create(const char *plugin_name) {
if (clone_handle != nullptr) {
LogErr(ERROR_LEVEL, ER_CLONE_HANDLER_EXISTS);
return 1;
}
clone_handle = new Clone_handler(plugin_name);
if (clone_handle == nullptr) {
LogErr(ERROR_LEVEL, ER_FAILED_TO_CREATE_CLONE_HANDLER);
return 1;
}
return clone_handle->init();
}
/** Drop clone handle. Called when Clone plugin is uninstalled.
@return error code */
int clone_handle_drop() {
if (clone_handle == nullptr) {
return 1;
}
delete clone_handle;
clone_handle = nullptr;
return 0;
}
/** Check if the clone plugin is installed and lock. If the plugin is ready,
return the handler to caller.
@param[in] thd server thread handle
@param[out] plugin plugin reference
@return clone handler on success otherwise NULL */
Clone_handler *clone_plugin_lock(THD *thd, plugin_ref *plugin) {
*plugin = my_plugin_lock_by_name(thd, to_lex_cstring(clone_plugin_nm),
MYSQL_CLONE_PLUGIN);
mysql_mutex_lock(&LOCK_plugin);
/* Return handler only if the plugin is ready. We might successfully
lock the plugin when initialization is progress. */
if (*plugin != nullptr && plugin_state(*plugin) == PLUGIN_IS_READY) {
mysql_mutex_unlock(&LOCK_plugin);
DBUG_ASSERT(clone_handle != nullptr);
return clone_handle;
}
mysql_mutex_unlock(&LOCK_plugin);
return nullptr;
}
/** Unlock the clone plugin.
@param[in] thd server thread handle
@param[out] plugin plugin reference */
void clone_plugin_unlock(THD *thd, plugin_ref plugin) {
plugin_unlock(thd, plugin);
}