/
fd_plugins.c
2067 lines (1859 loc) · 55.5 KB
/
fd_plugins.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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2007-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2013 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation, which is
listed in the file 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
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Bareos pluginloader
*
* Kern Sibbald, October 2007
*/
#include "bareos.h"
#include "filed.h"
extern CLIENTRES *me;
extern DLL_IMP_EXP char *exepath;
extern DLL_IMP_EXP char *version;
extern DLL_IMP_EXP char *dist_name;
const int dbglvl = 150;
#ifdef HAVE_WIN32
const char *plugin_type = "-fd.dll";
#else
const char *plugin_type = "-fd.so";
#endif
static alist *fd_plugin_list = NULL;
extern int save_file(JCR *jcr, FF_PKT *ff_pkt, bool top_level);
extern bool check_changes(JCR *jcr, FF_PKT *ff_pkt);
/*
* Function pointers to be set here
*/
extern DLL_IMP_EXP int (*plugin_bopen)(BFILE *bfd, const char *fname, int flags, mode_t mode);
extern DLL_IMP_EXP int (*plugin_bclose)(BFILE *bfd);
extern DLL_IMP_EXP ssize_t (*plugin_bread)(BFILE *bfd, void *buf, size_t count);
extern DLL_IMP_EXP ssize_t (*plugin_bwrite)(BFILE *bfd, void *buf, size_t count);
extern DLL_IMP_EXP boffset_t (*plugin_blseek)(BFILE *bfd, boffset_t offset, int whence);
/*
* Forward referenced functions
*/
static bRC bareosGetValue(bpContext *ctx, bVariable var, void *value);
static bRC bareosSetValue(bpContext *ctx, bVariable var, void *value);
static bRC bareosRegisterEvents(bpContext *ctx, int nr_events, ...);
static bRC bareosJobMsg(bpContext *ctx, const char *file, int line,
int type, utime_t mtime, const char *fmt, ...);
static bRC bareosDebugMsg(bpContext *ctx, const char *file, int line,
int level, const char *fmt, ...);
static void *bareosMalloc(bpContext *ctx, const char *file, int line,
size_t size);
static void bareosFree(bpContext *ctx, const char *file, int line, void *mem);
static bRC bareosAddExclude(bpContext *ctx, const char *file);
static bRC bareosAddInclude(bpContext *ctx, const char *file);
static bRC bareosAddOptions(bpContext *ctx, const char *opts);
static bRC bareosAddRegex(bpContext *ctx, const char *item, int type);
static bRC bareosAddWild(bpContext *ctx, const char *item, int type);
static bRC bareosNewOptions(bpContext *ctx);
static bRC bareosNewInclude(bpContext *ctx);
static bRC bareosNewPreInclude(bpContext *ctx);
static bool is_plugin_compatible(Plugin *plugin);
static bool get_plugin_name(JCR *jcr, char *cmd, int *ret);
static bRC bareosCheckChanges(bpContext *ctx, struct save_pkt *sp);
static bRC bareosAcceptFile(bpContext *ctx, struct save_pkt *sp);
/*
* These will be plugged into the global pointer structure for the findlib.
*/
static int my_plugin_bopen(BFILE *bfd, const char *fname, int flags, mode_t mode);
static int my_plugin_bclose(BFILE *bfd);
static ssize_t my_plugin_bread(BFILE *bfd, void *buf, size_t count);
static ssize_t my_plugin_bwrite(BFILE *bfd, void *buf, size_t count);
static boffset_t my_plugin_blseek(BFILE *bfd, boffset_t offset, int whence);
/* Bareos info */
static bInfo binfo = {
sizeof(bInfo),
FD_PLUGIN_INTERFACE_VERSION
};
/* Bareos entry points */
static bFuncs bfuncs = {
sizeof(bFuncs),
FD_PLUGIN_INTERFACE_VERSION,
bareosRegisterEvents,
bareosGetValue,
bareosSetValue,
bareosJobMsg,
bareosDebugMsg,
bareosMalloc,
bareosFree,
bareosAddExclude,
bareosAddInclude,
bareosAddOptions,
bareosAddRegex,
bareosAddWild,
bareosNewOptions,
bareosNewInclude,
bareosNewPreInclude,
bareosCheckChanges,
bareosAcceptFile
};
/*
* Bareos private context
*/
struct b_plugin_ctx {
JCR *jcr; /* jcr for plugin */
bRC rc; /* last return code */
bool disabled; /* set if plugin disabled */
char events[nbytes_for_bits(FD_NR_EVENTS + 1)]; /* enabled events bitmask */
findINCEXE *exclude; /* pointer to exclude files */
findINCEXE *include; /* pointer to include/exclude files */
};
static inline bool is_event_enabled(bpContext *ctx, bEventType eventType)
{
b_plugin_ctx *b_ctx;
if (!ctx) {
return true;
}
b_ctx = (b_plugin_ctx *)ctx->bContext;
if (!b_ctx) {
return true;
}
return bit_is_set(eventType, b_ctx->events);
}
static inline bool is_plugin_disabled(bpContext *ctx)
{
b_plugin_ctx *b_ctx;
Dsm_check(999);
if (!ctx) {
return true;
}
b_ctx = (b_plugin_ctx *)ctx->bContext;
if (!b_ctx) {
return true;
}
return b_ctx->disabled;
}
/*
* Test if event is for this plugin
*/
static bool for_this_plugin(Plugin *plugin, char *name, int len)
{
Dmsg4(dbglvl, "name=%s len=%d plugin=%s plen=%d\n", name, len, plugin->file, plugin->file_len);
if (!name) { /* if no plugin name, all plugins get it */
return true;
}
/* Return global VSS job metadata to all plugins */
if (bstrcmp("job", name)) { /* old V4.0 name for VSS job metadata */
return true;
}
if (bstrcmp("*all*", name)) { /* new v6.0 name for VSS job metadata */
return true;
}
/* Check if this is the correct plugin */
if (len == plugin->file_len && bstrncmp(plugin->file, name, len)) {
return true;
}
return false;
}
bool is_plugin_disabled(JCR *jcr)
{
return is_plugin_disabled(jcr->plugin_ctx);
}
static inline void trigger_plugin_event(JCR *jcr, bEventType eventType, bEvent *event, Plugin *plugin,
bpContext *plugin_ctx_list, int index, void *value)
{
bpContext *ctx;
/*
* Note, at this point do not change
* jcr->plugin or jcr->plugin_ctx
*/
Dsm_check(999);
ctx = &plugin_ctx_list[index];
if (!is_event_enabled(ctx, eventType)) {
Dmsg1(dbglvl, "Event %d disabled for this plugin.\n", eventType);
return;
}
if (is_plugin_disabled(ctx)) {
return;
}
if (eventType == bEventEndRestoreJob) {
Dmsg0(50, "eventType==bEventEndRestoreJob\n");
if (jcr->plugin && jcr->plugin->restoreFileStarted) {
plug_func(jcr->plugin)->endRestoreFile(jcr->plugin_ctx);
}
if (jcr->plugin) {
jcr->plugin->restoreFileStarted = false;
jcr->plugin->createFileCalled = false;
}
}
plug_func(plugin)->handlePluginEvent(ctx, event, value);
}
/**
* Create a plugin event When receiving bEventCancelCommand, this function is
* called by an other thread.
*/
void generate_plugin_event(JCR *jcr, bEventType eventType, void *value, bool reverse)
{
bEvent event;
Plugin *plugin;
char *name = NULL;
int i;
int len = 0;
bool call_if_canceled = false;
restore_object_pkt *rop;
Dsm_check(999);
if (!fd_plugin_list || !jcr || !jcr->plugin_ctx_list) {
return; /* Return if no plugins loaded */
}
/*
* Some events are sent to only a particular plugin or must be
* called even if the job is canceled.
*/
switch(eventType) {
case bEventPluginCommand:
case bEventOptionPlugin:
name = (char *)value;
if (!get_plugin_name(jcr, name, &len)) {
return;
}
break;
case bEventRestoreObject:
/*
* After all RestoreObject, we have it one more time with value = NULL
*/
if (value) {
/*
* Some RestoreObjects may not have a plugin name
*/
rop = (restore_object_pkt *)value;
if (*rop->plugin_name) {
name = rop->plugin_name;
get_plugin_name(jcr, name, &len);
}
}
break;
case bEventEndBackupJob:
case bEventEndVerifyJob:
call_if_canceled = true; /* plugin *must* see this call */
break;
case bEventStartRestoreJob:
foreach_alist_index(i, plugin, fd_plugin_list) {
plugin->restoreFileStarted = false;
plugin->createFileCalled = false;
}
break;
case bEventEndRestoreJob:
call_if_canceled = true; /* plugin *must* see this call */
break;
default:
break;
}
/*
* If call_if_canceled is set, we call the plugin anyway
*/
if (!call_if_canceled && jcr->is_job_canceled()) {
return;
}
bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
event.eventType = eventType;
Dmsg2(dbglvl, "plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx_list, jcr->JobId);
/*
* Pass event to every plugin that has requested this event type
* (except if name is set). If name is set, we pass it only to
* the plugin with that name.
*
* See if we need to trigger the loaded plugins in reverse order.
*/
if (reverse) {
foreach_alist_rindex(i, plugin, fd_plugin_list) {
if (!for_this_plugin(plugin, name, len)) {
Dmsg2(dbglvl, "Not for this plugin name=%s NULL=%d\n",
name, (name == NULL) ? 1 : 0);
continue;
}
trigger_plugin_event(jcr, eventType, &event, plugin, plugin_ctx_list, i, value);
}
} else {
foreach_alist_index(i, plugin, fd_plugin_list) {
if (!for_this_plugin(plugin, name, len)) {
Dmsg2(dbglvl, "Not for this plugin name=%s NULL=%d\n",
name, (name == NULL) ? 1 : 0);
continue;
}
trigger_plugin_event(jcr, eventType, &event, plugin, plugin_ctx_list, i, value);
}
}
return;
}
/**
* Check if file was seen for accurate
*/
bool plugin_check_file(JCR *jcr, char *fname)
{
Plugin *plugin;
int rc = bRC_OK;
int i;
Dsm_check(999);
if (!fd_plugin_list || !jcr || !jcr->plugin_ctx_list || jcr->is_job_canceled()) {
return false; /* Return if no plugins loaded */
}
bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
Dmsg2(dbglvl, "plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx_list, jcr->JobId);
/* Pass event to every plugin */
foreach_alist_index(i, plugin, fd_plugin_list) {
jcr->plugin_ctx = &plugin_ctx_list[i];
jcr->plugin = plugin;
if (is_plugin_disabled(jcr)) {
continue;
}
if (plug_func(plugin)->checkFile == NULL) {
continue;
}
rc = plug_func(plugin)->checkFile(jcr->plugin_ctx, fname);
if (rc == bRC_Seen) {
break;
}
}
Dsm_check(999);
jcr->plugin = NULL;
jcr->plugin_ctx = NULL;
return rc == bRC_Seen;
}
/*
* Get the first part of the the plugin command
* systemstate:/@SYSTEMSTATE/
* => ret = 11
* => can use for_this_plugin(plug, cmd, ret);
*
* The plugin command can contain only the plugin name
* Plugin = alldrives
* => ret = 9
*/
static bool get_plugin_name(JCR *jcr, char *cmd, int *ret)
{
char *p;
int len;
Dsm_check(999);
if (!cmd || (*cmd == '\0')) {
return false;
}
/* Handle plugin command here backup */
Dmsg1(dbglvl, "plugin cmd=%s\n", cmd);
if ((p = strchr(cmd, ':')) == NULL) {
if (strchr(cmd, ' ') == NULL) { /* we have just the plugin name */
len = strlen(cmd);
} else {
Jmsg1(jcr, M_ERROR, 0, "Malformed plugin command: %s\n", cmd);
return false;
}
} else { /* plugin:argument */
len = p - cmd;
if (len <= 0) {
return false;
}
}
*ret = len;
Dsm_check(999);
return true;
}
static void update_ff_pkt(FF_PKT *ff_pkt, struct save_pkt *sp)
{
Dsm_check(999);
ff_pkt->no_read = sp->no_read;
ff_pkt->delta_seq = sp->delta_seq;
if (sp->flags & FO_DELTA) {
ff_pkt->flags |= FO_DELTA;
ff_pkt->delta_seq++; /* make new delta sequence number */
} else {
ff_pkt->flags &= ~FO_DELTA; /* clean delta sequence number */
ff_pkt->delta_seq = 0;
}
if (sp->flags & FO_OFFSETS) {
ff_pkt->flags |= FO_OFFSETS;
} else {
ff_pkt->flags &= ~FO_OFFSETS;
}
/* Sparse code doesn't work with plugins
* that use FIFO or STDOUT/IN to communicate
*/
if (sp->flags & FO_SPARSE) {
ff_pkt->flags |= FO_SPARSE;
} else {
ff_pkt->flags &= ~FO_SPARSE;
}
if (sp->flags & FO_PORTABLE_DATA) {
ff_pkt->flags |= FO_PORTABLE_DATA;
} else {
ff_pkt->flags &= ~FO_PORTABLE_DATA;
}
ff_pkt->flags |= FO_PLUGIN; /* data from plugin */
Dsm_check(999);
}
/*
* Ask to a Option Plugin what to do with the current file
*/
bRC plugin_option_handle_file(JCR *jcr, FF_PKT *ff_pkt, struct save_pkt *sp)
{
Plugin *plugin;
bRC ret = bRC_Error;
char *cmd = ff_pkt->plugin;
int len;
int i=0;
bEvent event;
event.eventType = bEventHandleBackupFile;
Dsm_check(999);
bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
memset(sp, 0, sizeof(struct save_pkt));
sp->pkt_size = sp->pkt_end = sizeof(struct save_pkt);
sp->portable = true;
sp->cmd = cmd;
sp->link = ff_pkt->link;
sp->cmd = ff_pkt->plugin;
sp->statp = ff_pkt->statp;
sp->fname = ff_pkt->fname;
sp->delta_seq = ff_pkt->delta_seq;
sp->accurate_found = ff_pkt->accurate_found;
if (!fd_plugin_list || !jcr->plugin_ctx_list || jcr->is_job_canceled()) {
Jmsg1(jcr, M_FATAL, 0, "Command plugin \"%s\" requested, but is not loaded.\n", cmd);
goto bail_out; /* Return if no plugins loaded */
}
if (!get_plugin_name(jcr, cmd, &len)) {
goto bail_out;
}
/* Note, we stop the loop on the first plugin that matches the name */
foreach_alist_index(i, plugin, fd_plugin_list) {
Dmsg4(dbglvl, "plugin=%s plen=%d cmd=%s len=%d\n", plugin->file, plugin->file_len, cmd, len);
if (!for_this_plugin(plugin, cmd, len)) {
continue;
}
Dsm_check(999);
if (is_plugin_disabled(&plugin_ctx_list[i])) {
goto bail_out;
}
jcr->plugin_ctx = &plugin_ctx_list[i];
jcr->plugin = plugin;
ret = plug_func(plugin)->handlePluginEvent(&plugin_ctx_list[i],
&event, sp);
/* TODO: would be better to set this in save_file() */
if (ret == bRC_OK) {
jcr->opt_plugin = true;
jcr->plugin = plugin;
jcr->plugin_sp = sp; /* Unset sp in save_file */
jcr->plugin_ctx = &plugin_ctx_list[i];
update_ff_pkt(ff_pkt, sp);
/* reset plugin in JCR if not used this time */
} else {
jcr->plugin_ctx = NULL;
jcr->plugin = NULL;
}
goto bail_out;
} /* end foreach loop */
bail_out:
Dsm_check(999);
return ret;
}
/**
* Sequence of calls for a backup:
* 1. plugin_save() here is called with ff_pkt
* 2. we find the plugin requested on the command string
* 3. we generate a bEventBackupCommand event to the specified plugin
* and pass it the command string.
* 4. we make a startPluginBackup call to the plugin, which gives
* us the data we need in save_pkt
* 5. we call Bareos's save_file() subroutine to save the specified
* file. The plugin will be called at pluginIO() to supply the
* file data.
*
* Sequence of calls for restore:
* See subroutine plugin_name_stream() below.
*/
int plugin_save(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
{
Plugin *plugin;
int len;
int i;
char *cmd = ff_pkt->top_fname;
struct save_pkt sp;
bEvent event;
POOL_MEM fname(PM_FNAME);
POOL_MEM link(PM_FNAME);
Dsm_check(999);
if (!fd_plugin_list || !jcr->plugin_ctx_list || jcr->is_job_canceled()) {
Jmsg1(jcr, M_FATAL, 0, "Command plugin \"%s\" requested, but is not loaded.\n", cmd);
return 1; /* Return if no plugins loaded */
}
jcr->cmd_plugin = true;
bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
event.eventType = bEventBackupCommand;
if (!get_plugin_name(jcr, cmd, &len)) {
goto bail_out;
}
/* Note, we stop the loop on the first plugin that matches the name */
foreach_alist_index(i, plugin, fd_plugin_list) {
Dmsg4(dbglvl, "plugin=%s plen=%d cmd=%s len=%d\n", plugin->file, plugin->file_len, cmd, len);
if (!for_this_plugin(plugin, cmd, len)) {
continue;
}
/*
* We put the current plugin pointer, and the plugin context
* into the jcr, because during save_file(), the plugin
* will be called many times and these values are needed.
*/
Dsm_check(999);
jcr->plugin_ctx = &plugin_ctx_list[i];
jcr->plugin = plugin;
if (is_plugin_disabled(jcr)) {
goto bail_out;
}
Dmsg1(dbglvl, "Command plugin = %s\n", cmd);
/* Send the backup command to the right plugin*/
if (plug_func(plugin)->handlePluginEvent(jcr->plugin_ctx, &event, cmd) != bRC_OK) {
goto bail_out;
}
/* Loop getting filenames to backup then saving them */
while (!jcr->is_job_canceled()) {
memset(&sp, 0, sizeof(sp));
sp.pkt_size = sizeof(sp);
sp.pkt_end = sizeof(sp);
sp.portable = true;
sp.no_read = false;
sp.flags = 0;
sp.cmd = cmd;
Dmsg3(dbglvl, "startBackup st_size=%p st_blocks=%p sp=%p\n", &sp.statp.st_size, &sp.statp.st_blocks,
&sp);
Dsm_check(999);
/* Get the file save parameters. I.e. the stat pkt ... */
if (plug_func(plugin)->startBackupFile(jcr->plugin_ctx, &sp) != bRC_OK) {
goto bail_out;
}
if (sp.type == 0) {
Jmsg1(jcr, M_FATAL, 0, _("Command plugin \"%s\": no type in startBackupFile packet.\n"),
cmd);
goto bail_out;
}
jcr->plugin_sp = &sp;
ff_pkt = jcr->ff;
/*
* Copy fname and link because save_file() zaps them. This
* avoids zaping the plugin's strings.
*/
ff_pkt->type = sp.type;
if (IS_FT_OBJECT(sp.type)) {
if (!sp.object_name) {
Jmsg1(jcr, M_FATAL, 0, _("Command plugin \"%s\": no object_name in startBackupFile packet.\n"),
cmd);
goto bail_out;
}
ff_pkt->fname = cmd; /* full plugin string */
ff_pkt->object_name = sp.object_name;
ff_pkt->object_index = sp.index; /* restore object index */
ff_pkt->object_compression = 0; /* no compression for now */
ff_pkt->object = sp.object;
ff_pkt->object_len = sp.object_len;
} else {
Dsm_check(999);
if (!sp.fname) {
Jmsg1(jcr, M_FATAL, 0, _("Command plugin \"%s\": no fname in startBackupFile packet.\n"),
cmd);
goto bail_out;
}
pm_strcpy(fname, sp.fname);
pm_strcpy(link, sp.link);
ff_pkt->fname = fname.c_str();
ff_pkt->link = link.c_str();
update_ff_pkt(ff_pkt, &sp);
}
memcpy(&ff_pkt->statp, &sp.statp, sizeof(ff_pkt->statp));
Dmsg2(dbglvl, "startBackup returned type=%d, fname=%s\n", sp.type, sp.fname);
if (sp.object) {
Dmsg2(dbglvl, "index=%d object=%s\n", sp.index, sp.object);
}
/* Call Bareos core code to backup the plugin's file */
save_file(jcr, ff_pkt, true);
bRC rc = plug_func(plugin)->endBackupFile(jcr->plugin_ctx);
if (rc == bRC_More || rc == bRC_OK) {
accurate_mark_file_as_seen(jcr, fname.c_str());
}
Dsm_check(999);
if (rc == bRC_More) {
continue;
}
goto bail_out;
} /* end while loop */
goto bail_out;
} /* end loop over all plugins */
Jmsg1(jcr, M_FATAL, 0, "Command plugin \"%s\" not found.\n", cmd);
bail_out:
Dsm_check(999);
jcr->cmd_plugin = false;
jcr->plugin = NULL;
jcr->plugin_ctx = NULL;
return 1;
}
/**
* Sequence of calls for a estimate:
* 1. plugin_estimate() here is called with ff_pkt
* 2. we find the plugin requested on the command string
* 3. we generate a bEventEstimateCommand event to the specified plugin
* and pass it the command string.
* 4. we make a startPluginBackup call to the plugin, which gives
* us the data we need in save_pkt
*
*/
int plugin_estimate(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
{
Plugin *plugin;
int len;
int i;
char *cmd = ff_pkt->top_fname;
struct save_pkt sp;
bEvent event;
POOL_MEM fname(PM_FNAME);
POOL_MEM link(PM_FNAME);
ATTR attr;
Dsm_check(999);
if (!fd_plugin_list || !jcr->plugin_ctx_list) {
Jmsg1(jcr, M_FATAL, 0, "Command plugin \"%s\" requested, but is not loaded.\n", cmd);
return 1; /* Return if no plugins loaded */
}
jcr->cmd_plugin = true;
bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
event.eventType = bEventEstimateCommand;
if (!get_plugin_name(jcr, cmd, &len)) {
goto bail_out;
}
/*
* Note, we stop the loop on the first plugin that matches the name
*/
foreach_alist_index(i, plugin, fd_plugin_list) {
Dmsg4(dbglvl, "plugin=%s plen=%d cmd=%s len=%d\n", plugin->file, plugin->file_len, cmd, len);
if (!for_this_plugin(plugin, cmd, len)) {
continue;
}
/*
* We put the current plugin pointer, and the plugin context
* into the jcr, because during save_file(), the plugin
* will be called many times and these values are needed.
*/
Dsm_check(999);
jcr->plugin_ctx = &plugin_ctx_list[i];
jcr->plugin = plugin;
if (is_plugin_disabled(jcr)) {
goto bail_out;
}
Dmsg1(dbglvl, "Command plugin = %s\n", cmd);
/*
* Send the backup command to the right plugin
*/
if (plug_func(plugin)->handlePluginEvent(jcr->plugin_ctx, &event, cmd) != bRC_OK) {
goto bail_out;
}
/*
* Loop getting filenames to backup then saving them
*/
while (!jcr->is_job_canceled()) {
Dsm_check(999);
memset(&sp, 0, sizeof(sp));
sp.pkt_size = sizeof(sp);
sp.pkt_end = sizeof(sp);
sp.portable = true;
sp.flags = 0;
sp.cmd = cmd;
Dmsg3(dbglvl, "startBackup st_size=%p st_blocks=%p sp=%p\n", &sp.statp.st_size, &sp.statp.st_blocks,
&sp);
/* Get the file save parameters. I.e. the stat pkt ... */
if (plug_func(plugin)->startBackupFile(jcr->plugin_ctx, &sp) != bRC_OK) {
goto bail_out;
}
if (sp.type == 0) {
Jmsg1(jcr, M_FATAL, 0, _("Command plugin \"%s\": no type in startBackupFile packet.\n"),
cmd);
goto bail_out;
}
if (!IS_FT_OBJECT(sp.type)) {
if (!sp.fname) {
Jmsg1(jcr, M_FATAL, 0, _("Command plugin \"%s\": no fname in startBackupFile packet.\n"),
cmd);
goto bail_out;
}
/* Count only files backed up */
switch (sp.type) {
case FT_REGE:
case FT_REG:
case FT_LNK:
case FT_DIREND:
case FT_SPEC:
case FT_RAW:
case FT_FIFO:
case FT_LNKSAVED:
jcr->JobFiles++; /* increment number of files backed up */
break;
default:
break;
}
jcr->num_files_examined++;
if (sp.type != FT_LNKSAVED && S_ISREG(sp.statp.st_mode)) {
if (sp.statp.st_size > 0) {
jcr->JobBytes += sp.statp.st_size;
}
}
if (jcr->listing) {
memcpy(&attr.statp, &sp.statp, sizeof(struct stat));
attr.type = sp.type;
attr.ofname = (POOLMEM *)sp.fname;
attr.olname = (POOLMEM *)sp.link;
print_ls_output(jcr, &attr);
}
}
Dmsg2(dbglvl, "startBackup returned type=%d, fname=%s\n", sp.type, sp.fname);
if (sp.object) {
Dmsg2(dbglvl, "index=%d object=%s\n", sp.index, sp.object);
}
bRC rc = plug_func(plugin)->endBackupFile(jcr->plugin_ctx);
if (rc == bRC_More || rc == bRC_OK) {
accurate_mark_file_as_seen(jcr, sp.fname);
}
Dsm_check(999);
if (rc == bRC_More) {
continue;
}
goto bail_out;
} /* end while loop */
goto bail_out;
} /* end loop over all plugins */
Jmsg1(jcr, M_FATAL, 0, "Command plugin \"%s\" not found.\n", cmd);
bail_out:
Dsm_check(999);
jcr->cmd_plugin = false;
jcr->plugin = NULL;
jcr->plugin_ctx = NULL;
return 1;
}
/**
* Send plugin name start/end record to SD
*/
bool send_plugin_name(JCR *jcr, BSOCK *sd, bool start)
{
int status;
int index = jcr->JobFiles;
struct save_pkt *sp = (struct save_pkt *)jcr->plugin_sp;
Dsm_check(999);
if (!sp) {
Jmsg0(jcr, M_FATAL, 0, _("Plugin save packet not found.\n"));
return false;
}
if (jcr->is_job_canceled()) {
return false;
}
if (start) {
index++; /* JobFiles not incremented yet */
}
Dmsg1(dbglvl, "send_plugin_name=%s\n", sp->cmd);
/* Send stream header */
Dsm_check(999);
if (!sd->fsend("%ld %d 0", index, STREAM_PLUGIN_NAME)) {
Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
sd->bstrerror());
return false;
}
Dmsg1(dbglvl, "send plugin name hdr: %s\n", sd->msg);
Dsm_check(999);
if (start) {
/* Send data -- not much */
status = sd->fsend("%ld 1 %d %s%c", index, sp->portable, sp->cmd, 0);
} else {
/* Send end of data */
status = sd->fsend("%ld 0", jcr->JobFiles);
}
Dsm_check(999);
if (!status) {
Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
sd->bstrerror());
return false;
}
Dmsg1(dbglvl, "send plugin start/end: %s\n", sd->msg);
sd->signal(BNET_EOD); /* indicate end of plugin name data */
Dsm_check(999);
return true;
}
/**
* Plugin name stream found during restore. The record passed in
* argument name was generated in send_plugin_name() above.
*
* Returns: true if start of stream
* false if end of steam
*/
bool plugin_name_stream(JCR *jcr, char *name)
{
char *p = name;
char *cmd;
bool start;
Plugin *plugin;
int len;
int i;
bpContext *plugin_ctx_list = jcr->plugin_ctx_list;
Dsm_check(999);
Dmsg1(dbglvl, "Read plugin stream string=%s\n", name);
skip_nonspaces(&p); /* skip over jcr->JobFiles */
skip_spaces(&p);
start = *p == '1';
if (start) {
/* Start of plugin data */
skip_nonspaces(&p); /* skip start/end flag */
skip_spaces(&p);
// portable = *p == '1';
skip_nonspaces(&p); /* skip portable flag */
skip_spaces(&p);
cmd = p;
} else {
/*
* End of plugin data, notify plugin, then clear flags
*/
Dmsg2(dbglvl, "End plugin data plugin=%p ctx=%p\n", jcr->plugin, jcr->plugin_ctx);
if (jcr->plugin && jcr->plugin->restoreFileStarted) {
plug_func(jcr->plugin)->endRestoreFile(jcr->plugin_ctx);
}
if (jcr->plugin) {
jcr->plugin->restoreFileStarted = false;
jcr->plugin->createFileCalled = false;
}
jcr->plugin_ctx = NULL;
jcr->plugin = NULL;
goto bail_out;
}
Dsm_check(999);
if (!plugin_ctx_list) {
goto bail_out;
}
/*
* After this point, we are dealing with a restore start
*/
if (!get_plugin_name(jcr, cmd, &len)) {
goto bail_out;
}
/*
* Search for correct plugin as specified on the command
*/
Dsm_check(999);
foreach_alist_index(i, plugin, fd_plugin_list) {
bEvent event;
Dmsg3(dbglvl, "plugin=%s cmd=%s len=%d\n", plugin->file, cmd, len);
if (!for_this_plugin(plugin, cmd, len)) {
continue;
}
Dsm_check(999);
jcr->plugin_ctx = &plugin_ctx_list[i];
jcr->plugin = plugin;
if (is_plugin_disabled(jcr)) {
Dmsg1(dbglvl, "Plugin %s disabled\n", cmd);
goto bail_out;
}
Dmsg1(dbglvl, "Restore Command plugin = %s\n", cmd);
event.eventType = bEventRestoreCommand;
if (plug_func(plugin)->handlePluginEvent(jcr->plugin_ctx,
&event, cmd) != bRC_OK) {
Dmsg1(dbglvl, "Handle event failed. Plugin=%s\n", cmd);
goto bail_out;
}
if (plugin->restoreFileStarted) {
Jmsg2(jcr, M_FATAL, 0, "Second call to startRestoreFile. plugin=%s cmd=%s\n", plugin->file, cmd);
plugin->restoreFileStarted = false;
goto bail_out;
}
if (plug_func(plugin)->startRestoreFile(jcr->plugin_ctx, cmd) == bRC_OK) {
plugin->restoreFileStarted = true;
goto ok_out;
} else {
Dmsg1(dbglvl, "startRestoreFile failed. plugin=%s\n", cmd);
}
goto bail_out;
}
Jmsg1(jcr, M_WARNING, 0, _("Plugin=%s not found.\n"), cmd);
goto bail_out;
ok_out:
return start;
bail_out:
Dsm_check(999);
jcr->plugin = NULL;
jcr->plugin_ctx = NULL;
return start;
}
/**
* Tell the plugin to create the file. Return values are
* This is called only during Restore
*
* CF_ERROR -- error
* CF_SKIP -- skip processing this file
* CF_EXTRACT -- extract the file (i.e.call i/o routines)
* CF_CREATED -- created, but no content to extract (typically directories)
*
*/
int plugin_create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace)
{
bpContext *ctx = jcr->plugin_ctx;
Plugin *plugin = jcr->plugin;
struct restore_pkt rp;
int flags;
int rc;
Dsm_check(999);
if (!plugin || !ctx || !set_cmd_plugin(bfd, jcr) || jcr->is_job_canceled()) {
return CF_ERROR;
}
rp.pkt_size = sizeof(rp);