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