1 | /****************************************************** |
2 | Copyright (c) 2013 Percona LLC and/or its affiliates. |
3 | |
4 | Local datasink implementation for XtraBackup. |
5 | |
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. |
9 | |
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. |
14 | |
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., 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 | |
27 | typedef struct { |
28 | File fd; |
29 | } ds_stdout_file_t; |
30 | |
31 | static ds_ctxt_t *stdout_init(const char *root); |
32 | static ds_file_t *stdout_open(ds_ctxt_t *ctxt, const char *path, |
33 | MY_STAT *mystat); |
34 | static int stdout_write(ds_file_t *file, const uchar *buf, size_t len); |
35 | static int stdout_close(ds_file_t *file); |
36 | static void stdout_deinit(ds_ctxt_t *ctxt); |
37 | |
38 | datasink_t datasink_stdout = { |
39 | &stdout_init, |
40 | &stdout_open, |
41 | &stdout_write, |
42 | &stdout_close, |
43 | &stdout_deinit |
44 | }; |
45 | |
46 | static |
47 | ds_ctxt_t * |
48 | stdout_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 | |
59 | static |
60 | ds_file_t * |
61 | stdout_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 | |
93 | static |
94 | int |
95 | stdout_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 | |
107 | static |
108 | int |
109 | stdout_close(ds_file_t *file) |
110 | { |
111 | my_free(file); |
112 | |
113 | return 1; |
114 | } |
115 | |
116 | static |
117 | void |
118 | stdout_deinit(ds_ctxt_t *ctxt) |
119 | { |
120 | my_free(ctxt->root); |
121 | my_free(ctxt); |
122 | } |
123 | |