-
Notifications
You must be signed in to change notification settings - Fork 256
/
bareosfd.cc
2259 lines (1878 loc) · 65.3 KB
/
bareosfd.cc
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) 2020-2023 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.
*/
/**
* @file
* Python module for the Bareos filedaemon plugin
*/
#define PY_SSIZE_T_CLEAN
#define BUILD_PLUGIN
#if defined(HAVE_WIN32)
# include "include/bareos.h"
# include <Python.h>
#else
# include <Python.h>
# include "include/bareos.h"
#endif
#include "include/version_hex.h"
#define LOGPREFIX "python3-fd-mod: "
#include "filed/fd_plugins.h"
#define BAREOSFD_MODULE
#include "bareosfd.h"
#include "include/filetypes.h"
#include "lib/edit.h"
namespace filedaemon {
static const int debuglevel = 150;
static bRC set_bareos_core_functions(CoreFunctions* new_bareos_core_functions);
static bRC set_plugin_context(PluginContext* new_plugin_context);
static bRC PyParsePluginDefinition(PluginContext* plugin_ctx, void* value);
static bRC PyGetPluginValue(PluginContext* plugin_ctx,
pVariable var,
void* value);
static bRC PySetPluginValue(PluginContext* plugin_ctx,
pVariable var,
void* value);
static bRC PyHandlePluginEvent(PluginContext* plugin_ctx,
bEvent* event,
void* value);
static bRC PyStartBackupFile(PluginContext* plugin_ctx, save_pkt* sp);
static bRC PyEndBackupFile(PluginContext* plugin_ctx);
static bRC PyPluginIO(PluginContext* plugin_ctx, io_pkt* io);
static bRC PyStartRestoreFile(PluginContext* plugin_ctx, const char* cmd);
static bRC PyEndRestoreFile(PluginContext* plugin_ctx);
static bRC PyCreateFile(PluginContext* plugin_ctx, restore_pkt* rp);
static bRC PySetFileAttributes(PluginContext* plugin_ctx, restore_pkt* rp);
static bRC PyCheckFile(PluginContext* plugin_ctx, char* fname);
static bRC PyGetAcl(PluginContext* plugin_ctx, acl_pkt* ap);
static bRC PySetAcl(PluginContext* plugin_ctx, acl_pkt* ap);
static bRC PyGetXattr(PluginContext* plugin_ctx, xattr_pkt* xp);
static bRC PySetXattr(PluginContext* plugin_ctx, xattr_pkt* xp);
static bRC PyRestoreObjectData(PluginContext* plugin_ctx,
restore_object_pkt* rop);
static bRC PyHandleBackupFile(PluginContext* plugin_ctx, save_pkt* sp);
/* Pointers to Bareos functions */
static CoreFunctions* bareos_core_functions = NULL;
#include "plugins/filed/python/plugin_private_context.h"
#define NOPLUGINSETGETVALUE 1
/* functions common to all plugins */
#include "plugins/include/python_plugins_common.inc"
/* set the bareos_core_functions pointer to the given value */
static bRC set_bareos_core_functions(CoreFunctions* new_bareos_core_functions)
{
bareos_core_functions = new_bareos_core_functions;
return bRC_OK;
}
/* set the plugin context pointer to the given value */
static bRC set_plugin_context(PluginContext* new_plugin_context)
{
plugin_context = new_plugin_context;
return bRC_OK;
}
/**
* Any plugin options which are passed in are dispatched here to a Python
* method and it can parse the plugin options. This function is also called
* after PyLoadModule() has loaded the Python module and made sure things are
* operational. Normally you would only get one set of plugin options but for
* a restore overrides can be passed in before the actual plugin options are
* restored as part of the restore stream handling.
*/
static bRC PyParsePluginDefinition(PluginContext* plugin_ctx, void* value)
{
bRC retval = bRC_Error;
struct plugin_private_context* plugin_priv_ctx
= (struct plugin_private_context*)plugin_ctx->plugin_private_context;
PyObject* pFunc;
// Lookup the parse_plugin_definition() function in the python module.
pFunc = PyDict_GetItemString(
plugin_priv_ctx->pyModuleFunctionsDict,
"parse_plugin_definition"); /* Borrowed reference */
if (pFunc && PyCallable_Check(pFunc)) {
PyObject *pPluginDefinition, *pRetVal;
pPluginDefinition = PyUnicode_FromString((char*)value);
if (!pPluginDefinition) { goto bail_out; }
pRetVal = PyObject_CallFunctionObjArgs(pFunc, pPluginDefinition, NULL);
Py_DECREF(pPluginDefinition);
if (!pRetVal) {
goto bail_out;
} else {
retval = ConvertPythonRetvalTobRCRetval(pRetVal);
Py_DECREF(pRetVal);
}
return retval;
} else {
Dmsg(plugin_ctx, debuglevel,
LOGPREFIX
"Failed to find function named "
"parse_plugin_definition()\n");
return bRC_Error;
}
bail_out:
if (PyErr_Occurred()) { PyErrorHandler(plugin_ctx, M_FATAL); }
return retval;
}
static bRC PyGetPluginValue(PluginContext*, pVariable, void*) { return bRC_OK; }
static bRC PySetPluginValue(PluginContext*, pVariable, void*) { return bRC_OK; }
static bRC PyHandlePluginEvent(PluginContext* plugin_ctx, bEvent* event, void*)
{
bRC retval = bRC_Error;
plugin_private_context* plugin_priv_ctx
= (plugin_private_context*)plugin_ctx->plugin_private_context;
PyObject* pFunc;
// Lookup the handle_plugin_event() function in the python module.
pFunc = PyDict_GetItemString(plugin_priv_ctx->pyModuleFunctionsDict,
"handle_plugin_event"); /* Borrowed reference */
if (pFunc && PyCallable_Check(pFunc)) {
PyObject *pEventType, *pRetVal;
pEventType = PyLong_FromLong(event->eventType);
pRetVal = PyObject_CallFunctionObjArgs(pFunc, pEventType, NULL);
Py_DECREF(pEventType);
if (!pRetVal) {
goto bail_out;
} else {
retval = ConvertPythonRetvalTobRCRetval(pRetVal);
Py_DECREF(pRetVal);
}
} else {
Dmsg(plugin_ctx, debuglevel,
LOGPREFIX "Failed to find function named handle_plugin_event()\n");
}
return retval;
bail_out:
if (PyErr_Occurred()) { PyErrorHandler(plugin_ctx, M_FATAL); }
return retval;
}
static inline PyStatPacket* NativeToPyStatPacket(struct stat* statp)
{
PyStatPacket* pStatp = PyObject_New(PyStatPacket, &PyStatPacketType);
if (pStatp) {
pStatp->dev = statp->st_dev;
pStatp->ino = statp->st_ino;
pStatp->mode = statp->st_mode;
pStatp->nlink = statp->st_nlink;
pStatp->uid = statp->st_uid;
pStatp->gid = statp->st_gid;
pStatp->rdev = statp->st_rdev;
pStatp->size = statp->st_size;
pStatp->atime = statp->st_atime;
pStatp->mtime = statp->st_mtime;
pStatp->ctime = statp->st_ctime;
pStatp->blksize = statp->st_blksize;
pStatp->blocks = statp->st_blocks;
}
return pStatp;
}
static inline void PyStatPacketToNative(PyStatPacket* pStatp,
struct stat* statp)
{
statp->st_dev = pStatp->dev;
statp->st_ino = pStatp->ino;
statp->st_mode = pStatp->mode;
statp->st_nlink = pStatp->nlink;
statp->st_uid = pStatp->uid;
statp->st_gid = pStatp->gid;
statp->st_rdev = pStatp->rdev;
statp->st_size = pStatp->size;
statp->st_atime = pStatp->atime;
statp->st_mtime = pStatp->mtime;
statp->st_ctime = pStatp->ctime;
statp->st_blksize = pStatp->blksize;
statp->st_blocks = pStatp->blocks;
}
static inline PySavePacket* NativeToPySavePacket(save_pkt* sp)
{
PySavePacket* pSavePkt = PyObject_New(PySavePacket, &PySavePacketType);
if (pSavePkt) {
pSavePkt->fname = PyUnicode_FromString(sp->fname ? sp->fname : "");
pSavePkt->link = PyUnicode_FromString(sp->link ? sp->link : "");
if (sp->statp.st_mode) {
pSavePkt->statp = (PyObject*)NativeToPyStatPacket(&sp->statp);
} else {
pSavePkt->statp = NULL;
}
pSavePkt->type = sp->type;
pSavePkt->flags
= PyByteArray_FromStringAndSize(sp->flags, sizeof(sp->flags));
pSavePkt->no_read = sp->no_read;
pSavePkt->portable = sp->portable;
pSavePkt->accurate_found = sp->accurate_found;
pSavePkt->cmd = sp->cmd;
pSavePkt->save_time = sp->save_time;
pSavePkt->delta_seq = sp->delta_seq;
pSavePkt->object_name = NULL;
pSavePkt->object = NULL;
pSavePkt->object_len = sp->object_len;
pSavePkt->object_index = sp->index;
}
return pSavePkt;
} // namespace filedaemon
static inline bool PySavePacketToNative(
PySavePacket* pSavePkt,
save_pkt* sp,
struct plugin_private_context* plugin_priv_ctx,
bool is_options_plugin)
{
// See if this is for an Options Plugin.
if (!is_options_plugin) {
// Only copy back the arguments that are allowed to change.
if (pSavePkt->fname) {
/* As this has to linger as long as the backup is running we save it in
* our plugin context. */
if (PyUnicode_Check(pSavePkt->fname)) {
if (plugin_priv_ctx->fname) { free(plugin_priv_ctx->fname); }
const char* fileName_AsUTF8 = PyUnicode_AsUTF8(pSavePkt->fname);
if (!fileName_AsUTF8) return false;
plugin_priv_ctx->fname = strdup(fileName_AsUTF8);
sp->fname = plugin_priv_ctx->fname;
}
} else {
goto bail_out;
}
// Optional field.
if (pSavePkt->link) {
/* As this has to linger as long as the backup is running we save it in
* our plugin context. */
if (PyUnicode_Check(pSavePkt->link)) {
if (plugin_priv_ctx->link) { free(plugin_priv_ctx->link); }
plugin_priv_ctx->link = strdup(PyUnicode_AsUTF8(pSavePkt->link));
sp->link = plugin_priv_ctx->link;
}
}
// Handle the stat structure.
if (pSavePkt->statp) {
PyStatPacketToNative((PyStatPacket*)pSavePkt->statp, &sp->statp);
} else {
goto bail_out;
}
sp->type = pSavePkt->type;
if (PyByteArray_Check(pSavePkt->flags)) {
char* flags;
if (PyByteArray_Size(pSavePkt->flags) != sizeof(sp->flags)) {
goto bail_out;
}
if ((flags = PyByteArray_AsString(pSavePkt->flags))) {
memcpy(sp->flags, flags, sizeof(sp->flags));
} else {
goto bail_out;
}
} else {
goto bail_out;
}
// Special code for handling restore objects.
if (IS_FT_OBJECT(sp->type)) {
// See if a proper restore object was created.
if (pSavePkt->object_len > 0) {
/* As this has to linger as long as the backup is running we save it
* in our plugin context. */
if (pSavePkt->object_name && pSavePkt->object
&& PyUnicode_Check(pSavePkt->object_name)
&& PyByteArray_Check(pSavePkt->object)) {
char* buf;
if (plugin_priv_ctx->object_name) {
free(plugin_priv_ctx->object_name);
}
plugin_priv_ctx->object_name
= strdup(PyUnicode_AsUTF8(pSavePkt->object_name));
sp->object_name = plugin_priv_ctx->object_name;
sp->object_len = pSavePkt->object_len;
sp->index = pSavePkt->object_index;
if ((buf = PyByteArray_AsString(pSavePkt->object))) {
if (plugin_priv_ctx->object) { free(plugin_priv_ctx->object); }
plugin_priv_ctx->object = (char*)malloc(pSavePkt->object_len);
memcpy(plugin_priv_ctx->object, buf, pSavePkt->object_len);
sp->object = plugin_priv_ctx->object;
} else {
goto bail_out;
}
} else {
goto bail_out;
}
} else {
goto bail_out;
}
} else {
sp->no_read = pSavePkt->no_read;
sp->delta_seq = pSavePkt->delta_seq;
sp->save_time = pSavePkt->save_time;
}
} else {
sp->no_read = pSavePkt->no_read;
sp->delta_seq = pSavePkt->delta_seq;
sp->save_time = pSavePkt->save_time;
if (PyByteArray_Check(pSavePkt->flags)) {
char* flags;
if (PyByteArray_Size(pSavePkt->flags) != sizeof(sp->flags)) {
goto bail_out;
}
if ((flags = PyByteArray_AsString(pSavePkt->flags))) {
memcpy(sp->flags, flags, sizeof(sp->flags));
} else {
goto bail_out;
}
} else {
goto bail_out;
}
}
return true;
bail_out:
return false;
}
/**
* Called when starting to backup a file. Here the plugin must
* return the "stat" packet for the directory/file and provide
* certain information so that Bareos knows what the file is.
* The plugin can create "Virtual" files by giving them a
* name that is not normally found on the file system.
*/
static bRC PyStartBackupFile(PluginContext* plugin_ctx, save_pkt* sp)
{
bRC retval = bRC_Error;
struct plugin_private_context* plugin_priv_ctx
= (struct plugin_private_context*)plugin_ctx->plugin_private_context;
PyObject* pFunc;
// Lookup the start_backup_file() function in the python module.
pFunc = PyDict_GetItemString(plugin_priv_ctx->pyModuleFunctionsDict,
"start_backup_file"); /* Borrowed reference */
if (pFunc && PyCallable_Check(pFunc)) {
PySavePacket* pSavePkt;
PyObject* pRetVal;
pSavePkt = NativeToPySavePacket(sp);
if (!pSavePkt) { goto bail_out; }
pRetVal = PyObject_CallFunctionObjArgs(pFunc, (PyObject*)pSavePkt, NULL);
if (!pRetVal) {
Py_DECREF((PyObject*)pSavePkt);
goto bail_out;
} else {
retval = ConvertPythonRetvalTobRCRetval(pRetVal);
Py_DECREF(pRetVal);
if (!PySavePacketToNative(pSavePkt, sp, plugin_priv_ctx, false)) {
Py_DECREF((PyObject*)pSavePkt);
goto bail_out;
}
Py_DECREF((PyObject*)pSavePkt);
}
} else {
Dmsg(plugin_ctx, debuglevel,
LOGPREFIX "Failed to find function named start_backup_file()\n");
}
return retval;
bail_out:
if (PyErr_Occurred()) { PyErrorHandler(plugin_ctx, M_FATAL); }
return retval;
}
/**
* Called at the end of backing up a file for a command plugin.
* If the plugin's work is done, it should return bRC_OK.
* If the plugin wishes to create another file and back it up,
* then it must return bRC_More (not yet implemented).
*/
static bRC PyEndBackupFile(PluginContext* plugin_ctx)
{
bRC retval = bRC_Error;
struct plugin_private_context* plugin_priv_ctx
= (struct plugin_private_context*)plugin_ctx->plugin_private_context;
PyObject* pFunc;
// Lookup the end_backup_file() function in the python module.
pFunc = PyDict_GetItemString(plugin_priv_ctx->pyModuleFunctionsDict,
"end_backup_file"); /* Borrowed reference */
if (pFunc && PyCallable_Check(pFunc)) {
PyObject* pRetVal;
pRetVal = PyObject_CallFunctionObjArgs(pFunc, NULL);
if (!pRetVal) {
goto bail_out;
} else {
retval = ConvertPythonRetvalTobRCRetval(pRetVal);
}
} else {
Dmsg(plugin_ctx, debuglevel,
LOGPREFIX "Failed to find function named end_backup_file()\n");
}
return retval;
bail_out:
if (PyErr_Occurred()) { PyErrorHandler(plugin_ctx, M_FATAL); }
return retval;
}
static inline PyIoPacket* NativeToPyIoPacket(io_pkt* io)
{
PyIoPacket* pIoPkt = PyObject_New(PyIoPacket, &PyIoPacketType);
if (pIoPkt) {
// Initialize the Python IoPkt with the data we got passed in.
pIoPkt->func = io->func;
pIoPkt->count = io->count;
pIoPkt->flags = io->flags;
pIoPkt->mode = io->mode;
pIoPkt->fname = io->fname;
pIoPkt->whence = io->whence;
pIoPkt->offset = io->offset;
pIoPkt->filedes = io->filedes;
if (io->func == IO_WRITE && io->count > 0) {
/* Only initialize the buffer with read data when we are writing and
* there is data.*/
pIoPkt->buf = PyByteArray_FromStringAndSize(io->buf, io->count);
if (!pIoPkt->buf) {
Py_DECREF((PyObject*)pIoPkt);
return (PyIoPacket*)NULL;
}
} else {
pIoPkt->buf = NULL;
}
/* These must be set by the Python function but we initialize them to zero
* to be sure they have some valid setting an not random data. */
pIoPkt->io_errno = 0;
pIoPkt->lerror = 0;
pIoPkt->win32 = false;
pIoPkt->status = 0;
}
return pIoPkt;
}
static inline bool PyIoPacketToNative(PyIoPacket* pIoPkt, io_pkt* io)
{
// Only copy back the arguments that are allowed to change.
io->io_errno = pIoPkt->io_errno;
io->lerror = pIoPkt->lerror;
io->win32 = pIoPkt->win32;
io->status = pIoPkt->status;
io->filedes = pIoPkt->filedes;
if (io->func == IO_READ && io->status > 0) {
// Only copy back the data when doing a read and there is data.
if (PyByteArray_Check(pIoPkt->buf)) {
char* buf;
if (PyByteArray_Size(pIoPkt->buf) > io->count || io->status > io->count) {
return false;
}
if (!(buf = PyByteArray_AsString(pIoPkt->buf))) { return false; }
memcpy(io->buf, buf, io->status);
}
}
return true;
}
/**
* Do actual I/O. Bareos calls this after startBackupFile
* or after startRestoreFile to do the actual file
* input or output.
*/
static bRC PyPluginIO(PluginContext* plugin_ctx, io_pkt* io)
{
bRC retval = bRC_Error;
struct plugin_private_context* plugin_priv_ctx
= (struct plugin_private_context*)plugin_ctx->plugin_private_context;
PyObject* pFunc;
// Lookup the plugin_io() function in the python module.
pFunc = PyDict_GetItemString(plugin_priv_ctx->pyModuleFunctionsDict,
"plugin_io"); /* Borrowed reference */
if (pFunc && PyCallable_Check(pFunc)) {
PyIoPacket* pIoPkt;
PyObject* pRetVal;
pIoPkt = NativeToPyIoPacket(io);
if (!pIoPkt) { goto bail_out; }
pRetVal = PyObject_CallFunctionObjArgs(pFunc, (PyObject*)pIoPkt, NULL);
if (!pRetVal) {
Py_DECREF((PyObject*)pIoPkt);
goto bail_out;
} else {
retval = ConvertPythonRetvalTobRCRetval(pRetVal);
Py_DECREF(pRetVal);
if (!PyIoPacketToNative(pIoPkt, io)) {
Py_DECREF((PyObject*)pIoPkt);
goto bail_out;
}
}
Py_DECREF((PyObject*)pIoPkt);
} else {
Dmsg(plugin_ctx, debuglevel,
LOGPREFIX "Failed to find function named plugin_io()\n");
}
return retval;
bail_out:
if (PyErr_Occurred()) { PyErrorHandler(plugin_ctx, M_FATAL); }
io->status = -1;
return retval;
}
/**
* Called when the first record is read from the Volume that was previously
* written by the command plugin.
*/
static bRC PyStartRestoreFile(PluginContext* plugin_ctx, const char* cmd)
{
bRC retval = bRC_Error;
struct plugin_private_context* plugin_priv_ctx
= (struct plugin_private_context*)plugin_ctx->plugin_private_context;
PyObject* pFunc;
// Lookup the start_restore_file() function in the python module.
pFunc = PyDict_GetItemString(plugin_priv_ctx->pyModuleFunctionsDict,
"start_restore_file"); /* Borrowed reference */
if (pFunc && PyCallable_Check(pFunc)) {
PyObject *pCmd, *pRetVal;
pCmd = PyUnicode_FromString(cmd);
if (!pCmd) { goto bail_out; }
pRetVal = PyObject_CallFunctionObjArgs(pFunc, pCmd, NULL);
Py_DECREF(pCmd);
if (!pRetVal) {
goto bail_out;
} else {
retval = ConvertPythonRetvalTobRCRetval(pRetVal);
Py_DECREF(pRetVal);
}
} else {
Dmsg(plugin_ctx, debuglevel,
LOGPREFIX "Failed to find function named start_restore_file()\n");
}
return retval;
bail_out:
if (PyErr_Occurred()) { PyErrorHandler(plugin_ctx, M_FATAL); }
return retval;
}
// Called when a command plugin is done restoring a file.
static bRC PyEndRestoreFile(PluginContext* plugin_ctx)
{
bRC retval = bRC_Error;
struct plugin_private_context* plugin_priv_ctx
= (struct plugin_private_context*)plugin_ctx->plugin_private_context;
PyObject* pFunc;
// Lookup the end_restore_file() function in the python module.
pFunc = PyDict_GetItemString(plugin_priv_ctx->pyModuleFunctionsDict,
"end_restore_file"); /* Borrowed reference */
if (pFunc && PyCallable_Check(pFunc)) {
PyObject* pRetVal;
pRetVal = PyObject_CallFunctionObjArgs(pFunc, NULL);
if (!pRetVal) {
goto bail_out;
} else {
retval = ConvertPythonRetvalTobRCRetval(pRetVal);
}
} else {
Dmsg(plugin_ctx, debuglevel,
LOGPREFIX "Failed to find function named end_restore_file()\n");
}
return retval;
bail_out:
if (PyErr_Occurred()) { PyErrorHandler(plugin_ctx, M_FATAL); }
return retval;
}
static inline PyRestorePacket* NativeToPyRestorePacket(restore_pkt* rp)
{
PyRestorePacket* pRestorePacket
= PyObject_New(PyRestorePacket, &PyRestorePacketType);
if (pRestorePacket) {
pRestorePacket->stream = rp->stream;
pRestorePacket->data_stream = rp->data_stream;
pRestorePacket->type = rp->type;
pRestorePacket->file_index = rp->file_index;
pRestorePacket->LinkFI = rp->LinkFI;
pRestorePacket->uid = rp->uid;
pRestorePacket->statp = (PyObject*)NativeToPyStatPacket(&rp->statp);
pRestorePacket->attrEx = rp->attrEx;
pRestorePacket->ofname = rp->ofname;
pRestorePacket->olname = rp->olname;
pRestorePacket->where = rp->where;
pRestorePacket->RegexWhere = rp->RegexWhere;
pRestorePacket->replace = rp->replace;
pRestorePacket->create_status = rp->create_status;
pRestorePacket->filedes = rp->filedes;
}
return pRestorePacket;
}
static inline void PyRestorePacketToNative(PyRestorePacket* pRestorePacket,
restore_pkt* rp)
{
// Only copy back the fields that are allowed to be changed.
rp->create_status = pRestorePacket->create_status;
rp->filedes = pRestorePacket->filedes;
}
/**
* Called for a command plugin to create a file during a Restore job before
* restoring the data. This entry point is called before any I/O is done on
* the file. After this call, Bareos will call pluginIO() to open the file for
* write.
*
* We must return in rp->create_status:
*
* 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)
*/
static bRC PyCreateFile(PluginContext* plugin_ctx, restore_pkt* rp)
{
bRC retval = bRC_Error;
struct plugin_private_context* plugin_priv_ctx
= (struct plugin_private_context*)plugin_ctx->plugin_private_context;
PyObject* pFunc;
if (!rp) { return bRC_Error; }
// Lookup the create_file() function in the python module.
pFunc = PyDict_GetItemString(plugin_priv_ctx->pyModuleFunctionsDict,
"create_file"); /* Borrowed reference */
if (pFunc && PyCallable_Check(pFunc)) {
PyRestorePacket* pRestorePacket;
PyObject* pRetVal;
pRestorePacket = NativeToPyRestorePacket(rp);
if (!pRestorePacket) { goto bail_out; }
pRetVal = PyObject_CallFunctionObjArgs(pFunc, pRestorePacket, NULL);
if (!pRetVal) {
Py_DECREF(pRestorePacket);
goto bail_out;
} else {
retval = ConvertPythonRetvalTobRCRetval(pRetVal);
Py_DECREF(pRetVal);
PyRestorePacketToNative(pRestorePacket, rp);
Py_DECREF(pRestorePacket);
}
} else {
Dmsg(plugin_ctx, debuglevel,
LOGPREFIX "Failed to find function named create_file()\n");
}
return retval;
bail_out:
if (PyErr_Occurred()) { PyErrorHandler(plugin_ctx, M_FATAL); }
return retval;
}
static bRC PySetFileAttributes(PluginContext* plugin_ctx, restore_pkt* rp)
{
bRC retval = bRC_Error;
struct plugin_private_context* plugin_priv_ctx
= (struct plugin_private_context*)plugin_ctx->plugin_private_context;
PyObject* pFunc;
if (!rp) { return bRC_Error; }
// Lookup the set_file_attributes() function in the python module.
pFunc = PyDict_GetItemString(plugin_priv_ctx->pyModuleFunctionsDict,
"set_file_attributes"); /* Borrowed reference */
if (pFunc && PyCallable_Check(pFunc)) {
PyRestorePacket* pRestorePacket;
PyObject* pRetVal;
pRestorePacket = NativeToPyRestorePacket(rp);
if (!pRestorePacket) { goto bail_out; }
pRetVal = PyObject_CallFunctionObjArgs(pFunc, pRestorePacket, NULL);
if (!pRetVal) {
Py_DECREF(pRestorePacket);
goto bail_out;
} else {
retval = ConvertPythonRetvalTobRCRetval(pRetVal);
Py_DECREF(pRetVal);
Py_DECREF(pRestorePacket);
}
} else {
Dmsg(plugin_ctx, debuglevel,
LOGPREFIX "Failed to find function named set_file_attributes()\n");
}
return retval;
bail_out:
if (PyErr_Occurred()) { PyErrorHandler(plugin_ctx, M_FATAL); }
return retval;
}
static bRC PyCheckFile(PluginContext* plugin_ctx, char* fname)
{
bRC retval = bRC_Error;
struct plugin_private_context* plugin_priv_ctx
= (struct plugin_private_context*)plugin_ctx->plugin_private_context;
PyObject* pFunc;
if (!fname) { return bRC_Error; }
// Lookup the check_file() function in the python module.
pFunc = PyDict_GetItemString(plugin_priv_ctx->pyModuleFunctionsDict,
"check_file"); /* Borrowed reference */
if (pFunc && PyCallable_Check(pFunc)) {
PyObject *pFname, *pRetVal;
pFname = PyUnicode_FromString(fname);
pRetVal = PyObject_CallFunctionObjArgs(pFunc, pFname, NULL);
Py_DECREF(pFname);
if (!pRetVal) {
goto bail_out;
} else {
retval = ConvertPythonRetvalTobRCRetval(pRetVal);
Py_DECREF(pRetVal);
}
} else {
Dmsg(plugin_ctx, debuglevel,
LOGPREFIX "Failed to find function named check_file()\n");
}
return retval;
bail_out:
if (PyErr_Occurred()) { PyErrorHandler(plugin_ctx, M_FATAL); }
return retval;
}
static inline PyAclPacket* NativeToPyAclPacket(acl_pkt* ap)
{
PyAclPacket* pAclPacket = PyObject_New(PyAclPacket, &PyAclPacketType);
if (pAclPacket) {
pAclPacket->fname = ap->fname;
if (ap->content_length && ap->content) {
pAclPacket->content
= PyByteArray_FromStringAndSize(ap->content, ap->content_length);
} else {
pAclPacket->content = NULL;
}
}
return pAclPacket;
}
static inline bool PyAclPacketToNative(PyAclPacket* pAclPacket, acl_pkt* ap)
{
if (!pAclPacket->content) { return true; }
if (PyByteArray_Check(pAclPacket->content)) {
char* buf;
ap->content_length = PyByteArray_Size(pAclPacket->content);
if (ap->content_length <= 0
|| !(buf = PyByteArray_AsString(pAclPacket->content))) {
return false;
}
if (ap->content) { free(ap->content); }
ap->content = (char*)malloc(ap->content_length + 1);
memcpy(ap->content, buf, ap->content_length + 1);
} else {
PyErr_SetString(PyExc_TypeError,
"acl packet content needs to be of bytearray type");
return false;
}
return true;
}
static bRC PyGetAcl(PluginContext* plugin_ctx, acl_pkt* ap)
{
bRC retval = bRC_Error;
struct plugin_private_context* plugin_priv_ctx
= (struct plugin_private_context*)plugin_ctx->plugin_private_context;
PyObject* pFunc;
if (!ap) { return bRC_Error; }
// Lookup the get_acl() function in the python module.
pFunc = PyDict_GetItemString(plugin_priv_ctx->pyModuleFunctionsDict,
"get_acl"); /* Borrowed reference */
if (pFunc && PyCallable_Check(pFunc)) {
PyAclPacket* pAclPkt;
PyObject* pRetVal;
pAclPkt = NativeToPyAclPacket(ap);
if (!pAclPkt) { goto bail_out; }
pRetVal = PyObject_CallFunctionObjArgs(pFunc, pAclPkt, NULL);
if (!pRetVal) {
Py_DECREF((PyObject*)pAclPkt);
goto bail_out;
} else {
retval = ConvertPythonRetvalTobRCRetval(pRetVal);
Py_DECREF(pRetVal);
if (!PyAclPacketToNative(pAclPkt, ap)) {
Py_DECREF((PyObject*)pAclPkt);
goto bail_out;
}
Py_DECREF(pAclPkt);
}
} else {
Dmsg(plugin_ctx, debuglevel,
LOGPREFIX "Failed to find function named get_acl()\n");
}
return retval;
bail_out:
if (PyErr_Occurred()) { PyErrorHandler(plugin_ctx, M_FATAL); }
return retval;
}
static bRC PySetAcl(PluginContext* plugin_ctx, acl_pkt* ap)
{
bRC retval = bRC_Error;
struct plugin_private_context* plugin_priv_ctx
= (struct plugin_private_context*)plugin_ctx->plugin_private_context;
PyObject* pFunc;
if (!ap) { return bRC_Error; }
// Lookup the set_acl() function in the python module.
pFunc = PyDict_GetItemString(plugin_priv_ctx->pyModuleFunctionsDict,
"set_acl"); /* Borrowed reference */
if (pFunc && PyCallable_Check(pFunc)) {
PyAclPacket* pAclPkt;
PyObject* pRetVal;
pAclPkt = NativeToPyAclPacket(ap);
if (!pAclPkt) { goto bail_out; }
pRetVal = PyObject_CallFunctionObjArgs(pFunc, pAclPkt, NULL);
Py_DECREF(pAclPkt);
if (!pRetVal) {
goto bail_out;
} else {
retval = ConvertPythonRetvalTobRCRetval(pRetVal);
Py_DECREF(pRetVal);
}
} else {
Dmsg(plugin_ctx, debuglevel,
LOGPREFIX "Failed to find function named set_acl()\n");
}
return retval;
bail_out:
if (PyErr_Occurred()) { PyErrorHandler(plugin_ctx, M_FATAL); }
return retval;
}
static inline PyXattrPacket* NativeToPyXattrPacket(xattr_pkt* xp)
{
PyXattrPacket* pXattrPacket = PyObject_New(PyXattrPacket, &PyXattrPacketType);
if (pXattrPacket) {
pXattrPacket->fname = xp->fname;
if (xp->name_length && xp->name) {
pXattrPacket->name
= PyByteArray_FromStringAndSize(xp->name, xp->name_length);
} else {
pXattrPacket->name = NULL;
}
if (xp->value_length && xp->value) {
pXattrPacket->value
= PyByteArray_FromStringAndSize(xp->value, xp->value_length);
} else {
pXattrPacket->value = NULL;
}
}
return pXattrPacket;
}
static inline bool PyXattrPacketToNative(PyXattrPacket* pXattrPacket,
xattr_pkt* xp)
{
if (!pXattrPacket->name) { return true; }