2 * Bacula File Daemon restore.c Restorefiles.
4 * Kern Sibbald, November MM
10 Copyright (C) 2000-2004 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 #include <acl/libacl.h>
37 /* Data received from Storage Daemon */
38 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
40 /* Forward referenced functions */
42 static const char *zlib_strerror(int stat);
45 #define RETRY 10 /* retry wait time */
48 * Restore the requested files.
51 void do_restore(JCR *jcr)
56 uint32_t VolSessionId, VolSessionTime;
61 uint32_t total = 0; /* Job total but only 32 bits for debug */
62 char *wbuf; /* write buffer */
63 uint32_t wsize; /* write size */
64 uint64_t fileAddr = 0; /* file write address */
65 int non_support_data = 0;
66 int non_support_attr = 0;
67 int non_support_acl = 0;
68 int prog_name_msg = 0;
75 sd = jcr->store_bsock;
76 set_jcr_job_status(jcr, JS_Running);
79 CLIENT *client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
83 buf_size = client->max_network_buffer_size;
85 buf_size = 0; /* use default */
87 if (!bnet_set_buffer_size(sd, buf_size, BNET_SETBUF_WRITE)) {
88 set_jcr_job_status(jcr, JS_ErrorTerminated);
91 jcr->buf_size = sd->msglen;
96 uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
97 jcr->compress_buf = (char *)bmalloc(compress_buf_size);
101 * Get a record from the Storage daemon. We are guaranteed to
102 * receive records in the following order:
103 * 1. Stream record header
105 * a. Attributes (Unix or Win32)
106 * or b. File data for the file
107 * or c. Possibly MD5 or SHA1 record
110 while (bget_msg(sd) >= 0 && !job_canceled(jcr)) {
112 * First we expect a Stream Record Header
114 if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
115 &stream, &size) != 5) {
116 Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
119 Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
122 * Now we expect the Stream Data
124 if (bget_msg(sd) < 0) {
125 Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
128 if (size != (uint32_t)sd->msglen) {
129 Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
132 Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
134 /* File Attributes stream */
136 case STREAM_UNIX_ATTRIBUTES:
137 case STREAM_UNIX_ATTRIBUTES_EX:
138 Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
139 /* If extracting, it was from previous stream, so
140 * close the output file.
143 if (!is_bopen(&bfd)) {
144 Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open\n"));
146 set_attributes(jcr, attr, &bfd);
148 Dmsg0(30, "Stop extracting.\n");
151 if (!unpack_attributes_record(jcr, stream, sd->msg, attr)) {
154 if (file_index != attr->file_index) {
155 Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
156 file_index, attr->file_index);
157 Dmsg0(100, "File index error\n");
161 Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", attr->fname,
162 attr->attr, attr->attrEx);
164 attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
166 if (!is_stream_supported(attr->data_stream)) {
167 if (!non_support_data++) {
168 Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
169 stream_to_ascii(attr->data_stream));
174 build_attr_output_fnames(jcr, attr);
176 jcr->num_files_examined++;
178 Dmsg1(30, "Outfile=%s\n", attr->ofname);
180 stat = create_file(jcr, attr, &bfd, jcr->replace);
188 pm_strcpy(jcr->last_fname, attr->ofname);
192 print_ls_output(jcr, attr);
193 /* Set attributes after file extracted */
197 pm_strcpy(jcr->last_fname, attr->ofname);
201 print_ls_output(jcr, attr);
202 /* set attributes now because file will not be extracted */
203 set_attributes(jcr, attr, &bfd);
209 case STREAM_FILE_DATA:
210 case STREAM_SPARSE_DATA:
211 case STREAM_WIN32_DATA:
213 if (stream == STREAM_SPARSE_DATA) {
218 wbuf = sd->msg + SPARSE_FADDR_SIZE;
219 wsize = sd->msglen - SPARSE_FADDR_SIZE;
220 ser_begin(sd->msg, SPARSE_FADDR_SIZE);
222 if (fileAddr != faddr) {
224 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
226 be.set_errno(bfd.berrno);
227 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
228 edit_uint64(fileAddr, ec1), attr->ofname, be.strerror());
238 Dmsg2(30, "Write %u bytes, total before write=%u\n", wsize, total);
239 if ((uint32_t)bwrite(&bfd, wbuf, wsize) != wsize) {
240 Dmsg0(0, "===Write error===\n");
242 be.set_errno(bfd.berrno);
243 Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: ERR=%s\n"), attr->ofname,
250 jcr->JobBytes += wsize;
251 jcr->ReadBytes += wsize;
256 /* GZIP data stream */
257 case STREAM_GZIP_DATA:
258 case STREAM_SPARSE_GZIP_DATA:
259 case STREAM_WIN32_GZIP_DATA:
265 if (stream == STREAM_SPARSE_GZIP_DATA) {
269 wbuf = sd->msg + SPARSE_FADDR_SIZE;
270 wsize = sd->msglen - SPARSE_FADDR_SIZE;
271 ser_begin(sd->msg, SPARSE_FADDR_SIZE);
273 if (fileAddr != faddr) {
275 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
277 be.set_errno(bfd.berrno);
278 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
279 edit_uint64(fileAddr, ec1), attr->ofname, be.strerror());
289 compress_len = compress_buf_size;
290 Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
291 if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
292 (const Byte *)wbuf, (uLong)wsize)) != Z_OK) {
293 Jmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"),
294 attr->ofname, zlib_strerror(stat));
300 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
301 if ((uLong)bwrite(&bfd, jcr->compress_buf, compress_len) != compress_len) {
302 Dmsg0(0, "===Write error===\n");
304 be.set_errno(bfd.berrno);
305 Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), attr->ofname, be.strerror());
310 total += compress_len;
311 jcr->JobBytes += compress_len;
312 jcr->ReadBytes += wsize;
313 fileAddr += compress_len;
317 Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
325 case STREAM_UNIX_ATTRIBUTES_ACL:
327 /* Recover ACL from stream and check it */
328 acl = acl_from_text(sd->msg);
329 if (acl_valid(acl) != 0) {
330 Jmsg1(jcr, M_WARNING, 0, "Failure in the ACL of %s! FD is not able to restore it!\n", jcr->last_fname);
334 /* Try to restore ACL */
335 if (attr->type == FT_DIREND) {
337 if (acl_set_file(jcr->last_fname, ACL_TYPE_DEFAULT, acl) != 0 &&
338 acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
339 Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of directory: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
342 } else if (acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
343 Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of file: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
346 Dmsg1(200, "ACL of file: %s successfully restored!", jcr->last_fname);
350 break; /* unconfigured, ignore */
353 case STREAM_MD5_SIGNATURE:
354 case STREAM_SHA1_SIGNATURE:
357 case STREAM_PROGRAM_NAMES:
358 case STREAM_PROGRAM_DATA:
359 if (!prog_name_msg) {
360 Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
366 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
368 Dmsg1(30, "Found wierd stream %d\n", stream);
369 if (!is_bopen(&bfd)) {
370 Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
372 set_attributes(jcr, attr, &bfd);
375 Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), stream);
376 Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
378 } /* end switch(stream) */
380 } /* end while get_msg() */
382 /* If output file is still open, it was the last one in the
383 * archive since we just hit an end of file, so close the file.
385 if (is_bopen(&bfd)) {
386 set_attributes(jcr, attr, &bfd);
388 set_jcr_job_status(jcr, JS_Terminated);
392 set_jcr_job_status(jcr, JS_ErrorTerminated);
394 if (jcr->compress_buf) {
395 free(jcr->compress_buf);
396 jcr->compress_buf = NULL;
400 Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
402 if (non_support_data > 1 || non_support_attr > 1) {
403 Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
404 non_support_data, non_support_attr);
406 if (non_support_acl) {
407 Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_support_acl);
414 * Convert ZLIB error code into an ASCII message
416 static const char *zlib_strerror(int stat)
425 return "Zlib stream error";
427 return "Zlib data error";
429 return "Zlib memory error";
431 return "Zlib buffer error";
432 case Z_VERSION_ERROR:
433 return "Zlib version error";