]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/btape.c
edb6eef3bc4e51af6fd4f42fef81ce79bbc79c5a
[bacula/bacula] / bacula / src / stored / btape.c
1 /*
2  *
3  *   Bacula Tape manipulation program
4  *
5  *    Has various tape manipulation commands -- mostly for
6  *    use in determining how tapes really work.
7  *
8  *     Kern Sibbald, April MM
9  *
10  *   Note, this program reads stored.conf, and will only
11  *     talk to devices that are configured.
12  *
13  *   Version $Id$
14  *
15  */
16 /*
17    Copyright (C) 2000-2004 Kern Sibbald and John Walker
18
19    This program is free software; you can redistribute it and/or
20    modify it under the terms of the GNU General Public License as
21    published by the Free Software Foundation; either version 2 of
22    the License, or (at your option) any later version.
23
24    This program is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27    General Public License for more details.
28
29    You should have received a copy of the GNU General Public
30    License along with this program; if not, write to the Free
31    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32    MA 02111-1307, USA.
33
34  */
35
36 #include "bacula.h"
37 #include "stored.h"
38
39
40 /* External subroutines */
41 extern void free_config_resources();
42
43 /* Exported variables */
44 int quit = 0;
45 char buf[100000];
46 int bsize = TAPE_BSIZE;
47 char VolName[MAX_NAME_LENGTH];
48
49 /*
50  * If you change the format of the state file, 
51  *  increment this value
52  */ 
53 static uint32_t btape_state_level = 2;
54
55 DEVICE *dev = NULL;
56 DEVRES *device = NULL;
57
58             
59 /* Forward referenced subroutines */
60 static void do_tape_cmds();
61 static void helpcmd();
62 static void scancmd();
63 static void rewindcmd();
64 static void clearcmd();
65 static void wrcmd();
66 static void rrcmd();
67 static void eodcmd();
68 static void fillcmd();
69 static void qfillcmd();
70 static void statcmd();
71 static void unfillcmd();
72 static int flush_block(DEV_BLOCK *block, int dump);
73 #ifdef xxx_needed
74 static int record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
75 #endif
76 static int quickie_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
77 static bool compare_blocks(DEV_BLOCK *last_block, DEV_BLOCK *block);
78 static int my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block);
79 static void scan_blocks();
80 static void set_volume_name(char *VolName, int volnum);
81 static void rawfill_cmd();
82 static void bfill_cmd();
83 static bool open_the_device();
84 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd);
85 static void autochangercmd();
86 static void do_unfill();
87
88
89 /* Static variables */
90 #define CONFIG_FILE "bacula-sd.conf"
91 char *configfile;
92
93 #define MAX_CMD_ARGS 30
94 static POOLMEM *cmd;
95 static POOLMEM *args;
96 static char *argk[MAX_CMD_ARGS];
97 static char *argv[MAX_CMD_ARGS];
98 static int argc;
99
100 static int quickie_count = 0;
101 static BSR *bsr = NULL;
102 static int signals = TRUE;
103 static bool ok;
104 static int stop = 0;
105 static uint64_t vol_size;
106 static uint64_t VolBytes;
107 static time_t now;
108 static double kbs;
109 static int32_t file_index;
110 static int end_of_tape = 0;
111 static uint32_t LastBlock = 0;
112 static uint32_t eot_block;
113 static uint32_t eot_block_len;
114 static uint32_t eot_FileIndex;
115 static int dumped = 0;
116 static DEV_BLOCK *last_block1 = NULL;
117 static DEV_BLOCK *last_block2 = NULL;
118 static DEV_BLOCK *last_block = NULL;
119 static DEV_BLOCK *this_block = NULL;
120 static DEV_BLOCK *first_block = NULL;
121 static uint32_t last_file1 = 0;
122 static uint32_t last_file2 = 0;
123 static uint32_t last_file = 0;
124 static uint32_t last_block_num1 = 0;
125 static uint32_t last_block_num2 = 0;
126 static uint32_t last_block_num = 0;
127 static uint32_t BlockNumber = 0;
128 static bool simple = true; 
129
130 static char *VolumeName = NULL;
131 static int vol_num = 0;
132
133 static JCR *jcr = NULL;
134
135
136 static void usage();
137 static void terminate_btape(int sig);
138 int get_cmd(char *prompt);
139
140
141 /*********************************************************************
142  *
143  *         Main Bacula Pool Creation Program
144  *
145  */
146 int main(int margc, char *margv[])
147 {
148    int ch;
149
150    /* Sanity checks */
151    if (TAPE_BSIZE % DEV_BSIZE != 0 || TAPE_BSIZE / DEV_BSIZE == 0) {
152       Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
153          TAPE_BSIZE, DEV_BSIZE);
154    }
155    if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
156       Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
157    }
158
159    printf("Tape block granularity is %d bytes.\n", TAPE_BSIZE);
160
161    working_directory = "/tmp";
162    my_name_is(margc, margv, "btape");
163    init_msg(NULL, NULL);
164
165    while ((ch = getopt(margc, margv, "b:c:d:sv?")) != -1) {
166       switch (ch) {
167       case 'b':                    /* bootstrap file */
168          bsr = parse_bsr(NULL, optarg);
169 //       dump_bsr(bsr, true);
170          break;
171
172       case 'c':                    /* specify config file */
173          if (configfile != NULL) {
174             free(configfile);
175          }
176          configfile = bstrdup(optarg);
177          break;
178
179       case 'd':                    /* set debug level */
180          debug_level = atoi(optarg);
181          if (debug_level <= 0) {
182             debug_level = 1; 
183          }
184          break;
185
186       case 's':
187          signals = FALSE;
188          break;
189
190       case 'v':
191          verbose++;
192          break;
193
194       case '?':
195       default:
196          helpcmd();
197          exit(0);
198
199       }  
200    }
201    margc -= optind;
202    margv += optind;
203
204    cmd = get_pool_memory(PM_FNAME);
205    args = get_pool_memory(PM_FNAME);
206    
207    if (signals) {
208       init_signals(terminate_btape);
209    }
210
211    if (configfile == NULL) {
212       configfile = bstrdup(CONFIG_FILE);
213    }
214
215    daemon_start_time = time(NULL);
216
217    parse_config(configfile);
218
219
220    /* See if we can open a device */
221    if (margc == 0) {
222       Pmsg0(000, "No archive name specified.\n");
223       usage();
224       exit(1);
225    } else if (margc != 1) {
226       Pmsg0(000, "Improper number of arguments specified.\n");
227       usage();
228       exit(1);
229    }
230
231    jcr = setup_jcr("btape", margv[0], bsr, NULL);
232    dev = setup_to_access_device(jcr, 0);     /* acquire for write */
233    if (!dev) {
234       exit(1);
235    }
236    if (!open_the_device()) {
237       goto terminate;
238    }
239
240    Dmsg0(200, "Do tape commands\n");
241    do_tape_cmds();
242   
243 terminate:
244    terminate_btape(0);
245    return 0;
246 }
247
248 static void terminate_btape(int stat)
249 {
250
251    sm_check(__FILE__, __LINE__, False);
252    if (configfile) {
253       free(configfile);
254    }
255    free_config_resources();
256    if (args) {
257       free_pool_memory(args);
258       args = NULL;
259    }
260    if (cmd) {
261       free_pool_memory(cmd);
262       cmd = NULL;
263    }
264
265    if (dev) {
266       term_dev(dev);
267    }
268
269    if (debug_level > 10)
270       print_memory_pool_stats(); 
271
272    free_jcr(jcr);
273    jcr = NULL;
274
275    if (bsr) {
276       free_bsr(bsr);
277    }
278
279    if (this_block) {
280       free_block(this_block);
281    }
282
283    term_msg();
284    close_memory_pool();               /* free memory in pool */
285
286    sm_dump(False);
287    exit(stat);
288 }
289
290 static bool open_the_device()
291 {
292    DEV_BLOCK *block;
293    
294    block = new_block(dev);
295    lock_device(dev);
296    if (!(dev->state & ST_OPENED)) {
297       Dmsg1(200, "Opening device %s\n", jcr->VolumeName);
298       if (open_dev(dev, jcr->VolumeName, OPEN_READ_WRITE) < 0) {
299          Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
300          unlock_device(dev);
301          free_block(block);
302          return false;
303       }
304    }
305    Pmsg1(000, "open_dev %s OK\n", dev_name(dev));
306    unlock_device(dev);
307    free_block(block);
308    return true;
309 }
310
311
312 void quitcmd()
313 {
314    quit = 1;
315 }
316
317 /*
318  * Write a label to the tape   
319  */
320 static void labelcmd()
321 {
322    if (VolumeName) {
323       pm_strcpy(&cmd, VolumeName);
324    } else {
325       if (!get_cmd("Enter Volume Name: ")) {
326          return;
327       }
328    }
329          
330    if (!(dev->state & ST_OPENED)) {
331       if (!first_open_device(dev)) {
332          Pmsg1(0, "Device open failed. ERR=%s\n", strerror_dev(dev));
333       }
334    }
335    rewind_dev(dev);
336    write_volume_label_to_dev(jcr, jcr->device, cmd, "Default");
337    Pmsg1(-1, "Wrote Volume label for volume \"%s\".\n", cmd);
338 }
339
340 /*
341  * Read the tape label   
342  */
343 static void readlabelcmd()
344 {
345    int save_debug_level = debug_level;
346    int stat;
347    DEV_BLOCK *block;
348
349    block = new_block(dev);
350    stat = read_dev_volume_label(jcr, dev, block);
351    switch (stat) {
352       case VOL_NO_LABEL:
353          Pmsg0(0, "Volume has no label.\n");
354          break;
355       case VOL_OK:
356          Pmsg0(0, "Volume label read correctly.\n");
357          break;
358       case VOL_IO_ERROR:
359          Pmsg1(0, "I/O error on device: ERR=%s", strerror_dev(dev));
360          break;
361       case VOL_NAME_ERROR:
362          Pmsg0(0, "Volume name error\n");
363          break;
364       case VOL_CREATE_ERROR:
365          Pmsg1(0, "Error creating label. ERR=%s", strerror_dev(dev));
366          break;
367       case VOL_VERSION_ERROR:
368          Pmsg0(0, "Volume version error.\n");
369          break;
370       case VOL_LABEL_ERROR:
371          Pmsg0(0, "Bad Volume label type.\n");
372          break;
373       default:
374          Pmsg0(0, "Unknown error.\n");
375          break;
376    }
377
378    debug_level = 20;
379    dump_volume_label(dev); 
380    debug_level = save_debug_level;
381    free_block(block);
382 }
383
384
385 /*
386  * Load the tape should have prevously been taken
387  * off line, otherwise this command is not necessary.
388  */
389 static void loadcmd()
390 {
391
392    if (!load_dev(dev)) {
393       Pmsg1(0, "Bad status from load. ERR=%s\n", strerror_dev(dev));
394    } else
395       Pmsg1(0, "Loaded %s\n", dev_name(dev));
396 }
397
398 /*
399  * Rewind the tape.   
400  */
401 static void rewindcmd()
402 {
403    if (!rewind_dev(dev)) {
404       Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
405       clrerror_dev(dev, -1);
406    } else {
407       Pmsg1(0, "Rewound %s\n", dev_name(dev));
408    }
409 }
410
411 /*
412  * Clear any tape error   
413  */
414 static void clearcmd()
415 {
416    clrerror_dev(dev, -1);
417 }
418
419 /*
420  * Write and end of file on the tape   
421  */
422 static void weofcmd()
423 {
424    int stat;
425    int num = 1;
426    if (argc > 1) {
427       num = atoi(argk[1]);
428    }
429    if (num <= 0) {
430       num = 1;
431    }
432
433    if ((stat = weof_dev(dev, num)) < 0) {
434       Pmsg2(0, "Bad status from weof %d. ERR=%s\n", stat, strerror_dev(dev));
435       return;
436    } else {
437       Pmsg3(0, "Wrote %d EOF%s to %s\n", num, num==1?"":"s", dev_name(dev));
438    }
439 }
440
441
442 /* Go to the end of the medium -- raw command   
443  * The idea was orginally that the end of the Bacula
444  * medium would be flagged differently. This is not
445  * currently the case. So, this is identical to the
446  * eodcmd().
447  */
448 static void eomcmd()
449 {
450    if (!eod_dev(dev)) {
451       Pmsg1(0, "%s", strerror_dev(dev));
452       return;
453    } else {
454       Pmsg0(0, _("Moved to end of medium.\n"));
455    }
456 }
457
458 /*
459  * Go to the end of the medium (either hardware determined
460  *  or defined by two eofs.
461  */
462 static void eodcmd()
463 {
464    eomcmd();
465 }
466
467 /*
468  * Backspace file   
469  */
470 static void bsfcmd()
471 {
472    int num = 1;
473    if (argc > 1) {
474       num = atoi(argk[1]);
475    }
476    if (num <= 0) {
477       num = 1;
478    }
479
480    if (!bsf_dev(dev, num)) {
481       Pmsg1(0, _("Bad status from bsf. ERR=%s\n"), strerror_dev(dev));
482    } else {
483       Pmsg2(0, _("Backspaced %d file%s.\n"), num, num==1?"":"s");
484    }
485 }
486
487 /*
488  * Backspace record   
489  */
490 static void bsrcmd()
491 {
492    int num = 1;
493    if (argc > 1) {
494       num = atoi(argk[1]);
495    }
496    if (num <= 0) {
497       num = 1;
498    }
499    if (!bsr_dev(dev, num)) {
500       Pmsg1(0, _("Bad status from bsr. ERR=%s\n"), strerror_dev(dev));
501    } else {
502       Pmsg2(0, _("Backspaced %d record%s.\n"), num, num==1?"":"s");
503    }
504 }
505
506 /*
507  * List device capabilities as defined in the 
508  *  stored.conf file.
509  */
510 static void capcmd()
511 {
512    printf(_("Configured device capabilities:\n"));
513    printf("%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
514    printf("%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
515    printf("%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
516    printf("%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
517    printf("%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
518    printf("%sFASTFSF ", dev->capabilities & CAP_FASTFSF ? "" : "!");
519    printf("%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
520    printf("%sREM ", dev->capabilities & CAP_REM ? "" : "!");
521    printf("%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
522    printf("%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
523    printf("%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
524    printf("%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
525    printf("%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
526    printf("\n");
527
528    printf(_("Device status:\n"));
529    printf("%sOPENED ", dev->state & ST_OPENED ? "" : "!");
530    printf("%sTAPE ", dev->state & ST_TAPE ? "" : "!");
531    printf("%sLABEL ", dev->state & ST_LABEL ? "" : "!");
532    printf("%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
533    printf("%sAPPEND ", dev->state & ST_APPEND ? "" : "!");
534    printf("%sREAD ", dev->state & ST_READ ? "" : "!");
535    printf("%sEOT ", dev->state & ST_EOT ? "" : "!");
536    printf("%sWEOT ", dev->state & ST_WEOT ? "" : "!");
537    printf("%sEOF ", dev->state & ST_EOF ? "" : "!");
538    printf("%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
539    printf("%sSHORT ", dev->state & ST_SHORT ? "" : "!");
540    printf("\n");
541
542    printf(_("Device parameters:\n"));
543    printf("Device name: %s\n", dev->dev_name);
544    printf("File=%u block=%u\n", dev->file, dev->block_num);
545    printf("Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
546
547    printf("Status:\n");
548    statcmd();
549
550 }
551
552 /*
553  * Test writting larger and larger records.  
554  * This is a torture test for records.
555  */
556 static void rectestcmd()
557 {
558    DEV_BLOCK *block;
559    DEV_RECORD *rec;
560    int i, blkno = 0;
561
562    Pmsg0(0, "Test writting larger and larger records.\n"
563 "This is a torture test for records.\nI am going to write\n"
564 "larger and larger records. It will stop when the record size\n"
565 "plus the header exceeds the block size (by default about 64K)\n");
566
567
568    get_cmd("Do you want to continue? (y/n): ");
569    if (cmd[0] != 'y') {
570       Pmsg0(000, "Command aborted.\n");
571       return;
572    }
573
574    sm_check(__FILE__, __LINE__, False);
575    block = new_block(dev);
576    rec = new_record();
577
578    for (i=1; i<500000; i++) {
579       rec->data = check_pool_memory_size(rec->data, i);
580       memset(rec->data, i & 0xFF, i);
581       rec->data_len = i;
582       sm_check(__FILE__, __LINE__, False);
583       if (write_record_to_block(block, rec)) {
584          empty_block(block);
585          blkno++;
586          Pmsg2(0, "Block %d i=%d\n", blkno, i);
587       } else {
588          break;
589       }
590       sm_check(__FILE__, __LINE__, False);
591    }
592    free_record(rec);
593    free_block(block);
594    sm_check(__FILE__, __LINE__, False);
595 }
596
597 /*
598  * This test attempts to re-read a block written by Bacula
599  *   normally at the end of the tape. Bacula will then back up
600  *   over the two eof marks, backup over the record and reread
601  *   it to make sure it is valid.  Bacula can skip this validation
602  *   if you set "Backward space record = no"
603  */
604 static int re_read_block_test()
605 {
606    DEV_BLOCK *block;
607    DEV_RECORD *rec;
608    int stat = 0;
609    int len;
610
611    if (!(dev->capabilities & CAP_BSR)) {
612       Pmsg0(-1, _("Skipping read backwards test because BSR turned off.\n"));
613       return 0;
614    }
615
616    Pmsg0(-1, _("\n=== Write, backup, and re-read test ===\n\n"
617       "I'm going to write three records and an EOF\n"
618       "then backup over the EOF and re-read the last record.\n"     
619       "Bacula does this after writing the last block on the\n"
620       "tape to verify that the block was written correctly.\n\n"
621       "This is not an *essential* feature ...\n\n")); 
622    rewindcmd();
623    block = new_block(dev);
624    rec = new_record();
625    rec->data = check_pool_memory_size(rec->data, block->buf_len);
626    len = rec->data_len = block->buf_len-100;
627    memset(rec->data, 1, rec->data_len);
628    if (!write_record_to_block(block, rec)) {
629       Pmsg0(0, _("Error writing record to block.\n")); 
630       goto bail_out;
631    }
632    if (!write_block_to_dev(jcr->dcr, block)) {
633       Pmsg0(0, _("Error writing block to device.\n")); 
634       goto bail_out;
635    } else {
636       Pmsg1(0, _("Wrote first record of %d bytes.\n"), rec->data_len);
637    }
638    memset(rec->data, 2, rec->data_len);
639    if (!write_record_to_block(block, rec)) {
640       Pmsg0(0, _("Error writing record to block.\n")); 
641       goto bail_out;
642    }
643    if (!write_block_to_dev(jcr->dcr, block)) {
644       Pmsg0(0, _("Error writing block to device.\n")); 
645       goto bail_out;
646    } else {
647       Pmsg1(0, _("Wrote second record of %d bytes.\n"), rec->data_len);
648    }
649    memset(rec->data, 3, rec->data_len);
650    if (!write_record_to_block(block, rec)) {
651       Pmsg0(0, _("Error writing record to block.\n")); 
652       goto bail_out;
653    }
654    if (!write_block_to_dev(jcr->dcr, block)) {
655       Pmsg0(0, _("Error writing block to device.\n")); 
656       goto bail_out;
657    } else {
658       Pmsg1(0, _("Wrote third record of %d bytes.\n"), rec->data_len);
659    }
660    weofcmd();
661    if (dev_cap(dev, CAP_TWOEOF)) {
662       weofcmd();
663    }
664    if (!bsf_dev(dev, 1)) {
665       Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
666       goto bail_out;
667    }
668    if (dev_cap(dev, CAP_TWOEOF)) {
669       if (!bsf_dev(dev, 1)) {
670          Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
671          goto bail_out;
672       }
673    }
674    Pmsg0(0, "Backspaced over EOF OK.\n");
675    if (!bsr_dev(dev, 1)) {
676       Pmsg1(0, _("Backspace record failed! ERR=%s\n"), strerror_dev(dev));
677       goto bail_out;
678    }
679    Pmsg0(0, "Backspace record OK.\n");
680    if (!read_block_from_dev(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
681       Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
682       goto bail_out;
683    }
684    memset(rec->data, 0, rec->data_len);
685    if (!read_record_from_block(block, rec)) {
686       Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
687       goto bail_out;
688    }
689    for (int i=0; i<len; i++) {
690       if (rec->data[i] != 3) {
691          Pmsg0(0, _("Bad data in record. Test failed!\n"));
692          goto bail_out;
693       }
694    }
695    Pmsg0(0, _("\nBlock re-read correct. Test succeeded!\n"));
696    Pmsg0(-1, _("=== End Write, backup, and re-read test ===\n\n"));
697
698    stat = 1;
699
700 bail_out:
701    free_block(block);
702    free_record(rec);
703    if (stat == 0) {
704       Pmsg0(0, _("This is not terribly serious since Bacula only uses\n"
705                  "this function to verify the last block written to the\n"
706                  "tape. Bacula will skip the last block verification\n"
707                  "if you add:\n\n"
708                   "Backward Space Record = No\n\n"
709                   "to your Storage daemon's Device resource definition.\n"));
710    }   
711    return stat;
712 }
713
714
715 /*
716  * This test writes Bacula blocks to the tape in
717  *   several files. It then rewinds the tape and attepts
718  *   to read these blocks back checking the data.
719  */
720 static int write_read_test()
721 {
722    DEV_BLOCK *block;
723    DEV_RECORD *rec;
724    int stat = 0;
725    int len, i, j;
726    int *p;
727
728    Pmsg0(-1, _("\n=== Write, rewind, and re-read test ===\n\n"
729       "I'm going to write 1000 records and an EOF\n"
730       "then write 1000 records and an EOF, then rewind,\n"     
731       "and re-read the data to verify that it is correct.\n\n"
732       "This is an *essential* feature ...\n\n")); 
733    block = new_block(dev);
734    rec = new_record();
735    if (!rewind_dev(dev)) {
736       Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
737       goto bail_out;
738    }
739    rec->data = check_pool_memory_size(rec->data, block->buf_len);
740    rec->data_len = block->buf_len-100;
741    len = rec->data_len/sizeof(i);
742    for (i=1; i<=1000; i++) {
743       p = (int *)rec->data;
744       for (j=0; j<len; j++) {
745          *p++ = i;
746       }
747       if (!write_record_to_block(block, rec)) {
748          Pmsg0(0, _("Error writing record to block.\n")); 
749          goto bail_out;
750       }
751       if (!write_block_to_dev(jcr->dcr, block)) {
752          Pmsg0(0, _("Error writing block to device.\n")); 
753          goto bail_out;
754       }
755    }
756    Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
757    weofcmd();
758    for (i=1001; i<=2000; i++) {
759       p = (int *)rec->data;
760       for (j=0; j<len; j++) {
761          *p++ = i;
762       }
763       if (!write_record_to_block(block, rec)) {
764          Pmsg0(0, _("Error writing record to block.\n")); 
765          goto bail_out;
766       }
767       if (!write_block_to_dev(jcr->dcr, block)) {
768          Pmsg0(0, _("Error writing block to device.\n")); 
769          goto bail_out;
770       }
771    }
772    Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
773    weofcmd();
774    if (dev_cap(dev, CAP_TWOEOF)) {
775       weofcmd();
776    }
777    if (!rewind_dev(dev)) {
778       Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
779       goto bail_out;
780    } else {
781       Pmsg0(0, "Rewind OK.\n");
782    }
783    for (i=1; i<=2000; i++) {
784 read_again:
785       if (!read_block_from_dev(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
786          if (dev_state(dev, ST_EOF)) {
787             Pmsg0(-1, _("Got EOF on tape.\n"));
788             goto read_again;
789          }
790          Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
791          goto bail_out;
792       }
793       memset(rec->data, 0, rec->data_len);
794       if (!read_record_from_block(block, rec)) {
795          Pmsg1(0, _("Read record failed! ERR=%s\n"), strerror(dev->dev_errno));
796          goto bail_out;
797       }
798       p = (int *)rec->data;
799       for (j=0; j<len; j++) {
800          if (*p != i) {
801             Pmsg3(0, _("Bad data in record. Expected %d, got %d at byte %d. Test failed!\n"),
802                i, *p, j);
803             goto bail_out;
804          }
805          p++;
806       }
807       if (i == 1000 || i == 2000) {
808          Pmsg0(-1, _("1000 blocks re-read correctly.\n"));
809       }
810    }
811    Pmsg0(-1, _("=== Test Succeeded. End Write, rewind, and re-read test ===\n\n"));
812    stat = 1;
813
814 bail_out:
815    free_block(block);
816    free_record(rec);
817    return stat;
818 }
819
820 /*
821  * This test writes Bacula blocks to the tape in
822  *   several files. It then rewinds the tape and attepts
823  *   to read these blocks back checking the data.
824  */
825 static int position_test()
826 {
827    DEV_BLOCK *block;
828    DEV_RECORD *rec;
829    int stat = 0;
830    int len, i, j;
831    bool ok = true;
832    int recno = 0;
833    int file = 0, blk = 0;
834    int *p;
835
836    Pmsg0(-1, _("\n=== Write, rewind, and position test ===\n\n"
837       "I'm going to write 1000 records and an EOF\n"
838       "then write 1000 records and an EOF, then rewind,\n"     
839       "and position to a few blocks and verify that it is correct.\n\n"
840       "This is an *essential* feature ...\n\n")); 
841    block = new_block(dev);
842    rec = new_record();
843    if (!rewind_dev(dev)) {
844       Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
845       goto bail_out;
846    }
847    rec->data = check_pool_memory_size(rec->data, block->buf_len);
848    rec->data_len = block->buf_len-100;
849    len = rec->data_len/sizeof(i);
850    for (i=1; i<=1000; i++) {
851       p = (int *)rec->data;
852       for (j=0; j<len; j++) {
853          *p++ = i;
854       }
855       if (!write_record_to_block(block, rec)) {
856          Pmsg0(0, _("Error writing record to block.\n")); 
857          goto bail_out;
858       }
859       if (!write_block_to_dev(jcr->dcr, block)) {
860          Pmsg0(0, _("Error writing block to device.\n")); 
861          goto bail_out;
862       }
863    }
864    Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
865    weofcmd();
866    for (i=1001; i<=2000; i++) {
867       p = (int *)rec->data;
868       for (j=0; j<len; j++) {
869          *p++ = i;
870       }
871       if (!write_record_to_block(block, rec)) {
872          Pmsg0(0, _("Error writing record to block.\n")); 
873          goto bail_out;
874       }
875       if (!write_block_to_dev(jcr->dcr, block)) {
876          Pmsg0(0, _("Error writing block to device.\n")); 
877          goto bail_out;
878       }
879    }
880    Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
881    weofcmd();
882    if (dev_cap(dev, CAP_TWOEOF)) {
883       weofcmd();
884    }
885    if (!rewind_dev(dev)) {
886       Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
887       goto bail_out;
888    } else {
889       Pmsg0(0, "Rewind OK.\n");
890    }
891
892    while(ok) {
893       /* Set up next item to read based on where we are */
894       switch (recno) {
895       case 0:
896          recno = 5;
897          file = 0;
898          blk = 4;
899          break;
900       case 5:
901          recno = 201;
902          file = 0;
903          blk = 200;
904          break;
905       case 201:
906          recno = 1000;
907          file = 0;
908          blk = 999;
909          break;
910       case 1000:
911          recno = 1001;
912          file = 1;
913          blk = 0;
914          break;
915       case 1001:
916          recno = 1601;
917          file = 1;
918          blk = 600;
919          break;
920       case 1601:
921          recno = 2000;
922          file = 1;
923          blk = 999;
924          break;
925       case 2000:
926          ok = false;
927          continue;
928       }
929       Pmsg2(-1, "Reposition to file:block %d:%d\n", file, blk);
930       if (!reposition_dev(dev, file, blk)) {
931          Pmsg0(0, "Reposition error.\n");
932          goto bail_out;
933       }
934 read_again:
935       if (!read_block_from_dev(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
936          if (dev_state(dev, ST_EOF)) {
937             Pmsg0(-1, _("Got EOF on tape.\n"));
938             goto read_again;
939          }
940          Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
941          goto bail_out;
942       }
943       memset(rec->data, 0, rec->data_len);
944       if (!read_record_from_block(block, rec)) {
945          Pmsg1(0, _("Read record failed! ERR=%s\n"), strerror(dev->dev_errno));
946          goto bail_out;
947       }
948       p = (int *)rec->data;
949       for (j=0; j<len; j++) {
950          if (p[j] != recno) {
951             Pmsg3(0, _("Bad data in record. Expected %d, got %d at byte %d. Test failed!\n"),
952                recno, p[j], j);
953             goto bail_out;
954          }
955       }
956       Pmsg0(-1, _("Block re-read correctly.\n"));
957    }
958    Pmsg0(-1, _("=== Test Succeeded. End Write, rewind, and re-read test ===\n\n"));
959    stat = 1;
960
961 bail_out:
962    free_block(block);
963    free_record(rec);
964    return stat;
965 }
966
967
968
969
970 /*
971  * This test writes some records, then writes an end of file,
972  *   rewinds the tape, moves to the end of the data and attepts
973  *   to append to the tape.  This function is essential for
974  *   Bacula to be able to write multiple jobs to the tape.
975  */
976 static int append_test()
977 {
978    Pmsg0(-1, _("\n\n=== Append files test ===\n\n"
979                "This test is essential to Bacula.\n\n"
980 "I'm going to write one record  in file 0,\n"
981 "                   two records in file 1,\n"
982 "             and three records in file 2\n\n"));
983    argc = 1;
984    rewindcmd();
985    wrcmd();
986    weofcmd();      /* end file 0 */
987    wrcmd();
988    wrcmd();
989    weofcmd();      /* end file 1 */
990    wrcmd();
991    wrcmd();
992    wrcmd();
993    weofcmd();     /* end file 2 */
994    if (dev_cap(dev, CAP_TWOEOF)) {
995       weofcmd();
996    }
997    rewindcmd();
998    Pmsg0(0, _("Now moving to end of medium.\n"));
999    eodcmd();
1000    Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"), 
1001       dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
1002
1003    if (dev->file != 3) {
1004       return -1;
1005    }
1006
1007    Pmsg0(-1, _("\nNow the important part, I am going to attempt to append to the tape.\n\n"));
1008    wrcmd(); 
1009    weofcmd();
1010    if (dev_cap(dev, CAP_TWOEOF)) {
1011       weofcmd();
1012    }
1013    rewindcmd();
1014    Pmsg0(-1, _("Done appending, there should be no I/O errors\n\n"));
1015    Pmsg0(-1, "Doing Bacula scan of blocks:\n");
1016    scan_blocks();
1017    Pmsg0(-1, _("End scanning the tape.\n"));
1018    Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"), 
1019       dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
1020
1021    if (dev->file != 4) {
1022       return -2;
1023    }
1024    return 1;
1025 }
1026
1027
1028 /*
1029  * This test exercises the autochanger
1030  */
1031 static int autochanger_test()
1032 {
1033    POOLMEM *results, *changer;
1034    int slot, status, loaded;
1035    int timeout = 120;
1036    int sleep_time = 0;
1037
1038    if (!dev_cap(dev, CAP_AUTOCHANGER)) {
1039       return 1;
1040    }
1041    if (!(jcr->device && jcr->device->changer_name && jcr->device->changer_command)) {
1042       Pmsg0(-1, "\nAutochanger enabled, but no name or no command device specified.\n");
1043       return 1;
1044    }
1045
1046    Pmsg0(-1, "\nAh, I see you have an autochanger configured.\n"
1047              "To test the autochanger you must have a blank tape\n"
1048              " that I can write on in Slot 1.\n");
1049    if (!get_cmd("\nDo you wish to continue with the Autochanger test? (y/n): ")) {
1050       return 0;
1051    }
1052    if (cmd[0] != 'y' && cmd[0] != 'Y') {
1053       return 0;
1054    }
1055
1056    Pmsg0(-1, _("\n\n=== Autochanger test ===\n\n"));
1057
1058    results = get_pool_memory(PM_MESSAGE);
1059    changer = get_pool_memory(PM_FNAME);
1060
1061 try_again:
1062    slot = 1;
1063    jcr->VolCatInfo.Slot = slot;
1064    /* Find out what is loaded, zero means device is unloaded */
1065    Pmsg0(-1, _("3301 Issuing autochanger \"loaded\" command.\n"));
1066    changer = edit_device_codes(jcr, changer, jcr->device->changer_command, 
1067                 "loaded");
1068    status = run_program(changer, timeout, results);
1069    Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
1070    if (status == 0) {
1071       loaded = atoi(results);
1072    } else {
1073       Pmsg1(-1, _("3991 Bad autochanger \"load slot\" status=%d.\n"), status);
1074       loaded = -1;              /* force unload */
1075       goto bail_out;
1076    }
1077    if (loaded) {
1078       Pmsg1(-1, "Slot %d loaded. I am going to unload it.\n", loaded);
1079    } else {
1080       Pmsg0(-1, "Nothing loaded into the drive. OK.\n");
1081    }
1082    Dmsg1(100, "Results from loaded query=%s\n", results);
1083    if (loaded) {
1084       offline_or_rewind_dev(dev);
1085       /* We are going to load a new tape, so close the device */
1086       force_close_dev(dev);
1087       Pmsg0(-1, _("3302 Issuing autochanger \"unload\" command.\n"));
1088       changer = edit_device_codes(jcr, changer, 
1089                      jcr->device->changer_command, "unload");
1090       status = run_program(changer, timeout, NULL);
1091       Pmsg2(-1, "unload status=%s %d\n", status==0?"OK":"Bad", status);
1092    }
1093
1094    /*
1095     * Load the Slot 1
1096     */
1097    Pmsg1(-1, _("3303 Issuing autochanger \"load slot %d\" command.\n"), slot);
1098    changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "load");
1099    Dmsg1(200, "Changer=%s\n", changer);
1100    status = run_program(changer, timeout, NULL);
1101    if (status == 0) {
1102       Pmsg1(-1,  _("3304 Autochanger \"load slot %d\" status is OK.\n"), slot);
1103    } else {
1104       Pmsg1(-1,  _("3992 Bad autochanger \"load slot\" status=%d.\n"), status);
1105       goto bail_out;
1106    }
1107
1108    if (!open_the_device()) {
1109       goto bail_out;
1110    }
1111    bmicrosleep(sleep_time, 0);
1112    if (!rewind_dev(dev)) {
1113       Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
1114       clrerror_dev(dev, -1);
1115       Pmsg0(-1, "\nThe test failed, probably because you need to put\n"
1116                 "a longer sleep time in the mtx-script in the load) case.\n" 
1117                 "Adding a 30 second sleep and trying again ...\n");
1118       sleep_time += 30;
1119       goto try_again;
1120    } else {
1121       Pmsg1(0, "Rewound %s\n", dev_name(dev));
1122    }
1123       
1124    if ((status = weof_dev(dev, 1)) < 0) {
1125       Pmsg2(0, "Bad status from weof %d. ERR=%s\n", status, strerror_dev(dev));
1126       goto bail_out;
1127    } else {
1128       Pmsg1(0, "Wrote EOF to %s\n", dev_name(dev));
1129    }
1130
1131    if (sleep_time) {
1132       Pmsg1(-1, "\nThe test worked this time. Please add:\n\n"
1133                 "   sleep %d\n\n"
1134                 "to your mtx-changer script in the load) case.\n\n",
1135                 sleep_time);
1136    } else {
1137       Pmsg0(-1, "\nThe test autochanger worked!!\n\n");
1138    }
1139
1140    free_pool_memory(changer);
1141    free_pool_memory(results);
1142    return 1;
1143
1144
1145 bail_out:
1146    free_pool_memory(changer);
1147    free_pool_memory(results);
1148    Pmsg0(-1, "You must correct this error or the Autochanger will not work.\n");
1149    return -2;
1150 }
1151
1152 static void autochangercmd()
1153 {
1154    autochanger_test();
1155 }
1156
1157
1158 /*
1159  * This test assumes that the append test has been done,
1160  *   then it tests the fsf function.
1161  */
1162 static int fsf_test()
1163 {
1164    bool set_off = false;
1165    
1166    Pmsg0(-1, _("\n\n=== Forward space files test ===\n\n"
1167                "This test is essential to Bacula.\n\n"
1168                "I'm going to write five files then test forward spacing\n\n"));
1169    argc = 1;
1170    rewindcmd();
1171    wrcmd();
1172    weofcmd();      /* end file 0 */
1173    wrcmd();
1174    wrcmd();
1175    weofcmd();      /* end file 1 */
1176    wrcmd();
1177    wrcmd();
1178    wrcmd();
1179    weofcmd();     /* end file 2 */
1180    wrcmd();
1181    wrcmd();
1182    weofcmd();     /* end file 3 */
1183    wrcmd();
1184    weofcmd();     /* end file 4 */
1185    if (dev_cap(dev, CAP_TWOEOF)) {
1186       weofcmd();
1187    }
1188
1189 test_again:
1190    rewindcmd();
1191    Pmsg0(0, _("Now forward spacing 1 file.\n"));
1192    if (!fsf_dev(dev, 1)) {
1193       Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1194       goto bail_out;
1195    }
1196    Pmsg2(-1, _("We should be in file 1. I am at file %d. This is %s\n"), 
1197       dev->file, dev->file == 1 ? "correct!" : "NOT correct!!!!");
1198
1199    if (dev->file != 1) {
1200       goto bail_out;
1201    }
1202
1203    Pmsg0(0, _("Now forward spacing 2 files.\n"));
1204    if (!fsf_dev(dev, 2)) {
1205       Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1206       goto bail_out;
1207    }
1208    Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"), 
1209       dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
1210
1211    if (dev->file != 3) {
1212       goto bail_out;
1213    }
1214
1215    rewindcmd();
1216    Pmsg0(0, _("Now forward spacing 4 files.\n"));
1217    if (!fsf_dev(dev, 4)) {
1218       Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1219       goto bail_out;
1220    }
1221    Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"), 
1222       dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
1223
1224    if (dev->file != 4) {
1225       goto bail_out;
1226    }
1227    if (set_off) {
1228       Pmsg0(-1, "The test worked this time. Please add:\n\n"
1229                 "   Fast Forward Space File = no\n\n"
1230                 "to your Device resource for this drive.\n");
1231    }
1232
1233    Pmsg0(-1, "\n");
1234    Pmsg0(0, _("Now forward spacing 1 more file.\n"));
1235    if (!fsf_dev(dev, 1)) {
1236       Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1237    }
1238    Pmsg2(-1, _("We should be in file 5. I am at file %d. This is %s\n"), 
1239       dev->file, dev->file == 5 ? "correct!" : "NOT correct!!!!");
1240    if (dev->file != 5) {
1241       goto bail_out;
1242    }
1243    Pmsg0(-1, _("\n=== End Forward space files test ===\n\n"));
1244    return 1;
1245
1246 bail_out:
1247    Pmsg0(-1, _("\nThe forward space file test failed.\n"));
1248    if (dev_cap(dev, CAP_FASTFSF)) {
1249       Pmsg0(-1, "You have Fast Forward Space File enabled.\n"
1250               "I am turning it off then retrying the test.\n");
1251       dev->capabilities &= ~CAP_FASTFSF;
1252       set_off = true;
1253       goto test_again;
1254    }
1255    Pmsg0(-1, "You must correct this error or Bacula will not work.\n");
1256    return -2;
1257 }
1258
1259
1260
1261
1262
1263 /* 
1264  * This is a general test of Bacula's functions
1265  *   needed to read and write the tape.
1266  */
1267 static void testcmd()
1268 {
1269    int stat;
1270
1271    if (!write_read_test()) {
1272       return;
1273    }
1274    if (!position_test()) {
1275       return;
1276    }
1277
1278    stat = append_test();
1279    if (stat == 1) {                   /* OK get out */
1280       goto all_done;
1281    }
1282    if (stat == -1) {                  /* first test failed */
1283       if (dev_cap(dev, CAP_EOM) || dev_cap(dev, CAP_FASTFSF)) {
1284          Pmsg0(-1, "\nAppend test failed. Attempting again.\n"
1285                    "Setting \"Hardware End of Medium = no\n"
1286                    "    and \"Fast Forward Space File = no\n"
1287                    "and retrying append test.\n\n");
1288          dev->capabilities &= ~CAP_EOM; /* turn off eom */
1289          dev->capabilities &= ~CAP_FASTFSF; /* turn off fast fsf */
1290          stat = append_test();
1291          if (stat == 1) {
1292             Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
1293                      "    Hardware End of Medium = No\n\n"
1294                      "    Fast Forward Space File = No\n"
1295                      "to your Device resource in the Storage conf file.\n");
1296             goto all_done;
1297          }
1298          if (stat == -1) {
1299             Pmsg0(-1, "\n\nThat appears *NOT* to have corrected the problem.\n");
1300             goto failed;
1301          }
1302          /* Wrong count after append */
1303          if (stat == -2) {
1304             Pmsg0(-1, "\n\nIt looks like the append failed. Attempting again.\n"
1305                      "Setting \"BSF at EOM = yes\" and retrying append test.\n");
1306             dev->capabilities |= CAP_BSFATEOM; /* backspace on eom */
1307             stat = append_test();
1308             if (stat == 1) {
1309                Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
1310                      "    Hardware End of Medium = No\n"
1311                      "    Fast Forward Space File = No\n"
1312                      "    BSF at EOM = yes\n\n"
1313                      "to your Device resource in the Storage conf file.\n");
1314                goto all_done;
1315             }
1316          }
1317
1318       }
1319 failed:
1320       Pmsg0(-1, "\nAppend test failed.\n\n");
1321       Pmsg0(-1, "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
1322             "Unable to correct the problem. You MUST fix this\n"
1323              "problem before Bacula can use your tape drive correctly\n");
1324       Pmsg0(-1, "\nPerhaps running Bacula in fixed block mode will work.\n"
1325             "Do so by setting:\n\n"
1326             "Minimum Block Size = nnn\n"
1327             "Maximum Block Size = nnn\n\n"
1328             "in your Storage daemon's Device definition.\n"
1329             "nnn must match your tape driver's block size, which\n"
1330             "can be determined by reading your tape manufacturers\n"
1331             "information, and the information on your kernel dirver.\n"
1332             "Fixed block sizes, however, are not normally an ideal solution.\n");
1333        return;
1334    }
1335
1336 all_done:
1337    Pmsg0(-1, _("\nThe above Bacula scan should have output identical to what follows.\n"
1338         "Please double check it ...\n"
1339         "=== Sample correct output ===\n"
1340         "1 block of 64448 bytes in file 1\n"
1341         "End of File mark.\n"
1342         "2 blocks of 64448 bytes in file 2\n"
1343         "End of File mark.\n"
1344         "3 blocks of 64448 bytes in file 3\n"
1345         "End of File mark.\n"
1346         "1 block of 64448 bytes in file 4\n" 
1347         "End of File mark.\n"
1348         "Total files=4, blocks=7, bytes = 451,136\n"
1349         "=== End sample correct output ===\n\n"));
1350
1351    Pmsg0(-1, _("If the above scan output is not identical to the\n"
1352                "sample output, you MUST correct the problem\n"
1353                "or Bacula will not be able to write multiple Jobs to \n"
1354                "the tape.\n\n"));
1355
1356    if (stat == 1) {
1357       re_read_block_test();
1358    }
1359
1360    fsf_test();                        /* do fast forward space file test */
1361
1362    autochanger_test();                /* do autochanger test */
1363    
1364 }
1365
1366 /* Forward space a file */
1367 static void fsfcmd()
1368 {
1369    int num = 1;
1370    if (argc > 1) {
1371       num = atoi(argk[1]);
1372    }
1373    if (num <= 0) {
1374       num = 1;
1375    }
1376    if (!fsf_dev(dev, num)) {
1377       Pmsg1(0, "Bad status from fsf. ERR=%s\n", strerror_dev(dev));
1378       return;
1379    }
1380    Pmsg2(0, "Forward spaced %d file%s.\n", num, num==1?"":"s");
1381 }
1382
1383 /* Forward space a record */
1384 static void fsrcmd()
1385 {
1386    int num = 1;
1387    if (argc > 1) {
1388       num = atoi(argk[1]);
1389    }
1390    if (num <= 0) {
1391       num = 1;
1392    }
1393    if (!fsr_dev(dev, num)) {
1394       Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1395       return;
1396    }
1397    Pmsg2(0, "Forward spaced %d record%s.\n", num, num==1?"":"s");
1398 }
1399
1400
1401 /*
1402  * Write a Bacula block to the tape
1403  */
1404 static void wrcmd()
1405 {
1406    DEV_BLOCK *block;
1407    DEV_RECORD *rec;
1408    int i;
1409
1410    sm_check(__FILE__, __LINE__, False);
1411    block = new_block(dev);
1412    rec = new_record();
1413    dump_block(block, "test");
1414
1415    i = block->buf_len - 100;
1416    ASSERT (i > 0);
1417    rec->data = check_pool_memory_size(rec->data, i);
1418    memset(rec->data, i & 0xFF, i);
1419    rec->data_len = i;
1420    sm_check(__FILE__, __LINE__, False);
1421    if (!write_record_to_block(block, rec)) {
1422       Pmsg0(0, _("Error writing record to block.\n")); 
1423       goto bail_out;
1424    }
1425    if (!write_block_to_dev(jcr->dcr, block)) {
1426       Pmsg0(0, _("Error writing block to device.\n")); 
1427       goto bail_out;
1428    } else {
1429       Pmsg1(0, _("Wrote one record of %d bytes.\n"), i);
1430    }
1431    Pmsg0(0, _("Wrote block to device.\n"));
1432
1433 bail_out:
1434    sm_check(__FILE__, __LINE__, False);
1435    free_record(rec);
1436    free_block(block);
1437    sm_check(__FILE__, __LINE__, False);
1438 }
1439
1440 /* 
1441  * Read a record from the tape
1442  */
1443 static void rrcmd()
1444 {
1445    char *buf;
1446    int stat, len;
1447
1448    if (!get_cmd("Enter length to read: ")) {
1449       return;
1450    }
1451    len = atoi(cmd);
1452    if (len < 0 || len > 1000000) {
1453       Pmsg0(0, _("Bad length entered, using default of 1024 bytes.\n"));
1454       len = 1024;
1455    }
1456    buf = (char *)malloc(len);
1457    stat = read(dev->fd, buf, len);
1458    if (stat > 0 && stat <= len) {
1459       errno = 0;
1460    }
1461    Pmsg3(0, _("Read of %d bytes gives stat=%d. ERR=%s\n"),
1462       len, stat, strerror(errno));
1463    free(buf);
1464 }
1465
1466
1467 /*
1468  * Scan tape by reading block by block. Report what is
1469  * on the tape.  Note, this command does raw reads, and as such
1470  * will not work with fixed block size devices.
1471  */
1472 static void scancmd()
1473 {
1474    int stat;
1475    int blocks, tot_blocks, tot_files;
1476    int block_size;
1477    uint64_t bytes;
1478    char ec1[50];
1479
1480
1481    blocks = block_size = tot_blocks = 0;
1482    bytes = 0;
1483    if (dev->state & ST_EOT) {
1484       Pmsg0(0, "End of tape\n");
1485       return; 
1486    }
1487    update_pos_dev(dev);
1488    tot_files = dev->file;
1489    Pmsg1(0, _("Starting scan at file %u\n"), dev->file);
1490    for (;;) {
1491       if ((stat = read(dev->fd, buf, sizeof(buf))) < 0) {
1492          clrerror_dev(dev, -1);
1493          Mmsg2(&dev->errmsg, "read error on %s. ERR=%s.\n",
1494             dev->dev_name, strerror(dev->dev_errno));
1495          Pmsg2(0, "Bad status from read %d. ERR=%s\n", stat, strerror_dev(dev));
1496          if (blocks > 0)
1497             printf("%d block%s of %d bytes in file %d\n",        
1498                     blocks, blocks>1?"s":"", block_size, dev->file);
1499          return;
1500       }
1501       Dmsg1(200, "read status = %d\n", stat);
1502 /*    sleep(1); */
1503       if (stat != block_size) {
1504          update_pos_dev(dev);
1505          if (blocks > 0) {
1506             printf("%d block%s of %d bytes in file %d\n", 
1507                  blocks, blocks>1?"s":"", block_size, dev->file);
1508             blocks = 0;
1509          }
1510          block_size = stat;
1511       }
1512       if (stat == 0) {                /* EOF */
1513          update_pos_dev(dev);
1514          printf("End of File mark.\n");
1515          /* Two reads of zero means end of tape */
1516          if (dev->state & ST_EOF)
1517             dev->state |= ST_EOT;
1518          else {
1519             dev->state |= ST_EOF;
1520             dev->file++;
1521          }
1522          if (dev->state & ST_EOT) {
1523             printf("End of tape\n");
1524             break;
1525          }
1526       } else {                        /* Got data */
1527          dev->state &= ~ST_EOF;
1528          blocks++;
1529          tot_blocks++;
1530          bytes += stat;
1531       }
1532    }
1533    update_pos_dev(dev);
1534    tot_files = dev->file - tot_files;
1535    printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks, 
1536       edit_uint64_with_commas(bytes, ec1));
1537 }
1538
1539
1540 /*
1541  * Scan tape by reading Bacula block by block. Report what is
1542  * on the tape.  This function reads Bacula blocks, so if your
1543  * Device resource is correctly defined, it should work with
1544  * either variable or fixed block sizes.
1545  */
1546 static void scan_blocks()
1547 {
1548    int blocks, tot_blocks, tot_files;
1549    uint32_t block_size;
1550    uint64_t bytes;
1551    DEV_BLOCK *block;
1552    char ec1[50];
1553
1554    block = new_block(dev);
1555    blocks = block_size = tot_blocks = 0;
1556    bytes = 0;
1557
1558    update_pos_dev(dev);
1559    tot_files = dev->file;
1560    for (;;) {
1561       if (!read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
1562          Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
1563          if (dev->state & ST_EOT) {
1564             if (blocks > 0) {
1565                printf("%d block%s of %d bytes in file %d\n", 
1566                     blocks, blocks>1?"s":"", block_size, dev->file);
1567                blocks = 0;
1568             }
1569             goto bail_out;
1570          }
1571          if (dev->state & ST_EOF) {
1572             if (blocks > 0) {
1573                printf("%d block%s of %d bytes in file %d\n",        
1574                        blocks, blocks>1?"s":"", block_size, dev->file);
1575                blocks = 0;
1576             }
1577             printf(_("End of File mark.\n"));
1578             continue;
1579          }
1580          if (dev->state & ST_SHORT) {
1581             if (blocks > 0) {
1582                printf("%d block%s of %d bytes in file %d\n",        
1583                        blocks, blocks>1?"s":"", block_size, dev->file);
1584                blocks = 0;
1585             }
1586             printf(_("Short block read.\n"));
1587             continue;
1588          }
1589          printf(_("Error reading block. ERR=%s\n"), strerror_dev(dev));
1590          goto bail_out;
1591       }
1592       if (block->block_len != block_size) {
1593          if (blocks > 0) {
1594             printf("%d block%s of %d bytes in file %d\n",        
1595                     blocks, blocks>1?"s":"", block_size, dev->file);
1596             blocks = 0;
1597          }
1598          block_size = block->block_len;
1599       }
1600       blocks++;
1601       tot_blocks++;
1602       bytes += block->block_len;
1603       Dmsg6(100, "Blk_blk=%u dev_blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
1604          block->BlockNumber, dev->block_num, block->block_len, block->BlockVer,
1605          block->VolSessionId, block->VolSessionTime);
1606       if (verbose == 1) {
1607          DEV_RECORD *rec = new_record();
1608          read_record_from_block(block, rec);
1609          Pmsg8(-1, "Blk_block: %u dev_blk=%u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
1610               block->BlockNumber, dev->block_num, block->block_len,
1611               FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
1612               stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
1613          rec->remainder = 0;
1614          free_record(rec);
1615       } else if (verbose > 1) {
1616          dump_block(block, "");
1617       }
1618
1619    }
1620 bail_out:
1621    free_block(block);
1622    tot_files = dev->file - tot_files;
1623    printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks, 
1624       edit_uint64_with_commas(bytes, ec1));
1625 }
1626
1627
1628 static void statcmd()
1629 {
1630    int debug = debug_level;
1631    debug_level = 30;
1632    Pmsg2(0, "Device status: %u. ERR=%s\n", status_dev(dev), strerror_dev(dev));
1633 #ifdef xxxx
1634    dump_volume_label(dev);
1635 #endif
1636    debug_level = debug;
1637 }
1638
1639
1640 /* 
1641  * First we label the tape, then we fill
1642  *  it with data get a new tape and write a few blocks.
1643  */                            
1644 static void fillcmd()
1645 {
1646    DEV_RECORD rec;
1647    DEV_BLOCK  *block;
1648    char ec1[50];
1649    int fd;
1650    uint32_t i;
1651    uint32_t min_block_size;
1652
1653    ok = true;
1654    stop = 0;
1655    vol_num = 0;
1656    last_file = 0;
1657    last_block_num = 0;
1658    BlockNumber = 0;
1659
1660    Pmsg0(-1, "\n\
1661 This command simulates Bacula writing to a tape.\n\
1662 It requires either one or two blank tapes, which it\n\
1663 will label and write.\n\n\
1664 If you have an autochanger configured, it will use\n\
1665 the tapes that are in slots 1 and 2, otherwise, you will\n\
1666 be prompted to insert the tapes when necessary.\n\n\
1667 It will print a status approximately\n\
1668 every 322 MB, and write an EOF every 3.2 GB.  If you have\n\
1669 selected the simple test option, after writing the first tape\n\
1670 it will rewind it and re-read the last block written.\n\n\
1671 If you have selected the multiple tape test, when the first tape\n\
1672 fills, it will ask for a second, and after writing a few more \n\
1673 blocks, it will stop.  Then it will begin re-reading the\n\
1674 two tapes.\n\n\
1675 This may take a long time -- hours! ...\n\n");
1676
1677    get_cmd("Do you want to run the simplified test (s) with one tape\n"
1678            "or the complete multiple tape (m) test: (s/m) ");
1679    if (cmd[0] == 's') {
1680       Pmsg0(-1, "Simple test (single tape) selected.\n");
1681       simple = true;
1682    } else if (cmd[0] == 'm') {
1683       Pmsg0(-1, "Multiple tape test selected.\n"); 
1684       simple = false;
1685    } else {
1686       Pmsg0(000, "Command aborted.\n");
1687       return;
1688    }
1689
1690    Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
1691    Dmsg1(20, "MaxVolSize=%s\n", edit_uint64(dev->max_volume_size, ec1));
1692
1693    /* Use fixed block size to simplify read back */
1694    min_block_size = dev->min_block_size;
1695    dev->min_block_size = dev->max_block_size;
1696
1697    /* 
1698     * Acquire output device for writing.  Note, after acquiring a
1699     *   device, we MUST release it, which is done at the end of this
1700     *   subroutine.
1701     */
1702    Dmsg0(100, "just before acquire_device\n");
1703    if (!acquire_device_for_append(jcr)) {
1704       set_jcr_job_status(jcr, JS_ErrorTerminated);
1705       return;
1706    }
1707    block = jcr->dcr->block;
1708
1709    Dmsg0(100, "Just after acquire_device_for_append\n");
1710    /*
1711     * Write Begin Session Record
1712     */
1713    if (!write_session_label(jcr, block, SOS_LABEL)) {
1714       set_jcr_job_status(jcr, JS_ErrorTerminated);
1715       Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
1716          strerror_dev(dev));
1717       ok = false;
1718    }
1719    Pmsg0(-1, "Wrote Start Of Session label.\n");
1720
1721    memset(&rec, 0, sizeof(rec));
1722    rec.data = get_memory(100000);     /* max record size */
1723
1724 #define REC_SIZE 32768
1725    rec.data_len = REC_SIZE;
1726
1727    /* 
1728     * Put some random data in the record
1729     */
1730    fd = open("/dev/urandom", O_RDONLY);
1731    if (fd) {
1732       read(fd, rec.data, rec.data_len);
1733       close(fd);
1734    } else {
1735       uint32_t *p = (uint32_t *)rec.data;
1736       srandom(time(NULL));
1737       for (i=0; i<rec.data_len/sizeof(uint32_t); i++) {
1738          p[i] = random();
1739       }
1740    }
1741
1742    /* 
1743     * Generate data as if from File daemon, write to device   
1744     */
1745    jcr->dcr->VolFirstIndex = 0;
1746    time(&jcr->run_time);              /* start counting time for rates */
1747    if (simple) {
1748       Pmsg0(-1, "Begin writing Bacula records to tape ...\n");
1749    } else {
1750       Pmsg0(-1, "Begin writing Bacula records to first tape ...\n");
1751    }
1752    for (file_index = 0; ok && !job_canceled(jcr); ) {
1753       rec.VolSessionId = jcr->VolSessionId;
1754       rec.VolSessionTime = jcr->VolSessionTime;
1755       rec.FileIndex = ++file_index;
1756       rec.Stream = STREAM_FILE_DATA;
1757
1758       /* Mix up the data just a bit */
1759       uint32_t *lp = (uint32_t *)rec.data;
1760       lp[0] += lp[13];
1761       for (i=1; i < (rec.data_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) {
1762          lp[i] += lp[i-1];
1763       }
1764
1765       Dmsg4(250, "before write_rec FI=%d SessId=%d Strm=%s len=%d\n",
1766          rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream, rec.FileIndex), 
1767          rec.data_len);
1768        
1769       while (!write_record_to_block(block, &rec)) {
1770          /*
1771           * When we get here we have just filled a block
1772           */
1773          Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
1774                     rec.remainder);
1775
1776          /* Write block to tape */
1777          if (!flush_block(block, 1)) {
1778             break;
1779          }
1780
1781          /* Every 5000 blocks (approx 322MB) report where we are.
1782           */
1783          if ((block->BlockNumber % 5000) == 0) {
1784             now = time(NULL);
1785             now -= jcr->run_time;
1786             if (now <= 0) {
1787                now = 1;          /* prevent divide error */
1788             }
1789             kbs = (double)dev->VolCatInfo.VolCatBytes / (1000.0 * (double)now);
1790             Pmsg4(-1, "Wrote blk_block=%u, dev_blk_num=%u VolBytes=%s rate=%.1f KB/s\n", 
1791                block->BlockNumber, dev->block_num,
1792                edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), (float)kbs);
1793          }
1794          /* Every 15000 blocks (approx 1GB) write an EOF.
1795           */
1796          if ((block->BlockNumber % 15000) == 0) {
1797             Pmsg0(-1, "Flush block, write EOF\n");
1798             flush_block(block, 0);
1799             weof_dev(dev, 1);
1800          }
1801
1802          /* Get out after writing 10 blocks to the second tape */
1803          if (++BlockNumber > 10 && stop != 0) {      /* get out */
1804             break;    
1805          }
1806       }
1807       if (!ok) {
1808          Pmsg0(000, _("Not OK\n"));
1809          break;
1810       }
1811       jcr->JobBytes += rec.data_len;   /* increment bytes this job */
1812       Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
1813          FI_to_ascii(rec.FileIndex), rec.VolSessionId, 
1814          stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
1815
1816       /* Get out after writing 10 blocks to the second tape */
1817       if (BlockNumber > 10 && stop != 0) {      /* get out */
1818          Pmsg0(-1, "Done writing ...\n");
1819          break;    
1820       }
1821    }
1822    if (vol_num > 1) {
1823       Dmsg0(100, "Write_end_session_label()\n");
1824       /* Create Job status for end of session label */
1825       if (!job_canceled(jcr) && ok) {
1826          set_jcr_job_status(jcr, JS_Terminated);
1827       } else if (!ok) {
1828          set_jcr_job_status(jcr, JS_ErrorTerminated);
1829       }
1830       if (!write_session_label(jcr, block, EOS_LABEL)) {
1831          Pmsg1(000, _("Error writting end session label. ERR=%s\n"), strerror_dev(dev));
1832          ok = FALSE;
1833       }
1834       /* Write out final block of this session */
1835       if (!write_block_to_device(jcr->dcr, block)) {
1836          Pmsg0(-1, _("Set ok=FALSE after write_block_to_device.\n"));
1837          ok = FALSE;
1838       }
1839       Pmsg0(-1, _("Wrote End Of Session label.\n"));
1840
1841       /* Save last block info for second tape */
1842       last_block_num2 = last_block_num;
1843       last_file2 = last_file;
1844       if (last_block2) {
1845          free_block(last_block2);
1846       }
1847       last_block2 = dup_block(last_block);
1848    }
1849
1850    sprintf(buf, "%s/btape.state", working_directory);
1851    fd = open(buf, O_CREAT|O_TRUNC|O_WRONLY, 0640);
1852    if (fd >= 0) {
1853       write(fd, &btape_state_level, sizeof(btape_state_level));
1854       write(fd, &simple, sizeof(simple));
1855       write(fd, &last_block_num1, sizeof(last_block_num1));
1856       write(fd, &last_block_num2, sizeof(last_block_num2));
1857       write(fd, &last_file1, sizeof(last_file1));
1858       write(fd, &last_file2, sizeof(last_file2));
1859       write(fd, last_block1->buf, last_block1->buf_len);
1860       write(fd, last_block2->buf, last_block2->buf_len);
1861       write(fd, first_block->buf, first_block->buf_len);
1862       close(fd);
1863       Pmsg2(-1, "Wrote state file last_block_num1=%d last_block_num2=%d\n",
1864          last_block_num1, last_block_num2);
1865    } else {
1866       Pmsg2(-1, _("Could not create state file: %s ERR=%s\n"), buf,
1867                  strerror(errno));
1868    }
1869
1870    /* Release the device */
1871    if (!release_device(jcr)) {
1872       Pmsg0(-1, _("Error in release_device\n"));
1873       ok = FALSE;
1874    }
1875
1876
1877    Pmsg2(-1, _("\n\nDone filling tape%s. Now beginning re-read of %stape ...\n"),
1878       simple?"":"s", simple?"":"first ");
1879
1880    do_unfill();
1881
1882    dev->min_block_size = min_block_size;
1883    free_memory(rec.data);
1884 }
1885
1886 /*
1887  * Read two tapes written by the "fill" command and ensure
1888  *  that the data is valid.  If stop==1 we simulate full read back
1889  *  of two tapes.  If stop==-1 we simply read the last block and
1890  *  verify that it is correct.
1891  */
1892 static void unfillcmd()
1893 {
1894    int fd;
1895
1896    last_block1 = new_block(dev);
1897    last_block2 = new_block(dev);
1898    first_block = new_block(dev);
1899    sprintf(buf, "%s/btape.state", working_directory);
1900    fd = open(buf, O_RDONLY);
1901    if (fd >= 0) {
1902       uint32_t state_level;              
1903       read(fd, &state_level, sizeof(btape_state_level));
1904       read(fd, &simple, sizeof(simple));
1905       read(fd, &last_block_num1, sizeof(last_block_num1));
1906       read(fd, &last_block_num2, sizeof(last_block_num2));
1907       read(fd, &last_file1, sizeof(last_file1));
1908       read(fd, &last_file2, sizeof(last_file2));
1909       read(fd, last_block1->buf, last_block1->buf_len);
1910       read(fd, last_block2->buf, last_block2->buf_len);
1911       read(fd, first_block->buf, first_block->buf_len);
1912       close(fd);
1913       if (state_level != btape_state_level) {
1914           Pmsg0(-1, "\nThe state file level has changed. You must redo\n"
1915                   "the fill command.\n");
1916           return;
1917        }
1918    } else {
1919       Pmsg2(-1, "\nCould not find the state file: %s ERR=%s\n"
1920              "You must redo the fill command.\n", buf, strerror(errno));
1921       return;
1922    }
1923    do_unfill();
1924    this_block = NULL;
1925 }
1926
1927 static void do_unfill()
1928 {
1929    DEV_BLOCK *block;
1930    bool autochanger;
1931
1932    dumped = 0;
1933    VolBytes = 0;
1934    LastBlock = 0;
1935    block = new_block(dev);
1936
1937    Dmsg0(20, "Enter do_unfill\n");
1938    dev->capabilities |= CAP_ANONVOLS; /* allow reading any volume */
1939    dev->capabilities &= ~CAP_LABEL;   /* don't label anything here */
1940
1941    end_of_tape = 0;
1942
1943    time(&jcr->run_time);              /* start counting time for rates */
1944    stop = 0;
1945    file_index = 0;
1946    if (last_block) {
1947       free_block(last_block);
1948    }
1949    last_block_num = last_block_num1;
1950    last_file = last_file1;
1951    last_block = last_block1;
1952
1953    if (!simple) {
1954       /* Multiple Volume tape */
1955       /* Close device so user can use autochanger if desired */
1956       if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
1957          offline_dev(dev);
1958       }
1959       autochanger = autoload_device(jcr, dev, 1, NULL);
1960       if (!autochanger) {
1961          force_close_dev(dev);
1962          get_cmd(_("Mount first tape. Press enter when ready: ")); 
1963       }
1964    
1965       free_vol_list(jcr);
1966       set_volume_name("TestVolume1", 1);
1967       jcr->bsr = NULL;
1968       create_vol_list(jcr);
1969       close_dev(dev);
1970       dev->state &= ~ST_READ;
1971       if (!acquire_device_for_read(jcr)) {
1972          Pmsg1(-1, "%s", dev->errmsg);
1973          goto bail_out;
1974       }
1975    }
1976    /*
1977     * We now have the first tape mounted.
1978     * Note, re-reading last block may have caused us to 
1979     *   loose track of where we are (block number unknown).
1980     */
1981    rewind_dev(dev);                   /* get to a known place on tape */
1982    /* Read the first 1000 records */
1983    Pmsg0(-1, _("Reading the first 1000 records.\n"));
1984    read_records(jcr, dev, quickie_cb, my_mount_next_read_volume);
1985    Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num,
1986          last_file, last_block_num);
1987    if (!reposition_dev(dev, last_file, last_block_num)) {
1988       Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
1989    }
1990    Pmsg1(-1, _("Reading block %u.\n"), last_block_num);
1991    if (!read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
1992       Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
1993       goto bail_out;
1994    }
1995    if (compare_blocks(last_block, block)) {
1996       if (simple) {
1997          Pmsg0(-1, _("\nThe last block on the tape matches. Test succeeded.\n\n"));
1998       } else {
1999          Pmsg0(-1, _("\nThe last block of the first tape matches.\n\n"));
2000       }
2001    }
2002    if (simple) {
2003       goto bail_out;
2004    }
2005
2006    /* restore info for last block on second Volume */
2007    last_block_num = last_block_num2;
2008    last_file = last_file2;
2009    last_block = last_block2;
2010
2011    /* Multiple Volume tape */
2012    /* Close device so user can use autochanger if desired */
2013    if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2014       offline_dev(dev);
2015    }
2016
2017    free_vol_list(jcr);
2018    set_volume_name("TestVolume2", 2);
2019    jcr->bsr = NULL;
2020    create_vol_list(jcr);
2021    autochanger = autoload_device(jcr, dev, 1, NULL);
2022    if (!autochanger) {
2023       force_close_dev(dev);
2024       get_cmd(_("Mount second tape. Press enter when ready: ")); 
2025    }
2026
2027    dev->state &= ~ST_READ;
2028    if (!acquire_device_for_read(jcr)) {
2029       Pmsg1(-1, "%s", dev->errmsg);
2030       goto bail_out;
2031    }
2032
2033    /* Space to "first" block which is last block not written
2034     * on the previous tape.
2035     */
2036    Pmsg2(-1, _("Reposition from %u:%u to 0:1\n"), dev->file, dev->block_num);
2037    if (!reposition_dev(dev, 0, 1)) {
2038       Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2039       goto bail_out;
2040    }
2041    Pmsg1(-1, _("Reading block %d.\n"), dev->block_num);
2042    if (!read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
2043       Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2044       goto bail_out;
2045    }
2046    if (compare_blocks(first_block, block)) {
2047       Pmsg0(-1, _("\nThe first block on the second tape matches.\n\n"));
2048    }
2049
2050    /* Now find and compare the last block */
2051    Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num,
2052          last_file, last_block_num);
2053    if (!reposition_dev(dev, last_file, last_block_num)) {
2054       Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2055       goto bail_out;
2056    }
2057    Pmsg1(-1, _("Reading block %d.\n"), dev->block_num);
2058    if (!read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
2059       Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2060       goto bail_out;
2061    }
2062    if (compare_blocks(last_block, block)) {
2063       Pmsg0(-1, _("\nThe last block on the second tape matches. Test succeeded.\n\n"));
2064    }
2065
2066 bail_out:
2067    free_block(block);
2068    free_block(last_block1);
2069    free_block(last_block2);
2070    free_block(first_block);
2071 }
2072
2073 /* Read 1000 records then stop */
2074 static int quickie_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
2075 {
2076    quickie_count++;
2077    return quickie_count <= 1000;
2078 }
2079
2080 static bool compare_blocks(DEV_BLOCK *last_block, DEV_BLOCK *block) 
2081 {
2082    char *p, *q;
2083    uint32_t CheckSum, block_len;
2084    ser_declare;
2085
2086    p = last_block->buf;      
2087    q = block->buf;
2088    unser_begin(q, BLKHDR2_LENGTH);
2089    unser_uint32(CheckSum);
2090    unser_uint32(block_len);
2091    while (q < (block->buf+block_len)) {
2092       if (*p == *q) {
2093          p++;
2094          q++;
2095          continue;
2096       }
2097       Pmsg0(-1, "\n");
2098       dump_block(last_block, _("Last block written"));
2099       Pmsg0(-1, "\n");
2100       dump_block(block, _("Block read back"));
2101       Pmsg1(-1, "\n\nThe blocks differ at byte %u\n", p - last_block->buf);
2102       Pmsg0(-1, "\n\n!!!! The last block written and the block\n"
2103                 "that was read back differ. The test FAILED !!!!\n"
2104                 "This must be corrected before you use Bacula\n"
2105                 "to write multi-tape Volumes.!!!!\n");
2106       return false;
2107    }
2108    if (verbose) {
2109       dump_block(last_block, _("Last block written"));
2110       dump_block(block, _("Block read back"));
2111    }
2112    return true;
2113 }
2114
2115
2116
2117
2118
2119 /*
2120  * Write current block to tape regardless of whether or
2121  *   not it is full. If the tape fills, attempt to
2122  *   acquire another tape.
2123  */
2124 static int flush_block(DEV_BLOCK *block, int dump)
2125 {
2126    char ec1[50];
2127    lock_device(dev);
2128    DEV_BLOCK *tblock;
2129    uint32_t this_file, this_block_num;
2130
2131    if (!this_block) {
2132       this_block = new_block(dev);
2133    }
2134    if (!last_block) {
2135       last_block = new_block(dev);
2136    }
2137    /* Copy block */
2138    this_file = dev->file;
2139    this_block_num = dev->block_num;
2140    if (!write_block_to_dev(jcr->dcr, block)) {
2141       Pmsg3(000, "Last block at: %u:%u this_dev_block_num=%d\n", 
2142                   last_file, last_block_num, this_block_num);
2143       if (vol_num == 1) {
2144          /* 
2145           * This is 1st tape, so save first tape info separate
2146           *  from second tape info 
2147           */
2148          last_block_num1 = last_block_num;
2149          last_file1 = last_file;
2150          last_block1 = dup_block(last_block);
2151          last_block2 = dup_block(last_block);
2152          first_block = dup_block(block); /* first block second tape */
2153       }
2154       if (verbose) {
2155          Pmsg3(000, "Block not written: FileIndex=%u blk_block=%u Size=%u\n", 
2156             (unsigned)file_index, block->BlockNumber, block->block_len);
2157          dump_block(last_block, "Last block written");
2158          Pmsg0(-1, "\n");
2159          dump_block(block, "Block not written");
2160       }
2161       if (stop == 0) {
2162          eot_block = block->BlockNumber;
2163          eot_block_len = block->block_len;
2164          eot_FileIndex = file_index;
2165          stop = 1;
2166       }
2167       now = time(NULL);
2168       now -= jcr->run_time;
2169       if (now <= 0) {
2170          now = 1;                     /* don't divide by zero */
2171       }
2172       kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now);
2173       vol_size = dev->VolCatInfo.VolCatBytes;
2174       Pmsg2(000, "End of tape. VolumeCapacity=%s. Write rate = %.1f KB/s\n", 
2175          edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), kbs);
2176
2177       if (simple) {
2178          stop = -1;                   /* stop, but do simplified test */
2179       } else {
2180          /* Full test in progress */
2181          if (!fixup_device_block_write_error(jcr, dev, block)) {
2182             Pmsg1(000, _("Cannot fixup device error. %s\n"), strerror_dev(dev));
2183             ok = FALSE;
2184             unlock_device(dev);
2185             return 0;
2186          }
2187          BlockNumber = 0;             /* start counting for second tape */
2188       }
2189       unlock_device(dev);
2190       return 1;                       /* end of tape reached */
2191    }
2192
2193    /* Save contents after write so that the header is serialized */
2194    memcpy(this_block->buf, block->buf, this_block->buf_len);
2195
2196    /*
2197     * Toggle between two allocated blocks for efficiency.
2198     * Switch blocks so that the block just successfully written is
2199     *  always in last_block. 
2200     */
2201    tblock = last_block;
2202    last_block = this_block; 
2203    this_block = tblock;
2204    last_file = this_file;
2205    last_block_num = this_block_num;
2206
2207    unlock_device(dev);
2208    return 1;
2209 }
2210
2211
2212 /* 
2213  * First we label the tape, then we fill
2214  *  it with data get a new tape and write a few blocks.
2215  */                            
2216 static void qfillcmd()
2217 {
2218    DEV_BLOCK *block;
2219    DEV_RECORD *rec;
2220    int i, count;
2221
2222    Pmsg0(0, "Test writing blocks of 64512 bytes to tape.\n");
2223
2224    get_cmd("How many blocks do you want to write? (1000): ");
2225
2226    count = atoi(cmd);
2227    if (count <= 0) {
2228       count = 1000;
2229    }
2230
2231    sm_check(__FILE__, __LINE__, False);
2232    block = new_block(dev);
2233    rec = new_record();
2234
2235    i = block->buf_len - 100;
2236    ASSERT (i > 0);
2237    rec->data = check_pool_memory_size(rec->data, i);
2238    memset(rec->data, i & 0xFF, i);
2239    rec->data_len = i;
2240    rewindcmd();
2241    Pmsg1(0, "Begin writing %d Bacula blocks to tape ...\n", count);
2242    for (i=0; i < count; i++) {
2243       if (i % 100 == 0) {
2244          printf("+");
2245          fflush(stdout);
2246       }
2247       if (!write_record_to_block(block, rec)) {
2248          Pmsg0(0, _("Error writing record to block.\n")); 
2249          goto bail_out;
2250       }
2251       if (!write_block_to_dev(jcr->dcr, block)) {
2252          Pmsg0(0, _("Error writing block to device.\n")); 
2253          goto bail_out;
2254       }
2255    }
2256    printf("\n");
2257    weofcmd();
2258    if (dev_cap(dev, CAP_TWOEOF)) {
2259       weofcmd();
2260    }
2261    rewindcmd();
2262    scan_blocks();
2263
2264 bail_out:
2265    sm_check(__FILE__, __LINE__, False);
2266    free_record(rec);
2267    free_block(block);
2268    sm_check(__FILE__, __LINE__, False);
2269
2270 }
2271
2272 /*
2273  * Fill a tape using raw write() command
2274  */
2275 static void rawfill_cmd()
2276 {
2277    DEV_BLOCK *block;
2278    int stat;
2279    int fd;
2280    uint32_t block_num = 0;
2281    uint32_t *p;
2282    int my_errno;
2283    uint32_t i;
2284
2285    block = new_block(dev);
2286    fd = open("/dev/urandom", O_RDONLY);
2287    if (fd) {
2288       read(fd, block->buf, block->buf_len);
2289       close(fd);
2290    } else {
2291       uint32_t *p = (uint32_t *)block->buf;
2292       srandom(time(NULL));
2293       for (i=0; i<block->buf_len/sizeof(uint32_t); i++) {
2294          p[i] = random();
2295       }
2296    }
2297    p = (uint32_t *)block->buf;
2298    Pmsg1(0, "Begin writing raw blocks of %u bytes.\n", block->buf_len);
2299    for ( ;; ) {
2300       *p = block_num;
2301       stat = write(dev->fd, block->buf, block->buf_len);
2302       if (stat == (int)block->buf_len) {
2303          if ((block_num++ % 100) == 0) {
2304             printf("+");
2305             fflush(stdout);
2306          }
2307          p[0] += p[13];
2308          for (i=1; i<(block->buf_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) {
2309             p[i] += p[i-1];
2310          }
2311          continue;
2312       }
2313       break;
2314    }
2315    my_errno = errno;
2316    printf("\n");
2317    printf("Write failed at block %u. stat=%d ERR=%s\n", block_num, stat,
2318       strerror(my_errno));
2319    weofcmd();
2320    free_block(block);
2321 }
2322
2323
2324 /*
2325  * Fill a tape using raw write() command
2326  */
2327 static void bfill_cmd()
2328 {
2329    DEV_BLOCK *block;
2330    uint32_t block_num = 0;
2331    uint32_t *p;
2332    int my_errno;
2333    int fd;   
2334    uint32_t i;
2335
2336    block = new_block(dev);
2337    fd = open("/dev/urandom", O_RDONLY);
2338    if (fd) {
2339       read(fd, block->buf, block->buf_len);
2340       close(fd);
2341    } else {
2342       uint32_t *p = (uint32_t *)block->buf;
2343       srandom(time(NULL));
2344       for (i=0; i<block->buf_len/sizeof(uint32_t); i++) {
2345          p[i] = random();
2346       }
2347    }
2348    p = (uint32_t *)block->buf;
2349    Pmsg1(0, "Begin writing Bacula blocks of %u bytes.\n", block->buf_len);
2350    for ( ;; ) {
2351       *p = block_num;
2352       block->binbuf = block->buf_len;
2353       block->bufp = block->buf + block->binbuf;
2354       if (!write_block_to_dev(jcr->dcr, block)) {
2355          break;
2356       }
2357       if ((block_num++ % 100) == 0) {
2358          printf("+");
2359          fflush(stdout);
2360       }
2361       p[0] += p[13];
2362       for (i=1; i<(block->buf_len/sizeof(uint32_t)-1); i++) {
2363          p[i] += p[i-1];
2364       }
2365    }
2366    my_errno = errno;
2367    printf("\n");
2368    printf("Write failed at block %u.\n", block_num);     
2369    weofcmd();
2370    free_block(block);
2371 }
2372
2373
2374 struct cmdstruct { char *key; void (*func)(); char *help; }; 
2375 static struct cmdstruct commands[] = {
2376  {"autochanger", autochangercmd, "test autochanger"},
2377  {"bsf",        bsfcmd,       "backspace file"},
2378  {"bsr",        bsrcmd,       "backspace record"},
2379  {"bfill",      bfill_cmd,    "fill tape using Bacula writes"},
2380  {"cap",        capcmd,       "list device capabilities"},
2381  {"clear",      clearcmd,     "clear tape errors"},
2382  {"eod",        eodcmd,       "go to end of Bacula data for append"},
2383  {"eom",        eomcmd,       "go to the physical end of medium"},
2384  {"fill",       fillcmd,      "fill tape, write onto second volume"},
2385  {"unfill",     unfillcmd,    "read filled tape"},
2386  {"fsf",        fsfcmd,       "forward space a file"},
2387  {"fsr",        fsrcmd,       "forward space a record"},
2388  {"help",       helpcmd,      "print this command"},
2389  {"label",      labelcmd,     "write a Bacula label to the tape"},
2390  {"load",       loadcmd,      "load a tape"},
2391  {"quit",       quitcmd,      "quit btape"},   
2392  {"rawfill",    rawfill_cmd,  "use write() to fill tape"},
2393  {"readlabel",  readlabelcmd, "read and print the Bacula tape label"},
2394  {"rectest",    rectestcmd,   "test record handling functions"},
2395  {"rewind",     rewindcmd,    "rewind the tape"},
2396  {"scan",       scancmd,      "read() tape block by block to EOT and report"}, 
2397  {"scanblocks", scan_blocks,  "Bacula read block by block to EOT and report"},
2398  {"status",     statcmd,      "print tape status"},
2399  {"test",       testcmd,      "General test Bacula tape functions"},
2400  {"weof",       weofcmd,      "write an EOF on the tape"},
2401  {"wr",         wrcmd,        "write a single Bacula block"}, 
2402  {"rr",         rrcmd,        "read a single record"},
2403  {"qfill",      qfillcmd,     "quick fill command"},
2404              };
2405 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
2406
2407 static void
2408 do_tape_cmds()
2409 {
2410    unsigned int i;
2411    bool found;
2412
2413    while (get_cmd("*")) {
2414       sm_check(__FILE__, __LINE__, False);
2415       found = false;
2416       parse_args(cmd, &args, &argc, argk, argv, MAX_CMD_ARGS);
2417       for (i=0; i<comsize; i++)       /* search for command */
2418          if (argc > 0 && fstrsch(argk[0],  commands[i].key)) {
2419             (*commands[i].func)();    /* go execute command */
2420             found = true;
2421             break;
2422          }
2423       if (!found)
2424          Pmsg1(0, _("%s is an illegal command\n"), cmd);
2425       if (quit)
2426          break;
2427    }
2428 }
2429
2430 static void helpcmd()
2431 {
2432    unsigned int i;
2433    usage();
2434    printf(_("Interactive commands:\n"));
2435    printf(_("  Command    Description\n  =======    ===========\n"));
2436    for (i=0; i<comsize; i++)
2437       printf("  %-10s %s\n", commands[i].key, commands[i].help);
2438    printf("\n");
2439 }
2440
2441 static void usage()
2442 {
2443    fprintf(stderr, _(
2444 "\nVersion: " VERSION " (" BDATE ")\n\n"
2445 "Usage: btape <options> <device_name>\n"
2446 "       -b <file>   specify bootstrap file\n"
2447 "       -c <file>   set configuration file to file\n"
2448 "       -d <nn>     set debug level to nn\n"
2449 "       -s          turn off signals\n"
2450 "       -v          be verbose\n"
2451 "       -?          print this message.\n"  
2452 "\n"));
2453
2454 }
2455
2456 /*      
2457  * Get next input command from terminal.  This
2458  * routine is REALLY primitive, and should be enhanced
2459  * to have correct backspacing, etc.
2460  */
2461 int 
2462 get_cmd(char *prompt)
2463 {
2464    int i = 0;
2465    int ch;
2466    fprintf(stdout, prompt);
2467
2468    /* We really should turn off echoing and pretty this
2469     * up a bit.
2470     */
2471    cmd[i] = 0;
2472    while ((ch = fgetc(stdin)) != EOF) { 
2473       if (ch == '\n') {
2474          strip_trailing_junk(cmd);
2475          return 1;
2476       } else if (ch == 4 || ch == 0xd3 || ch == 0x8) {
2477          if (i > 0)
2478             cmd[--i] = 0;
2479          continue;
2480       } 
2481          
2482       cmd[i++] = ch;
2483       cmd[i] = 0;
2484    }
2485    quit = 1;
2486    return 0;
2487 }
2488
2489 /* Dummies to replace askdir.c */
2490 int     dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
2491 int     dir_send_job_status(JCR *jcr) {return 1;}
2492
2493 int dir_update_volume_info(JCR *jcr, DEVICE *dev, int relabel) 
2494
2495    return 1;
2496 }
2497
2498
2499 int dir_get_volume_info(JCR *jcr, enum get_vol_info_rw  writing)             
2500 {
2501    Dmsg0(20, "Enter dir_get_volume_info\n");
2502    bstrncpy(jcr->VolCatInfo.VolCatName, jcr->VolumeName, sizeof(jcr->VolCatInfo.VolCatName));
2503    return 1;
2504 }
2505
2506 int dir_create_jobmedia_record(JCR *jcr)
2507 {
2508    jcr->dcr->WroteVol = false;
2509    return 1;
2510 }
2511
2512
2513 int dir_find_next_appendable_volume(JCR *jcr) 
2514
2515    Dmsg1(20, "Enter dir_find_next_appendable_volume. stop=%d\n", stop);
2516    return jcr->VolumeName[0] != 0;
2517 }
2518
2519 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
2520 {
2521    Dmsg0(20, "Enter dir_ask_sysop_to_mount_volume\n");
2522    if (jcr->VolumeName[0] == 0) {
2523       return dir_ask_sysop_to_create_appendable_volume(jcr, dev);
2524    }
2525    /* Close device so user can use autochanger if desired */
2526    if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2527       offline_dev(dev);
2528    }
2529    force_close_dev(dev);
2530    Pmsg1(-1, "%s", dev->errmsg);           /* print reason */
2531    if (jcr->VolumeName[0] == 0 || strcmp(jcr->VolumeName, "TestVolume2") == 0) {
2532       fprintf(stderr, "Mount second Volume on device %s and press return when ready: ",
2533          dev_name(dev));
2534    } else {
2535       fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
2536          jcr->VolumeName, dev_name(dev));
2537    }
2538    getchar();   
2539    return 1;
2540 }
2541
2542 int dir_ask_sysop_to_create_appendable_volume(JCR *jcr, DEVICE *dev)
2543 {
2544    bool autochanger;
2545    Dmsg0(20, "Enter dir_ask_sysop_to_create_appendable_volume\n");
2546    if (stop == 0) {
2547       set_volume_name("TestVolume1", 1);
2548    } else {
2549       set_volume_name("TestVolume2", 2);
2550    }
2551    /* Close device so user can use autochanger if desired */
2552    if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2553       offline_dev(dev);
2554    }
2555    autochanger = autoload_device(jcr, dev, 1, NULL);
2556    if (!autochanger) {
2557       force_close_dev(dev);
2558       fprintf(stderr, "Mount blank Volume on device %s and press return when ready: ",
2559          dev_name(dev));
2560       getchar();   
2561    }
2562    open_device(jcr, dev);
2563    labelcmd();
2564    VolumeName = NULL;
2565    BlockNumber = 0;
2566    return 1;
2567 }
2568
2569 static int my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
2570 {
2571    char ec1[50];
2572
2573    Dmsg0(20, "Enter my_mount_next_read_volume\n");
2574    Pmsg1(000, "End of Volume \"%s\"\n", jcr->VolumeName);
2575
2576    if (LastBlock != block->BlockNumber) {
2577       VolBytes += block->block_len;
2578    }
2579    LastBlock = block->BlockNumber;
2580    now = time(NULL);
2581    now -= jcr->run_time;
2582    if (now <= 0) {
2583       now = 1;
2584    }
2585    kbs = (double)VolBytes / (1000.0 * (double)now);
2586    Pmsg3(-1, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
2587             edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
2588
2589    if (strcmp(jcr->VolumeName, "TestVolume2") == 0) {
2590       end_of_tape = 1;
2591       return 0;
2592    }
2593
2594    free_vol_list(jcr);
2595    set_volume_name("TestVolume2", 2);
2596    jcr->bsr = NULL;
2597    create_vol_list(jcr);
2598    close_dev(dev);
2599    dev->state &= ~ST_READ; 
2600    if (!acquire_device_for_read(jcr)) {
2601       Pmsg2(0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev), jcr->VolumeName);
2602       return 0;
2603    }
2604    return 1;                       /* next volume mounted */
2605 }
2606
2607 static void set_volume_name(char *VolName, int volnum) 
2608 {
2609    DCR *dcr = jcr->dcr;
2610    VolumeName = VolName;
2611    vol_num = volnum;
2612    pm_strcpy(&jcr->VolumeName, VolName);
2613    bstrncpy(dev->VolCatInfo.VolCatName, VolName, sizeof(dev->VolCatInfo.VolCatName));
2614    bstrncpy(jcr->VolCatInfo.VolCatName, VolName, sizeof(jcr->VolCatInfo.VolCatName));
2615    bstrncpy(dcr->VolCatInfo.VolCatName, VolName, sizeof(dcr->VolCatInfo.VolCatName));
2616    bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
2617    jcr->VolCatInfo.Slot = volnum;
2618    dcr->VolCatInfo.Slot = volnum;
2619 }
2620
2621 /*
2622  * Edit codes into ChangerCommand
2623  *  %% = %
2624  *  %a = archive device name
2625  *  %c = changer device name
2626  *  %f = Client's name
2627  *  %j = Job name
2628  *  %o = command
2629  *  %s = Slot base 0
2630  *  %S = Slot base 1
2631  *  %v = Volume name
2632  *
2633  *
2634  *  omsg = edited output message
2635  *  imsg = input string containing edit codes (%x)
2636  *  cmd = command string (load, unload, ...) 
2637  *
2638  */
2639 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd) 
2640 {
2641    char *p;
2642    const char *str;
2643    char add[20];
2644
2645    *omsg = 0;
2646    Dmsg1(400, "edit_device_codes: %s\n", imsg);
2647    for (p=imsg; *p; p++) {
2648       if (*p == '%') {
2649          switch (*++p) {
2650          case '%':
2651             str = "%";
2652             break;
2653          case 'a':
2654             str = dev_name(jcr->device->dev);
2655             break;
2656          case 'c':
2657             str = NPRT(jcr->device->changer_name);
2658             break;
2659          case 'o':
2660             str = NPRT(cmd);
2661             break;
2662          case 's':
2663             sprintf(add, "%d", jcr->VolCatInfo.Slot - 1);
2664             str = add;
2665             break;
2666          case 'S':
2667             sprintf(add, "%d", jcr->VolCatInfo.Slot);
2668             str = add;
2669             break;
2670          case 'j':                    /* Job name */
2671             str = jcr->Job;
2672             break;
2673          case 'v':
2674             str = NPRT(jcr->VolumeName);
2675             break;
2676          case 'f':
2677             str = NPRT(jcr->client_name);
2678             break;
2679
2680          default:
2681             add[0] = '%';
2682             add[1] = *p;
2683             add[2] = 0;
2684             str = add;
2685             break;
2686          }
2687       } else {
2688          add[0] = *p;
2689          add[1] = 0;
2690          str = add;
2691       }
2692       Dmsg1(400, "add_str %s\n", str);
2693       pm_strcat(&omsg, (char *)str);
2694       Dmsg1(400, "omsg=%s\n", omsg);
2695    }
2696    return omsg;
2697 }
2698
2699 #ifdef xxxx_needed
2700 /* 
2701  * We are called here from "unfill" for each record on the tape.
2702  */
2703 static int record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
2704 {
2705    SESSION_LABEL label;
2706
2707    if (stop > 1 && !dumped) {         /* on second tape */
2708       dumped = 1;
2709       if (verbose) {
2710          dump_block(block, "First block on second tape");
2711       }
2712       Pmsg4(-1, "Blk: FileIndex=%d: block=%u size=%d vol=%s\n", 
2713            rec->FileIndex, block->BlockNumber, block->block_len, dev->VolHdr.VolName);
2714       Pmsg6(-1, "   Rec: VId=%d VT=%d FI=%s Strm=%s len=%d state=%x\n",
2715            rec->VolSessionId, rec->VolSessionTime, 
2716            FI_to_ascii(rec->FileIndex), stream_to_ascii(rec->Stream, rec->FileIndex),
2717            rec->data_len, rec->state);
2718    }
2719    if (rec->FileIndex < 0) {
2720       if (verbose > 1) {
2721          dump_label_record(dev, rec, 1);
2722       }
2723       switch (rec->FileIndex) {
2724       case PRE_LABEL:
2725          Pmsg0(-1, "Volume is prelabeled. This tape cannot be scanned.\n");
2726          return 1;;
2727       case VOL_LABEL:
2728          unser_volume_label(dev, rec);
2729          Pmsg3(-1, "VOL_LABEL: block=%u size=%d vol=%s\n", block->BlockNumber, 
2730             block->block_len, dev->VolHdr.VolName);
2731          stop++;
2732          break;
2733       case SOS_LABEL:
2734          unser_session_label(&label, rec);
2735          Pmsg1(-1, "SOS_LABEL: JobId=%u\n", label.JobId);
2736          break;
2737       case EOS_LABEL:
2738          unser_session_label(&label, rec);
2739          Pmsg2(-1, "EOS_LABEL: block=%u JobId=%u\n", block->BlockNumber, 
2740             label.JobId);
2741          break;
2742       case EOM_LABEL:
2743          Pmsg0(-1, "EOM_LABEL:\n");
2744          break;
2745       case EOT_LABEL:              /* end of all tapes */
2746          char ec1[50];
2747
2748          if (LastBlock != block->BlockNumber) {
2749             VolBytes += block->block_len;
2750          }
2751          LastBlock = block->BlockNumber;
2752          now = time(NULL);
2753          now -= jcr->run_time;
2754          if (now <= 0) {
2755             now = 1;
2756          }
2757          kbs = (double)VolBytes / (1000 * now);
2758          Pmsg3(000, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
2759                   edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
2760
2761          Pmsg0(000, "End of all tapes.\n");
2762
2763          break;
2764       default:
2765          break;
2766       }
2767       return 1;
2768    }
2769    if (++file_index != rec->FileIndex) {
2770       Pmsg3(000, "Incorrect FileIndex in Block %u. Got %d, expected %d.\n", 
2771          block->BlockNumber, rec->FileIndex, file_index);
2772    }
2773    if (LastBlock != block->BlockNumber) {
2774       VolBytes += block->block_len;
2775    }
2776    if ((block->BlockNumber != LastBlock) && (block->BlockNumber % 50000) == 0) {
2777       char ec1[50];
2778       now = time(NULL);
2779       now -= jcr->run_time;
2780       if (now <= 0) {
2781          now = 1;
2782       }
2783       kbs = (double)VolBytes / (1000 * now);
2784       Pmsg3(000, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
2785                edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
2786    }
2787    LastBlock = block->BlockNumber;
2788    if (end_of_tape) {
2789       Pmsg1(000, "End of all blocks. Block=%u\n", block->BlockNumber);
2790    }
2791    return 1;
2792 }
2793 #endif