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