]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/plugins/fd/bpipe-fd.c
Plugin update
[bacula/bacula] / bacula / src / plugins / fd / bpipe-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 #undef malloc
38 #undef free
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 #define PLUGIN_LICENSE      "GPL"
45 #define PLUGIN_AUTHOR       "Kern Sibbald"
46 #define PLUGIN_DATE         "January 2008"
47 #define PLUGIN_VERSION      "1"
48 #define PLUGIN_DESCRIPTION  "Test File Daemon Plugin"
49
50 /* Forward referenced functions */
51 static bRC newPlugin(bpContext *ctx);
52 static bRC freePlugin(bpContext *ctx);
53 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
54 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
55 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
56 static bRC startPluginBackup(bpContext *ctx, struct save_pkt *sp);
57 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
58
59
60 /* Pointers to Bacula functions */
61 static bFuncs *bfuncs = NULL;
62 static bInfo  *binfo = NULL;
63
64 static pInfo pluginInfo = {
65    sizeof(pluginInfo),
66    PLUGIN_INTERFACE_VERSION,
67    PLUGIN_MAGIC,
68    PLUGIN_LICENSE,
69    PLUGIN_AUTHOR,
70    PLUGIN_DATE,
71    PLUGIN_VERSION,
72    PLUGIN_DESCRIPTION,
73 };
74
75 static pFuncs pluginFuncs = {
76    sizeof(pluginFuncs),
77    PLUGIN_INTERFACE_VERSION,
78
79    /* Entry points into plugin */
80    newPlugin,                         /* new plugin instance */
81    freePlugin,                        /* free plugin instance */
82    getPluginValue,
83    setPluginValue,
84    handlePluginEvent,
85    startPluginBackup,
86    pluginIO
87 };
88
89 struct plugin_ctx {
90    int record;
91    boffset_t offset;
92 };
93
94 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
95 {
96    bfuncs = lbfuncs;                  /* set Bacula funct pointers */
97    binfo  = lbinfo;
98    printf("bpipe-fd: Loaded: size=%d version=%d\n", bfuncs->size, bfuncs->version);
99
100    *pinfo  = &pluginInfo;             /* return pointer to our info */
101    *pfuncs = &pluginFuncs;            /* return pointer to our functions */
102
103    return bRC_OK;
104 }
105
106 bRC unloadPlugin() 
107 {
108    printf("bpipe-fd: Unloaded\n");
109    return bRC_OK;
110 }
111
112 static bRC newPlugin(bpContext *ctx)
113 {
114    struct plugin_ctx *p_ctx = (struct plugin_ctx *)malloc(sizeof(struct plugin_ctx));
115
116    p_ctx->record = -1;
117    ctx->pContext = (void *)p_ctx;        /* set our context pointer */
118    return bRC_OK;
119 }
120
121 static bRC freePlugin(bpContext *ctx)
122 {
123    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
124    free(p_ctx);
125    return bRC_OK;
126 }
127
128 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value) 
129 {
130    return bRC_OK;
131 }
132
133 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value) 
134 {
135    return bRC_OK;
136 }
137
138 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
139 {
140    char *name;
141
142    switch (event->eventType) {
143    case bEventJobStart:
144       printf("bpipe-fd: JobStart=%s\n", (char *)value);
145       break;
146    case bEventJobEnd:
147       printf("bpipe-fd: JobEnd\n");
148       break;
149    case bEventBackupStart:
150       printf("bpipe-fd: BackupStart\n");
151       break;
152    case bEventBackupEnd:
153       printf("bpipe-fd: BackupEnd\n");
154       break;
155    case bEventLevel:
156       printf("bpipe-fd: JobLevel=%c %d\n", (int)value, (int)value);
157       break;
158    case bEventSince:
159       printf("bpipe-fd: since=%d\n", (int)value);
160       break;
161
162    case bEventRestoreStart: 
163       printf("bpipe-fd: RestoreStart\n");
164       break;
165    case bEventRestoreEnd:
166       printf("bpipe-fd: RestoreEnd\n");
167       break;
168
169    /* Plugin command e.g. plugin = <plugin-name>:<name-space>:command */
170    case bEventPluginCommand:
171       printf("bpipe-fd: command=%s\n", (char *)value);
172       break;
173
174    default:
175       printf("bpipe-fd: unknown event=%d\n", event->eventType);
176    }
177    bfuncs->getBaculaValue(ctx, bVarFDName, (void *)&name);
178 // printf("FD Name=%s\n", name);
179 // bfuncs->JobMessage(ctx, __FILE__, __LINE__, 1, 0, "JobMesssage message");
180 // bfuncs->DebugMessage(ctx, __FILE__, __LINE__, 1, "DebugMesssage message");
181    return bRC_OK;
182 }
183
184 static bRC startPluginBackup(bpContext *ctx, struct save_pkt *sp)
185 {
186    static char *fname = (char *)"/@BPIPE/test.txt";
187    time_t now = time(NULL);
188    sp->fname = fname;
189    sp->statp.st_mode = 0700;
190    sp->statp.st_ctime = now;
191    sp->statp.st_mtime = now;
192    sp->statp.st_atime = now;
193    sp->statp.st_size = 100;
194    sp->statp.st_blksize = 4096;
195    sp->statp.st_blocks = 1;
196    printf("bpipe-fd: startPluginBackup\n");
197    return bRC_OK;
198 }
199
200 /*
201  * Do actual I/O
202  */
203 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
204 {
205    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
206     
207    io->status = 0;
208    io->io_errno = 0;
209    switch(io->func) {
210    case IO_OPEN:
211       p_ctx->record = 0;
212       printf("bpipe-fd: IO_OPEN\n");
213       break;
214    case IO_READ:
215       printf("bpipe-fd: IO_READ buf=%p len=%d\n", io->buf, io->count);
216       if (p_ctx->record == 0) {
217          strcpy(io->buf, "This is a test string.\n");
218          io->status = strlen(io->buf);
219          p_ctx->offset = io->status;
220          p_ctx->record = 1;
221          return bRC_OK;
222       }
223       io->status = 0;
224       break;
225    case IO_WRITE:
226       printf("bpipe-fd: IO_WRITE buf=%p len=%d\n", io->buf, io->count);
227       break;
228    case IO_CLOSE:
229       printf("bpipe-fd: IO_CLOSE\n");
230       break;
231    case IO_SEEK:
232       io->offset = p_ctx->offset;
233       break;
234    }
235    return bRC_OK;
236 }
237
238 #ifdef __cplusplus
239 }
240 #endif