forked from bareos/contrib-pgsql-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgsqllib.h
377 lines (333 loc) · 8.8 KB
/
pgsqllib.h
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* Copyright (c) 2013 by Inteos sp. z o.o.
* All rights reserved. See LICENSE.pgsql for details.
*
* Common definitions and utility functions for pgsql plugin.
* Functions defines a common framework used in all utilities and plugins
*/
#ifndef _PGSQLLIB_H_
#define _PGSQLLIB_H_
#include <libpq-fe.h>
#include <sys/types.h>
#include <dirent.h>
#include <bareos.h>
#include "keylist.h"
#include "parseconfig.h"
/* definitions */
/* size of different string or sql buffers */
#define CONNSTRLEN 256
#define BUFLEN 1024
#define SQLLEN 256
#define LOGMSGLEN (6 + 32 + 7 + 24 + 1)
/* Assertions definitions */
#ifndef ASSERT_bfuncs
#define ASSERT_bfuncs \
if ( ! bfuncs ) \
{ \
return bRC_Error; \
}
#endif
#ifndef ASSERT_ctx
#define ASSERT_ctx \
if ( ! ctx ) \
{ \
return bRC_Error; \
}
#endif
#ifndef ASSERT_ctx_p_RET
#define ASSERT_ctx_p_RET \
if ( ! ctx || ! ctx->pContext ) \
{ \
return; \
}
#endif
#ifndef ASSERT_ctx_p
#define ASSERT_ctx_p \
if ( ! ctx || ! ctx->pContext ) \
{ \
return bRC_Error; \
}
#endif
#ifndef ASSERT_ctx_p_RET_NULL
#define ASSERT_ctx_p_RET_NULL \
if ( ! ctx || ! ctx->pContext ) \
{ \
return NULL; \
}
#endif
/* check valid pointer if not simply return */
/* #define ASSERT_NVAL_RET(value) ASSERT_pp(value)
#define ASSERT_pp(value) \ */
#ifndef ASSERT_NVAL_RET
#define ASSERT_NVAL_RET(value) \
if ( ! value ){ \
return; \
}
#endif
/* check valid pointer with Null return */
/* #define ASSERT_NVAL_RET_NULL(value) ASSERT_p(value)
#define ASSERT_p(value) \ */
#ifndef ASSERT_NVAL_RET_NULL
#define ASSERT_NVAL_RET_NULL(value) \
if ( ! value ) \
{ \
return NULL; \
}
#endif
/* check valid pointer with int/err return */
/* #define ASSERT_NVAL_RET_ONE(value) ASSERT_np(value)
#define ASSERT_np(value) \ */
#ifndef ASSERT_NVAL_RET_ONE
#define ASSERT_NVAL_RET_ONE(value) \
if ( ! value ) \
{ \
return 1; \
}
#endif
/* check valid pointer with int/err return */
#ifndef ASSERT_NVAL_RET_NONE
#define ASSERT_NVAL_RET_NONE(value) \
if ( ! value ) \
{ \
return -1; \
}
#endif
/* check valid pointer with bRC return */
/* #define ASSERT_NVAL_RET_BRCERR(value) ASSERT_bp(value)
#define ASSERT_bp(value) \ */
#ifndef ASSERT_NVAL_RET_BRCERR
#define ASSERT_NVAL_RET_BRCERR(value) \
if ( ! value ) \
{ \
return bRC_Error; \
}
#endif
/* check valid pointer if not exit with error */
/* #define ASSERT_NVAL_EXIT_BRCERR(value) ASSERT_bpex(value)
#define ASSERT_bpex(value) \ */
#ifndef ASSERT_NVAL_EXIT_BRCERR
#define ASSERT_NVAL_EXIT_BRCERR(value) \
if ( ! value ){ \
exit ( bRC_Error ); \
}
#endif
/* check error if not exit with error */
#ifndef ASSERT_NVAL_EXIT_ONE
#define ASSERT_NVAL_EXIT_ONE(value) \
if ( ! value ){ \
exit ( 1 ); \
}
#endif
/* check value if not simply return */
/* #define ASSERT_VAL_RET(value) ASSERT_vp(value)
#define ASSERT_vp(value) \ */
#ifndef ASSERT_VAL_RET
#define ASSERT_VAL_RET(value) \
if ( value ){ \
return; \
}
#endif
/* checks error value then Null return */
/* #define ASSERT_VAL_RET_NULL(value) ASSERT_pn(value)
#define ASSERT_pn(value) \ */
#ifndef ASSERT_VAL_RET_NULL
#define ASSERT_VAL_RET_NULL(value) \
if ( value ){ \
return NULL; \
}
#endif
/* checks error value then int/err return */
/* #define ASSERT_VAL_RET_ONE(value) ASSERT_n(value)
#define ASSERT_n(value) \ */
#ifndef ASSERT_VAL_RET_ONE
#define ASSERT_VAL_RET_ONE(value) \
if ( value ) \
{ \
return 1; \
}
#endif
/* checks error value then int/err return */
#ifndef ASSERT_VAL_RET_NONE
#define ASSERT_VAL_RET_NONE(value) \
if ( value ) \
{ \
return -1; \
}
#endif
/* checks error value then bRC return */
/* #define ASSERT_VAL_RET_BRCERR(value) ASSERT_bn(value)
#define ASSERT_bn(value) \ */
#ifndef ASSERT_VAL_RET_BRCERR
#define ASSERT_VAL_RET_BRCERR(value) \
if ( value ) \
{ \
return bRC_Error; \
}
#endif
/* checks error value then exit one */
#ifndef ASSERT_VAL_EXIT_ONE
#define ASSERT_VAL_EXIT_ONE(value) \
if ( value ) \
{ \
exit ( 1 ); \
}
#endif
/* memory allocation/deallocation */
#define MALLOC(size) \
(char *) malloc ( size );
#define FREE(ptr) \
if ( ptr != NULL ){ \
free ( ptr ); \
ptr = NULL; \
}
/* pgsql filetype */
typedef enum {
PG_NONE = 0,
PG_FILE,
PG_LINK,
PG_DIR
} PGSQL_FILETYPE_T;
/* log levels (utils only) */
typedef enum {
LOGERROR,
LOGWARNING,
LOGINFO
} LOG_LEVEL_T;
/* pgsql plugin or utils backup/restore modes */
typedef enum {
PGSQL_NONE = 0,
PGSQL_ARCH_BACKUP,
PGSQL_ARCH_CTL_BACKUP,
PGSQL_DB_BACKUP,
PGSQL_DB_RESTORE,
PGSQL_ARCH_RESTORE
} PGSQL_MODE_T;
/* restore point-in-time */
typedef enum {
PITR_CURRENT,
PITR_TIME,
PITR_XID
} PGSQL_PITR_T;
/* pgsql_status definitions */
typedef enum {
PGSQL_STATUS_UNKNOWN = 0,
PGSQL_STATUS_WAL_ARCH_START = 1,
PGSQL_STATUS_WAL_ARCH_INPROG = 2, /* unimplemented */
PGSQL_STATUS_WAL_ARCH_FINISH = 3,
PGSQL_STATUS_WAL_ARCH_FAILED = 4,
PGSQL_STATUS_WAL_ARCH_MULTI = 5,
PGSQL_STATUS_WAL_BACK_START = 6,
PGSQL_STATUS_WAL_BACK_INPROG = 7, /* unimplemented */
PGSQL_STATUS_WAL_BACK_DONE = 8,
PGSQL_STATUS_WAL_BACK_FAILED = 9, /* unimplemented */
PGSQL_STATUS_DB_ONLINE_START = 10, /* unimplemented */
PGSQL_STATUS_DB_ONLINE_INPROG = 11, /* unimplemented */
PGSQL_STATUS_DB_ONLINE_FINISH = 12, /* unimplemented */
PGSQL_STATUS_DB_ONLINE_FAILED = 13, /* unimplemented */
PGSQL_STATUS_DB_OFFLINE_START = 14, /* unimplemented */
PGSQL_STATUS_DB_OFFLINE_INPROG = 15, /* unimplemented */
PGSQL_STATUS_DB_OFFLINE_FINISH = 16, /* unimplemented */
PGSQL_STATUS_DB_OFFLINE_FAILED = 17 /* unimplemented */
} PGSQL_STATUS_T;
#define PGSQL_STATUS_WAL_OK "3,5,8"
#define PGSQL_STATUS_WAL_ERR "4,9"
#define PGSQL_STATUS_DB_OK "12,16"
#define PGSQL_STATUS_DB_ERR "13,17"
/* pgsqldata for pgsql-arch or pgsql-restore */
typedef struct _pgsqldata pgsqldata;
struct _pgsqldata
{
keylist * paramlist;
int mode;
PGconn * catdb;
char * configfile;
char * walfilename;
char * pathtowalfilename;
int pitr;
char * restorepit;
char * where;
char * restoreclient;
int verbose;
int bsock;
};
/* pgsqlpinst for pgsql-fd instance data */
typedef struct _pgsqlpinst pgsqlpinst;
struct _pgsqlpinst
{
int JobId;
keylist * paramlist;
int mode;
PGconn * catdb;
char * configfile;
keylist * filelist;
keyitem * curfile;
int curfd;
int diropen;
char * linkval;
int linkread;
};
/* uid/gid struct */
typedef struct _pgugid pgugid;
struct _pgugid {
uid_t uid;
gid_t gid;
};
/*
* BAREOS console communication message structure
*/
#define MSGBUFLEN 1024
typedef struct _consmsgbuf consmsgbuf;
struct _consmsgbuf {
int blen;
char mbuf[ MSGBUFLEN ];
};
/*
* IPC MSG Buffer
*/
typedef struct _PGSQL_MSG_BUF_T PGSQL_MSG_BUF_T;
struct _PGSQL_MSG_BUF_T {
long mtype;
char mtext [ MSGBUFLEN ];
};
/*
* date/time verification function structs
*/
typedef struct _pg_time pg_time;
struct _pg_time {
int y;
int m;
int d;
int h;
int mi;
int s;
};
/* utilities functions */
void pgsqllibinit ( int argc, char * argv[] );
char * get_program_name ( void );
char * get_program_directory ( void );
pgsqldata * allocpdata ( void );
pgsqlpinst * allocpinst ( void );
void freepdata ( pgsqldata * pdata );
void freepinst ( pgsqlpinst * pinst );
char * logstr ( char * msg, LOG_LEVEL_T level );
void logprg ( LOG_LEVEL_T level, const char * msg );
void abortprg ( pgsqldata * pdata, int err, const char * msg );
PGconn * catdbconnect ( keylist * paramlist );
keylist * parse_pgsql_conf ( char * configfile );
//bRC pg_internal_conn ( bpContext *ctx, const char * sql );
keylist * get_file_list ( keylist * list, const char * base, const char * path );
int _get_walid_from_catalog ( pgsqldata * pdata );
int _get_walstatus_from_catalog ( pgsqldata * pdata );
int _insert_status_in_catalog ( pgsqldata * pdata, int status );
int _update_status_in_catalog ( pgsqldata * pdata, int pgid, int status );
int _copy_wal_file ( pgsqldata * pdata, char * src, char * dst );
int _check_postgres_is_running ( pgsqldata * pdata, char * pgdataloc );
const char * find_pgctl ( pgsqldata * pdata );
//int readline ( int fd, char * buf, int size );
//int freadline ( FILE * stream, char * buf, int size );
int pgsql_msg_init ( pgsqldata * pdata, int prg );
int pgsql_msg_send ( pgsqldata * pdata, int msqid, uint type, const char * message );
int pgsql_msg_recv ( pgsqldata * pdata, int msqid, int type, char * message );
void pgsql_msg_shutdown ( pgsqldata * pdata, int msqid );
char * format_btime ( const char * str );
#endif /* _PGSQLLIB_H_ */