~sergei.glushchenko/percona-xtrabackup/BT31424-2.1-xb-bug1174314

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/******************************************************
Copyright (c) 2011 Percona Ireland Ltd.

Local datasink implementation for XtraBackup.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

*******************************************************/

#include <my_base.h>
#include <mysys_err.h>
#include "common.h"
#include "datasink.h"

typedef struct {
	File fd;
} ds_stdout_file_t;

static ds_ctxt_t *stdout_init(const char *root);
static ds_file_t *stdout_open(ds_ctxt_t *ctxt, const char *path,
			     MY_STAT *mystat);
static int stdout_write(ds_file_t *file, const void *buf, size_t len);
static int stdout_close(ds_file_t *file);
static void stdout_deinit(ds_ctxt_t *ctxt);

datasink_t datasink_stdout = {
	&stdout_init,
	&stdout_open,
	&stdout_write,
	&stdout_close,
	&stdout_deinit
};

static
ds_ctxt_t *
stdout_init(const char *root)
{
	ds_ctxt_t *ctxt;

	ctxt = my_malloc(sizeof(ds_ctxt_t), MYF(MY_FAE));

	ctxt->root = my_strdup(root, MYF(MY_FAE));

	return ctxt;
}

static
ds_file_t *
stdout_open(ds_ctxt_t *ctxt __attribute__((unused)),
	    const char *path __attribute__((unused)),
	    MY_STAT *mystat __attribute__((unused)))
{
	ds_stdout_file_t 	*stdout_file;
	ds_file_t		*file;
	size_t			pathlen;
	const char		*fullpath = "<STDOUT>";

	pathlen = strlen(fullpath) + 1;

	file = (ds_file_t *) my_malloc(sizeof(ds_file_t) +
				       sizeof(ds_stdout_file_t) +
				       pathlen,
				       MYF(MY_FAE));
	stdout_file = (ds_stdout_file_t *) (file + 1);


#ifdef __WIN__
	setmode(fileno(stdout), _O_BINARY);
#endif

	stdout_file->fd = fileno(stdout);

	file->path = (char *) stdout_file + sizeof(ds_stdout_file_t);
	memcpy(file->path, fullpath, pathlen);

	file->ptr = stdout_file;

	return file;
}

static
int
stdout_write(ds_file_t *file, const void *buf, size_t len)
{
	File fd = ((ds_stdout_file_t *) file->ptr)->fd;

	if (!my_write(fd, buf, len, MYF(MY_WME | MY_NABP))) {
		posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
		return 0;
	}

	return 1;
}

static
int
stdout_close(ds_file_t *file)
{
	MY_FREE(file);

	return 1;
}

static
void
stdout_deinit(ds_ctxt_t *ctxt)
{
	MY_FREE(ctxt->root);
	MY_FREE(ctxt);
}