3 Copyright (C) 2007-2008 Kern Sibbald
5 You may freely use this code to create your own plugin provided
6 it is to write a plugin for Bacula licensed under GPLv2
7 (as Bacula is), and in that case, you may also remove
8 the above Copyright and this notice as well as modify
13 #include "fd_plugins.h"
19 #define PLUGIN_LICENSE "GPL"
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"
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);
40 /* Pointers to Bacula functions */
41 static bFuncs *bfuncs = NULL;
42 static bInfo *binfo = NULL;
44 static pInfo pluginInfo = {
46 FD_PLUGIN_INTERFACE_VERSION,
55 static pFuncs pluginFuncs = {
57 FD_PLUGIN_INTERFACE_VERSION,
59 /* Entry points into plugin */
60 newPlugin, /* new plugin instance */
61 freePlugin, /* free plugin instance */
75 * Plugin called here when it is first loaded
77 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
79 bfuncs = lbfuncs; /* set Bacula funct pointers */
81 printf("plugin: Loaded: size=%d version=%d\n", bfuncs->size, bfuncs->version);
83 *pinfo = &pluginInfo; /* return pointer to our info */
84 *pfuncs = &pluginFuncs; /* return pointer to our functions */
90 * Plugin called here when it is unloaded, normally when
91 * Bacula is going to exit.
95 printf("plugin: Unloaded\n");
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
106 static bRC newPlugin(bpContext *ctx)
109 bfuncs->getBaculaValue(ctx, bVarJobId, (void *)&JobId);
110 // printf("plugin: newPlugin JobId=%d\n", JobId);
111 bfuncs->registerBaculaEvents(ctx, 1, 2, 0);
116 * Release everything concerning a particular instance of a
117 * plugin. Normally called when the Job terminates.
119 static bRC freePlugin(bpContext *ctx)
122 bfuncs->getBaculaValue(ctx, bVarJobId, (void *)&JobId);
123 // printf("plugin: freePlugin JobId=%d\n", JobId);
128 * Called by core code to get a variable from the plugin.
129 * Not currently used.
131 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value)
133 // printf("plugin: getPluginValue var=%d\n", var);
138 * Called by core code to set a plugin variable.
139 * Not currently used.
141 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value)
143 // printf("plugin: setPluginValue var=%d\n", var);
148 * Called by Bacula when there are certain events that the
149 * plugin might want to know. The value depends on the
152 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
156 switch (event->eventType) {
158 printf("plugin: JobStart=%s\n", NPRT((char *)value));
161 printf("plugin: JobEnd\n");
163 case bEventStartBackupJob:
164 printf("plugin: BackupStart\n");
166 case bEventEndBackupJob:
167 printf("plugin: BackupEnd\n");
170 printf("plugin: JobLevel=%c %d\n", (int64_t)value, (int64_t)value);
173 printf("plugin: since=%d\n", (int64_t)value);
175 case bEventStartRestoreJob:
176 printf("plugin: StartRestoreJob\n");
178 case bEventEndRestoreJob:
179 printf("plugin: EndRestoreJob\n");
182 /* Plugin command e.g. plugin = <plugin-name>:<name-space>:command */
183 case bEventRestoreCommand:
184 printf("plugin: backup command=%s\n", NPRT((char *)value));
187 case bEventBackupCommand:
188 printf("plugin: backup command=%s\n", NPRT((char *)value));
192 printf("plugin: unknown event=%d\n", event->eventType);
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");
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.
208 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
214 * Done backing up a file.
216 static bRC endBackupFile(bpContext *ctx)
222 * Do actual I/O. Bacula calls this after startBackupFile
223 * or after startRestoreFile to do the actual file
226 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
232 printf("plugin: IO_OPEN\n");
235 printf("plugin: IO_READ buf=%p len=%d\n", io->buf, io->count);
238 printf("plugin: IO_WRITE buf=%p len=%d\n", io->buf, io->count);
241 printf("plugin: IO_CLOSE\n");
247 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
252 static bRC endRestoreFile(bpContext *ctx)
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.
264 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
270 * Called after the file has been restored. This can be used to
271 * set directory permissions, ...
273 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)