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:
139 Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
140 /* If extracting, it was from previous stream, so
141 * close the output file.
144 if (!is_bopen(&bfd)) {
145 Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open\n"));
147 set_attributes(jcr, attr, &bfd);
149 Dmsg0(30, "Stop extracting.\n");
152 if (!unpack_attributes_record(jcr, stream, sd->msg, attr)) {
155 if (file_index != attr->file_index) {
156 Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
157 file_index, attr->file_index);
158 Dmsg0(100, "File index error\n");
162 Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", attr->fname,
163 attr->attr, attr->attrEx);
165 attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
167 if (!is_stream_supported(attr->data_stream)) {
168 if (!non_support_data++) {
169 Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
170 stream_to_ascii(attr->data_stream));
175 build_attr_output_fnames(jcr, attr);
177 jcr->num_files_examined++;
179 Dmsg1(30, "Outfile=%s\n", attr->ofname);
181 stat = create_file(jcr, attr, &bfd, jcr->replace);
189 pm_strcpy(&jcr->last_fname, attr->ofname);
193 print_ls_output(jcr, attr);
194 /* Set attributes after file extracted */
198 pm_strcpy(&jcr->last_fname, attr->ofname);
202 print_ls_output(jcr, attr);
203 /* set attributes now because file will not be extracted */
204 set_attributes(jcr, attr, &bfd);
210 case STREAM_FILE_DATA:
211 case STREAM_SPARSE_DATA:
212 case STREAM_WIN32_DATA:
215 if (stream == STREAM_SPARSE_DATA) {
220 wbuf = sd->msg + SPARSE_FADDR_SIZE;
221 wsize = sd->msglen - SPARSE_FADDR_SIZE;
222 ser_begin(sd->msg, SPARSE_FADDR_SIZE);
224 if (fileAddr != faddr) {
226 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
228 be.set_errno(bfd.berrno);
229 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
230 edit_uint64(fileAddr, ec1), attr->ofname, be.strerror());
240 Dmsg2(30, "Write %u bytes, total before write=%u\n", wsize, total);
241 if ((uint32_t)bwrite(&bfd, wbuf, wsize) != wsize) {
242 Dmsg0(0, "===Write error===\n");
244 be.set_errno(bfd.berrno);
245 Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: ERR=%s\n"), attr->ofname,
252 jcr->JobBytes += wsize;
253 jcr->ReadBytes += wsize;
258 /* GZIP data stream */
259 case STREAM_GZIP_DATA:
260 case STREAM_SPARSE_GZIP_DATA:
261 case STREAM_WIN32_GZIP_DATA:
267 if (stream == STREAM_SPARSE_GZIP_DATA) {
271 wbuf = sd->msg + SPARSE_FADDR_SIZE;
272 wsize = sd->msglen - SPARSE_FADDR_SIZE;
273 ser_begin(sd->msg, SPARSE_FADDR_SIZE);
275 if (fileAddr != faddr) {
277 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
279 be.set_errno(bfd.berrno);
280 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
281 edit_uint64(fileAddr, ec1), attr->ofname, be.strerror());
291 compress_len = compress_buf_size;
292 Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
293 if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
294 (const Byte *)wbuf, (uLong)wsize)) != Z_OK) {
295 Jmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"),
296 attr->ofname, zlib_strerror(stat));
302 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
303 if ((uLong)bwrite(&bfd, jcr->compress_buf, compress_len) != compress_len) {
304 Dmsg0(0, "===Write error===\n");
306 be.set_errno(bfd.berrno);
307 Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), attr->ofname, be.strerror());
312 total += compress_len;
313 jcr->JobBytes += compress_len;
314 jcr->ReadBytes += wsize;
315 fileAddr += compress_len;
319 Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
327 case STREAM_UNIX_ATTRIBUTES_ACL:
329 /* Recover ACL from stream and check it */
330 acl = acl_from_text(sd->msg);
331 if (acl_valid(acl) != 0) {
332 Jmsg1(jcr, M_WARNING, 0, "Failure in the ACL of %s! FD is not able to restore it!\n", jcr->last_fname);
336 /* Try to restore ACL */
337 if (attr->type == FT_DIREND) {
339 if (acl_set_file(jcr->last_fname, ACL_TYPE_DEFAULT, acl) != 0 &&
340 acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
341 Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of directory: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
344 } else if (acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
345 Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of file: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
348 Dmsg1(200, "ACL of file: %s successfully restored!", jcr->last_fname);
352 break; /* unconfigured, ignore */
355 case STREAM_MD5_SIGNATURE:
356 case STREAM_SHA1_SIGNATURE:
359 case STREAM_PROGRAM_NAMES:
360 case STREAM_PROGRAM_DATA:
361 if (!prog_name_msg) {
362 Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
368 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
370 Dmsg1(30, "Found wierd stream %d\n", stream);
371 if (!is_bopen(&bfd)) {
372 Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
374 set_attributes(jcr, attr, &bfd);
377 Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), stream);
378 Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
380 } /* end switch(stream) */
382 } /* end while get_msg() */
384 /* If output file is still open, it was the last one in the
385 * archive since we just hit an end of file, so close the file.
387 if (is_bopen(&bfd)) {
388 set_attributes(jcr, attr, &bfd);
390 set_jcr_job_status(jcr, JS_Terminated);
394 set_jcr_job_status(jcr, JS_ErrorTerminated);
396 if (jcr->compress_buf) {
397 free(jcr->compress_buf);
398 jcr->compress_buf = NULL;
402 Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
404 if (non_support_data > 1 || non_support_attr > 1) {
405 Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
406 non_support_data, non_support_attr);
408 if (non_support_acl) {
409 Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_support_acl);
416 * Convert ZLIB error code into an ASCII message
418 static const char *zlib_strerror(int stat)
427 return "Zlib stream error";
429 return "Zlib data error";
431 return "Zlib memory error";
433 return "Zlib buffer error";
434 case Z_VERSION_ERROR:
435 return "Zlib version error";