1
/******************************************************
2
Copyright (c) 2011 Percona Inc.
4
Local datasink implementation for XtraBackup.
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; version 2 of the License.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
*******************************************************/
22
#include <mysys_err.h>
30
static ds_ctxt_t *local_init(const char *root);
31
static ds_file_t *local_open(ds_ctxt_t *ctxt, const char *path,
33
static int local_write(ds_file_t *file, const void *buf, size_t len);
34
static int local_close(ds_file_t *file);
35
static void local_deinit(ds_ctxt_t *ctxt);
37
datasink_t datasink_local = {
47
local_init(const char *root)
51
if (my_mkdir(root, 0777, MYF(0)) < 0 && my_errno != EEXIST)
53
my_error(EE_CANT_MKDIR, MYF(ME_BELL | ME_WAITTANG),
58
ctxt = my_malloc(sizeof(ds_ctxt_t), MYF(MY_FAE));
60
ctxt->datasink = &datasink_local;
61
ctxt->root = my_strdup(root, MYF(MY_FAE));
68
local_open(ds_ctxt_t *ctxt, const char *path,
69
MY_STAT *mystat __attribute__((unused)))
71
char fullpath[FN_REFLEN];
72
char dirpath[FN_REFLEN];
75
ds_local_file_t *local_file;
79
fn_format(fullpath, path, ctxt->root, "", MYF(MY_RELATIVE_PATH));
81
/* Create the directory if needed */
82
dirname_part(dirpath, fullpath, &dirpath_len);
83
if (my_mkdir(dirpath, 0777, MYF(0)) < 0 && my_errno != EEXIST) {
84
my_error(EE_CANT_MKDIR, MYF(ME_BELL | ME_WAITTANG),
89
fd = my_create(fullpath, 0, O_WRONLY | O_BINARY | O_EXCL | O_NOFOLLOW,
95
path_len = strlen(fullpath) + 1; /* terminating '\0' */
97
file = (ds_file_t *) my_malloc(sizeof(ds_file_t) +
98
sizeof(ds_local_file_t) +
101
local_file = (ds_local_file_t *) (file + 1);
105
file->path = (char *) local_file + sizeof(ds_local_file_t);
106
memcpy(file->path, fullpath, path_len);
108
file->ptr = local_file;
115
local_write(ds_file_t *file, const void *buf, size_t len)
117
File fd = ((ds_local_file_t *) file->ptr)->fd;
119
if (!my_write(fd, buf, len, MYF(MY_WME | MY_NABP))) {
120
#ifdef USE_POSIX_FADVISE
121
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
131
local_close(ds_file_t *file)
133
File fd = ((ds_local_file_t *) file->ptr)->fd;
137
my_sync(fd, MYF(MY_WME));
139
return my_close(fd, MYF(MY_WME));
144
local_deinit(ds_ctxt_t *ctxt)