2 * Bacula File Daemon backup.c send file attributes and data
3 * to the Storage daemon.
5 * Kern Sibbald, March MM
11 Copyright (C) 2000-2004 Kern Sibbald and John Walker
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.
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.
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,
35 #include <acl/libacl.h>
38 static int save_file(FF_PKT *ff_pkt, void *pkt);
41 * Find all the requested files and send them
42 * to the Storage daemon.
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).
52 bool blast_data_to_storage_daemon(JCR *jcr, char *addr)
57 sd = jcr->store_bsock;
59 set_jcr_job_status(jcr, JS_Running);
61 Dmsg1(300, "bfiled: opened data connection %d to stored\n", sd->fd);
64 CLIENT *client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
68 buf_size = client->max_network_buffer_size;
70 buf_size = 0; /* use default */
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"));
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.
85 jcr->compress_buf_size = jcr->buf_size + ((jcr->buf_size+999) / 1000) + 30;
86 jcr->compress_buf = get_memory(jcr->compress_buf_size);
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");
92 start_heartbeat_monitor(jcr);
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"));
101 stop_heartbeat_monitor(jcr);
103 bnet_sig(sd, BNET_EOD); /* end data connection */
109 if (jcr->compress_buf) {
110 free_pool_memory(jcr->compress_buf);
111 jcr->compress_buf = NULL;
113 Dmsg1(300, "end blast_data stat=%d\n", ok);
118 * Called here by find() for each file included.
120 * *****FIXME***** add FSMs File System Modules
122 * Send the file and its data to the Storage daemon.
126 * -1 to ignore file/directory (not used here)
128 static int save_file(FF_PKT *ff_pkt, void *vjcr)
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;
138 unsigned char signature[30]; /* large enough for either signature */
140 JCR *jcr = (JCR *)vjcr;
143 if (job_canceled(jcr)) {
147 sd = jcr->store_bsock;
148 jcr->num_files_examined++; /* bump total file count */
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);
155 Dmsg1(130, "FT_REGE saving: %s\n", ff_pkt->fname);
158 Dmsg1(130, "FT_REG saving: %s\n", ff_pkt->fname);
161 Dmsg2(130, "FT_LNK saving: %s -> %s\n", ff_pkt->fname, ff_pkt->link);
164 return 1; /* not used */
169 if (ff_pkt->type == FT_NORECURSE) {
170 Jmsg(jcr, M_INFO, 1, _(" Recursion turned off. Will not descend into %s\n"),
172 } else if (ff_pkt->type == FT_NOFSCHG) {
173 Jmsg(jcr, M_INFO, 1, _(" File system change prohibited. Will not descend into %s\n"),
175 } else if (ff_pkt->type == FT_INVALIDFS) {
176 Jmsg(jcr, M_INFO, 1, _(" Disallowed filesystem. Will not descend into %s\n"),
179 ff_pkt->type = FT_DIREND; /* value is used below */
180 Dmsg1(130, "FT_DIR saving: %s\n", ff_pkt->link);
183 Dmsg1(130, "FT_SPEC saving: %s\n", ff_pkt->fname);
186 Dmsg1(130, "FT_RAW saving: %s\n", ff_pkt->fname);
189 Dmsg1(130, "FT_FIFO saving: %s\n", ff_pkt->fname);
193 be.set_errno(ff_pkt->ff_errno);
194 Jmsg(jcr, M_NOTSAVED, 0, _(" Could not access %s: ERR=%s\n"), ff_pkt->fname,
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,
209 be.set_errno(ff_pkt->ff_errno);
210 Jmsg(jcr, M_NOTSAVED, 0, _(" Could not stat %s: ERR=%s\n"), ff_pkt->fname,
217 Jmsg(jcr, M_SKIPPED, 1, _(" Unchanged file skipped: %s\n"), ff_pkt->fname);
220 Jmsg(jcr, M_NOTSAVED, 0, _(" Archive file not saved: %s\n"), ff_pkt->fname);
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,
231 Jmsg(jcr, M_NOTSAVED, 0, _(" Unknown file type %d; not saved: %s\n"), ff_pkt->type, ff_pkt->fname);
237 if (ff_pkt->flags & FO_PORTABLE) {
238 set_portable_backup(&ff_pkt->bfd); /* disable Win32 BackupRead() */
240 if (ff_pkt->reader) {
241 set_prog(&ff_pkt->bfd, ff_pkt->reader, jcr);
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.
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)) {
254 if (ff_pkt->type == FT_FIFO) {
255 tid = start_thread_timer(pthread_self(), 60);
259 if (bopen(&ff_pkt->bfd, ff_pkt->fname, O_RDONLY | O_BINARY, 0) < 0) {
260 ff_pkt->ff_errno = errno;
262 Jmsg(jcr, M_NOTSAVED, 0, _(" Cannot open %s: ERR=%s.\n"), ff_pkt->fname,
266 stop_thread_timer(tid);
272 stop_thread_timer(tid);
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 */
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;
288 Jmsg(jcr, M_NOTSAVED, -1, _(" Cannot open resource fork for %s: ERR=%s.\n"), ff_pkt->fname,
291 if (is_bopen(&ff_pkt->bfd)) {
292 bclose(&ff_pkt->bfd);
300 Dmsg1(130, "bfiled: sending %s to stored\n", ff_pkt->fname);
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);
306 /* Now possibly extend the attributes */
307 attr_stream = encode_attribsEx(jcr, attribsEx, ff_pkt);
309 Dmsg3(300, "File %s\nattribs=%s\nattribsEx=%s\n", ff_pkt->fname, attribs, attribsEx);
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);
318 * Send Attributes header to Storage daemon
319 * <file-index> <stream> <info>
321 if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, attr_stream)) {
323 if (is_bopen(&ff_pkt->bfd)) {
324 bclose(&ff_pkt->bfd);
326 if (is_bopen(&ff_pkt->rsrc_bfd)) {
327 bclose(&ff_pkt->rsrc_bfd);
329 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
333 Dmsg1(300, ">stored: attrhdr %s\n", sd->msg);
336 * Send file attributes to Storage daemon
339 * Filename (full path)
341 * Link name (if type==FT_LNK or FT_LNKSAVED)
342 * Encoded extended-attributes (for Win32)
344 * For a directory, link is the same as fname, but with trailing
345 * slash. For a linked file, link is the link.
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,
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);
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);
361 Dmsg2(300, ">stored: attr len=%d: %s\n", sd->msglen, sd->msg);
364 if (is_bopen(&ff_pkt->bfd)) {
365 bclose(&ff_pkt->bfd);
367 if (is_bopen(&ff_pkt->rsrc_bfd)) {
368 bclose(&ff_pkt->rsrc_bfd);
370 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
374 bnet_sig(sd, BNET_EOD); /* indicate end of attributes data */
376 if (is_bopen(&ff_pkt->bfd) || hfsplus) {
377 if (ff_pkt->flags & FO_MD5) {
379 } else if (ff_pkt->flags & FO_SHA1) {
385 * If the file has data, read it and send to the Storage daemon
388 if (is_bopen(&ff_pkt->bfd)) {
389 uint64_t fileAddr = 0; /* file address */
391 int rsize = jcr->buf_size; /* read buffer size */
394 rbuf = sd->msg; /* read buffer */
395 wbuf = sd->msg; /* write buffer */
398 Dmsg1(300, "Saving data, type=%d\n", ff_pkt->type);
402 uLong compress_len, max_compress_len = 0;
403 const Bytef *cbuf = NULL;
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;
410 cbuf = (Bytef *)jcr->compress_buf;
411 max_compress_len = jcr->compress_buf_size; /* set max length */
413 wbuf = jcr->compress_buf; /* compressed output here */
418 * Send Data header to Storage daemon
419 * <file-index> <stream> <info>
421 if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, data_stream)) {
423 bclose(&ff_pkt->bfd);
424 if (is_bopen(&ff_pkt->rsrc_bfd)) {
425 bclose(&ff_pkt->rsrc_bfd);
427 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
431 Dmsg1(300, ">stored: datahdr %s\n", sd->msg);
434 * Make space at beginning of buffer for fileAddr because this
435 * same buffer will be used for writing if compression if off.
437 if (ff_pkt->flags & FO_SPARSE) {
438 rbuf += SPARSE_FADDR_SIZE;
439 rsize -= SPARSE_FADDR_SIZE;
440 #ifdef HAVE_FREEBSD_OS
442 * To read FreeBSD partitions, the read size must be
445 rsize = (rsize/512) * 512;
452 while ((sd->msglen=(uint32_t)bread(&ff_pkt->bfd, rbuf, rsize)) > 0) {
455 /* Check for sparse blocks */
456 if (ff_pkt->flags & FO_SPARSE) {
458 if (sd->msglen == rsize &&
459 (fileAddr+sd->msglen < (uint64_t)ff_pkt->statp.st_size)) {
460 sparseBlock = is_buf_zero(rbuf, rsize);
463 ser_begin(wbuf, SPARSE_FADDR_SIZE);
464 ser_uint64(fileAddr); /* store fileAddr in begin of buffer */
467 jcr->ReadBytes += sd->msglen; /* count bytes read */
468 fileAddr += sd->msglen;
470 /* Update MD5 if requested */
471 if (ff_pkt->flags & FO_MD5) {
472 MD5Update(&md5c, (unsigned char *)rbuf, sd->msglen);
474 } else if (ff_pkt->flags & FO_SHA1) {
475 SHA1Update(&sha1c, (unsigned char *)rbuf, sd->msglen);
480 /* Do compression if turned on */
481 if (!sparseBlock && ff_pkt->flags & FO_GZIP) {
483 compress_len = max_compress_len;
484 Dmsg4(400, "cbuf=0x%x len=%u rbuf=0x%x len=%u\n", cbuf, compress_len,
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);
493 bclose(&ff_pkt->bfd);
494 if (is_bopen(&ff_pkt->rsrc_bfd)) {
495 bclose(&ff_pkt->rsrc_bfd);
497 set_jcr_job_status(jcr, JS_ErrorTerminated);
500 Dmsg2(400, "compressed len=%d uncompressed len=%d\n",
501 compress_len, sd->msglen);
503 sd->msglen = compress_len; /* set compressed length */
507 /* Send the buffer to the Storage daemon */
509 if (ff_pkt->flags & FO_SPARSE) {
510 sd->msglen += SPARSE_FADDR_SIZE; /* include fileAddr in size */
512 sd->msg = wbuf; /* set correct write buffer */
513 if (!bnet_send(sd)) {
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 */
519 bclose(&ff_pkt->bfd);
520 if (is_bopen(&ff_pkt->rsrc_bfd)) {
521 bclose(&ff_pkt->rsrc_bfd);
526 Dmsg1(130, "Send data to SD len=%d\n", sd->msglen);
528 jcr->JobBytes += sd->msglen; /* count bytes saved possibly compressed */
529 sd->msg = msgsave; /* restore read buffer */
531 } /* end while read file data */
534 if (sd->msglen < 0) {
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());
541 bclose(&ff_pkt->bfd); /* close file */
542 if (!bnet_sig(sd, BNET_EOD)) { /* indicate end of file data */
544 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
546 if (is_bopen(&ff_pkt->rsrc_bfd)) {
547 bclose(&ff_pkt->rsrc_bfd);
553 #ifdef HAVE_DARWIN_OS
555 * If resource fork has data, read it and send to the Storage daemon
558 if (is_bopen(&ff_pkt->rsrc_bfd)) {
559 uint64_t fileAddr = 0; /* file address */
560 int rsize = jcr->buf_size; /* read buffer size */
562 Dmsg1(300, "Saving resource fork for \"%s\"", ff_pkt->fname);
565 * Send Data header to Storage daemon
566 * <file-index> <stream> <info>
568 if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, STREAM_MACOS_FORK_DATA)) {
570 bclose(&ff_pkt->rsrc_bfd);
571 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
575 Dmsg1(300, ">stored: datahdr %s\n", sd->msg);
578 * Read the resource fork
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;
584 /* Update MD5 if requested */
585 if (ff_pkt->flags & FO_MD5) {
586 MD5Update(&md5c, (unsigned char *)sd->msg, sd->msglen);
588 } else if (ff_pkt->flags & FO_SHA1) {
589 SHA1Update(&sha1c, (unsigned char *)sd->msg, sd->msglen);
593 /* Send the buffer to the Storage daemon */
594 if (!bnet_send(sd)) {
596 Jmsg2(jcr, M_FATAL, 0, _("Network send error %d to SD. ERR=%s\n"),
597 sd->msglen, bnet_strerror(sd));
599 bclose(&ff_pkt->rsrc_bfd);
602 Dmsg1(130, "Send data to SD len=%d\n", sd->msglen);
604 jcr->JobBytes += sd->msglen; /* count bytes saved possibly compressed */
605 } /* end while read file data */
607 if (sd->msglen < 0) {
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());
614 bclose(&ff_pkt->rsrc_bfd); /* close resource fork */
615 if (!bnet_sig(sd, BNET_EOD)) { /* indicate end of resource fork */
617 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
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);
632 /* Update MD5 if requested */
633 if (ff_pkt->flags & FO_MD5) {
634 MD5Update(&md5c, (unsigned char *)sd->msg, sd->msglen);
636 } else if (ff_pkt->flags & FO_SHA1) {
637 SHA1Update(&sha1c, (unsigned char *)sd->msg, sd->msglen);
641 bnet_sig(sd, BNET_EOD);
648 if (ff_pkt->flags & FO_ACL) {
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);
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);
659 Jmsg1(jcr, M_WARNING, 0, "Error while trying to get ACL of directory: %s!\n", ff_pkt->fname);
662 acl_text = acl_to_any_text(myAcl, NULL, ',', TEXT_ABBREVIATE);
667 acl_t myAcl = acl_get_file(ff_pkt->fname, ACL_TYPE_ACCESS);
669 Jmsg1(jcr, M_WARNING, 0, "Error while trying to get ACL of file: %s!\n", ff_pkt->fname);
672 acl_text = acl_to_any_text(myAcl, NULL, ',', TEXT_ABBREVIATE);
676 /* If there is an ACL, send it to the Storage daemon */
678 sd = jcr->store_bsock;
679 pm_strcpy(&jcr->last_fname, ff_pkt->fname);
685 if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, STREAM_UNIX_ATTRIBUTES_ACL)) {
687 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
692 /* Send the buffer to the storage deamon */
695 sd->msglen = strlen(acl_text) + 1;
696 if (!bnet_send(sd)) {
700 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
703 jcr->JobBytes += sd->msglen;
705 if (!bnet_sig(sd, BNET_EOD)) {
707 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
710 Dmsg1(200, "ACL of file: %s successfully backed up!\n", ff_pkt->fname);
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);
725 bnet_sig(sd, BNET_EOD); /* end of MD5 */
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);
736 bnet_sig(sd, BNET_EOD); /* end of SHA1 */