]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/label.c
Apply Preben 'Peppe' Guldberg <peppe@wielders.org>
[bacula/bacula] / bacula / src / stored / label.c
1 /*
2  *
3  *  label.c  Bacula routines to handle labels
4  *
5  *   Kern Sibbald, MM
6  *
7  *
8  *   Version $Id$
9  */
10 /*
11    Copyright (C) 2000-2004 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"                   /* pull in global headers */
31 #include "stored.h"                   /* pull in Storage Deamon headers */
32
33 /* Forward referenced functions */
34 static void create_volume_label_record(DCR *dcr, DEV_RECORD *rec);
35
36 extern char my_name[];
37 extern int debug_level;
38
39 /*
40  * Read the volume label
41  *
42  *  If dcr->VolumeName == NULL, we accept any Bacula Volume
43  *  If dcr->VolumeName[0] == 0, we accept any Bacula Volume
44  *  otherwise dcr->VolumeName must match the Volume.
45  *
46  *  If VolName given, ensure that it matches
47  *
48  *  Returns VOL_  code as defined in record.h
49  *    VOL_NOT_READ
50  *    VOL_OK
51  *    VOL_NO_LABEL
52  *    VOL_IO_ERROR
53  *    VOL_NAME_ERROR
54  *    VOL_CREATE_ERROR
55  *    VOL_VERSION_ERROR
56  *    VOL_LABEL_ERROR
57  *    VOL_NO_MEDIA
58  */
59 int read_dev_volume_label(DCR *dcr)
60 {
61    JCR *jcr = dcr->jcr;
62    DEVICE *dev = dcr->dev;
63    char *VolName = dcr->VolumeName;
64    DEV_RECORD *record;
65    bool ok = false;
66    DEV_BLOCK *block = dcr->block;
67
68    Dmsg3(100, "Enter read_volume_label device=%s vol=%s dev_Vol=%s\n",
69       dev_name(dev), VolName, dev->VolHdr.VolName);
70
71    if (dev_state(dev, ST_LABEL)) {       /* did we already read label? */
72       /* Compare Volume Names allow special wild card */
73       if (VolName && *VolName && *VolName != '*' && strcmp(dev->VolHdr.VolName, VolName) != 0) {
74          Mmsg(jcr->errmsg, _("Wrong Volume mounted on device %s: Wanted %s have %s\n"),
75             dev_name(dev), VolName, dev->VolHdr.VolName);
76          /*
77           * Cancel Job if too many label errors
78           *  => we are in a loop
79           */
80          if (!dev->poll && jcr->label_errors++ > 100) {
81             Jmsg(jcr, M_FATAL, 0, "Too many tries: %s", jcr->errmsg);
82          }
83          return VOL_NAME_ERROR;
84       }
85       Dmsg0(30, "Leave read_volume_label() VOL_OK\n");
86       return VOL_OK;       /* label already read */
87    }
88
89    dev->state &= ~(ST_LABEL|ST_APPEND|ST_READ);  /* set no label, no append */
90
91    if (!rewind_dev(dev)) {
92       Mmsg(jcr->errmsg, _("Couldn't rewind device %s ERR=%s\n"), dev_name(dev),
93          strerror_dev(dev));
94       Dmsg1(30, "%s", jcr->errmsg);
95       return VOL_NO_MEDIA;
96    }
97    bstrncpy(dev->VolHdr.Id, "**error**", sizeof(dev->VolHdr.Id));
98
99    /* Read the Volume label block */
100    record = new_record();
101    empty_block(block);
102    Dmsg0(90, "Big if statement in read_volume_label\n");
103    if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
104       Mmsg(jcr->errmsg, _("Requested Volume \"%s\" on %s is not a Bacula "
105            "labeled Volume, because: ERR=%s"), NPRT(VolName), dev_name(dev),
106            strerror_dev(dev));
107       Dmsg1(30, "%s", jcr->errmsg);
108    } else if (!read_record_from_block(block, record)) {
109       Mmsg(jcr->errmsg, _("Could not read Volume label from block.\n"));
110       Dmsg1(30, "%s", jcr->errmsg);
111    } else if (!unser_volume_label(dev, record)) {
112       Mmsg(jcr->errmsg, _("Could not unserialize Volume label: ERR=%s\n"),
113          strerror_dev(dev));
114       Dmsg1(30, "%s", jcr->errmsg);
115    } else if (strcmp(dev->VolHdr.Id, BaculaId) != 0 &&
116               strcmp(dev->VolHdr.Id, OldBaculaId) != 0) {
117       Mmsg(jcr->errmsg, _("Volume Header Id bad: %s\n"), dev->VolHdr.Id);
118       Dmsg1(30, "%s", jcr->errmsg);
119    } else {
120       ok = true;
121    }
122    if (!ok) {
123       free_record(record);
124       if (forge_on || jcr->ignore_label_errors) {
125          dev->state |= ST_LABEL;      /* set has Bacula label */
126          Jmsg(jcr, M_ERROR, 0, "%s", jcr->errmsg);
127          return VOL_OK;
128       }
129       empty_block(block);
130       rewind_dev(dev);
131       return VOL_NO_LABEL;
132    }
133
134    free_record(record);
135    /* If we are a streaming device, we only get one chance to read */
136    if (!dev_cap(dev, CAP_STREAM)) {
137       empty_block(block);
138       rewind_dev(dev);
139    }
140
141    if (dev->VolHdr.VerNum != BaculaTapeVersion &&
142        dev->VolHdr.VerNum != OldCompatibleBaculaTapeVersion1 &&
143        dev->VolHdr.VerNum != OldCompatibleBaculaTapeVersion2) {
144       Mmsg(jcr->errmsg, _("Volume on %s has wrong Bacula version. Wanted %d got %d\n"),
145          dev_name(dev), BaculaTapeVersion, dev->VolHdr.VerNum);
146       Dmsg1(30, "%s", jcr->errmsg);
147       return VOL_VERSION_ERROR;
148    }
149
150    /* We are looking for either an unused Bacula tape (PRE_LABEL) or
151     * a Bacula volume label (VOL_LABEL)
152     */
153    if (dev->VolHdr.LabelType != PRE_LABEL && dev->VolHdr.LabelType != VOL_LABEL) {
154       Mmsg(jcr->errmsg, _("Volume on %s has bad Bacula label type: %x\n"),
155           dev_name(dev), dev->VolHdr.LabelType);
156       Dmsg1(30, "%s", jcr->errmsg);
157       return VOL_LABEL_ERROR;
158    }
159
160    dev->state |= ST_LABEL;            /* set has Bacula label */
161
162    /* Compare Volume Names */
163    Dmsg2(30, "Compare Vol names: VolName=%s hdr=%s\n", VolName?VolName:"*", dev->VolHdr.VolName);
164    if (VolName && *VolName && *VolName != '*' && strcmp(dev->VolHdr.VolName, VolName) != 0) {
165       Mmsg(jcr->errmsg, _("Wrong Volume mounted on device %s: Wanted %s have %s\n"),
166            dev_name(dev), VolName, dev->VolHdr.VolName);
167       Dmsg1(30, "%s", jcr->errmsg);
168       /*
169        * Cancel Job if too many label errors
170        *  => we are in a loop
171        */
172       if (!dev->poll && jcr->label_errors++ > 100) {
173          Jmsg(jcr, M_FATAL, 0, "Too many tries: %s", jcr->errmsg);
174       }
175       return VOL_NAME_ERROR;
176    }
177    Dmsg1(30, "Copy vol_name=%s\n", dev->VolHdr.VolName);
178
179    if (debug_level >= 10) {
180       dump_volume_label(dev);
181    }
182    Dmsg0(30, "Leave read_volume_label() VOL_OK\n");
183    return VOL_OK;
184 }
185
186 /*  unser_volume_label
187  *
188  * Unserialize the Volume label into the device Volume_Label
189  * structure.
190  *
191  * Assumes that the record is already read.
192  *
193  * Returns: false on error
194  *          true  on success
195 */
196
197 bool unser_volume_label(DEVICE *dev, DEV_RECORD *rec)
198 {
199    ser_declare;
200
201    if (rec->FileIndex != VOL_LABEL && rec->FileIndex != PRE_LABEL) {
202       Mmsg3(dev->errmsg, _("Expecting Volume Label, got FI=%s Stream=%s len=%d\n"),
203               FI_to_ascii(rec->FileIndex),
204               stream_to_ascii(rec->Stream, rec->FileIndex),
205               rec->data_len);
206       if (!forge_on) {
207          return false;
208       }
209    }
210
211    dev->VolHdr.LabelType = rec->FileIndex;
212    dev->VolHdr.LabelSize = rec->data_len;
213
214
215    /* Unserialize the record into the Volume Header */
216    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Volume_Label);
217    ser_begin(rec->data, SER_LENGTH_Volume_Label);
218    unser_string(dev->VolHdr.Id);
219    unser_uint32(dev->VolHdr.VerNum);
220
221    if (dev->VolHdr.VerNum >= 11) {
222       unser_btime(dev->VolHdr.label_btime);
223       unser_btime(dev->VolHdr.write_btime);
224    } else { /* old way */
225       unser_float64(dev->VolHdr.label_date);
226       unser_float64(dev->VolHdr.label_time);
227    }
228    unser_float64(dev->VolHdr.write_date);    /* Unused with VerNum >= 11 */
229    unser_float64(dev->VolHdr.write_time);    /* Unused with VerNum >= 11 */
230
231    unser_string(dev->VolHdr.VolName);
232    unser_string(dev->VolHdr.PrevVolName);
233    unser_string(dev->VolHdr.PoolName);
234    unser_string(dev->VolHdr.PoolType);
235    unser_string(dev->VolHdr.MediaType);
236
237    unser_string(dev->VolHdr.HostName);
238    unser_string(dev->VolHdr.LabelProg);
239    unser_string(dev->VolHdr.ProgVersion);
240    unser_string(dev->VolHdr.ProgDate);
241
242    ser_end(rec->data, SER_LENGTH_Volume_Label);
243    Dmsg0(90, "ser_read_vol\n");
244    if (debug_level >= 90) {
245       dump_volume_label(dev);
246    }
247    return true;
248 }
249
250 /*
251  * Put a volume label into the block
252  *
253  *  Returns: false on failure
254  *           true  on success
255  */
256 bool write_volume_label_to_block(DCR *dcr)
257 {
258    DEV_RECORD rec;
259    DEVICE *dev = dcr->dev;
260    JCR *jcr = dcr->jcr;
261    DEV_BLOCK *block = dcr->block;
262
263    Dmsg0(20, "write Label in write_volume_label_to_block()\n");
264    memset(&rec, 0, sizeof(rec));
265    rec.data = get_memory(SER_LENGTH_Volume_Label);
266    empty_block(block);                /* Volume label always at beginning */
267
268    create_volume_label_record(dcr, &rec);
269
270    block->BlockNumber = 0;
271    if (!write_record_to_block(block, &rec)) {
272       free_pool_memory(rec.data);
273       Jmsg1(jcr, M_FATAL, 0, _("Cannot write Volume label to block for device %s\n"),
274          dev_name(dev));
275       return false;
276    } else {
277       Dmsg1(90, "Wrote label of %d bytes to block\n", rec.data_len);
278    }
279    free_pool_memory(rec.data);
280    return true;
281 }
282
283 /*
284  *  create_volume_label_record
285  *   Serialize label (from dev->VolHdr structure) into device record.
286  *   Assumes that the dev->VolHdr structure is properly
287  *   initialized.
288 */
289 static void create_volume_label_record(DCR *dcr, DEV_RECORD *rec)
290 {
291    ser_declare;
292    struct date_time dt;
293    DEVICE *dev = dcr->dev;
294    JCR *jcr = dcr->jcr;
295
296    /* Serialize the label into the device record. */
297
298    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Volume_Label);
299    ser_begin(rec->data, SER_LENGTH_Volume_Label);
300    ser_string(dev->VolHdr.Id);
301
302    ser_uint32(dev->VolHdr.VerNum);
303
304    if (dev->VolHdr.VerNum >= 11) {
305       ser_btime(dev->VolHdr.label_btime);
306       dev->VolHdr.write_btime = get_current_btime();
307       ser_btime(dev->VolHdr.write_btime);
308       dev->VolHdr.write_date = 0;
309       dev->VolHdr.write_time = 0;
310    } else {
311       /* OLD WAY DEPRECATED */
312       ser_float64(dev->VolHdr.label_date);
313       ser_float64(dev->VolHdr.label_time);
314       get_current_time(&dt);
315       dev->VolHdr.write_date = dt.julian_day_number;
316       dev->VolHdr.write_time = dt.julian_day_fraction;
317    }
318    ser_float64(dev->VolHdr.write_date);   /* 0 if VerNum >= 11 */
319    ser_float64(dev->VolHdr.write_time);   /* 0  if VerNum >= 11 */
320
321    ser_string(dev->VolHdr.VolName);
322    ser_string(dev->VolHdr.PrevVolName);
323    ser_string(dev->VolHdr.PoolName);
324    ser_string(dev->VolHdr.PoolType);
325    ser_string(dev->VolHdr.MediaType);
326
327    ser_string(dev->VolHdr.HostName);
328    ser_string(dev->VolHdr.LabelProg);
329    ser_string(dev->VolHdr.ProgVersion);
330    ser_string(dev->VolHdr.ProgDate);
331
332    ser_end(rec->data, SER_LENGTH_Volume_Label);
333    rec->data_len = ser_length(rec->data);
334    rec->FileIndex = dev->VolHdr.LabelType;
335    rec->VolSessionId = jcr->VolSessionId;
336    rec->VolSessionTime = jcr->VolSessionTime;
337    rec->Stream = jcr->NumVolumes;
338    Dmsg2(100, "Created Vol label rec: FI=%s len=%d\n", FI_to_ascii(rec->FileIndex),
339       rec->data_len);
340 }
341
342
343 /*
344  * Create a volume label in memory
345  */
346 void create_volume_label(DEVICE *dev, const char *VolName, const char *PoolName)
347 {
348    DEVRES *device = (DEVRES *)dev->device;
349
350    Dmsg0(90, "Start create_volume_label()\n");
351
352    ASSERT(dev != NULL);
353
354    memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
355
356    bstrncpy(dev->VolHdr.Id, BaculaId, sizeof(dev->VolHdr.Id));
357    dev->VolHdr.VerNum = BaculaTapeVersion;
358    dev->VolHdr.LabelType = PRE_LABEL;  /* Mark tape as unused */
359    bstrncpy(dev->VolHdr.VolName, VolName, sizeof(dev->VolHdr.VolName));
360    bstrncpy(dev->VolHdr.PoolName, PoolName, sizeof(dev->VolHdr.PoolName));
361    bstrncpy(dev->VolHdr.MediaType, device->media_type, sizeof(dev->VolHdr.MediaType));
362
363    bstrncpy(dev->VolHdr.PoolType, "Backup", sizeof(dev->VolHdr.PoolType));
364
365    dev->VolHdr.label_btime = get_current_btime();
366    dev->VolHdr.label_date = 0;
367    dev->VolHdr.label_time = 0;
368
369    if (gethostname(dev->VolHdr.HostName, sizeof(dev->VolHdr.HostName)) != 0) {
370       dev->VolHdr.HostName[0] = 0;
371    }
372    bstrncpy(dev->VolHdr.LabelProg, my_name, sizeof(dev->VolHdr.LabelProg));
373    sprintf(dev->VolHdr.ProgVersion, "Ver. %s %s", VERSION, BDATE);
374    sprintf(dev->VolHdr.ProgDate, "Build %s %s", __DATE__, __TIME__);
375    dev->state |= ST_LABEL;            /* set has Bacula label */
376    if (debug_level >= 90) {
377       dump_volume_label(dev);
378    }
379 }
380
381 /*
382  * Write a Volume Label
383  *  !!! Note, this is ONLY used for writing
384  *            a fresh volume label.  Any data
385  *            after the label will be destroyed,
386  *            in fact, we write the label 5 times !!!!
387  *
388  *  This routine expects that open_device() was previously called.
389  *
390  *  This routine should be used only when labeling a blank tape.
391  */
392 bool write_new_volume_label_to_dev(DCR *dcr, const char *VolName, const char *PoolName)
393 {
394    bool ok = false;
395    DEVICE *dev = dcr->dev;
396
397
398    Dmsg0(99, "write_volume_label()\n");
399    empty_block(dcr->block);
400    /* Create PRE_LABEL */
401    create_volume_label(dev, VolName, PoolName);
402
403    if (!rewind_dev(dev)) {
404       memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
405       Dmsg2(30, "Bad status on %s from rewind. ERR=%s\n", dev_name(dev), strerror_dev(dev));
406       if (!forge_on) {
407          return false;
408       }
409    }
410
411    create_volume_label_record(dcr, dcr->rec);
412    dcr->rec->Stream = 0;
413
414    /* Temporarily mark in append state to enable writing */
415    dev->state |= ST_APPEND;
416    if (!write_record_to_block(dcr->block, dcr->rec)) {
417       Dmsg2(30, "Bad Label write on %s. ERR=%s\n", dev_name(dev), strerror_dev(dev));
418       memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
419       goto bail_out;
420    } else {
421       Dmsg2(30, "Wrote label of %d bytes to %s\n", dcr->rec->data_len, dev_name(dev));
422    }
423
424    Dmsg0(99, "Call write_block_to_dev()\n");
425    if (!write_block_to_dev(dcr)) {
426       memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
427       Dmsg2(30, "Bad Label write on %s. ERR=%s\n", dev_name(dev), strerror_dev(dev));
428       goto bail_out;
429    }
430    Dmsg0(99, " Wrote block to device\n");
431
432    if (weof_dev(dev, 1) == 0) {
433       dev->state |= ST_LABEL;
434       ok = true;
435    }
436
437    if (debug_level >= 20)  {
438       dump_volume_label(dev);
439    }
440
441 bail_out:
442    dev->state &= ~ST_APPEND;          /* remove append since this is PRE_LABEL */
443    return ok;
444 }
445
446
447 /*
448  * Create session label
449  *  The pool memory must be released by the calling program
450  */
451 void create_session_label(DCR *dcr, DEV_RECORD *rec, int label)
452 {
453    JCR *jcr = dcr->jcr;
454    ser_declare;
455
456    rec->VolSessionId   = jcr->VolSessionId;
457    rec->VolSessionTime = jcr->VolSessionTime;
458    rec->Stream         = jcr->JobId;
459
460    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Session_Label);
461    ser_begin(rec->data, SER_LENGTH_Session_Label);
462    ser_string(BaculaId);
463    ser_uint32(BaculaTapeVersion);
464
465    ser_uint32(jcr->JobId);
466
467    /* Changed in VerNum 11 */
468    ser_btime(get_current_btime());
469    ser_float64(0);
470
471    ser_string(dcr->pool_name);
472    ser_string(dcr->pool_type);
473    ser_string(jcr->job_name);         /* base Job name */
474    ser_string(jcr->client_name);
475
476    /* Added in VerNum 10 */
477    ser_string(jcr->Job);              /* Unique name of this Job */
478    ser_string(jcr->fileset_name);
479    ser_uint32(jcr->JobType);
480    ser_uint32(jcr->JobLevel);
481    /* Added in VerNum 11 */
482    ser_string(jcr->fileset_md5);
483
484    if (label == EOS_LABEL) {
485       ser_uint32(jcr->JobFiles);
486       ser_uint64(jcr->JobBytes);
487       ser_uint32(dcr->StartBlock);
488       ser_uint32(dcr->EndBlock);
489       ser_uint32(dcr->StartFile);
490       ser_uint32(dcr->EndFile);
491       ser_uint32(jcr->JobErrors);
492
493       /* Added in VerNum 11 */
494       ser_uint32(jcr->JobStatus);
495    }
496    ser_end(rec->data, SER_LENGTH_Session_Label);
497    rec->data_len = ser_length(rec->data);
498 }
499
500 /* Write session label
501  *  Returns: false on failure
502  *           true  on success
503  */
504 bool write_session_label(DCR *dcr, int label)
505 {
506    JCR *jcr = dcr->jcr;
507    DEVICE *dev = dcr->dev;
508    DEV_RECORD *rec;
509    DEV_BLOCK *block = dcr->block;
510
511    rec = new_record();
512    Dmsg1(90, "session_label record=%x\n", rec);
513    switch (label) {
514    case SOS_LABEL:
515       if (dev->state & ST_TAPE) {
516          dcr->StartBlock = dev->block_num;
517          dcr->StartFile  = dev->file;
518       } else {
519          dcr->StartBlock = (uint32_t)dev->file_addr;
520          dcr->StartFile = (uint32_t)(dev->file_addr >> 32);
521       }
522       break;
523    case EOS_LABEL:
524       if (dev->state & ST_TAPE) {
525          dcr->EndBlock = dev->EndBlock;
526          dcr->EndFile  = dev->EndFile;
527       } else {
528          dcr->EndBlock = (uint32_t)dev->file_addr;
529          dcr->EndFile = (uint32_t)(dev->file_addr >> 32);
530       }
531       break;
532    default:
533       Jmsg1(jcr, M_ABORT, 0, _("Bad session label = %d\n"), label);
534       break;
535    }
536    create_session_label(dcr, rec, label);
537    rec->FileIndex = label;
538
539    /*
540     * We guarantee that the session record can totally fit
541     *  into a block. If not, write the block, and put it in
542     *  the next block. Having the sesssion record totally in
543     *  one block makes reading them much easier (no need to
544     *  read the next block).
545     */
546    if (!can_write_record_to_block(block, rec)) {
547       Dmsg0(100, "Cannot write session label to block.\n");
548       if (!write_block_to_device(dcr)) {
549          Dmsg0(90, "Got session label write_block_to_dev error.\n");
550          /* ****FIXME***** errno is not set here */
551          Jmsg(jcr, M_FATAL, 0, _("Error writing Session label to %s: %s\n"),
552                            dev_vol_name(dev), strerror(errno));
553          free_record(rec);
554          return false;
555       }
556    }
557    if (!write_record_to_block(block, rec)) {
558       Jmsg(jcr, M_FATAL, 0, _("Error writing Session label to %s: %s\n"),
559                         dev_vol_name(dev), strerror(errno));
560       free_record(rec);
561       return false;
562    }
563
564    Dmsg6(20, "Write sesson_label record JobId=%d FI=%s SessId=%d Strm=%s len=%d "
565              "remainder=%d\n", jcr->JobId,
566       FI_to_ascii(rec->FileIndex), rec->VolSessionId,
567       stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
568       rec->remainder);
569
570    free_record(rec);
571    Dmsg2(20, "Leave write_session_label Block=%d File=%d\n",
572       dev->block_num, dev->file);
573    return true;
574 }
575
576 void dump_volume_label(DEVICE *dev)
577 {
578    int dbl = debug_level;
579    uint32_t File;
580    const char *LabelType;
581    char buf[30];
582    struct tm tm;
583    struct date_time dt;
584
585    debug_level = 1;
586    File = dev->file;
587    switch (dev->VolHdr.LabelType) {
588       case PRE_LABEL:
589          LabelType = "PRE_LABEL";
590          break;
591       case VOL_LABEL:
592          LabelType = "VOL_LABEL";
593          break;
594       case EOM_LABEL:
595          LabelType = "EOM_LABEL";
596          break;
597       case SOS_LABEL:
598          LabelType = "SOS_LABEL";
599          break;
600       case EOS_LABEL:
601          LabelType = "EOS_LABEL";
602          break;
603       case EOT_LABEL:
604          goto bail_out;
605       default:
606          LabelType = buf;
607          sprintf(buf, "Unknown %d", dev->VolHdr.LabelType);
608          break;
609    }
610
611
612    Pmsg11(-1, "\nVolume Label:\n"
613 "Id                : %s"
614 "VerNo             : %d\n"
615 "VolName           : %s\n"
616 "PrevVolName       : %s\n"
617 "VolFile           : %d\n"
618 "LabelType         : %s\n"
619 "LabelSize         : %d\n"
620 "PoolName          : %s\n"
621 "MediaType         : %s\n"
622 "PoolType          : %s\n"
623 "HostName          : %s\n"
624 "",
625              dev->VolHdr.Id, dev->VolHdr.VerNum,
626              dev->VolHdr.VolName, dev->VolHdr.PrevVolName,
627              File, LabelType, dev->VolHdr.LabelSize,
628              dev->VolHdr.PoolName, dev->VolHdr.MediaType,
629              dev->VolHdr.PoolType, dev->VolHdr.HostName);
630
631    if (dev->VolHdr.VerNum >= 11) {
632       char dt[50];
633       bstrftime(dt, sizeof(dt), btime_to_unix(dev->VolHdr.label_btime));
634       Pmsg1(-1, "Date label written: %s\n", dt);
635    } else {
636    dt.julian_day_number   = dev->VolHdr.label_date;
637    dt.julian_day_fraction = dev->VolHdr.label_time;
638    tm_decode(&dt, &tm);
639    Pmsg5(-1,
640 "Date label written: %04d-%02d-%02d at %02d:%02d\n",
641       tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min);
642    }
643
644 bail_out:
645    debug_level = dbl;
646 }
647
648 bool unser_session_label(SESSION_LABEL *label, DEV_RECORD *rec)
649 {
650    ser_declare;
651
652    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Session_Label);
653    unser_begin(rec->data, SER_LENGTH_Session_Label);
654    unser_string(label->Id);
655    unser_uint32(label->VerNum);
656    unser_uint32(label->JobId);
657    if (label->VerNum >= 11) {
658       unser_btime(label->write_btime);
659    } else {
660       unser_float64(label->write_date);
661    }
662    unser_float64(label->write_time);
663    unser_string(label->PoolName);
664    unser_string(label->PoolType);
665    unser_string(label->JobName);
666    unser_string(label->ClientName);
667    if (label->VerNum >= 10) {
668       unser_string(label->Job);          /* Unique name of this Job */
669       unser_string(label->FileSetName);
670       unser_uint32(label->JobType);
671       unser_uint32(label->JobLevel);
672    }
673    if (label->VerNum >= 11) {
674       unser_string(label->FileSetMD5);
675    } else {
676       label->FileSetMD5[0] = 0;
677    }
678    if (rec->FileIndex == EOS_LABEL) {
679       unser_uint32(label->JobFiles);
680       unser_uint64(label->JobBytes);
681       unser_uint32(label->StartBlock);
682       unser_uint32(label->EndBlock);
683       unser_uint32(label->StartFile);
684       unser_uint32(label->EndFile);
685       unser_uint32(label->JobErrors);
686       if (label->VerNum >= 11) {
687          unser_uint32(label->JobStatus);
688       } else {
689          label->JobStatus = JS_Terminated; /* kludge */
690       }
691    }
692    return true;
693 }
694
695
696 static void dump_session_label(DEV_RECORD *rec, const char *type)
697 {
698    int dbl;
699    struct date_time dt;
700    struct tm tm;
701    SESSION_LABEL label;
702    char ec1[30], ec2[30], ec3[30], ec4[30], ec5[30], ec6[30], ec7[30];
703
704    unser_session_label(&label, rec);
705    dbl = debug_level;
706    debug_level = 1;
707    Pmsg7(-1, "\n%s Record:\n"
708 "JobId             : %d\n"
709 "VerNum            : %d\n"
710 "PoolName          : %s\n"
711 "PoolType          : %s\n"
712 "JobName           : %s\n"
713 "ClientName        : %s\n"
714 "",    type, label.JobId, label.VerNum,
715       label.PoolName, label.PoolType,
716       label.JobName, label.ClientName);
717
718    if (label.VerNum >= 10) {
719       Pmsg4(-1, ""
720 "Job (unique name) : %s\n"
721 "FileSet           : %s\n"
722 "JobType           : %c\n"
723 "JobLevel          : %c\n"
724 "", label.Job, label.FileSetName, label.JobType, label.JobLevel);
725    }
726
727    if (rec->FileIndex == EOS_LABEL) {
728       Pmsg8(-1, ""
729 "JobFiles          : %s\n"
730 "JobBytes          : %s\n"
731 "StartBlock        : %s\n"
732 "EndBlock          : %s\n"
733 "StartFile         : %s\n"
734 "EndFile           : %s\n"
735 "JobErrors         : %s\n"
736 "JobStatus         : %c\n"
737 "",
738          edit_uint64_with_commas(label.JobFiles, ec1),
739          edit_uint64_with_commas(label.JobBytes, ec2),
740          edit_uint64_with_commas(label.StartBlock, ec3),
741          edit_uint64_with_commas(label.EndBlock, ec4),
742          edit_uint64_with_commas(label.StartFile, ec5),
743          edit_uint64_with_commas(label.EndFile, ec6),
744          edit_uint64_with_commas(label.JobErrors, ec7),
745          label.JobStatus);
746    }
747    if (label.VerNum >= 11) {
748       char dt[50];
749       bstrftime(dt, sizeof(dt), btime_to_unix(label.write_btime));
750       Pmsg1(-1, _("Date written      : %s\n"), dt);
751    } else {
752       dt.julian_day_number   = label.write_date;
753       dt.julian_day_fraction = label.write_time;
754       tm_decode(&dt, &tm);
755       Pmsg5(-1, _(""
756 "Date written      : %04d-%02d-%02d at %02d:%02d\n"),
757       tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min);
758    }
759
760    debug_level = dbl;
761 }
762
763 void dump_label_record(DEVICE *dev, DEV_RECORD *rec, int verbose)
764 {
765    const char *type;
766    int dbl;
767
768    dbl = debug_level;
769    debug_level = 1;
770    switch (rec->FileIndex) {
771    case PRE_LABEL:
772       type = _("Fresh Volume");
773       break;
774    case VOL_LABEL:
775       type = _("Volume");
776       break;
777    case SOS_LABEL:
778       type = _("Begin Job Session");
779       break;
780    case EOS_LABEL:
781       type = _("End Job Session");
782       break;
783    case EOM_LABEL:
784       type = _("End of Media");
785       break;
786    case EOT_LABEL:
787       type = ("End of Tape");
788       break;
789    default:
790       type = _("Unknown");
791       break;
792    }
793    if (verbose) {
794       switch (rec->FileIndex) {
795       case PRE_LABEL:
796       case VOL_LABEL:
797          unser_volume_label(dev, rec);
798          dump_volume_label(dev);
799          break;
800       case SOS_LABEL:
801          dump_session_label(rec, type);
802          break;
803       case EOS_LABEL:
804          dump_session_label(rec, type);
805          break;
806       case EOM_LABEL:
807          Pmsg5(-1, "%s Record: SessId=%d SessTime=%d JobId=%d DataLen=%d\n",
808             type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
809          break;
810       case EOT_LABEL:
811          Pmsg0(-1, _("End of physical tape.\n"));
812          break;
813       default:
814          Pmsg5(-1, "%s Record: SessId=%d SessTime=%d JobId=%d DataLen=%d\n",
815             type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
816          break;
817       }
818    } else {
819       SESSION_LABEL label;
820       switch (rec->FileIndex) {
821       case SOS_LABEL:
822          unser_session_label(&label, rec);
823          Pmsg6(-1, "%s Record: SessId=%d SessTime=%d JobId=%d Level=%c Type=%c\n",
824             type, rec->VolSessionId, rec->VolSessionTime, rec->Stream,
825             label.JobLevel, label.JobType);
826          break;
827       case EOS_LABEL:
828          char ed1[30], ed2[30];
829          unser_session_label(&label, rec);
830          Pmsg6(-1, "%s Record: SessId=%d SessTime=%d JobId=%d Level=%c Type=%c\n",
831             type, rec->VolSessionId, rec->VolSessionTime, rec->Stream,
832             label.JobLevel, label.JobType);
833          Pmsg4(-1, "   Files=%s Bytes=%s Errors=%d Status=%c\n",
834             edit_uint64_with_commas(label.JobFiles, ed1),
835             edit_uint64_with_commas(label.JobBytes, ed2),
836             label.JobErrors, (char)label.JobStatus);
837          break;
838       case EOM_LABEL:
839       case PRE_LABEL:
840       case VOL_LABEL:
841       default:
842          Pmsg5(-1, "%s Record: SessId=%d SessTime=%d JobId=%d DataLen=%d\n",
843       type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
844          break;
845       case EOT_LABEL:
846          break;
847       }
848    }
849    debug_level = dbl;
850 }