Skip to content

Commit 04e3bc2

Browse files
Incorporate changes from code review.
1 parent 6a8e0c2 commit 04e3bc2

3 files changed

Lines changed: 68 additions & 49 deletions

File tree

sql/sql_backup.cc

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,7 @@
2121
#include "sql_backup_interface.h"
2222
#include "sql_parse.h"
2323

24-
#ifdef _WIN32
25-
#elif defined __APPLE__
26-
# include <sys/attr.h>
27-
# include <sys/clonefile.h>
28-
# include <copyfile.h>
29-
30-
int copy_entire_file(int src, int dst)
31-
{
32-
return fcopyfile(src, dst, nullptr, COPYFILE_ALL | COPYFILE_CLONE);
33-
}
34-
35-
extern "C" int copy_file(int src, int dst, off_t)
36-
{
37-
return fcopyfile(src, dst, nullptr, COPYFILE_ALL | COPYFILE_CLONE);
38-
}
39-
40-
#else
24+
#if !defined __APPLE__ && !defined _WIN32
4125
using copying_step= ssize_t(int,int,size_t,off_t*);
4226
template<copying_step step>
4327
static ssize_t copy(int in_fd, int out_fd, off_t c) noexcept

sql/sql_backup_interface.h

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,34 @@
1313
along with this program; if not, write to the Free Software
1414
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
1515

16-
#ifndef _WIN32
17-
/* On Windows you have to use CopyFileEx() and friends manually */
18-
# ifdef __cplusplus
16+
#ifdef __APPLE__
17+
# include <sys/attr.h>
18+
# include <sys/clonefile.h>
19+
# include <copyfile.h>
20+
#endif
21+
22+
#ifdef __cplusplus
1923
extern "C"
20-
# endif
24+
{
25+
#endif
26+
27+
#ifdef _WIN32
28+
/* You have to use CopyFileEx() and friends manually */
29+
#elif defined __APPLE__
30+
31+
inline
32+
int copy_entire_file(int src, int dst)
33+
{
34+
return fcopyfile(src, dst, nullptr, COPYFILE_ALL | COPYFILE_CLONE);
35+
}
36+
37+
inline
38+
int copy_file(int src, int dst, off_t)
39+
{
40+
return fcopyfile(src, dst, nullptr, COPYFILE_ALL | COPYFILE_CLONE);
41+
}
42+
43+
#else
2144
/** Copy a file.
2245
@param src source file descriptor
2346
@param dst target to append src to
@@ -26,13 +49,15 @@ extern "C"
2649
@retval 0 on success */
2750
int copy_file(int src, int dst, off_t size);
2851

29-
# ifdef __cplusplus
30-
extern "C"
31-
# endif
52+
3253
/** Copy an entire file.
3354
@param src source file descriptor
3455
@param dst target to append src to
3556
@return error code (negative)
3657
@retval 0 on success */
3758
int copy_entire_file(int src, int dst);
3859
#endif
60+
61+
#ifdef __cplusplus
62+
}
63+
#endif

storage/maria/ma_backup.cc

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ namespace
124124
#ifndef _WIN32
125125
const int datadir_fd;
126126
#endif
127-
static const std::vector<std::string> data_exts;
128-
static const std::string log_file_prefix;
127+
static const LEX_CSTRING data_exts[];
128+
static const LEX_CSTRING log_file_prefix;
129129
using dir_name = std::string;
130130
using dir_contents = std::vector<std::string>;
131131
using database_dir = std::pair<dir_name, dir_contents>;
@@ -258,25 +258,30 @@ namespace
258258

259259
int copy_file(const std::string &path) const noexcept
260260
{
261+
return copy_file(path.c_str());
262+
}
263+
264+
int copy_file(const char*path) const noexcept
265+
{
261266
#ifndef _WIN32
262267
int ret_val = 0;
263-
int src_fd = openat(datadir_fd, path.c_str(), O_RDONLY);
268+
int src_fd = openat(datadir_fd, path, O_RDONLY);
264269
if (src_fd < 0)
265270
{
266-
my_error(ER_CANT_OPEN_FILE, MYF(0), path.c_str(), errno);
271+
my_error(ER_CANT_OPEN_FILE, MYF(0), path, errno);
267272
return 1;
268273
}
269-
int tgt_fd = openat(target.fd, path.c_str(),
274+
int tgt_fd = openat(target.fd, path,
270275
O_CREAT | O_EXCL | O_WRONLY, 0777);
271276
if (tgt_fd < 0)
272277
{
273-
my_error(ER_CANT_CREATE_FILE, MYF(0), path.c_str(), errno);
278+
my_error(ER_CANT_CREATE_FILE, MYF(0), path, errno);
274279
ret_val = 1;
275280
goto finish;
276281
}
277282
if (copy_entire_file(src_fd, tgt_fd) != 0)
278283
{
279-
my_error(ER_ERROR_ON_WRITE, MYF(0), path.c_str(), errno);
284+
my_error(ER_ERROR_ON_WRITE, MYF(0), path, errno);
280285
ret_val = 1;
281286
}
282287
close(tgt_fd);
@@ -298,31 +303,22 @@ namespace
298303
}
299304

300305

301-
static bool is_db_file(const char* file_name) noexcept
302-
{
303-
for (const std::string& ext : data_exts)
304-
{
305-
if (ends_with(file_name, ext))
306-
return true;
307-
}
308-
/* As a stop-gap db/opt files are also copied here, this should be done in SQL layer. */
309-
return !strcmp(file_name, "db.opt");
310-
}
306+
static bool is_db_file(const char* file_name) noexcept;
311307

312-
static bool ends_with(const char* str, const std::string& suffix) noexcept
308+
static bool ends_with(const char* str, const LEX_CSTRING& suffix) noexcept
313309
{
314310
size_t str_len = strlen(str);
315-
size_t suffix_len = suffix.size();
311+
size_t suffix_len = suffix.length;
316312
if (str_len < suffix_len)
317313
return false;
318314
return memcmp(str + str_len - suffix_len,
319-
suffix.data(),
315+
suffix.str,
320316
suffix_len) == 0;
321317
}
322318

323-
static bool begins_with(const char* str, const std::string& prefix) noexcept
319+
static bool begins_with(const char* str, const LEX_CSTRING& prefix) noexcept
324320
{
325-
return strncmp(str, prefix.data(), prefix.size()) == 0;
321+
return strncmp(str, prefix.str, prefix.length) == 0;
326322
}
327323

328324
#ifdef _WIN32
@@ -336,9 +332,23 @@ namespace
336332

337333
/* TODO: .frm failes are not Aria-specific, .MYD and .MYI are MyISAM files;
338334
they are copied here as a stop-gap */
339-
const std::vector<std::string>
340-
Aria_backup::data_exts {".MAD"s, ".MAI"s, "MYD"s, "MYI"s, "frm"s};
341-
const std::string Aria_backup::log_file_prefix {"aria_log."};
335+
const LEX_CSTRING Aria_backup::data_exts[] {{C_STRING_WITH_LEN(".MAD")},
336+
{C_STRING_WITH_LEN(".MAI")},
337+
{C_STRING_WITH_LEN(".MYD")},
338+
{C_STRING_WITH_LEN(".MYI")},
339+
{C_STRING_WITH_LEN(".frm")}};
340+
const LEX_CSTRING Aria_backup::log_file_prefix {C_STRING_WITH_LEN("aria_log.")};
341+
342+
bool Aria_backup::is_db_file(const char* file_name) noexcept
343+
{
344+
for (const LEX_CSTRING& ext : data_exts)
345+
{
346+
if (ends_with(file_name, ext))
347+
return true;
348+
}
349+
/* As a stop-gap db/opt files are also copied here, this should be done in SQL layer. */
350+
return !strcmp(file_name, "db.opt");
351+
}
342352

343353
std::unique_ptr<Aria_backup> aria_backup;
344354
}

0 commit comments

Comments
 (0)