]> git.sur5r.net Git - bacula/docs/blob - docs/manuals/en/developers/generaldevel.tex
33312af460dfa8999c8311d4e6b3921deab3af8b
[bacula/docs] / docs / manuals / en / developers / generaldevel.tex
1 %%
2 %%
3
4 \chapter{Bacula Developer Notes}
5 \label{_ChapterStart10}
6 \index{Bacula Developer Notes}
7 \index{Notes!Bacula Developer}
8 \addcontentsline{toc}{section}{Bacula Developer Notes}
9
10 This document is intended mostly for developers and describes how you can
11 contribute to the Bacula project and the the general framework of making
12 Bacula source changes.
13
14 \subsection{Contributions}
15 \index{Contributions}
16 \addcontentsline{toc}{subsubsection}{Contributions}
17
18 Contributions to the Bacula project come in many forms: ideas,
19 participation in helping people on the bacula-users email list, packaging
20 Bacula binaries for the community, helping improve the documentation, and
21 submitting code.
22
23 Contributions in the form of submissions for inclusion in the project are
24 broken into two groups.  The first are contributions that are aids and not
25 essential to Bacula.  In general, these will be scripts or will go into the
26 {\bf bacula/examples} directory.  For these kinds of non-essential
27 contributions there is no obligation to do a copyright assignment as
28 described below.  However, a copyright assignment would still be
29 appreciated.
30
31 The second class of contributions are those which will be integrated with
32 Bacula and become an essential part (code, scripts, documentation, ...)
33 Within this class of contributions, there are two hurdles to surmount.  One
34 is getting your patch accepted, and two is dealing with copyright issues.
35 The following text describes some of the requirements for such code.
36
37 \subsection{Patches}
38 \index{Patches}
39 \addcontentsline{toc}{subsubsection}{Patches}
40
41 Subject to the copyright assignment described below, your patches should be
42 sent in {\bf git format-patch} format relative to the current contents of the 
43 master branch of the Source Forge Git repository.  Please attach the
44 output file or files generated by the {\bf git format-patch} to the email
45 rather than include them directory to avoid wrapping of the lines
46 in the patch.  Please be sure to use the Bacula
47 indenting standard (see below) for source code.  If you have checked out
48 the source with Git, you can get a diff using.
49
50 \begin{verbatim}
51 git pull
52 git format-patch -M
53 \end{verbatim}
54
55 If you plan on doing significant development work over a period of time,
56 after having your first patch reviewed and approved, you will be eligible
57 for having developer Git write access so that you can commit your changes
58 directly to the Git repository.  To do so, you will need a userid on Source
59 Forge.
60
61 \subsection{Copyrights}
62 \index{Copyrights}
63 \addcontentsline{toc}{subsubsection}{Copyrights}
64
65 To avoid future problems concerning changing licensing or
66 copyrights, all code contributions more than a hand full of lines
67 must be in the Public Domain or have the copyright transferred to
68 the Free Software Foundation Europe e.V. with a Fiduciary License
69 Agreement (FLA) as the case for all the current code.  
70
71 Prior to November 2004, all the code was copyrighted by Kern Sibbald and
72 John Walker.  After November 2004, the code was copyrighted by Kern
73 Sibbald, then on the 15th of November 2006, Kern transferred the copyright
74 to the Free Software Foundation Europe e.V. In signing the FLA and
75 transferring the copyright, you retain the right to use the code you have
76 submitted as you want, and you ensure that Bacula will always remain Free
77 and Open Source.
78
79 Your name should be clearly indicated as the author of the code, and you
80 must be extremely careful not to violate any copyrights or patents or use
81 other people's code without acknowledging it.  The purpose of this
82 requirement is to avoid future copyright, patent, or intellectual property
83 problems.  Please read the LICENSE agreement in the main Bacula source code
84 directory.  When you sign the Fiduciary License Agreement (FLA) and send it
85 in, you are agreeing to the terms of that LICENSE file.
86
87 If you don't understand what we mean by future problems, please
88 examine the difficulties Mozilla was having finding
89 previous contributors at \elink{
90 http://www.mozilla.org/MPL/missing.html}
91 {http://www.mozilla.org/MPL/missing.html}. The other important issue is to
92 avoid copyright, patent, or intellectual property violations as was
93 (May 2003) claimed by SCO against IBM. 
94
95 Although the copyright will be held by the Free Software
96 Foundation Europe e.V., each developer is expected to indicate
97 that he wrote and/or modified a particular module (or file) and
98 any other sources.  The copyright assignment may seem a bit
99 unusual, but in reality, it is not.  Most large projects require
100 this.
101
102 If you have any doubts about this, please don't hesitate to ask.  The
103 objective is to assure the long term survival of the Bacula project. 
104
105 Items not needing a copyright assignment are: most small changes,
106 enhancements, or bug fixes of 5-10 lines of code, which amount to    
107 less than 20% of any particular file.
108
109 \subsection{Copyright Assignment -- Fiduciary License Agreement}
110 \index{Copyright Assignment}
111 \index{Assignment!Copyright}
112 \addcontentsline{toc}{subsubsection}{Copyright Assignment -- Fiduciary License Agreement}
113
114 Since this is not a commercial enterprise, and we prefer to believe in
115 everyone's good faith, previously developers could assign the copyright by
116 explicitly acknowledging that they do so in their first submission.  This
117 was sufficient if the developer is independent, or an employee of a
118 not-for-profit organization or a university.  However, in an effort to
119 ensure that the Bacula code is really clean, beginning in August 2006, all
120 previous and future developers with SVN write access will be asked to submit a
121 copyright assignment (or Fiduciary License Agreement -- FLA),
122 which means you agree to the LICENSE in the main source
123 directory. It also means that you receive back the right to use
124 the code that you have submitted.
125
126 Any developer who wants to contribute and is employed by a company should
127 either list the employer as the owner of the code, or get explicit
128 permission from him to sign the copyright assignment.  This is because in
129 many countries, all work that an employee does whether on company time or
130 in the employee's free time is considered to be Intellectual Property of
131 the company.  Obtaining official approval or an FLA from the company will
132 avoid misunderstandings between the employee, the company, and the Bacula
133 project.  A good number of companies have already followed this procedure.
134
135 The Fiduciary License Agreement is posted on the Bacula web site at:
136 \elink{http://www.bacula.org/en/FLA-bacula.en.pdf}{http://www.bacula.org/en/FLA-bacula.en.pdf}
137
138 The instructions for filling out this agreement are also at:
139 \elink{http://www.bacula.org/?page=fsfe}{http://www.bacula.org/?page=fsfe}
140
141 It should be filled out, then sent to:
142
143 \begin{verbatim}
144      Kern Sibbald
145      Cotes-de-Montmoiret 9
146      1012 Lausanne
147      Switzerland
148 \end{verbatim}
149
150 Please note that the above address is different from the officially
151 registered office mentioned in the document.  When you send in such a
152 complete document, please notify me: kern at sibbald dot com, and 
153 please add your email address to the FLA so that I can contact you
154 to confirm reception of the signed FLA.
155
156
157 \section{The Development Cycle}
158 \index{Developement Cycle}
159 \index{Cycle!Developement}
160 \addcontentsline{toc}{subsubsection}{Development Cycle}
161
162 As discussed on the email lists, the number of contributions are
163 increasing significantly.  We expect this positive trend
164 will continue.  As a consequence, we have modified how we do
165 development, and instead of making a list of all the features that we will
166 implement in the next version, each developer signs up for one (maybe
167 two) projects at a time, and when they are complete, and the code
168 is stable, we will release a new version.  The release cycle will probably
169 be roughly six months.
170
171 The difference is that with a shorter release cycle and fewer released
172 feature, we will have more time to review the new code that is being
173 contributed, and will be able to devote more time to a smaller number of
174 projects (some prior versions had too many new features for us to handle
175 correctly).
176
177 Future release schedules will be much the same, and the
178 number of new features will also be much the same providing that the
179 contributions continue to come -- and they show no signs of let up :-)
180
181 \index{Feature Requests}
182 {\bf Feature Requests:} \\
183 In addition, we have "formalizee" the feature requests a bit.
184
185 Instead of me maintaining an informal list of everything I run into 
186 (kernstodo), we now maintain a "formal" list of projects.  This 
187 means that all new feature requests, including those recently discussed on 
188 the email lists, must be formally submitted and approved. 
189
190 Formal submission of feature requests will take two forms: \\
191 1. non-mandatory, but highly recommended is to discuss proposed new features
192 on the mailing list.\\
193 2.  Formal submission of an Feature Request in a special format.  We'll
194 give an example of this below, but you can also find it on the web site
195 under "Support -\gt{} Feature Requests".  Since it takes a bit of time to
196 properly fill out a Feature Request form, you probably should check on the
197 email list first.
198
199 Once the Feature Request is received by the keeper of the projects list, it
200 will be sent to the Bacula project manager (Kern), and he will either
201 accept it (90% of the time), send it back asking for clarification (10% of
202 the time), send it to the email list asking for opinions, or reject it
203 (very few cases).
204
205 If it is accepted, it will go in the "projects" file (a simple ASCII file) 
206 maintained in the main Bacula source directory.
207
208 {\bf Implementation of Feature Requests:}\\
209 Any qualified developer can sign up for a project.  The project must have
210 an entry in the projects file, and the developer's name will appear in the
211 Status field.
212
213 {\bf How Feature Requests are accepted:}\\
214 Acceptance of Feature Requests depends on several things: \\
215 1.  feedback from users.  If it is negative, the Feature Request will probably not be
216 accepted.  \\
217 2.  the difficulty of the project.  A project that is so
218 difficult that we cannot imagine finding someone to implement probably won't
219 be accepted. Obviously if you know how to implement it, don't hesitate
220 to put it in your Feature Request  \\
221  3.  whether or not the Feature Request fits within the current strategy of
222 Bacula (for example an Feature Request that requests changing the tape to
223 tar format probably would not be accepted, ...).
224
225 {\bf How Feature Requests are prioritized:}\\
226 Once an Feature Request is accepted, it needs to be implemented.  If you
227 can find a developer for it, or one signs up for implementing it, then the
228 Feature Request becomes top priority (at least for that developer).
229
230 Between releases of Bacula, we will generally solicit Feature Request input
231 for the next version, and by way of this email, we suggest that you send
232 discuss and send in your Feature Requests for the next release.  Please
233 verify that the Feature Request is not in the current list (attached to this email).
234
235 Once users have had several weeks to submit Feature Requests, the keeper of
236 the projects list will organize them, and request users to vote on them.
237 This will allow fixing prioritizing the Feature Requests.  Having a
238 priority is one thing, but getting it implement is another thing -- we are
239 hoping that the Bacula community will take more responsibility for assuring
240 the implementation of accepted Feature Requests.
241
242 Feature Request format:
243 \begin{verbatim}
244 ============= Empty Feature Request form ===========
245 Item n:   One line summary ...
246   Date:   Date submitted
247   Origin: Name and email of originator.
248   Status:
249
250   What:   More detailed explanation ...
251
252   Why:    Why it is important ...
253
254   Notes:  Additional notes or features (omit if not used)
255 ============== End Feature Request form ==============
256 \end{verbatim}
257
258 \begin{verbatim}
259 ============= Example Completed  Feature Request form ===========
260 Item 1:   Implement a Migration job type that will move the job
261           data from one device to another.
262   Origin: Sponsored by Riege Sofware International GmbH. Contact:
263           Daniel Holtkamp <holtkamp at riege dot com>
264   Date:   28 October 2005
265   Status: Partially coded in 1.37 -- much more to do. Assigned to
266           Kern.
267
268   What:   The ability to copy, move, or archive data that is on a
269           device to another device is very important.
270
271   Why:    An ISP might want to backup to disk, but after 30 days
272           migrate the data to tape backup and delete it from
273           disk.  Bacula should be able to handle this
274           automatically.  It needs to know what was put where,
275           and when, and what to migrate -- it is a bit like
276           retention periods.  Doing so would allow space to be
277           freed up for current backups while maintaining older
278           data on tape drives.
279
280   Notes:  Migration could be triggered by:
281            Number of Jobs
282            Number of Volumes
283            Age of Jobs
284            Highwater size (keep total size)
285            Lowwater mark
286 =================================================
287 \end{verbatim}
288
289
290 \section{Bacula Code Submissions and Projects}
291 \index{Submissions and Projects}
292 \addcontentsline{toc}{subsection}{Code Submissions and Projects}
293
294 Getting code implemented in Bacula works roughly as follows:
295
296 \begin{itemize}
297
298 \item Kern is the project manager, but prefers not to be a "gate keeper".
299       This means that the developers are expected to be self-motivated,
300       and once they have experience submit directly to the Git
301       repositories. However,
302       it is a good idea to have your patches reviewed prior to submitting,
303       and it is a bad idea to submit monster patches because no one will
304       be able to properly review them. See below for more details on this.
305
306 \item  There are growing numbers of contributions (very good).
307
308 \item Some contributions come in the form of relatively small patches,
309      which Kern reviews, integrates, documents, tests, and maintains.
310
311 \item All Bacula developers take full
312    responsibility for writing the code, posting as patches so that we can
313    review it as time permits, integrating it at an appropriate time,
314    responding to our requests for tweaking it (name changes, ...),
315    document it in the code, document it in the manual (even though
316    their mother tongue is not English), test it, develop and commit
317    regression scripts, and answer in a timely fashion all bug reports --
318    even occasionally accepting additional bugs :-)
319
320    This is a sustainable way of going forward with Bacula, and the
321    direction that the project will be taking more and more.  For
322    example, in the past, we have had some very dedicated programmers
323    who did major projects. However, some of these
324    programmers due to outside obligations (job responsibilities change of
325    job, school duties, ...) could not continue to maintain the code.  In
326    those cases, the code suffers from lack of maintenance, sometimes we
327    patch it, sometimes not.  In the end, if the code is not maintained, the
328    code gets dropped from the project (there are two such contributions
329    that are heading in that direction).  When ever possible, we would like
330    to avoid this, and ensure a continuation of the code and a sharing of
331    the development, debugging, documentation, and maintenance
332    responsibilities.
333 \end{itemize}
334
335 \section{Patches for Released Versions}
336 \index{Patches for Released Versions}
337 \addcontentsline{toc}{subsection}{Patches for Released Versions}
338 If you fix a bug in a released version, you should, unless it is
339 an absolutely trivial bug, create and release a patch file for the
340 bug.  The procedure is as follows:
341
342 Fix the bug in the released branch and in the develpment master branch.
343
344 Make a patch file for the branch and add the branch patch to 
345 the patches directory in both the branch and the trunk.
346 The name should be 2.2.4-xxx.patch  where xxx is unique, in this case it can 
347 be "restore", e.g. 2.2.4-restore.patch.  Add to the top of the
348 file  a brief description and instructions for applying it -- see for example 
349 2.2.4-poll-mount.patch.  The best way to create the patch file is as
350 follows:
351
352 \begin{verbatim}
353   (edit) 2.2.4-restore.patch
354   (input description)
355   (end edit)
356
357    git format-patch -M
358    mv 0001-xxx 2.2.4-restore.patch
359 \end{verbatim}
360
361 check to make sure no extra junk got put into the patch file (i.e.
362 it should have the patch for that bug only).
363
364 If there is not a bug report on the problem, create one, then add the
365 patch to the bug report. 
366
367 Then upload it to the 2.2.x release of bacula-patches.
368
369 So, end the end, the patch file is:
370 \begin{itemize}
371 \item Attached to the bug report
372
373 \item In Branch-2.2/bacula/patches/...
374
375 \item In the trunk
376
377 \item Loaded on Source Forge bacula-patches 2.2.x release. When
378    you add it, click on the check box to send an Email so that all the
379    users that are monitoring SF patches get notified.
380 \end{itemize}
381
382
383 \section{Developing Bacula}
384 \index{Developing Bacula}
385 \index{Bacula!Developing}
386 \addcontentsline{toc}{subsubsection}{Developing Bacula}
387
388 Typically the simplest way to develop Bacula is to open one xterm window
389 pointing to the source directory you wish to update; a second xterm window at
390 the top source directory level, and a third xterm window at the bacula
391 directory \lt{}top\gt{}/src/bacula. After making source changes in one of the
392 directories, in the top source directory xterm, build the source, and start
393 the daemons by entering: 
394
395 make and 
396
397 ./startit then in the enter: 
398
399 ./console or 
400
401 ./gnome-console to start the Console program. Enter any commands for testing.
402 For example: run kernsverify full. 
403
404 Note, the instructions here to use {\bf ./startit} are different from using a
405 production system where the administrator starts Bacula by entering {\bf
406 ./bacula start}. This difference allows a development version of {\bf Bacula}
407 to be run on a computer at the same time that a production system is running.
408 The {\bf ./startit} strip starts {\bf Bacula} using a different set of
409 configuration files, and thus permits avoiding conflicts with any production
410 system. 
411
412 To make additional source changes, exit from the Console program, and in the
413 top source directory, stop the daemons by entering: 
414
415 ./stopit then repeat the process. 
416
417 \subsection{Debugging}
418 \index{Debugging}
419 \addcontentsline{toc}{subsubsection}{Debugging}
420
421 Probably the first thing to do is to turn on debug output. 
422
423 A good place to start is with a debug level of 20 as in {\bf ./startit -d20}.
424 The startit command starts all the daemons with the same debug level.
425 Alternatively, you can start the appropriate daemon with the debug level you
426 want. If you really need more info, a debug level of 60 is not bad, and for
427 just about everything a level of 200. 
428
429 \subsection{Using a Debugger}
430 \index{Using a Debugger}
431 \index{Debugger!Using a}
432 \addcontentsline{toc}{subsubsection}{Using a Debugger}
433
434 If you have a serious problem such as a segmentation fault, it can usually be
435 found quickly using a good multiple thread debugger such as {\bf gdb}. For
436 example, suppose you get a segmentation violation in {\bf bacula-dir}. You
437 might use the following to find the problem: 
438
439 \lt{}start the Storage and File daemons\gt{}
440 cd dird
441 gdb ./bacula-dir
442 run -f -s -c ./dird.conf
443 \lt{}it dies with a segmentation fault\gt{}
444 where
445 The {\bf -f} option is specified on the {\bf run} command to inhibit {\bf
446 dird} from going into the background. You may also want to add the {\bf -s}
447 option to the run command to disable signals which can potentially interfere
448 with the debugging. 
449
450 As an alternative to using the debugger, each {\bf Bacula} daemon has a built
451 in back trace feature when a serious error is encountered. It calls the
452 debugger on itself, produces a back trace, and emails the report to the
453 developer. For more details on this, please see the chapter in the main Bacula
454 manual entitled ``What To Do When Bacula Crashes (Kaboom)''. 
455
456 \subsection{Memory Leaks}
457 \index{Leaks!Memory}
458 \index{Memory Leaks}
459 \addcontentsline{toc}{subsubsection}{Memory Leaks}
460
461 Because Bacula runs routinely and unattended on client and server machines, it
462 may run for a long time. As a consequence, from the very beginning, Bacula
463 uses SmartAlloc to ensure that there are no memory leaks. To make detection of
464 memory leaks effective, all Bacula code that dynamically allocates memory MUST
465 have a way to release it. In general when the memory is no longer needed, it
466 should be immediately released, but in some cases, the memory will be held
467 during the entire time that Bacula is executing. In that case, there MUST be a
468 routine that can be called at termination time that releases the memory. In
469 this way, we will be able to detect memory leaks. Be sure to immediately
470 correct any and all memory leaks that are printed at the termination of the
471 daemons. 
472
473 \subsection{Special Files}
474 \index{Files!Special}
475 \index{Special Files}
476 \addcontentsline{toc}{subsubsection}{Special Files}
477
478 Kern uses files named 1, 2, ... 9 with any extension as scratch files. Thus
479 any files with these names are subject to being rudely deleted at any time. 
480
481 \subsection{When Implementing Incomplete Code}
482 \index{Code!When Implementing Incomplete}
483 \index{When Implementing Incomplete Code}
484 \addcontentsline{toc}{subsubsection}{When Implementing Incomplete Code}
485
486 Please identify all incomplete code with a comment that contains 
487
488 \begin{verbatim}
489 ***FIXME***
490 \end{verbatim} 
491
492 where there are three asterisks (*) before and after the word
493 FIXME (in capitals) and no intervening spaces. This is important as it allows
494 new programmers to easily recognize where things are partially implemented. 
495
496 \subsection{Bacula Source File Structure}
497 \index{Structure!Bacula Source File}
498 \index{Bacula Source File Structure}
499 \addcontentsline{toc}{subsubsection}{Bacula Source File Structure}
500
501 The distribution generally comes as a tar file of the form {\bf
502 bacula.x.y.z.tar.gz} where x, y, and z are the version, release, and update
503 numbers respectively. 
504
505 Once you detar this file, you will have a directory structure as follows: 
506
507 \footnotesize
508 \begin{verbatim}
509 |
510 Tar file:
511 |- depkgs
512    |- mtx              (autochanger control program + tape drive info)
513    |- sqlite           (SQLite database program)
514
515 Tar file:
516 |- depkgs-win32
517    |- pthreads         (Native win32 pthreads library -- dll)
518    |- zlib             (Native win32 zlib library)
519    |- wx               (wxWidgets source code)
520
521 Project bacula:
522 |- bacula              (main source directory containing configuration
523    |                    and installation files)
524    |- autoconf         (automatic configuration files, not normally used
525    |                    by users)
526    |- intl             (programs used to translate)
527    |- platforms        (OS specific installation files)
528       |- redhat        (Red Hat installation)
529       |- solaris       (Sun installation)
530       |- freebsd       (FreeBSD installation)
531       |- irix          (Irix installation -- not tested)
532       |- unknown       (Default if system not identified)
533    |- po               (translations of source strings)
534    |- src              (source directory; contains global header files)
535       |- cats          (SQL catalog database interface directory)
536       |- console       (bacula user agent directory)
537       |- dird          (Director daemon)
538       |- filed         (Unix File daemon)
539          |- win32      (Win32 files to make bacula-fd be a service)
540       |- findlib       (Unix file find library for File daemon)
541       |- gnome-console (GNOME version of console program)
542       |- lib           (General Bacula library)
543       |- stored        (Storage daemon)
544       |- tconsole      (Tcl/tk console program -- not yet working)
545       |- testprogs     (test programs -- normally only in Kern's tree)
546       |- tools         (Various tool programs)
547       |- win32         (Native Win32 File daemon)
548          |- baculafd   (Visual Studio project file)
549          |- compat     (compatibility interface library)
550          |- filed      (links to src/filed)
551          |- findlib    (links to src/findlib)
552          |- lib        (links to src/lib)
553          |- console    (beginning of native console program)
554          |- wx-console (wxWidget console Win32 specific parts)
555      |- wx-console     (wxWidgets console main source program)
556
557 Project regress:
558 |- regress             (Regression scripts)
559    |- bin              (temporary directory to hold Bacula installed binaries)
560    |- build            (temporary directory to hold Bacula source)
561    |- scripts          (scripts and .conf files)
562    |- tests            (test scripts)
563    |- tmp              (temporary directory for temp files)
564    |- working          (temporary working directory for Bacula daemons)
565
566 Project docs:
567 |- docs                (documentation directory)
568    |- developers       (Developer's guide)
569    |- home-page        (Bacula's home page source)
570    |- manual           (html document directory)
571    |- manual-fr        (French translation) 
572    |- manual-de        (German translation) 
573    |- techlogs         (Technical development notes);
574
575 Project rescue:
576 |- rescue              (Bacula rescue CDROM)
577    |- linux            (Linux rescue CDROM)
578       |- cdrom         (Linux rescue CDROM code)
579         ...
580    |- solaris          (Solaris rescue -- incomplete) 
581    |- freebsd          (FreeBSD rescue -- incomplete)
582
583 Project gui:
584 |- gui                 (Bacula GUI projects)
585   |- bacula-web        (Bacula web php management code)
586   |- bimagemgr         (Web application for burning CDROMs)
587
588
589 \end{verbatim}
590 \normalsize
591
592 \subsection{Header Files}
593 \index{Header Files}
594 \index{Files!Header}
595 \addcontentsline{toc}{subsubsection}{Header Files}
596
597 Please carefully follow the scheme defined below as it permits in general only
598 two header file includes per C file, and thus vastly simplifies programming.
599 With a large complex project like Bacula, it isn't always easy to ensure that
600 the right headers are invoked in the right order (there are a few kludges to
601 make this happen -- i.e. in a few include files because of the chicken and egg
602 problem, certain references to typedefs had to be replaced with {\bf void} ). 
603
604 Every file should include {\bf bacula.h}. It pulls in just about everything,
605 with very few exceptions. If you have system dependent ifdefing, please do it
606 in {\bf baconfig.h}. The version number and date are kept in {\bf version.h}. 
607
608 Each of the subdirectories (console, cats, dird, filed, findlib, lib, stored,
609 ...) contains a single directory dependent include file generally the name of
610 the directory, which should be included just after the include of {\bf
611 bacula.h}. This file (for example, for the dird directory, it is {\bf dird.h})
612 contains either definitions of things generally needed in this directory, or
613 it includes the appropriate header files. It always includes {\bf protos.h}.
614 See below. 
615
616 Each subdirectory contains a header file named {\bf protos.h}, which contains
617 the prototypes for subroutines exported by files in that directory. {\bf
618 protos.h} is always included by the main directory dependent include file. 
619
620 \subsection{Programming Standards}
621 \index{Standards!Programming}
622 \index{Programming Standards}
623 \addcontentsline{toc}{subsubsection}{Programming Standards}
624
625 For the most part, all code should be written in C unless there is a burning
626 reason to use C++, and then only the simplest C++ constructs will be used.
627 Note, Bacula is slowly evolving to use more and more C++. 
628
629 Code should have some documentation -- not a lot, but enough so that I can
630 understand it. Look at the current code, and you will see that I document more
631 than most, but am definitely not a fanatic. 
632
633 We prefer simple linear code where possible. Gotos are strongly discouraged
634 except for handling an error to either bail out or to retry some code, and
635 such use of gotos can vastly simplify the program. 
636
637 Remember this is a C program that is migrating to a {\bf tiny} subset of C++,
638 so be conservative in your use of C++ features. 
639
640 \subsection{Do Not Use}
641 \index{Use!Do Not}
642 \index{Do Not Use}
643 \addcontentsline{toc}{subsubsection}{Do Not Use}
644
645 \begin{itemize}
646  \item STL -- it is totally incomprehensible. 
647 \end{itemize}
648
649 \subsection{Avoid if Possible}
650 \index{Possible!Avoid if}
651 \index{Avoid if Possible}
652 \addcontentsline{toc}{subsubsection}{Avoid if Possible}
653
654 \begin{itemize}
655 \item Using {\bf void *} because this generally means that one must
656    using casting, and in C++ casting is rather ugly.  It is OK to use
657    void * to pass structure address where the structure is not known  
658    to the routines accepting the packet (typically callback routines).
659    However, declaring "void *buf" is a bad idea.  Please use the
660    correct types whenever possible.
661
662 \item Using undefined storage specifications such as (short, int, long,
663    long long, size\_t ...).  The problem with all these is that the number of bytes
664    they allocate depends on the compiler and the system.  Instead use
665    Bacula's types (int8\_t, uint8\_t, int32\_t, uint32\_t, int64\_t, and 
666    uint64\_t).  This guarantees that the variables are given exactly the
667    size you want. Please try at all possible to avoid using size\_t ssize\_t
668    and the such. They are very system dependent.  However, some system
669    routines may need them, so their use is often unavoidable.
670
671 \item Returning a malloc'ed buffer from a subroutine --  someone will forget
672    to release it. 
673
674 \item Heap allocation (malloc) unless needed -- it is expensive. Use
675       POOL\_MEM instead.
676
677 \item Templates -- they can create portability problems. 
678
679 \item Fancy or tricky C or C++ code, unless you give a  good explanation of
680    why you used it. 
681
682 \item Too much inheritance -- it can complicate the code,  and make reading it
683    difficult (unless you are in love  with colons) 
684
685 \end{itemize}
686
687 \subsection{Do Use Whenever Possible}
688 \index{Possible!Do Use Whenever}
689 \index{Do Use Whenever Possible}
690 \addcontentsline{toc}{subsubsection}{Do Use Whenever Possible}
691
692 \begin{itemize}
693 \item Locking and unlocking within a single subroutine.  
694
695 \item A single point of exit from all subroutines. A goto is 
696      perfectly OK to use to get out early, but only to a label
697      named bail\_out, and possibly an ok\_out.  See current code
698      examples.
699
700 \item Malloc and free within a single subroutine.  
701
702 \item Comments and global explanations on what your code or  algorithm does. 
703
704 \end{itemize}
705
706 \subsection{Indenting Standards}
707 \index{Standards!Indenting}
708 \index{Indenting Standards}
709 \addcontentsline{toc}{subsubsection}{Indenting Standards}
710
711 We find it very hard to read code indented 8 columns at a time. 
712 Even 4 at a time uses a lot of space, so we have adopted indenting
713 3 spaces at every level. Note, indention is the visual appearance of the
714 source on the page, while tabbing is replacing a series of up to 8 spaces from
715 a tab character. 
716
717 The closest set of parameters for the Linux {\bf indent} program that will
718 produce reasonably indented code are: 
719
720 \footnotesize
721 \begin{verbatim}
722 -nbad -bap -bbo -nbc -br -brs -c36 -cd36 -ncdb -ce -ci3 -cli0
723 -cp36 -d0 -di1 -ndj -nfc1 -nfca -hnl -i3 -ip0 -l85 -lp -npcs
724 -nprs -npsl -saf -sai -saw -nsob -nss -nbc -ncs -nbfda
725 \end{verbatim}
726 \normalsize
727
728 You can put the above in your .indent.pro file, and then just invoke indent on
729 your file. However, be warned. This does not produce perfect indenting, and it
730 will mess up C++ class statements pretty badly. 
731
732 Braces are required in all if statements (missing in some very old code). To
733 avoid generating too many lines, the first brace appears on the first line
734 (e.g. of an if), and the closing brace is on a line by itself. E.g. 
735
736 \footnotesize
737 \begin{verbatim}
738    if (abc) {
739       some_code;
740   }
741 \end{verbatim}
742 \normalsize
743
744 Just follow the convention in the code. For example we I prefer non-indented cases. 
745
746 \footnotesize
747 \begin{verbatim}
748    switch (code) {
749    case 'A':
750       do something
751       break;
752    case 'B':
753       again();
754       break;
755    default:
756       break;
757   }
758 \end{verbatim}
759 \normalsize
760
761 Avoid using // style comments except for temporary code or turning off debug
762 code. Standard C comments are preferred (this also keeps the code closer to
763 C). 
764
765 Attempt to keep all lines less than 85 characters long so that the whole line
766 of code is readable at one time. This is not a rigid requirement. 
767
768 Always put a brief description at the top of any new file created describing
769 what it does and including your name and the date it was first written. Please
770 don't forget any Copyrights and acknowledgments if it isn't 100\% your code.
771 Also, include the Bacula copyright notice that is in {\bf src/c}. 
772
773 In general you should have two includes at the top of the an include for the
774 particular directory the code is in, for includes are needed, but this should
775 be rare. 
776
777 In general (except for self-contained packages), prototypes should all be put
778 in {\bf protos.h} in each directory. 
779
780 Always put space around assignment and comparison operators. 
781
782 \footnotesize
783 \begin{verbatim}
784    a = 1;
785    if (b >= 2) {
786      cleanup();
787   }
788 \end{verbatim}
789 \normalsize
790
791 but your can compress things in a {\bf for} statement: 
792
793 \footnotesize
794 \begin{verbatim}
795    for (i=0; i < del.num_ids; i++) {
796     ...
797 \end{verbatim}
798 \normalsize
799
800 Don't overuse the inline if (?:). A full {\bf if} is preferred, except in a
801 print statement, e.g.: 
802
803 \footnotesize
804 \begin{verbatim}
805    if (ua->verbose \&& del.num_del != 0) {
806       bsendmsg(ua, _("Pruned %d %s on Volume %s from catalog.\n"), del.num_del,
807          del.num_del == 1 ? "Job" : "Jobs", mr->VolumeName);
808   }
809 \end{verbatim}
810 \normalsize
811
812 Leave a certain amount of debug code (Dmsg) in code you submit, so that future
813 problems can be identified. This is particularly true for complicated code
814 likely to break. However, try to keep the debug code to a minimum to avoid
815 bloating the program and above all to keep the code readable. 
816
817 Please keep the same style in all new code you develop. If you include code
818 previously written, you have the option of leaving it with the old indenting
819 or re-indenting it. If the old code is indented with 8 spaces, then please
820 re-indent it to Bacula standards. 
821
822 If you are using {\bf vim}, simply set your tabstop to 8 and your shiftwidth
823 to 3. 
824
825 \subsection{Tabbing}
826 \index{Tabbing}
827 \addcontentsline{toc}{subsubsection}{Tabbing}
828
829 Tabbing (inserting the tab character in place of spaces) is as normal on all
830 Unix systems -- a tab is converted space up to the next column multiple of 8.
831 My editor converts strings of spaces to tabs automatically -- this results in
832 significant compression of the files. Thus, you can remove tabs by replacing
833 them with spaces if you wish. Please don't confuse tabbing (use of tab
834 characters) with indenting (visual alignment of the code). 
835
836 \subsection{Don'ts}
837 \index{Don'ts}
838 \addcontentsline{toc}{subsubsection}{Don'ts}
839
840 Please don't use: 
841
842 \footnotesize
843 \begin{verbatim}
844 strcpy()
845 strcat()
846 strncpy()
847 strncat();
848 sprintf()
849 snprintf()
850 \end{verbatim}
851 \normalsize
852
853 They are system dependent and un-safe. These should be replaced by the Bacula
854 safe equivalents: 
855
856 \footnotesize
857 \begin{verbatim}
858 char *bstrncpy(char *dest, char *source, int dest_size);
859 char *bstrncat(char *dest, char *source, int dest_size);
860 int bsnprintf(char *buf, int32_t buf_len, const char *fmt, ...);
861 int bvsnprintf(char *str, int32_t size, const char  *format, va_list ap);
862 \end{verbatim}
863 \normalsize
864
865 See src/lib/bsys.c for more details on these routines. 
866
867 Don't use the {\bf \%lld} or the {\bf \%q} printf format editing types to edit
868 64 bit integers -- they are not portable. Instead, use {\bf \%s} with {\bf
869 edit\_uint64()}. For example: 
870
871 \footnotesize
872 \begin{verbatim}
873    char buf[100];
874    uint64_t num = something;
875    char ed1[50];
876    bsnprintf(buf, sizeof(buf), "Num=%s\n", edit_uint64(num, ed1));
877 \end{verbatim}
878 \normalsize
879
880 Note: {\bf \%lld} is now permitted in Bacula code -- we have our
881 own printf routines which handle it correctly. The edit\_uint64() subroutine
882 can still be used if you wish, but over time, most of that old style will
883 be removed.
884
885 The edit buffer {\bf ed1} must be at least 27 bytes long to avoid overflow.
886 See src/lib/edit.c for more details. If you look at the code, don't start
887 screaming that I use {\bf lld}. I actually use subtle trick taught to me by
888 John Walker. The {\bf lld} that appears in the editing routine is actually
889 {\bf \#define} to a what is needed on your OS (usually ``lld'' or ``q'') and
890 is defined in autoconf/configure.in for each OS. C string concatenation causes
891 the appropriate string to be concatenated to the ``\%''. 
892
893 Also please don't use the STL or Templates or any complicated C++ code. 
894
895 \subsection{Message Classes}
896 \index{Classes!Message}
897 \index{Message Classes}
898 \addcontentsline{toc}{subsubsection}{Message Classes}
899
900 Currently, there are five classes of messages: Debug, Error, Job, Memory, 
901 and Queued.
902
903 \subsection{Debug Messages}
904 \index{Messages!Debug}
905 \index{Debug Messages}
906 \addcontentsline{toc}{subsubsection}{Debug Messages}
907
908 Debug messages are designed to be turned on at a specified debug level and are
909 always sent to STDOUT. There are designed to only be used in the development
910 debug process. They are coded as: 
911
912 DmsgN(level, message, arg1, ...) where the N is a number indicating how many
913 arguments are to be substituted into the message (i.e. it is a count of the
914 number arguments you have in your message -- generally the number of percent
915 signs (\%)). {\bf level} is the debug level at which you wish the message to
916 be printed. message is the debug message to be printed, and arg1, ... are the
917 arguments to be substituted. Since not all compilers support \#defines with
918 varargs, you must explicitly specify how many arguments you have. 
919
920 When the debug message is printed, it will automatically be prefixed by the
921 name of the daemon which is running, the filename where the Dmsg is, and the
922 line number within the file. 
923
924 Some actual examples are: 
925
926 Dmsg2(20, ``MD5len=\%d MD5=\%s\textbackslash{}n'', strlen(buf), buf); 
927
928 Dmsg1(9, ``Created client \%s record\textbackslash{}n'', client->hdr.name); 
929
930 \subsection{Error Messages}
931 \index{Messages!Error}
932 \index{Error Messages}
933 \addcontentsline{toc}{subsubsection}{Error Messages}
934
935 Error messages are messages that are related to the daemon as a whole rather
936 than a particular job. For example, an out of memory condition my generate an
937 error message. They should be very rarely needed. In general, you should be
938 using Job and Job Queued messages (Jmsg and Qmsg). They are coded as: 
939
940 EmsgN(error-code, level, message, arg1, ...) As with debug messages, you must
941 explicitly code the of arguments to be substituted in the message. error-code
942 indicates the severity or class of error, and it may be one of the following: 
943
944 \addcontentsline{lot}{table}{Message Error Code Classes}
945 \begin{longtable}{lp{3in}}
946 {{\bf M\_ABORT} } & {Causes the daemon to immediately abort. This should be
947 used only  in extreme cases. It attempts to produce a  traceback. } \\
948 {{\bf M\_ERROR\_TERM} } & {Causes the daemon to immediately terminate. This
949 should be used only  in extreme cases. It does not produce a  traceback. } \\
950 {{\bf M\_FATAL} } & {Causes the daemon to terminate the current job, but the
951 daemon keeps running } \\
952 {{\bf M\_ERROR} } & {Reports the error. The daemon and the job continue
953 running } \\
954 {{\bf M\_WARNING} } & {Reports an warning message. The daemon and the job
955 continue running } \\
956 {{\bf M\_INFO} } & {Reports an informational message.}
957
958 \end{longtable}
959
960 There are other error message classes, but they are in a state of being
961 redesigned or deprecated, so please do not use them. Some actual examples are:
962
963
964 Emsg1(M\_ABORT, 0, ``Cannot create message thread: \%s\textbackslash{}n'',
965 strerror(status)); 
966
967 Emsg3(M\_WARNING, 0, ``Connect to File daemon \%s at \%s:\%d failed. Retrying
968 ...\textbackslash{}n'',  client-\gt{}hdr.name, client-\gt{}address,
969 client-\gt{}port); 
970
971 Emsg3(M\_FATAL, 0, ``bdird\lt{}filed: bad response from Filed to \%s command:
972 \%d \%s\textbackslash{}n'',  cmd, n, strerror(errno)); 
973
974 \subsection{Job Messages}
975 \index{Job Messages}
976 \index{Messages!Job}
977 \addcontentsline{toc}{subsubsection}{Job Messages}
978
979 Job messages are messages that pertain to a particular job such as a file that
980 could not be saved, or the number of files and bytes that were saved. They
981 Are coded as:
982 \begin{verbatim}
983 Jmsg(jcr, M\_FATAL, 0, "Text of message");
984 \end{verbatim}
985 A Jmsg with M\_FATAL will fail the job. The Jmsg() takes varargs so can
986 have any number of arguments for substituted in a printf like format.
987 Output from the Jmsg() will go to the Job report.
988 <br>
989 If the Jmsg is followed with a number such as Jmsg1(...), the number
990 indicates the number of arguments to be substituted (varargs is not
991 standard for \#defines), and what is more important is that the file and
992 line number will be prefixed to the message. This permits a sort of debug
993 from user's output.
994
995 \subsection{Queued Job Messages}
996 \index{Queued Job Messages}
997 \index{Messages!Job}
998 \addcontentsline{toc}{subsubsection}{Queued Job Messages}
999 Queued Job messages are similar to Jmsg()s except that the message is
1000 Queued rather than immediately dispatched. This is necessary within the
1001 network subroutines and in the message editing routines. This is to prevent
1002 recursive loops, and to ensure that messages can be delivered even in the
1003 event of a network error.
1004
1005
1006 \subsection{Memory Messages}
1007 \index{Messages!Memory}
1008 \index{Memory Messages}
1009 \addcontentsline{toc}{subsubsection}{Memory Messages}
1010
1011 Memory messages are messages that are edited into a memory buffer. Generally
1012 they are used in low level routines such as the low level device file dev.c in
1013 the Storage daemon or in the low level Catalog routines. These routines do not
1014 generally have access to the Job Control Record and so they return error
1015 essages reformatted in a memory buffer. Mmsg() is the way to do this. 
1016
1017 \subsection{Bugs Database}
1018 \index{Database!Bugs}
1019 \index{Bugs Database}
1020 \addcontentsline{toc}{subsubsection}{Bugs Database}
1021 We have a bugs database which is at:
1022 \elink{http://bugs.bacula.org}{http://bugs.bacula.org}, and as
1023 a developer you will need to respond to bugs, perhaps bugs in general 
1024 if you have time, otherwise just bugs that correspond to code that
1025 you wrote.
1026
1027 If you need to answer bugs, please be sure to ask the Project Manager
1028 (currently Kern) to give you Developer access to the bugs database. This
1029 allows you to modify statuses and close bugs.
1030
1031 The first thing is if you want to take over a bug, rather than just make a
1032 note, you should assign the bug to yourself. This helps other developers
1033 know that you are the principal person to deal with the bug.  You can do so
1034 by going into the bug and clicking on the {\bf Update Issue} button. Then
1035 you simply go to the {\bf Assigned To} box and select your name from the
1036 drop down box.  To actually update it you must click on the {\bf Update
1037 Information} button a bit further down on the screen, but if you have other
1038 things to do such as add a Note, you might wait before clicking on the {\bf
1039 Update Information} button.
1040
1041 Generally, we set the {\bf Status} field to either acknowledged, confirmed,
1042 or feedback when we first start working on the bug.  Feedback is set when
1043 we expect that the user should give us more information.
1044
1045 Normally, once you are reasonably sure that the bug is fixed, and a patch
1046 is made and attached to the bug report, and/or in the SVN, you can close
1047 the bug.  If you want the user to test the patch, then leave the bug open,
1048 otherwise close it and set {\bf Resolution} to {\bf Fixed}.  We generally
1049 close bug reports rather quickly, even without confirmation, especially if
1050 we have run tests and can see that for us the problem is fixed.  However,
1051 in doing so, it avoids misunderstandings if you leave a note while you are
1052 closing the bug that says something to the following effect:
1053 We are closing this bug because ...   If for some reason, it does not fix
1054 your problem, please feel free to reopen it, or to open a new bug report
1055 describing the problem".
1056
1057 We do not recommend that you attempt to edit any of the bug notes that have
1058 been submitted, nor to delete them or make them private.  In fact, if
1059 someone accidentally makes a bug note private, you should ask the reason
1060 and if at all possible (with his agreement) make the bug note public.  
1061
1062 If the user has not properly filled in most of the important fields
1063 (platorm, OS, Product Version, ...) please do not hesitate to politely ask
1064 him.  Also, if the bug report is a request for a new feature, please
1065 politely send the user to the Feature Request menu item on www.bacula.org.
1066 The same applies to a support request (we answer only bugs), you might give
1067 the user a tip, but please politely refer him to the manual and the
1068 Getting Support page of www.bacula.org.