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