2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2014 Free Software Foundation Europe e.V.
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.
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.
14 Bacula® is a registered trademark of Kern Sibbald.
17 * A simple delta plugin for the Bacula File Daemon
22 #include "fd_plugins.h"
23 #include "fd_common.h"
33 static const int dbglvl = 0;
35 #define PLUGIN_LICENSE "Bacula AGPLv3"
36 #define PLUGIN_AUTHOR "Eric Bollengier"
37 #define PLUGIN_DATE "November 2010"
38 #define PLUGIN_VERSION "1"
39 #define PLUGIN_DESCRIPTION "Bacula Delta Test Plugin"
41 /* Forward referenced functions */
42 static bRC newPlugin(bpContext *ctx);
43 static bRC freePlugin(bpContext *ctx);
44 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
45 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
46 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
47 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
48 static bRC endBackupFile(bpContext *ctx);
49 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
50 static bRC startRestoreFile(bpContext *ctx, const char *cmd);
51 static bRC endRestoreFile(bpContext *ctx);
52 static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
53 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
55 /* Pointers to Bacula functions */
56 static bFuncs *bfuncs = NULL;
57 static bInfo *binfo = NULL;
59 /* Plugin Information block */
60 static pInfo pluginInfo = {
62 FD_PLUGIN_INTERFACE_VERSION,
71 /* Plugin entry points for Bacula */
72 static pFuncs pluginFuncs = {
74 FD_PLUGIN_INTERFACE_VERSION,
76 /* Entry points into plugin */
77 newPlugin, /* new plugin instance */
78 freePlugin, /* free plugin instance */
89 NULL /* no checkFile */
92 #define get_self(x) ((delta_test*)((x)->pContext))
93 #define FO_DELTA (1<<28) /* Do delta on file */
94 #define FO_OFFSETS (1<<30) /* Keep block offsets */
102 POOLMEM *fname; /* Filename to save */
108 delta_test(bpContext *bpc) {
114 fname = get_pool_memory(PM_FNAME);
117 free_and_null_pool_memory(fname);
122 * loadPlugin() and unloadPlugin() are entry points that are
123 * exported, so Bacula can directly call these two entry points
124 * they are common to all Bacula plugins.
127 * External entry point called by Bacula to "load the plugin
129 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
131 bfuncs = lbfuncs; /* set Bacula funct pointers */
133 *pinfo = &pluginInfo; /* return pointer to our info */
134 *pfuncs = &pluginFuncs; /* return pointer to our functions */
136 /* Activate this plugin only in developer mode */
145 * External entry point to unload the plugin
149 // Dmsg(NULL, dbglvl, "delta-test-fd: Unloaded\n");
154 * The following entry points are accessed through the function
155 * pointers we supplied to Bacula. Each plugin type (dir, fd, sd)
156 * has its own set of entry points that the plugin must define.
159 * Create a new instance of the plugin i.e. allocate our private storage
161 static bRC newPlugin(bpContext *ctx)
163 delta_test *self = new delta_test(ctx);
167 ctx->pContext = (void *)self; /* set our context pointer */
172 * Free a plugin instance, i.e. release our private storage
174 static bRC freePlugin(bpContext *ctx)
176 delta_test *self = get_self(ctx);
185 * Return some plugin value (none defined)
187 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value)
193 * Set a plugin value (none defined)
195 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value)
201 * Handle an event that was generated in Bacula
203 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
205 delta_test *self = get_self(ctx);
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.
219 switch (event->eventType) {
220 case bEventPluginCommand:
222 // "delta-test-fd: PluginCommand=%s\n", (char *)value);
225 // Dmsg(ctx, dbglvl, "delta-test-fd: JobStart=%s\n", (char *)value);
228 // Dmsg(ctx, dbglvl, "delta-test-fd: JobEnd\n");
230 case bEventStartBackupJob:
231 // Dmsg(ctx, dbglvl, "delta-test-fd: StartBackupJob\n");
233 case bEventEndBackupJob:
234 // Dmsg(ctx, dbglvl, "delta-test-fd: EndBackupJob\n");
237 // Dmsg(ctx, dbglvl, "delta-test-fd: JobLevel=%c %d\n", (int)value, (int)value);
238 self->level = (int)(intptr_t)value;
241 // Dmsg(ctx, dbglvl, "delta-test-fd: since=%d\n", (int)value);
244 case bEventStartRestoreJob:
245 // Dmsg(ctx, dbglvl, "delta-test-fd: StartRestoreJob\n");
248 case bEventEndRestoreJob:
249 // Dmsg(ctx, dbglvl, "delta-test-fd: EndRestoreJob\n");
252 /* Plugin command e.g. plugin = <plugin-name>:<name-space>:read command:write command */
253 case bEventRestoreCommand:
254 // Dmsg(ctx, dbglvl, "delta-test-fd: EventRestoreCommand cmd=%s\n", (char *)value);
255 /* Fall-through wanted */
257 case bEventBackupCommand:
258 Dmsg(ctx, dbglvl, "delta-test-fd: pluginEvent cmd=%s\n", (char *)value);
259 if (self->level == 'I' || self->level == 'D') {
260 bfuncs->getBaculaValue(ctx, bVarAccurate, (void *)&accurate);
261 if (!accurate) { /* can be changed to FATAL */
263 "Accurate mode should be turned on when using the "
264 "delta-test plugin\n");
271 // Dmsg(ctx, dbglvl, "delta-test-fd: unknown event=%d\n", event->eventType);
277 static const char *files[] = {
283 static int nb_files = 4;
286 * Start the backup of a specific file
288 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
290 delta_test *self = get_self(ctx);
294 time_t now = time(NULL);
295 sp->fname = (char *)"/delta.txt";
297 sp->statp.st_mode = 0700 | S_IFREG;
298 sp->statp.st_ctime = now;
299 sp->statp.st_mtime = now;
300 sp->statp.st_atime = now;
301 sp->statp.st_size = -1;
302 sp->statp.st_blksize = 4096;
303 sp->statp.st_blocks = 1;
304 if (self->level == 'I' || self->level == 'D') {
305 bRC state = bfuncs->checkChanges(ctx, sp);
306 /* Should always be bRC_OK */
307 sp->type = (state == bRC_Seen)? FT_NOCHG : FT_REG;
308 sp->flags |= (FO_DELTA|FO_OFFSETS);
309 self->delta = sp->delta_seq + 1;
311 pm_strcpy(self->fname, files[self->delta % nb_files]);
312 Dmsg(ctx, dbglvl, "delta-test-fd: delta_seq=%i delta=%i fname=%s\n",
313 sp->delta_seq, self->delta, self->fname);
314 // Dmsg(ctx, dbglvl, "delta-test-fd: startBackupFile\n");
319 * Done with backup of this file
321 static bRC endBackupFile(bpContext *ctx)
324 * We would return bRC_More if we wanted startBackupFile to be
325 * called again to backup another file
332 * Bacula is calling us to do the actual I/O
334 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
336 delta_test *self = get_self(ctx);
346 Dmsg(ctx, dbglvl, "delta-test-fd: IO_OPEN\n");
347 if (io->flags & (O_CREAT | O_WRONLY)) {
348 /* TODO: if the file already exists, the result is undefined */
349 if (stat(io->fname, &statp) == 0) { /* file exists */
350 self->fd = fopen(io->fname, "r+");
352 self->fd = fopen(io->fname, "w"); /* file doesn't exist,create it */
355 io->io_errno = errno;
357 "Open failed: ERR=%s\n", strerror(errno));
362 self->fd = fopen(self->fname, "r");
364 io->io_errno = errno;
366 "Open failed: ERR=%s\n", strerror(errno));
374 Jmsg(ctx, M_FATAL, "Logic error: NULL read FD\n");
380 /* first time, read 300, then replace 50-250 by other data */
381 if (self->delta == 0) {
382 io->status = fread(io->buf, 1, 400, self->fd);
384 io->offset = self->delta * 100 / 2; /* chunks are melted */
385 io->status = fread(io->buf, 1, 100, self->fd);
387 Dmsg(ctx, dbglvl, "delta-test-fd: READ offset=%lld\n", (int64_t)io->offset);
390 if (io->status == 0 && ferror(self->fd)) {
392 "Pipe read error: ERR=%s\n", strerror(errno));
394 "Pipe read error: ERR=%s\n", strerror(errno));
397 Dmsg(ctx, dbglvl, "offset=%d\n", io->offset);
402 Jmsg(ctx, M_FATAL, "Logic error: NULL write FD\n");
405 Dmsg(ctx, dbglvl, "delta-test-fd: WRITE count=%lld\n", (int64_t)io->count);
406 io->status = fwrite(io->buf, 1, io->count, self->fd);
407 if (io->status == 0 && ferror(self->fd)) {
409 "Pipe write error\n");
411 "Pipe read error: ERR=%s\n", strerror(errno));
418 Jmsg(ctx, M_FATAL, "Logic error: NULL FD on delta close\n");
421 io->status = fclose(self->fd);
426 Jmsg(ctx, M_FATAL, "Logic error: NULL FD on delta close\n");
429 Dmsg(ctx, dbglvl, "delta-test-fd: SEEK offset=%lld\n", (int64_t)io->offset);
430 io->status = fseek(self->fd, io->offset, io->whence);
431 Dmsg(ctx, dbglvl, "after SEEK=%lld\n", (int64_t)ftell(self->fd));
438 * Bacula is notifying us that a plugin name string was found, and
439 * passing us the plugin command, so we can prepare for a restore.
441 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
443 // Dmsg(ctx, dbglvl, "delta-test-fd: startRestoreFile cmd=%s\n", cmd);
448 * Bacula is notifying us that the plugin data has terminated, so
449 * the restore for this particular file is done.
451 static bRC endRestoreFile(bpContext *ctx)
453 // Dmsg(ctx, dbglvl, "delta-test-fd: endRestoreFile\n");
458 * This is called during restore to create the file (if necessary)
459 * We must return in rp->create_status:
462 * CF_SKIP -- skip processing this file
463 * CF_EXTRACT -- extract the file (i.e.call i/o routines)
464 * CF_CREATED -- created, but no content to extract (typically directories)
467 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
469 delta_test *self = get_self(ctx);
470 pm_strcpy(self->fname, rp->ofname);
471 rp->create_status = CF_EXTRACT;
476 * We will get here if the File is a directory after everything
477 * is written in the directory.
479 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
481 // Dmsg(ctx, dbglvl, "delta-test-fd: setFileAttributes\n");