]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/plugins/fd/example-plugin-fd.c
Plugin implementation
[bacula/bacula] / bacula / src / plugins / fd / example-plugin-fd.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2008 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 John Walker.
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  * Sample Plugin program
30  *
31  *  Kern Sibbald, October 2007
32  *
33  */
34 #include <stdio.h>
35 #include "fd-plugins.h"
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 #define PLUGIN_LICENSE      "GPL"
42 #define PLUGIN_AUTHOR       "Kern Sibbald"
43 #define PLUGIN_DATE         "January 2008"
44 #define PLUGIN_VERSION      "1"
45 #define PLUGIN_DESCRIPTION  "Test File Daemon Plugin"
46
47 /* Forward referenced functions */
48 static bRC newPlugin(bpContext *ctx);
49 static bRC freePlugin(bpContext *ctx);
50 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
51 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
52 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
53 static bRC startPluginBackup(bpContext *ctx, struct save_pkt *sp);
54 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
55
56
57 /* Pointers to Bacula functions */
58 static bFuncs *bfuncs = NULL;
59 static bInfo  *binfo = NULL;
60
61 static pInfo pluginInfo = {
62    sizeof(pluginInfo),
63    PLUGIN_INTERFACE_VERSION,
64    PLUGIN_MAGIC,
65    PLUGIN_LICENSE,
66    PLUGIN_AUTHOR,
67    PLUGIN_DATE,
68    PLUGIN_VERSION,
69    PLUGIN_DESCRIPTION,
70 };
71
72 static pFuncs pluginFuncs = {
73    sizeof(pluginFuncs),
74    PLUGIN_INTERFACE_VERSION,
75
76    /* Entry points into plugin */
77    newPlugin,                         /* new plugin instance */
78    freePlugin,                        /* free plugin instance */
79    getPluginValue,
80    setPluginValue,
81    handlePluginEvent,
82    startPluginBackup,
83    pluginIO
84 };
85
86 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
87 {
88    bfuncs = lbfuncs;                  /* set Bacula funct pointers */
89    binfo  = lbinfo;
90    printf("plugin: Loaded: size=%d version=%d\n", bfuncs->size, bfuncs->version);
91
92    *pinfo  = &pluginInfo;             /* return pointer to our info */
93    *pfuncs = &pluginFuncs;            /* return pointer to our functions */
94
95    return bRC_OK;
96 }
97
98 bRC unloadPlugin() 
99 {
100    printf("plugin: Unloaded\n");
101    return bRC_OK;
102 }
103
104 static bRC newPlugin(bpContext *ctx)
105 {
106    int JobId = 0;
107    bfuncs->getBaculaValue(ctx, bVarJobId, (void *)&JobId);
108 // printf("plugin: newPlugin JobId=%d\n", JobId);
109    bfuncs->registerBaculaEvents(ctx, 1, 2, 0);
110    return bRC_OK;
111 }
112
113 static bRC freePlugin(bpContext *ctx)
114 {
115    int JobId = 0;
116    bfuncs->getBaculaValue(ctx, bVarJobId, (void *)&JobId);
117 // printf("plugin: freePlugin JobId=%d\n", JobId);
118    return bRC_OK;
119 }
120
121 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value) 
122 {
123 // printf("plugin: getPluginValue var=%d\n", var);
124    return bRC_OK;
125 }
126
127 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value) 
128 {
129 // printf("plugin: setPluginValue var=%d\n", var);
130    return bRC_OK;
131 }
132
133 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
134 {
135    char *name;
136
137    switch (event->eventType) {
138    case bEventJobStart:
139       printf("plugin: JobStart=%s\n", (char *)value);
140       break;
141    case bEventJobEnd:
142       printf("plugin: JobEnd\n");
143       break;
144    case bEventBackupStart:
145       printf("plugin: BackupStart\n");
146       break;
147    case bEventBackupEnd:
148       printf("plugin: BackupEnd\n");
149       break;
150    case bEventLevel:
151       printf("plugin: JobLevel=%c %d\n", (int)value, (int)value);
152       break;
153    case bEventSince:
154       printf("plugin: since=%d\n", (int)value);
155       break;
156
157    /* Plugin command e.g. plugin = <plugin-name>:<name-space>:command */
158    case bEventPluginCommand:
159       printf("plugin: command=%s\n", (char *)value);
160       break;
161
162    default:
163       printf("plugin: unknown event=%d\n", event->eventType);
164    }
165    bfuncs->getBaculaValue(ctx, bVarFDName, (void *)&name);
166 // printf("FD Name=%s\n", name);
167 // bfuncs->JobMessage(ctx, __FILE__, __LINE__, 1, 0, "JobMesssage message");
168 // bfuncs->DebugMessage(ctx, __FILE__, __LINE__, 1, "DebugMesssage message");
169    return bRC_OK;
170 }
171
172 static bRC startPluginBackup(bpContext *ctx, struct save_pkt *sp)
173 {
174    return bRC_OK;
175 }
176
177 /*
178  * Do actual I/O
179  */
180 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
181 {
182    io->status = 0;
183    io->io_errno = 0;
184    switch(io->func) {
185    case IO_OPEN:
186       printf("plugin: IO_OPEN\n");
187       break;
188    case IO_READ:
189       printf("plugin: IO_READ buf=%p len=%d\n", io->buf, io->count);
190       break;
191    case IO_WRITE:
192       printf("plugin: IO_WRITE buf=%p len=%d\n", io->buf, io->count);
193       break;
194    case IO_CLOSE:
195       printf("plugin: IO_CLOSE\n");
196       break;
197    }
198    return bRC_OK;
199 }
200
201 #ifdef __cplusplus
202 }
203 #endif