]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/plugins/fd/test-plugin-fd.c
Switch from GPLv2 to AGPLv3
[bacula/bacula] / bacula / src / plugins / fd / test-plugin-fd.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2010 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation, which is 
11    listed in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  * A simple test plugin for the Bacula File Daemon derived from
30  *   the bpipe plugin, but used for testing new features.
31  *
32  *  Kern Sibbald, October 2007
33  *
34  */
35 #include "bacula.h"
36 #include "fd_plugins.h"
37
38 #undef malloc
39 #undef free
40 #undef strdup
41
42 #define fi __FILE__
43 #define li __LINE__
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 static const int dbglvl = 000;
50
51 #define PLUGIN_LICENSE      "Bacula AGPLv3"
52 #define PLUGIN_AUTHOR       "Kern Sibbald"
53 #define PLUGIN_DATE         "April 2010"
54 #define PLUGIN_VERSION      "1"
55 #define PLUGIN_DESCRIPTION  "Bacula Test File Daemon Plugin"
56
57 /* Forward referenced functions */
58 static bRC newPlugin(bpContext *ctx);
59 static bRC freePlugin(bpContext *ctx);
60 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
61 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
62 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
63 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
64 static bRC endBackupFile(bpContext *ctx);
65 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
66 static bRC startRestoreFile(bpContext *ctx, const char *cmd);
67 static bRC endRestoreFile(bpContext *ctx);
68 static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
69 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
70
71 /* Pointers to Bacula functions */
72 static bFuncs *bfuncs = NULL;
73 static bInfo  *binfo = NULL;
74
75 /* Plugin Information block */
76 static pInfo pluginInfo = {
77    sizeof(pluginInfo),
78    FD_PLUGIN_INTERFACE_VERSION,
79    FD_PLUGIN_MAGIC,
80    PLUGIN_LICENSE,
81    PLUGIN_AUTHOR,
82    PLUGIN_DATE,
83    PLUGIN_VERSION,
84    PLUGIN_DESCRIPTION,
85 };
86
87 /* Plugin entry points for Bacula */
88 static pFuncs pluginFuncs = {
89    sizeof(pluginFuncs),
90    FD_PLUGIN_INTERFACE_VERSION,
91
92    /* Entry points into plugin */
93    newPlugin,                         /* new plugin instance */
94    freePlugin,                        /* free plugin instance */
95    getPluginValue,
96    setPluginValue,
97    handlePluginEvent,
98    startBackupFile,
99    endBackupFile,
100    startRestoreFile,
101    endRestoreFile,
102    pluginIO,
103    createFile,
104    setFileAttributes
105 };
106
107 /*
108  * Plugin private context
109  */
110 struct plugin_ctx {
111    boffset_t offset;
112    FILE *fd;                          /* pipe file descriptor */
113    bool backup;                       /* set for backup (not needed) */
114    char *cmd;                         /* plugin command line */
115    char *fname;                       /* filename to "backup/restore" */
116    char *reader;                      /* reader program for backup */
117    char *writer;                      /* writer program for backup */
118
119    char where[512];
120    int replace;
121 };
122
123 /*
124  * loadPlugin() and unloadPlugin() are entry points that are
125  *  exported, so Bacula can directly call these two entry points
126  *  they are common to all Bacula plugins.
127  */
128 /*
129  * External entry point called by Bacula to "load the plugin
130  */
131 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
132 {
133    bfuncs = lbfuncs;                  /* set Bacula funct pointers */
134    binfo  = lbinfo;
135    *pinfo  = &pluginInfo;             /* return pointer to our info */
136    *pfuncs = &pluginFuncs;            /* return pointer to our functions */
137
138    return bRC_OK;
139 }
140
141 /*
142  * External entry point to unload the plugin 
143  */
144 bRC unloadPlugin() 
145 {
146 // printf("test-plugin-fd: Unloaded\n");
147    return bRC_OK;
148 }
149
150 /*
151  * The following entry points are accessed through the function 
152  *   pointers we supplied to Bacula. Each plugin type (dir, fd, sd)
153  *   has its own set of entry points that the plugin must define.
154  */
155 /*
156  * Create a new instance of the plugin i.e. allocate our private storage
157  */
158 static bRC newPlugin(bpContext *ctx)
159 {
160    struct plugin_ctx *p_ctx = (struct plugin_ctx *)malloc(sizeof(struct plugin_ctx));
161    if (!p_ctx) {
162       return bRC_Error;
163    }
164    memset(p_ctx, 0, sizeof(struct plugin_ctx));
165    ctx->pContext = (void *)p_ctx;        /* set our context pointer */
166    return bRC_OK;
167 }
168
169 /*
170  * Free a plugin instance, i.e. release our private storage
171  */
172 static bRC freePlugin(bpContext *ctx)
173 {
174    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
175    if (!p_ctx) {
176       return bRC_Error;
177    }
178    if (p_ctx->cmd) {
179       free(p_ctx->cmd);                  /* free any allocated command string */
180    }
181    free(p_ctx);                          /* free our private context */
182    p_ctx = NULL;
183    return bRC_OK;
184 }
185
186 /*
187  * Return some plugin value (none defined)
188  */
189 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value) 
190 {
191    return bRC_OK;
192 }
193
194 /*
195  * Set a plugin value (none defined)
196  */
197 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value) 
198 {
199    return bRC_OK;
200 }
201
202 /*
203  * Handle an event that was generated in Bacula
204  */
205 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
206 {
207    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
208    restore_object_pkt *rop;
209    if (!p_ctx) {
210       return bRC_Error;
211    }
212
213 // char *name;
214
215    /*
216     * Most events don't interest us so we ignore them.
217     *   the printfs are so that plugin writers can enable them to see
218     *   what is really going on.
219     */
220    switch (event->eventType) {
221    case bEventJobStart:
222       bfuncs->DebugMessage(ctx, fi, li, dbglvl, "test-plugin-fd: JobStart=%s\n", (char *)value);
223       break;
224    case bEventJobEnd:
225    case bEventEndBackupJob:
226    case bEventLevel:
227    case bEventSince:
228    case bEventStartRestoreJob:
229    case bEventEndRestoreJob:
230       break;
231    /* End of Dir FileSet commands, now we can add excludes */
232    case bEventEndFileSet:
233       bfuncs->NewOptions(ctx);
234       bfuncs->AddWild(ctx, "*.c", ' ');
235       bfuncs->AddWild(ctx, "*.cpp", ' ');
236       bfuncs->AddOptions(ctx, "ei");         /* exclude, ignore case */
237       bfuncs->AddExclude(ctx, "/home/kern/bacula/regress/README");
238       break;
239    case bEventStartBackupJob:
240       break;
241    case bEventRestoreObject:
242       printf("Plugin RestoreObject\n");
243       if (!value) {
244          bfuncs->DebugMessage(ctx, fi, li, dbglvl, "test-plugin-fd: End restore objects\n");
245          break;
246       }
247       rop = (restore_object_pkt *)value;
248       bfuncs->DebugMessage(ctx, fi, li, dbglvl, "test-plugin-fd: len=%d JobId=%d oname=%s\n",
249          rop->object_len, rop->JobId, rop->object_name);
250       break;
251    /* Plugin command e.g. plugin = <plugin-name>:<name-space>:read command:write command */
252    case bEventRestoreCommand:
253       /* Fall-through wanted */
254    case bEventBackupCommand:
255       char *p;
256       bfuncs->DebugMessage(ctx, fi, li, dbglvl, "test-plugin-fd: pluginEvent cmd=%s\n", (char *)value);
257       p_ctx->cmd = strdup((char *)value);
258       p = strchr(p_ctx->cmd, ':');
259       if (!p) {
260          bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Plugin terminator not found: %s\n", (char *)value);
261          return bRC_Error;
262       }
263       *p++ = 0;           /* terminate plugin */
264       p_ctx->fname = p;
265       p = strchr(p, ':');
266       if (!p) {
267          bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "File terminator not found: %s\n", (char *)value);
268          return bRC_Error;
269       }
270       *p++ = 0;           /* terminate file */
271       p_ctx->reader = p;
272       p = strchr(p, ':');
273       if (!p) {
274          bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Reader terminator not found: %s\n", (char *)value);
275          return bRC_Error;
276       }
277       *p++ = 0;           /* terminate reader string */
278       p_ctx->writer = p;
279       printf("test-plugin-fd: plugin=%s fname=%s reader=%s writer=%s\n", 
280           p_ctx->cmd, p_ctx->fname, p_ctx->reader, p_ctx->writer);
281       break;
282
283    default:
284       printf("test-plugin-fd: unknown event=%d\n", event->eventType);
285       break;
286    }
287    return bRC_OK;
288 }
289
290 /* 
291  * Start the backup of a specific file
292  */
293 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
294 {
295    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
296    if (!p_ctx) {
297       return bRC_Error;
298    }
299    time_t now = time(NULL);
300    sp->object_name = (char *)"james.xml";
301    sp->object = (char *)"This is test data for the restore object.";
302    sp->object_len = strlen(sp->object);
303    sp->index = 2;
304    sp->type = FT_RESTORE_FIRST;
305    sp->statp.st_mode = 0700 | S_IFREG;
306    sp->statp.st_ctime = now;
307    sp->statp.st_mtime = now;
308    sp->statp.st_atime = now;
309    sp->statp.st_size = sp->object_len;
310    sp->statp.st_blksize = 4096;
311    sp->statp.st_blocks = 1;
312    p_ctx->backup = true;
313    printf("test-plugin-fd: startBackupFile\n");
314    return bRC_OK;
315 }
316
317 /*
318  * Done with backup of this file
319  */
320 static bRC endBackupFile(bpContext *ctx)
321 {
322    /*
323     * We would return bRC_More if we wanted startBackupFile to be
324     * called again to backup another file
325     */
326    return bRC_OK;
327 }
328
329
330 /*
331  * Bacula is calling us to do the actual I/O
332  */
333 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
334 {
335    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
336    if (!p_ctx) {
337       return bRC_Error;
338    }
339     
340    io->status = 0;
341    io->io_errno = 0;
342    return bRC_OK;
343 }
344
345 /*
346  * Bacula is notifying us that a plugin name string was found, and
347  *   passing us the plugin command, so we can prepare for a restore.
348  */
349 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
350 {
351    printf("test-plugin-fd: startRestoreFile cmd=%s\n", cmd);
352    return bRC_OK;
353 }
354
355 /*
356  * Bacula is notifying us that the plugin data has terminated, so
357  *  the restore for this particular file is done.
358  */
359 static bRC endRestoreFile(bpContext *ctx)
360 {
361    printf("test-plugin-fd: endRestoreFile\n");
362    return bRC_OK;
363 }
364
365 /*
366  * This is called during restore to create the file (if necessary)
367  * We must return in rp->create_status:
368  *   
369  *  CF_ERROR    -- error
370  *  CF_SKIP     -- skip processing this file
371  *  CF_EXTRACT  -- extract the file (i.e.call i/o routines)
372  *  CF_CREATED  -- created, but no content to extract (typically directories)
373  *
374  */
375 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
376 {
377    printf("test-plugin-fd: createFile\n");
378    if (strlen(rp->where) > 512) {
379       printf("Restore target dir too long. Restricting to first 512 bytes.\n");
380    }
381    strncpy(((struct plugin_ctx *)ctx->pContext)->where, rp->where, 513);
382    ((struct plugin_ctx *)ctx->pContext)->replace = rp->replace;
383    rp->create_status = CF_EXTRACT;
384    return bRC_OK;
385 }
386
387 /*
388  * We will get here if the File is a directory after everything
389  * is written in the directory.
390  */
391 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
392 {
393    printf("test-plugin-fd: setFileAttributes\n");
394    return bRC_OK;
395 }
396
397
398 #ifdef __cplusplus
399 }
400 #endif