]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/plugins/fd/test-plugin-fd.c
358352641b2b8b72f226f76725b2b5c50542b4da
[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 two of the GNU 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 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 = 150;
50
51 #define PLUGIN_LICENSE      "Bacula GPLv2"
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    if (!p_ctx) {
209       return bRC_Error;
210    }
211
212 // char *name;
213
214    /*
215     * Most events don't interest us so we ignore them.
216     *   the printfs are so that plugin writers can enable them to see
217     *   what is really going on.
218     */
219    switch (event->eventType) {
220    case bEventJobStart:
221       bfuncs->DebugMessage(ctx, fi, li, dbglvl, "test-plugin-fd: JobStart=%s\n", (char *)value);
222       break;
223    case bEventJobEnd:
224    case bEventEndBackupJob:
225    case bEventLevel:
226    case bEventSince:
227    case bEventStartRestoreJob:
228    case bEventEndRestoreJob:
229       break;
230    case bEventStartBackupJob:
231       bfuncs->AddExclude(ctx, "/home/kern/bacula/regress/README");
232       break;
233    /* Plugin command e.g. plugin = <plugin-name>:<name-space>:read command:write command */
234    case bEventRestoreCommand:
235       /* Fall-through wanted */
236    case bEventBackupCommand:
237       char *p;
238       bfuncs->DebugMessage(ctx, fi, li, dbglvl, "test-plugin-fd: pluginEvent cmd=%s\n", (char *)value);
239       p_ctx->cmd = strdup((char *)value);
240       p = strchr(p_ctx->cmd, ':');
241       if (!p) {
242          bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Plugin terminator not found: %s\n", (char *)value);
243          return bRC_Error;
244       }
245       *p++ = 0;           /* terminate plugin */
246       p_ctx->fname = p;
247       p = strchr(p, ':');
248       if (!p) {
249          bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "File terminator not found: %s\n", (char *)value);
250          return bRC_Error;
251       }
252       *p++ = 0;           /* terminate file */
253       p_ctx->reader = p;
254       p = strchr(p, ':');
255       if (!p) {
256          bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Reader terminator not found: %s\n", (char *)value);
257          return bRC_Error;
258       }
259       *p++ = 0;           /* terminate reader string */
260       p_ctx->writer = p;
261       printf("test-plugin-fd: plugin=%s fname=%s reader=%s writer=%s\n", 
262           p_ctx->cmd, p_ctx->fname, p_ctx->reader, p_ctx->writer);
263       break;
264
265    default:
266       printf("test-plugin-fd: unknown event=%d\n", event->eventType);
267       break;
268    }
269    return bRC_OK;
270 }
271
272 /* 
273  * Start the backup of a specific file
274  */
275 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
276 {
277    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
278    if (!p_ctx) {
279       return bRC_Error;
280    }
281    time_t now = time(NULL);
282    sp->fname = p_ctx->fname;
283    sp->type = FT_RESTORE_FIRST;
284    sp->statp.st_mode = 0700 | S_IFREG;
285    sp->statp.st_ctime = now;
286    sp->statp.st_mtime = now;
287    sp->statp.st_atime = now;
288    sp->statp.st_size = -1;
289    sp->statp.st_blksize = 4096;
290    sp->statp.st_blocks = 1;
291    sp->object = (char *)"This is test data for the restore object.";
292    sp->object_len = strlen(sp->object);
293    sp->index = 2;
294    p_ctx->backup = true;
295    printf("test-plugin-fd: startBackupFile\n");
296    return bRC_OK;
297 }
298
299 /*
300  * Done with backup of this file
301  */
302 static bRC endBackupFile(bpContext *ctx)
303 {
304    /*
305     * We would return bRC_More if we wanted startBackupFile to be
306     * called again to backup another file
307     */
308    return bRC_OK;
309 }
310
311
312 /*
313  * Bacula is calling us to do the actual I/O
314  */
315 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
316 {
317    printf("test-plugin-fd: pluginIO\n");
318    return bRC_Error;
319 }
320
321 /*
322  * Bacula is notifying us that a plugin name string was found, and
323  *   passing us the plugin command, so we can prepare for a restore.
324  */
325 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
326 {
327    printf("test-plugin-fd: startRestoreFile cmd=%s\n", cmd);
328    return bRC_OK;
329 }
330
331 /*
332  * Bacula is notifying us that the plugin data has terminated, so
333  *  the restore for this particular file is done.
334  */
335 static bRC endRestoreFile(bpContext *ctx)
336 {
337    printf("test-plugin-fd: endRestoreFile\n");
338    return bRC_OK;
339 }
340
341 /*
342  * This is called during restore to create the file (if necessary)
343  * We must return in rp->create_status:
344  *   
345  *  CF_ERROR    -- error
346  *  CF_SKIP     -- skip processing this file
347  *  CF_EXTRACT  -- extract the file (i.e.call i/o routines)
348  *  CF_CREATED  -- created, but no content to extract (typically directories)
349  *
350  */
351 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
352 {
353    printf("test-plugin-fd: createFile\n");
354    if (strlen(rp->where) > 512) {
355       printf("Restore target dir too long. Restricting to first 512 bytes.\n");
356    }
357    strncpy(((struct plugin_ctx *)ctx->pContext)->where, rp->where, 513);
358    ((struct plugin_ctx *)ctx->pContext)->replace = rp->replace;
359    rp->create_status = CF_EXTRACT;
360    return bRC_OK;
361 }
362
363 /*
364  * We will get here if the File is a directory after everything
365  * is written in the directory.
366  */
367 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
368 {
369    printf("test-plugin-fd: setFileAttributes\n");
370    return bRC_OK;
371 }
372
373
374 #ifdef __cplusplus
375 }
376 #endif