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