1/******************************************************
2Copyright (c) 2013 Percona LLC and/or its affiliates.
3
4Local datasink implementation for XtraBackup.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; version 2 of the License.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18
19*******************************************************/
20
21#include <my_global.h>
22#include <my_base.h>
23#include <mysys_err.h>
24#include "common.h"
25#include "datasink.h"
26
27typedef struct {
28 File fd;
29} ds_stdout_file_t;
30
31static ds_ctxt_t *stdout_init(const char *root);
32static ds_file_t *stdout_open(ds_ctxt_t *ctxt, const char *path,
33 MY_STAT *mystat);
34static int stdout_write(ds_file_t *file, const uchar *buf, size_t len);
35static int stdout_close(ds_file_t *file);
36static void stdout_deinit(ds_ctxt_t *ctxt);
37
38datasink_t datasink_stdout = {
39 &stdout_init,
40 &stdout_open,
41 &stdout_write,
42 &stdout_close,
43 &stdout_deinit
44};
45
46static
47ds_ctxt_t *
48stdout_init(const char *root)
49{
50 ds_ctxt_t *ctxt;
51
52 ctxt = my_malloc(sizeof(ds_ctxt_t), MYF(MY_FAE));
53
54 ctxt->root = my_strdup(root, MYF(MY_FAE));
55
56 return ctxt;
57}
58
59static
60ds_file_t *
61stdout_open(ds_ctxt_t *ctxt __attribute__((unused)),
62 const char *path __attribute__((unused)),
63 MY_STAT *mystat __attribute__((unused)))
64{
65 ds_stdout_file_t *stdout_file;
66 ds_file_t *file;
67 size_t pathlen;
68 const char *fullpath = "<STDOUT>";
69
70 pathlen = strlen(fullpath) + 1;
71
72 file = (ds_file_t *) my_malloc(sizeof(ds_file_t) +
73 sizeof(ds_stdout_file_t) +
74 pathlen,
75 MYF(MY_FAE));
76 stdout_file = (ds_stdout_file_t *) (file + 1);
77
78
79#ifdef __WIN__
80 setmode(fileno(stdout), _O_BINARY);
81#endif
82
83 stdout_file->fd = my_fileno(stdout);
84
85 file->path = (char *) stdout_file + sizeof(ds_stdout_file_t);
86 memcpy(file->path, fullpath, pathlen);
87
88 file->ptr = stdout_file;
89
90 return file;
91}
92
93static
94int
95stdout_write(ds_file_t *file, const uchar *buf, size_t len)
96{
97 File fd = ((ds_stdout_file_t *) file->ptr)->fd;
98
99 if (!my_write(fd, buf, len, MYF(MY_WME | MY_NABP))) {
100 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
101 return 0;
102 }
103
104 return 1;
105}
106
107static
108int
109stdout_close(ds_file_t *file)
110{
111 my_free(file);
112
113 return 1;
114}
115
116static
117void
118stdout_deinit(ds_ctxt_t *ctxt)
119{
120 my_free(ctxt->root);
121 my_free(ctxt);
122}
123