]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/catreq.c
Merge branch 'master' of ssh://bacula.git.sourceforge.net/gitroot/bacula/bacula
[bacula/bacula] / bacula / src / dird / catreq.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2001-2008 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  *   Version $Id$
41  */
42
43 #include "bacula.h"
44 #include "dird.h"
45 #include "findlib/find.h"
46
47 /*
48  * Handle catalog request
49  *  For now, we simply return next Volume to be used
50  */
51
52 /* Requests from the Storage daemon */
53 static char Find_media[] = "CatReq Job=%127s FindMedia=%d pool_name=%127s media_type=%127s\n";
54 static char Get_Vol_Info[] = "CatReq Job=%127s GetVolInfo VolName=%127s write=%d\n";
55
56 static char Update_media[] = "CatReq Job=%127s UpdateMedia VolName=%s"
57    " VolJobs=%u VolFiles=%u VolBlocks=%u VolBytes=%lld VolMounts=%u"
58    " VolErrors=%u VolWrites=%u MaxVolBytes=%lld EndTime=%lld VolStatus=%10s"
59    " Slot=%d relabel=%d InChanger=%d VolReadTime=%lld VolWriteTime=%lld"
60    " VolFirstWritten=%lld VolParts=%u\n";
61
62 static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia "
63    " FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u "
64    " StartBlock=%u EndBlock=%u Copy=%d Strip=%d MediaId=%" lld "\n";
65
66
67 /* Responses  sent to Storage daemon */
68 static char OK_media[] = "1000 OK VolName=%s VolJobs=%u VolFiles=%u"
69    " VolBlocks=%u VolBytes=%s VolMounts=%u VolErrors=%u VolWrites=%u"
70    " MaxVolBytes=%s VolCapacityBytes=%s VolStatus=%s Slot=%d"
71    " MaxVolJobs=%u MaxVolFiles=%u InChanger=%d VolReadTime=%s"
72    " VolWriteTime=%s EndFile=%u EndBlock=%u VolParts=%u LabelType=%d"
73    " MediaId=%s\n";
74
75 static char OK_create[] = "1000 OK CreateJobMedia\n";
76
77
78 static int send_volume_info_to_storage_daemon(JCR *jcr, BSOCK *sd, MEDIA_DBR *mr)
79 {
80    int stat;
81    char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50], ed6[50];
82
83    jcr->MediaId = mr->MediaId;
84    pm_strcpy(jcr->VolumeName, mr->VolumeName);
85    bash_spaces(mr->VolumeName);
86    stat = sd->fsend(OK_media, mr->VolumeName, mr->VolJobs,
87       mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
88       mr->VolMounts, mr->VolErrors, mr->VolWrites,
89       edit_uint64(mr->MaxVolBytes, ed2),
90       edit_uint64(mr->VolCapacityBytes, ed3),
91       mr->VolStatus, mr->Slot, mr->MaxVolJobs, mr->MaxVolFiles,
92       mr->InChanger,
93       edit_int64(mr->VolReadTime, ed4),
94       edit_int64(mr->VolWriteTime, ed5),
95       mr->EndFile, mr->EndBlock,
96       mr->VolParts,
97       mr->LabelType,
98       edit_uint64(mr->MediaId, ed6));
99    unbash_spaces(mr->VolumeName);
100    Dmsg2(100, "Vol Info for %s: %s", jcr->Job, sd->msg);
101    return stat;
102 }
103
104 void catalog_request(JCR *jcr, BSOCK *bs)
105 {
106    MEDIA_DBR mr, sdmr;
107    JOBMEDIA_DBR jm;
108    char Job[MAX_NAME_LENGTH];
109    char pool_name[MAX_NAME_LENGTH];
110    int index, ok, label, writing;
111    POOLMEM *omsg;
112    POOL_DBR pr;
113    uint32_t Stripe;
114    uint64_t MediaId;
115    utime_t VolFirstWritten;
116    utime_t VolLastWritten;
117
118    memset(&mr, 0, sizeof(mr));
119    memset(&sdmr, 0, sizeof(sdmr));
120    memset(&jm, 0, sizeof(jm));
121    Dsm_check(100);      
122
123    /*
124     * Request to find next appendable Volume for this Job
125     */
126    Dmsg1(100, "catreq %s", bs->msg);
127    if (!jcr->db) {
128       omsg = get_memory(bs->msglen+1);
129       pm_strcpy(omsg, bs->msg);
130       bs->fsend(_("1990 Invalid Catalog Request: %s"), omsg);    
131       Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog request; DB not open: %s"), omsg);
132       free_memory(omsg);
133       return;
134    }
135    /*
136     * Find next appendable medium for SD
137     */
138    if (sscanf(bs->msg, Find_media, &Job, &index, &pool_name, &mr.MediaType) == 4) {
139       memset(&pr, 0, sizeof(pr));
140       bstrncpy(pr.Name, pool_name, sizeof(pr.Name));
141       unbash_spaces(pr.Name);
142       ok = db_get_pool_record(jcr, jcr->db, &pr);
143       if (ok) {
144          mr.PoolId = pr.PoolId;
145          mr.StorageId = jcr->wstore->StorageId;
146          mr.ScratchPoolId = pr.ScratchPoolId;
147          ok = find_next_volume_for_append(jcr, &mr, index, fnv_create_vol, fnv_prune);
148          Dmsg3(050, "find_media ok=%d idx=%d vol=%s\n", ok, index, mr.VolumeName);
149       }
150       /*
151        * Send Find Media response to Storage daemon
152        */
153       if (ok) {
154          send_volume_info_to_storage_daemon(jcr, bs, &mr);
155       } else {
156          bs->fsend(_("1901 No Media.\n"));
157          Dmsg0(500, "1901 No Media.\n");
158       }
159
160    /*
161     * Request to find specific Volume information
162     */
163    } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
164       Dmsg1(100, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
165       /*
166        * Find the Volume
167        */
168       unbash_spaces(mr.VolumeName);
169       if (db_get_media_record(jcr, jcr->db, &mr)) {
170          const char *reason = NULL;           /* detailed reason for rejection */
171          /*
172           * If we are reading, accept any volume (reason == NULL)
173           * If we are writing, check if the Volume is valid
174           *   for this job, and do a recycle if necessary
175           */
176          if (writing) {
177             /*
178              * SD wants to write this Volume, so make
179              *   sure it is suitable for this job, i.e.
180              *   Pool matches, and it is either Append or Recycle
181              *   and Media Type matches and Pool allows any volume.
182              */
183             if (mr.PoolId != jcr->jr.PoolId) {
184                reason = _("not in Pool");
185             } else if (strcmp(mr.MediaType, jcr->wstore->media_type) != 0) {
186                reason = _("not correct MediaType");
187             } else {
188                /*
189                 * Now try recycling if necessary
190                 *   reason set non-NULL if we cannot use it
191                 */
192                check_if_volume_valid_or_recyclable(jcr, &mr, &reason);
193             }
194          }
195          if (!reason && mr.Enabled != 1) {
196             reason = _("is not Enabled");
197          }
198          if (reason == NULL) {
199             /*
200              * Send Find Media response to Storage daemon
201              */
202             send_volume_info_to_storage_daemon(jcr, bs, &mr);
203          } else {
204             /* Not suitable volume */
205             bs->fsend(_("1998 Volume \"%s\" status is %s, %s.\n"), mr.VolumeName,
206                mr.VolStatus, reason);
207          }
208
209       } else {
210          bs->fsend(_("1997 Volume \"%s\" not in catalog.\n"), mr.VolumeName);
211          Dmsg1(100, "1997 Volume \"%s\" not in catalog.\n", mr.VolumeName);
212       }
213
214    /*
215     * Request to update Media record. Comes typically at the end
216     *  of a Storage daemon Job Session, when labeling/relabeling a
217     *  Volume, or when an EOF mark is written.
218     */
219    } else if (sscanf(bs->msg, Update_media, &Job, &sdmr.VolumeName,
220       &sdmr.VolJobs, &sdmr.VolFiles, &sdmr.VolBlocks, &sdmr.VolBytes,
221       &sdmr.VolMounts, &sdmr.VolErrors, &sdmr.VolWrites, &sdmr.MaxVolBytes,
222       &VolLastWritten, &sdmr.VolStatus, &sdmr.Slot, &label, &sdmr.InChanger,
223       &sdmr.VolReadTime, &sdmr.VolWriteTime, &VolFirstWritten,
224       &sdmr.VolParts) == 19) {
225
226       db_lock(jcr->db);
227       Dmsg3(400, "Update media %s oldStat=%s newStat=%s\n", sdmr.VolumeName,
228          mr.VolStatus, sdmr.VolStatus);
229       bstrncpy(mr.VolumeName, sdmr.VolumeName, sizeof(mr.VolumeName)); /* copy Volume name */
230       unbash_spaces(mr.VolumeName);
231       if (!db_get_media_record(jcr, jcr->db, &mr)) {
232          Jmsg(jcr, M_ERROR, 0, _("Unable to get Media record for Volume %s: ERR=%s\n"),
233               mr.VolumeName, db_strerror(jcr->db));
234          bs->fsend(_("1991 Catalog Request for vol=%s failed: %s"),
235             mr.VolumeName, db_strerror(jcr->db));
236          db_unlock(jcr->db);
237          return;
238       }
239       /* Set first written time if this is first job */
240       if (mr.FirstWritten == 0) {
241          if (VolFirstWritten == 0) {
242             mr.FirstWritten = jcr->start_time;   /* use Job start time as first write */
243          } else {
244             mr.FirstWritten = VolFirstWritten;
245          }
246          mr.set_first_written = true;
247       }
248       /* If we just labeled the tape set time */
249       if (label || mr.LabelDate == 0) {
250          mr.LabelDate = jcr->start_time;
251          mr.set_label_date = true;
252          if (mr.InitialWrite == 0) {
253             mr.InitialWrite = jcr->start_time;
254          }
255          Dmsg2(400, "label=%d labeldate=%d\n", label, mr.LabelDate);
256       } else {
257          /*
258           * Insanity check for VolFiles get set to a smaller value
259           */
260          if (sdmr.VolFiles < mr.VolFiles) {
261             Jmsg(jcr, M_FATAL, 0, _("Volume Files at %u being set to %u"
262                  " for Volume \"%s\". This is incorrect.\n"),
263                mr.VolFiles, sdmr.VolFiles, mr.VolumeName);
264             bs->fsend(_("1992 Update Media error. VolFiles=%u, CatFiles=%u\n"),
265                sdmr.VolFiles, mr.VolFiles);
266             db_unlock(jcr->db);
267             return;
268          }
269       }
270       Dmsg2(400, "Update media: BefVolJobs=%u After=%u\n", mr.VolJobs, sdmr.VolJobs);
271
272       /*
273        * Check if the volume has been written by the job, 
274        * and update the LastWritten field if needed.
275        */
276       if (mr.VolBlocks != sdmr.VolBlocks && VolLastWritten != 0) {
277          mr.LastWritten = VolLastWritten;
278       }
279
280       /*
281        * Update to point to the last device used to write the Volume.
282        *   However, do so only if we are writing the tape, i.e.
283        *   the number of VolWrites has increased.
284        */
285       if (jcr->wstore && jcr->wstore->StorageId && sdmr.VolWrites > mr.VolWrites) {
286          mr.StorageId = jcr->wstore->StorageId;
287       }
288
289       /* Copy updated values to original media record */
290       mr.VolJobs      = sdmr.VolJobs;
291       mr.VolFiles     = sdmr.VolFiles;
292       mr.VolBlocks    = sdmr.VolBlocks;
293       mr.VolBytes     = sdmr.VolBytes;
294       mr.VolMounts    = sdmr.VolMounts;
295       mr.VolErrors    = sdmr.VolErrors;
296       mr.VolWrites    = sdmr.VolWrites;
297       mr.Slot         = sdmr.Slot;
298       mr.InChanger    = sdmr.InChanger;
299       mr.VolParts     = sdmr.VolParts;
300       bstrncpy(mr.VolStatus, sdmr.VolStatus, sizeof(mr.VolStatus));
301       if (sdmr.VolReadTime >= 0) { 
302          mr.VolReadTime  = sdmr.VolReadTime;
303       }
304       if (sdmr.VolWriteTime >= 0) {
305          mr.VolWriteTime = sdmr.VolWriteTime;
306       }
307
308       Dmsg2(400, "db_update_media_record. Stat=%s Vol=%s\n", mr.VolStatus, mr.VolumeName);
309       /*
310        * Update the database, then before sending the response to the
311        *  SD, check if the Volume has expired.
312        */
313       if (!db_update_media_record(jcr, jcr->db, &mr)) {
314          Jmsg(jcr, M_FATAL, 0, _("Catalog error updating Media record. %s"),
315             db_strerror(jcr->db));
316          bs->fsend(_("1993 Update Media error\n"));
317          Dmsg0(400, "send error\n");
318       } else {
319          (void)has_volume_expired(jcr, &mr);
320          send_volume_info_to_storage_daemon(jcr, bs, &mr);
321       }
322       db_unlock(jcr->db);
323
324    /*
325     * Request to create a JobMedia record
326     */
327    } else if (sscanf(bs->msg, Create_job_media, &Job,
328       &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
329       &jm.StartBlock, &jm.EndBlock, &jm.Copy, &Stripe, &MediaId) == 10) {
330
331       if (jcr->mig_jcr) {
332          jm.JobId = jcr->mig_jcr->JobId;
333       } else {
334          jm.JobId = jcr->JobId;
335       }
336       jm.MediaId = MediaId;
337       Dmsg6(400, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
338          jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
339       if (!db_create_jobmedia_record(jcr, jcr->db, &jm)) {
340          Jmsg(jcr, M_FATAL, 0, _("Catalog error creating JobMedia record. %s"),
341             db_strerror(jcr->db));
342          bs->fsend(_("1992 Create JobMedia error\n"));
343       } else {
344          Dmsg0(400, "JobMedia record created\n");
345          bs->fsend(OK_create);
346       }
347
348    } else {
349       omsg = get_memory(bs->msglen+1);
350       pm_strcpy(omsg, bs->msg);
351       bs->fsend(_("1990 Invalid Catalog Request: %s"), omsg);
352       Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog request: %s"), omsg);
353       free_memory(omsg);
354    }
355    Dmsg1(400, ">CatReq response: %s", bs->msg);
356    Dmsg1(400, "Leave catreq jcr 0x%x\n", jcr);
357    return;
358 }
359
360 /*
361  * Note, we receive the whole attribute record, but we select out only the stat
362  * packet, VolSessionId, VolSessionTime, FileIndex, file type, and file name to
363  * store in the catalog.
364  */
365 static void update_attribute(JCR *jcr, char *msg, int32_t msglen)
366 {
367    unser_declare;
368    uint32_t VolSessionId, VolSessionTime;
369    int32_t Stream;
370    uint32_t FileIndex;
371    uint32_t data_len;
372    char *p;
373    int len;
374    char *fname, *attr;
375    ATTR_DBR *ar = NULL;
376
377    /* Start transaction allocates jcr->attr and jcr->ar if needed */
378    db_start_transaction(jcr, jcr->db);     /* start transaction if not already open */
379    ar = jcr->ar;      
380
381    /* Start by scanning directly in the message buffer to get Stream   
382     *  there may be a cached attr so we cannot yet write into
383     *  jcr->attr or jcr->ar  
384     */
385    p = msg;
386    skip_nonspaces(&p);                /* UpdCat */
387    skip_spaces(&p);
388    skip_nonspaces(&p);                /* Job=nnn */
389    skip_spaces(&p);
390    skip_nonspaces(&p);                /* FileAttributes */
391    p += 1;
392    unser_begin(p, 0);
393    unser_uint32(VolSessionId);
394    unser_uint32(VolSessionTime);
395    unser_int32(FileIndex);
396    unser_int32(Stream);
397    unser_uint32(data_len);
398    p += unser_length(p);
399
400    Dmsg1(400, "UpdCat msg=%s\n", msg);
401    Dmsg5(400, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
402       VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
403
404    if (Stream == STREAM_UNIX_ATTRIBUTES || Stream == STREAM_UNIX_ATTRIBUTES_EX) {
405       if (jcr->cached_attribute) {
406          Dmsg2(400, "Cached attr. Stream=%d fname=%s\n", ar->Stream, ar->fname);
407          if (!db_create_attributes_record(jcr, jcr->db, ar)) {
408             Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
409          }
410       }
411       /* Any cached attr is flushed so we can reuse jcr->attr and jcr->ar */
412       jcr->attr = check_pool_memory_size(jcr->attr, msglen);
413       memcpy(jcr->attr, msg, msglen);
414       p = jcr->attr - msg + p;    /* point p into jcr->attr */
415       skip_nonspaces(&p);             /* skip FileIndex */
416       skip_spaces(&p);
417       ar->FileType = str_to_int32(p);     /* TODO: choose between unserialize and str_to_int32 */
418       skip_nonspaces(&p);             /* skip FileType */
419       skip_spaces(&p);
420       fname = p;
421       len = strlen(fname);        /* length before attributes */
422       attr = &fname[len+1];
423
424       Dmsg2(400, "dird<stored: stream=%d %s\n", Stream, fname);
425       Dmsg1(400, "dird<stored: attr=%s\n", attr);
426       ar->attr = attr;
427       ar->fname = fname;
428       if (ar->FileType == FT_DELETED) {
429          ar->FileIndex = 0;     /* special value */
430       } else {
431          ar->FileIndex = FileIndex;
432       }
433       ar->Stream = Stream;
434       ar->link = NULL;
435       if (jcr->mig_jcr) {
436          ar->JobId = jcr->mig_jcr->JobId;
437       } else {
438          ar->JobId = jcr->JobId;
439       }
440       ar->Digest = NULL;
441       ar->DigestType = CRYPTO_DIGEST_NONE;
442       jcr->cached_attribute = true;
443
444       Dmsg2(400, "dird<filed: stream=%d %s\n", Stream, fname);
445       Dmsg1(400, "dird<filed: attr=%s\n", attr);
446
447    } else if (crypto_digest_stream_type(Stream) != CRYPTO_DIGEST_NONE) {
448       fname = p;
449       if (ar->FileIndex != FileIndex) {
450          Jmsg(jcr, M_WARNING, 0, _("Got %s but not same File as attributes\n"), stream_to_ascii(Stream));
451       } else {
452          /* Update digest in catalog */
453          char digestbuf[BASE64_SIZE(CRYPTO_DIGEST_MAX_SIZE)];
454          int len = 0;
455          int type = CRYPTO_DIGEST_NONE;
456
457          switch(Stream) {
458          case STREAM_MD5_DIGEST:
459             len = CRYPTO_DIGEST_MD5_SIZE;
460             type = CRYPTO_DIGEST_MD5;
461             break;
462          case STREAM_SHA1_DIGEST:
463             len = CRYPTO_DIGEST_SHA1_SIZE;
464             type = CRYPTO_DIGEST_SHA1;
465             break;
466          case STREAM_SHA256_DIGEST:
467             len = CRYPTO_DIGEST_SHA256_SIZE;
468             type = CRYPTO_DIGEST_SHA256;
469             break;
470          case STREAM_SHA512_DIGEST:
471             len = CRYPTO_DIGEST_SHA512_SIZE;
472             type = CRYPTO_DIGEST_SHA512;
473             break;
474          default:
475             /* Never reached ... */
476             Jmsg(jcr, M_ERROR, 0, _("Catalog error updating file digest. Unsupported digest stream type: %d"),
477                  Stream);
478          }
479
480          bin_to_base64(digestbuf, sizeof(digestbuf), fname, len, true);
481          Dmsg3(400, "DigestLen=%d Digest=%s type=%d\n", strlen(digestbuf),
482                digestbuf, Stream);
483          if (jcr->cached_attribute) {
484             ar->Digest = digestbuf;
485             ar->DigestType = type;
486             Dmsg2(400, "Cached attr with digest. Stream=%d fname=%s\n",
487                   ar->Stream, ar->fname);
488
489             /* Update BaseFile table */
490             if (!db_create_attributes_record(jcr, jcr->db, ar)) {
491                Jmsg1(jcr, M_FATAL, 0, _("attribute create error. %s"),
492                         db_strerror(jcr->db));
493             }
494             jcr->cached_attribute = false; 
495          } else {
496             if (!db_add_digest_to_file_record(jcr, jcr->db, ar->FileId, digestbuf, type)) {
497                Jmsg(jcr, M_ERROR, 0, _("Catalog error updating file digest. %s"),
498                   db_strerror(jcr->db));
499             }
500          }
501       }
502    }
503 }
504
505 /*
506  * Update File Attributes in the catalog with data
507  *  sent by the Storage daemon.
508  */
509 void catalog_update(JCR *jcr, BSOCK *bs)
510 {
511    if (!jcr->pool->catalog_files) {
512       return;                         /* user disabled cataloging */
513    }
514    if (job_canceled(jcr)) {
515       goto bail_out;
516    }
517    if (!jcr->db) {
518       POOLMEM *omsg = get_memory(bs->msglen+1);
519       pm_strcpy(omsg, bs->msg);
520       bs->fsend(_("1994 Invalid Catalog Update: %s"), omsg);    
521       Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog Update; DB not open: %s"), omsg);
522       free_memory(omsg);
523       goto bail_out;
524    }
525    update_attribute(jcr, bs->msg, bs->msglen);
526
527 bail_out:
528    if (job_canceled(jcr)) {
529       cancel_storage_daemon_job(jcr);
530    }
531 }
532
533 /*
534  * Update File Attributes in the catalog with data read from
535  * the storage daemon spool file. We receive the filename and
536  * we try to read it.
537  */
538 bool despool_attributes_from_file(JCR *jcr, const char *file)
539 {
540    bool ret=false;
541    int32_t pktsiz;
542    size_t nbytes;
543    ssize_t last = 0, size = 0;
544    int count = 0;
545    int32_t msglen;                    /* message length */
546    POOLMEM *msg = get_pool_memory(PM_MESSAGE);
547    FILE *spool_fd=NULL;
548
549    Dmsg0(100, "Begin despool_attributes_from_file\n");
550
551    if (job_canceled(jcr) || !jcr->pool->catalog_files || !jcr->db) {
552       goto bail_out;                  /* user disabled cataloging */
553    }
554
555    spool_fd = fopen(file, "rb");
556    if (!spool_fd) {
557       Dmsg0(100, "cancel despool_attributes_from_file\n");
558       /* send an error message */
559       goto bail_out;
560    }
561 #if defined(HAVE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
562    posix_fadvise(fileno(spool_fd), 0, 0, POSIX_FADV_WILLNEED);
563 #endif
564
565    while (fread((char *)&pktsiz, 1, sizeof(int32_t), spool_fd) ==
566           sizeof(int32_t)) {
567       size += sizeof(int32_t);
568       msglen = ntohl(pktsiz);
569       if (msglen > 0) {
570          if (msglen > (int32_t) sizeof_pool_memory(msg)) {
571             msg = realloc_pool_memory(msg, msglen + 1);
572          }
573          nbytes = fread(msg, 1, msglen, spool_fd);
574          if (nbytes != (size_t) msglen) {
575             berrno be;
576             Dmsg2(400, "nbytes=%d msglen=%d\n", nbytes, msglen);
577             Qmsg1(jcr, M_FATAL, 0, _("fread attr spool error. ERR=%s\n"),
578                   be.bstrerror());
579             goto bail_out;
580          }
581          size += nbytes;
582          if ((++count & 0x3F) == 0) {
583             last = size;
584          }
585       }
586       if (!job_canceled(jcr)) {
587          update_attribute(jcr, msg, msglen);
588          if (job_canceled(jcr)) {
589             goto bail_out;
590          }
591       }
592    }
593    if (ferror(spool_fd)) {
594       berrno be;
595       Qmsg1(jcr, M_FATAL, 0, _("fread attr spool error. ERR=%s\n"),
596             be.bstrerror());
597       goto bail_out;
598    }
599    ret = true;
600
601 bail_out:
602    if (spool_fd) {
603       fclose(spool_fd);
604    }
605
606    if (job_canceled(jcr)) {
607       cancel_storage_daemon_job(jcr);
608    }
609
610    free_pool_memory(msg);
611    Dmsg1(100, "End despool_attributes_from_file ret=%i\n", ret);
612    return ret;
613 }