]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/backup.c
Integrated Preben 'Peppe' Guldberg <peppe@wielders.org>
[bacula/bacula] / bacula / src / filed / backup.c
1 /*
2  *  Bacula File Daemon  backup.c  send file attributes and data
3  *   to the Storage daemon.
4  *
5  *    Kern Sibbald, March MM
6  *
7  *   Version $Id$
8  *
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"
31 #include "filed.h"
32
33 #ifdef HAVE_ACL
34 #include <sys/acl.h>
35 #include <acl/libacl.h>
36 #endif
37
38 static int save_file(FF_PKT *ff_pkt, void *pkt);
39
40 /* 
41  * Find all the requested files and send them
42  * to the Storage daemon. 
43  *
44  * Note, we normally carry on a one-way
45  * conversation from this point on with the SD, simply blasting
46  * data to him.  To properly know what is going on, we
47  * also run a "heartbeat" monitor which reads the socket and
48  * reacts accordingly (at the moment it has nothing to do
49  * except echo the heartbeat to the Director).
50  * 
51  */
52 bool blast_data_to_storage_daemon(JCR *jcr, char *addr) 
53 {
54    BSOCK *sd;
55    bool ok = true;
56
57    sd = jcr->store_bsock;
58
59    set_jcr_job_status(jcr, JS_Running);
60
61    Dmsg1(300, "bfiled: opened data connection %d to stored\n", sd->fd);
62
63    LockRes();
64    CLIENT *client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
65    UnlockRes();
66    uint32_t buf_size;
67    if (client) {
68       buf_size = client->max_network_buffer_size;
69    } else {
70       buf_size = 0;                   /* use default */
71    }
72    if (!bnet_set_buffer_size(sd, buf_size, BNET_SETBUF_WRITE)) {
73       set_jcr_job_status(jcr, JS_ErrorTerminated);
74       Jmsg(jcr, M_FATAL, 0, _("Cannot set buffer size FD->SD.\n"));
75       return false;
76    }
77
78    jcr->buf_size = sd->msglen;             
79    /* Adjust for compression so that output buffer is
80     * 12 bytes + 0.1% larger than input buffer plus 18 bytes.
81     * This gives a bit extra plus room for the sparse addr if any.
82     * Note, we adjust the read size to be smaller so that the
83     * same output buffer can be used without growing it.
84     */
85    jcr->compress_buf_size = jcr->buf_size + ((jcr->buf_size+999) / 1000) + 30;
86    jcr->compress_buf = get_memory(jcr->compress_buf_size);
87
88    Dmsg1(300, "set_find_options ff=%p\n", jcr->ff);
89    set_find_options((FF_PKT *)jcr->ff, jcr->incremental, jcr->mtime);
90    Dmsg0(300, "start find files\n");
91
92    start_heartbeat_monitor(jcr);
93
94    /* Subroutine save_file() is called for each file */
95    if (!find_files(jcr, (FF_PKT *)jcr->ff, save_file, (void *)jcr)) {
96       ok = false;                     /* error */
97       set_jcr_job_status(jcr, JS_ErrorTerminated);
98 //    Jmsg(jcr, M_FATAL, 0, _("Find files error.\n"));
99    }
100
101    stop_heartbeat_monitor(jcr);
102
103    bnet_sig(sd, BNET_EOD);            /* end data connection */
104
105    if (jcr->big_buf) {
106       free(jcr->big_buf);
107       jcr->big_buf = NULL;
108    }
109    if (jcr->compress_buf) {
110       free_pool_memory(jcr->compress_buf);
111       jcr->compress_buf = NULL;
112    }
113    Dmsg1(300, "end blast_data stat=%d\n", ok);
114    return ok;
115 }          
116
117 /* 
118  * Called here by find() for each file included.
119  *
120  *  *****FIXME*****   add FSMs File System Modules
121  *
122  *  Send the file and its data to the Storage daemon.
123  *
124  *  Returns: 1 if OK
125  *           0 if error
126  *          -1 to ignore file/directory (not used here)
127  */
128 static int save_file(FF_PKT *ff_pkt, void *vjcr)
129 {
130    char attribs[MAXSTRING];
131    char attribsEx[MAXSTRING];
132    int stat, attr_stream, data_stream;
133    bool hfsplus = false;
134    struct MD5Context md5c;
135    struct SHA1Context sha1c;
136    int gotMD5 = 0;
137    int gotSHA1 = 0;
138    unsigned char signature[30];       /* large enough for either signature */
139    BSOCK *sd;
140    JCR *jcr = (JCR *)vjcr;
141    POOLMEM *msgsave;
142
143    if (job_canceled(jcr)) {
144       return 0;
145    }
146
147    sd = jcr->store_bsock;
148    jcr->num_files_examined++;         /* bump total file count */
149
150    switch (ff_pkt->type) {
151    case FT_LNKSAVED:                  /* Hard linked, file already saved */
152       Dmsg2(130, "FT_LNKSAVED hard link: %s => %s\n", ff_pkt->fname, ff_pkt->link);
153       break;
154    case FT_REGE:
155       Dmsg1(130, "FT_REGE saving: %s\n", ff_pkt->fname);
156       break;
157    case FT_REG:
158       Dmsg1(130, "FT_REG saving: %s\n", ff_pkt->fname);
159       break;
160    case FT_LNK:
161       Dmsg2(130, "FT_LNK saving: %s -> %s\n", ff_pkt->fname, ff_pkt->link);
162       break;
163    case FT_DIRBEGIN:
164       return 1;                       /* not used */
165    case FT_NORECURSE:
166    case FT_NOFSCHG:
167    case FT_INVALIDFS:
168    case FT_DIREND:
169       if (ff_pkt->type == FT_NORECURSE) {
170          Jmsg(jcr, M_INFO, 1, _("     Recursion turned off. Will not descend into %s\n"), 
171             ff_pkt->fname);
172       } else if (ff_pkt->type == FT_NOFSCHG) {
173          Jmsg(jcr, M_INFO, 1, _("     File system change prohibited. Will not descend into %s\n"), 
174             ff_pkt->fname);
175       } else if (ff_pkt->type == FT_INVALIDFS) {
176          Jmsg(jcr, M_INFO, 1, _("     Disallowed filesystem. Will not descend into %s\n"), 
177             ff_pkt->fname);
178       }
179       ff_pkt->type = FT_DIREND;       /* value is used below */
180       Dmsg1(130, "FT_DIR saving: %s\n", ff_pkt->link);
181       break;
182    case FT_SPEC:
183       Dmsg1(130, "FT_SPEC saving: %s\n", ff_pkt->fname);
184       break;
185    case FT_RAW:
186       Dmsg1(130, "FT_RAW saving: %s\n", ff_pkt->fname);
187       break;
188    case FT_FIFO:
189       Dmsg1(130, "FT_FIFO saving: %s\n", ff_pkt->fname);
190       break;
191    case FT_NOACCESS: {
192       berrno be;
193       be.set_errno(ff_pkt->ff_errno);
194       Jmsg(jcr, M_NOTSAVED, 0, _("     Could not access %s: ERR=%s\n"), ff_pkt->fname, 
195          be.strerror());
196       jcr->Errors++;
197       return 1;
198    }
199    case FT_NOFOLLOW: {
200       berrno be;
201       be.set_errno(ff_pkt->ff_errno);
202       Jmsg(jcr, M_NOTSAVED, 0, _("     Could not follow link %s: ERR=%s\n"), ff_pkt->fname, 
203          be.strerror());
204       jcr->Errors++;
205       return 1;
206    }
207    case FT_NOSTAT: {
208       berrno be;
209       be.set_errno(ff_pkt->ff_errno);
210       Jmsg(jcr, M_NOTSAVED, 0, _("     Could not stat %s: ERR=%s\n"), ff_pkt->fname, 
211          be.strerror());
212       jcr->Errors++;
213       return 1;
214    }
215    case FT_DIRNOCHG:
216    case FT_NOCHG:
217       Jmsg(jcr, M_SKIPPED, 1, _("     Unchanged file skipped: %s\n"), ff_pkt->fname);
218       return 1;
219    case FT_ISARCH:
220       Jmsg(jcr, M_NOTSAVED, 0, _("     Archive file not saved: %s\n"), ff_pkt->fname);
221       return 1;
222    case FT_NOOPEN: {
223       berrno be;
224       be.set_errno(ff_pkt->ff_errno);
225       Jmsg(jcr, M_NOTSAVED, 0, _("     Could not open directory %s: ERR=%s\n"), ff_pkt->fname, 
226          be.strerror());
227       jcr->Errors++;
228       return 1;
229    }
230    default:
231       Jmsg(jcr, M_NOTSAVED, 0,  _("     Unknown file type %d; not saved: %s\n"), ff_pkt->type, ff_pkt->fname);
232       jcr->Errors++;
233       return 1;
234    }
235
236    binit(&ff_pkt->bfd);
237    if (ff_pkt->flags & FO_PORTABLE) {
238       set_portable_backup(&ff_pkt->bfd); /* disable Win32 BackupRead() */
239    }
240    if (ff_pkt->reader) {
241       set_prog(&ff_pkt->bfd, ff_pkt->reader, jcr);
242    }
243
244    /* 
245     * Open any file with data that we intend to save.  
246     * Note, if is_win32_backup, we must open the Directory so that
247     * the BackupRead will save its permissions and ownership streams.
248     */
249    if (ff_pkt->type != FT_LNKSAVED && (S_ISREG(ff_pkt->statp.st_mode) && 
250          ff_pkt->statp.st_size > 0) || 
251          ff_pkt->type == FT_RAW || ff_pkt->type == FT_FIFO ||
252          (!is_portable_backup(&ff_pkt->bfd) && ff_pkt->type == FT_DIREND)) {
253       btimer_t *tid;    
254       if (ff_pkt->type == FT_FIFO) {
255          tid = start_thread_timer(pthread_self(), 60);
256       } else {
257          tid = NULL;
258       }
259       if (bopen(&ff_pkt->bfd, ff_pkt->fname, O_RDONLY | O_BINARY, 0) < 0) {
260          ff_pkt->ff_errno = errno;
261          berrno be;
262          Jmsg(jcr, M_NOTSAVED, 0, _("     Cannot open %s: ERR=%s.\n"), ff_pkt->fname, 
263               be.strerror());
264          jcr->Errors++;
265          if (tid) {
266             stop_thread_timer(tid);
267             tid = NULL;
268          }
269          return 1;
270       }
271       if (tid) {
272          stop_thread_timer(tid);
273          tid = NULL;
274       }
275    }
276
277    binit(&ff_pkt->rsrc_bfd);
278 #ifdef HAVE_DARWIN_OS
279    /* Open resource fork if necessary */
280    if (ff_pkt->type != FT_LNKSAVED && (S_ISREG(ff_pkt->statp.st_mode) &&
281             ff_pkt->flags & FO_HFSPLUS)) {
282       /* Remember Finder Info, whether we have data or fork, or not */
283       hfsplus = true;
284       if (ff_pkt->hfsinfo.rsrclength > 0) {
285          if (!bopen_rsrc(&ff_pkt->rsrc_bfd, ff_pkt->fname, O_RDONLY | O_BINARY, 0) < 0) {
286             ff_pkt->ff_errno = errno;
287             berrno be;
288             Jmsg(jcr, M_NOTSAVED, -1, _("     Cannot open resource fork for %s: ERR=%s.\n"), ff_pkt->fname, 
289                   be.strerror());
290             jcr->Errors++;
291             if (is_bopen(&ff_pkt->bfd)) {
292                bclose(&ff_pkt->bfd);
293             }
294             return 1;
295          }
296       }
297    }
298 #endif
299
300    Dmsg1(130, "bfiled: sending %s to stored\n", ff_pkt->fname);
301
302    /* Find what data stream we will use, then encode the attributes */
303    data_stream = select_data_stream(ff_pkt);
304    encode_stat(attribs, ff_pkt, data_stream);
305
306    /* Now possibly extend the attributes */
307    attr_stream = encode_attribsEx(jcr, attribsEx, ff_pkt);
308
309    Dmsg3(300, "File %s\nattribs=%s\nattribsEx=%s\n", ff_pkt->fname, attribs, attribsEx);
310      
311    P(jcr->mutex);
312    jcr->JobFiles++;                    /* increment number of files sent */
313    ff_pkt->FileIndex = jcr->JobFiles;  /* return FileIndex */
314    pm_strcpy(jcr->last_fname, ff_pkt->fname);
315    V(jcr->mutex);
316     
317    /*
318     * Send Attributes header to Storage daemon
319     *    <file-index> <stream> <info>
320     */
321    if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, attr_stream)) {
322       berrno be;
323       if (is_bopen(&ff_pkt->bfd)) {
324          bclose(&ff_pkt->bfd);
325       }
326       if (is_bopen(&ff_pkt->rsrc_bfd)) {
327          bclose(&ff_pkt->rsrc_bfd);
328       }
329       Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
330             bnet_strerror(sd));
331       return 0;
332    }
333    Dmsg1(300, ">stored: attrhdr %s\n", sd->msg);
334
335    /*
336     * Send file attributes to Storage daemon
337     *   File_index
338     *   File type
339     *   Filename (full path)
340     *   Encoded attributes
341     *   Link name (if type==FT_LNK or FT_LNKSAVED)
342     *   Encoded extended-attributes (for Win32)
343     *
344     * For a directory, link is the same as fname, but with trailing
345     * slash. For a linked file, link is the link.
346     */
347    if (ff_pkt->type == FT_LNK || ff_pkt->type == FT_LNKSAVED) {
348       Dmsg2(300, "Link %s to %s\n", ff_pkt->fname, ff_pkt->link);
349       stat = bnet_fsend(sd, "%ld %d %s%c%s%c%s%c%s%c", jcr->JobFiles, 
350                ff_pkt->type, ff_pkt->fname, 0, attribs, 0, ff_pkt->link, 0,
351                attribsEx, 0);
352    } else if (ff_pkt->type == FT_DIREND) {
353       /* Here link is the canonical filename (i.e. with trailing slash) */
354       stat = bnet_fsend(sd, "%ld %d %s%c%s%c%c%s%c", jcr->JobFiles, 
355                ff_pkt->type, ff_pkt->link, 0, attribs, 0, 0, attribsEx, 0);
356    } else {
357       stat = bnet_fsend(sd, "%ld %d %s%c%s%c%c%s%c", jcr->JobFiles, 
358                ff_pkt->type, ff_pkt->fname, 0, attribs, 0, 0, attribsEx, 0);
359    }
360
361    Dmsg2(300, ">stored: attr len=%d: %s\n", sd->msglen, sd->msg);
362    if (!stat) {
363       berrno be;
364       if (is_bopen(&ff_pkt->bfd)) {
365          bclose(&ff_pkt->bfd);
366       }
367       if (is_bopen(&ff_pkt->rsrc_bfd)) {
368          bclose(&ff_pkt->rsrc_bfd);
369       }
370       Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
371             bnet_strerror(sd));
372       return 0;
373    }
374    bnet_sig(sd, BNET_EOD);            /* indicate end of attributes data */
375
376    if (is_bopen(&ff_pkt->bfd) || hfsplus) {
377       if (ff_pkt->flags & FO_MD5) {
378          MD5Init(&md5c);
379       } else if (ff_pkt->flags & FO_SHA1) {
380          SHA1Init(&sha1c);
381       }
382    }
383
384    /* 
385     * If the file has data, read it and send to the Storage daemon
386     *
387     */
388    if (is_bopen(&ff_pkt->bfd)) {
389       uint64_t fileAddr = 0;          /* file address */
390       char *rbuf, *wbuf;
391       int rsize = jcr->buf_size;      /* read buffer size */
392
393       msgsave = sd->msg;
394       rbuf = sd->msg;                 /* read buffer */             
395       wbuf = sd->msg;                 /* write buffer */
396
397
398       Dmsg1(300, "Saving data, type=%d\n", ff_pkt->type);
399
400
401 #ifdef HAVE_LIBZ
402       uLong compress_len, max_compress_len = 0;
403       const Bytef *cbuf = NULL;
404
405       if (ff_pkt->flags & FO_GZIP) {
406          if (ff_pkt->flags & FO_SPARSE) {
407             cbuf = (Bytef *)jcr->compress_buf + SPARSE_FADDR_SIZE;
408             max_compress_len = jcr->compress_buf_size - SPARSE_FADDR_SIZE;
409          } else {
410             cbuf = (Bytef *)jcr->compress_buf;
411             max_compress_len = jcr->compress_buf_size; /* set max length */
412          }
413          wbuf = jcr->compress_buf;    /* compressed output here */
414       }
415 #endif
416
417       /*
418        * Send Data header to Storage daemon
419        *    <file-index> <stream> <info>
420        */
421       if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, data_stream)) {
422          berrno be;
423          bclose(&ff_pkt->bfd);
424          if (is_bopen(&ff_pkt->rsrc_bfd)) {
425             bclose(&ff_pkt->rsrc_bfd);
426          }
427          Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
428                bnet_strerror(sd));
429          return 0;
430       }
431       Dmsg1(300, ">stored: datahdr %s\n", sd->msg);
432
433       /*
434        * Make space at beginning of buffer for fileAddr because this
435        *   same buffer will be used for writing if compression if off. 
436        */
437       if (ff_pkt->flags & FO_SPARSE) {
438          rbuf += SPARSE_FADDR_SIZE;
439          rsize -= SPARSE_FADDR_SIZE;
440 #ifdef HAVE_FREEBSD_OS
441          /* 
442           * To read FreeBSD partitions, the read size must be
443           *  a multiple of 512.
444           */
445          rsize = (rsize/512) * 512;
446 #endif
447       }
448
449       /* 
450        * Read the file data
451        */
452       while ((sd->msglen=(uint32_t)bread(&ff_pkt->bfd, rbuf, rsize)) > 0) {
453          int sparseBlock = 0;
454
455          /* Check for sparse blocks */
456          if (ff_pkt->flags & FO_SPARSE) {
457             ser_declare;
458             if (sd->msglen == rsize && 
459                 (fileAddr+sd->msglen < (uint64_t)ff_pkt->statp.st_size)) {
460                sparseBlock = is_buf_zero(rbuf, rsize);
461             }
462                
463             ser_begin(wbuf, SPARSE_FADDR_SIZE);
464             ser_uint64(fileAddr);     /* store fileAddr in begin of buffer */
465          } 
466
467          jcr->ReadBytes += sd->msglen;      /* count bytes read */
468          fileAddr += sd->msglen;
469
470          /* Update MD5 if requested */
471          if (ff_pkt->flags & FO_MD5) {
472             MD5Update(&md5c, (unsigned char *)rbuf, sd->msglen);
473             gotMD5 = 1;
474          } else if (ff_pkt->flags & FO_SHA1) {
475             SHA1Update(&sha1c, (unsigned char *)rbuf, sd->msglen);
476             gotSHA1 = 1;
477          }
478
479 #ifdef HAVE_LIBZ
480          /* Do compression if turned on */
481          if (!sparseBlock && ff_pkt->flags & FO_GZIP) {
482             int zstat;
483             compress_len = max_compress_len;
484             Dmsg4(400, "cbuf=0x%x len=%u rbuf=0x%x len=%u\n", cbuf, compress_len,
485                rbuf, sd->msglen);
486             /* NOTE! This call modifies compress_len !!! */
487             if ((zstat=compress2((Bytef *)cbuf, &compress_len, 
488                   (const Bytef *)rbuf, (uLong)sd->msglen,
489                   ff_pkt->GZIP_level)) != Z_OK) {
490                Jmsg(jcr, M_FATAL, 0, _("Compression error: %d\n"), zstat);
491                sd->msg = msgsave;
492                sd->msglen = 0;
493                bclose(&ff_pkt->bfd);
494                if (is_bopen(&ff_pkt->rsrc_bfd)) {
495                   bclose(&ff_pkt->rsrc_bfd);
496                }
497                set_jcr_job_status(jcr, JS_ErrorTerminated);
498                return 0;
499             }
500             Dmsg2(400, "compressed len=%d uncompressed len=%d\n", 
501                compress_len, sd->msglen);
502
503             sd->msglen = compress_len;   /* set compressed length */
504          }
505 #endif
506
507          /* Send the buffer to the Storage daemon */
508          if (!sparseBlock) {
509             if (ff_pkt->flags & FO_SPARSE) {
510                sd->msglen += SPARSE_FADDR_SIZE; /* include fileAddr in size */
511             }
512             sd->msg = wbuf;           /* set correct write buffer */
513             if (!bnet_send(sd)) {
514                berrno be;
515                Jmsg2(jcr, M_FATAL, 0, _("Network send error %d to SD. ERR=%s\n"),
516                      sd->msglen, bnet_strerror(sd));
517                sd->msg = msgsave;     /* restore bnet buffer */
518                sd->msglen = 0;
519                bclose(&ff_pkt->bfd);
520                if (is_bopen(&ff_pkt->rsrc_bfd)) {
521                   bclose(&ff_pkt->rsrc_bfd);
522                }
523                return 0;
524             }
525          }
526          Dmsg1(130, "Send data to SD len=%d\n", sd->msglen);
527          /*       #endif */
528          jcr->JobBytes += sd->msglen;   /* count bytes saved possibly compressed */
529          sd->msg = msgsave;             /* restore read buffer */
530
531       } /* end while read file data */
532
533
534       if (sd->msglen < 0) {
535          berrno be;
536          be.set_errno(ff_pkt->bfd.berrno);
537          Jmsg(jcr, M_ERROR, 0, _("Read error on file %s. ERR=%s\n"),
538             ff_pkt->fname, be.strerror());
539       }
540
541       bclose(&ff_pkt->bfd);              /* close file */
542       if (!bnet_sig(sd, BNET_EOD)) {     /* indicate end of file data */
543          berrno be;
544          Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
545                bnet_strerror(sd));
546          if (is_bopen(&ff_pkt->rsrc_bfd)) {
547             bclose(&ff_pkt->rsrc_bfd);
548          }
549          return 0;
550       }
551    }
552
553 #ifdef HAVE_DARWIN_OS
554    /* 
555     * If resource fork has data, read it and send to the Storage daemon
556     *
557     */
558    if (is_bopen(&ff_pkt->rsrc_bfd)) {
559       uint64_t fileAddr = 0;          /* file address */
560       int rsize = jcr->buf_size;      /* read buffer size */
561
562       Dmsg1(300, "Saving resource fork for \"%s\"", ff_pkt->fname);
563
564       /*
565        * Send Data header to Storage daemon
566        *    <file-index> <stream> <info>
567        */
568       if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, STREAM_MACOS_FORK_DATA)) {
569          berrno be;
570          bclose(&ff_pkt->rsrc_bfd);
571          Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
572                bnet_strerror(sd));
573          return 0;
574       }
575       Dmsg1(300, ">stored: datahdr %s\n", sd->msg);
576
577       /* 
578        * Read the resource fork
579        */
580       while ((sd->msglen=(uint32_t)bread(&ff_pkt->rsrc_bfd, sd->msg, rsize)) > 0) {
581          jcr->ReadBytes += sd->msglen;      /* count bytes read */
582          fileAddr += sd->msglen;
583
584          /* Update MD5 if requested */
585          if (ff_pkt->flags & FO_MD5) {
586             MD5Update(&md5c, (unsigned char *)sd->msg, sd->msglen);
587             gotMD5 = 1;
588          } else if (ff_pkt->flags & FO_SHA1) {
589             SHA1Update(&sha1c, (unsigned char *)sd->msg, sd->msglen);
590             gotSHA1 = 1;
591          }
592
593          /* Send the buffer to the Storage daemon */
594          if (!bnet_send(sd)) {
595             berrno be;
596             Jmsg2(jcr, M_FATAL, 0, _("Network send error %d to SD. ERR=%s\n"),
597                   sd->msglen, bnet_strerror(sd));
598             sd->msglen = 0;
599             bclose(&ff_pkt->rsrc_bfd);
600             return 0;
601          }
602          Dmsg1(130, "Send data to SD len=%d\n", sd->msglen);
603          /*       #endif */
604          jcr->JobBytes += sd->msglen;   /* count bytes saved possibly compressed */
605       } /* end while read file data */
606
607       if (sd->msglen < 0) {
608          berrno be;
609          be.set_errno(ff_pkt->rsrc_bfd.berrno);
610          Jmsg(jcr, M_ERROR, 0, _("Read error on resource fork of %s. ERR=%s\n"),
611             ff_pkt->fname, be.strerror());
612       }
613
614       bclose(&ff_pkt->rsrc_bfd);                 /* close resource fork */
615       if (!bnet_sig(sd, BNET_EOD)) {     /* indicate end of resource fork */
616          berrno be;
617          Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
618                bnet_strerror(sd));
619          return 0;
620       }
621    }
622 #endif
623
624 #ifdef HAVE_DARWIN_OS
625    /* Handle Finder Info */
626    if (S_ISREG(ff_pkt->statp.st_mode) && ff_pkt->flags & FO_HFSPLUS) {
627       Dmsg1(300, "Saving Finder Info for \"%s\"", ff_pkt->fname);
628       bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, STREAM_HFSPLUS_ATTRIBUTES);
629       Dmsg1(300, "bfiled>stored:header %s\n", sd->msg);
630       memcpy(sd->msg, ff_pkt->hfsinfo.fndrinfo, 32);
631       sd->msglen = 32;
632       /* Update MD5 if requested */
633       if (ff_pkt->flags & FO_MD5) {
634          MD5Update(&md5c, (unsigned char *)sd->msg, sd->msglen);
635          gotMD5 = 1;
636       } else if (ff_pkt->flags & FO_SHA1) {
637          SHA1Update(&sha1c, (unsigned char *)sd->msg, sd->msglen);
638          gotSHA1 = 1;
639       }
640       bnet_send(sd);
641       bnet_sig(sd, BNET_EOD);
642    }
643 #endif
644
645    
646 #ifdef HAVE_ACL
647    /* ACL stream */
648    if (ff_pkt->flags & FO_ACL) {
649       char *acl_text;
650       /* Read ACLs for files, dirs and links */
651       if (ff_pkt->type == FT_DIREND) {
652          /* Directory: Try for default ACL*/
653          acl_t myAcl = acl_get_file(ff_pkt->fname, ACL_TYPE_DEFAULT);
654          if (!myAcl) {
655             Dmsg1(200, "No default ACL defined for directory: %s!\n", ff_pkt->fname);
656             /* If there is no default ACL get standard ACL */
657             myAcl = acl_get_file(ff_pkt->fname, ACL_TYPE_ACCESS);
658             if (!myAcl) {
659                Jmsg1(jcr, M_WARNING, 0, "Error while trying to get ACL of directory: %s!\n", ff_pkt->fname);
660             }
661          }
662          acl_text = acl_to_any_text(myAcl, NULL, ',', TEXT_ABBREVIATE);
663          /* Free memory */
664          acl_free(myAcl);
665       } else {
666          /* Files or links */
667          acl_t myAcl = acl_get_file(ff_pkt->fname, ACL_TYPE_ACCESS);
668          if (!myAcl) {
669             Jmsg1(jcr, M_WARNING, 0, "Error while trying to get ACL of file: %s!\n", ff_pkt->fname);
670             acl_free(myAcl);
671          }
672          acl_text = acl_to_any_text(myAcl, NULL, ',', TEXT_ABBREVIATE);
673          acl_free(myAcl);
674       }
675       
676       /* If there is an ACL, send it to the Storage daemon */
677       if (acl_text) {
678          sd = jcr->store_bsock;
679          pm_strcpy(&jcr->last_fname, ff_pkt->fname);
680       
681          /*
682          * Send ACL header
683          *
684          */
685          if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, STREAM_UNIX_ATTRIBUTES_ACL)) {
686             berrno be;
687             Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
688                   bnet_strerror(sd));
689             return 0;
690          }
691       
692          /* Send the buffer to the storage deamon */
693          msgsave = sd->msg;
694          sd->msg = acl_text;
695          sd->msglen = strlen(acl_text) + 1;
696          if (!bnet_send(sd)) {
697             berrno be;
698             sd->msg = msgsave;
699             sd->msglen = 0;
700             Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
701                   bnet_strerror(sd));
702          } else {
703             jcr->JobBytes += sd->msglen;
704             sd->msg = msgsave;
705             if (!bnet_sig(sd, BNET_EOD)) {
706                berrno be;
707                Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
708                      bnet_strerror(sd));
709             } else {
710                Dmsg1(200, "ACL of file: %s successfully backed up!\n", ff_pkt->fname);
711             }
712          }  
713       }
714    }
715 #endif
716
717    /* Terminate any MD5 signature and send it to Storage daemon and the Director */
718    if (gotMD5 && ff_pkt->flags & FO_MD5) {
719       MD5Final(signature, &md5c);
720       bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, STREAM_MD5_SIGNATURE);
721       Dmsg1(300, "bfiled>stored:header %s\n", sd->msg);
722       memcpy(sd->msg, signature, 16);
723       sd->msglen = 16;
724       bnet_send(sd);
725       bnet_sig(sd, BNET_EOD);         /* end of MD5 */
726       gotMD5 = 0;
727
728    } else if (gotSHA1 && ff_pkt->flags & FO_SHA1) {
729    /* Terminate any SHA1 signature and send it to Storage daemon and the Director */
730       SHA1Final(&sha1c, signature);
731       bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, STREAM_SHA1_SIGNATURE);
732       Dmsg1(300, "bfiled>stored:header %s\n", sd->msg);
733       memcpy(sd->msg, signature, 20);
734       sd->msglen = 20;
735       bnet_send(sd);
736       bnet_sig(sd, BNET_EOD);         /* end of SHA1 */
737       gotMD5 = 0;
738    }
739    return 1;
740 }