1/******************************************************
2Copyright (c) 2011-2013 Percona LLC and/or its affiliates.
3
4Data sink interface.
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 "common.h"
24#include "datasink.h"
25#include "ds_compress.h"
26#include "ds_archive.h"
27#include "ds_xbstream.h"
28#include "ds_local.h"
29#include "ds_stdout.h"
30#include "ds_tmpfile.h"
31#include "ds_buffer.h"
32
33/************************************************************************
34Create a datasink of the specified type */
35ds_ctxt_t *
36ds_create(const char *root, ds_type_t type)
37{
38 datasink_t *ds;
39 ds_ctxt_t *ctxt;
40
41 switch (type) {
42 case DS_TYPE_STDOUT:
43 ds = &datasink_stdout;
44 break;
45 case DS_TYPE_LOCAL:
46 ds = &datasink_local;
47 break;
48 case DS_TYPE_ARCHIVE:
49#ifdef HAVE_LIBARCHIVE
50 ds = &datasink_archive;
51#else
52 msg("Error : mariabackup was built without libarchive support");
53 exit(EXIT_FAILURE);
54#endif
55 break;
56 case DS_TYPE_XBSTREAM:
57 ds = &datasink_xbstream;
58 break;
59 case DS_TYPE_COMPRESS:
60 ds = &datasink_compress;
61 break;
62 case DS_TYPE_ENCRYPT:
63 case DS_TYPE_DECRYPT:
64 msg("Error : mariabackup does not support encrypted backups.");
65 exit(EXIT_FAILURE);
66 break;
67
68 case DS_TYPE_TMPFILE:
69 ds = &datasink_tmpfile;
70 break;
71 case DS_TYPE_BUFFER:
72 ds = &datasink_buffer;
73 break;
74 default:
75 msg("Unknown datasink type: %d\n", type);
76 xb_ad(0);
77 return NULL;
78 }
79
80 ctxt = ds->init(root);
81 if (ctxt != NULL) {
82 ctxt->datasink = ds;
83 } else {
84 msg("Error: failed to initialize datasink.\n");
85 exit(EXIT_FAILURE);
86 }
87
88 return ctxt;
89}
90
91/************************************************************************
92Open a datasink file */
93ds_file_t *
94ds_open(ds_ctxt_t *ctxt, const char *path, MY_STAT *stat)
95{
96 ds_file_t *file;
97
98 file = ctxt->datasink->open(ctxt, path, stat);
99 if (file != NULL) {
100 file->datasink = ctxt->datasink;
101 }
102
103 return file;
104}
105
106/************************************************************************
107Write to a datasink file.
108@return 0 on success, 1 on error. */
109int
110ds_write(ds_file_t *file, const void *buf, size_t len)
111{
112 return file->datasink->write(file, (const uchar *)buf, len);
113}
114
115/************************************************************************
116Close a datasink file.
117@return 0 on success, 1, on error. */
118int
119ds_close(ds_file_t *file)
120{
121 return file->datasink->close(file);
122}
123
124/************************************************************************
125Destroy a datasink handle */
126void
127ds_destroy(ds_ctxt_t *ctxt)
128{
129 ctxt->datasink->deinit(ctxt);
130}
131
132/************************************************************************
133Set the destination pipe for a datasink (only makes sense for compress and
134tmpfile). */
135void ds_set_pipe(ds_ctxt_t *ctxt, ds_ctxt_t *pipe_ctxt)
136{
137 ctxt->pipe_ctxt = pipe_ctxt;
138}
139