4 \section*{Bacula Developer Notes}
5 \label{_ChapterStart10}
6 \index{Bacula Developer Notes}
7 \index{Notes!Bacula Developer}
8 \addcontentsline{toc}{section}{Bacula Developer Notes}
12 \addcontentsline{toc}{subsection}{General}
14 This document is intended mostly for developers and describes the the general
15 framework of making Bacula source changes.
17 \subsubsection*{Contributions}
19 \addcontentsline{toc}{subsubsection}{Contributions}
21 Contributions from programmers are broken into two groups. The first are
22 contributions that are aids and not essential to Bacula. In general, these
23 will be scripts or will go into and examples or contributions directory.
24 For these kinds of non-essential contributions there is no obligation to do
25 a copyright assignment as described below. However, a copyright assignment
26 would still be appreciated.
28 The second class of contributions are those which will be integrated with
29 Bacula and become an essential part. Within this class of contributions, there
30 are two hurdles to surmount. One is getting your patch accepted, and two is
31 dealing with copyright issues. The following text describes some of the
32 requirements for such code.
34 \subsubsection*{Patches}
36 \addcontentsline{toc}{subsubsection}{Patches}
38 Subject to the copyright assignment described below, your patches should be
39 sent in {\bf diff -u} format relative to the current contents of the Source
40 Forge CVS, which is the easiest to understand and integrate.
41 Please be sure to use the Bacula indenting standard (see below).
42 If you have checked out the source with CVS, you can get a diff using:
45 cvs diff -u > change.patch
48 If you plan on doing significant development work over a period of time,
49 after having your first patch reviewed and approved, you will be eligible
50 for having developer CVS access so that you can commit your changes
51 directly to the CVS repository. To do so, you will need a userid on Source
54 \subsubsection*{Copyrights}
56 \addcontentsline{toc}{subsubsection}{Copyrights}
58 To avoid future problems concerning changing licensing or copyrights, all
59 code contributions more than a hand full of lines must be in the Public
60 Domain or have the copyright transferred to the Free Software Foundation
61 Europe e.V. with a Fiduciary License Agreement (FLA).
63 code. Note, prior to November 2004, the code was copyrighted by Kern
64 Sibbald and John Walker. After November 2004, the code was copyrighted
65 by Kern Sibbald, then on the 15th of November 2006, the copyright was
66 transferred to the Free Software Foundation Europe e.V.
68 Your name should be clearly indicated as the author of the code, and you
69 must be extremely careful not to violate any copyrights or use other
70 people's code without acknowledging it. The purpose of this requirement is
71 to avoid future copyright, patent, or intellectual property problems. To
72 understand the possible source of future problems, please examine the
73 difficulties Mozilla is (was?) having finding previous contributors at
74 \elink{ http://www.mozilla.org/MPL/missing.html}
75 {http://www.mozilla.org/MPL/missing.html}. The other important issue is to
76 avoid copyright, patent, or intellectual property violations as are currently
77 (May 2003) being claimed by SCO against IBM.
79 Although the copyright will be held by the Free Software Foundation Europe
80 e.V., each developer is expected to
81 indicate that he wrote and/or modified a particular module (or file) and
82 any other sources. The copyright assignment may seem a bit unusual, but in
83 reality, it is not. Most large projects require this.
85 If you have any doubts about this, please don't hesitate to ask. The
86 objective is to assure the long term servival of the Bacula project.
88 Items not needing a copyright assignment are: most small changes,
89 enhancements, or bug fixes of 5-10 lines of code, which amount to
90 less than 20% of any particular file.
92 \subsubsection*{Copyright Assignment -- Fiduciary License Agreement}
93 \index{Copyright Assignment}
94 \index{Assignment!Copyright}
95 \addcontentsline{toc}{subsubsection}{Copyright Assignment -- Fiduciary License Agreement}
97 Since this is not a commercial enterprise, and we prefer to believe in
98 everyone's good faith, previously developers could assign the copyright by
99 explicitly acknowledging that they do so in their first submission. This
100 was sufficient if the developer is independent, or an employee of a
101 not-for-profit organization or a university. However, in an effort to
102 ensure that the Bacula code is really clean, beginning in August 2006, all
103 previous and future developers with CVS access will be asked to submit a
104 copyright assignment (or Fiduciary License Agreement -- FLA).
106 Any developer who wants to contribute and is employed by a company should
107 get a copyright assignment from his employer. This is because in many
108 counties, all work that an employee does whether on company time or in the
109 employee's free time is considered to be Intellectual Property of the
110 company. Obtaining a signed FLA from the company will avoid
111 misunderstandings between the employee, the company, and the Bacula
112 project. A good number of companies have already followed this procedure.
114 The Fiduciary License Agreement is posted on the Bacula web site at:
115 \elink{http://www.bacula.org/FLA-bacula.en.pdf}{http://www.bacula.org/FLA-bacula.en.pdf}
117 It should be filled out, then sent to:
120 Free Software Foundation Europe
127 Please note that the above address is different from the officially
128 registered office mentioned in the document. When you send in such a
129 complete document, please notify me: kern at sibbald dot com.
132 \subsection*{Basic CVS Usage}
133 \index{Basic CVS Usage}
135 \addcontentsline{toc}{subsection}{Basic CVS Usage}
137 The Bacula CVS is kept at Source Forge. If you are not a developer,
138 you only be able to access the public CVS, which runs about 6 hours behind
139 the developer's CVS. If you are a developer, then you will have immediate
140 access to "the" CVS and any changes ("commit") that you make will be
141 immediately seen by other developers.
143 For developer access to the CVS, go to the Bacula page on Source Forge
144 and click on the CVS menu item. Then follow the instructions for
145 installing your SSH public key. It may take a few hours until the
146 key is actually installed, and then you will have access to the CVS.
148 The Bacula CVS is divided into the following CVS "projects" or "modules".
152 bacula (Bacula source code)
153 docs (Bacula documentation)
154 gui (Some of the GUI programs that do not use
155 the Bacula core code)
156 rescue (Bacula CDROM rescue code)
157 regress (Bacula regression scripts)
158 ryol (Roll your own Linux -- incomplete project)
162 Most of you will want either the Bacula source (bacula) and/or the
165 To get the source for a project, you must check it out ("checkout"), which
168 \subsubsection*{Public CVS Access}
169 \index{Public CVS Access}
170 \addcontentsline{toc}{subsubsection}{Public CVS Access}
172 The first time you checkout the code for each project, you will need to
173 tell the cvs program where the CVS repository is. The procedure for
174 checking out code from the public CVS is:
177 cvs -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/bacula login
179 Then when it prompts for the password for {\bf anonymous}, simply
180 press the Enter key. The above command is necessary only once
181 the very first time you login. Then enter the following command:
184 cvs -z3 -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/bacula co -P bacula
187 The above will place the contents of the bacula module in a directory
188 named {\bf bacula} in the current directory. This data will come
189 from the public CVS, which typically runs 6 hours to a day behind the
190 developer's CVS. Once you have created a copy of the CVS, you can use
191 the commands listed below under the title CVS Usage.
193 \subsubsection*{Developer CVS Access}
194 \index{Developer CVS Access}
195 \addcontentsline{toc}{subsubsection}{Developer CVS Access}
197 If you are registered as a Bacula developer (contact Kern about this),
198 you may access the developer's CVS using:
202 export CVSROOT=:ext:<nnnn>@cvs.bacula.sourceforge.net:/cvsroot/bacula
205 where you replace \lt{}nnnn\gt{} by your Source Forge user name.
210 cvs -z3 checkout -d <directory> bacula
213 where you replace \lt{}directory\gt{} by the name of the directory you want
214 to contain the Bacula source code. If you want the docs, replace the
215 word "bacula" on the above line by "docs" and be sure to put it in
216 a different directory. The -z3 just tells CVS to use compression during
217 the transmission, which makes things go faster. There is no need to
218 do the anonymous login as is the case for non-developers.
220 The above command should generate output that looks a bit like the
224 cvs checkout: Updating bacula
232 \subsubsection*{CVS Usage}
234 \addcontentsline{toc}{subsubsection}{CVS Usage}
236 The commands that follow with the exception of the {\bf commit}
237 work the same whether you are accessing the public CVS or
240 Let's assume you used the name "bacula" for the directory, so your
244 cvs -z3 checkout -d bacula bacula
247 When the command is done, simply do:
254 and then build Bacula. You will notice a lot of extra CVS directories
255 in the source code. Don't ever change or delete them, or you will mess up
256 your copy of the project. Also, do not rename or delete any of the files
257 in your copy of the repository or you may have problems. Any files that
258 you change will remain only on your system until you do a "commit", which
261 Let's say you are working in the directory scripts. You would then do:
268 when you are happy with your changes, you can do the following:
271 cd bacula (to your top level directory)
272 cvs diff -u >my-changes.patch
275 When the command is done, you can look in the file my-changes.patch
276 and you will see all the changes you have made to your copy of the
277 repository. Make sure that you understand all the changes that
278 it reports before proceeding. If you modified files that you do
279 do not want to commit to the main repository, you can simply delete
280 them from your local directory, and they will be restored from the
281 repository with the "cvs update" that is shown below. Normally, you
282 should not find changes to files that you do not want to commit, and
283 if you find yourself in that position a lot, you are probably doing
286 Let's assume that now you want to commit your changes to the main CVS
296 When you do this, it will pull any changes made by other developers into
297 your local copy of the repository, and it will check for conflicts. If there
298 are any, it will tell you, and you will need to resolve them. The problems
299 of resolving conflicts are a bit more than this document can cover, but
300 you can examine the files it claims have conflicts and look for \lt{}\lt{}\lt{}\lt{}
301 or look in the .rej files that it creates. If you have problems, just ask
302 on the developer's list.
304 Note, doing the above "cvs update" is not absolutely necessary. There are
305 times when you may be working on code and you want to commit it, but you
306 explicitly do not want to move up to the latest version of the code in
307 the CVS. If that is the case, you can simply skip the "cvs update" and
308 do the commit shown below. If the commit fails because of a conflict, it
309 will tell you, and you must resolve the conflict before it will permit
310 you to do the commit.
312 Once your local copy of the repository has been updated, you can now
316 cvs commit -m "Some comment about what you changed"
319 or if you really only want to commit a single file, you can
323 cvs commit -m "comment" scripts/file-I-edited
326 Note, if you have done a build in your directory, or you have added
327 other new files, the commit will update only the files that are
328 actually in the repository. For example, none of the object files
329 are stored in the repository, so when you do a commit, those object
330 files will simply be ignored.
332 If you want to add new files or remove files from the main CVS
333 repository, and you are not experienced with CVS, please ask Kern
334 to do it. If you follow the simple steps above, it is unlikely that
335 you will do any damage to the repository, and if you do, it is always
336 possible for us to recover, but it can be painful.
338 If you are only working in one subdirectory of say the bacula project,
339 for example, the scripts directory, you can do your commit from
340 that subdirectory, and only the changes in that directory and all its
341 subdirectories will be committed. This can be helpful for translators.
342 If you are doing a French translation, you will be working in
343 docs/manual-fr, and if you are always cd'ed into that directory when
344 doing your commits, your commit will effect only that directory. As
345 long as you are careful only to change files that you want changed,
346 you have little to worry about.
348 \subsection*{The Development Cycle}
349 \index{Developement Cycle}
350 \index{Cycle!Developement}
351 \addcontentsline{toc}{subsubsection}{Development Cycle}
353 As I noted in the 1.38 ReleaseNotes, version 1.38 was different from prior
354 versions because it had a lot more contributions. I expect that this trend
355 will continue. As a consequence, I am going to modify how I normally do
356 development, and instead of making a list of all the features that I will
357 implement in the next version, I will personally sign up for one (maybe
358 two) projects at a time, and when they are complete, I will release a new
361 The difference is that I will have more time to review the new code that is
362 being contributed, and will be able to devote more time to a smaller number
363 of projects (1.38 had too many new features for me to handle correctly).
365 I expect that future release schedules will be much the same, and the
366 number of new features will also be much the same providing that the
367 contributions continue to come -- and they show no signs of let up :-)
369 \index{Feature Requests}
370 {\bf Feature Requests:} \\
371 In addition, I would like to "formalize" the feature requests a bit.
373 Instead of me maintaining an informal list of everything I run into
374 (kernstodo), I would like to maintain a "formal" list of projects. This
375 means that all new feature requests, including those recently discussed on
376 the email lists, must be formally submitted and approved.
378 Formal submission of feature requests will take two forms: \\
379 1. non-mandatory, but highly recommended is to discuss proposed new features
380 on the mailing list.\\
381 2. Formal submission of an Feature Request in a special format.
382 I'll give an example of this below, but you can also find it on the web
383 site under "Support -\gt{} Feature Requests". Since it takes a bit of time to
384 properly fill out a Feature Request form, you probably should check on the email list
387 Once I receive the Feature Request, I will either accept it, send it back
388 asking for clarification, send it to the email list asking for opinions, or
391 If it is accepted, it will go in the "projects" file (a simple ASCII file)
392 maintained in the main Bacula source directory.
394 {\bf Implementation of Feature Requests:}\\
395 Any qualified developer can sign up for a project. The project must have
396 an entry in the projects file, and the developer's name will appear in the
399 {\bf How Feature Requests are accepted:}\\
400 Acceptance of Feature Requests depends on several things: \\
401 1. feedback from users. If it is negative, the Feature Request will probably not be
403 2. the difficulty of the project. A project that is so
404 difficult that I cannot imagine finding someone to implement probably won't
406 3. whether or not the Feature Request fits within the
407 current stategy of Bacula (for example an Feature Request that requests changing the
408 tape to tar format would not be accepted, ...)
410 {\bf How Feature Requests are prioritized:}\\
411 Once an Feature Request is accepted, it needs to be implemented. If you
412 can find a developer for it, or one signs up for implementing it, then the
413 Feature Request becomes top priority (at least for that developer).
415 Between releases of Bacula, I will generally solicit Feature Request input
416 for the next version, and by way of this email, I suggest that you send
417 discuss and send in your Feature Requests for the next release. Please
418 verify that the Feature Request is not in the current list (attached to this email).
420 Once users have had several weeks to submit Feature Requests, I will
421 organize them, and request users to vote on them. This will allow fixing
422 prioritizing the Feature Requests. Having a priority is one thing, but
423 getting it implement is another thing -- I am hoping that the Bacula
424 community will take more responsibility for assuring the implementation of
425 accepted Feature Requests.
427 Feature Request format:
429 ============= Empty Feature Request form ===========
430 Item n: One line summary ...
432 Origin: Name and email of originator.
435 What: More detailed explanation ...
437 Why: Why it is important ...
439 Notes: Additional notes or features (omit if not used)
440 ============== End Feature Request form ==============
444 ============= Example Completed Feature Request form ===========
445 Item 1: Implement a Migration job type that will move the job
446 data from one device to another.
447 Origin: Sponsored by Riege Sofware International GmbH. Contact:
448 Daniel Holtkamp <holtkamp at riege dot com>
449 Date: 28 October 2005
450 Status: Partially coded in 1.37 -- much more to do. Assigned to
453 What: The ability to copy, move, or archive data that is on a
454 device to another device is very important.
456 Why: An ISP might want to backup to disk, but after 30 days
457 migrate the data to tape backup and delete it from
458 disk. Bacula should be able to handle this
459 automatically. It needs to know what was put where,
460 and when, and what to migrate -- it is a bit like
461 retention periods. Doing so would allow space to be
462 freed up for current backups while maintaining older
465 Notes: Migration could be triggered by:
469 Highwater size (keep total size)
471 =================================================
475 \subsection*{Developing Bacula}
476 \index{Developing Bacula}
477 \index{Bacula!Developing}
478 \addcontentsline{toc}{subsubsection}{Developing Bacula}
480 Typically the simplest way to develop Bacula is to open one xterm window
481 pointing to the source directory you wish to update; a second xterm window at
482 the top source directory level, and a third xterm window at the bacula
483 directory \lt{}top\gt{}/src/bacula. After making source changes in one of the
484 directories, in the top source directory xterm, build the source, and start
485 the daemons by entering:
489 ./startit then in the enter:
493 ./gnome-console to start the Console program. Enter any commands for testing.
494 For example: run kernsverify full.
496 Note, the instructions here to use {\bf ./startit} are different from using a
497 production system where the administrator starts Bacula by entering {\bf
498 ./bacula start}. This difference allows a development version of {\bf Bacula}
499 to be run on a computer at the same time that a production system is running.
500 The {\bf ./startit} strip starts {\bf Bacula} using a different set of
501 configuration files, and thus permits avoiding conflicts with any production
504 To make additional source changes, exit from the Console program, and in the
505 top source directory, stop the daemons by entering:
507 ./stopit then repeat the process.
509 \subsubsection*{Debugging}
511 \addcontentsline{toc}{subsubsection}{Debugging}
513 Probably the first thing to do is to turn on debug output.
515 A good place to start is with a debug level of 20 as in {\bf ./startit -d20}.
516 The startit command starts all the daemons with the same debug level.
517 Alternatively, you can start the appropriate daemon with the debug level you
518 want. If you really need more info, a debug level of 60 is not bad, and for
519 just about everything a level of 200.
521 \subsubsection*{Using a Debugger}
522 \index{Using a Debugger}
523 \index{Debugger!Using a}
524 \addcontentsline{toc}{subsubsection}{Using a Debugger}
526 If you have a serious problem such as a segmentation fault, it can usually be
527 found quickly using a good multiple thread debugger such as {\bf gdb}. For
528 example, suppose you get a segmentation violation in {\bf bacula-dir}. You
529 might use the following to find the problem:
531 \lt{}start the Storage and File daemons\gt{}
534 run -f -s -c ./dird.conf
535 \lt{}it dies with a segmentation fault\gt{}
537 The {\bf -f} option is specified on the {\bf run} command to inhibit {\bf
538 dird} from going into the background. You may also want to add the {\bf -s}
539 option to the run command to disable signals which can potentially interfere
542 As an alternative to using the debugger, each {\bf Bacula} daemon has a built
543 in back trace feature when a serious error is encountered. It calls the
544 debugger on itself, produces a back trace, and emails the report to the
545 developer. For more details on this, please see the chapter in the main Bacula
546 manual entitled ``What To Do When Bacula Crashes (Kaboom)''.
548 \subsubsection*{Memory Leaks}
551 \addcontentsline{toc}{subsubsection}{Memory Leaks}
553 Because Bacula runs routinely and unattended on client and server machines, it
554 may run for a long time. As a consequence, from the very beginning, Bacula
555 uses SmartAlloc to ensure that there are no memory leaks. To make detection of
556 memory leaks effective, all Bacula code that dynamically allocates memory MUST
557 have a way to release it. In general when the memory is no longer needed, it
558 should be immediately released, but in some cases, the memory will be held
559 during the entire time that Bacula is executing. In that case, there MUST be a
560 routine that can be called at termination time that releases the memory. In
561 this way, we will be able to detect memory leaks. Be sure to immediately
562 correct any and all memory leaks that are printed at the termination of the
565 \subsubsection*{Special Files}
566 \index{Files!Special}
567 \index{Special Files}
568 \addcontentsline{toc}{subsubsection}{Special Files}
570 Kern uses files named 1, 2, ... 9 with any extension as scratch files. Thus
571 any files with these names are subject to being rudely deleted at any time.
573 \subsubsection*{When Implementing Incomplete Code}
574 \index{Code!When Implementing Incomplete}
575 \index{When Implementing Incomplete Code}
576 \addcontentsline{toc}{subsubsection}{When Implementing Incomplete Code}
578 Please identify all incomplete code with a comment that contains
584 where there are three asterisks (*) before and after the word
585 FIXME (in capitals) and no intervening spaces. This is important as it allows
586 new programmers to easily recognize where things are partially implemented.
588 \subsubsection*{Bacula Source File Structure}
589 \index{Structure!Bacula Source File}
590 \index{Bacula Source File Structure}
591 \addcontentsline{toc}{subsubsection}{Bacula Source File Structure}
593 The distribution generally comes as a tar file of the form {\bf
594 bacula.x.y.z.tar.gz} where x, y, and z are the version, release, and update
595 numbers respectively.
597 Once you detar this file, you will have a directory structure as follows:
604 |- mtx (autochanger control program + tape drive info)
605 |- sqlite (SQLite database program)
609 |- pthreads (Native win32 pthreads library -- dll)
610 |- zlib (Native win32 zlib library)
611 |- wx (wxWidgets source code)
614 |- bacula (main source directory containing configuration
615 | and installation files)
616 |- autoconf (automatic configuration files, not normally used
618 |- intl (programs used to translate)
619 |- platforms (OS specific installation files)
620 |- redhat (Red Hat installation)
621 |- solaris (Sun installation)
622 |- freebsd (FreeBSD installation)
623 |- irix (Irix installation -- not tested)
624 |- unknown (Default if system not identified)
625 |- po (translations of source strings)
626 |- src (source directory; contains global header files)
627 |- cats (SQL catalog database interface directory)
628 |- console (bacula user agent directory)
629 |- dird (Director daemon)
630 |- filed (Unix File daemon)
631 |- win32 (Win32 files to make bacula-fd be a service)
632 |- findlib (Unix file find library for File daemon)
633 |- gnome-console (GNOME version of console program)
634 |- lib (General Bacula library)
635 |- stored (Storage daemon)
636 |- tconsole (Tcl/tk console program -- not yet working)
637 |- testprogs (test programs -- normally only in Kern's tree)
638 |- tools (Various tool programs)
639 |- win32 (Native Win32 File daemon)
640 |- baculafd (Visual Studio project file)
641 |- compat (compatibility interface library)
642 |- filed (links to src/filed)
643 |- findlib (links to src/findlib)
644 |- lib (links to src/lib)
645 |- console (beginning of native console program)
646 |- wx-console (wxWidget console Win32 specific parts)
647 |- wx-console (wxWidgets console main source program)
650 |- regress (Regression scripts)
651 |- bin (temporary directory to hold Bacula installed binaries)
652 |- build (temporary directory to hold Bacula source)
653 |- scripts (scripts and .conf files)
654 |- tests (test scripts)
655 |- tmp (temporary directory for temp files)
656 |- working (temporary working directory for Bacula daemons)
659 |- docs (documentation directory)
660 |- developers (Developer's guide)
661 |- home-page (Bacula's home page source)
662 |- manual (html document directory)
663 |- manual-fr (French translation)
664 |- manual-de (German translation)
665 |- techlogs (Technical development notes);
668 |- rescue (Bacula rescue CDROM)
669 |- linux (Linux rescue CDROM)
670 |- cdrom (Linux rescue CDROM code)
672 |- solaris (Solaris rescue -- incomplete)
673 |- freebsd (FreeBSD rescue -- incomplete)
676 |- gui (Bacula GUI projects)
677 |- bacula-web (Bacula web php management code)
678 |- bimagemgr (Web application for burning CDROMs)
684 \subsubsection*{Header Files}
687 \addcontentsline{toc}{subsubsection}{Header Files}
689 Please carefully follow the scheme defined below as it permits in general only
690 two header file includes per C file, and thus vastly simplifies programming.
691 With a large complex project like Bacula, it isn't always easy to ensure that
692 the right headers are invoked in the right order (there are a few kludges to
693 make this happen -- i.e. in a few include files because of the chicken and egg
694 problem, certain references to typedefs had to be replaced with {\bf void} ).
696 Every file should include {\bf bacula.h}. It pulls in just about everything,
697 with very few exceptions. If you have system dependent ifdefing, please do it
698 in {\bf baconfig.h}. The version number and date are kept in {\bf version.h}.
700 Each of the subdirectories (console, cats, dird, filed, findlib, lib, stored,
701 ...) contains a single directory dependent include file generally the name of
702 the directory, which should be included just after the include of {\bf
703 bacula.h}. This file (for example, for the dird directory, it is {\bf dird.h})
704 contains either definitions of things generally needed in this directory, or
705 it includes the appropriate header files. It always includes {\bf protos.h}.
708 Each subdirectory contains a header file named {\bf protos.h}, which contains
709 the prototypes for subroutines exported by files in that directory. {\bf
710 protos.h} is always included by the main directory dependent include file.
712 \subsubsection*{Programming Standards}
713 \index{Standards!Programming}
714 \index{Programming Standards}
715 \addcontentsline{toc}{subsubsection}{Programming Standards}
717 For the most part, all code should be written in C unless there is a burning
718 reason to use C++, and then only the simplest C++ constructs will be used.
719 Note, Bacula is slowly evolving to use more and more C++.
721 Code should have some documentation -- not a lot, but enough so that I can
722 understand it. Look at the current code, and you will see that I document more
723 than most, but am definitely not a fanatic.
725 I prefer simple linear code where possible. Gotos are strongly discouraged
726 except for handling an error to either bail out or to retry some code, and
727 such use of gotos can vastly simplify the program.
729 Remember this is a C program that is migrating to a {\bf tiny} subset of C++,
730 so be conservative in your use of C++ features.
732 \subsubsection*{Do Not Use}
735 \addcontentsline{toc}{subsubsection}{Do Not Use}
738 \item STL -- it is totally incomprehensible.
741 \subsubsection*{Avoid if Possible}
742 \index{Possible!Avoid if}
743 \index{Avoid if Possible}
744 \addcontentsline{toc}{subsubsection}{Avoid if Possible}
747 \item Using {\bf void *} because this generally means that one must
748 using casting, and in C++ casting is rather ugly. It is OK to use
749 void * to pass structure address where the structure is not known
750 to the routines accepting the packet (typically callback routines).
751 However, declaring "void *buf" is a bad idea. Please use the
752 correct types whenever possible.
753 \item Using undefined storage specifications such as (short, int, long,
754 long long, size_t ...). The problem with all these is that the number of bytes
755 they allocate depends on the compiler and the system. Instead use
756 Bacula's types (int8_t, uint8_t, int32_t, uint32_t, int64_t, and
757 uint64_t). This guarantees that the variables are given exactly the
758 size you want. Please try at all possible to avoid using size_t ssize_t
759 and the such. They are very system dependent. However, some system
760 routines may need them, so their use is often unavoidable.
761 \item Returning a malloc'ed buffer from a subroutine -- someone will forget
763 \item Using reference variables -- it allows subroutines to create side
764 effects. Reference variables are OK, if you are sure the variable
765 *always* exists, and you are sure you can handle the side effects of
766 a subroutine changing the "pointer".
767 \item Heap allocation (malloc) unless needed -- it is expensive.
768 \item Templates -- they can create portability problems.
769 \item Fancy or tricky C or C++ code, unless you give a good explanation of
771 \item Too much inheritance -- it can complicate the code, and make reading it
772 difficult (unless you are in love with colons)
775 \subsubsection*{Do Use Whenever Possible}
776 \index{Possible!Do Use Whenever}
777 \index{Do Use Whenever Possible}
778 \addcontentsline{toc}{subsubsection}{Do Use Whenever Possible}
781 \item Locking and unlocking within a single subroutine.
782 \item Malloc and free within a single subroutine.
783 \item Comments and global explanations on what your code or algorithm does.
786 \subsubsection*{Indenting Standards}
787 \index{Standards!Indenting}
788 \index{Indenting Standards}
789 \addcontentsline{toc}{subsubsection}{Indenting Standards}
791 I cannot stand code indented 8 columns at a time. This makes the code
792 unreadable. Even 4 at a time uses a lot of space, so I have adopted indenting
793 3 spaces at every level. Note, indention is the visual appearance of the
794 source on the page, while tabbing is replacing a series of up to 8 spaces from
797 The closest set of parameters for the Linux {\bf indent} program that will
798 produce reasonably indented code are:
802 -nbad -bap -bbo -nbc -br -brs -c36 -cd36 -ncdb -ce -ci3 -cli0
803 -cp36 -d0 -di1 -ndj -nfc1 -nfca -hnl -i3 -ip0 -l85 -lp -npcs
804 -nprs -npsl -saf -sai -saw -nsob -nss -nbc -ncs -nbfda
808 You can put the above in your .indent.pro file, and then just invoke indent on
809 your file. However, be warned. This does not produce perfect indenting, and it
810 will mess up C++ class statements pretty badly.
812 Braces are required in all if statements (missing in some very old code). To
813 avoid generating too many lines, the first brace appears on the first line
814 (e.g. of an if), and the closing brace is on a line by itself. E.g.
824 Just follow the convention in the code. Originally I indented case clauses
825 under a switch(), but now I prefer non-indented cases.
842 Avoid using // style comments except for temporary code or turning off debug
843 code. Standard C comments are preferred (this also keeps the code closer to
846 Attempt to keep all lines less than 85 characters long so that the whole line
847 of code is readable at one time. This is not a rigid requirement.
849 Always put a brief description at the top of any new file created describing
850 what it does and including your name and the date it was first written. Please
851 don't forget any Copyrights and acknowledgments if it isn't 100\% your code.
852 Also, include the Bacula copyright notice that is in {\bf src/c}.
854 In general you should have two includes at the top of the an include for the
855 particular directory the code is in, for includes are needed, but this should
858 In general (except for self-contained packages), prototypes should all be put
859 in {\bf protos.h} in each directory.
861 Always put space around assignment and comparison operators.
872 but your can compress things in a {\bf for} statement:
876 for (i=0; i < del.num_ids; i++) {
881 Don't overuse the inline if (?:). A full {\bf if} is preferred, except in a
882 print statement, e.g.:
886 if (ua->verbose \&& del.num_del != 0) {
887 bsendmsg(ua, _("Pruned %d %s on Volume %s from catalog.\n"), del.num_del,
888 del.num_del == 1 ? "Job" : "Jobs", mr->VolumeName);
893 Leave a certain amount of debug code (Dmsg) in code you submit, so that future
894 problems can be identified. This is particularly true for complicated code
895 likely to break. However, try to keep the debug code to a minimum to avoid
896 bloating the program and above all to keep the code readable.
898 Please keep the same style in all new code you develop. If you include code
899 previously written, you have the option of leaving it with the old indenting
900 or re-indenting it. If the old code is indented with 8 spaces, then please
901 re-indent it to Bacula standards.
903 If you are using {\bf vim}, simply set your tabstop to 8 and your shiftwidth
906 \subsubsection*{Tabbing}
908 \addcontentsline{toc}{subsubsection}{Tabbing}
910 Tabbing (inserting the tab character in place of spaces) is as normal on all
911 Unix systems -- a tab is converted space up to the next column multiple of 8.
912 My editor converts strings of spaces to tabs automatically -- this results in
913 significant compression of the files. Thus, you can remove tabs by replacing
914 them with spaces if you wish. Please don't confuse tabbing (use of tab
915 characters) with indenting (visual alignment of the code).
917 \subsubsection*{Don'ts}
919 \addcontentsline{toc}{subsubsection}{Don'ts}
934 They are system dependent and un-safe. These should be replaced by the Bacula
939 char *bstrncpy(char *dest, char *source, int dest_size);
940 char *bstrncat(char *dest, char *source, int dest_size);
941 int bsnprintf(char *buf, int32_t buf_len, const char *fmt, ...);
942 int bvsnprintf(char *str, int32_t size, const char *format, va_list ap);
946 See src/lib/bsys.c for more details on these routines.
948 Don't use the {\bf \%lld} or the {\bf \%q} printf format editing types to edit
949 64 bit integers -- they are not portable. Instead, use {\bf \%s} with {\bf
950 edit\_uint64()}. For example:
955 uint64_t num = something;
957 bsnprintf(buf, sizeof(buf), "Num=%s\n", edit_uint64(num, ed1));
961 The edit buffer {\bf ed1} must be at least 27 bytes long to avoid overflow.
962 See src/lib/edit.c for more details. If you look at the code, don't start
963 screaming that I use {\bf lld}. I actually use subtle trick taught to me by
964 John Walker. The {\bf lld} that appears in the editing routine is actually
965 {\bf \#define} to a what is needed on your OS (usually ``lld'' or ``q'') and
966 is defined in autoconf/configure.in for each OS. C string concatenation causes
967 the appropriate string to be concatenated to the ``\%''.
969 Also please don't use the STL or Templates or any complicated C++ code.
971 \subsubsection*{Message Classes}
972 \index{Classes!Message}
973 \index{Message Classes}
974 \addcontentsline{toc}{subsubsection}{Message Classes}
976 Currently, there are five classes of messages: Debug, Error, Job, Memory,
979 \subsubsection*{Debug Messages}
980 \index{Messages!Debug}
981 \index{Debug Messages}
982 \addcontentsline{toc}{subsubsection}{Debug Messages}
984 Debug messages are designed to be turned on at a specified debug level and are
985 always sent to STDOUT. There are designed to only be used in the development
986 debug process. They are coded as:
988 DmsgN(level, message, arg1, ...) where the N is a number indicating how many
989 arguments are to be substituted into the message (i.e. it is a count of the
990 number arguments you have in your message -- generally the number of percent
991 signs (\%)). {\bf level} is the debug level at which you wish the message to
992 be printed. message is the debug message to be printed, and arg1, ... are the
993 arguments to be substituted. Since not all compilers support \#defines with
994 varargs, you must explicitly specify how many arguments you have.
996 When the debug message is printed, it will automatically be prefixed by the
997 name of the daemon which is running, the filename where the Dmsg is, and the
998 line number within the file.
1000 Some actual examples are:
1002 Dmsg2(20, ``MD5len=\%d MD5=\%s\textbackslash{}n'', strlen(buf), buf);
1004 Dmsg1(9, ``Created client \%s record\textbackslash{}n'', client->hdr.name);
1006 \subsubsection*{Error Messages}
1007 \index{Messages!Error}
1008 \index{Error Messages}
1009 \addcontentsline{toc}{subsubsection}{Error Messages}
1011 Error messages are messages that are related to the daemon as a whole rather
1012 than a particular job. For example, an out of memory condition my generate an
1013 error message. They should be very rarely needed. In general, you should be
1014 using Job and Job Queued messages (Jmsg and Qmsg). They are coded as:
1016 EmsgN(error-code, level, message, arg1, ...) As with debug messages, you must
1017 explicitly code the of arguments to be substituted in the message. error-code
1018 indicates the severity or class of error, and it may be one of the following:
1020 \addcontentsline{lot}{table}{Message Error Code Classes}
1021 \begin{longtable}{lp{3in}}
1022 {{\bf M\_ABORT} } & {Causes the daemon to immediately abort. This should be
1023 used only in extreme cases. It attempts to produce a traceback. } \\
1024 {{\bf M\_ERROR\_TERM} } & {Causes the daemon to immediately terminate. This
1025 should be used only in extreme cases. It does not produce a traceback. } \\
1026 {{\bf M\_FATAL} } & {Causes the daemon to terminate the current job, but the
1027 daemon keeps running } \\
1028 {{\bf M\_ERROR} } & {Reports the error. The daemon and the job continue
1030 {{\bf M\_WARNING} } & {Reports an warning message. The daemon and the job
1031 continue running } \\
1032 {{\bf M\_INFO} } & {Reports an informational message.}
1036 There are other error message classes, but they are in a state of being
1037 redesigned or deprecated, so please do not use them. Some actual examples are:
1040 Emsg1(M\_ABORT, 0, ``Cannot create message thread: \%s\textbackslash{}n'',
1043 Emsg3(M\_WARNING, 0, ``Connect to File daemon \%s at \%s:\%d failed. Retrying
1044 ...\textbackslash{}n'', client-\gt{}hdr.name, client-\gt{}address,
1047 Emsg3(M\_FATAL, 0, ``bdird\lt{}filed: bad response from Filed to \%s command:
1048 \%d \%s\textbackslash{}n'', cmd, n, strerror(errno));
1050 \subsubsection*{Job Messages}
1051 \index{Job Messages}
1052 \index{Messages!Job}
1053 \addcontentsline{toc}{subsubsection}{Job Messages}
1055 Job messages are messages that pertain to a particular job such as a file that
1056 could not be saved, or the number of files and bytes that were saved. They
1059 Jmsg(jcr, M\_FATAL, 0, "Text of message");
1061 A Jmsg with M\_FATAL will fail the job. The Jmsg() takes varargs so can
1062 have any number of arguments for substituted in a printf like format.
1063 Output from the Jmsg() will go to the Job report.
1065 If the Jmsg is followed with a number such as Jmsg1(...), the number
1066 indicates the number of arguments to be substituted (varargs is not
1067 standard for \#defines), and what is more important is that the file and
1068 line number will be prefixed to the message. This permits a sort of debug
1071 \subsubsection*{Queued Job Messages}
1072 \index{Queued Job Messages}
1073 \index{Messages!Job}
1074 \addcontentsline{toc}{subsubsection}{Queued Job Messages}
1075 Queued Job messages are similar to Jmsg()s except that the message is
1076 Queued rather than immediately dispatched. This is necessary within the
1077 network subroutines and in the message editing routines. This is to prevent
1078 recursive loops, and to ensure that messages can be delivered even in the
1079 event of a network error.
1082 \subsubsection*{Memory Messages}
1083 \index{Messages!Memory}
1084 \index{Memory Messages}
1085 \addcontentsline{toc}{subsubsection}{Memory Messages}
1087 Memory messages are messages that are edited into a memory buffer. Generally
1088 they are used in low level routines such as the low level device file dev.c in
1089 the Storage daemon or in the low level Catalog routines. These routines do not
1090 generally have access to the Job Control Record and so they return error
1091 messages reformatted in a memory buffer. Mmsg() is the way to do this.