]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/plugins/fd/example-plugin-fd.c
b3f2dfdcb1be746654577bc4716d345cec033ea8
[bacula/bacula] / bacula / src / plugins / fd / example-plugin-fd.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 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 #define BUILD_PLUGIN
20 #define BUILDING_DLL            /* required for Windows plugin */
21
22 #include "bacula.h"
23 #include "fd_plugins.h"
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #define PLUGIN_LICENSE      "AGPLv3"
30 #define PLUGIN_AUTHOR       "Your name"
31 #define PLUGIN_DATE         "January 2010"
32 #define PLUGIN_VERSION      "1"
33 #define PLUGIN_DESCRIPTION  "Test File Daemon Plugin"
34
35 /* Forward referenced functions */
36 static bRC newPlugin(bpContext *ctx);
37 static bRC freePlugin(bpContext *ctx);
38 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
39 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
40 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
41 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
42 static bRC endBackupFile(bpContext *ctx);
43 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
44 static bRC startRestoreFile(bpContext *ctx, const char *cmd);
45 static bRC endRestoreFile(bpContext *ctx);
46 static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
47 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
48 static bRC checkFile(bpContext *ctx, char *fname);
49
50
51 /* Pointers to Bacula functions */
52 static bFuncs *bfuncs = NULL;
53 static bInfo  *binfo = NULL;
54
55 static pInfo pluginInfo = {
56    sizeof(pluginInfo),
57    FD_PLUGIN_INTERFACE_VERSION,
58    FD_PLUGIN_MAGIC,
59    PLUGIN_LICENSE,
60    PLUGIN_AUTHOR,
61    PLUGIN_DATE,
62    PLUGIN_VERSION,
63    PLUGIN_DESCRIPTION,
64 };
65
66 static pFuncs pluginFuncs = {
67    sizeof(pluginFuncs),
68    FD_PLUGIN_INTERFACE_VERSION,
69
70    /* Entry points into plugin */
71    newPlugin,                         /* new plugin instance */
72    freePlugin,                        /* free plugin instance */
73    getPluginValue,
74    setPluginValue,
75    handlePluginEvent,
76    startBackupFile,
77    endBackupFile,
78    startRestoreFile,
79    endRestoreFile,
80    pluginIO,
81    createFile,
82    setFileAttributes,
83    checkFile
84 };
85
86 /*
87  * Plugin called here when it is first loaded
88  */
89 bRC DLL_IMP_EXP
90 loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
91 {
92    bfuncs = lbfuncs;                  /* set Bacula funct pointers */
93    binfo  = lbinfo;
94    printf("plugin: Loaded: size=%d version=%d\n", bfuncs->size, bfuncs->version);
95
96    *pinfo  = &pluginInfo;             /* return pointer to our info */
97    *pfuncs = &pluginFuncs;            /* return pointer to our functions */
98
99    return bRC_OK;
100 }
101
102 /*
103  * Plugin called here when it is unloaded, normally when
104  *  Bacula is going to exit.
105  */
106 bRC DLL_IMP_EXP
107 unloadPlugin() 
108 {
109    printf("plugin: Unloaded\n");
110    return bRC_OK;
111 }
112
113 /*
114  * Called here to make a new instance of the plugin -- i.e. when
115  *  a new Job is started.  There can be multiple instances of
116  *  each plugin that are running at the same time.  Your
117  *  plugin instance must be thread safe and keep its own
118  *  local data.
119  */
120 static bRC newPlugin(bpContext *ctx)
121 {
122    int JobId = 0;
123    bfuncs->getBaculaValue(ctx, bVarJobId, (void *)&JobId);
124 // printf("plugin: newPlugin JobId=%d\n", JobId);
125    bfuncs->registerBaculaEvents(ctx, 1, 2, 0);
126    return bRC_OK;
127 }
128
129 /*
130  * Release everything concerning a particular instance of a 
131  *  plugin. Normally called when the Job terminates.
132  */
133 static bRC freePlugin(bpContext *ctx)
134 {
135    int JobId = 0;
136    bfuncs->getBaculaValue(ctx, bVarJobId, (void *)&JobId);
137 // printf("plugin: freePlugin JobId=%d\n", JobId);
138    return bRC_OK;
139 }
140
141 /*
142  * Called by core code to get a variable from the plugin.
143  *   Not currently used.
144  */
145 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value) 
146 {
147 // printf("plugin: getPluginValue var=%d\n", var);
148    return bRC_OK;
149 }
150
151 /* 
152  * Called by core code to set a plugin variable.
153  *  Not currently used.
154  */
155 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value) 
156 {
157 // printf("plugin: setPluginValue var=%d\n", var);
158    return bRC_OK;
159 }
160
161 /*
162  * Called by Bacula when there are certain events that the
163  *   plugin might want to know.  The value depends on the
164  *   event.
165  */
166 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
167 {
168    char *name;
169
170    switch (event->eventType) {
171    case bEventJobStart:
172       printf("plugin: JobStart=%s\n", NPRT((char *)value));
173       break;
174    case bEventJobEnd:
175       printf("plugin: JobEnd\n");
176       break;
177    case bEventStartBackupJob:
178       printf("plugin: BackupStart\n");
179       break;
180    case bEventEndBackupJob:
181       printf("plugin: BackupEnd\n");
182       break;
183    case bEventLevel:
184       printf("plugin: JobLevel=%c %d\n", (int64_t)value, (int64_t)value);
185       break;
186    case bEventSince:
187       printf("plugin: since=%d\n", (int64_t)value);
188       break;
189    case bEventStartRestoreJob:
190       printf("plugin: StartRestoreJob\n");
191       break;
192    case bEventEndRestoreJob:
193       printf("plugin: EndRestoreJob\n");
194       break;
195    /* Plugin command e.g. plugin = <plugin-name>:<name-space>:command */
196    case bEventRestoreCommand:
197       printf("plugin: backup command=%s\n", NPRT((char *)value));
198       break;
199    case bEventBackupCommand:
200       printf("plugin: backup command=%s\n", NPRT((char *)value));
201       break;
202
203    case bEventComponentInfo:
204       printf("plugin: Component=%s\n", NPRT((char *)value));
205       break;
206
207    default:
208       printf("plugin: unknown event=%d\n", event->eventType);
209    }
210    bfuncs->getBaculaValue(ctx, bVarFDName, (void *)&name);
211 // printf("FD Name=%s\n", name);
212 // bfuncs->JobMessage(ctx, __FILE__, __LINE__, 1, 0, "JobMesssage message");
213 // bfuncs->DebugMessage(ctx, __FILE__, __LINE__, 1, "DebugMesssage message");
214    return bRC_OK;
215 }
216
217 /*
218  * Called when starting to backup a file.  Here the plugin must
219  *  return the "stat" packet for the directory/file and provide
220  *  certain information so that Bacula knows what the file is.
221  *  The plugin can create "Virtual" files by giving them a
222  *  name that is not normally found on the file system.
223  */
224 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
225 {
226    return bRC_OK;
227 }
228
229 /*
230  * Done backing up a file.
231  */
232 static bRC endBackupFile(bpContext *ctx)
233
234    return bRC_OK;
235 }
236
237 /*
238  * Do actual I/O.  Bacula calls this after startBackupFile
239  *   or after startRestoreFile to do the actual file 
240  *   input or output.
241  */
242 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
243 {
244    io->status = 0;
245    io->io_errno = 0;
246    switch(io->func) {
247    case IO_OPEN:
248       printf("plugin: IO_OPEN\n");
249       break;
250    case IO_READ:
251       printf("plugin: IO_READ buf=%p len=%d\n", io->buf, io->count);
252       break;
253    case IO_WRITE:
254       printf("plugin: IO_WRITE buf=%p len=%d\n", io->buf, io->count);
255       break;
256    case IO_CLOSE:
257       printf("plugin: IO_CLOSE\n");
258       break;
259    }
260    return bRC_OK;
261 }
262
263 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
264 {
265    return bRC_OK;
266 }
267
268 static bRC endRestoreFile(bpContext *ctx)
269 {
270    return bRC_OK;
271 }
272
273 /*
274  * Called here to give the plugin the information needed to
275  *  re-create the file on a restore.  It basically gets the
276  *  stat packet that was created during the backup phase.
277  *  This data is what is needed to create the file, but does
278  *  not contain actual file data.
279  */
280 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
281 {
282    return bRC_OK;
283 }
284
285 /*
286  * Called after the file has been restored. This can be used to
287  *  set directory permissions, ...
288  */
289 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
290 {
291    return bRC_OK;
292 }
293
294 /* When using Incremental dump, all previous dumps are necessary */
295 static bRC checkFile(bpContext *ctx, char *fname)
296 {
297    return bRC_OK;
298 }
299
300
301 #ifdef __cplusplus
302 }
303 #endif