]> git.sur5r.net Git - bacula/docs/blob - docs/manuals/fr/developers/pluginAPI.tex
Reset everything to English
[bacula/docs] / docs / manuals / fr / developers / pluginAPI.tex
1 %%
2 %%
3
4 \chapter{Bacula FD Plugin API}
5 To write a Bacula plugin, you create a dynamic shared object program (or dll on
6 Win32) with a particular name and two exported entry points, place it in the
7 {\bf Plugins Directory}, which is defined in the {\bf bacula-fd.conf} file in
8 the {\bf Client} resource, and when the FD starts, it will load all the plugins
9 that end with {\bf -fd.so} (or {\bf -fd.dll} on Win32) found in that directory.
10
11 \section{Normal vs Command Plugins}
12 In general, there are two ways that plugins are called. The first way, is when
13 a particular event is detected in Bacula, it will transfer control to each
14 plugin that is loaded in turn informing the plugin of the event.  This is very
15 similar to how a {\bf RunScript} works, and the events are very similar.  Once
16 the plugin gets control, it can interact with Bacula by getting and setting
17 Bacula variables.  In this way, it behaves much like a RunScript.  Currently
18 very few Bacula variables are defined, but they will be implemented as the need
19 arrises, and it is very extensible.
20
21 We plan to have plugins register to receive events that they normally would
22 not receive, such as an event for each file examined for backup or restore.
23 This feature is not yet implemented.
24
25 The second type of plugin, which is more useful and fully implemented in the
26 current version is what we call a command plugin.  As with all plugins, it gets
27 notified of important events as noted above (details described below), but in
28 addition, this kind of plugin can accept a command line, which is a:
29
30 \begin{verbatim}
31    Plugin = <command-string>
32 \end{verbatim}
33
34 directive that is placed in the Include section of a FileSet and is very
35 similar to the "File = " directive.  When this Plugin directive is encountered
36 by Bacula during backup, it passes the "command" part of the Plugin directive
37 only to the plugin that is explicitly named in the first field of that command
38 string.  This allows that plugin to backup any file or files on the system that
39 it wants. It can even create "virtual files" in the catalog that contain data
40 to be restored but do not necessarily correspond to actual files on the
41 filesystem.
42
43 The important features of the command plugin entry points are:
44 \begin{itemize}
45  \item It is triggered by a "Plugin =" directive in the FileSet
46  \item Only a single plugin is called that is named on the "Plugin =" directive.
47  \item The full command string after the "Plugin =" is passed to the plugin
48     so that it can be told what to backup/restore.
49 \end{itemize}
50
51
52 \section{Loading Plugins}
53 Once the File daemon loads the plugins, it asks the OS for the
54 two entry points (loadPlugin and unloadPlugin) then calls the
55 {\bf loadPlugin} entry point (see below).
56
57 Bacula passes information to the plugin through this call and it gets
58 back information that it needs to use the plugin.  Later, Bacula
59  will call particular functions that are defined by the
60 {\bf loadPlugin} interface.  
61
62 When Bacula is finished with the plugin 
63 (when Bacula is going to exit), it will call the {\bf unloadPlugin}
64 entry point.
65
66 The two entry points are:
67
68 \begin{verbatim}
69 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
70
71 and
72
73 bRC unloadPlugin()
74 \end{verbatim}
75
76 both these external entry points to the shared object are defined as C entry
77 points to avoid name mangling complications with C++.  However, the shared
78 object can actually be written in any language (preferrably C or C++) providing
79 that it follows C language calling conventions.
80
81 The definitions for {\bf bRC} and the arguments are {\bf
82 src/filed/fd-plugins.h} and so this header file needs to be included in
83 your plugin.  It along with {\bf src/lib/plugins.h} define basically the whole
84 plugin interface.  Within this header file, it includes the following
85 files:
86
87 \begin{verbatim}
88 #include <sys/types.h>
89 #include "config.h"
90 #include "bc_types.h"
91 #include "lib/plugins.h"
92 #include <sys/stat.h>
93 \end{verbatim}
94
95 Aside from the {\bf bc\_types.h} and {\bf confit.h} headers, the plugin
96 definition uses the minimum code from Bacula.  The bc\_types.h file is required
97 to ensure that the data type defintions in arguments correspond to the Bacula
98 core code.
99
100 The return codes are defined as:
101 \begin{verbatim}
102 typedef enum {
103   bRC_OK    = 0,                         /* OK */
104   bRC_Stop  = 1,                         /* Stop calling other plugins */
105   bRC_Error = 2,                         /* Some kind of error */
106   bRC_More  = 3,                         /* More files to backup */
107 } bRC;
108 \end{verbatim}
109
110
111 At a future point in time, we hope to make the Bacula libbac.a into a
112 shared object so that the plugin can use much more of Bacula's
113 infrastructure, but for this first cut, we have tried to minimize the
114 dependence on Bacula.
115
116 \section{loadPlugin}
117 As previously mentioned, the {\bf loadPlugin} entry point in the plugin
118 is called immediately after Bacula loads the plugin when the File daemon
119 itself is first starting.  This entry point is only called once during the
120 execution of the File daemon.  In calling the
121 plugin, the first two arguments are information from Bacula that
122 is passed to the plugin, and the last two arguments are information
123 about the plugin that the plugin must return to Bacula.  The call is:
124
125 \begin{verbatim}
126 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
127 \end{verbatim}
128
129 and the arguments are:
130
131 \begin{description}
132 \item [lbinfo]
133 This is information about Bacula in general. Currently, the only value
134 defined in the bInfo structure is the version, which is the Bacula plugin 
135 interface version, currently defined as 1.  The {\bf size} is set to the
136 byte size of the structure. The exact definition of the bInfo structure
137 as of this writing is: 
138
139 \begin{verbatim}
140 typedef struct s_baculaInfo {
141    uint32_t size;
142    uint32_t version;
143 } bInfo;
144 \end{verbatim}
145
146 \item [lbfuncs]
147 The bFuncs structure defines the callback entry points within Bacula
148 that the plugin can use register events, get Bacula values, set
149 Bacula values, and send messages to the Job output or debug output.
150
151 The exact definition as of this writing is:
152 \begin{verbatim}
153 typedef struct s_baculaFuncs {
154    uint32_t size;
155    uint32_t version;
156    bRC (*registerBaculaEvents)(bpContext *ctx, ...);
157    bRC (*getBaculaValue)(bpContext *ctx, bVariable var, void *value);
158    bRC (*setBaculaValue)(bpContext *ctx, bVariable var, void *value);
159    bRC (*JobMessage)(bpContext *ctx, const char *file, int line,
160        int type, utime_t mtime, const char *fmt, ...);
161    bRC (*DebugMessage)(bpContext *ctx, const char *file, int line,
162        int level, const char *fmt, ...);
163    void *(*baculaMalloc)(bpContext *ctx, const char *file, int line,
164        size_t size);
165    void (*baculaFree)(bpContext *ctx, const char *file, int line, void *mem);
166 } bFuncs;
167 \end{verbatim}
168
169 We will discuss these entry points and how to use them a bit later when
170 describing the plugin code.
171
172
173 \item [pInfo]
174 When the loadPlugin entry point is called, the plugin must initialize
175 an information structure about the plugin and return a pointer to
176 this structure to Bacula.
177
178 The exact definition as of this writing is:
179
180 \begin{verbatim}
181 typedef struct s_pluginInfo {
182    uint32_t size;
183    uint32_t version;
184    const char *plugin_magic;
185    const char *plugin_license;
186    const char *plugin_author;
187    const char *plugin_date;
188    const char *plugin_version;
189    const char *plugin_description;
190 } pInfo;
191 \end{verbatim}
192
193 Where:
194  \begin{description}
195  \item [version] is the current Bacula defined plugin interface version, currently
196    set to 1. If the interface version differs from the current version of 
197    Bacula, the plugin will not be run (not yet implemented).
198  \item [plugin\_magic] is a pointer to the text string "*FDPluginData*", a
199    sort of sanity check.  If this value is not specified, the plugin
200    will not be run (not yet implemented).
201  \item [plugin\_license] is a pointer to a text string that describes the
202    plugin license. Bacula will only accept compatible licenses (not yet
203    implemented).
204  \item [plugin\_author] is a pointer to the text name of the author of the program.
205    This string can be anything but is generally the author's name.
206  \item [plugin\_date] is the pointer text string containing the date of the plugin.
207    This string can be anything but is generally some human readable form of 
208    the date.
209  \item [plugin\_version] is a pointer to a text string containing the version of
210    the plugin.  The contents are determined by the plugin writer.
211  \item [plugin\_description] is a pointer to a string describing what the
212    plugin does. The contents are determined by the plugin writer.
213  \end{description}
214
215 The pInfo structure must be defined in static memory because Bacula does not
216 copy it and may refer to the values at any time while the plugin is
217 loaded. All values must be supplied or the plugin will not run (not yet
218 implemented).  All text strings must be either ASCII or UTF-8 strings that
219 are terminated with a zero byte.
220
221 \item [pFuncs]
222 When the loadPlugin entry point is called, the plugin must initialize
223 an entry point structure about the plugin and return a pointer to
224 this structure to Bacula. This structure contains pointer to each
225 of the entry points that the plugin must provide for Bacula. When
226 Bacula is actually running the plugin, it will call the defined
227 entry points at particular times.  All entry points must be defined.
228
229 The pFuncs structure must be defined in static memory because Bacula does not
230 copy it and may refer to the values at any time while the plugin is
231 loaded.
232
233 The exact definition as of this writing is:
234
235 \begin{verbatim}
236 typedef struct s_pluginFuncs {
237    uint32_t size;
238    uint32_t version;
239    bRC (*newPlugin)(bpContext *ctx);
240    bRC (*freePlugin)(bpContext *ctx);
241    bRC (*getPluginValue)(bpContext *ctx, pVariable var, void *value);
242    bRC (*setPluginValue)(bpContext *ctx, pVariable var, void *value);
243    bRC (*handlePluginEvent)(bpContext *ctx, bEvent *event, void *value);
244    bRC (*startBackupFile)(bpContext *ctx, struct save_pkt *sp);
245    bRC (*endBackupFile)(bpContext *ctx);
246    bRC (*startRestoreFile)(bpContext *ctx, const char *cmd);
247    bRC (*endRestoreFile)(bpContext *ctx);
248    bRC (*pluginIO)(bpContext *ctx, struct io_pkt *io);
249    bRC (*createFile)(bpContext *ctx, struct restore_pkt *rp);
250    bRC (*setFileAttributes)(bpContext *ctx, struct restore_pkt *rp);
251    bRC (*checkFile)(bpContext *ctx, char *fname);
252 } pFuncs;
253 \end{verbatim}
254
255 The details of the entry points will be presented in
256 separate sections below. 
257
258 Where:
259  \begin{description}
260  \item [size] is the byte size of the structure.
261  \item [version] is the plugin interface version currently set to 3.
262  \end{description}
263
264 Sample code for loadPlugin:
265 \begin{verbatim}
266   bfuncs = lbfuncs;                  /* set Bacula funct pointers */
267   binfo  = lbinfo;
268   *pinfo  = &pluginInfo;             /* return pointer to our info */
269   *pfuncs = &pluginFuncs;            /* return pointer to our functions */
270
271    return bRC_OK;
272 \end{verbatim}
273
274 where pluginInfo and pluginFuncs are statically defined structures. 
275 See bpipe-fd.c for details.
276
277
278
279 \end{description}
280
281 \section{Plugin Entry Points}
282 This section will describe each of the entry points (subroutines) within
283 the plugin that the plugin must provide for Bacula, when they are called
284 and their arguments. As noted above, pointers to these subroutines are
285 passed back to Bacula in the pFuncs structure when Bacula calls the 
286 loadPlugin() externally defined entry point.
287
288 \subsection{newPlugin(bpContext *ctx)}
289   This is the entry point that Bacula will call
290   when a new "instance" of the plugin is created. This typically
291   happens at the beginning of a Job.  If 10 Jobs are running
292   simultaneously, there will be at least 10 instances of the
293   plugin.
294
295   The bpContext structure will be passed to the plugin, and
296   during this call, if the plugin needs to have its private
297   working storage that is associated with the particular
298   instance of the plugin, it should create it from the heap
299   (malloc the memory) and store a pointer to
300   its private working storage in the {\bf pContext} variable.
301   Note: since Bacula is a multi-threaded program, you must not
302   keep any variable data in your plugin unless it is truely meant
303   to apply globally to the whole plugin.  In addition, you must
304   be aware that except the first and last call to the plugin
305   (loadPlugin and unloadPlugin) all the other calls will be 
306   made by threads that correspond to a Bacula job.  The 
307   bpContext that will be passed for each thread will remain the
308   same throughout the Job thus you can keep your privat Job specific
309   data in it ({\bf bContext}).
310
311 \begin{verbatim}
312 typedef struct s_bpContext {
313   void *pContext;   /* Plugin private context */
314   void *bContext;   /* Bacula private context */
315 } bpContext;
316
317 \end{verbatim}
318    
319   This context pointer will be passed as the first argument to all
320   the entry points that Bacula calls within the plugin.  Needless
321   to say, the plugin should not change the bContext variable, which
322   is Bacula's private context pointer for this instance (Job) of this
323   plugin.
324
325 \subsection{freePlugin(bpContext *ctx)}
326 This entry point is called when the
327 this instance of the plugin is no longer needed (the Job is
328 ending), and the plugin should release all memory it may
329 have allocated for this particular instance (Job) i.e. the pContext.  
330 This is not the final termination
331 of the plugin signaled by a call to {\bf unloadPlugin}. 
332 Any other instances (Job) will
333 continue to run, and the entry point {\bf newPlugin} may be called
334 again if other jobs start.
335
336 \subsection{getPluginValue(bpContext *ctx, pVariable var, void *value)} 
337 Bacula will call this entry point to get
338 a value from the plugin.  This entry point is currently not called.
339
340 \subsection{setPluginValue(bpContext *ctx, pVariable var, void *value)}
341 Bacula will call this entry point to set
342 a value in the plugin.  This entry point is currently not called.
343  
344 \subsection{handlePluginEvent(bpContext *ctx, bEvent *event, void *value)}
345 This entry point is called when Bacula
346 encounters certain events (discussed below). This is, in fact, the 
347 main way that most plugins get control when a Job runs and how
348 they know what is happening in the job. It can be likened to the
349 {\bf RunScript} feature that calls external programs and scripts,
350 and is very similar to the Bacula Python interface.
351 When the plugin is called, Bacula passes it the pointer to an event
352 structure (bEvent), which currently has one item, the eventType:
353
354 \begin{verbatim}
355 typedef struct s_bEvent {
356    uint32_t eventType;
357 } bEvent;
358 \end{verbatim}
359
360   which defines what event has been triggered, and for each event,
361   Bacula will pass a pointer to a value associated with that event.
362   If no value is associated with a particular event, Bacula will 
363   pass a NULL pointer, so the plugin must be careful to always check
364   value pointer prior to dereferencing it.
365   
366   The current list of events are:
367
368 \begin{verbatim}
369 typedef enum {
370   bEventJobStart        = 1,
371   bEventJobEnd          = 2,
372   bEventStartBackupJob  = 3,
373   bEventEndBackupJob    = 4,
374   bEventStartRestoreJob = 5,
375   bEventEndRestoreJob   = 6,
376   bEventStartVerifyJob  = 7,
377   bEventEndVerifyJob    = 8,
378   bEventBackupCommand   = 9,
379   bEventRestoreCommand  = 10,
380   bEventLevel           = 11,
381   bEventSince           = 12,
382 } bEventType;
383
384 \end{verbatim}
385  
386 Most of the above are self-explanatory.
387
388 \begin{description}
389  \item [bEventJobStart] is called whenever a Job starts. The value
390    passed is a pointer to a string that contains: "Jobid=nnn 
391    Job=job-name". Where nnn will be replaced by the JobId and job-name
392    will be replaced by the Job name. The variable is temporary so if you
393    need the values, you must copy them.
394
395  \item [bEventJobEnd] is called whenever a Job ends. No value is passed.
396
397  \item [bEventStartBackupJob] is called when a Backup Job begins. No value
398    is passed.
399
400  \item [bEventEndBackupJob] is called when a Backup Job ends. No value is 
401    passed.
402
403  \item [bEventStartRestoreJob] is called when a Restore Job starts. No value
404    is passed.
405
406  \item [bEventEndRestoreJob] is called when a Restore Job ends. No value is
407    passed.
408
409  \item [bEventStartVerifyJob] is called when a Verify Job starts. No value
410    is passed.
411
412  \item [bEventEndVerifyJob] is called when a Verify Job ends. No value
413    is passed.
414
415  \item [bEventBackupCommand] is called prior to the bEventStartBackupJob and
416    the plugin is passed the command string (everything after the equal sign
417    in "Plugin =" as the value.
418
419    Note, if you intend to backup a file, this is an important first point to
420    write code that copies the command string passed into your pContext area
421    so that you will know that a backup is being performed and you will know
422    the full contents of the "Plugin =" command (i.e. what to backup and
423    what virtual filename the user wants to call it.
424
425  \item [bEventRestoreCommand] is called prior to the bEventStartRestoreJob and
426    the plugin is passed the command string (everything after the equal sign
427    in "Plugin =" as the value.
428
429    See the notes above concerning backup and the command string. This is the
430    point at which Bacula passes you the original command string that was
431    specified during the backup, so you will want to save it in your pContext
432    area for later use when Bacula calls the plugin again.
433
434  \item [bEventLevel] is called when the level is set for a new Job. The value
435    is a 32 bit integer stored in the void*, which represents the Job Level code.
436
437  \item [bEventSince] is called when the since time is set for a new Job. The 
438    value is a time\_t time at which the last job was run.
439 \end{description}
440
441 During each of the above calls, the plugin receives either no specific value or
442 only one value, which in some cases may not be sufficient.  However, knowing
443 the context of the event, the plugin can call back to the Bacula entry points
444 it was passed during the {\bf loadPlugin} call and get to a number of Bacula
445 variables.  (at the current time few Bacula variables are implemented, but it
446 easily extended at a future time and as needs require).
447
448 \subsection{startBackupFile(bpContext *ctx, struct save\_pkt *sp)}
449 This entry point is called only if your plugin is a command plugin, and 
450 it is called when Bacula encounters the "Plugin = " directive in
451 the Include section of the FileSet.
452 Called when beginning the backup of a file. Here Bacula provides you
453 with a pointer to the {\bf save\_pkt} structure and you must fill in 
454 this packet with the "attribute" data of the file.
455
456 \begin{verbatim}
457 struct save_pkt {
458    int32_t pkt_size;                  /* size of this packet */
459    char *fname;                       /* Full path and filename */
460    char *link;                        /* Link name if any */
461    struct stat statp;                 /* System stat() packet for file */
462    int32_t type;                      /* FT_xx for this file */
463    uint32_t flags;                    /* Bacula internal flags */
464    bool portable;                     /* set if data format is portable */
465    char *cmd;                         /* command */
466    int32_t pkt_end;                   /* end packet sentinel */
467 };
468 \end{verbatim}
469
470 The second argument is a pointer to the {\bf save\_pkt} structure for the file
471 to be backed up.  The plugin is responsible for filling in all the fields 
472 of the {\bf save\_pkt}. If you are backing up
473 a real file, then generally, the statp structure can be filled in by doing
474 a {\bf stat} system call on the file.  
475
476 If you are backing up a database or
477 something that is more complex, you might want to create a virtual file.
478 That is a file that does not actually exist on the filesystem, but represents 
479 say an object that you are backing up.  In that case, you need to ensure
480 that the {\bf fname} string that you pass back is unique so that it
481 does not conflict with a real file on the system, and you need to 
482 artifically create values in the statp packet.
483
484 Example programs such as {\bf bpipe-fd.c} show how to set these fields.  You
485 must take care not to store pointers the stack in the pointer fields such as
486 fname and link, because when you return from your function, your stack entries
487 will be destroyed. The solution in that case is to malloc() and return the
488 pointer to it. In order to not have memory leaks, you should store a pointer to
489 all memory allocated in your pContext structure so that in subsequent calls or
490 at termination, you can release it back to the system.
491
492 Once the backup has begun, Bacula will call your plugin at the {\bf pluginIO}
493 entry point to "read" the data to be backed up.  Please see the {\bf bpipe-fd.c}
494 plugin for how to do I/O.
495
496 Example of filling in the save\_pkt as used in bpipe-fd.c:
497
498 \begin{verbatim}
499    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
500    time_t now = time(NULL);
501    sp->fname = p_ctx->fname;
502    sp->statp.st_mode = 0700 | S_IFREG;
503    sp->statp.st_ctime = now;
504    sp->statp.st_mtime = now;
505    sp->statp.st_atime = now;
506    sp->statp.st_size = -1;
507    sp->statp.st_blksize = 4096;
508    sp->statp.st_blocks = 1;
509    p_ctx->backup = true;
510    return bRC_OK; 
511 \end{verbatim}
512
513 Note: the filename to be created has already been created from the 
514 command string previously sent to the plugin and is in the plugin 
515 context (p\_ctx->fname) and is a malloc()ed string.  This example
516 creates a regular file (S\_IFREG), with various fields being created.
517
518 In general, the sequence of commands issued from Bacula to the plugin
519 to do a backup while processing the "Plugin = " directive are:
520
521 \begin{enumerate}
522  \item generate a bEventBackupCommand event to the specified plugin
523        and pass it the command string.
524  \item make a startPluginBackup call to the plugin, which
525        fills in the data needed in save\_pkt to save as the file
526        attributes and to put on the Volume and in the catalog.
527  \item call Bacula's internal save\_file() subroutine to save the specified
528        file.  The plugin will then be called at pluginIO() to "open"
529        the file, and then to read the file data.
530        Note, if you are dealing with a virtual file, the "open" operation
531        is something the plugin does internally and it doesn't necessarily
532        mean opening a file on the filesystem.  For example in the case of
533        the bpipe-fd.c program, it initiates a pipe to the requested program.
534        Finally when the plugin signals to Bacula that all the data was read,
535        Bacula will call the plugin with the "close" pluginIO() function.
536 \end{enumerate}
537
538
539 \subsection{endBackupFile(bpContext *ctx)}
540 Called at the end of backing up a file for a command plugin.  If the plugin's
541 work is done, it should return bRC\_OK.  If the plugin wishes to create another
542 file and back it up, then it must return bRC\_More (not yet implemented).  This
543 is probably a good time to release any malloc()ed memory you used to pass back
544 filenames.
545
546 \subsection{startRestoreFile(bpContext *ctx, const char *cmd)}
547 Called when the first record is read from the Volume that was 
548 previously written by the command plugin.
549
550 \subsection{createFile(bpContext *ctx, struct restore\_pkt *rp)}
551 Called for a command plugin to create a file during a Restore job before 
552 restoring the data. 
553 This entry point is called before any I/O is done on the file.  After
554 this call, Bacula will call pluginIO() to open the file for write.
555
556 The data in the 
557 restore\_pkt is passed to the plugin and is based on the data that was
558 originally given by the plugin during the backup and the current user
559 restore settings (e.g. where, RegexWhere, replace).  This allows the
560 plugin to first create a file (if necessary) so that the data can
561 be transmitted to it.  The next call to the plugin will be a
562 pluginIO command with a request to open the file write-only.
563
564 This call must return one of the following values:
565
566 \begin{verbatim}
567  enum {
568    CF_SKIP = 1,       /* skip file (not newer or something) */
569    CF_ERROR,          /* error creating file */
570    CF_EXTRACT,        /* file created, data to extract */
571    CF_CREATED         /* file created, no data to extract */
572 };
573 \end{verbatim}
574
575 in the restore\_pkt value {\bf create\_status}.  For a normal file,
576 unless there is an error, you must return {\bf CF\_EXTRACT}.
577
578 \begin{verbatim}
579  
580 struct restore_pkt {
581    int32_t pkt_size;                  /* size of this packet */
582    int32_t stream;                    /* attribute stream id */
583    int32_t data_stream;               /* id of data stream to follow */
584    int32_t type;                      /* file type FT */
585    int32_t file_index;                /* file index */
586    int32_t LinkFI;                    /* file index to data if hard link */
587    uid_t uid;                         /* userid */
588    struct stat statp;                 /* decoded stat packet */
589    const char *attrEx;                /* extended attributes if any */
590    const char *ofname;                /* output filename */
591    const char *olname;                /* output link name */
592    const char *where;                 /* where */
593    const char *RegexWhere;            /* regex where */
594    int replace;                       /* replace flag */
595    int create_status;                 /* status from createFile() */
596    int32_t pkt_end;                   /* end packet sentinel */
597
598 };
599 \end{verbatim}
600
601 Typical code to create a regular file would be the following:
602
603 \begin{verbatim}
604    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
605    time_t now = time(NULL);
606    sp->fname = p_ctx->fname;   /* set the full path/filename I want to create */
607    sp->type = FT_REG;
608    sp->statp.st_mode = 0700 | S_IFREG;
609    sp->statp.st_ctime = now;
610    sp->statp.st_mtime = now;
611    sp->statp.st_atime = now;
612    sp->statp.st_size = -1;
613    sp->statp.st_blksize = 4096;
614    sp->statp.st_blocks = 1;
615    return bRC_OK;
616 \end{verbatim}
617
618 This will create a virtual file.  If you are creating a file that actually 
619 exists, you will most likely want to fill the statp packet using the
620 stat() system call.
621
622 Creating a directory is similar, but requires a few extra steps:
623
624 \begin{verbatim}
625    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
626    time_t now = time(NULL);
627    sp->fname = p_ctx->fname;   /* set the full path I want to create */
628    sp->link = xxx; where xxx is p_ctx->fname with a trailing forward slash
629    sp->type = FT_DIREND
630    sp->statp.st_mode = 0700 | S_IFDIR;
631    sp->statp.st_ctime = now;
632    sp->statp.st_mtime = now;
633    sp->statp.st_atime = now;
634    sp->statp.st_size = -1;
635    sp->statp.st_blksize = 4096;
636    sp->statp.st_blocks = 1;
637    return bRC_OK;
638 \end{verbatim}
639
640 The link field must be set with the full cononical path name, which always 
641 ends with a forward slash.  If you do not terminate it with a forward slash,
642 you will surely have problems later.
643
644 As with the example that creates a file, if you are backing up a real
645 directory, you will want to do an stat() on the directory.  
646
647 Note, if you want the directory permissions and times to be correctly
648 restored, you must create the directory {\bf after} all the file directories
649 have been sent to Bacula. That allows the restore process to restore all the
650 files in a directory using default directory options, then at the end, restore
651 the directory permissions.  If you do it the other way around, each time you
652 restore a file, the OS will modify the time values for the directory entry.
653
654 \subsection{setFileAttributes(bpContext *ctx, struct restore\_pkt *rp)}
655 This is call not yet implemented.  Called for a command plugin.
656
657 See the definition of {\bf restre\_pkt} in the above section.
658
659 \subsection{endRestoreFile(bpContext *ctx)}
660 Called when a command plugin is done restoring a file.
661
662 \subsection{pluginIO(bpContext *ctx, struct io\_pkt *io)}
663 Called to do the input (backup) or output (restore) of data from or to a file
664 for a command plugin. These routines simulate the Unix read(), write(), open(),
665 close(), and lseek() I/O calls, and the arguments are passed in the packet and
666 the return values are also placed in the packet.  In addition for Win32 systems
667 the plugin must return two additional values (described below).
668
669 \begin{verbatim}
670  enum {
671    IO_OPEN = 1,
672    IO_READ = 2,
673    IO_WRITE = 3,
674    IO_CLOSE = 4,
675    IO_SEEK = 5
676 };
677
678 struct io_pkt {
679    int32_t pkt_size;                  /* Size of this packet */
680    int32_t func;                      /* Function code */
681    int32_t count;                     /* read/write count */
682    mode_t mode;                       /* permissions for created files */
683    int32_t flags;                     /* Open flags */
684    char *buf;                         /* read/write buffer */
685    const char *fname;                 /* open filename */
686    int32_t status;                    /* return status */
687    int32_t io_errno;                  /* errno code */
688    int32_t lerror;                    /* Win32 error code */
689    int32_t whence;                    /* lseek argument */
690    boffset_t offset;                  /* lseek argument */
691    bool win32;                        /* Win32 GetLastError returned */
692    int32_t pkt_end;                   /* end packet sentinel */
693 };
694 \end{verbatim}
695
696 The particular Unix function being simulated is indicated by the {\bf func},
697 which will have one of the IO\_OPEN, IO\_READ, ... codes listed above.  The
698 status code that would be returned from a Unix call is returned in {\bf status}
699 for IO\_OPEN, IO\_CLOSE, IO\_READ, and IO\_WRITE. The return value for IO\_SEEK
700 is returned in {\bf offset} which in general is a 64 bit value.
701
702 When there is an error on Unix systems, you must always set io\_error, and
703 on a Win32 system, you must always set win32, and the returned value from
704 the OS call GetLastError() in lerror.
705
706 For all except IO\_SEEK, {\bf status} is the return result.  In general it is
707 a positive integer unless there is an error in which case it is -1.
708
709 The following describes each call and what you get and what you
710 should return:
711
712 \begin{description}
713  \item [IO\_OPEN]
714    You will be passed fname, mode, and flags.
715    You must set on return: status, and if there is a Unix error
716    io\_errno must be set to the errno value, and if there is a 
717    Win32 error win32 and lerror. 
718
719  \item [IO\_READ]
720   You will be passed: count, and buf (buffer of size count).
721   You must set on return: status to the number of bytes 
722   read into the buffer (buf) or -1 on an error, 
723   and if there is a Unix error
724   io\_errno must be set to the errno value, and if there is a
725   Win32 error, win32 and lerror must be set.
726
727  \item [IO\_WRITE]
728   You will be passed: count, and buf (buffer of size count).
729   You must set on return: status to the number of bytes 
730   written from the buffer (buf) or -1 on an error, 
731   and if there is a Unix error
732   io\_errno must be set to the errno value, and if there is a
733   Win32 error, win32 and lerror must be set.
734
735  \item [IO\_CLOSE]
736   Nothing will be passed to you.  On return you must set 
737   status to 0 on success and -1 on failure.  If there is a Unix error
738   io\_errno must be set to the errno value, and if there is a
739   Win32 error, win32 and lerror must be set.
740
741  \item [IO\_LSEEK]
742   You will be passed: offset, and whence. offset is a 64 bit value
743   and is the position to seek to relative to whence.  whence is one
744   of the following SEEK\_SET, SEEK\_CUR, or SEEK\_END indicating to
745   either to seek to an absolute possition, relative to the current 
746   position or relative to the end of the file.
747   You must pass back in offset the absolute location to which you 
748   seeked. If there is an error, offset should be set to -1.
749   If there is a Unix error
750   io\_errno must be set to the errno value, and if there is a
751   Win32 error, win32 and lerror must be set.
752
753   Note: Bacula will call IO\_SEEK only when writing a sparse file.
754   
755 \end{description}
756
757 \subsection{bool checkFile(bpContext *ctx, char *fname)}
758 If this entry point is set, Bacula will call it after backing up all file
759 data during an Accurate backup.  It will be passed the full filename for
760 each file that Bacula is proposing to mark as deleted.  Only files
761 previously backed up but not backed up in the current session will be
762 marked to be deleted.  If you return {\bf false}, the file will be be
763 marked deleted.  If you return {\bf true} the file will not be marked
764 deleted.  This permits a plugin to ensure that previously saved virtual
765 files or files controlled by your plugin that have not change (not backed
766 up in the current job) are not marked to be deleted.  This entry point will
767 only be called during Accurate Incrmental and Differential backup jobs.
768
769
770 \section{Bacula Plugin Entrypoints}
771 When Bacula calls one of your plugin entrypoints, you can call back to
772 the entrypoints in Bacula that were supplied during the xxx plugin call
773 to get or set information within Bacula.
774
775 \subsection{bRC registerBaculaEvents(bpContext *ctx, ...)}
776 This Bacula entrypoint will allow you to register to receive events
777 that are not autmatically passed to your plugin by default. This 
778 entrypoint currently is unimplemented.
779
780 \subsection{bRC getBaculaValue(bpContext *ctx, bVariable var, void *value)}
781 Calling this entrypoint, you can obtain specific values that are available
782 in Bacula. The following Variables can be referenced:
783 \begin{itemize}
784 \item bVarJobId returns an int
785 \item bVarFDName returns a char *
786 \item bVarLevel returns an int
787 \item bVarClient returns a char *
788 \item bVarJobName returns a char *
789 \item bVarJobStatus returns an int
790 \item bVarSinceTime returns an int (time\_t)
791 \item bVarAccurate returns an int
792 \end{itemize}
793
794 \subsection{bRC setBaculaValue(bpContext *ctx, bVariable var, void *value)}
795 Calling this entrypoint allows you to set particular values in
796 Bacula. The only variable that can currently be set is 
797 {\bf bVarFileSeen} and the value passed is a char * that points
798 to the full filename for a file that you are indicating has been
799 seen and hence is not deleted.
800
801 \subsection{bRC JobMessage(bpContext *ctx, const char *file, int line,
802        int type, utime\_t mtime, const char *fmt, ...)}
803 This call permits you to put a message in the Job Report.
804
805
806 \subsection{bRC DebugMessage(bpContext *ctx, const char *file, int line,
807        int level, const char *fmt, ...)}
808 This call permits you to print a debug message.
809
810
811 \subsection{void baculaMalloc(bpContext *ctx, const char *file, int line,
812        size\_t size)}
813 This call permits you to obtain memory from Bacula's memory allocator.
814
815
816 \subsection{void baculaFree(bpContext *ctx, const char *file, int line, void *mem)}
817 This call permits you to free memory obtained from Bacula's memory allocator.
818
819 \section{Building Bacula Plugins}
820 There is currently one sample program {\bf example-plugin-fd.c} and
821 one working plugin {\bf bpipe-fd.c} that can be found in the Bacula
822 {\bf src/plugins/fd} directory.  Both are built with the following:
823
824 \begin{verbatim}
825  cd <bacula-source>
826  ./configure <your-options>
827  make
828  ...
829  cd src/plugins/fd
830  make
831  make test
832 \end{verbatim}
833
834 After building Bacula and changing into the src/plugins/fd directory,
835 the {\bf make} command will build the {\bf bpipe-fd.so} plugin, which 
836 is a very useful and working program.
837
838 The {\bf make test} command will build the {\bf example-plugin-fd.so}
839 plugin and a binary named {\bf main}, which is build from the source
840 code located in {\bf src/filed/fd\_plugins.c}. 
841
842 If you execute {\bf ./main}, it will load and run the example-plugin-fd
843 plugin simulating a small number of the calling sequences that Bacula uses
844 in calling a real plugin.  This allows you to do initial testing of 
845 your plugin prior to trying it with Bacula.
846
847 You can get a good idea of how to write your own plugin by first 
848 studying the example-plugin-fd, and actually running it.  Then
849 it can also be instructive to read the bpipe-fd.c code as it is 
850 a real plugin, which is still rather simple and small.
851
852 When actually writing your own plugin, you may use the example-plugin-fd.c
853 code as a template for your code.
854