-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlibobk.c
553 lines (460 loc) · 12.2 KB
/
libobk.c
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
* ORACLE RMAN SBT API
*
* WARNING:
* All interfaces are based on reverse engineering of
* RMAN SBT (System Backup to Tape). Since the SBT API
* is a big secret of Oracle, you have no way to obtain
* it by free, so you use this at your own risk.
*
* Authors:
* Sunding Wei: [email protected]
* Zhongkai He: [email protected]
*
* Dingding Technology, China
* 2017.3.13
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#define SBTERROR_FATAL 7501 // fatal error, IO error etc
#define SBTERROR_NOTFOUND 7502 // file is not found
#define SBTERROR_EXIST 7503 // file already exists in catalog
#define SBTERROR_EOF 7504 // end of file
#define SBTERROR_NOTFOUND2 7090 // no such file
#define SBTERROR_7505 7505 // could not proxy backup file
#define SBTERROR_7012 7012 // file is already open
#define SBTERROR_NOTOPEN 7020 // no file is open
#define SBTERROR_CLOSE 7023 // error closing backup file
#define LOG(fmt, ...) \
do {\
if (ctx->fp) \
fprintf(ctx->fp, "%s: " fmt, __FUNCTION__, __VA_ARGS__); } while (0)
struct sbtinfo {
int type;
void * value;
};
struct sbtopt {
int type;
void * value;
};
struct sbtdata {
int status;
int error;
};
struct sbtapi
{
void * sbtbackup;
void * sbtclose2;
void * sbtcommand;
void * sbtend;
void * sbterror;
void * sbtinfo2;
void * sbtinit2;
void * sbtread2;
void * sbtremove2;
void * sbtrestore;
void * sbtwrite2;
// Proxy copy
void * sbtpcbackup;
void * sbtpccancel;
void * sbtpccommit;
void * sbtpcend;
void * sbtpcquerybackup;
void * sbtpcqueryrestore;
void * sbtpcrestore;
void * sbtpcstart;
void * sbtpcstatus;
void * sbtpcvalidate;
};
struct global_data_area {
struct sbtopt options[10];
struct sbtapi api;
int api_version;
int mm_version;
int context_size;
int proxy_limit;
int proxy_copy_support;
int maxsize;
};
struct sbtctx
{
int fd;
int error;
int block_size;
int pid;
int tid;
char * file;
char path[256];
// Logger
FILE * fp;
// Related sbtinfo for backup or restore files
struct sbtinfo * pairs;
};
struct global_data_area global = {};
int sbtopen(void)
{
return (-1);
}
int sbtread(void)
{
return (-1);
}
int sbtwrite(void)
{
return (-1);
}
int sbtclose(void)
{
return (-1);
}
int sbtinfo(void)
{
return (-1);
}
int sbtremove(void)
{
return (-1);
}
#define SBTOPT_END 1
#define SBTOPT_2 2
#define SBTOPT_MAXSIZE 3
#define SBTOPT_API_VERSION 4
#define SBTOPT_VENDOR 5
#define SBTOPT_API 6
#define SBTOPT_CONTEXT_SIZE 7
#define SBTOPT_MM_VERSION 8
#define SBTOPT_PROXY_COPY 9
#define SBTOPT_PROXY_LIMIT 10
#define SBTINFO_NAME 1 // (value -> char *) File name
#define SBTINFO_METHOD 2 // (value -> int*) 1 - stream, 2 - proxy copy
#define SBTINFO_CTIME 3 // (value -> time_t*) File creation date and time
#define SBTINFO_ETIME 4 // (value -> time_t*) File expiration date and time
#define SBTINFO_LABEL 5 // (value -> char *) Media ID
#define SBTINFO_SHARE 6 // (value -> int*) 1 - Single user, 2 - Multiple concurrent users
#define SBTINFO_ORDER 7 // (value -> int*) 1 - Sequential, 2 - Random
#define SBTINFO_NOTFOUND 8 // (value -> NULL) No such file
#define SBTINFO_COMMENT 9 // (value -> char*) Comment
#define SBTINFO_END 9999 // (value -> NULL) End marker
#define ROOT "/home/swei/libobk/data"
int catenate(char * buf, size_t n, const char * dir, const char * file)
{
const char * p = file;
// Remove leading slash
while (*p == '/')
++p;
return snprintf(buf, n, "%s/%s", dir, p);
}
int make_dir(char * buf, size_t n, const char * dir, const char * file)
{
char * path = NULL;
char * cursor = NULL;
int ret = 0;
if (catenate(buf, n, dir, file) < 0)
return (-1);
// mkdir
path = strdup(buf);
cursor = path + 1;
while ((cursor = strchr(cursor, '/')) != NULL) {
*cursor = '\0';
if ((ret = mkdir(path, 0755)) == -1) {
if (errno != EEXIST) {
free(path);
return (-1);
}
ret = 0;
}
*cursor++ = '/';
}
free(path);
return (ret);
}
int sbtinit2(struct sbtctx *ctx, int flags, const struct sbtinfo *args)
{
char buf[256];
const struct sbtinfo * arg = args;
ctx->pid = getpid();
ctx->tid = pthread_self();
snprintf(buf, sizeof(buf), "/tmp/log-%d-%x.txt", ctx->pid, ctx->tid);
if ((ctx->fp = fopen(buf, "w")) == NULL)
return (-1);
LOG("pid %d, tid 0x%x\n", ctx->pid, ctx->tid);
while (arg->type != SBTINFO_END) {
if (arg->type == SBTINFO_NAME) {
LOG("media %s\n", (const char *)arg->value);
}
LOG("type %d\n", arg->type);
++arg;
}
return (0);
}
int sbtbackup(struct sbtctx *ctx,
int flags,
const char *filename,
const struct sbtinfo *info,
int block_size,
int not_used,
int duplex)
{
ctx->file = strdup(filename);
ctx->block_size = block_size;
if (make_dir(ctx->path, sizeof(ctx->path), ROOT, filename) == -1)
return (-1);
LOG("open %s\n", ctx->path);
ctx->fd = open(ctx->path, O_CREAT | O_RDWR, 0644);
if (ctx->fd == -1) {
LOG("Error to open %s\n", ctx->path);
return (-1);
}
LOG("Block size %d\n", block_size);
LOG("not_used %d\n", not_used);
LOG("duplex %d\n", duplex);
return (0);
}
int sbtclose2(struct sbtctx * ctx, char flags)
{
LOG("Close %s\n", ctx->path);
if (ctx->fd != -1) {
close(ctx->fd);
ctx->fd = -1;
}
if (ctx->file) {
free(ctx->file);
ctx->file = NULL;
}
return (0);
}
int sbtcommand(struct sbtctx *ctx, int flags, const char *cmd)
{
LOG("cmd = %s\n", cmd);
return (0);
}
int sbtend(struct sbtctx *ctx, char flags)
{
LOG("%s\n", "end");
if (ctx->file) {
free(ctx->file);
ctx->file = NULL;
}
if (ctx->pairs) {
// TODO: free values
free(ctx->pairs);
ctx->pairs = NULL;
}
if (ctx->fp) {
fclose(ctx->fp);
ctx->fp = NULL;
}
return (0);
}
int sbterror(struct sbtctx *ctx,
int flags,
int *error,
const char **error_native,
char **error_utf8
)
{
*error = ctx->error;
LOG("error = %d\n", *error);
*error_native = strerror(*error);
*error_utf8 = "Error message in utf8";
return (0);
}
int sbtinfo2(struct sbtctx *ctx,
char flags,
const char **files,
struct sbtinfo **output
)
{
char path[256];
struct stat meta;
const char ** p = files;
const char * file = NULL;;
int count = 0;
int i = 0;
int ret = 0;
time_t * ts = NULL;
int * v = NULL;
while (*p++)
++count;
// Allocate n sbtinfo groups,
// each group starts with SBTINFO_NAME,
// SBTINFO_END must be set if no more data.
LOG("files count %d\n", count);
ctx->pairs = calloc(count * 9 + 1, sizeof(struct sbtinfo));
ctx->error = 0;
p = files;
i = 0;
while (*p) {
file = *p++;
catenate(path, sizeof(path), ROOT, file);
LOG("%s\n", path);
// Media information for backup file
ctx->pairs[i].type = SBTINFO_NAME;
ctx->pairs[i].value = strdup(file);
++i;
if ((ret = stat(path, &meta)) == -1) {
LOG("no such file %s\n", path);
ctx->error = SBTERROR_NOTFOUND;
ctx->pairs[i].type = SBTINFO_NOTFOUND;
ctx->pairs[i].value = NULL;
++i;
continue;
}
// Media sharing mode
*(v = (int *)malloc(4)) = 1;
ctx->pairs[i].type = SBTINFO_SHARE;
ctx->pairs[i].value = v;
++i;
// File ordering mode
*(v = (int *)malloc(4)) = 1;
ctx->pairs[i].type = SBTINFO_ORDER;
ctx->pairs[i].value = v;
++i;
// File creation method, stream
*(v = (int *)malloc(4)) = 1;
ctx->pairs[i].type = SBTINFO_METHOD;
ctx->pairs[i].value = v;
++i;
// Media ID
ctx->pairs[i].type = SBTINFO_LABEL;
ctx->pairs[i].value = strdup("Media ID");
++i;
// File creation date and time
*(ts = (time_t *)malloc(sizeof(time_t))) = meta.st_ctime;
ctx->pairs[i].type = SBTINFO_CTIME;
ctx->pairs[i].value = ts;
++i;
#if 0
// File expiration date and time
*(ts = (time_t *)malloc(sizeof(time_t))) = meta.st_ctime + 315360000;
ctx->pairs[i].type = SBTINFO_ETIME;
ctx->pairs[i].value = ts;
++i;
#endif
// Comment
ctx->pairs[i].type = SBTINFO_COMMENT;
ctx->pairs[i].value = strdup("Comment of the file");
++i;
}
// END marker
ctx->pairs[i].type = SBTINFO_END;
ctx->pairs[i].value = NULL;
*output = ctx->pairs;
return (0);
}
// Read until eof
int sbtread2(struct sbtctx *ctx, int flags, void *buf)
{
int ret = read(ctx->fd, buf, ctx->block_size);
if (ret > 0)
return (0);
ctx->error = 0;
if (ret == 0)
ctx->error = SBTERROR_EOF;
return (-1);
}
int sbtwrite2(struct sbtctx *ctx, int flags, void *buf)
{
int ret = write(ctx->fd, buf, ctx->block_size);
if (ret == -1)
return (-1);
return (0);
}
int sbtremove2(struct sbtctx *ctx, int flags, const char **files)
{
const char ** file = files;
char buf[256];
int ret;
while (*file) {
catenate(buf, sizeof(buf), ROOT, *file);
ret = remove(buf);
LOG("remove %s, ret %d\n", buf, ret);
++file;
}
return (0);
}
int sbtrestore(struct sbtctx *ctx, int flags, char *filename, int block_size)
{
ctx->block_size = block_size;
ctx->file = strdup(filename);
if (catenate(ctx->path, sizeof(ctx->path), ROOT, filename) == -1)
return (-1);
LOG("Restore %s\n", ctx->path);
if ((ctx->fd = open(ctx->path, O_RDONLY)) == -1) {
LOG("Error (%d) to open %s\n", errno, ctx->path);
return (-1);
}
return (0);
}
int sbtinit(struct sbtdata * data,
const struct sbtinfo * input,
struct sbtopt **output
)
{
int i = 0;
int ret = 0;
data->status = 0;
data->error = 0;
// Setup SBT version 2 api
global.api.sbtbackup = sbtbackup;
global.api.sbtclose2 = sbtclose2;
global.api.sbtcommand = sbtcommand;
global.api.sbtend = sbtend;
global.api.sbterror = sbterror;
global.api.sbtinfo2 = sbtinfo2;
global.api.sbtinit2 = sbtinit2;
global.api.sbtread2 = sbtread2;
global.api.sbtremove2 = sbtremove2;
global.api.sbtrestore = sbtrestore;
global.api.sbtwrite2 = sbtwrite2;
global.api_version = 0x0200;
global.options[i].type = SBTOPT_API_VERSION;
global.options[i].value = &global.api_version;
++i;
global.options[i].type = SBTOPT_VENDOR;
global.options[i].value = "SBT by Dingding Technology";
++i;
global.options[i].type = SBTOPT_API;
global.options[i].value = &global.api;
++i;
global.context_size = sizeof(struct sbtctx);
global.options[i].type = SBTOPT_CONTEXT_SIZE;
global.options[i].value = &global.context_size;
++i;
// Media manager version
global.mm_version = 0x01020304;
global.options[i].type = SBTOPT_MM_VERSION;
global.options[i].value = &global.mm_version;
++i;
#if 0
// Maximum backup file size
global.maxsize = 1024*1024;
global.options[i].type = SBTOPT_MAXSIZE;
global.options[i].value = &global.maxsize;
++i;
// Proxy copy
global.options[i].type = 9;
global.options[i].value = NULL;
++i;
// Maximum concurrent proxy copy files
global.proxy_limit = 0;
global.options[i].type = SBTOPT_PROXY_LIMIT;
global.options[i].value = &global.proxy_limit;
++i;
#endif
// End
global.options[i].type = SBTOPT_END;
global.options[i].value = NULL;
++i;
*output = global.options;
return (ret);
}