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) {
227 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
228 edit_uint64(fileAddr, ec1), attr->ofname, berror(&bfd));
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");
241 Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: ERR=%s\n"), attr->ofname, berror(&bfd));
247 jcr->JobBytes += wsize;
248 jcr->ReadBytes += wsize;
253 /* GZIP data stream */
254 case STREAM_GZIP_DATA:
255 case STREAM_SPARSE_GZIP_DATA:
256 case STREAM_WIN32_GZIP_DATA:
262 if (stream == STREAM_SPARSE_GZIP_DATA) {
266 wbuf = sd->msg + SPARSE_FADDR_SIZE;
267 wsize = sd->msglen - SPARSE_FADDR_SIZE;
268 ser_begin(sd->msg, SPARSE_FADDR_SIZE);
270 if (fileAddr != faddr) {
272 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
273 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
274 edit_uint64(fileAddr, ec1), attr->ofname, berror(&bfd));
284 compress_len = compress_buf_size;
285 Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
286 if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
287 (const Byte *)wbuf, (uLong)wsize)) != Z_OK) {
288 Jmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"),
289 attr->ofname, zlib_strerror(stat));
295 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
296 if ((uLong)bwrite(&bfd, jcr->compress_buf, compress_len) != compress_len) {
297 Dmsg0(0, "===Write error===\n");
298 Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), attr->ofname, berror(&bfd));
303 total += compress_len;
304 jcr->JobBytes += compress_len;
305 jcr->ReadBytes += wsize;
306 fileAddr += compress_len;
310 Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
318 case STREAM_UNIX_ATTRIBUTES_ACL:
320 /* Recover ACL from stream and check it */
321 acl = acl_from_text(sd->msg);
322 if (acl_valid(acl) != 0) {
323 Emsg1(M_WARNING, 0, "Failure in the ACL of %s! FD is not able to restore it!\n", jcr->last_fname);
327 /* Try to restore ACL */
328 if (attr->type == FT_DIREND) {
330 if (acl_set_file(jcr->last_fname, ACL_TYPE_DEFAULT, acl) != 0 &&
331 acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
332 Emsg1(M_WARNING, 0, "Error! Can't restore ACL of directory: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
335 } else if (acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
336 Emsg1(M_WARNING, 0, "Error! Can't restore ACL of file: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
339 Dmsg1(200, "ACL of file: %s successfully restored!", jcr->last_fname);
343 break; /* unconfigured, ignore */
346 case STREAM_MD5_SIGNATURE:
347 case STREAM_SHA1_SIGNATURE:
350 case STREAM_PROGRAM_NAMES:
351 case STREAM_PROGRAM_DATA:
352 if (!prog_name_msg) {
353 Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
359 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
361 Dmsg1(30, "Found wierd stream %d\n", stream);
362 if (!is_bopen(&bfd)) {
363 Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
365 set_attributes(jcr, attr, &bfd);
368 Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), stream);
369 Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
371 } /* end switch(stream) */
373 } /* end while get_msg() */
375 /* If output file is still open, it was the last one in the
376 * archive since we just hit an end of file, so close the file.
378 if (is_bopen(&bfd)) {
379 set_attributes(jcr, attr, &bfd);
381 set_jcr_job_status(jcr, JS_Terminated);
385 set_jcr_job_status(jcr, JS_ErrorTerminated);
387 if (jcr->compress_buf) {
388 free(jcr->compress_buf);
389 jcr->compress_buf = NULL;
393 Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
395 if (non_support_data > 1 || non_support_attr > 1) {
396 Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
397 non_support_data, non_support_attr);
399 if (non_support_acl) {
400 Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_support_acl);
407 * Convert ZLIB error code into an ASCII message
409 static const char *zlib_strerror(int stat)
418 return "Zlib stream error";
420 return "Zlib data error";
422 return "Zlib memory error";
424 return "Zlib buffer error";
425 case Z_VERSION_ERROR:
426 return "Zlib version error";