]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/plugins/fd/delta-test-fd.c
b3657119019f416ee0bc859cf849161367dae480
[bacula/bacula] / bacula / src / plugins / fd / delta-test-fd.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2011 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 three of the GNU Affero 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 Affero 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 Kern Sibbald.
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  * A simple delta plugin for the Bacula File Daemon
30  *
31  *
32  */
33 #include "bacula.h"
34 #include "fd_plugins.h"
35 #include "fd_common.h"
36
37 #undef malloc
38 #undef free
39 #undef strdup
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 static const int dbglvl = 0;
46
47 #define PLUGIN_LICENSE      "Bacula AGPLv3"
48 #define PLUGIN_AUTHOR       "Eric Bollengier"
49 #define PLUGIN_DATE         "November 2010"
50 #define PLUGIN_VERSION      "1"
51 #define PLUGIN_DESCRIPTION  "Bacula Delta Test Plugin"
52
53 /* Forward referenced functions */
54 static bRC newPlugin(bpContext *ctx);
55 static bRC freePlugin(bpContext *ctx);
56 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
57 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
58 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
59 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
60 static bRC endBackupFile(bpContext *ctx);
61 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
62 static bRC startRestoreFile(bpContext *ctx, const char *cmd);
63 static bRC endRestoreFile(bpContext *ctx);
64 static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
65 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
66
67 /* Pointers to Bacula functions */
68 static bFuncs *bfuncs = NULL;
69 static bInfo  *binfo = NULL;
70
71 /* Plugin Information block */
72 static pInfo pluginInfo = {
73    sizeof(pluginInfo),
74    FD_PLUGIN_INTERFACE_VERSION,
75    FD_PLUGIN_MAGIC,
76    PLUGIN_LICENSE,
77    PLUGIN_AUTHOR,
78    PLUGIN_DATE,
79    PLUGIN_VERSION,
80    PLUGIN_DESCRIPTION,
81 };
82
83 /* Plugin entry points for Bacula */
84 static pFuncs pluginFuncs = {
85    sizeof(pluginFuncs),
86    FD_PLUGIN_INTERFACE_VERSION,
87
88    /* Entry points into plugin */
89    newPlugin,                         /* new plugin instance */
90    freePlugin,                        /* free plugin instance */
91    getPluginValue,
92    setPluginValue,
93    handlePluginEvent,
94    startBackupFile,
95    endBackupFile,
96    startRestoreFile,
97    endRestoreFile,
98    pluginIO,
99    createFile,
100    setFileAttributes,
101    NULL                         /* no checkFile */
102 };
103
104 #define get_self(x) ((delta_test*)((x)->pContext))
105 #define FO_DELTA        (1<<28)       /* Do delta on file */
106 #define FO_OFFSETS      (1<<30)       /* Keep block offsets */
107
108 class delta_test
109 {
110 private:
111    bpContext *ctx;
112
113 public:
114    POOLMEM *fname;              /* Filename to save */
115    int32_t delta;
116    FILE *fd;
117    bool done;
118    int level;
119
120    delta_test(bpContext *bpc) { 
121       fd = NULL;
122       ctx = bpc;
123       done = false;
124       level = 0;
125       delta = 0;
126       fname = get_pool_memory(PM_FNAME);
127    }
128    ~delta_test() {
129       free_and_null_pool_memory(fname);
130    }
131 };
132
133 /*
134  * loadPlugin() and unloadPlugin() are entry points that are
135  *  exported, so Bacula can directly call these two entry points
136  *  they are common to all Bacula plugins.
137  */
138 /*
139  * External entry point called by Bacula to "load the plugin
140  */
141 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
142 {
143    bfuncs = lbfuncs;                  /* set Bacula funct pointers */
144    binfo  = lbinfo;
145    *pinfo  = &pluginInfo;             /* return pointer to our info */
146    *pfuncs = &pluginFuncs;            /* return pointer to our functions */
147
148    /* Activate this plugin only in developer mode */
149 #ifdef DEVELOPER
150    return bRC_OK;
151 #else
152    return bRC_Error;
153 #endif
154 }
155
156 /*
157  * External entry point to unload the plugin 
158  */
159 bRC unloadPlugin() 
160 {
161 // Dmsg(NULL, dbglvl, "delta-test-fd: Unloaded\n");
162    return bRC_OK;
163 }
164
165 /*
166  * The following entry points are accessed through the function 
167  *   pointers we supplied to Bacula. Each plugin type (dir, fd, sd)
168  *   has its own set of entry points that the plugin must define.
169  */
170 /*
171  * Create a new instance of the plugin i.e. allocate our private storage
172  */
173 static bRC newPlugin(bpContext *ctx)
174 {
175    delta_test *self = new delta_test(ctx);
176    if (!self) {
177       return bRC_Error;
178    }
179    ctx->pContext = (void *)self;        /* set our context pointer */
180    return bRC_OK;
181 }
182
183 /*
184  * Free a plugin instance, i.e. release our private storage
185  */
186 static bRC freePlugin(bpContext *ctx)
187 {
188    delta_test *self = get_self(ctx);
189    if (!self) {
190       return bRC_Error;
191    }
192    delete self;
193    return bRC_OK;
194 }
195
196 /*
197  * Return some plugin value (none defined)
198  */
199 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value) 
200 {
201    return bRC_OK;
202 }
203
204 /*
205  * Set a plugin value (none defined)
206  */
207 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value) 
208 {
209    return bRC_OK;
210 }
211
212 /*
213  * Handle an event that was generated in Bacula
214  */
215 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
216 {
217    delta_test *self = get_self(ctx);
218    int accurate=0;
219
220    if (!self) {
221       return bRC_Error;
222    }
223
224 // char *name;
225
226    /*
227     * Most events don't interest us so we ignore them.
228     *   the printfs are so that plugin writers can enable them to see
229     *   what is really going on.
230     */
231    switch (event->eventType) {
232    case bEventPluginCommand:
233 //    Dmsg(ctx, dbglvl, 
234 //         "delta-test-fd: PluginCommand=%s\n", (char *)value);
235       break;
236    case bEventJobStart:
237 //    Dmsg(ctx, dbglvl, "delta-test-fd: JobStart=%s\n", (char *)value);
238       break;
239    case bEventJobEnd:
240 //    Dmsg(ctx, dbglvl, "delta-test-fd: JobEnd\n");
241       break;
242    case bEventStartBackupJob:
243 //    Dmsg(ctx, dbglvl, "delta-test-fd: StartBackupJob\n");
244       break;
245    case bEventEndBackupJob:
246 //    Dmsg(ctx, dbglvl, "delta-test-fd: EndBackupJob\n");
247       break;
248    case bEventLevel:
249 //    Dmsg(ctx, dbglvl, "delta-test-fd: JobLevel=%c %d\n", (int)value, (int)value);
250       self->level = (int)(intptr_t)value;
251       break;
252    case bEventSince:
253 //    Dmsg(ctx, dbglvl, "delta-test-fd: since=%d\n", (int)value);
254       break;
255
256    case bEventStartRestoreJob:
257 //    Dmsg(ctx, dbglvl, "delta-test-fd: StartRestoreJob\n");
258       break;
259
260    case bEventEndRestoreJob:
261 //    Dmsg(ctx, dbglvl, "delta-test-fd: EndRestoreJob\n");
262       break;
263
264    /* Plugin command e.g. plugin = <plugin-name>:<name-space>:read command:write command */
265    case bEventRestoreCommand:
266 //    Dmsg(ctx, dbglvl, "delta-test-fd: EventRestoreCommand cmd=%s\n", (char *)value);
267       /* Fall-through wanted */
268       break;
269    case bEventBackupCommand:
270       Dmsg(ctx, dbglvl, "delta-test-fd: pluginEvent cmd=%s\n", (char *)value);
271       if (self->level == 'I' || self->level == 'D') {
272          bfuncs->getBaculaValue(ctx, bVarAccurate, (void *)&accurate);
273          if (!accurate) {       /* can be changed to FATAL */
274             Jmsg(ctx, M_FATAL, 
275                  "Accurate mode should be turned on when using the "
276                  "delta-test plugin\n");
277             return bRC_Error;
278          }
279       }
280       break;
281
282    default:
283 //    Dmsg(ctx, dbglvl, "delta-test-fd: unknown event=%d\n", event->eventType);
284       break;
285    }
286    return bRC_OK;
287 }
288
289 static const char *files[] = {
290    "/etc/passwd",
291    "/etc/group",
292    "/etc/hosts",
293    "/etc/services"
294 };
295 static int nb_files = 4;
296
297 /* 
298  * Start the backup of a specific file
299  */
300 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
301 {
302    delta_test *self = get_self(ctx);
303    if (!self) {
304       return bRC_Error;
305    }
306    time_t now = time(NULL);
307    sp->fname = (char *)"/delta.txt";
308    sp->type = FT_REG;
309    sp->statp.st_mode = 0700 | S_IFREG;
310    sp->statp.st_ctime = now;
311    sp->statp.st_mtime = now;
312    sp->statp.st_atime = now;
313    sp->statp.st_size = -1;
314    sp->statp.st_blksize = 4096;
315    sp->statp.st_blocks = 1;
316    if (self->level == 'I' || self->level == 'D') {
317       bRC state = bfuncs->checkChanges(ctx, sp);
318       /* Should always be bRC_OK */
319       sp->type = (state == bRC_Seen)? FT_NOCHG : FT_REG;
320       sp->flags |= (FO_DELTA|FO_OFFSETS);
321       self->delta = sp->delta_seq + 1;
322    }
323    pm_strcpy(self->fname, files[self->delta % nb_files]);
324    Dmsg(ctx, dbglvl, "delta-test-fd: delta_seq=%i delta=%i fname=%s\n", 
325         sp->delta_seq, self->delta, self->fname);
326 // Dmsg(ctx, dbglvl, "delta-test-fd: startBackupFile\n");
327    return bRC_OK;
328 }
329
330 /*
331  * Done with backup of this file
332  */
333 static bRC endBackupFile(bpContext *ctx)
334 {
335    /*
336     * We would return bRC_More if we wanted startBackupFile to be
337     * called again to backup another file
338     */
339    return bRC_OK;
340 }
341
342
343 /*
344  * Bacula is calling us to do the actual I/O
345  */
346 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
347 {
348    delta_test *self = get_self(ctx);
349    struct stat statp;
350    if (!self) {
351       return bRC_Error;
352    }
353     
354    io->status = 0;
355    io->io_errno = 0;
356    switch(io->func) {
357    case IO_OPEN:
358       Dmsg(ctx, dbglvl, "delta-test-fd: IO_OPEN\n");
359       if (io->flags & (O_CREAT | O_WRONLY)) {
360          /* TODO: if the file already exists, the result is undefined */
361          if (stat(io->fname, &statp) == 0) { /* file exists */
362             self->fd = fopen(io->fname, "r+");
363          } else {
364             self->fd = fopen(io->fname, "w"); /* file doesn't exist,create it */
365          }
366          if (!self->fd) {
367             io->io_errno = errno;
368             Jmsg(ctx, M_FATAL, 
369                  "Open failed: ERR=%s\n", strerror(errno));
370             return bRC_Error;
371          }
372
373       } else {
374          self->fd = fopen(self->fname, "r");
375          if (!self->fd) {
376             io->io_errno = errno;
377             Jmsg(ctx, M_FATAL, 
378                "Open failed: ERR=%s\n", strerror(errno));
379             return bRC_Error;
380          }
381       }
382       break;
383
384    case IO_READ:
385       if (!self->fd) {
386          Jmsg(ctx, M_FATAL, "Logic error: NULL read FD\n");
387          return bRC_Error;
388       }
389       if (self->done) {
390          io->status = 0;
391       } else {
392          /* first time, read 300, then replace 50-250 by other data */
393          if (self->delta == 0) {
394             io->status = fread(io->buf, 1, 400, self->fd);            
395          } else {
396             io->offset = self->delta * 100 / 2; /* chunks are melted */
397             io->status = fread(io->buf, 1, 100, self->fd);
398          }
399          Dmsg(ctx, dbglvl, "delta-test-fd: READ offset=%lld\n", (int64_t)io->offset);
400          self->done = true;
401       }
402       if (io->status == 0 && ferror(self->fd)) {
403          Jmsg(ctx, M_FATAL, 
404             "Pipe read error: ERR=%s\n", strerror(errno));
405          Dmsg(ctx, dbglvl, 
406             "Pipe read error: ERR=%s\n", strerror(errno));
407          return bRC_Error;
408       }
409       Dmsg(ctx, dbglvl, "offset=%d\n", io->offset);
410       break;
411
412    case IO_WRITE:
413       if (!self->fd) {
414          Jmsg(ctx, M_FATAL, "Logic error: NULL write FD\n");
415          return bRC_Error;
416       }
417       Dmsg(ctx, dbglvl, "delta-test-fd: WRITE count=%lld\n", (int64_t)io->count);
418       io->status = fwrite(io->buf, 1, io->count, self->fd);
419       if (io->status == 0 && ferror(self->fd)) {
420          Jmsg(ctx, M_FATAL, 
421             "Pipe write error\n");
422          Dmsg(ctx, dbglvl, 
423             "Pipe read error: ERR=%s\n", strerror(errno));
424          return bRC_Error;
425       }
426       break;
427
428    case IO_CLOSE:
429       if (!self->fd) {
430          Jmsg(ctx, M_FATAL, "Logic error: NULL FD on delta close\n");
431          return bRC_Error;
432       }
433       io->status = fclose(self->fd);
434       break;
435
436    case IO_SEEK:
437       if (!self->fd) {
438          Jmsg(ctx, M_FATAL, "Logic error: NULL FD on delta close\n");
439          return bRC_Error;
440       }
441       Dmsg(ctx, dbglvl, "delta-test-fd: SEEK offset=%lld\n", (int64_t)io->offset);
442       io->status = fseek(self->fd, io->offset, io->whence);
443       Dmsg(ctx, dbglvl, "after SEEK=%lld\n", (int64_t)ftell(self->fd));
444       break;
445    }
446    return bRC_OK;
447 }
448
449 /*
450  * Bacula is notifying us that a plugin name string was found, and
451  *   passing us the plugin command, so we can prepare for a restore.
452  */
453 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
454 {
455 // Dmsg(ctx, dbglvl, "delta-test-fd: startRestoreFile cmd=%s\n", cmd);
456    return bRC_OK;
457 }
458
459 /*
460  * Bacula is notifying us that the plugin data has terminated, so
461  *  the restore for this particular file is done.
462  */
463 static bRC endRestoreFile(bpContext *ctx)
464 {
465 // Dmsg(ctx, dbglvl, "delta-test-fd: endRestoreFile\n");
466    return bRC_OK;
467 }
468
469 /*
470  * This is called during restore to create the file (if necessary)
471  * We must return in rp->create_status:
472  *   
473  *  CF_ERROR    -- error
474  *  CF_SKIP     -- skip processing this file
475  *  CF_EXTRACT  -- extract the file (i.e.call i/o routines)
476  *  CF_CREATED  -- created, but no content to extract (typically directories)
477  *
478  */
479 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
480 {
481    delta_test *self = get_self(ctx);
482    pm_strcpy(self->fname, rp->ofname);
483    rp->create_status = CF_EXTRACT;
484    return bRC_OK;
485 }
486
487 /*
488  * We will get here if the File is a directory after everything
489  * is written in the directory.
490  */
491 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
492 {
493 // Dmsg(ctx, dbglvl, "delta-test-fd: setFileAttributes\n");
494    return bRC_OK;
495 }
496
497 #ifdef __cplusplus
498 }
499 #endif