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