2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2015 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many 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 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * A simple delta plugin for the Bacula File Daemon
25 #include "fd_plugins.h"
26 #include "fd_common.h"
36 static const int dbglvl = 0;
38 #define PLUGIN_LICENSE "AGPLv3"
39 #define PLUGIN_AUTHOR "Eric Bollengier"
40 #define PLUGIN_DATE "November 2010"
41 #define PLUGIN_VERSION "1"
42 #define PLUGIN_DESCRIPTION "Bacula Delta Test Plugin"
44 /* Forward referenced functions */
45 static bRC newPlugin(bpContext *ctx);
46 static bRC freePlugin(bpContext *ctx);
47 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
48 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
49 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
50 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
51 static bRC endBackupFile(bpContext *ctx);
52 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
53 static bRC startRestoreFile(bpContext *ctx, const char *cmd);
54 static bRC endRestoreFile(bpContext *ctx);
55 static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
56 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
57 static bRC checkFile(bpContext *ctx, char *fname);
58 static bRC handleXACLdata(bpContext *ctx, struct xacl_pkt *xacl);
60 /* Pointers to Bacula functions */
61 static bFuncs *bfuncs = NULL;
62 static bInfo *binfo = NULL;
64 /* Plugin Information block */
65 static pInfo pluginInfo = {
67 FD_PLUGIN_INTERFACE_VERSION,
76 /* Plugin entry points for Bacula */
77 static pFuncs pluginFuncs = {
79 FD_PLUGIN_INTERFACE_VERSION,
81 /* Entry points into plugin */
82 newPlugin, /* new plugin instance */
83 freePlugin, /* free plugin instance */
98 #define get_self(x) ((delta_test*)((x)->pContext))
99 #define FO_DELTA (1<<28) /* Do delta on file */
100 #define FO_OFFSETS (1<<30) /* Keep block offsets */
108 POOLMEM *fname; /* Filename to save */
114 delta_test(bpContext *bpc) {
120 fname = get_pool_memory(PM_FNAME);
123 free_and_null_pool_memory(fname);
128 * loadPlugin() and unloadPlugin() are entry points that are
129 * exported, so Bacula can directly call these two entry points
130 * they are common to all Bacula plugins.
133 * External entry point called by Bacula to "load the plugin
135 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
137 bfuncs = lbfuncs; /* set Bacula funct pointers */
139 *pinfo = &pluginInfo; /* return pointer to our info */
140 *pfuncs = &pluginFuncs; /* return pointer to our functions */
142 /* Activate this plugin only in developer mode */
151 * External entry point to unload the plugin
155 // Dmsg(NULL, dbglvl, "delta-test-fd: Unloaded\n");
160 * The following entry points are accessed through the function
161 * pointers we supplied to Bacula. Each plugin type (dir, fd, sd)
162 * has its own set of entry points that the plugin must define.
165 * Create a new instance of the plugin i.e. allocate our private storage
167 static bRC newPlugin(bpContext *ctx)
169 delta_test *self = new delta_test(ctx);
173 ctx->pContext = (void *)self; /* set our context pointer */
178 * Free a plugin instance, i.e. release our private storage
180 static bRC freePlugin(bpContext *ctx)
182 delta_test *self = get_self(ctx);
191 * Return some plugin value (none defined)
193 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value)
199 * Set a plugin value (none defined)
201 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value)
207 * Handle an event that was generated in Bacula
209 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
211 delta_test *self = get_self(ctx);
221 * Most events don't interest us so we ignore them.
222 * the printfs are so that plugin writers can enable them to see
223 * what is really going on.
225 switch (event->eventType) {
226 case bEventPluginCommand:
228 // "delta-test-fd: PluginCommand=%s\n", (char *)value);
231 // Dmsg(ctx, dbglvl, "delta-test-fd: JobStart=%s\n", (char *)value);
234 // Dmsg(ctx, dbglvl, "delta-test-fd: JobEnd\n");
236 case bEventStartBackupJob:
237 // Dmsg(ctx, dbglvl, "delta-test-fd: StartBackupJob\n");
239 case bEventEndBackupJob:
240 // Dmsg(ctx, dbglvl, "delta-test-fd: EndBackupJob\n");
243 // Dmsg(ctx, dbglvl, "delta-test-fd: JobLevel=%c %d\n", (int)value, (int)value);
244 self->level = (int)(intptr_t)value;
247 // Dmsg(ctx, dbglvl, "delta-test-fd: since=%d\n", (int)value);
250 case bEventStartRestoreJob:
251 // Dmsg(ctx, dbglvl, "delta-test-fd: StartRestoreJob\n");
254 case bEventEndRestoreJob:
255 // Dmsg(ctx, dbglvl, "delta-test-fd: EndRestoreJob\n");
258 /* Plugin command e.g. plugin = <plugin-name>:<name-space>:read command:write command */
259 case bEventRestoreCommand:
260 // Dmsg(ctx, dbglvl, "delta-test-fd: EventRestoreCommand cmd=%s\n", (char *)value);
261 /* Fall-through wanted */
263 case bEventBackupCommand:
264 Dmsg(ctx, dbglvl, "delta-test-fd: pluginEvent cmd=%s\n", (char *)value);
265 if (self->level == 'I' || self->level == 'D') {
266 bfuncs->getBaculaValue(ctx, bVarAccurate, (void *)&accurate);
267 if (!accurate) { /* can be changed to FATAL */
269 "Accurate mode should be turned on when using the "
270 "delta-test plugin\n");
277 // Dmsg(ctx, dbglvl, "delta-test-fd: unknown event=%d\n", event->eventType);
283 static const char *files[] = {
289 static int nb_files = 4;
292 * Start the backup of a specific file
294 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
296 delta_test *self = get_self(ctx);
300 time_t now = time(NULL);
301 sp->fname = (char *)"/delta.txt";
303 sp->statp.st_mode = 0700 | S_IFREG;
304 sp->statp.st_ctime = now;
305 sp->statp.st_mtime = now;
306 sp->statp.st_atime = now;
307 sp->statp.st_size = -1;
308 sp->statp.st_blksize = 4096;
309 sp->statp.st_blocks = 1;
310 if (self->level == 'I' || self->level == 'D') {
311 bRC state = bfuncs->checkChanges(ctx, sp);
312 /* Should always be bRC_OK */
313 sp->type = (state == bRC_Seen)? FT_NOCHG : FT_REG;
314 sp->flags |= (FO_DELTA|FO_OFFSETS);
315 self->delta = sp->delta_seq + 1;
317 pm_strcpy(self->fname, files[self->delta % nb_files]);
318 Dmsg(ctx, dbglvl, "delta-test-fd: delta_seq=%i delta=%i fname=%s\n",
319 sp->delta_seq, self->delta, self->fname);
320 // Dmsg(ctx, dbglvl, "delta-test-fd: startBackupFile\n");
325 * Done with backup of this file
327 static bRC endBackupFile(bpContext *ctx)
330 * We would return bRC_More if we wanted startBackupFile to be
331 * called again to backup another file
338 * Bacula is calling us to do the actual I/O
340 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
342 delta_test *self = get_self(ctx);
352 Dmsg(ctx, dbglvl, "delta-test-fd: IO_OPEN\n");
353 if (io->flags & (O_CREAT | O_WRONLY)) {
354 /* TODO: if the file already exists, the result is undefined */
355 if (stat(io->fname, &statp) == 0) { /* file exists */
356 self->fd = fopen(io->fname, "r+");
358 self->fd = fopen(io->fname, "w"); /* file doesn't exist,create it */
361 io->io_errno = errno;
363 "Open failed: ERR=%s\n", strerror(errno));
368 self->fd = fopen(self->fname, "r");
370 io->io_errno = errno;
372 "Open failed: ERR=%s\n", strerror(errno));
380 Jmsg(ctx, M_FATAL, "Logic error: NULL read FD\n");
386 /* first time, read 300, then replace 50-250 by other data */
387 if (self->delta == 0) {
388 io->status = fread(io->buf, 1, 400, self->fd);
390 io->offset = self->delta * 100 / 2; /* chunks are melted */
391 io->status = fread(io->buf, 1, 100, self->fd);
393 Dmsg(ctx, dbglvl, "delta-test-fd: READ offset=%lld\n", (int64_t)io->offset);
396 if (io->status == 0 && ferror(self->fd)) {
398 "Pipe read error: ERR=%s\n", strerror(errno));
400 "Pipe read error: ERR=%s\n", strerror(errno));
403 Dmsg(ctx, dbglvl, "offset=%d\n", io->offset);
408 Jmsg(ctx, M_FATAL, "Logic error: NULL write FD\n");
411 Dmsg(ctx, dbglvl, "delta-test-fd: WRITE count=%lld\n", (int64_t)io->count);
412 io->status = fwrite(io->buf, 1, io->count, self->fd);
413 if (io->status == 0 && ferror(self->fd)) {
415 "Pipe write error\n");
417 "Pipe read error: ERR=%s\n", strerror(errno));
424 Jmsg(ctx, M_FATAL, "Logic error: NULL FD on delta close\n");
427 io->status = fclose(self->fd);
432 Jmsg(ctx, M_FATAL, "Logic error: NULL FD on delta close\n");
435 Dmsg(ctx, dbglvl, "delta-test-fd: SEEK offset=%lld\n", (int64_t)io->offset);
436 io->status = fseek(self->fd, io->offset, io->whence);
437 Dmsg(ctx, dbglvl, "after SEEK=%lld\n", (int64_t)ftell(self->fd));
444 * Bacula is notifying us that a plugin name string was found, and
445 * passing us the plugin command, so we can prepare for a restore.
447 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
449 // Dmsg(ctx, dbglvl, "delta-test-fd: startRestoreFile cmd=%s\n", cmd);
454 * Bacula is notifying us that the plugin data has terminated, so
455 * the restore for this particular file is done.
457 static bRC endRestoreFile(bpContext *ctx)
459 // Dmsg(ctx, dbglvl, "delta-test-fd: endRestoreFile\n");
464 * This is called during restore to create the file (if necessary)
465 * We must return in rp->create_status:
468 * CF_SKIP -- skip processing this file
469 * CF_EXTRACT -- extract the file (i.e.call i/o routines)
470 * CF_CREATED -- created, but no content to extract (typically directories)
473 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
475 delta_test *self = get_self(ctx);
476 pm_strcpy(self->fname, rp->ofname);
477 rp->create_status = CF_EXTRACT;
482 * We will get here if the File is a directory after everything
483 * is written in the directory.
485 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
487 // Dmsg(ctx, dbglvl, "delta-test-fd: setFileAttributes\n");
491 /* When using Incremental dump, all previous dumps are necessary */
492 static bRC checkFile(bpContext *ctx, char *fname)
498 * New Bacula Plugin API require this
500 static bRC handleXACLdata(bpContext *ctx, struct xacl_pkt *xacl)