1 /*
2    Bacula(R) - The Network Backup Solution
3 
4    Copyright (C) 2000-2020 Kern Sibbald
5 
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8 
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13 
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16 
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  * Common definitions and utility functions for Inteos plugins.
21  * Functions defines a common framework used in our utilities and plugins.
22  *
23  * Author: Radosław Korzeniewski, MMXIX
24  * radoslaw@korzeniewski.net, radekk@inteos.pl
25  * Inteos Sp. z o.o. http://www.inteos.pl/
26  */
27 
28 #ifndef _PLUGLIB_H_
29 #define _PLUGLIB_H_
30 
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <ctype.h>
34 
35 #include "bacula.h"
36 #include "fd_plugins.h"
37 
38 /* Pointers to Bacula functions used in plugins */
39 extern bFuncs *bfuncs;
40 extern bInfo *binfo;
41 
42 /* definitions */
43 /* size of different string or query buffers */
44 #define BUFLEN       4096
45 #define BIGBUFLEN    65536
46 
47 /* debug and messages functions */
48 #define JMSG0(ctx,type,msg) \
49       if (ctx) bfuncs->JobMessage ( ctx, __FILE__, __LINE__, type, 0, PLUGINPREFIX " " msg );
50 
51 #define JMSG1 JMSG
52 #define JMSG(ctx,type,msg,var) \
53       if (ctx) bfuncs->JobMessage ( ctx, __FILE__, __LINE__, type, 0, PLUGINPREFIX " " msg, var );
54 
55 #define JMSG2(ctx,type,msg,var1,var2) \
56       if (ctx) bfuncs->JobMessage ( ctx, __FILE__, __LINE__, type, 0, PLUGINPREFIX " " msg, var1, var2 );
57 
58 #define JMSG3(ctx,type,msg,var1,var2,var3) \
59       if (ctx) bfuncs->JobMessage ( ctx, __FILE__, __LINE__, type, 0, PLUGINPREFIX " " msg, var1, var2, var3 );
60 
61 #define JMSG4(ctx,type,msg,var1,var2,var3,var4) \
62       if (ctx) bfuncs->JobMessage ( ctx, __FILE__, __LINE__, type, 0, PLUGINPREFIX " " msg, var1, var2, var3, var4 );
63 
64 #define DMSG0(ctx,level,msg) \
65       if (ctx) bfuncs->DebugMessage ( ctx, __FILE__, __LINE__, level, PLUGINPREFIX " " msg );
66 
67 #define DMSG1 DMSG
68 #define DMSG(ctx,level,msg,var) \
69       if (ctx) bfuncs->DebugMessage ( ctx, __FILE__, __LINE__, level, PLUGINPREFIX " " msg, var );
70 
71 #define DMSG2(ctx,level,msg,var1,var2) \
72       if (ctx) bfuncs->DebugMessage ( ctx, __FILE__, __LINE__, level, PLUGINPREFIX " " msg, var1, var2 );
73 
74 #define DMSG3(ctx,level,msg,var1,var2,var3) \
75       if (ctx) bfuncs->DebugMessage ( ctx, __FILE__, __LINE__, level, PLUGINPREFIX " " msg, var1, var2, var3 );
76 
77 #define DMSG4(ctx,level,msg,var1,var2,var3,var4) \
78       if (ctx) bfuncs->DebugMessage ( ctx, __FILE__, __LINE__, level, PLUGINPREFIX " " msg, var1, var2, var3, var4 );
79 
80 #define DMSG6(ctx,level,msg,var1,var2,var3,var4,var5,var6) \
81       if (ctx) bfuncs->DebugMessage ( ctx, __FILE__, __LINE__, level, PLUGINPREFIX " " msg, var1, var2, var3, var4, var5, var6 );
82 
83 /* fixed debug level definitions */
84 #define D1  1                    /* debug for every error */
85 #define DERROR D1
86 #define D2  10                   /* debug only important stuff */
87 #define DINFO  D2
88 #define D3  200                  /* debug for information only */
89 #define DDEBUG D3
90 #define D4  800                  /* debug for detailed information only */
91 #define DVDEBUG D4
92 
93 #define getBaculaVar(bvar,val)  bfuncs->getBaculaValue(ctx, bvar, val);
94 
95 /* used for sanity check in plugin functions */
96 #define ASSERT_CTX \
97     if (!ctx || !ctx->pContext || !bfuncs) \
98     { \
99         return bRC_Error; \
100     }
101 
102 /* defines for handleEvent */
103 #define DMSG_EVENT_STR(event,value)       DMSG2(ctx, DINFO, "%s value=%s\n", eventtype2str(event), NPRT((char *)value));
104 #define DMSG_EVENT_CHAR(event,value)      DMSG2(ctx, DINFO, "%s value='%c'\n", eventtype2str(event), (char)value);
105 #define DMSG_EVENT_LONG(event,value)      DMSG2(ctx, DINFO, "%s value=%ld\n", eventtype2str(event), (intptr_t)value);
106 #define DMSG_EVENT_PTR(event,value)       DMSG2(ctx, DINFO, "%s value=%p\n", eventtype2str(event), value);
107 
108 const char *eventtype2str(bEvent *event);
109 uint64_t pluglib_size_suffix(int disksize, char suff);
110 uint64_t pluglib_size_suffix(double disksize, char suff);
111 bRC pluglib_mkpath(bpContext* ctx, char* path, bool isfatal);
112 
113 /*
114  * Checks if plugin command points to our Plugin
115  *
116  * in:
117  *    command - the plugin command used for backup/restore
118  * out:
119  *    True - if it is our plugin command
120  *    False - the other plugin command
121  */
isourplugincommand(const char * pluginprefix,const char * command)122 inline bool isourplugincommand(const char *pluginprefix, const char *command)
123 {
124    /* check if it is our Plugin command */
125    if (strncmp(pluginprefix, command, strlen(pluginprefix)) == 0){
126       /* it is not our plugin prefix */
127       return true;
128    }
129    return false;
130 }
131 
132 #endif                           /* _PLUGLIB_H_ */
133