summaryrefslogtreecommitdiff
path: root/extra/mariabackup/common.h
blob: 89b189e3a871de465c4594f723d019137ed3b948 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/******************************************************
Copyright (c) 2011-2013 Percona LLC and/or its affiliates.

Common declarations 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA

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

#ifndef XB_COMMON_H
#define XB_COMMON_H

#include <my_global.h>
#include <mysql_version.h>
#include <fcntl.h>
#include <stdarg.h>
#include <my_sys.h>


/** Determine if (i) is a user tablespace id or not. */
# define fil_is_user_tablespace_id(i) (i != 0 \
				       && !srv_is_undo_tablespace(i))

#ifdef _MSC_VER
#define stat _stati64
#define PATH_MAX MAX_PATH
#endif

#ifndef HAVE_VASPRINTF
static inline int vasprintf(char **strp, const char *fmt, va_list args)
{
  int len;
#ifdef _MSC_VER
  len = _vscprintf(fmt, args);
#else
  len = vsnprintf(NULL, 0, fmt, args);
#endif
  if (len < 0)
  {
    return -1;
  }
  *strp = (char *)malloc(len + 1);
  if (!*strp)
  {
    return -1;
  }
  vsprintf(*strp, fmt, args);
  return len;
}

static inline int asprintf(char **strp, const char *fmt,...)
{
  va_list	args;
  va_start(args, fmt);
  int len = vasprintf(strp, fmt, args);
  va_end(args);
  return len;
}
#endif

#define xb_a(expr)							\
	do {								\
		if (!(expr)) {						\
			fprintf(stderr,"Assertion \"%s\" failed at %s:%lu\n",	\
			    #expr, __FILE__, (ulong) __LINE__);		\
			abort();					\
		}							\
	} while (0);

#ifdef XB_DEBUG
#define xb_ad(expr) xb_a(expr)
#else
#define xb_ad(expr)
#endif

#define XB_DELTA_INFO_SUFFIX ".meta"

static inline int msg1(uint thread_num, const char *prefix, const char *fmt, va_list args)
{
  int result;
  time_t t = time(NULL);
  char date[100];
  char *line;
  strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&t));
  result = vasprintf(&line, fmt, args);
  if (result != -1) {
    if (fmt && fmt[strlen(fmt)] != '\n')
      result = fprintf(stderr, "[%02u] %s%s %s\n", thread_num, prefix, date, line);
    else
      result = fprintf(stderr, "[%02u] %s%s %s", thread_num, prefix, date, line);
    free(line);
  }
  return result;
}

static inline  ATTRIBUTE_FORMAT(printf, 2, 3) int msg(unsigned int thread_num, const char *fmt, ...)
{
  int result;
  va_list args;
  va_start(args, fmt);
  result = msg1(thread_num,"", fmt, args);
  va_end(args);
  return result;
}

static inline ATTRIBUTE_FORMAT(printf, 1, 2) int msg(const char *fmt, ...)
{
  int result;
  va_list args;
  va_start(args, fmt);
  result = msg1(0, "", fmt, args);
  va_end(args);
  return result;
}

static inline ATTRIBUTE_FORMAT(printf, 1,2) ATTRIBUTE_NORETURN void die(const char *fmt, ...)
{
  va_list args;
  va_start(args, fmt);
  msg1(0, "FATAL ERROR: ", fmt, args);
  va_end(args);
  fflush(stderr);
  _exit(EXIT_FAILURE);
}


/* Use POSIX_FADV_NORMAL when available */

#ifdef POSIX_FADV_NORMAL
# define USE_POSIX_FADVISE
#else
# define POSIX_FADV_NORMAL
# define POSIX_FADV_SEQUENTIAL
# define POSIX_FADV_DONTNEED
# define posix_fadvise(a,b,c,d) do {} while(0)
#endif

/***********************************************************************
Computes bit shift for a given value. If the argument is not a power
of 2, returns 0.*/
static inline unsigned get_bit_shift(size_t value)
{
    unsigned shift;

    if (value == 0)
	return 0;

    for (shift = 0; !(value & 1); shift++) {
	value >>= 1;
    }
    return (value >> 1) ? 0 : shift;
}

/****************************************************************************
Read 'len' bytes from 'fd'. It is identical to my_read(..., MYF(MY_FULL_IO)),
i.e. tries to combine partial reads into a single block of size 'len', except
that it bails out on EOF or error, and returns the number of successfully read
bytes instead. */
static inline size_t
xb_read_full(File fd, uchar *buf, size_t len)
{
	size_t tlen = 0;
	size_t tbytes;

	while (tlen < len) {
		tbytes = my_read(fd, buf, len - tlen, MYF(MY_WME));
		if (tbytes == 0 || tbytes == MY_FILE_ERROR) {
			break;
		}

		buf += tbytes;
		tlen += tbytes;
	}

	return tlen;
}

#endif