2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
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 three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
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.
18 You should have received a copy of the GNU Affero 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
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.
29 * Bacula File Daemon backup.c send file attributes and data
30 * to the Storage daemon.
32 * Kern Sibbald, March MM
41 const bool have_darwin_os = true;
43 const bool have_darwin_os = false;
47 const bool have_acl = true;
49 const bool have_acl = false;
52 #if defined(HAVE_XATTR)
53 const bool have_xattr = true;
55 const bool have_xattr = false;
58 /* Forward referenced functions */
59 int save_file(JCR *jcr, FF_PKT *ff_pkt, bool top_level);
60 static int send_data(JCR *jcr, int stream, FF_PKT *ff_pkt, DIGEST *digest, DIGEST *signature_digest);
61 bool encode_and_send_attributes(JCR *jcr, FF_PKT *ff_pkt, int &data_stream);
62 static bool crypto_session_start(JCR *jcr);
63 static void crypto_session_end(JCR *jcr);
64 static bool crypto_session_send(JCR *jcr, BSOCK *sd);
65 static void close_vss_backup_session(JCR *jcr);
68 * Find all the requested files and send them
69 * to the Storage daemon.
71 * Note, we normally carry on a one-way
72 * conversation from this point on with the SD, simply blasting
73 * data to him. To properly know what is going on, we
74 * also run a "heartbeat" monitor which reads the socket and
75 * reacts accordingly (at the moment it has nothing to do
76 * except echo the heartbeat to the Director).
79 bool blast_data_to_storage_daemon(JCR *jcr, char *addr)
83 // TODO landonf: Allow user to specify encryption algorithm
85 sd = jcr->store_bsock;
87 jcr->setJobStatus(JS_Running);
89 Dmsg1(300, "bfiled: opened data connection %d to stored\n", sd->m_fd);
92 CLIENT *client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
96 buf_size = client->max_network_buffer_size;
98 buf_size = 0; /* use default */
100 if (!sd->set_buffer_size(buf_size, BNET_SETBUF_WRITE)) {
101 jcr->setJobStatus(JS_ErrorTerminated);
102 Jmsg(jcr, M_FATAL, 0, _("Cannot set buffer size FD->SD.\n"));
106 jcr->buf_size = sd->msglen;
108 * Adjust for compression so that output buffer is
109 * 12 bytes + 0.1% larger than input buffer plus 18 bytes.
110 * This gives a bit extra plus room for the sparse addr if any.
111 * Note, we adjust the read size to be smaller so that the
112 * same output buffer can be used without growing it.
114 * For LZO1X compression the recommended value is :
115 * output_block_size = input_block_size + (input_block_size / 16) + 64 + 3 + sizeof(comp_stream_header)
117 * The zlib compression workset is initialized here to minimize
118 * the "per file" load. The jcr member is only set, if the init
121 * For the same reason, lzo compression is initialized here.
124 jcr->compress_buf_size = MAX(jcr->buf_size + (jcr->buf_size / 16) + 67 + (int)sizeof(comp_stream_header), jcr->buf_size + ((jcr->buf_size+999) / 1000) + 30);
125 jcr->compress_buf = get_memory(jcr->compress_buf_size);
127 jcr->compress_buf_size = jcr->buf_size + ((jcr->buf_size+999) / 1000) + 30;
128 jcr->compress_buf = get_memory(jcr->compress_buf_size);
132 z_stream *pZlibStream = (z_stream*)malloc(sizeof(z_stream));
134 pZlibStream->zalloc = Z_NULL;
135 pZlibStream->zfree = Z_NULL;
136 pZlibStream->opaque = Z_NULL;
137 pZlibStream->state = Z_NULL;
139 if (deflateInit(pZlibStream, Z_DEFAULT_COMPRESSION) == Z_OK) {
140 jcr->pZLIB_compress_workset = pZlibStream;
148 lzo_voidp pLzoMem = (lzo_voidp) malloc(LZO1X_1_MEM_COMPRESS);
150 if (lzo_init() == LZO_E_OK) {
151 jcr->LZO_compress_workset = pLzoMem;
158 if (!crypto_session_start(jcr)) {
162 set_find_options((FF_PKT *)jcr->ff, jcr->incremental, jcr->mtime);
164 /** in accurate mode, we overload the find_one check function */
166 set_find_changed_function((FF_PKT *)jcr->ff, accurate_check_file);
169 start_heartbeat_monitor(jcr);
172 jcr->acl_data = (acl_data_t *)malloc(sizeof(acl_data_t));
173 memset(jcr->acl_data, 0, sizeof(acl_data_t));
174 jcr->acl_data->u.build = (acl_build_data_t *)malloc(sizeof(acl_build_data_t));
175 memset(jcr->acl_data->u.build, 0, sizeof(acl_build_data_t));
176 jcr->acl_data->u.build->content = get_pool_memory(PM_MESSAGE);
180 jcr->xattr_data = (xattr_data_t *)malloc(sizeof(xattr_data_t));
181 memset(jcr->xattr_data, 0, sizeof(xattr_data_t));
182 jcr->xattr_data->u.build = (xattr_build_data_t *)malloc(sizeof(xattr_build_data_t));
183 memset(jcr->xattr_data->u.build, 0, sizeof(xattr_build_data_t));
184 jcr->xattr_data->u.build->content = get_pool_memory(PM_MESSAGE);
187 /** Subroutine save_file() is called for each file */
188 if (!find_files(jcr, (FF_PKT *)jcr->ff, save_file, plugin_save)) {
189 ok = false; /* error */
190 jcr->setJobStatus(JS_ErrorTerminated);
193 if (have_acl && jcr->acl_data->u.build->nr_errors > 0) {
194 Jmsg(jcr, M_WARNING, 0, _("Encountered %ld acl errors while doing backup\n"),
195 jcr->acl_data->u.build->nr_errors);
197 if (have_xattr && jcr->xattr_data->u.build->nr_errors > 0) {
198 Jmsg(jcr, M_WARNING, 0, _("Encountered %ld xattr errors while doing backup\n"),
199 jcr->xattr_data->u.build->nr_errors);
202 close_vss_backup_session(jcr);
204 accurate_finish(jcr); /* send deleted or base file list to SD */
206 stop_heartbeat_monitor(jcr);
208 sd->signal(BNET_EOD); /* end of sending data */
210 if (have_acl && jcr->acl_data) {
211 free_pool_memory(jcr->acl_data->u.build->content);
212 free(jcr->acl_data->u.build);
214 jcr->acl_data = NULL;
216 if (have_xattr && jcr->xattr_data) {
217 free_pool_memory(jcr->xattr_data->u.build->content);
218 free(jcr->xattr_data->u.build);
219 free(jcr->xattr_data);
220 jcr->xattr_data = NULL;
226 if (jcr->compress_buf) {
227 free_pool_memory(jcr->compress_buf);
228 jcr->compress_buf = NULL;
230 if (jcr->pZLIB_compress_workset) {
231 /* Free the zlib stream */
233 deflateEnd((z_stream *)jcr->pZLIB_compress_workset);
235 free (jcr->pZLIB_compress_workset);
236 jcr->pZLIB_compress_workset = NULL;
238 if (jcr->LZO_compress_workset) {
239 free (jcr->LZO_compress_workset);
240 jcr->LZO_compress_workset = NULL;
243 crypto_session_end(jcr);
246 Dmsg1(100, "end blast_data ok=%d\n", ok);
250 static bool crypto_session_start(JCR *jcr)
252 crypto_cipher_t cipher = CRYPTO_CIPHER_AES_128_CBC;
255 * Create encryption session data and a cached, DER-encoded session data
256 * structure. We use a single session key for each backup, so we'll encode
257 * the session data only once.
259 if (jcr->crypto.pki_encrypt) {
262 /** Create per-job session encryption context */
263 jcr->crypto.pki_session = crypto_session_new(cipher, jcr->crypto.pki_recipients);
265 /** Get the session data size */
266 if (!crypto_session_encode(jcr->crypto.pki_session, (uint8_t *)0, &size)) {
267 Jmsg(jcr, M_FATAL, 0, _("An error occurred while encrypting the stream.\n"));
271 /** Allocate buffer */
272 jcr->crypto.pki_session_encoded = get_memory(size);
274 /** Encode session data */
275 if (!crypto_session_encode(jcr->crypto.pki_session, (uint8_t *)jcr->crypto.pki_session_encoded, &size)) {
276 Jmsg(jcr, M_FATAL, 0, _("An error occurred while encrypting the stream.\n"));
280 /** ... and store the encoded size */
281 jcr->crypto.pki_session_encoded_size = size;
283 /** Allocate the encryption/decryption buffer */
284 jcr->crypto.crypto_buf = get_memory(CRYPTO_CIPHER_MAX_BLOCK_SIZE);
289 static void crypto_session_end(JCR *jcr)
291 if (jcr->crypto.crypto_buf) {
292 free_pool_memory(jcr->crypto.crypto_buf);
293 jcr->crypto.crypto_buf = NULL;
295 if (jcr->crypto.pki_session) {
296 crypto_session_free(jcr->crypto.pki_session);
298 if (jcr->crypto.pki_session_encoded) {
299 free_pool_memory(jcr->crypto.pki_session_encoded);
300 jcr->crypto.pki_session_encoded = NULL;
304 static bool crypto_session_send(JCR *jcr, BSOCK *sd)
308 /** Send our header */
309 Dmsg2(100, "Send hdr fi=%ld stream=%d\n", jcr->JobFiles, STREAM_ENCRYPTED_SESSION_DATA);
310 sd->fsend("%ld %d 0", jcr->JobFiles, STREAM_ENCRYPTED_SESSION_DATA);
313 sd->msg = jcr->crypto.pki_session_encoded;
314 sd->msglen = jcr->crypto.pki_session_encoded_size;
315 jcr->JobBytes += sd->msglen;
317 Dmsg1(100, "Send data len=%d\n", sd->msglen);
320 sd->signal(BNET_EOD);
326 * Called here by find() for each file included.
327 * This is a callback. The original is find_files() above.
329 * Send the file and its data to the Storage daemon.
333 * -1 to ignore file/directory (not used here)
335 int save_file(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
337 bool do_read = false;
338 bool plugin_started = false;
339 bool do_plugin_set = false;
340 int stat, data_stream;
342 DIGEST *digest = NULL;
343 DIGEST *signing_digest = NULL;
344 int digest_stream = STREAM_NONE;
345 SIGNATURE *sig = NULL;
346 bool has_file_data = false;
347 struct save_pkt sp; /* use by option plugin */
348 // TODO landonf: Allow the user to specify the digest algorithm
350 crypto_digest_t signing_algorithm = CRYPTO_DIGEST_SHA256;
352 crypto_digest_t signing_algorithm = CRYPTO_DIGEST_SHA1;
354 BSOCK *sd = jcr->store_bsock;
356 if (jcr->is_canceled() || jcr->is_incomplete()) {
360 jcr->num_files_examined++; /* bump total file count */
362 switch (ff_pkt->type) {
363 case FT_LNKSAVED: /* Hard linked, file already saved */
364 Dmsg2(130, "FT_LNKSAVED hard link: %s => %s\n", ff_pkt->fname, ff_pkt->link);
367 Dmsg1(130, "FT_REGE saving: %s\n", ff_pkt->fname);
368 has_file_data = true;
371 Dmsg1(130, "FT_REG saving: %s\n", ff_pkt->fname);
372 has_file_data = true;
375 Dmsg2(130, "FT_LNK saving: %s -> %s\n", ff_pkt->fname, ff_pkt->link);
377 case FT_RESTORE_FIRST:
378 Dmsg1(100, "FT_RESTORE_FIRST saving: %s\n", ff_pkt->fname);
380 case FT_PLUGIN_CONFIG:
381 Dmsg1(100, "FT_PLUGIN_CONFIG saving: %s\n", ff_pkt->fname);
384 jcr->num_files_examined--; /* correct file count */
385 return 1; /* not used */
387 Jmsg(jcr, M_INFO, 1, _(" Recursion turned off. Will not descend from %s into %s\n"),
388 ff_pkt->top_fname, ff_pkt->fname);
389 ff_pkt->type = FT_DIREND; /* Backup only the directory entry */
392 /* Suppress message for /dev filesystems */
393 if (!is_in_fileset(ff_pkt)) {
394 Jmsg(jcr, M_INFO, 1, _(" %s is a different filesystem. Will not descend from %s into it.\n"),
395 ff_pkt->fname, ff_pkt->top_fname);
397 ff_pkt->type = FT_DIREND; /* Backup only the directory entry */
400 Jmsg(jcr, M_INFO, 1, _(" Disallowed filesystem. Will not descend from %s into %s\n"),
401 ff_pkt->top_fname, ff_pkt->fname);
402 ff_pkt->type = FT_DIREND; /* Backup only the directory entry */
405 Jmsg(jcr, M_INFO, 1, _(" Disallowed drive type. Will not descend into %s\n"),
411 Dmsg1(130, "FT_DIREND: %s\n", ff_pkt->link);
414 Dmsg1(130, "FT_SPEC saving: %s\n", ff_pkt->fname);
415 if (S_ISSOCK(ff_pkt->statp.st_mode)) {
416 Jmsg(jcr, M_SKIPPED, 1, _(" Socket file skipped: %s\n"), ff_pkt->fname);
421 Dmsg1(130, "FT_RAW saving: %s\n", ff_pkt->fname);
422 has_file_data = true;
425 Dmsg1(130, "FT_FIFO saving: %s\n", ff_pkt->fname);
429 Jmsg(jcr, M_NOTSAVED, 0, _(" Could not access \"%s\": ERR=%s\n"), ff_pkt->fname,
430 be.bstrerror(ff_pkt->ff_errno));
436 Jmsg(jcr, M_NOTSAVED, 0, _(" Could not follow link \"%s\": ERR=%s\n"),
437 ff_pkt->fname, be.bstrerror(ff_pkt->ff_errno));
443 Jmsg(jcr, M_NOTSAVED, 0, _(" Could not stat \"%s\": ERR=%s\n"), ff_pkt->fname,
444 be.bstrerror(ff_pkt->ff_errno));
450 Jmsg(jcr, M_SKIPPED, 1, _(" Unchanged file skipped: %s\n"), ff_pkt->fname);
453 Jmsg(jcr, M_NOTSAVED, 0, _(" Archive file not saved: %s\n"), ff_pkt->fname);
457 Jmsg(jcr, M_NOTSAVED, 0, _(" Could not open directory \"%s\": ERR=%s\n"),
458 ff_pkt->fname, be.bstrerror(ff_pkt->ff_errno));
463 Dmsg1(130, "FT_DELETED: %s\n", ff_pkt->fname);
466 Jmsg(jcr, M_NOTSAVED, 0, _(" Unknown file type %d; not saved: %s\n"),
467 ff_pkt->type, ff_pkt->fname);
472 Dmsg1(130, "bfiled: sending %s to stored\n", ff_pkt->fname);
474 /** Digests and encryption are only useful if there's file data */
477 * Setup for digest handling. If this fails, the digest will be set to NULL
478 * and not used. Note, the digest (file hash) can be any one of the four
481 * The signing digest is a single algorithm depending on
482 * whether or not we have SHA2.
483 * ****FIXME**** the signing algoritm should really be
484 * determined a different way!!!!!! What happens if
485 * sha2 was available during backup but not restore?
487 if (ff_pkt->flags & FO_MD5) {
488 digest = crypto_digest_new(jcr, CRYPTO_DIGEST_MD5);
489 digest_stream = STREAM_MD5_DIGEST;
491 } else if (ff_pkt->flags & FO_SHA1) {
492 digest = crypto_digest_new(jcr, CRYPTO_DIGEST_SHA1);
493 digest_stream = STREAM_SHA1_DIGEST;
495 } else if (ff_pkt->flags & FO_SHA256) {
496 digest = crypto_digest_new(jcr, CRYPTO_DIGEST_SHA256);
497 digest_stream = STREAM_SHA256_DIGEST;
499 } else if (ff_pkt->flags & FO_SHA512) {
500 digest = crypto_digest_new(jcr, CRYPTO_DIGEST_SHA512);
501 digest_stream = STREAM_SHA512_DIGEST;
504 /** Did digest initialization fail? */
505 if (digest_stream != STREAM_NONE && digest == NULL) {
506 Jmsg(jcr, M_WARNING, 0, _("%s digest initialization failed\n"),
507 stream_to_ascii(digest_stream));
511 * Set up signature digest handling. If this fails, the signature digest
512 * will be set to NULL and not used.
514 /* TODO landonf: We should really only calculate the digest once, for
515 * both verification and signing.
517 if (jcr->crypto.pki_sign) {
518 signing_digest = crypto_digest_new(jcr, signing_algorithm);
520 /** Full-stop if a failure occurred initializing the signature digest */
521 if (signing_digest == NULL) {
522 Jmsg(jcr, M_NOTSAVED, 0, _("%s signature digest initialization failed\n"),
523 stream_to_ascii(signing_algorithm));
529 /** Enable encryption */
530 if (jcr->crypto.pki_encrypt) {
531 ff_pkt->flags |= FO_ENCRYPT;
535 /** Initialize the file descriptor we use for data and other streams. */
537 if (ff_pkt->flags & FO_PORTABLE) {
538 set_portable_backup(&ff_pkt->bfd); /* disable Win32 BackupRead() */
541 if (ff_pkt->cmd_plugin) {
542 do_plugin_set = true;
544 /* option and cmd plugin are not compatible together */
545 } else if (ff_pkt->opt_plugin) {
547 /* ask the option plugin what to do with this file */
548 switch (plugin_option_handle_file(jcr, ff_pkt, &sp)) {
550 Dmsg2(10, "Option plugin %s will be used to backup %s\n",
551 ff_pkt->plugin, ff_pkt->fname);
552 do_plugin_set = true;
555 Dmsg2(10, "Option plugin %s decided to skip %s\n",
556 ff_pkt->plugin, ff_pkt->fname);
559 Dmsg2(10, "Option plugin %s decided to let bacula handle %s\n",
560 ff_pkt->plugin, ff_pkt->fname);
566 /* Tell bfile that it needs to call plugin */
567 if (!set_cmd_plugin(&ff_pkt->bfd, jcr)) {
570 send_plugin_name(jcr, sd, true); /* signal start of plugin data */
571 plugin_started = true;
574 /** Send attributes -- must be done after binit() */
575 if (!encode_and_send_attributes(jcr, ff_pkt, data_stream)) {
578 /** Meta data only for restore object */
579 if (IS_FT_OBJECT(ff_pkt->type)) {
582 /** Meta data only for deleted files */
583 if (ff_pkt->type == FT_DELETED) {
586 /** Set up the encryption context and send the session data to the SD */
587 if (has_file_data && jcr->crypto.pki_encrypt) {
588 if (!crypto_session_send(jcr, sd)) {
594 * Open any file with data that we intend to save, then save it.
596 * Note, if is_win32_backup, we must open the Directory so that
597 * the BackupRead will save its permissions and ownership streams.
599 if (ff_pkt->type != FT_LNKSAVED && S_ISREG(ff_pkt->statp.st_mode)) {
601 do_read = !is_portable_backup(&ff_pkt->bfd) || ff_pkt->statp.st_size > 0;
603 do_read = ff_pkt->statp.st_size > 0;
605 } else if (ff_pkt->type == FT_RAW || ff_pkt->type == FT_FIFO ||
606 ff_pkt->type == FT_REPARSE || ff_pkt->type == FT_JUNCTION ||
607 (!is_portable_backup(&ff_pkt->bfd) && ff_pkt->type == FT_DIREND)) {
611 if (ff_pkt->cmd_plugin && !ff_pkt->no_read) {
615 Dmsg2(150, "type=%d do_read=%d\n", ff_pkt->type, do_read);
619 if (ff_pkt->type == FT_FIFO) {
620 tid = start_thread_timer(jcr, pthread_self(), 60);
624 int noatime = ff_pkt->flags & FO_NOATIME ? O_NOATIME : 0;
625 ff_pkt->bfd.reparse_point = (ff_pkt->type == FT_REPARSE ||
626 ff_pkt->type == FT_JUNCTION);
627 if (bopen(&ff_pkt->bfd, ff_pkt->fname, O_RDONLY | O_BINARY | noatime, 0) < 0) {
628 ff_pkt->ff_errno = errno;
630 Jmsg(jcr, M_NOTSAVED, 0, _(" Cannot open \"%s\": ERR=%s.\n"), ff_pkt->fname,
634 stop_thread_timer(tid);
640 stop_thread_timer(tid);
644 stat = send_data(jcr, data_stream, ff_pkt, digest, signing_digest);
646 if (ff_pkt->flags & FO_CHKCHANGES) {
647 has_file_changed(jcr, ff_pkt);
650 bclose(&ff_pkt->bfd);
657 if (have_darwin_os) {
658 /** Regular files can have resource forks and Finder Info */
659 if (ff_pkt->type != FT_LNKSAVED && (S_ISREG(ff_pkt->statp.st_mode) &&
660 ff_pkt->flags & FO_HFSPLUS)) {
661 if (ff_pkt->hfsinfo.rsrclength > 0) {
664 if (!bopen_rsrc(&ff_pkt->bfd, ff_pkt->fname, O_RDONLY | O_BINARY, 0) < 0) {
665 ff_pkt->ff_errno = errno;
667 Jmsg(jcr, M_NOTSAVED, -1, _(" Cannot open resource fork for \"%s\": ERR=%s.\n"),
668 ff_pkt->fname, be.bstrerror());
670 if (is_bopen(&ff_pkt->bfd)) {
671 bclose(&ff_pkt->bfd);
675 flags = ff_pkt->flags;
676 ff_pkt->flags &= ~(FO_COMPRESS|FO_SPARSE|FO_OFFSETS);
677 if (flags & FO_ENCRYPT) {
678 rsrc_stream = STREAM_ENCRYPTED_MACOS_FORK_DATA;
680 rsrc_stream = STREAM_MACOS_FORK_DATA;
682 stat = send_data(jcr, rsrc_stream, ff_pkt, digest, signing_digest);
683 ff_pkt->flags = flags;
684 bclose(&ff_pkt->bfd);
690 Dmsg1(300, "Saving Finder Info for \"%s\"\n", ff_pkt->fname);
691 sd->fsend("%ld %d 0", jcr->JobFiles, STREAM_HFSPLUS_ATTRIBUTES);
692 Dmsg1(300, "bfiled>stored:header %s\n", sd->msg);
693 pm_memcpy(sd->msg, ff_pkt->hfsinfo.fndrinfo, 32);
696 crypto_digest_update(digest, (uint8_t *)sd->msg, sd->msglen);
698 if (signing_digest) {
699 crypto_digest_update(signing_digest, (uint8_t *)sd->msg, sd->msglen);
702 sd->signal(BNET_EOD);
707 * Save ACLs when requested and available for anything not being a symlink
708 * and not being a plugin.
711 if (ff_pkt->flags & FO_ACL && ff_pkt->type != FT_LNK && !ff_pkt->cmd_plugin) {
712 switch (build_acl_streams(jcr, ff_pkt)) {
713 case bacl_exit_fatal:
715 case bacl_exit_error:
717 * Non-fatal errors, count them and when the number is under
718 * ACL_REPORT_ERR_MAX_PER_JOB print the error message set by the
719 * lower level routine in jcr->errmsg.
721 if (jcr->acl_data->u.build->nr_errors < ACL_REPORT_ERR_MAX_PER_JOB) {
722 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
724 jcr->acl_data->u.build->nr_errors++;
733 * Save Extended Attributes when requested and available for all files not
737 if (ff_pkt->flags & FO_XATTR && !ff_pkt->cmd_plugin) {
738 switch (build_xattr_streams(jcr, ff_pkt)) {
739 case bxattr_exit_fatal:
741 case bxattr_exit_error:
743 * Non-fatal errors, count them and when the number is under
744 * XATTR_REPORT_ERR_MAX_PER_JOB print the error message set by the
745 * lower level routine in jcr->errmsg.
747 if (jcr->xattr_data->u.build->nr_errors < XATTR_REPORT_ERR_MAX_PER_JOB) {
748 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
750 jcr->xattr_data->u.build->nr_errors++;
758 /** Terminate the signing digest and send it to the Storage daemon */
759 if (signing_digest) {
762 if ((sig = crypto_sign_new(jcr)) == NULL) {
763 Jmsg(jcr, M_FATAL, 0, _("Failed to allocate memory for crypto signature.\n"));
767 if (!crypto_sign_add_signer(sig, signing_digest, jcr->crypto.pki_keypair)) {
768 Jmsg(jcr, M_FATAL, 0, _("An error occurred while signing the stream.\n"));
772 /** Get signature size */
773 if (!crypto_sign_encode(sig, NULL, &size)) {
774 Jmsg(jcr, M_FATAL, 0, _("An error occurred while signing the stream.\n"));
778 /** Grow the bsock buffer to fit our message if necessary */
779 if (sizeof_pool_memory(sd->msg) < (int32_t)size) {
780 sd->msg = realloc_pool_memory(sd->msg, size);
783 /** Send our header */
784 sd->fsend("%ld %ld 0", jcr->JobFiles, STREAM_SIGNED_DIGEST);
785 Dmsg1(300, "bfiled>stored:header %s\n", sd->msg);
787 /** Encode signature data */
788 if (!crypto_sign_encode(sig, (uint8_t *)sd->msg, &size)) {
789 Jmsg(jcr, M_FATAL, 0, _("An error occurred while signing the stream.\n"));
795 sd->signal(BNET_EOD); /* end of checksum */
798 /** Terminate any digest and send it to Storage daemon */
802 sd->fsend("%ld %d 0", jcr->JobFiles, digest_stream);
803 Dmsg1(300, "bfiled>stored:header %s\n", sd->msg);
805 size = CRYPTO_DIGEST_MAX_SIZE;
807 /** Grow the bsock buffer to fit our message if necessary */
808 if (sizeof_pool_memory(sd->msg) < (int32_t)size) {
809 sd->msg = realloc_pool_memory(sd->msg, size);
812 if (!crypto_digest_finalize(digest, (uint8_t *)sd->msg, &size)) {
813 Jmsg(jcr, M_FATAL, 0, _("An error occurred finalizing signing the stream.\n"));
817 /* Keep the checksum if this file is a hardlink */
818 if (ff_pkt->linked) {
819 ff_pkt_set_link_digest(ff_pkt, digest_stream, sd->msg, size);
824 sd->signal(BNET_EOD); /* end of checksum */
827 /* Check if original file has a digest, and send it */
828 if (ff_pkt->type == FT_LNKSAVED && ff_pkt->digest) {
829 Dmsg2(300, "Link %s digest %d\n", ff_pkt->fname, ff_pkt->digest_len);
830 sd->fsend("%ld %d 0", jcr->JobFiles, ff_pkt->digest_stream);
832 sd->msg = check_pool_memory_size(sd->msg, ff_pkt->digest_len);
833 memcpy(sd->msg, ff_pkt->digest, ff_pkt->digest_len);
834 sd->msglen = ff_pkt->digest_len;
837 sd->signal(BNET_EOD); /* end of hardlink record */
841 rtnstat = jcr->is_canceled() ? 0 : 1; /* good return if not canceled */
844 if (jcr->is_incomplete() || jcr->is_canceled()) {
847 if (plugin_started) {
848 send_plugin_name(jcr, sd, false); /* signal end of plugin data */
850 if (ff_pkt->opt_plugin) {
851 jcr->plugin_sp = NULL; /* sp is local to this function */
852 jcr->plugin_ctx = NULL;
854 jcr->opt_plugin = false;
857 crypto_digest_free(digest);
859 if (signing_digest) {
860 crypto_digest_free(signing_digest);
863 crypto_sign_free(sig);
869 * Send data read from an already open file descriptor.
871 * We return 1 on sucess and 0 on errors.
874 * We use ff_pkt->statp.st_size when FO_SPARSE to know when to stop
876 * Currently this is not a problem as the only other stream, resource forks,
877 * are not handled as sparse files.
879 static int send_data(JCR *jcr, int stream, FF_PKT *ff_pkt, DIGEST *digest,
880 DIGEST *signing_digest)
882 BSOCK *sd = jcr->store_bsock;
883 uint64_t fileAddr = 0; /* file address */
885 int32_t rsize = jcr->buf_size; /* read buffer size */
887 CIPHER_CONTEXT *cipher_ctx = NULL; /* Quell bogus uninitialized warnings */
888 const uint8_t *cipher_input;
889 uint32_t cipher_input_len;
890 uint32_t cipher_block_size;
891 uint32_t encrypted_len;
892 #ifdef FD_NO_SEND_TEST
897 rbuf = sd->msg; /* read buffer */
898 wbuf = sd->msg; /* write buffer */
899 cipher_input = (uint8_t *)rbuf; /* encrypt uncompressed data */
901 Dmsg1(300, "Saving data, type=%d\n", ff_pkt->type);
903 #if defined(HAVE_LIBZ) || defined(HAVE_LZO)
904 uLong compress_len = 0;
905 uLong max_compress_len = 0;
906 const Bytef *cbuf = NULL;
910 if ((ff_pkt->flags & FO_COMPRESS) && ff_pkt->Compress_algo == COMPRESS_GZIP) {
911 if ((ff_pkt->flags & FO_SPARSE) || (ff_pkt->flags & FO_OFFSETS)) {
912 cbuf = (Bytef *)jcr->compress_buf + OFFSET_FADDR_SIZE;
913 max_compress_len = jcr->compress_buf_size - OFFSET_FADDR_SIZE;
915 cbuf = (Bytef *)jcr->compress_buf;
916 max_compress_len = jcr->compress_buf_size; /* set max length */
918 wbuf = jcr->compress_buf; /* compressed output here */
919 cipher_input = (uint8_t *)jcr->compress_buf; /* encrypt compressed data */
922 * Only change zlib parameters if there is no pending operation.
923 * This should never happen as deflatereset is called after each
927 if (((z_stream*)jcr->pZLIB_compress_workset)->total_in == 0) {
928 /** set gzip compression level - must be done per file */
929 if ((zstat=deflateParams((z_stream*)jcr->pZLIB_compress_workset,
930 ff_pkt->Compress_level, Z_DEFAULT_STRATEGY)) != Z_OK) {
931 Jmsg(jcr, M_FATAL, 0, _("Compression deflateParams error: %d\n"), zstat);
932 jcr->setJobStatus(JS_ErrorTerminated);
941 comp_stream_header ch;
943 memset(&ch, 0, sizeof(comp_stream_header));
946 if ((ff_pkt->flags & FO_COMPRESS) && ff_pkt->Compress_algo == COMPRESS_LZO1X) {
947 if ((ff_pkt->flags & FO_SPARSE) || (ff_pkt->flags & FO_OFFSETS)) {
948 cbuf = (Bytef *)jcr->compress_buf + OFFSET_FADDR_SIZE;
949 cbuf2 = (Bytef *)jcr->compress_buf + OFFSET_FADDR_SIZE + sizeof(comp_stream_header);
950 max_compress_len = jcr->compress_buf_size - OFFSET_FADDR_SIZE;
952 cbuf = (Bytef *)jcr->compress_buf;
953 cbuf2 = (Bytef *)jcr->compress_buf + sizeof(comp_stream_header);
954 max_compress_len = jcr->compress_buf_size; /* set max length */
956 ch.magic = COMPRESS_LZO1X;
957 ch.version = COMP_HEAD_VERSION;
958 wbuf = jcr->compress_buf; /* compressed output here */
959 cipher_input = (uint8_t *)jcr->compress_buf; /* encrypt compressed data */
963 const uint32_t max_compress_len = 0;
966 if (ff_pkt->flags & FO_ENCRYPT) {
967 if ((ff_pkt->flags & FO_SPARSE) || (ff_pkt->flags & FO_OFFSETS)) {
968 Jmsg0(jcr, M_FATAL, 0, _("Encrypting sparse or offset data not supported.\n"));
971 /** Allocate the cipher context */
972 if ((cipher_ctx = crypto_cipher_new(jcr->crypto.pki_session, true,
973 &cipher_block_size)) == NULL) {
974 /* Shouldn't happen! */
975 Jmsg0(jcr, M_FATAL, 0, _("Failed to initialize encryption context.\n"));
980 * Grow the crypto buffer, if necessary.
981 * crypto_cipher_update() will buffer up to (cipher_block_size - 1).
982 * We grow crypto_buf to the maximum number of blocks that
983 * could be returned for the given read buffer size.
984 * (Using the larger of either rsize or max_compress_len)
986 jcr->crypto.crypto_buf = check_pool_memory_size(jcr->crypto.crypto_buf,
987 (MAX(rsize + (int)sizeof(uint32_t), (int32_t)max_compress_len) +
988 cipher_block_size - 1) / cipher_block_size * cipher_block_size);
990 wbuf = jcr->crypto.crypto_buf; /* Encrypted, possibly compressed output here. */
994 * Send Data header to Storage daemon
995 * <file-index> <stream> <info>
997 if (!sd->fsend("%ld %d 0", jcr->JobFiles, stream)) {
998 if (!jcr->is_job_canceled()) {
999 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
1004 Dmsg1(300, ">stored: datahdr %s\n", sd->msg);
1007 * Make space at beginning of buffer for fileAddr because this
1008 * same buffer will be used for writing if compression is off.
1010 if ((ff_pkt->flags & FO_SPARSE) || (ff_pkt->flags & FO_OFFSETS)) {
1011 rbuf += OFFSET_FADDR_SIZE;
1012 rsize -= OFFSET_FADDR_SIZE;
1013 #ifdef HAVE_FREEBSD_OS
1015 * To read FreeBSD partitions, the read size must be
1016 * a multiple of 512.
1018 rsize = (rsize/512) * 512;
1022 /** a RAW device read on win32 only works if the buffer is a multiple of 512 */
1024 if (S_ISBLK(ff_pkt->statp.st_mode))
1025 rsize = (rsize/512) * 512;
1029 * Read the file data
1031 while ((sd->msglen=(uint32_t)bread(&ff_pkt->bfd, rbuf, rsize)) > 0) {
1033 /** Check for sparse blocks */
1034 if (ff_pkt->flags & FO_SPARSE) {
1036 bool allZeros = false;
1037 if ((sd->msglen == rsize &&
1038 fileAddr+sd->msglen < (uint64_t)ff_pkt->statp.st_size) ||
1039 ((ff_pkt->type == FT_RAW || ff_pkt->type == FT_FIFO) &&
1040 (uint64_t)ff_pkt->statp.st_size == 0)) {
1041 allZeros = is_buf_zero(rbuf, rsize);
1044 /** Put file address as first data in buffer */
1045 ser_begin(wbuf, OFFSET_FADDR_SIZE);
1046 ser_uint64(fileAddr); /* store fileAddr in begin of buffer */
1048 fileAddr += sd->msglen; /* update file address */
1049 /** Skip block of all zeros */
1051 continue; /* skip block of zeros */
1053 } else if (ff_pkt->flags & FO_OFFSETS) {
1055 ser_begin(wbuf, OFFSET_FADDR_SIZE);
1056 ser_uint64(ff_pkt->bfd.offset); /* store offset in begin of buffer */
1059 jcr->ReadBytes += sd->msglen; /* count bytes read */
1061 /** Uncompressed cipher input length */
1062 cipher_input_len = sd->msglen;
1064 /** Update checksum if requested */
1066 crypto_digest_update(digest, (uint8_t *)rbuf, sd->msglen);
1069 /** Update signing digest if requested */
1070 if (signing_digest) {
1071 crypto_digest_update(signing_digest, (uint8_t *)rbuf, sd->msglen);
1075 /** Do compression if turned on */
1076 if (ff_pkt->flags & FO_COMPRESS && ff_pkt->Compress_algo == COMPRESS_GZIP && jcr->pZLIB_compress_workset) {
1077 Dmsg3(400, "cbuf=0x%x rbuf=0x%x len=%u\n", cbuf, rbuf, sd->msglen);
1079 ((z_stream*)jcr->pZLIB_compress_workset)->next_in = (Bytef *)rbuf;
1080 ((z_stream*)jcr->pZLIB_compress_workset)->avail_in = sd->msglen;
1081 ((z_stream*)jcr->pZLIB_compress_workset)->next_out = (Bytef *)cbuf;
1082 ((z_stream*)jcr->pZLIB_compress_workset)->avail_out = max_compress_len;
1084 if ((zstat=deflate((z_stream*)jcr->pZLIB_compress_workset, Z_FINISH)) != Z_STREAM_END) {
1085 Jmsg(jcr, M_FATAL, 0, _("Compression deflate error: %d\n"), zstat);
1086 jcr->setJobStatus(JS_ErrorTerminated);
1089 compress_len = ((z_stream*)jcr->pZLIB_compress_workset)->total_out;
1090 /** reset zlib stream to be able to begin from scratch again */
1091 if ((zstat=deflateReset((z_stream*)jcr->pZLIB_compress_workset)) != Z_OK) {
1092 Jmsg(jcr, M_FATAL, 0, _("Compression deflateReset error: %d\n"), zstat);
1093 jcr->setJobStatus(JS_ErrorTerminated);
1097 Dmsg2(400, "GZIP compressed len=%d uncompressed len=%d\n", compress_len,
1100 sd->msglen = compress_len; /* set compressed length */
1101 cipher_input_len = compress_len;
1105 /** Do compression if turned on */
1106 if (ff_pkt->flags & FO_COMPRESS && ff_pkt->Compress_algo == COMPRESS_LZO1X && jcr->LZO_compress_workset) {
1107 lzo_uint len; /* TODO: See with the latest patch how to handle lzo_uint with 64bit */
1110 ser_begin(cbuf, sizeof(comp_stream_header));
1112 Dmsg3(400, "cbuf=0x%x rbuf=0x%x len=%u\n", cbuf, rbuf, sd->msglen);
1114 lzores = lzo1x_1_compress((const unsigned char*)rbuf, sd->msglen, cbuf2,
1115 &len, jcr->LZO_compress_workset);
1117 if (lzores == LZO_E_OK && compress_len <= max_compress_len)
1119 /* complete header */
1120 ser_uint32(COMPRESS_LZO1X);
1121 ser_uint32(compress_len);
1122 ser_uint16(ch.level);
1123 ser_uint16(ch.version);
1125 /** this should NEVER happen */
1126 Jmsg(jcr, M_FATAL, 0, _("Compression LZO error: %d\n"), lzores);
1127 jcr->setJobStatus(JS_ErrorTerminated);
1131 Dmsg2(400, "LZO compressed len=%d uncompressed len=%d\n", compress_len,
1134 compress_len += sizeof(comp_stream_header); /* add size of header */
1135 sd->msglen = compress_len; /* set compressed length */
1136 cipher_input_len = compress_len;
1141 * Note, here we prepend the current record length to the beginning
1142 * of the encrypted data. This is because both sparse and compression
1143 * restore handling want records returned to them with exactly the
1144 * same number of bytes that were processed in the backup handling.
1145 * That is, both are block filters rather than a stream. When doing
1146 * compression, the compression routines may buffer data, so that for
1147 * any one record compressed, when it is decompressed the same size
1148 * will not be obtained. Of course, the buffered data eventually comes
1149 * out in subsequent crypto_cipher_update() calls or at least
1150 * when crypto_cipher_finalize() is called. Unfortunately, this
1151 * "feature" of encryption enormously complicates the restore code.
1153 if (ff_pkt->flags & FO_ENCRYPT) {
1154 uint32_t initial_len = 0;
1157 if ((ff_pkt->flags & FO_SPARSE) || (ff_pkt->flags & FO_OFFSETS)) {
1158 cipher_input_len += OFFSET_FADDR_SIZE;
1161 /** Encrypt the length of the input block */
1162 uint8_t packet_len[sizeof(uint32_t)];
1164 ser_begin(packet_len, sizeof(uint32_t));
1165 ser_uint32(cipher_input_len); /* store data len in begin of buffer */
1166 Dmsg1(20, "Encrypt len=%d\n", cipher_input_len);
1168 if (!crypto_cipher_update(cipher_ctx, packet_len, sizeof(packet_len),
1169 (uint8_t *)jcr->crypto.crypto_buf, &initial_len)) {
1170 /** Encryption failed. Shouldn't happen. */
1171 Jmsg(jcr, M_FATAL, 0, _("Encryption error\n"));
1175 /** Encrypt the input block */
1176 if (crypto_cipher_update(cipher_ctx, cipher_input, cipher_input_len,
1177 (uint8_t *)&jcr->crypto.crypto_buf[initial_len], &encrypted_len)) {
1178 if ((initial_len + encrypted_len) == 0) {
1179 /** No full block of data available, read more data */
1182 Dmsg2(400, "encrypted len=%d unencrypted len=%d\n", encrypted_len,
1184 sd->msglen = initial_len + encrypted_len; /* set encrypted length */
1186 /** Encryption failed. Shouldn't happen. */
1187 Jmsg(jcr, M_FATAL, 0, _("Encryption error\n"));
1192 /* Send the buffer to the Storage daemon */
1193 if ((ff_pkt->flags & FO_SPARSE) || (ff_pkt->flags & FO_OFFSETS)) {
1194 sd->msglen += OFFSET_FADDR_SIZE; /* include fileAddr in size */
1196 sd->msg = wbuf; /* set correct write buffer */
1198 if (!jcr->is_job_canceled()) {
1199 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
1204 Dmsg1(130, "Send data to SD len=%d\n", sd->msglen);
1206 jcr->JobBytes += sd->msglen; /* count bytes saved possibly compressed/encrypted */
1207 sd->msg = msgsave; /* restore read buffer */
1209 } /* end while read file data */
1211 if (sd->msglen < 0) { /* error */
1213 Jmsg(jcr, M_ERROR, 0, _("Read error on file %s. ERR=%s\n"),
1214 ff_pkt->fname, be.bstrerror(ff_pkt->bfd.berrno));
1215 if (jcr->JobErrors++ > 1000) { /* insanity check */
1216 Jmsg(jcr, M_FATAL, 0, _("Too many errors. JobErrors=%d.\n"), jcr->JobErrors);
1218 } else if (ff_pkt->flags & FO_ENCRYPT) {
1220 * For encryption, we must call finalize to push out any
1223 if (!crypto_cipher_finalize(cipher_ctx, (uint8_t *)jcr->crypto.crypto_buf,
1225 /* Padding failed. Shouldn't happen. */
1226 Jmsg(jcr, M_FATAL, 0, _("Encryption padding error\n"));
1230 /** Note, on SSL pre-0.9.7, there is always some output */
1231 if (encrypted_len > 0) {
1232 sd->msglen = encrypted_len; /* set encrypted length */
1233 sd->msg = jcr->crypto.crypto_buf; /* set correct write buffer */
1235 if (!jcr->is_job_canceled()) {
1236 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
1241 Dmsg1(130, "Send data to SD len=%d\n", sd->msglen);
1242 jcr->JobBytes += sd->msglen; /* count bytes saved possibly compressed/encrypted */
1243 sd->msg = msgsave; /* restore bnet buffer */
1247 if (!sd->signal(BNET_EOD)) { /* indicate end of file data */
1248 if (!jcr->is_job_canceled()) {
1249 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
1255 /** Free the cipher context */
1257 crypto_cipher_free(cipher_ctx);
1262 /** Free the cipher context */
1264 crypto_cipher_free(cipher_ctx);
1267 sd->msg = msgsave; /* restore bnet buffer */
1272 bool encode_and_send_attributes(JCR *jcr, FF_PKT *ff_pkt, int &data_stream)
1274 BSOCK *sd = jcr->store_bsock;
1275 char attribs[MAXSTRING];
1276 char attribsExBuf[MAXSTRING];
1277 char *attribsEx = NULL;
1281 int hangup = get_hangup();
1282 #ifdef FD_NO_SEND_TEST
1286 Dmsg1(300, "encode_and_send_attrs fname=%s\n", ff_pkt->fname);
1287 /** Find what data stream we will use, then encode the attributes */
1288 if ((data_stream = select_data_stream(ff_pkt)) == STREAM_NONE) {
1289 /* This should not happen */
1290 Jmsg0(jcr, M_FATAL, 0, _("Invalid file flags, no supported data stream type.\n"));
1293 encode_stat(attribs, &ff_pkt->statp, sizeof(ff_pkt->statp), ff_pkt->LinkFI, data_stream);
1295 /** Now possibly extend the attributes */
1296 if (IS_FT_OBJECT(ff_pkt->type)) {
1297 attr_stream = STREAM_RESTORE_OBJECT;
1299 attribsEx = attribsExBuf;
1300 attr_stream = encode_attribsEx(jcr, attribsEx, ff_pkt);
1303 Dmsg3(300, "File %s\nattribs=%s\nattribsEx=%s\n", ff_pkt->fname, attribs, attribsEx);
1306 jcr->JobFiles++; /* increment number of files sent */
1307 ff_pkt->FileIndex = jcr->JobFiles; /* return FileIndex */
1308 pm_strcpy(jcr->last_fname, ff_pkt->fname);
1311 /* Debug code: check if we must hangup */
1312 if (hangup && (jcr->JobFiles > (uint32_t)hangup)) {
1313 jcr->setJobStatus(JS_Incomplete);
1314 Jmsg1(jcr, M_FATAL, 0, "Debug hangup requested after %d files.\n", hangup);
1320 * Send Attributes header to Storage daemon
1321 * <file-index> <stream> <info>
1323 if (!sd->fsend("%ld %d 0", jcr->JobFiles, attr_stream)) {
1324 if (!jcr->is_canceled() && !jcr->is_incomplete()) {
1325 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
1330 Dmsg1(300, ">stored: attrhdr %s\n", sd->msg);
1333 * Send file attributes to Storage daemon
1336 * Filename (full path)
1337 * Encoded attributes
1338 * Link name (if type==FT_LNK or FT_LNKSAVED)
1339 * Encoded extended-attributes (for Win32)
1341 * or send Restore Object to Storage daemon
1345 * Object_len (possibly compressed)
1346 * Object_full_len (not compressed)
1347 * Object_compression
1350 * Binary Object data
1352 * For a directory, link is the same as fname, but with trailing
1353 * slash. For a linked file, link is the link.
1355 if (ff_pkt->type != FT_DELETED) { /* already stripped */
1358 switch (ff_pkt->type) {
1361 Dmsg3(300, "Link %d %s to %s\n", jcr->JobFiles, ff_pkt->fname, ff_pkt->link);
1362 stat = sd->fsend("%ld %d %s%c%s%c%s%c%s%c%u%c", jcr->JobFiles,
1363 ff_pkt->type, ff_pkt->fname, 0, attribs, 0,
1364 ff_pkt->link, 0, attribsEx, 0, ff_pkt->delta_seq, 0);
1369 /* Here link is the canonical filename (i.e. with trailing slash) */
1370 stat = sd->fsend("%ld %d %s%c%s%c%c%s%c%u%c", jcr->JobFiles,
1371 ff_pkt->type, ff_pkt->link, 0, attribs, 0, 0,
1372 attribsEx, 0, ff_pkt->delta_seq, 0);
1374 case FT_PLUGIN_CONFIG:
1375 case FT_RESTORE_FIRST:
1376 comp_len = ff_pkt->object_len;
1377 ff_pkt->object_compression = 0;
1378 if (ff_pkt->object_len > 1000) {
1379 /* Big object, compress it */
1380 comp_len = ff_pkt->object_len + 1000;
1381 POOLMEM *comp_obj = get_memory(comp_len);
1382 /* *** FIXME *** check Zdeflate error */
1383 Zdeflate(ff_pkt->object, ff_pkt->object_len, comp_obj, comp_len);
1384 if (comp_len < ff_pkt->object_len) {
1385 ff_pkt->object = comp_obj;
1386 ff_pkt->object_compression = 1; /* zlib level 9 compression */
1388 /* Uncompressed object smaller, use it */
1389 comp_len = ff_pkt->object_len;
1391 Dmsg2(100, "Object compressed from %d to %d bytes\n", ff_pkt->object_len, comp_len);
1393 sd->msglen = Mmsg(sd->msg, "%d %d %d %d %d %d %s%c%s%c",
1394 jcr->JobFiles, ff_pkt->type, ff_pkt->object_index,
1395 comp_len, ff_pkt->object_len, ff_pkt->object_compression,
1396 ff_pkt->fname, 0, ff_pkt->object_name, 0);
1397 sd->msg = check_pool_memory_size(sd->msg, sd->msglen + comp_len + 2);
1398 memcpy(sd->msg + sd->msglen, ff_pkt->object, comp_len);
1399 /* Note we send one extra byte so Dir can store zero after object */
1400 sd->msglen += comp_len + 1;
1402 if (ff_pkt->object_compression) {
1403 free_and_null_pool_memory(ff_pkt->object);
1407 stat = sd->fsend("%ld %d %s%c%s%c%c%s%c%d%c", jcr->JobFiles,
1408 ff_pkt->type, ff_pkt->fname, 0, attribs, 0, 0, attribsEx, 0,
1409 ff_pkt->delta_seq, 0);
1412 stat = sd->fsend("%ld %d %s%c%s%c%c%s%c%u%c", jcr->JobFiles,
1413 ff_pkt->type, ff_pkt->fname, 0, attribs, 0, 0,
1414 attribsEx, 0, ff_pkt->delta_seq, 0);
1417 if (ff_pkt->type != FT_DELETED) {
1418 unstrip_path(ff_pkt);
1421 Dmsg2(300, ">stored: attr len=%d: %s\n", sd->msglen, sd->msg);
1422 if (!stat && !jcr->is_job_canceled()) {
1423 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
1426 sd->signal(BNET_EOD); /* indicate end of attributes data */
1431 * Do in place strip of path
1433 static bool do_strip(int count, char *in)
1439 /** Copy to first path separator -- Win32 might have c: ... */
1440 while (*in && !IsPathSeparator(*in)) {
1443 if (*in) { /* Not at the end of the string */
1445 numsep++; /* one separator seen */
1447 for (stripped=0; stripped<count && *in; stripped++) {
1448 while (*in && !IsPathSeparator(*in)) {
1449 in++; /* skip chars */
1452 numsep++; /* count separators seen */
1453 in++; /* skip separator */
1457 while (*in) { /* copy to end */
1458 if (IsPathSeparator(*in)) {
1464 Dmsg4(500, "stripped=%d count=%d numsep=%d sep>count=%d\n",
1465 stripped, count, numsep, numsep>count);
1466 return stripped==count && numsep>count;
1470 * If requested strip leading components of the path so that we can
1471 * save file as if it came from a subdirectory. This is most useful
1472 * for dealing with snapshots, by removing the snapshot directory, or
1473 * in handling vendor migrations where files have been restored with
1474 * a vendor product into a subdirectory.
1476 void strip_path(FF_PKT *ff_pkt)
1478 if (!(ff_pkt->flags & FO_STRIPPATH) || ff_pkt->strip_path <= 0) {
1479 Dmsg1(200, "No strip for %s\n", ff_pkt->fname);
1482 if (!ff_pkt->fname_save) {
1483 ff_pkt->fname_save = get_pool_memory(PM_FNAME);
1484 ff_pkt->link_save = get_pool_memory(PM_FNAME);
1486 pm_strcpy(ff_pkt->fname_save, ff_pkt->fname);
1487 if (ff_pkt->type != FT_LNK && ff_pkt->fname != ff_pkt->link) {
1488 pm_strcpy(ff_pkt->link_save, ff_pkt->link);
1489 Dmsg2(500, "strcpy link_save=%d link=%d\n", strlen(ff_pkt->link_save),
1490 strlen(ff_pkt->link));
1495 * Strip path. If it doesn't succeed put it back. If
1496 * it does, and there is a different link string,
1497 * attempt to strip the link. If it fails, back them
1499 * Do not strip symlinks.
1500 * I.e. if either stripping fails don't strip anything.
1502 if (!do_strip(ff_pkt->strip_path, ff_pkt->fname)) {
1503 unstrip_path(ff_pkt);
1506 /** Strip links but not symlinks */
1507 if (ff_pkt->type != FT_LNK && ff_pkt->fname != ff_pkt->link) {
1508 if (!do_strip(ff_pkt->strip_path, ff_pkt->link)) {
1509 unstrip_path(ff_pkt);
1514 Dmsg3(100, "fname=%s stripped=%s link=%s\n", ff_pkt->fname_save, ff_pkt->fname,
1518 void unstrip_path(FF_PKT *ff_pkt)
1520 if (!(ff_pkt->flags & FO_STRIPPATH) || ff_pkt->strip_path <= 0) {
1523 strcpy(ff_pkt->fname, ff_pkt->fname_save);
1524 if (ff_pkt->type != FT_LNK && ff_pkt->fname != ff_pkt->link) {
1525 Dmsg2(500, "strcpy link=%s link_save=%s\n", ff_pkt->link,
1527 strcpy(ff_pkt->link, ff_pkt->link_save);
1528 Dmsg2(500, "strcpy link=%d link_save=%d\n", strlen(ff_pkt->link),
1529 strlen(ff_pkt->link_save));
1534 static void close_vss_backup_session(JCR *jcr)
1536 #if defined(WIN32_VSS)
1537 /* STOP VSS ON WIN32 */
1538 /* tell vss to close the backup session */
1540 if (g_pVSSClient->CloseBackup()) {
1541 /* inform user about writer states */
1542 for (int i=0; i<(int)g_pVSSClient->GetWriterCount(); i++) {
1543 int msg_type = M_INFO;
1544 if (g_pVSSClient->GetWriterState(i) < 1) {
1545 msg_type = M_WARNING;
1548 Jmsg(jcr, msg_type, 0, _("VSS Writer (BackupComplete): %s\n"), g_pVSSClient->GetWriterInfo(i));
1551 /* Generate Job global writer metadata */
1552 WCHAR *metadata = g_pVSSClient->GetMetadata();
1554 FF_PKT *ff_pkt = jcr->ff;
1555 ff_pkt->fname = (char *)"*all*"; /* for all plugins */
1556 ff_pkt->type = FT_RESTORE_FIRST;
1558 ff_pkt->object_name = (char *)"job_metadata.xml";
1559 ff_pkt->object = (char *)metadata;
1560 ff_pkt->object_len = (wcslen(metadata) + 1) * sizeof(WCHAR);
1561 ff_pkt->object_index = (int)time(NULL);
1562 save_file(jcr, ff_pkt, true);