]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/plugins/fd/example-plugin-fd.c
ebl Initialize plugin in director
[bacula/bacula] / bacula / src / plugins / fd / example-plugin-fd.c
1 /*
2
3    Copyright (C) 2007-2008 Kern Sibbald
4
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 
9    the code in any way. 
10
11 */
12 #include "bacula.h"
13 #include "fd_plugins.h"
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
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"
24
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);
38
39
40 /* Pointers to Bacula functions */
41 static bFuncs *bfuncs = NULL;
42 static bInfo  *binfo = NULL;
43
44 static pInfo pluginInfo = {
45    sizeof(pluginInfo),
46    FD_PLUGIN_INTERFACE_VERSION,
47    FD_PLUGIN_MAGIC,
48    PLUGIN_LICENSE,
49    PLUGIN_AUTHOR,
50    PLUGIN_DATE,
51    PLUGIN_VERSION,
52    PLUGIN_DESCRIPTION,
53 };
54
55 static pFuncs pluginFuncs = {
56    sizeof(pluginFuncs),
57    FD_PLUGIN_INTERFACE_VERSION,
58
59    /* Entry points into plugin */
60    newPlugin,                         /* new plugin instance */
61    freePlugin,                        /* free plugin instance */
62    getPluginValue,
63    setPluginValue,
64    handlePluginEvent,
65    startBackupFile,
66    endBackupFile,
67    startRestoreFile,
68    endRestoreFile,
69    pluginIO,
70    createFile,
71    setFileAttributes
72 };
73
74 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
75 {
76    bfuncs = lbfuncs;                  /* set Bacula funct pointers */
77    binfo  = lbinfo;
78    printf("plugin: Loaded: size=%d version=%d\n", bfuncs->size, bfuncs->version);
79
80    *pinfo  = &pluginInfo;             /* return pointer to our info */
81    *pfuncs = &pluginFuncs;            /* return pointer to our functions */
82
83    return bRC_OK;
84 }
85
86 bRC unloadPlugin() 
87 {
88    printf("plugin: Unloaded\n");
89    return bRC_OK;
90 }
91
92 static bRC newPlugin(bpContext *ctx)
93 {
94    int JobId = 0;
95    bfuncs->getBaculaValue(ctx, bVarJobId, (void *)&JobId);
96 // printf("plugin: newPlugin JobId=%d\n", JobId);
97    bfuncs->registerBaculaEvents(ctx, 1, 2, 0);
98    return bRC_OK;
99 }
100
101 static bRC freePlugin(bpContext *ctx)
102 {
103    int JobId = 0;
104    bfuncs->getBaculaValue(ctx, bVarJobId, (void *)&JobId);
105 // printf("plugin: freePlugin JobId=%d\n", JobId);
106    return bRC_OK;
107 }
108
109 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value) 
110 {
111 // printf("plugin: getPluginValue var=%d\n", var);
112    return bRC_OK;
113 }
114
115 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value) 
116 {
117 // printf("plugin: setPluginValue var=%d\n", var);
118    return bRC_OK;
119 }
120
121 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
122 {
123    char *name;
124
125    switch (event->eventType) {
126    case bEventJobStart:
127       printf("plugin: JobStart=%s\n", NPRT((char *)value));
128       break;
129    case bEventJobEnd:
130       printf("plugin: JobEnd\n");
131       break;
132    case bEventStartBackupJob:
133       printf("plugin: BackupStart\n");
134       break;
135    case bEventEndBackupJob:
136       printf("plugin: BackupEnd\n");
137       break;
138    case bEventLevel:
139       printf("plugin: JobLevel=%c %d\n", (int64_t)value, (int64_t)value);
140       break;
141    case bEventSince:
142       printf("plugin: since=%d\n", (int64_t)value);
143       break;
144    case bEventStartRestoreJob:
145       printf("plugin: StartRestoreJob\n");
146       break;
147    case bEventEndRestoreJob:
148       printf("plugin: EndRestoreJob\n");
149       break;
150
151    /* Plugin command e.g. plugin = <plugin-name>:<name-space>:command */
152    case bEventRestoreCommand:
153       printf("plugin: backup command=%s\n", NPRT((char *)value));
154       break;
155
156    case bEventBackupCommand:
157       printf("plugin: backup command=%s\n", NPRT((char *)value));
158       break;
159
160    default:
161       printf("plugin: unknown event=%d\n", event->eventType);
162    }
163    bfuncs->getBaculaValue(ctx, bVarFDName, (void *)&name);
164 // printf("FD Name=%s\n", name);
165 // bfuncs->JobMessage(ctx, __FILE__, __LINE__, 1, 0, "JobMesssage message");
166 // bfuncs->DebugMessage(ctx, __FILE__, __LINE__, 1, "DebugMesssage message");
167    return bRC_OK;
168 }
169
170 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
171 {
172    return bRC_OK;
173 }
174
175 static bRC endBackupFile(bpContext *ctx)
176
177    return bRC_OK;
178 }
179
180 /*
181  * Do actual I/O
182  */
183 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
184 {
185    io->status = 0;
186    io->io_errno = 0;
187    switch(io->func) {
188    case IO_OPEN:
189       printf("plugin: IO_OPEN\n");
190       break;
191    case IO_READ:
192       printf("plugin: IO_READ buf=%p len=%d\n", io->buf, io->count);
193       break;
194    case IO_WRITE:
195       printf("plugin: IO_WRITE buf=%p len=%d\n", io->buf, io->count);
196       break;
197    case IO_CLOSE:
198       printf("plugin: IO_CLOSE\n");
199       break;
200    }
201    return bRC_OK;
202 }
203
204 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
205 {
206    return bRC_OK;
207 }
208
209 static bRC endRestoreFile(bpContext *ctx)
210 {
211    return bRC_OK;
212 }
213
214 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
215 {
216    return bRC_OK;
217 }
218
219 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
220 {
221    return bRC_OK;
222 }
223
224
225 #ifdef __cplusplus
226 }
227 #endif