]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/catreq.c
Add compress/decompress of Object Record data
[bacula/bacula] / bacula / src / dird / catreq.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2001-2010 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *
30  *   Bacula Director -- catreq.c -- handles the message channel
31  *    catalog request from the Storage daemon.
32  *
33  *     Kern Sibbald, March MMI
34  *
35  *    This routine runs as a thread and must be thread reentrant.
36  *
37  *  Basic tasks done here:
38  *      Handle Catalog services.
39  *
40  */
41
42 #include "bacula.h"
43 #include "dird.h"
44 #include "findlib/find.h"
45
46 /*
47  * Handle catalog request
48  *  For now, we simply return next Volume to be used
49  */
50
51 /* Requests from the Storage daemon */
52 static char Find_media[] = "CatReq Job=%127s FindMedia=%d pool_name=%127s media_type=%127s\n";
53 static char Get_Vol_Info[] = "CatReq Job=%127s GetVolInfo VolName=%127s write=%d\n";
54
55 static char Update_media[] = "CatReq Job=%127s UpdateMedia VolName=%s"
56    " VolJobs=%u VolFiles=%u VolBlocks=%u VolBytes=%lld VolMounts=%u"
57    " VolErrors=%u VolWrites=%u MaxVolBytes=%lld EndTime=%lld VolStatus=%10s"
58    " Slot=%d relabel=%d InChanger=%d VolReadTime=%lld VolWriteTime=%lld"
59    " VolFirstWritten=%lld VolParts=%u\n";
60
61 static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia "
62    " FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u "
63    " StartBlock=%u EndBlock=%u Copy=%d Strip=%d MediaId=%" lld "\n";
64
65
66 /* Responses  sent to Storage daemon */
67 static char OK_media[] = "1000 OK VolName=%s VolJobs=%u VolFiles=%u"
68    " VolBlocks=%u VolBytes=%s VolMounts=%u VolErrors=%u VolWrites=%u"
69    " MaxVolBytes=%s VolCapacityBytes=%s VolStatus=%s Slot=%d"
70    " MaxVolJobs=%u MaxVolFiles=%u InChanger=%d VolReadTime=%s"
71    " VolWriteTime=%s EndFile=%u EndBlock=%u VolParts=%u LabelType=%d"
72    " MediaId=%s\n";
73
74 static char OK_create[] = "1000 OK CreateJobMedia\n";
75
76
77 static int send_volume_info_to_storage_daemon(JCR *jcr, BSOCK *sd, MEDIA_DBR *mr)
78 {
79    int stat;
80    char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50], ed6[50];
81
82    jcr->MediaId = mr->MediaId;
83    pm_strcpy(jcr->VolumeName, mr->VolumeName);
84    bash_spaces(mr->VolumeName);
85    stat = sd->fsend(OK_media, mr->VolumeName, mr->VolJobs,
86       mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
87       mr->VolMounts, mr->VolErrors, mr->VolWrites,
88       edit_uint64(mr->MaxVolBytes, ed2),
89       edit_uint64(mr->VolCapacityBytes, ed3),
90       mr->VolStatus, mr->Slot, mr->MaxVolJobs, mr->MaxVolFiles,
91       mr->InChanger,
92       edit_int64(mr->VolReadTime, ed4),
93       edit_int64(mr->VolWriteTime, ed5),
94       mr->EndFile, mr->EndBlock,
95       mr->VolParts,
96       mr->LabelType,
97       edit_uint64(mr->MediaId, ed6));
98    unbash_spaces(mr->VolumeName);
99    Dmsg2(100, "Vol Info for %s: %s", jcr->Job, sd->msg);
100    return stat;
101 }
102
103 void catalog_request(JCR *jcr, BSOCK *bs)
104 {
105    MEDIA_DBR mr, sdmr;
106    JOBMEDIA_DBR jm;
107    char Job[MAX_NAME_LENGTH];
108    char pool_name[MAX_NAME_LENGTH];
109    int index, ok, label, writing;
110    POOLMEM *omsg;
111    POOL_DBR pr;
112    uint32_t Stripe, Copy;
113    uint64_t MediaId;
114    utime_t VolFirstWritten;
115    utime_t VolLastWritten;
116
117    memset(&mr, 0, sizeof(mr));
118    memset(&sdmr, 0, sizeof(sdmr));
119    memset(&jm, 0, sizeof(jm));
120    Dsm_check(100);      
121
122    /*
123     * Request to find next appendable Volume for this Job
124     */
125    Dmsg1(100, "catreq %s", bs->msg);
126    if (!jcr->db) {
127       omsg = get_memory(bs->msglen+1);
128       pm_strcpy(omsg, bs->msg);
129       bs->fsend(_("1990 Invalid Catalog Request: %s"), omsg);    
130       Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog request; DB not open: %s"), omsg);
131       free_memory(omsg);
132       return;
133    }
134    /*
135     * Find next appendable medium for SD
136     */
137    if (sscanf(bs->msg, Find_media, &Job, &index, &pool_name, &mr.MediaType) == 4) {
138       memset(&pr, 0, sizeof(pr));
139       bstrncpy(pr.Name, pool_name, sizeof(pr.Name));
140       unbash_spaces(pr.Name);
141       ok = db_get_pool_record(jcr, jcr->db, &pr);
142       if (ok) {
143          mr.PoolId = pr.PoolId;
144          mr.StorageId = jcr->wstore->StorageId;
145          mr.ScratchPoolId = pr.ScratchPoolId;
146          ok = find_next_volume_for_append(jcr, &mr, index, fnv_create_vol, fnv_prune);
147          Dmsg3(050, "find_media ok=%d idx=%d vol=%s\n", ok, index, mr.VolumeName);
148       }
149       /*
150        * Send Find Media response to Storage daemon
151        */
152       if (ok) {
153          send_volume_info_to_storage_daemon(jcr, bs, &mr);
154       } else {
155          bs->fsend(_("1901 No Media.\n"));
156          Dmsg0(500, "1901 No Media.\n");
157       }
158
159    /*
160     * Request to find specific Volume information
161     */
162    } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
163       Dmsg1(100, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
164       /*
165        * Find the Volume
166        */
167       unbash_spaces(mr.VolumeName);
168       if (db_get_media_record(jcr, jcr->db, &mr)) {
169          const char *reason = NULL;           /* detailed reason for rejection */
170          /*
171           * If we are reading, accept any volume (reason == NULL)
172           * If we are writing, check if the Volume is valid
173           *   for this job, and do a recycle if necessary
174           */
175          if (writing) {
176             /*
177              * SD wants to write this Volume, so make
178              *   sure it is suitable for this job, i.e.
179              *   Pool matches, and it is either Append or Recycle
180              *   and Media Type matches and Pool allows any volume.
181              */
182             if (mr.PoolId != jcr->jr.PoolId) {
183                reason = _("not in Pool");
184             } else if (strcmp(mr.MediaType, jcr->wstore->media_type) != 0) {
185                reason = _("not correct MediaType");
186             } else {
187                /*
188                 * Now try recycling if necessary
189                 *   reason set non-NULL if we cannot use it
190                 */
191                check_if_volume_valid_or_recyclable(jcr, &mr, &reason);
192             }
193          }
194          if (!reason && mr.Enabled != 1) {
195             reason = _("is not Enabled");
196          }
197          if (reason == NULL) {
198             /*
199              * Send Find Media response to Storage daemon
200              */
201             send_volume_info_to_storage_daemon(jcr, bs, &mr);
202          } else {
203             /* Not suitable volume */
204             bs->fsend(_("1998 Volume \"%s\" status is %s, %s.\n"), mr.VolumeName,
205                mr.VolStatus, reason);
206          }
207
208       } else {
209          bs->fsend(_("1997 Volume \"%s\" not in catalog.\n"), mr.VolumeName);
210          Dmsg1(100, "1997 Volume \"%s\" not in catalog.\n", mr.VolumeName);
211       }
212
213    /*
214     * Request to update Media record. Comes typically at the end
215     *  of a Storage daemon Job Session, when labeling/relabeling a
216     *  Volume, or when an EOF mark is written.
217     */
218    } else if (sscanf(bs->msg, Update_media, &Job, &sdmr.VolumeName,
219       &sdmr.VolJobs, &sdmr.VolFiles, &sdmr.VolBlocks, &sdmr.VolBytes,
220       &sdmr.VolMounts, &sdmr.VolErrors, &sdmr.VolWrites, &sdmr.MaxVolBytes,
221       &VolLastWritten, &sdmr.VolStatus, &sdmr.Slot, &label, &sdmr.InChanger,
222       &sdmr.VolReadTime, &sdmr.VolWriteTime, &VolFirstWritten,
223       &sdmr.VolParts) == 19) {
224
225       db_lock(jcr->db);
226       Dmsg3(400, "Update media %s oldStat=%s newStat=%s\n", sdmr.VolumeName,
227          mr.VolStatus, sdmr.VolStatus);
228       bstrncpy(mr.VolumeName, sdmr.VolumeName, sizeof(mr.VolumeName)); /* copy Volume name */
229       unbash_spaces(mr.VolumeName);
230       if (!db_get_media_record(jcr, jcr->db, &mr)) {
231          Jmsg(jcr, M_ERROR, 0, _("Unable to get Media record for Volume %s: ERR=%s\n"),
232               mr.VolumeName, db_strerror(jcr->db));
233          bs->fsend(_("1991 Catalog Request for vol=%s failed: %s"),
234             mr.VolumeName, db_strerror(jcr->db));
235          db_unlock(jcr->db);
236          return;
237       }
238       /* Set first written time if this is first job */
239       if (mr.FirstWritten == 0) {
240          if (VolFirstWritten == 0) {
241             mr.FirstWritten = jcr->start_time;   /* use Job start time as first write */
242          } else {
243             mr.FirstWritten = VolFirstWritten;
244          }
245          mr.set_first_written = true;
246       }
247       /* If we just labeled the tape set time */
248       if (label || mr.LabelDate == 0) {
249          mr.LabelDate = jcr->start_time;
250          mr.set_label_date = true;
251          if (mr.InitialWrite == 0) {
252             mr.InitialWrite = jcr->start_time;
253          }
254          Dmsg2(400, "label=%d labeldate=%d\n", label, mr.LabelDate);
255       } else {
256          /*
257           * Insanity check for VolFiles get set to a smaller value
258           */
259          if (sdmr.VolFiles < mr.VolFiles) {
260             Jmsg(jcr, M_FATAL, 0, _("Volume Files at %u being set to %u"
261                  " for Volume \"%s\". This is incorrect.\n"),
262                mr.VolFiles, sdmr.VolFiles, mr.VolumeName);
263             bs->fsend(_("1992 Update Media error. VolFiles=%u, CatFiles=%u\n"),
264                sdmr.VolFiles, mr.VolFiles);
265             db_unlock(jcr->db);
266             return;
267          }
268       }
269       Dmsg2(400, "Update media: BefVolJobs=%u After=%u\n", mr.VolJobs, sdmr.VolJobs);
270
271       /*
272        * Check if the volume has been written by the job, 
273        * and update the LastWritten field if needed.
274        */
275       if (mr.VolBlocks != sdmr.VolBlocks && VolLastWritten != 0) {
276          mr.LastWritten = VolLastWritten;
277       }
278
279       /*
280        * Update to point to the last device used to write the Volume.
281        *   However, do so only if we are writing the tape, i.e.
282        *   the number of VolWrites has increased.
283        */
284       if (jcr->wstore && jcr->wstore->StorageId && sdmr.VolWrites > mr.VolWrites) {
285          Dmsg2(050, "Update StorageId old=%d new=%d\n",
286                mr.StorageId, jcr->wstore->StorageId);
287          if (jcr->wstore->StorageId == 0) {
288             Jmsg(jcr, M_ERROR, 0, _("Attempt to set StorageId to zero.\n"));
289          } else {
290             mr.StorageId = jcr->wstore->StorageId;
291          }
292       }
293
294       /* Copy updated values to original media record */
295       mr.VolJobs      = sdmr.VolJobs;
296       mr.VolFiles     = sdmr.VolFiles;
297       mr.VolBlocks    = sdmr.VolBlocks;
298       mr.VolBytes     = sdmr.VolBytes;
299       mr.VolMounts    = sdmr.VolMounts;
300       mr.VolErrors    = sdmr.VolErrors;
301       mr.VolWrites    = sdmr.VolWrites;
302       mr.Slot         = sdmr.Slot;
303       mr.InChanger    = sdmr.InChanger;
304       mr.VolParts     = sdmr.VolParts;
305       bstrncpy(mr.VolStatus, sdmr.VolStatus, sizeof(mr.VolStatus));
306       if (sdmr.VolReadTime >= 0) { 
307          mr.VolReadTime  = sdmr.VolReadTime;
308       }
309       if (sdmr.VolWriteTime >= 0) {
310          mr.VolWriteTime = sdmr.VolWriteTime;
311       }
312
313       Dmsg2(400, "db_update_media_record. Stat=%s Vol=%s\n", mr.VolStatus, mr.VolumeName);
314       /*
315        * Update the database, then before sending the response to the
316        *  SD, check if the Volume has expired.
317        */
318       if (!db_update_media_record(jcr, jcr->db, &mr)) {
319          Jmsg(jcr, M_FATAL, 0, _("Catalog error updating Media record. %s"),
320             db_strerror(jcr->db));
321          bs->fsend(_("1993 Update Media error\n"));
322          Dmsg0(400, "send error\n");
323       } else {
324          (void)has_volume_expired(jcr, &mr);
325          send_volume_info_to_storage_daemon(jcr, bs, &mr);
326       }
327       db_unlock(jcr->db);
328
329    /*
330     * Request to create a JobMedia record
331     */
332    } else if (sscanf(bs->msg, Create_job_media, &Job,
333       &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
334       &jm.StartBlock, &jm.EndBlock, &Copy, &Stripe, &MediaId) == 10) {
335
336       if (jcr->mig_jcr) {
337          jm.JobId = jcr->mig_jcr->JobId;
338       } else {
339          jm.JobId = jcr->JobId;
340       }
341       jm.MediaId = MediaId;
342       Dmsg6(400, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
343          jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
344       if (!db_create_jobmedia_record(jcr, jcr->db, &jm)) {
345          Jmsg(jcr, M_FATAL, 0, _("Catalog error creating JobMedia record. %s"),
346             db_strerror(jcr->db));
347          bs->fsend(_("1992 Create JobMedia error\n"));
348       } else {
349          Dmsg0(400, "JobMedia record created\n");
350          bs->fsend(OK_create);
351       }
352
353    } else {
354       omsg = get_memory(bs->msglen+1);
355       pm_strcpy(omsg, bs->msg);
356       bs->fsend(_("1990 Invalid Catalog Request: %s"), omsg);
357       Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog request: %s"), omsg);
358       free_memory(omsg);
359    }
360    Dmsg1(400, ">CatReq response: %s", bs->msg);
361    Dmsg1(400, "Leave catreq jcr 0x%x\n", jcr);
362    return;
363 }
364
365 /*
366  * Note, we receive the whole attribute record, but we select out only the stat
367  * packet, VolSessionId, VolSessionTime, FileIndex, file type, and file name to
368  * store in the catalog.
369  */
370 static void update_attribute(JCR *jcr, char *msg, int32_t msglen)
371 {
372    unser_declare;
373    uint32_t VolSessionId, VolSessionTime;
374    int32_t Stream;
375    uint32_t FileIndex;
376    char *p;
377    int len;
378    char *fname, *attr;
379    ATTR_DBR *ar = NULL;
380    uint32_t reclen;
381
382    /* Start transaction allocates jcr->attr and jcr->ar if needed */
383    db_start_transaction(jcr, jcr->db);     /* start transaction if not already open */
384    ar = jcr->ar;      
385
386    /*
387     * Start by scanning directly in the message buffer to get Stream   
388     *  there may be a cached attr so we cannot yet write into
389     *  jcr->attr or jcr->ar  
390     */
391    p = msg;
392    skip_nonspaces(&p);                /* UpdCat */
393    skip_spaces(&p);
394    skip_nonspaces(&p);                /* Job=nnn */
395    skip_spaces(&p);
396    skip_nonspaces(&p);                /* "FileAttributes" */
397    p += 1;
398    /* The following "SD header" fields are serialized */
399    unser_begin(p, 0);
400    unser_uint32(VolSessionId);        /* VolSessionId */
401    unser_uint32(VolSessionTime);      /* VolSessionTime */
402    unser_int32(FileIndex);            /* FileIndex */
403    unser_int32(Stream);               /* Stream */
404    unser_uint32(reclen);              /* Record length */
405    p += unser_length(p);              /* Raw record follows */
406
407    /**
408     * At this point p points to the raw record, which varies according
409     *  to what kind of a record (Stream) was sent.  Note, the integer
410     *  fields at the beginning of these "raw" records are in ASCII with
411     *  spaces between them so one can use scanf or manual scanning to
412     *  extract the fields.
413     *
414     * File Attributes
415     *   File_index
416     *   File type
417     *   Filename (full path)
418     *   Encoded attributes
419     *   Link name (if type==FT_LNK or FT_LNKSAVED)
420     *   Encoded extended-attributes (for Win32)
421     *
422     * Restore Object
423     *   File_index
424     *   File_type
425     *   Object_index
426     *   Object_len (possibly compressed)
427     *   Object_full_len (not compressed)
428     *   Object_compression
429     *   Plugin_name
430     *   Object_name
431     *   Binary Object data
432     */
433
434    Dmsg1(400, "UpdCat msg=%s\n", msg);
435    Dmsg5(400, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d reclen=%d\n",
436       VolSessionId, VolSessionTime, FileIndex, Stream, reclen);
437
438    if (Stream == STREAM_UNIX_ATTRIBUTES || Stream == STREAM_UNIX_ATTRIBUTES_EX) {
439       if (jcr->cached_attribute) {
440          Dmsg2(400, "Cached attr. Stream=%d fname=%s\n", ar->Stream, ar->fname);
441          if (!db_create_attributes_record(jcr, jcr->db, ar)) {
442             Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
443          }
444          jcr->cached_attribute = false;
445       }
446       /* Any cached attr is flushed so we can reuse jcr->attr and jcr->ar */
447       jcr->attr = check_pool_memory_size(jcr->attr, msglen);
448       memcpy(jcr->attr, msg, msglen);
449       p = jcr->attr - msg + p;    /* point p into jcr->attr */
450       skip_nonspaces(&p);             /* skip FileIndex */
451       skip_spaces(&p);
452       ar->FileType = str_to_int32(p); 
453       skip_nonspaces(&p);             /* skip FileType */
454       skip_spaces(&p);
455       fname = p;
456       len = strlen(fname);        /* length before attributes */
457       attr = &fname[len+1];
458
459       Dmsg2(400, "dird<stored: stream=%d %s\n", Stream, fname);
460       Dmsg1(400, "dird<stored: attr=%s\n", attr);
461       ar->attr = attr;
462       ar->fname = fname;
463       if (ar->FileType == FT_DELETED) {
464          ar->FileIndex = 0;     /* special value */
465       } else {
466          ar->FileIndex = FileIndex;
467       }
468       ar->Stream = Stream;
469       ar->link = NULL;
470       if (jcr->mig_jcr) {
471          ar->JobId = jcr->mig_jcr->JobId;
472       } else {
473          ar->JobId = jcr->JobId;
474       }
475       ar->Digest = NULL;
476       ar->DigestType = CRYPTO_DIGEST_NONE;
477       jcr->cached_attribute = true;
478
479       Dmsg2(400, "dird<filed: stream=%d %s\n", Stream, fname);
480       Dmsg1(400, "dird<filed: attr=%s\n", attr);
481
482    } else if (Stream == STREAM_RESTORE_OBJECT) {
483       ROBJECT_DBR ro;
484
485       memset(&ro, 0, sizeof(ro));
486       ro.Stream = Stream;
487       ro.FileIndex = FileIndex;
488       if (jcr->mig_jcr) {
489          ro.JobId = jcr->mig_jcr->JobId;
490       } else {
491          ro.JobId = jcr->JobId;
492       }
493
494       Dmsg1(100, "Robj=%s\n", p);
495       
496       skip_nonspaces(&p);                  /* skip FileIndex */
497       skip_spaces(&p);
498       ro.FileType = str_to_int32(p);        /* FileType */
499       skip_nonspaces(&p);
500       skip_spaces(&p);
501       ro.object_index = str_to_int32(p);    /* Object Index */
502       skip_nonspaces(&p);
503       skip_spaces(&p);
504       ro.object_len = str_to_int32(p);      /* object length possibly compressed */
505       skip_nonspaces(&p);                  
506       skip_spaces(&p);
507       ro.object_full_len = str_to_int32(p); /* uncompressed object length */
508       skip_nonspaces(&p);
509       skip_spaces(&p);
510       ro.object_compression = str_to_int32(p); /* compression */
511       skip_nonspaces(&p);
512       skip_spaces(&p);
513
514       ro.plugin_name = p;                      /* point to plugin name */
515       len = strlen(ro.plugin_name);
516       ro.object_name = &ro.plugin_name[len+1]; /* point to object name */
517       len = strlen(ro.object_name);
518       ro.object = &ro.object_name[len+1];      /* point to object */
519       ro.object[ro.object_len] = 0;            /* add zero for those who attempt printing */
520       Dmsg7(100, "oname=%s stream=%d FT=%d FI=%d JobId=%d, obj_len=%d\nobj=\"%s\"\n",
521          ro.object_name, ro.Stream, ro.FileType, ro.FileIndex, ro.JobId,
522          ro.object_len, ro.object);
523       /* Send it */
524       if (!db_create_restore_object_record(jcr, jcr->db, &ro)) {
525          Jmsg1(jcr, M_FATAL, 0, _("Restore object create error. %s"), db_strerror(jcr->db));
526       }
527
528    } else if (crypto_digest_stream_type(Stream) != CRYPTO_DIGEST_NONE) {
529       fname = p;
530       if (ar->FileIndex != FileIndex) {
531          Jmsg(jcr, M_WARNING, 0, _("Got %s but not same File as attributes\n"), stream_to_ascii(Stream));
532       } else {
533          /* Update digest in catalog */
534          char digestbuf[BASE64_SIZE(CRYPTO_DIGEST_MAX_SIZE)];
535          int len = 0;
536          int type = CRYPTO_DIGEST_NONE;
537
538          switch(Stream) {
539          case STREAM_MD5_DIGEST:
540             len = CRYPTO_DIGEST_MD5_SIZE;
541             type = CRYPTO_DIGEST_MD5;
542             break;
543          case STREAM_SHA1_DIGEST:
544             len = CRYPTO_DIGEST_SHA1_SIZE;
545             type = CRYPTO_DIGEST_SHA1;
546             break;
547          case STREAM_SHA256_DIGEST:
548             len = CRYPTO_DIGEST_SHA256_SIZE;
549             type = CRYPTO_DIGEST_SHA256;
550             break;
551          case STREAM_SHA512_DIGEST:
552             len = CRYPTO_DIGEST_SHA512_SIZE;
553             type = CRYPTO_DIGEST_SHA512;
554             break;
555          default:
556             /* Never reached ... */
557             Jmsg(jcr, M_ERROR, 0, _("Catalog error updating file digest. Unsupported digest stream type: %d"),
558                  Stream);
559          }
560
561          bin_to_base64(digestbuf, sizeof(digestbuf), fname, len, true);
562          Dmsg3(400, "DigestLen=%d Digest=%s type=%d\n", strlen(digestbuf),
563                digestbuf, Stream);
564          if (jcr->cached_attribute) {
565             ar->Digest = digestbuf;
566             ar->DigestType = type;
567             Dmsg2(400, "Cached attr with digest. Stream=%d fname=%s\n",
568                   ar->Stream, ar->fname);
569
570             /* Update BaseFile table */
571             if (!db_create_attributes_record(jcr, jcr->db, ar)) {
572                Jmsg1(jcr, M_FATAL, 0, _("attribute create error. %s"),
573                         db_strerror(jcr->db));
574             }
575             jcr->cached_attribute = false; 
576          } else {
577             if (!db_add_digest_to_file_record(jcr, jcr->db, ar->FileId, digestbuf, type)) {
578                Jmsg(jcr, M_ERROR, 0, _("Catalog error updating file digest. %s"),
579                   db_strerror(jcr->db));
580             }
581          }
582       }
583    }
584 }
585
586 /*
587  * Update File Attributes in the catalog with data
588  *  sent by the Storage daemon.
589  */
590 void catalog_update(JCR *jcr, BSOCK *bs)
591 {
592    if (!jcr->pool->catalog_files) {
593       return;                         /* user disabled cataloging */
594    }
595    if (jcr->is_job_canceled()) {
596       goto bail_out;
597    }
598    if (!jcr->db) {
599       POOLMEM *omsg = get_memory(bs->msglen+1);
600       pm_strcpy(omsg, bs->msg);
601       bs->fsend(_("1994 Invalid Catalog Update: %s"), omsg);    
602       Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog Update; DB not open: %s"), omsg);
603       free_memory(omsg);
604       goto bail_out;
605    }
606    update_attribute(jcr, bs->msg, bs->msglen);
607
608 bail_out:
609    if (jcr->is_job_canceled()) {
610       cancel_storage_daemon_job(jcr);
611    }
612 }
613
614 /*
615  * Update File Attributes in the catalog with data read from
616  * the storage daemon spool file. We receive the filename and
617  * we try to read it.
618  */
619 bool despool_attributes_from_file(JCR *jcr, const char *file)
620 {
621    bool ret=false;
622    int32_t pktsiz;
623    size_t nbytes;
624    ssize_t last = 0, size = 0;
625    int count = 0;
626    int32_t msglen;                    /* message length */
627    POOLMEM *msg = get_pool_memory(PM_MESSAGE);
628    FILE *spool_fd=NULL;
629
630    Dmsg0(100, "Begin despool_attributes_from_file\n");
631
632    if (jcr->is_job_canceled() || !jcr->pool->catalog_files || !jcr->db) {
633       goto bail_out;                  /* user disabled cataloging */
634    }
635
636    spool_fd = fopen(file, "rb");
637    if (!spool_fd) {
638       Dmsg0(100, "cancel despool_attributes_from_file\n");
639       /* send an error message */
640       goto bail_out;
641    }
642 #if defined(HAVE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
643    posix_fadvise(fileno(spool_fd), 0, 0, POSIX_FADV_WILLNEED);
644 #endif
645
646    while (fread((char *)&pktsiz, 1, sizeof(int32_t), spool_fd) ==
647           sizeof(int32_t)) {
648       size += sizeof(int32_t);
649       msglen = ntohl(pktsiz);
650       if (msglen > 0) {
651          if (msglen > (int32_t) sizeof_pool_memory(msg)) {
652             msg = realloc_pool_memory(msg, msglen + 1);
653          }
654          nbytes = fread(msg, 1, msglen, spool_fd);
655          if (nbytes != (size_t) msglen) {
656             berrno be;
657             Dmsg2(400, "nbytes=%d msglen=%d\n", nbytes, msglen);
658             Qmsg1(jcr, M_FATAL, 0, _("fread attr spool error. ERR=%s\n"),
659                   be.bstrerror());
660             goto bail_out;
661          }
662          size += nbytes;
663          if ((++count & 0x3F) == 0) {
664             last = size;
665          }
666       }
667       if (!jcr->is_job_canceled()) {
668          update_attribute(jcr, msg, msglen);
669          if (jcr->is_job_canceled()) {
670             goto bail_out;
671          }
672       }
673    }
674    if (ferror(spool_fd)) {
675       berrno be;
676       Qmsg1(jcr, M_FATAL, 0, _("fread attr spool error. ERR=%s\n"),
677             be.bstrerror());
678       goto bail_out;
679    }
680    ret = true;
681
682 bail_out:
683    if (spool_fd) {
684       fclose(spool_fd);
685    }
686
687    if (jcr->is_job_canceled()) {
688       cancel_storage_daemon_job(jcr);
689    }
690
691    free_pool_memory(msg);
692    Dmsg1(100, "End despool_attributes_from_file ret=%i\n", ret);
693    return ret;
694 }