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-2005 Kern Sibbald
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,
33 static int save_file(FF_PKT *ff_pkt, void *pkt);
34 static int send_data(JCR *jcr, int stream, FF_PKT *ff_pkt, struct CHKSUM *chksum);
36 static int read_and_send_acl(JCR *jcr, int acltype, int stream);
40 * Find all the requested files and send them
41 * to the Storage daemon.
43 * Note, we normally carry on a one-way
44 * conversation from this point on with the SD, simply blasting
45 * data to him. To properly know what is going on, we
46 * also run a "heartbeat" monitor which reads the socket and
47 * reacts accordingly (at the moment it has nothing to do
48 * except echo the heartbeat to the Director).
51 bool blast_data_to_storage_daemon(JCR *jcr, char *addr)
56 sd = jcr->store_bsock;
58 set_jcr_job_status(jcr, JS_Running);
60 Dmsg1(300, "bfiled: opened data connection %d to stored\n", sd->fd);
63 CLIENT *client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
67 buf_size = client->max_network_buffer_size;
69 buf_size = 0; /* use default */
71 if (!bnet_set_buffer_size(sd, buf_size, BNET_SETBUF_WRITE)) {
72 set_jcr_job_status(jcr, JS_ErrorTerminated);
73 Jmsg(jcr, M_FATAL, 0, _("Cannot set buffer size FD->SD.\n"));
77 jcr->buf_size = sd->msglen;
78 /* Adjust for compression so that output buffer is
79 * 12 bytes + 0.1% larger than input buffer plus 18 bytes.
80 * This gives a bit extra plus room for the sparse addr if any.
81 * Note, we adjust the read size to be smaller so that the
82 * same output buffer can be used without growing it.
84 jcr->compress_buf_size = jcr->buf_size + ((jcr->buf_size+999) / 1000) + 30;
85 jcr->compress_buf = get_memory(jcr->compress_buf_size);
87 Dmsg1(300, "set_find_options ff=%p\n", jcr->ff);
88 set_find_options((FF_PKT *)jcr->ff, jcr->incremental, jcr->mtime);
89 Dmsg0(300, "start find files\n");
91 start_heartbeat_monitor(jcr);
93 jcr->acl_text = get_pool_memory(PM_MESSAGE);
95 /* Subroutine save_file() is called for each file */
96 if (!find_files(jcr, (FF_PKT *)jcr->ff, save_file, (void *)jcr)) {
97 ok = false; /* error */
98 set_jcr_job_status(jcr, JS_ErrorTerminated);
99 // Jmsg(jcr, M_FATAL, 0, _("Find files error.\n"));
102 free_pool_memory(jcr->acl_text);
104 stop_heartbeat_monitor(jcr);
106 bnet_sig(sd, BNET_EOD); /* end data connection */
112 if (jcr->compress_buf) {
113 free_pool_memory(jcr->compress_buf);
114 jcr->compress_buf = NULL;
116 Dmsg1(300, "end blast_data stat=%d\n", ok);
121 * Called here by find() for each file included.
122 * This is a callback. The original is find_files() above.
124 * Send the file and its data to the Storage daemon.
128 * -1 to ignore file/directory (not used here)
130 static int save_file(FF_PKT *ff_pkt, void *vjcr)
132 char attribs[MAXSTRING];
133 char attribsEx[MAXSTRING];
134 int stat, attr_stream, data_stream;
135 struct CHKSUM chksum;
137 JCR *jcr = (JCR *)vjcr;
139 if (job_canceled(jcr)) {
143 sd = jcr->store_bsock;
144 jcr->num_files_examined++; /* bump total file count */
146 switch (ff_pkt->type) {
147 case FT_LNKSAVED: /* Hard linked, file already saved */
148 Dmsg2(130, "FT_LNKSAVED hard link: %s => %s\n", ff_pkt->fname, ff_pkt->link);
151 Dmsg1(130, "FT_REGE saving: %s\n", ff_pkt->fname);
154 Dmsg1(130, "FT_REG saving: %s\n", ff_pkt->fname);
157 Dmsg2(130, "FT_LNK saving: %s -> %s\n", ff_pkt->fname, ff_pkt->link);
160 return 1; /* not used */
165 if (ff_pkt->type == FT_NORECURSE) {
166 Jmsg(jcr, M_INFO, 1, _(" Recursion turned off. Will not descend into %s\n"),
168 } else if (ff_pkt->type == FT_NOFSCHG) {
169 Jmsg(jcr, M_INFO, 1, _(" File system change prohibited. Will not descend into %s\n"),
171 } else if (ff_pkt->type == FT_INVALIDFS) {
172 Jmsg(jcr, M_INFO, 1, _(" Disallowed filesystem. Will not descend into %s\n"),
175 ff_pkt->type = FT_DIREND; /* value is used below */
176 Dmsg1(130, "FT_DIR saving: %s\n", ff_pkt->link);
179 Dmsg1(130, "FT_SPEC saving: %s\n", ff_pkt->fname);
182 Dmsg1(130, "FT_RAW saving: %s\n", ff_pkt->fname);
185 Dmsg1(130, "FT_FIFO saving: %s\n", ff_pkt->fname);
189 Jmsg(jcr, M_NOTSAVED, 0, _(" Could not access %s: ERR=%s\n"), ff_pkt->fname,
190 be.strerror(ff_pkt->ff_errno));
196 Jmsg(jcr, M_NOTSAVED, 0, _(" Could not follow link %s: ERR=%s\n"), ff_pkt->fname,
197 be.strerror(ff_pkt->ff_errno));
203 Jmsg(jcr, M_NOTSAVED, 0, _(" Could not stat %s: ERR=%s\n"), ff_pkt->fname,
204 be.strerror(ff_pkt->ff_errno));
210 Jmsg(jcr, M_SKIPPED, 1, _(" Unchanged file skipped: %s\n"), ff_pkt->fname);
213 Jmsg(jcr, M_NOTSAVED, 0, _(" Archive file not saved: %s\n"), ff_pkt->fname);
217 Jmsg(jcr, M_NOTSAVED, 0, _(" Could not open directory %s: ERR=%s\n"), ff_pkt->fname,
218 be.strerror(ff_pkt->ff_errno));
223 Jmsg(jcr, M_NOTSAVED, 0, _(" Unknown file type %d; not saved: %s\n"), ff_pkt->type, ff_pkt->fname);
228 Dmsg1(130, "bfiled: sending %s to stored\n", ff_pkt->fname);
230 /* Find what data stream we will use, then encode the attributes */
231 data_stream = select_data_stream(ff_pkt);
232 encode_stat(attribs, ff_pkt, data_stream);
234 /* Now possibly extend the attributes */
235 attr_stream = encode_attribsEx(jcr, attribsEx, ff_pkt);
237 Dmsg3(300, "File %s\nattribs=%s\nattribsEx=%s\n", ff_pkt->fname, attribs, attribsEx);
240 jcr->JobFiles++; /* increment number of files sent */
241 ff_pkt->FileIndex = jcr->JobFiles; /* return FileIndex */
242 pm_strcpy(jcr->last_fname, ff_pkt->fname);
246 * Send Attributes header to Storage daemon
247 * <file-index> <stream> <info>
249 if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, attr_stream)) {
250 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
254 Dmsg1(300, ">stored: attrhdr %s\n", sd->msg);
257 * Send file attributes to Storage daemon
260 * Filename (full path)
262 * Link name (if type==FT_LNK or FT_LNKSAVED)
263 * Encoded extended-attributes (for Win32)
265 * For a directory, link is the same as fname, but with trailing
266 * slash. For a linked file, link is the link.
268 if (ff_pkt->type == FT_LNK || ff_pkt->type == FT_LNKSAVED) {
269 Dmsg2(300, "Link %s to %s\n", ff_pkt->fname, ff_pkt->link);
270 stat = bnet_fsend(sd, "%ld %d %s%c%s%c%s%c%s%c", jcr->JobFiles,
271 ff_pkt->type, ff_pkt->fname, 0, attribs, 0, ff_pkt->link, 0,
273 } else if (ff_pkt->type == FT_DIREND) {
274 /* Here link is the canonical filename (i.e. with trailing slash) */
275 stat = bnet_fsend(sd, "%ld %d %s%c%s%c%c%s%c", jcr->JobFiles,
276 ff_pkt->type, ff_pkt->link, 0, attribs, 0, 0, attribsEx, 0);
278 stat = bnet_fsend(sd, "%ld %d %s%c%s%c%c%s%c", jcr->JobFiles,
279 ff_pkt->type, ff_pkt->fname, 0, attribs, 0, 0, attribsEx, 0);
282 Dmsg2(300, ">stored: attr len=%d: %s\n", sd->msglen, sd->msg);
284 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
288 bnet_sig(sd, BNET_EOD); /* indicate end of attributes data */
291 * Setup for signature handling.
292 * Then initialise the file descriptor we use for data and other streams.
294 chksum_init(&chksum, ff_pkt->flags);
297 if (ff_pkt->flags & FO_PORTABLE) {
298 set_portable_backup(&ff_pkt->bfd); /* disable Win32 BackupRead() */
300 if (ff_pkt->reader) {
301 set_prog(&ff_pkt->bfd, ff_pkt->reader, jcr);
305 * Open any file with data that we intend to save, then save it.
307 * Note, if is_win32_backup, we must open the Directory so that
308 * the BackupRead will save its permissions and ownership streams.
310 if (ff_pkt->type != FT_LNKSAVED && (S_ISREG(ff_pkt->statp.st_mode) &&
311 ff_pkt->statp.st_size > 0) ||
312 ff_pkt->type == FT_RAW || ff_pkt->type == FT_FIFO ||
313 (!is_portable_backup(&ff_pkt->bfd) && ff_pkt->type == FT_DIREND)) {
315 if (ff_pkt->type == FT_FIFO) {
316 tid = start_thread_timer(pthread_self(), 60);
320 if (bopen(&ff_pkt->bfd, ff_pkt->fname, O_RDONLY | O_BINARY, 0) < 0) {
321 ff_pkt->ff_errno = errno;
323 Jmsg(jcr, M_NOTSAVED, 0, _(" Cannot open %s: ERR=%s.\n"), ff_pkt->fname,
327 stop_thread_timer(tid);
333 stop_thread_timer(tid);
336 stat = send_data(jcr, data_stream, ff_pkt, &chksum);
337 bclose(&ff_pkt->bfd);
343 #ifdef HAVE_DARWIN_OS
344 /* Regular files can have resource forks and Finder Info */
345 if (ff_pkt->type != FT_LNKSAVED && (S_ISREG(ff_pkt->statp.st_mode) &&
346 ff_pkt->flags & FO_HFSPLUS)) {
347 if (ff_pkt->hfsinfo.rsrclength > 0) {
349 if (!bopen_rsrc(&ff_pkt->bfd, ff_pkt->fname, O_RDONLY | O_BINARY, 0) < 0) {
350 ff_pkt->ff_errno = errno;
352 Jmsg(jcr, M_NOTSAVED, -1, _(" Cannot open resource fork for %s: ERR=%s.\n"), ff_pkt->fname,
355 if (is_bopen(&ff_pkt->bfd)) {
356 bclose(&ff_pkt->bfd);
360 flags = ff_pkt->flags;
361 ff_pkt->flags &= ~(FO_GZIP|FO_SPARSE);
362 stat = send_data(jcr, STREAM_MACOS_FORK_DATA, ff_pkt, &chksum);
363 ff_pkt->flags = flags;
364 bclose(&ff_pkt->bfd);
370 Dmsg1(300, "Saving Finder Info for \"%s\"\n", ff_pkt->fname);
371 bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, STREAM_HFSPLUS_ATTRIBUTES);
372 Dmsg1(300, "bfiled>stored:header %s\n", sd->msg);
373 memcpy(sd->msg, ff_pkt->hfsinfo.fndrinfo, 32);
375 chksum_update(&chksum, (unsigned char *)sd->msg, sd->msglen);
377 bnet_sig(sd, BNET_EOD);
382 if (ff_pkt->flags & FO_ACL) {
383 /* Read access ACLs for files, dirs and links */
384 if (!read_and_send_acl(jcr, BACL_TYPE_ACCESS, STREAM_UNIX_ATTRIBUTES_ACCESS_ACL)) {
387 /* Directories can have default ACLs too */
388 if (ff_pkt->type == FT_DIREND && (BACL_CAP & BACL_CAP_DEFAULTS_DIR)) {
389 if (!read_and_send_acl(jcr, BACL_TYPE_DEFAULT, STREAM_UNIX_ATTRIBUTES_DEFAULT_ACL)) {
396 /* Terminate any signature and send it to Storage daemon and the Director */
397 if (chksum.updated) {
399 chksum_final(&chksum);
400 if (chksum.type == CHKSUM_MD5) {
401 stream = STREAM_MD5_SIGNATURE;
402 } else if (chksum.type == CHKSUM_SHA1) {
403 stream = STREAM_SHA1_SIGNATURE;
405 Jmsg1(jcr, M_WARNING, 0, _("Unknown signature type %i.\n"), chksum.type);
408 bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, stream);
409 Dmsg1(300, "bfiled>stored:header %s\n", sd->msg);
410 memcpy(sd->msg, chksum.signature, chksum.length);
411 sd->msglen = chksum.length;
413 bnet_sig(sd, BNET_EOD); /* end of checksum */
421 * Send data read from an already open file descriptor.
423 * We return 1 on sucess and 0 on errors.
426 * We use ff_pkt->statp.st_size when FO_SPARSE.
427 * Currently this is not a problem as the only other stream, resource forks,
428 * are not handled as sparse files.
430 int send_data(JCR *jcr, int stream, FF_PKT *ff_pkt, struct CHKSUM *chksum)
432 BSOCK *sd = jcr->store_bsock;
433 uint64_t fileAddr = 0; /* file address */
435 int rsize = jcr->buf_size; /* read buffer size */
439 rbuf = sd->msg; /* read buffer */
440 wbuf = sd->msg; /* write buffer */
443 Dmsg1(300, "Saving data, type=%d\n", ff_pkt->type);
447 uLong compress_len, max_compress_len = 0;
448 const Bytef *cbuf = NULL;
450 if (ff_pkt->flags & FO_GZIP) {
451 if (ff_pkt->flags & FO_SPARSE) {
452 cbuf = (Bytef *)jcr->compress_buf + SPARSE_FADDR_SIZE;
453 max_compress_len = jcr->compress_buf_size - SPARSE_FADDR_SIZE;
455 cbuf = (Bytef *)jcr->compress_buf;
456 max_compress_len = jcr->compress_buf_size; /* set max length */
458 wbuf = jcr->compress_buf; /* compressed output here */
463 * Send Data header to Storage daemon
464 * <file-index> <stream> <info>
466 if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, stream)) {
467 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
471 Dmsg1(300, ">stored: datahdr %s\n", sd->msg);
474 * Make space at beginning of buffer for fileAddr because this
475 * same buffer will be used for writing if compression if off.
477 if (ff_pkt->flags & FO_SPARSE) {
478 rbuf += SPARSE_FADDR_SIZE;
479 rsize -= SPARSE_FADDR_SIZE;
480 #ifdef HAVE_FREEBSD_OS
482 * To read FreeBSD partitions, the read size must be
485 rsize = (rsize/512) * 512;
492 while ((sd->msglen=(uint32_t)bread(&ff_pkt->bfd, rbuf, rsize)) > 0) {
495 /* Check for sparse blocks */
496 if (ff_pkt->flags & FO_SPARSE) {
498 if (sd->msglen == rsize &&
499 (fileAddr+sd->msglen < (uint64_t)ff_pkt->statp.st_size)) {
500 sparseBlock = is_buf_zero(rbuf, rsize);
503 ser_begin(wbuf, SPARSE_FADDR_SIZE);
504 ser_uint64(fileAddr); /* store fileAddr in begin of buffer */
507 jcr->ReadBytes += sd->msglen; /* count bytes read */
508 fileAddr += sd->msglen;
510 /* Update checksum if requested */
511 chksum_update(chksum, (unsigned char *)rbuf, sd->msglen);
514 /* Do compression if turned on */
515 if (!sparseBlock && ff_pkt->flags & FO_GZIP) {
517 compress_len = max_compress_len;
518 Dmsg4(400, "cbuf=0x%x len=%u rbuf=0x%x len=%u\n", cbuf, compress_len,
520 /* NOTE! This call modifies compress_len !!! */
521 if ((zstat=compress2((Bytef *)cbuf, &compress_len,
522 (const Bytef *)rbuf, (uLong)sd->msglen,
523 ff_pkt->GZIP_level)) != Z_OK) {
524 Jmsg(jcr, M_FATAL, 0, _("Compression error: %d\n"), zstat);
527 set_jcr_job_status(jcr, JS_ErrorTerminated);
530 Dmsg2(400, "compressed len=%d uncompressed len=%d\n",
531 compress_len, sd->msglen);
533 sd->msglen = compress_len; /* set compressed length */
537 /* Send the buffer to the Storage daemon */
539 if (ff_pkt->flags & FO_SPARSE) {
540 sd->msglen += SPARSE_FADDR_SIZE; /* include fileAddr in size */
542 sd->msg = wbuf; /* set correct write buffer */
543 if (!bnet_send(sd)) {
544 Jmsg2(jcr, M_FATAL, 0, _("Network send error %d to SD. ERR=%s\n"),
545 sd->msglen, bnet_strerror(sd));
546 sd->msg = msgsave; /* restore bnet buffer */
551 Dmsg1(130, "Send data to SD len=%d\n", sd->msglen);
553 jcr->JobBytes += sd->msglen; /* count bytes saved possibly compressed */
554 sd->msg = msgsave; /* restore read buffer */
556 } /* end while read file data */
559 if (sd->msglen < 0) {
561 Jmsg(jcr, M_ERROR, 0, _("Read error on file %s. ERR=%s\n"),
562 ff_pkt->fname, be.strerror(ff_pkt->bfd.berrno));
565 if (!bnet_sig(sd, BNET_EOD)) { /* indicate end of file data */
566 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
576 * Read and send an ACL for the last encountered file.
578 static int read_and_send_acl(JCR *jcr, int acltype, int stream)
580 BSOCK *sd = jcr->store_bsock;
584 len = bacl_get(jcr, acltype);
586 Jmsg1(jcr, M_WARNING, 0, "Error reading ACL of %s\n", jcr->last_fname);
590 return 1; /* no ACL */
594 if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, stream)) {
595 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
600 /* Send the buffer to the storage deamon */
601 Dmsg2(400, "Backing up ACL type 0x%2x <%s>\n", acltype, jcr->acl_text);
603 sd->msg = jcr->acl_text;
604 sd->msglen = len + 1;
605 if (!bnet_send(sd)) {
608 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
613 jcr->JobBytes += sd->msglen;
615 if (!bnet_sig(sd, BNET_EOD)) {
616 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
621 Dmsg1(200, "ACL of file: %s successfully backed up!\n", jcr->last_fname);