]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/bscan.c
Removed unused variable.
[bacula/bacula] / bacula / src / stored / bscan.c
1 /*
2  *
3  *  Program to scan a Bacula Volume and compare it with
4  *    the catalog and optionally synchronize the catalog
5  *    with the tape.
6  *
7  *   Kern E. Sibbald, December 2001
8  *
9  *
10  *   Version $Id$
11  */
12 /*
13    Copyright (C) 2001-2004 Kern Sibbald and John Walker
14
15    This program is free software; you can redistribute it and/or
16    modify it under the terms of the GNU General Public License as
17    published by the Free Software Foundation; either version 2 of
18    the License, or (at your option) any later version.
19
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public
26    License along with this program; if not, write to the Free
27    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28    MA 02111-1307, USA.
29
30  */
31
32 #include "bacula.h"
33 #include "stored.h"
34 #include "findlib/find.h"
35 #include "cats/cats.h"
36
37 /* Forward referenced functions */
38 static void do_scan(void);
39 static int record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
40 static int  create_file_attributes_record(B_DB *db, JCR *mjcr, 
41                                char *fname, char *lname, int type,
42                                char *ap, DEV_RECORD *rec);
43 static int  create_media_record(B_DB *db, MEDIA_DBR *mr, VOLUME_LABEL *vl);
44 static int  update_media_record(B_DB *db, MEDIA_DBR *mr);
45 static int  create_pool_record(B_DB *db, POOL_DBR *pr);
46 static JCR *create_job_record(B_DB *db, JOB_DBR *mr, SESSION_LABEL *label, DEV_RECORD *rec);
47 static int  update_job_record(B_DB *db, JOB_DBR *mr, SESSION_LABEL *elabel, 
48                               DEV_RECORD *rec);
49 static int  create_client_record(B_DB *db, CLIENT_DBR *cr);
50 static int  create_fileset_record(B_DB *db, FILESET_DBR *fsr);
51 static int  create_jobmedia_record(B_DB *db, JCR *jcr);
52 static JCR *create_jcr(JOB_DBR *jr, DEV_RECORD *rec, uint32_t JobId);
53 static int update_SIG_record(B_DB *db, char *SIGbuf, DEV_RECORD *rec, int type);
54
55
56 /* Global variables */
57 STORES *me;
58 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
59 int win32_client = 1;
60 #else
61 int win32_client = 0;
62 #endif
63
64
65 /* Local variables */
66 static DEVICE *dev = NULL;
67 static B_DB *db;
68 static JCR *bjcr;                     /* jcr for bscan */
69 static BSR *bsr = NULL;
70 static MEDIA_DBR mr;
71 static POOL_DBR pr;
72 static JOB_DBR jr;
73 static CLIENT_DBR cr;
74 static FILESET_DBR fsr;
75 static ATTR_DBR ar;
76 static FILE_DBR fr;
77 static SESSION_LABEL label;
78 static SESSION_LABEL elabel;
79 static ATTR *attr;
80
81 static time_t lasttime = 0;
82
83 static char *db_name = "bacula";
84 static char *db_user = "bacula";
85 static char *db_password = "";
86 static char *db_host = NULL;
87 static char *wd = NULL;
88 static int update_db = 0;
89 static int update_vol_info = 0;
90 static int list_records = 0;
91 static int ignored_msgs = 0;
92
93 #define CONFIG_FILE "bacula-sd.conf"
94 char *configfile;
95 bool forge_on = false;
96
97
98 static void usage()
99 {
100    fprintf(stderr, _(
101 "\nVersion: " VERSION " (" BDATE ")\n\n"
102 "Usage: bscan [-d debug_level] <bacula-archive>\n"
103 "       -b bootstrap      specify a bootstrap file\n"
104 "       -c <file>         specify configuration file\n"
105 "       -d <nn>           set debug level to nn\n"
106 "       -m                update media info in database\n"
107 "       -n <name>         specify the database name (default bacula)\n"
108 "       -u <user>         specify database user name (default bacula)\n"
109 "       -P <password      specify database password (default none)\n"
110 "       -h <host>         specify database host (default NULL)\n"
111 "       -p                proceed inspite of I/O errors\n"
112 "       -r                list records\n"
113 "       -s                synchronize or store in database\n"
114 "       -v                verbose\n"
115 "       -V <Volumes>      specify Volume names (separated by |)\n"
116 "       -w <dir>          specify working directory (default from conf file)\n"
117 "       -?                print this message\n\n"));
118    exit(1);
119 }
120
121 int main (int argc, char *argv[])
122 {
123    int ch;
124    struct stat stat_buf;
125    char *VolumeName = NULL;
126
127    my_name_is(argc, argv, "bscan");
128    init_msg(NULL, NULL);
129
130
131    while ((ch = getopt(argc, argv, "b:c:d:h:mn:pP:rsu:vV:w:?")) != -1) {
132       switch (ch) {
133       case 'b':
134          bsr = parse_bsr(NULL, optarg);
135          break;
136
137       case 'c':                    /* specify config file */
138          if (configfile != NULL) {
139             free(configfile);
140          }
141          configfile = bstrdup(optarg);
142          break;
143
144       case 'd':                    /* debug level */
145          debug_level = atoi(optarg);
146          if (debug_level <= 0)
147             debug_level = 1; 
148          break;
149
150       case 'h':
151          db_host = optarg;
152          break;
153
154       case 'm':
155          update_vol_info = 1;
156          break;
157
158       case 'n':
159          db_name = optarg;
160          break;
161
162       case 'u':
163          db_user = optarg;
164          break;
165
166       case 'P':
167          db_password = optarg;
168          break;
169
170       case 'p':
171          forge_on = true;
172          break;
173
174       case 'r':
175          list_records = 1;
176          break;
177
178       case 's':
179          update_db = 1;
180          break;
181
182       case 'v':
183          verbose++;
184          break;
185
186       case 'V':                    /* Volume name */
187          VolumeName = optarg;
188          break;
189
190       case 'w':
191          wd = optarg;
192          break;
193
194       case '?':
195       default:
196          usage();
197
198       }  
199    }
200    argc -= optind;
201    argv += optind;
202
203    if (argc != 1) {
204       Pmsg0(0, _("Wrong number of arguments: \n"));
205       usage();
206    }
207
208    if (configfile == NULL) {
209       configfile = bstrdup(CONFIG_FILE);
210    }
211
212    parse_config(configfile);
213    LockRes();
214    me = (STORES *)GetNextRes(R_STORAGE, NULL);
215    if (!me) {
216       UnlockRes();
217       Emsg1(M_ERROR_TERM, 0, _("No Storage resource defined in %s. Cannot continue.\n"), 
218          configfile);
219    }
220    UnlockRes();
221    /* Check if -w option given, otherwise use resource for working directory */
222    if (wd) { 
223       working_directory = wd;
224    } else if (!me->working_directory) {
225       Emsg1(M_ERROR_TERM, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
226          configfile);
227    } else {
228       working_directory = me->working_directory;
229    }
230
231    /* Check that working directory is good */
232    if (stat(working_directory, &stat_buf) != 0) {
233       Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s not found. Cannot continue.\n"),
234          working_directory);
235    }
236    if (!S_ISDIR(stat_buf.st_mode)) {
237       Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s is not a directory. Cannot continue.\n"),
238          working_directory);
239    }
240
241    bjcr = setup_jcr("bscan", argv[0], bsr, VolumeName);
242    dev = setup_to_access_device(bjcr, 1);   /* read device */
243    if (!dev) { 
244       exit(1);
245    }
246
247    if ((db=db_init_database(NULL, db_name, db_user, db_password, db_host, 0, NULL)) == NULL) {
248       Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
249    }
250    if (!db_open_database(NULL, db)) {
251       Emsg0(M_ERROR_TERM, 0, db_strerror(db));
252    }
253    Dmsg0(200, "Database opened\n");
254    if (verbose) {
255       Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
256    }
257
258    do_scan();
259
260    free_jcr(bjcr);
261    return 0;
262 }
263   
264 /*  
265  * We are at the end of reading a tape. Now, we simulate handling
266  *   the end of writing a tape by wiffling through the attached
267  *   jcrs creating jobmedia records.
268  */
269 static int bscan_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
270 {
271    Dmsg1(100, "Walk attached jcrs. Volume=%s\n", dev->VolCatInfo.VolCatName);
272    for (JCR *mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
273       DCR *dcr = mjcr->dcr;
274       if (verbose) {
275          Pmsg1(000, _("Create JobMedia for Job %s\n"), mjcr->Job);
276       }
277       if (dev->state & ST_TAPE) {
278          dcr->EndBlock = dev->EndBlock;
279          dcr->EndFile = dev->EndFile;
280       } else {
281          dcr->EndBlock = (uint32_t)dev->file_addr;
282          dcr->StartBlock = (uint32_t)(dev->file_addr >> 32);
283       }
284       if (!create_jobmedia_record(db, mjcr)) {
285          Pmsg2(000, _("Could not create JobMedia record for Volume=%s Job=%s\n"),
286             dev->VolCatInfo.VolCatName, mjcr->Job);
287       }
288    }  
289    /* Now let common read routine get up next tape. Note,
290     * we call mount_next... with bscan's jcr because that is where we
291     * have the Volume list, but we get attached.
292     */
293    int stat = mount_next_read_volume(jcr, dev, block);
294    /* we must once more detach ourselves (attached by mount_next ...) */
295    detach_jcr_from_device(dev, jcr); /* detach bscan jcr */
296    return stat;
297 }
298
299 static void do_scan()             
300 {
301    attr = new_attr();
302
303    memset(&ar, 0, sizeof(ar));
304    memset(&pr, 0, sizeof(pr));
305    memset(&jr, 0, sizeof(jr));
306    memset(&cr, 0, sizeof(cr));
307    memset(&fsr, 0, sizeof(fsr));
308    memset(&fr, 0, sizeof(fr));
309
310    /* Detach bscan's jcr as we are not a real Job on the tape */
311    detach_jcr_from_device(dev, bjcr);
312
313    read_records(bjcr, dev, record_cb, bscan_mount_next_read_volume);
314    release_device(bjcr);
315
316    free_attr(attr);
317    term_dev(dev);
318 }
319
320 static int record_cb(JCR *bjcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
321 {
322    JCR *mjcr;
323    DCR *dcr;
324    char ec1[30];
325
326    if (rec->data_len > 0) {
327       mr.VolBytes += rec->data_len + WRITE_RECHDR_LENGTH; /* Accumulate Volume bytes */
328    }
329    if (list_records) {
330       Pmsg5(000, _("Record: SessId=%u SessTim=%u FileIndex=%d Stream=%d len=%u\n"),
331             rec->VolSessionId, rec->VolSessionTime, rec->FileIndex, 
332             rec->Stream, rec->data_len);
333    }
334    /* 
335     * Check for Start or End of Session Record 
336     *
337     */
338    if (rec->FileIndex < 0) {
339       int save_update_db = update_db;
340
341       if (verbose > 1) {
342          dump_label_record(dev, rec, 1);
343       }
344       switch (rec->FileIndex) {
345       case PRE_LABEL:
346          Pmsg0(000, _("Volume is prelabeled. This tape cannot be scanned.\n"));
347          return 1;
348          break;
349
350       case VOL_LABEL:
351          unser_volume_label(dev, rec);
352          /* Check Pool info */
353          bstrncpy(pr.Name, dev->VolHdr.PoolName, sizeof(pr.Name));
354          bstrncpy(pr.PoolType, dev->VolHdr.PoolType, sizeof(pr.PoolType));
355          if (db_get_pool_record(bjcr, db, &pr)) {
356             if (verbose) {
357                Pmsg1(000, _("Pool record for %s found in DB.\n"), pr.Name);
358             }
359          } else {
360             if (!update_db) {
361                Pmsg1(000, _("VOL_LABEL: Pool record not found for Pool: %s\n"),
362                   pr.Name);
363             }
364             create_pool_record(db, &pr);
365          }
366          if (strcmp(pr.PoolType, dev->VolHdr.PoolType) != 0) {
367             Pmsg2(000, _("VOL_LABEL: PoolType mismatch. DB=%s Vol=%s\n"),
368                pr.PoolType, dev->VolHdr.PoolType);
369             return 1;
370          } else if (verbose) {
371             Pmsg1(000, _("Pool type \"%s\" is OK.\n"), pr.PoolType);
372          }
373
374          /* Check Media Info */
375          memset(&mr, 0, sizeof(mr));
376          bstrncpy(mr.VolumeName, dev->VolHdr.VolName, sizeof(mr.VolumeName));
377          mr.PoolId = pr.PoolId;
378          if (db_get_media_record(bjcr, db, &mr)) {
379             if (verbose) {
380                Pmsg1(000, _("Media record for %s found in DB.\n"), mr.VolumeName);
381             }
382             /* Clear out some volume statistics that will be updated */
383             mr.VolJobs = mr.VolFiles = mr.VolBlocks = 0;
384             mr.VolBytes = rec->data_len + 20;
385          } else {
386             if (!update_db) {
387                Pmsg1(000, _("VOL_LABEL: Media record not found for Volume: %s\n"),
388                   mr.VolumeName);
389             }
390             bstrncpy(mr.MediaType, dev->VolHdr.MediaType, sizeof(mr.MediaType));
391             create_media_record(db, &mr, &dev->VolHdr);
392          }
393          if (strcmp(mr.MediaType, dev->VolHdr.MediaType) != 0) {
394             Pmsg2(000, _("VOL_LABEL: MediaType mismatch. DB=%s Vol=%s\n"),
395                mr.MediaType, dev->VolHdr.MediaType);
396             return 1;
397          } else if (verbose) {
398             Pmsg1(000, _("Media type \"%s\" is OK.\n"), mr.MediaType);
399          }
400          /* Reset some JCR variables */
401          for (mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
402             dcr = mjcr->dcr;
403             dcr->VolFirstIndex = dcr->FileIndex = 0;
404             dcr->StartBlock = dcr->EndBlock = 0;
405             dcr->StartFile = dcr->EndFile = 0;
406          }
407
408          Pmsg1(000, _("VOL_LABEL: OK for Volume: %s\n"), mr.VolumeName);
409          break;
410
411       case SOS_LABEL:
412          mr.VolJobs++;
413          if (ignored_msgs > 0) {
414             Pmsg1(000, _("%d \"errors\" ignored before first Start of Session record.\n"), 
415                   ignored_msgs);
416             ignored_msgs = 0;
417          }
418          unser_session_label(&label, rec);
419          memset(&jr, 0, sizeof(jr));
420          jr.JobId = label.JobId;
421          if (db_get_job_record(bjcr, db, &jr)) {
422             /* Job record already exists in DB */
423             update_db = 0;  /* don't change db in create_job_record */
424             if (verbose) {
425                Pmsg1(000, _("SOS_LABEL: Found Job record for JobId: %d\n"), jr.JobId);
426             }
427          } else {
428             /* Must create a Job record in DB */
429             if (!update_db) {
430                Pmsg1(000, _("SOS_LABEL: Job record not found for JobId: %d\n"),
431                   jr.JobId);
432             }
433          }
434          /* Create Client record if not already there */
435             bstrncpy(cr.Name, label.ClientName, sizeof(cr.Name));
436             create_client_record(db, &cr);
437             jr.ClientId = cr.ClientId;
438
439          /* process label, if Job record exists don't update db */
440          mjcr = create_job_record(db, &jr, &label, rec);
441          dcr = mjcr->dcr;
442          update_db = save_update_db;
443
444          jr.PoolId = pr.PoolId;
445          /* Set start positions into JCR */
446          if (dev->state & ST_TAPE) {
447             dcr->StartBlock = dev->block_num;
448             dcr->StartFile = dev->file;
449          } else {
450             dcr->StartBlock = (uint32_t)dev->file_addr;
451             dcr->StartFile = (uint32_t)(dev->file_addr >> 32);
452          }
453          mjcr->start_time = jr.StartTime;
454          mjcr->JobLevel = jr.Level;
455
456          mjcr->client_name = get_pool_memory(PM_FNAME);
457          pm_strcpy(&mjcr->client_name, label.ClientName);
458          mjcr->pool_type = get_pool_memory(PM_FNAME);
459          pm_strcpy(&mjcr->pool_type, label.PoolType);
460          mjcr->fileset_name = get_pool_memory(PM_FNAME);
461          pm_strcpy(&mjcr->fileset_name, label.FileSetName);
462          mjcr->pool_name = get_pool_memory(PM_FNAME);
463          pm_strcpy(&mjcr->pool_name, label.PoolName);
464
465          if (rec->VolSessionId != jr.VolSessionId) {
466             Pmsg3(000, _("SOS_LABEL: VolSessId mismatch for JobId=%u. DB=%d Vol=%d\n"),
467                jr.JobId,
468                jr.VolSessionId, rec->VolSessionId);
469             return 1;
470          }
471          if (rec->VolSessionTime != jr.VolSessionTime) {
472             Pmsg3(000, _("SOS_LABEL: VolSessTime mismatch for JobId=%u. DB=%d Vol=%d\n"),
473                jr.JobId,
474                jr.VolSessionTime, rec->VolSessionTime);
475             return 1;
476          }
477          if (jr.PoolId != pr.PoolId) {
478             Pmsg3(000, _("SOS_LABEL: PoolId mismatch for JobId=%u. DB=%d Vol=%d\n"),
479                jr.JobId,
480                jr.PoolId, pr.PoolId);
481             return 1;
482          }
483          break;
484
485       case EOS_LABEL:
486          unser_session_label(&elabel, rec);
487
488          /* Create FileSet record */
489          bstrncpy(fsr.FileSet, label.FileSetName, sizeof(fsr.FileSet));
490          bstrncpy(fsr.MD5, label.FileSetMD5, sizeof(fsr.MD5));
491          create_fileset_record(db, &fsr);
492          jr.FileSetId = fsr.FileSetId;
493
494          mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
495          if (!mjcr) {
496             Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
497                   rec->VolSessionId, rec->VolSessionTime);
498             break;
499          }
500
501          /* Do the final update to the Job record */
502          update_job_record(db, &jr, &elabel, rec);
503
504          mjcr->end_time = jr.EndTime;
505          mjcr->JobStatus = JS_Terminated;
506
507          /* Create JobMedia record */
508          create_jobmedia_record(db, mjcr);
509          detach_jcr_from_device(dev, mjcr);
510          free_jcr(mjcr);
511
512          break;
513
514       case EOM_LABEL:
515          break;
516
517       case EOT_LABEL:              /* end of all tapes */
518          /* 
519           * Wiffle through all jobs still open and close
520           *   them.
521           */
522          if (update_db) {
523             mjcr=next_attached_jcr(dev, NULL);
524             for ( ; mjcr; ) {
525                JCR *njcr; 
526                jr.JobId = mjcr->JobId;
527                jr.JobStatus = JS_ErrorTerminated;
528                jr.JobFiles = mjcr->JobFiles;
529                jr.JobBytes = mjcr->JobBytes;
530                jr.VolSessionId = mjcr->VolSessionId;
531                jr.VolSessionTime = mjcr->VolSessionTime;
532                jr.JobTDate = (utime_t)mjcr->start_time;
533                jr.ClientId = mjcr->ClientId;
534                if (!db_update_job_end_record(bjcr, db, &jr)) {
535                   Pmsg1(0, _("Could not update job record. ERR=%s\n"), db_strerror(db));
536                }
537                njcr = mjcr->next_dev;
538                free_jcr(mjcr);
539                mjcr = njcr;
540             }
541          }
542          mr.VolFiles = rec->File;
543          mr.VolBlocks = rec->Block;
544          mr.VolBytes += mr.VolBlocks * WRITE_BLKHDR_LENGTH; /* approx. */
545          mr.VolMounts++;
546          update_media_record(db, &mr);
547          Pmsg3(0, _("End of all Volumes. VolFiles=%u VolBlocks=%u VolBytes=%s\n"), mr.VolFiles,
548                     mr.VolBlocks, edit_uint64_with_commas(mr.VolBytes, ec1));
549          break;
550       default:
551          break;
552       } /* end switch */
553       return 1;
554    }
555
556    mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
557    if (!mjcr) {
558       if (mr.VolJobs > 0) {
559          Pmsg2(000, _("Could not find Job for SessId=%d SessTime=%d record.\n"),
560                       rec->VolSessionId, rec->VolSessionTime);
561       } else {
562          ignored_msgs++;
563       }
564       return 1;
565    }
566    dcr = mjcr->dcr;
567    if (dcr->VolFirstIndex == 0) {
568       dcr->VolFirstIndex = block->FirstIndex;
569    }
570
571    /* File Attributes stream */
572    switch (rec->Stream) {
573    case STREAM_UNIX_ATTRIBUTES:   
574    case STREAM_UNIX_ATTRIBUTES_EX:  
575
576       if (!unpack_attributes_record(bjcr, rec->Stream, rec->data, attr)) {
577          Emsg0(M_ERROR_TERM, 0, _("Cannot continue.\n"));
578       }
579
580       if (attr->file_index != rec->FileIndex) {
581          Emsg2(M_ERROR_TERM, 0, _("Record header file index %ld not equal record index %ld\n"),
582             rec->FileIndex, attr->file_index);
583       }
584        
585       if (verbose > 1) {
586          decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
587          build_attr_output_fnames(bjcr, attr);
588          print_ls_output(bjcr, attr);
589       }
590       fr.JobId = mjcr->JobId;
591       fr.FileId = 0;
592       if (db_get_file_attributes_record(bjcr, db, attr->fname, NULL, &fr)) {
593          if (verbose > 1) {
594             Pmsg1(000, _("File record already exists for: %s\n"), attr->fname);
595          }
596       } else {
597          create_file_attributes_record(db, mjcr, attr->fname, attr->lname, 
598             attr->type, attr->attr, rec);
599       }
600       free_jcr(mjcr);
601       break;
602
603    /* Data stream */
604    case STREAM_WIN32_DATA:
605    case STREAM_FILE_DATA:
606    case STREAM_SPARSE_DATA:
607       mjcr->JobBytes += rec->data_len;
608       if (rec->Stream == STREAM_SPARSE_DATA) {
609          mjcr->JobBytes -= sizeof(uint64_t);
610       }
611          
612       free_jcr(mjcr);                 /* done using JCR */
613       break;
614
615    case STREAM_GZIP_DATA:
616       mjcr->JobBytes += rec->data_len; /* No correct, we should expand it */
617       free_jcr(mjcr);                 /* done using JCR */
618       break;
619
620    case STREAM_SPARSE_GZIP_DATA:
621       mjcr->JobBytes += rec->data_len - sizeof(uint64_t); /* No correct, we should expand it */
622       free_jcr(mjcr);                 /* done using JCR */
623       break;
624
625    /* Win32 GZIP stream */
626    case STREAM_WIN32_GZIP_DATA:
627       mjcr->JobBytes += rec->data_len;
628       free_jcr(mjcr);                 /* done using JCR */
629       break;
630
631    case STREAM_MD5_SIGNATURE:
632       char MD5buf[50];
633       bin_to_base64(MD5buf, (char *)rec->data, 16); /* encode 16 bytes */
634       if (verbose > 1) {
635          Pmsg1(000, _("Got MD5 record: %s\n"), MD5buf);
636       }
637       update_SIG_record(db, MD5buf, rec, MD5_SIG);
638       break;
639
640    case STREAM_SHA1_SIGNATURE:
641       char SIGbuf[50];
642       bin_to_base64(SIGbuf, (char *)rec->data, 20); /* encode 20 bytes */
643       if (verbose > 1) {
644          Pmsg1(000, _("Got SHA1 record: %s\n"), SIGbuf);
645       }
646       update_SIG_record(db, SIGbuf, rec, SHA1_SIG);
647       break;
648
649
650    case STREAM_PROGRAM_NAMES:
651       if (verbose) {
652          Pmsg1(000, _("Got Prog Names Stream: %s\n"), rec->data);
653       }
654       break;
655
656    case STREAM_PROGRAM_DATA:
657       if (verbose > 1) {
658          Pmsg0(000, _("Got Prog Data Stream record.\n"));
659       }
660       break;
661    default:
662       Pmsg2(0, _("Unknown stream type!!! stream=%d data=%s\n"), rec->Stream, rec->data);
663       break;
664    }
665    return 1;
666 }
667
668 /*
669  * Free the Job Control Record if no one is still using it.
670  *  Called from main free_jcr() routine in src/lib/jcr.c so
671  *  that we can do our Director specific cleanup of the jcr.
672  */
673 static void bscan_free_jcr(JCR *jcr)
674 {
675    Dmsg0(200, "Start dird free_jcr\n");
676
677    if (jcr->file_bsock) {
678       Dmsg0(200, "Close File bsock\n");
679       bnet_close(jcr->file_bsock);
680    }
681    if (jcr->store_bsock) {
682       Dmsg0(200, "Close Store bsock\n");
683       bnet_close(jcr->store_bsock);
684    }
685    if (jcr->RestoreBootstrap) {
686       free(jcr->RestoreBootstrap);
687    }
688    if (jcr->dcr) {
689       free_dcr(jcr->dcr);
690       jcr->dcr = NULL;
691    }
692    Dmsg0(200, "End dird free_jcr\n");
693 }
694
695 /*
696  * We got a File Attributes record on the tape.  Now, lookup the Job
697  *   record, and then create the attributes record.
698  */
699 static int create_file_attributes_record(B_DB *db, JCR *mjcr,
700                                char *fname, char *lname, int type,
701                                char *ap, DEV_RECORD *rec)
702 {
703    DCR *dcr = mjcr->dcr;
704    ar.fname = fname;
705    ar.link = lname;
706    ar.ClientId = mjcr->ClientId;
707    ar.JobId = mjcr->JobId;
708    ar.Stream = rec->Stream;
709    ar.FileIndex = rec->FileIndex;
710    ar.attr = ap;
711    if (dcr->VolFirstIndex == 0) {
712       dcr->VolFirstIndex = rec->FileIndex;
713    }
714    dcr->FileIndex = rec->FileIndex;
715    mjcr->JobFiles++;
716
717    if (!update_db) {
718       return 1;
719    }
720
721    if (!db_create_file_attributes_record(bjcr, db, &ar)) {
722       Pmsg1(0, _("Could not create File Attributes record. ERR=%s\n"), db_strerror(db));
723       return 0;
724    }
725    mjcr->FileId = ar.FileId;
726
727    if (verbose > 1) {
728       Pmsg1(000, _("Created File record: %s\n"), fname);   
729    }
730    return 1;
731 }
732
733 /*
734  * For each Volume we see, we create a Medium record
735  */
736 static int create_media_record(B_DB *db, MEDIA_DBR *mr, VOLUME_LABEL *vl)
737 {
738    struct date_time dt;
739    struct tm tm;
740
741    /* We mark Vols as Archive to keep them from being re-written */
742    bstrncpy(mr->VolStatus, "Archive", sizeof(mr->VolStatus));
743    mr->VolRetention = 365 * 3600 * 24; /* 1 year */
744    if (vl->VerNum >= 11) {
745       mr->FirstWritten = btime_to_utime(vl->write_btime);
746       mr->LabelDate    = btime_to_utime(vl->label_btime);
747    } else {
748       /* DEPRECATED DO NOT USE */
749       dt.julian_day_number = vl->write_date;
750       dt.julian_day_fraction = vl->write_time;
751       tm_decode(&dt, &tm);
752       mr->FirstWritten = mktime(&tm);
753       dt.julian_day_number = vl->label_date;
754       dt.julian_day_fraction = vl->label_time;
755       tm_decode(&dt, &tm);
756       mr->LabelDate = mktime(&tm);
757    }
758    lasttime = mr->LabelDate;
759
760    if (!update_db) {
761       return 1;
762    }
763
764    if (!db_create_media_record(bjcr, db, mr)) {
765       Pmsg1(0, _("Could not create media record. ERR=%s\n"), db_strerror(db));
766       return 0;
767    }
768    if (!db_update_media_record(bjcr, db, mr)) {
769       Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
770       return 0;
771    }
772    if (verbose) {
773       Pmsg1(000, _("Created Media record for Volume: %s\n"), mr->VolumeName);
774    }
775    return 1;
776
777 }
778
779 /*
780  * Called at end of media to update it
781  */
782 static int update_media_record(B_DB *db, MEDIA_DBR *mr)
783 {
784    if (!update_db && !update_vol_info) {
785       return 1;
786    }
787
788    mr->LastWritten = lasttime;
789    if (!db_update_media_record(bjcr, db, mr)) {
790       Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
791       return 0;
792    }
793    if (verbose) {
794       Pmsg1(000, _("Updated Media record at end of Volume: %s\n"), mr->VolumeName);
795    }
796    return 1;
797
798 }
799
800
801 static int create_pool_record(B_DB *db, POOL_DBR *pr)
802 {
803    pr->NumVols++;
804    pr->UseCatalog = 1;
805    pr->VolRetention = 355 * 3600 * 24; /* 1 year */
806
807    if (!update_db) {
808       return 1;
809    }
810    if (!db_create_pool_record(bjcr, db, pr)) {
811       Pmsg1(0, _("Could not create pool record. ERR=%s\n"), db_strerror(db));
812       return 0;
813    }
814    if (verbose) {
815       Pmsg1(000, _("Created Pool record for Pool: %s\n"), pr->Name);
816    }
817    return 1;
818
819 }
820
821
822 /*
823  * Called from SOS to create a client for the current Job 
824  */
825 static int create_client_record(B_DB *db, CLIENT_DBR *cr)
826 {
827    if (!update_db) {
828       return 1;
829    }
830    if (!db_create_client_record(bjcr, db, cr)) {
831       Pmsg1(0, _("Could not create Client record. ERR=%s\n"), db_strerror(db));
832       return 0;
833    }
834    if (verbose) {
835       Pmsg1(000, _("Created Client record for Client: %s\n"), cr->Name);
836    }
837    return 1;
838 }
839
840 static int create_fileset_record(B_DB *db, FILESET_DBR *fsr)
841 {
842    if (!update_db) {
843       return 1;
844    }
845    fsr->FileSetId = 0;
846    if (fsr->MD5[0] == 0) {
847       fsr->MD5[0] = ' ';              /* Equivalent to nothing */
848       fsr->MD5[1] = 0;
849    }
850    if (db_get_fileset_record(bjcr, db, fsr)) {
851       if (verbose) {
852          Pmsg1(000, _("Fileset \"%s\" already exists.\n"), fsr->FileSet);
853       }
854    } else {
855       if (!db_create_fileset_record(bjcr, db, fsr)) {
856          Pmsg2(0, _("Could not create FileSet record \"%s\". ERR=%s\n"), 
857             fsr->FileSet, db_strerror(db));
858          return 0;
859       }
860       if (verbose) {
861          Pmsg1(000, _("Created FileSet record \"%s\"\n"), fsr->FileSet);
862       }
863    }
864    return 1;
865 }
866
867 /*
868  * Simulate the two calls on the database to create
869  *  the Job record and to update it when the Job actually
870  *  begins running.
871  */
872 static JCR *create_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *label, 
873                              DEV_RECORD *rec)
874 {
875    JCR *mjcr;
876    struct date_time dt;
877    struct tm tm;
878
879    jr->JobId = label->JobId;
880    jr->Type = label->JobType;
881    jr->Level = label->JobLevel;
882    jr->JobStatus = JS_Created;
883    bstrncpy(jr->Name, label->JobName, sizeof(jr->Name));
884    bstrncpy(jr->Job, label->Job, sizeof(jr->Job));
885    if (label->VerNum >= 11) {
886       jr->SchedTime = btime_to_unix(label->write_btime);
887    } else {
888       dt.julian_day_number = label->write_date;
889       dt.julian_day_fraction = label->write_time;
890       tm_decode(&dt, &tm);
891       jr->SchedTime = mktime(&tm);
892    }
893
894    jr->StartTime = jr->SchedTime;
895    jr->JobTDate = (utime_t)jr->SchedTime;
896    jr->VolSessionId = rec->VolSessionId;
897    jr->VolSessionTime = rec->VolSessionTime;
898
899    /* Now create a JCR as if starting the Job */
900    mjcr = create_jcr(jr, rec, label->JobId);
901
902    if (!update_db) {
903       return mjcr;
904    }
905
906    /* This creates the bare essentials */
907    if (!db_create_job_record(bjcr, db, jr)) {
908       Pmsg1(0, _("Could not create JobId record. ERR=%s\n"), db_strerror(db));
909       return mjcr;
910    }
911
912    /* This adds the client, StartTime, JobTDate, ... */
913    if (!db_update_job_start_record(bjcr, db, jr)) {
914       Pmsg1(0, _("Could not update job start record. ERR=%s\n"), db_strerror(db));
915       return mjcr;
916    }
917    Pmsg2(000, _("Created new JobId=%u record for original JobId=%u\n"), jr->JobId, 
918          label->JobId);
919    mjcr->JobId = jr->JobId;           /* set new JobId */
920    return mjcr;
921 }
922
923 /* 
924  * Simulate the database call that updates the Job 
925  *  at Job termination time.
926  */
927 static int update_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *elabel,
928                               DEV_RECORD *rec)
929 {
930    struct date_time dt;
931    struct tm tm;
932    JCR *mjcr;
933
934    mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
935    if (!mjcr) {
936       Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
937                    rec->VolSessionId, rec->VolSessionTime);
938       return 0;
939    }
940    if (elabel->VerNum >= 11) {
941       jr->EndTime = btime_to_unix(elabel->write_btime);
942    } else {
943       dt.julian_day_number = elabel->write_date;
944       dt.julian_day_fraction = elabel->write_time;
945       tm_decode(&dt, &tm);
946       jr->EndTime = mktime(&tm);
947    }
948    lasttime = jr->EndTime;
949    mjcr->end_time = jr->EndTime;
950
951    jr->JobId = mjcr->JobId;
952    jr->JobStatus = elabel->JobStatus;
953    mjcr->JobStatus = elabel->JobStatus;
954    jr->JobFiles = elabel->JobFiles;
955    jr->JobBytes = elabel->JobBytes;
956    jr->VolSessionId = rec->VolSessionId;
957    jr->VolSessionTime = rec->VolSessionTime;
958    jr->JobTDate = (utime_t)mjcr->start_time;
959    jr->ClientId = mjcr->ClientId;
960
961    if (!update_db) {
962       free_jcr(mjcr);
963       return 1;
964    }
965    
966    if (!db_update_job_end_record(bjcr, db, jr)) {
967       Pmsg2(0, _("Could not update JobId=%u record. ERR=%s\n"), jr->JobId,  db_strerror(db));
968       free_jcr(mjcr);
969       return 0;
970    }
971    if (verbose) {
972       Pmsg1(000, _("Updated Job termination record for new JobId=%u\n"), jr->JobId);
973    }
974    if (verbose > 1) {
975       char *term_msg;
976       static char term_code[70];
977       char sdt[50], edt[50];
978       char ec1[30], ec2[30], ec3[30];
979
980       switch (mjcr->JobStatus) {
981       case JS_Terminated:
982          term_msg = _("Backup OK");
983          break;
984       case JS_FatalError:
985       case JS_ErrorTerminated:
986          term_msg = _("*** Backup Error ***");
987          break;
988       case JS_Canceled:
989          term_msg = _("Backup Canceled");
990          break;
991       default:
992          term_msg = term_code;
993          sprintf(term_code, _("Job Termination code: %d"), mjcr->JobStatus);
994          break;
995       }
996       bstrftime(sdt, sizeof(sdt), mjcr->start_time);
997       bstrftime(edt, sizeof(edt), mjcr->end_time);
998       Pmsg14(000,  _("%s\n\
999 JobId:                  %d\n\
1000 Job:                    %s\n\
1001 FileSet:                %s\n\
1002 Backup Level:           %s\n\
1003 Client:                 %s\n\
1004 Start time:             %s\n\
1005 End time:               %s\n\
1006 Files Written:          %s\n\
1007 Bytes Written:          %s\n\
1008 Volume Session Id:      %d\n\
1009 Volume Session Time:    %d\n\
1010 Last Volume Bytes:      %s\n\
1011 Termination:            %s\n\n"),
1012         edt,
1013         mjcr->JobId,
1014         mjcr->Job,
1015         mjcr->fileset_name,
1016         job_level_to_str(mjcr->JobLevel),
1017         mjcr->client_name,
1018         sdt,
1019         edt,
1020         edit_uint64_with_commas(mjcr->JobFiles, ec1),
1021         edit_uint64_with_commas(mjcr->JobBytes, ec2),
1022         mjcr->VolSessionId,
1023         mjcr->VolSessionTime,
1024         edit_uint64_with_commas(mr.VolBytes, ec3),
1025         term_msg);
1026    }
1027    free_jcr(mjcr);
1028    return 1;
1029 }
1030
1031 static int create_jobmedia_record(B_DB *db, JCR *mjcr)
1032 {
1033    JOBMEDIA_DBR jmr;
1034    DCR *dcr = mjcr->dcr;
1035
1036    if (dev->state & ST_TAPE) {
1037       dcr->EndBlock = dev->EndBlock;
1038       dcr->EndFile  = dev->EndFile;
1039    } else {
1040       dcr->EndBlock = (uint32_t)dev->file_addr;
1041       dcr->EndFile = (uint32_t)(dev->file_addr >> 32);
1042    }
1043
1044    memset(&jmr, 0, sizeof(jmr));
1045    jmr.JobId = mjcr->JobId;
1046    jmr.MediaId = mr.MediaId;
1047    jmr.FirstIndex = dcr->VolFirstIndex;
1048    jmr.LastIndex = dcr->FileIndex;
1049    jmr.StartFile = dcr->StartFile;
1050    jmr.EndFile = dcr->EndFile;
1051    jmr.StartBlock = dcr->StartBlock;
1052    jmr.EndBlock = dcr->EndBlock;
1053
1054
1055    if (!update_db) {
1056       return 1;
1057    }
1058
1059    if (!db_create_jobmedia_record(bjcr, db, &jmr)) {
1060       Pmsg1(0, _("Could not create JobMedia record. ERR=%s\n"), db_strerror(db));
1061       return 0;
1062    }
1063    if (verbose) {
1064       Pmsg2(000, _("Created JobMedia record JobId %d, MediaId %d\n"), 
1065                 jmr.JobId, jmr.MediaId);
1066    }
1067    return 1;
1068 }
1069
1070 /* 
1071  * Simulate the database call that updates the MD5/SHA1 record
1072  */
1073 static int update_SIG_record(B_DB *db, char *SIGbuf, DEV_RECORD *rec, int type)
1074 {
1075    JCR *mjcr;
1076
1077    mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
1078    if (!mjcr) {
1079       if (mr.VolJobs > 0) {
1080          Pmsg2(000, _("Could not find SessId=%d SessTime=%d for MD5/SHA1 record.\n"),
1081                       rec->VolSessionId, rec->VolSessionTime);
1082       } else {
1083          ignored_msgs++;
1084       }
1085       return 0;
1086    }
1087
1088    if (!update_db) {
1089       free_jcr(mjcr);
1090       return 1;
1091    }
1092    
1093    if (!db_add_SIG_to_file_record(bjcr, db, mjcr->FileId, SIGbuf, type)) {
1094       Pmsg1(0, _("Could not add MD5/SHA1 to File record. ERR=%s\n"), db_strerror(db));
1095       free_jcr(mjcr);
1096       return 0;
1097    }
1098    if (verbose > 1) {
1099       Pmsg0(000, _("Updated MD5/SHA1 record\n"));
1100    }
1101    free_jcr(mjcr);
1102    return 1;
1103 }
1104
1105
1106 /* 
1107  * Create a JCR as if we are really starting the job
1108  */
1109 static JCR *create_jcr(JOB_DBR *jr, DEV_RECORD *rec, uint32_t JobId)
1110 {
1111    JCR *jobjcr;
1112    /*
1113     * Transfer as much as possible to the Job JCR. Most important is
1114     *   the JobId and the ClientId.
1115     */
1116    jobjcr = new_jcr(sizeof(JCR), bscan_free_jcr);
1117    jobjcr->JobType = jr->Type;
1118    jobjcr->JobLevel = jr->Level;
1119    jobjcr->JobStatus = jr->JobStatus;
1120    bstrncpy(jobjcr->Job, jr->Job, sizeof(jobjcr->Job));
1121    jobjcr->JobId = JobId;      /* this is JobId on tape */
1122    jobjcr->sched_time = jr->SchedTime;
1123    jobjcr->start_time = jr->StartTime;
1124    jobjcr->VolSessionId = rec->VolSessionId;
1125    jobjcr->VolSessionTime = rec->VolSessionTime;
1126    jobjcr->ClientId = jr->ClientId;
1127    attach_jcr_to_device(dev, jobjcr);
1128    new_dcr(jobjcr, dev);
1129    return jobjcr;
1130 }
1131
1132 /* Dummies to replace askdir.c */
1133 int     dir_get_volume_info(JCR *jcr, enum get_vol_info_rw  writing) { return 1;}
1134 int     dir_find_next_appendable_volume(JCR *jcr) { return 1;}
1135 int     dir_update_volume_info(JCR *jcr, DEVICE *dev, int relabel) { return 1; }
1136 int     dir_create_jobmedia_record(JCR *jcr) { return 1; }
1137 int     dir_ask_sysop_to_create_appendable_volume(JCR *jcr, DEVICE *dev) { return 1; }
1138 int     dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
1139 int     dir_send_job_status(JCR *jcr) {return 1;}
1140
1141
1142 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
1143 {
1144    fprintf(stderr, _("Mount Volume \"%s\" on device \"%s\" and press return when ready: "),
1145       jcr->VolumeName, dev_name(dev));
1146    getchar();   
1147    return 1;
1148 }