3 * Program to scan a Bacula Volume and compare it with
4 * the catalog and optionally synchronize the catalog
7 * Kern E. Sibbald, December 2001
13 Copyright (C) 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
34 #include "findlib/find.h"
35 #include "cats/cats.h"
37 /* Forward referenced functions */
38 static void do_scan(char *fname);
39 static void 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,
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_MD5_record(B_DB *db, char *MD5buf, DEV_RECORD *rec);
56 /* Global variables */
57 static DEVICE *dev = NULL;
59 static JCR *bjcr; /* jcr for bscan */
61 static struct stat statp;
63 static long record_file_index;
64 static POOLMEM *fname; /* original file name */
65 static POOLMEM *ofile; /* output name with prefix */
66 static POOLMEM *lname; /* link name */
71 static FILESET_DBR fsr;
74 static SESSION_LABEL label;
75 static SESSION_LABEL elabel;
77 static time_t lasttime = 0;
79 static char *db_name = "bacula";
80 static char *db_user = "bacula";
81 static char *db_password = "";
82 static char *wd = "/tmp";
83 static int verbose = 0;
84 static int update_db = 0;
85 static int update_vol_info = 0;
86 static int list_records = 0;
91 "\nVersion: " VERSION " (" DATE ")\n\n"
92 "Usage: bscan [-d debug_level] <bacula-archive>\n"
93 " -b bootstrap specify a bootstrap file\n"
94 " -dnn set debug level to nn\n"
95 " -m update media info in database\n"
96 " -n name specify the database name (default bacula)\n"
97 " -u user specify database user name (default bacula)\n"
98 " -p password specify database password (default none)\n"
100 " -s synchronize or store in database\n"
102 " -w dir specify working directory (default /tmp)\n"
103 " -? print this message\n\n"));
107 int main (int argc, char *argv[])
111 my_name_is(argc, argv, "bscan");
112 init_msg(NULL, NULL);
115 while ((ch = getopt(argc, argv, "b:d:mn:p:rsu:vw:?")) != -1) {
118 bsr = parse_bsr(NULL, optarg);
120 case 'd': /* debug level */
121 debug_level = atoi(optarg);
122 if (debug_level <= 0)
139 db_password = optarg;
168 Pmsg0(0, _("Wrong number of arguments: \n"));
172 working_directory = wd;
174 bjcr = setup_jcr("bscan", argv[0], bsr);
176 if ((db=db_init_database(NULL, db_name, db_user, db_password)) == NULL) {
177 Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
179 if (!db_open_database(db)) {
180 Emsg0(M_ERROR_TERM, 0, db_strerror(db));
182 Dmsg0(200, "Database opened\n");
184 Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
194 static void do_scan(char *devname)
197 dev = setup_to_read_device(bjcr);
202 fname = get_pool_memory(PM_FNAME);
203 ofile = get_pool_memory(PM_FNAME);
204 lname = get_pool_memory(PM_FNAME);
206 memset(&ar, 0, sizeof(ar));
207 memset(&pr, 0, sizeof(pr));
208 memset(&jr, 0, sizeof(jr));
209 memset(&cr, 0, sizeof(cr));
210 memset(&fsr, 0, sizeof(fsr));
211 memset(&fr, 0, sizeof(fr));
213 detach_jcr_from_device(dev, bjcr);
215 read_records(bjcr, dev, record_cb, mount_next_read_volume);
216 release_device(bjcr, dev);
218 free_pool_memory(fname);
219 free_pool_memory(ofile);
220 free_pool_memory(lname);
224 static void record_cb(JCR *bjcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
229 if (rec->data_len > 0) {
230 mr.VolBytes += rec->data_len + WRITE_RECHDR_LENGTH; /* Accumulate Volume bytes */
233 Pmsg5(000, _("Record: SessId=%u SessTim=%u FileIndex=%d Stream=%d len=%u\n"),
234 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex,
235 rec->Stream, rec->data_len);
238 * Check for Start or End of Session Record
241 if (rec->FileIndex < 0) {
242 int save_update_db = update_db;
245 dump_label_record(dev, rec, 1);
247 switch (rec->FileIndex) {
249 Pmsg0(000, "Volume is prelabeled. This tape cannot be scanned.\n");
253 unser_volume_label(dev, rec);
254 /* Check Pool info */
255 strcpy(pr.Name, dev->VolHdr.PoolName);
256 strcpy(pr.PoolType, dev->VolHdr.PoolType);
257 if (db_get_pool_record(db, &pr)) {
259 Pmsg1(000, "Pool record for %s found in DB.\n", pr.Name);
263 Pmsg1(000, "VOL_LABEL: Pool record not found for Pool: %s\n",
266 create_pool_record(db, &pr);
268 if (strcmp(pr.PoolType, dev->VolHdr.PoolType) != 0) {
269 Pmsg2(000, "VOL_LABEL: PoolType mismatch. DB=%s Vol=%s\n",
270 pr.PoolType, dev->VolHdr.PoolType);
272 } else if (verbose) {
273 Pmsg1(000, "Pool type \"%s\" is OK.\n", pr.PoolType);
276 /* Check Media Info */
277 memset(&mr, 0, sizeof(mr));
278 strcpy(mr.VolumeName, dev->VolHdr.VolName);
279 mr.PoolId = pr.PoolId;
280 if (db_get_media_record(db, &mr)) {
282 Pmsg1(000, "Media record for %s found in DB.\n", mr.VolumeName);
284 /* Clear out some volume statistics that will be updated */
285 mr.VolJobs = mr.VolFiles = mr.VolBlocks = 0;
286 mr.VolBytes = rec->data_len + 20;
289 Pmsg1(000, "VOL_LABEL: Media record not found for Volume: %s\n",
292 strcpy(mr.MediaType, dev->VolHdr.MediaType);
293 create_media_record(db, &mr, &dev->VolHdr);
295 if (strcmp(mr.MediaType, dev->VolHdr.MediaType) != 0) {
296 Pmsg2(000, "VOL_LABEL: MediaType mismatch. DB=%s Vol=%s\n",
297 mr.MediaType, dev->VolHdr.MediaType);
299 } else if (verbose) {
300 Pmsg1(000, "Media type \"%s\" is OK.\n", mr.MediaType);
302 /* Reset some JCR variables */
303 for (mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
304 mjcr->VolFirstFile = mjcr->FileIndex = 0;
305 mjcr->StartBlock = mjcr->EndBlock = 0;
306 mjcr->StartFile = mjcr->EndFile = 0;
309 Pmsg1(000, "VOL_LABEL: OK for Volume: %s\n", mr.VolumeName);
314 unser_session_label(&label, rec);
315 memset(&jr, 0, sizeof(jr));
316 jr.JobId = label.JobId;
317 if (db_get_job_record(db, &jr)) {
318 /* Job record already exists in DB */
319 update_db = 0; /* don't change db in create_job_record */
321 Pmsg1(000, _("SOS_LABEL: Found Job record for JobId: %d\n"), jr.JobId);
324 /* Must create a Job record in DB */
326 Pmsg1(000, "SOS_LABEL: Job record not found for JobId: %d\n",
330 /* Create Client record if not already there */
331 strcpy(cr.Name, label.ClientName);
332 create_client_record(db, &cr);
333 jr.ClientId = cr.ClientId;
335 /* process label, if Job record exists don't update db */
336 mjcr = create_job_record(db, &jr, &label, rec);
337 update_db = save_update_db;
339 jr.PoolId = pr.PoolId;
340 /* Set start positions into JCR */
341 mjcr->StartBlock = dev->block_num;
342 mjcr->StartFile = dev->file;
343 mjcr->start_time = jr.StartTime;
344 mjcr->JobLevel = jr.Level;
346 mjcr->client_name = get_pool_memory(PM_FNAME);
347 pm_strcpy(&mjcr->client_name, label.ClientName);
348 mjcr->pool_type = get_pool_memory(PM_FNAME);
349 pm_strcpy(&mjcr->pool_type, label.PoolType);
350 mjcr->fileset_name = get_pool_memory(PM_FNAME);
351 pm_strcpy(&mjcr->fileset_name, label.FileSetName);
352 mjcr->pool_name = get_pool_memory(PM_FNAME);
353 pm_strcpy(&mjcr->pool_name, label.PoolName);
355 if (rec->VolSessionId != jr.VolSessionId) {
356 Pmsg3(000, "SOS_LABEL: VolSessId mismatch for JobId=%u. DB=%d Vol=%d\n",
358 jr.VolSessionId, rec->VolSessionId);
361 if (rec->VolSessionTime != jr.VolSessionTime) {
362 Pmsg3(000, "SOS_LABEL: VolSessTime mismatch for JobId=%u. DB=%d Vol=%d\n",
364 jr.VolSessionTime, rec->VolSessionTime);
367 if (jr.PoolId != pr.PoolId) {
368 Pmsg3(000, "SOS_LABEL: PoolId mismatch for JobId=%u. DB=%d Vol=%d\n",
370 jr.PoolId, pr.PoolId);
375 unser_session_label(&elabel, rec);
377 /* Create FileSet record */
378 strcpy(fsr.FileSet, label.FileSetName);
379 create_fileset_record(db, &fsr);
380 jr.FileSetId = fsr.FileSetId;
383 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
385 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
386 rec->VolSessionId, rec->VolSessionTime);
390 /* Do the final update to the Job record */
391 update_job_record(db, &jr, &elabel, rec);
393 mjcr->end_time = jr.EndTime;
394 mjcr->JobStatus = JS_Terminated;
396 /* Create JobMedia record */
397 create_jobmedia_record(db, mjcr);
398 detach_jcr_from_device(dev, mjcr);
404 case EOT_LABEL: /* end of all tapes */
406 * Wiffle through all jobs still open and close
410 for (mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
411 jr.JobId = mjcr->JobId;
412 jr.JobStatus = JS_ErrorTerminated;
413 jr.JobFiles = mjcr->JobFiles;
414 jr.JobBytes = mjcr->JobBytes;
415 jr.VolSessionId = mjcr->VolSessionId;
416 jr.VolSessionTime = mjcr->VolSessionTime;
417 jr.JobTDate = (btime_t)mjcr->start_time;
418 jr.ClientId = mjcr->ClientId;
420 if (!db_update_job_end_record(db, &jr)) {
421 Pmsg1(0, _("Could not update job record. ERR=%s\n"), db_strerror(db));
425 mr.VolFiles = rec->File;
426 mr.VolBlocks = rec->Block;
427 mr.VolBytes += mr.VolBlocks * WRITE_BLKHDR_LENGTH; /* approx. */
429 update_media_record(db, &mr);
430 Pmsg3(0, _("End of Volume. VolFiles=%u VolBlocks=%u VolBytes=%s\n"), mr.VolFiles,
431 mr.VolBlocks, edit_uint64_with_commas(mr.VolBytes, ec1));
440 /* File Attributes stream */
441 if (rec->Stream == STREAM_UNIX_ATTRIBUTES) {
444 if (sizeof_pool_memory(fname) < rec->data_len) {
445 fname = realloc_pool_memory(fname, rec->data_len + 1);
447 if (sizeof_pool_memory(lname) < rec->data_len) {
448 lname = realloc_pool_memory(lname, rec->data_len + 1);
454 * An Attributes record consists of:
459 * Link name (if file linked i.e. FT_LNK)
462 sscanf(rec->data, "%ld %d", &record_file_index, &type);
463 if (record_file_index != rec->FileIndex)
464 Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
465 rec->FileIndex, record_file_index);
467 while (*ap++ != ' ') /* skip record file index */
469 while (*ap++ != ' ') /* skip type */
471 /* Save filename and position to attributes */
476 *fp = *ap++; /* terminate filename & point to attribs */
478 /* Skip through attributes to link name */
483 strcat(lname, lp); /* "save" link name */
487 decode_stat(ap, &statp);
488 print_ls_output(fname, lname, type, &statp);
490 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
492 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Attributes record.\n"),
493 rec->VolSessionId, rec->VolSessionTime);
496 fr.JobId = mjcr->JobId;
498 if (db_get_file_attributes_record(db, fname, &fr)) {
500 Pmsg1(000, _("File record already exists for: %s\n"), fname);
503 create_file_attributes_record(db, mjcr, fname, lname, type, ap, rec);
507 /* Data stream and extracting */
508 } else if (rec->Stream == STREAM_FILE_DATA) {
509 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
511 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Attributes record.\n"),
512 rec->VolSessionId, rec->VolSessionTime);
515 mjcr->JobBytes += rec->data_len;
516 free_jcr(mjcr); /* done using JCR */
518 } else if (rec->Stream == STREAM_GZIP_DATA) {
519 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
521 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Attributes record.\n"),
522 rec->VolSessionId, rec->VolSessionTime);
525 mjcr->JobBytes += rec->data_len;
526 free_jcr(mjcr); /* done using JCR */
528 } else if (rec->Stream == STREAM_MD5_SIGNATURE) {
530 bin_to_base64(MD5buf, (char *)rec->data, 16); /* encode 16 bytes */
532 Pmsg1(000, _("Got MD5 record: %s\n"), MD5buf);
534 update_MD5_record(db, MD5buf, rec);
536 Pmsg2(0, _("Unknown stream type!!! stream=%d data=%s\n"), rec->Stream, rec->data);
542 * Free the Job Control Record if no one is still using it.
543 * Called from main free_jcr() routine in src/lib/jcr.c so
544 * that we can do our Director specific cleanup of the jcr.
546 static void dird_free_jcr(JCR *jcr)
548 Dmsg0(200, "Start dird free_jcr\n");
550 if (jcr->file_bsock) {
551 Dmsg0(200, "Close File bsock\n");
552 bnet_close(jcr->file_bsock);
554 if (jcr->store_bsock) {
555 Dmsg0(200, "Close Store bsock\n");
556 bnet_close(jcr->store_bsock);
558 if (jcr->RestoreBootstrap) {
559 free(jcr->RestoreBootstrap);
561 Dmsg0(200, "End dird free_jcr\n");
565 * We got a File Attributes record on the tape. Now, lookup the Job
566 * record, and then create the attributes record.
568 static int create_file_attributes_record(B_DB *db, JCR *mjcr,
569 char *fname, char *lname, int type,
570 char *ap, DEV_RECORD *rec)
575 ar.ClientId = mjcr->ClientId;
576 ar.JobId = mjcr->JobId;
577 ar.Stream = rec->Stream;
578 ar.FileIndex = rec->FileIndex;
580 if (mjcr->VolFirstFile == 0) {
581 mjcr->VolFirstFile = rec->FileIndex;
583 mjcr->FileIndex = rec->FileIndex;
590 if (!db_create_file_attributes_record(db, &ar)) {
591 Pmsg1(0, _("Could not create File Attributes record. ERR=%s\n"), db_strerror(db));
594 mjcr->FileId = ar.FileId;
597 Pmsg1(000, _("Created File record: %s\n"), fname);
603 * For each Volume we see, we create a Medium record
605 static int create_media_record(B_DB *db, MEDIA_DBR *mr, VOLUME_LABEL *vl)
610 strcpy(mr->VolStatus, "Full");
611 mr->VolRetention = 355 * 3600 * 24; /* 1 year */
612 if (vl->VerNum >= 11) {
613 mr->FirstWritten = (time_t)vl->write_btime;
614 mr->LabelDate = (time_t)vl->label_btime;
616 dt.julian_day_number = vl->write_date;
617 dt.julian_day_fraction = vl->write_time;
619 mr->FirstWritten = mktime(&tm);
620 dt.julian_day_number = vl->label_date;
621 dt.julian_day_fraction = vl->label_time;
623 mr->LabelDate = mktime(&tm);
625 lasttime = mr->LabelDate;
631 if (!db_create_media_record(db, mr)) {
632 Pmsg1(0, _("Could not create media record. ERR=%s\n"), db_strerror(db));
635 if (!db_update_media_record(db, mr)) {
636 Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
640 Pmsg1(000, _("Created Media record for Volume: %s\n"), mr->VolumeName);
647 * Called at end of media to update it
649 static int update_media_record(B_DB *db, MEDIA_DBR *mr)
651 if (!update_db && !update_vol_info) {
655 mr->LastWritten = lasttime;
656 if (!db_update_media_record(db, mr)) {
657 Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
661 Pmsg1(000, _("Updated Media record at end of Volume: %s\n"), mr->VolumeName);
668 static int create_pool_record(B_DB *db, POOL_DBR *pr)
672 pr->VolRetention = 355 * 3600 * 24; /* 1 year */
677 if (!db_create_pool_record(db, pr)) {
678 Pmsg1(0, _("Could not create pool record. ERR=%s\n"), db_strerror(db));
682 Pmsg1(000, _("Created Pool record for Pool: %s\n"), pr->Name);
690 * Called from SOS to create a client for the current Job
692 static int create_client_record(B_DB *db, CLIENT_DBR *cr)
697 if (!db_create_client_record(db, cr)) {
698 Pmsg1(0, _("Could not create Client record. ERR=%s\n"), db_strerror(db));
702 Pmsg1(000, _("Created Client record for Client: %s\n"), cr->Name);
707 static int create_fileset_record(B_DB *db, FILESET_DBR *fsr)
713 fsr->MD5[0] = ' '; /* ***FIXME*** */
715 if (db_get_fileset_record(db, fsr)) {
717 Pmsg1(000, _("Fileset \"%s\" already exists.\n"), fsr->FileSet);
720 if (!db_create_fileset_record(db, fsr)) {
721 Pmsg2(0, _("Could not create FileSet record \"%s\". ERR=%s\n"),
722 fsr->FileSet, db_strerror(db));
726 Pmsg1(000, _("Created FileSet record \"%s\"\n"), fsr->FileSet);
733 * Simulate the two calls on the database to create
734 * the Job record and to update it when the Job actually
737 static JCR *create_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *label,
744 jr->JobId = label->JobId;
745 jr->Type = label->JobType;
746 jr->Level = label->JobLevel;
747 jr->JobStatus = JS_Created;
748 strcpy(jr->Name, label->JobName);
749 strcpy(jr->Job, label->Job);
750 if (label->VerNum >= 11) {
751 jr->SchedTime = (time_t)label->write_btime;
753 dt.julian_day_number = label->write_date;
754 dt.julian_day_fraction = label->write_time;
756 jr->SchedTime = mktime(&tm);
759 jr->StartTime = jr->SchedTime;
760 jr->JobTDate = (btime_t)jr->SchedTime;
761 jr->VolSessionId = rec->VolSessionId;
762 jr->VolSessionTime = rec->VolSessionTime;
764 /* Now create a JCR as if starting the Job */
765 mjcr = create_jcr(jr, rec, label->JobId);
771 /* This creates the bare essentials */
772 if (!db_create_job_record(db, jr)) {
773 Pmsg1(0, _("Could not create JobId record. ERR=%s\n"), db_strerror(db));
777 /* This adds the client, StartTime, JobTDate, ... */
778 if (!db_update_job_start_record(db, jr)) {
779 Pmsg1(0, _("Could not update job start record. ERR=%s\n"), db_strerror(db));
783 Pmsg2(000, _("Created new JobId=%u record for original JobId=%u\n"), jr->JobId,
786 mjcr->JobId = jr->JobId; /* set new JobId */
791 * Simulate the database call that updates the Job
792 * at Job termination time.
794 static int update_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *elabel,
801 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
803 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
804 rec->VolSessionId, rec->VolSessionTime);
807 if (elabel->VerNum >= 11) {
808 jr->EndTime = (time_t)elabel->write_btime;
810 dt.julian_day_number = elabel->write_date;
811 dt.julian_day_fraction = elabel->write_time;
813 jr->EndTime = mktime(&tm);
815 lasttime = jr->EndTime;
816 mjcr->end_time = jr->EndTime;
818 jr->JobId = mjcr->JobId;
819 jr->JobStatus = elabel->JobStatus;
820 mjcr->JobStatus = elabel->JobStatus;
821 jr->JobFiles = elabel->JobFiles;
822 jr->JobBytes = elabel->JobBytes;
823 jr->VolSessionId = rec->VolSessionId;
824 jr->VolSessionTime = rec->VolSessionTime;
825 jr->JobTDate = (btime_t)mjcr->start_time;
826 jr->ClientId = mjcr->ClientId;
833 if (!db_update_job_end_record(db, jr)) {
834 Pmsg2(0, _("Could not update JobId=%u record. ERR=%s\n"), jr->JobId, db_strerror(db));
839 Pmsg1(000, _("Updated Job termination record for new JobId=%u\n"), jr->JobId);
843 static char term_code[70];
844 char sdt[50], edt[50];
845 char ec1[30], ec2[30], ec3[30];
847 switch (mjcr->JobStatus) {
849 term_msg = _("Backup OK");
852 case JS_ErrorTerminated:
853 term_msg = _("*** Backup Error ***");
856 term_msg = _("Backup Cancelled");
859 term_msg = term_code;
860 sprintf(term_code, _("Job Termination code: %d"), mjcr->JobStatus);
863 bstrftime(sdt, sizeof(sdt), mjcr->start_time);
864 bstrftime(edt, sizeof(edt), mjcr->end_time);
875 Volume Session Id: %d\n\
876 Volume Session Time: %d\n\
877 Last Volume Bytes: %s\n\
878 Termination: %s\n\n"),
883 job_level_to_str(mjcr->JobLevel),
887 edit_uint64_with_commas(mjcr->JobFiles, ec1),
888 edit_uint64_with_commas(mjcr->JobBytes, ec2),
890 mjcr->VolSessionTime,
891 edit_uint64_with_commas(mr.VolBytes, ec3),
898 static int create_jobmedia_record(B_DB *db, JCR *mjcr)
902 mjcr->EndBlock = dev->block_num;
903 mjcr->EndFile = dev->file;
905 memset(&jmr, 0, sizeof(jmr));
906 jmr.JobId = mjcr->JobId;
907 jmr.MediaId = mr.MediaId;
908 jmr.FirstIndex = mjcr->VolFirstFile;
909 jmr.LastIndex = mjcr->FileIndex;
910 jmr.StartFile = mjcr->StartFile;
911 jmr.EndFile = mjcr->EndFile;
912 jmr.StartBlock = mjcr->StartBlock;
913 jmr.EndBlock = mjcr->EndBlock;
920 if (!db_create_jobmedia_record(db, &jmr)) {
921 Pmsg1(0, _("Could not create JobMedia record. ERR=%s\n"), db_strerror(db));
925 Pmsg2(000, _("Created JobMedia record JobId %d, MediaId %d\n"),
926 jmr.JobId, jmr.MediaId);
932 * Simulate the database call that updates the MD5 record
934 static int update_MD5_record(B_DB *db, char *MD5buf, DEV_RECORD *rec)
938 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
940 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
941 rec->VolSessionId, rec->VolSessionTime);
950 if (!db_add_MD5_to_file_record(db, mjcr->FileId, MD5buf)) {
951 Pmsg1(0, _("Could not add MD5 to File record. ERR=%s\n"), db_strerror(db));
956 Pmsg0(000, _("Updated MD5 record\n"));
964 * Create a JCR as if we are really starting the job
966 static JCR *create_jcr(JOB_DBR *jr, DEV_RECORD *rec, uint32_t JobId)
970 * Transfer as much as possible to the Job JCR. Most important is
971 * the JobId and the ClientId.
973 jobjcr = new_jcr(sizeof(JCR), dird_free_jcr);
974 jobjcr->JobType = jr->Type;
975 jobjcr->JobLevel = jr->Level;
976 jobjcr->JobStatus = jr->JobStatus;
977 strcpy(jobjcr->Job, jr->Job);
978 jobjcr->JobId = JobId; /* this is JobId on tape */
979 jobjcr->sched_time = jr->SchedTime;
980 jobjcr->start_time = jr->StartTime;
981 jobjcr->VolSessionId = rec->VolSessionId;
982 jobjcr->VolSessionTime = rec->VolSessionTime;
983 jobjcr->ClientId = jr->ClientId;
984 attach_jcr_to_device(dev, jobjcr);
988 /* Dummies to replace askdir.c */
989 int dir_get_volume_info(JCR *jcr, int writing) { return 1;}
990 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
991 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
992 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
993 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
994 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
995 int dir_send_job_status(JCR *jcr) {return 1;}
998 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
1001 * We are at the end of reading a tape. Now, we simulate handling
1002 * the end of writing a tape by wiffling through the attached
1003 * jcrs creating jobmedia records.
1005 Dmsg1(100, "Walk attached jcrs. Volume=%s\n", dev->VolCatInfo.VolCatName);
1006 for (JCR *mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
1008 Pmsg1(000, "create JobMedia for Job %s\n", mjcr->Job);
1010 mjcr->EndBlock = dev->block_num;
1011 mjcr->EndFile = dev->file;
1012 if (!create_jobmedia_record(db, mjcr)) {
1013 Pmsg2(000, _("Could not create JobMedia record for Volume=%s Job=%s\n"),
1014 dev->VolCatInfo.VolCatName, mjcr->Job);
1018 fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
1019 jcr->VolumeName, dev_name(dev));