2 * Bacula File Daemon restore.c Restorefiles.
4 * Kern Sibbald, November MM
10 Copyright (C) 2000-2003 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,
32 /* Data received from Storage Daemon */
33 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
35 /* Forward referenced functions */
36 static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp);
38 #define RETRY 10 /* retry wait time */
41 * Restore the requested files.
44 void do_restore(JCR *jcr)
48 POOLMEM *fname; /* original file name */
49 POOLMEM *ofile; /* output name with possible prefix */
50 POOLMEM *lname; /* link name with possible prefix */
51 POOLMEM *attribsEx; /* Extended attributes (Win32) */
54 uint32_t VolSessionId, VolSessionTime, file_index;
55 uint32_t record_file_index;
60 uint32_t total = 0; /* Job total but only 32 bits for debug */
61 char *wbuf; /* write buffer */
62 uint32_t wsize; /* write size */
63 uint64_t fileAddr = 0; /* file write address */
65 wherelen = strlen(jcr->where);
67 sd = jcr->store_bsock;
68 set_jcr_job_status(jcr, JS_Running);
70 if (!bnet_set_buffer_size(sd, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_READ)) {
71 set_jcr_job_status(jcr, JS_ErrorTerminated);
74 jcr->buf_size = sd->msglen;
76 fname = get_pool_memory(PM_FNAME);
77 ofile = get_pool_memory(PM_FNAME);
78 lname = get_pool_memory(PM_FNAME);
79 attribsEx = get_pool_memory(PM_FNAME);
82 uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
83 jcr->compress_buf = (char *)bmalloc(compress_buf_size);
87 * Get a record from the Storage daemon. We are guaranteed to
88 * receive records in the following order:
89 * 1. Stream record header
91 * a. Attributes (Unix or Win32)
92 * or b. File data for the file
93 * or c. Possibly MD5 or SHA1 record
96 while (bnet_recv(sd) >= 0 && !job_cancelled(jcr)) {
98 * First we expect a Stream Record Header
100 if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
101 &stream, &size) != 5) {
102 Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
105 Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
108 * Now we expect the Stream Data
110 if (bnet_recv(sd) < 0) {
111 Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
114 if (size != (uint32_t)sd->msglen) {
115 Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
118 Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
120 /* File Attributes stream */
121 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_WIN32_ATTRIBUTES) {
122 char *ap, *lp, *fp, *apex;
125 Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
126 /* If extracting, it was from previous stream, so
127 * close the output file.
131 Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open\n"));
133 set_attributes(jcr, fname, ofile, lname, type, stream,
134 &statp, attribsEx, &ofd);
136 Dmsg0(30, "Stop extracting.\n");
139 if ((int)sizeof_pool_memory(fname) < sd->msglen) {
140 fname = realloc_pool_memory(fname, sd->msglen + 1);
142 if ((int)sizeof_pool_memory(ofile) < sd->msglen + wherelen + 1) {
143 ofile = realloc_pool_memory(ofile, sd->msglen + wherelen + 1);
145 if ((int)sizeof_pool_memory(lname) < sd->msglen + wherelen + 1) {
146 lname = realloc_pool_memory(lname, sd->msglen + wherelen + 1);
152 * An Attributes record consists of:
157 * Link name (if file linked i.e. FT_LNK)
158 * Extended attributes (Win32)
161 Dmsg1(100, "Attr: %s\n", sd->msg);
162 if (sscanf(sd->msg, "%d %d", &record_file_index, &type) != 2) {
163 Jmsg(jcr, M_FATAL, 0, _("Error scanning attributes: %s\n"), sd->msg);
164 Dmsg1(100, "\nError scanning attributes. %s\n", sd->msg);
167 Dmsg2(100, "Got Attr: FilInx=%d type=%d\n", record_file_index, type);
168 if (record_file_index != file_index) {
169 Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
170 file_index, record_file_index);
171 Dmsg0(100, "File index error\n");
175 while (*ap++ != ' ') /* skip record file index */
177 while (*ap++ != ' ') /* skip type */
179 /* Save filename and position to attributes */
182 *fp++ = *ap++; /* copy filename to fname */
184 *fp = *ap++; /* terminate filename & point to attribs */
186 /* Skip to Link name */
187 if (type == FT_LNK || type == FT_LNKSAVED) {
196 if (stream == STREAM_WIN32_ATTRIBUTES) {
197 apex = ap; /* start at attributes */
198 while (*apex++ != 0) { /* skip attributes */
201 while (*apex++ != 0) { /* skip link name */
204 pm_strcpy(&attribsEx, apex); /* make a copy */
206 *attribsEx = 0; /* no extended attributes */
209 Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", fname, ap, attribsEx);
211 decode_stat(ap, &statp, &LinkFI);
213 * Prepend the where directory so that the
214 * files are put where the user wants.
216 * We do a little jig here to handle Win32 files with
217 * a drive letter -- we simply strip the drive: from
218 * every filename if a prefix is supplied.
221 if (jcr->where[0] == 0) {
222 strcpy(ofile, fname);
226 strcpy(ofile, jcr->where); /* copy prefix */
227 if (win32_client && fname[1] == ':') {
228 fn = fname+2; /* skip over drive: */
230 fn = fname; /* take whole name */
232 /* Ensure where is terminated with a slash */
233 if (jcr->where[wherelen-1] != '/' && fn[0] != '/') {
236 strcat(ofile, fn); /* copy rest of name */
237 /* Fixup link name */
238 if (type == FT_LNKSAVED || (type == FT_LNK && jcr->prefix_links)) {
239 if (lp[0] == '/') { /* if absolute path */
240 strcpy(lname, jcr->where);
242 if (win32_client && lp[1] == ':') {
243 strcat(lname, lp+2); /* copy rest of name */
245 strcat(lname, lp); /* On Unix systems we take everything */
250 Dmsg1(30, "Outfile=%s\n", ofile);
253 stat = create_file(jcr, fname, ofile, lname, type,
254 stream, &statp, attribsEx, &ofd, jcr->replace);
262 pm_strcpy(&jcr->last_fname, ofile);
264 /* Fall-through wanted */
268 print_ls_output(jcr, ofile, lname, type, &statp);
272 jcr->num_files_examined++;
275 } else if (stream == STREAM_FILE_DATA || stream == STREAM_SPARSE_DATA) {
277 if (stream == STREAM_SPARSE_DATA) {
282 wbuf = sd->msg + SPARSE_FADDR_SIZE;
283 wsize = sd->msglen - SPARSE_FADDR_SIZE;
284 ser_begin(sd->msg, SPARSE_FADDR_SIZE);
286 if (fileAddr != faddr) {
288 if (lseek(ofd, (off_t)fileAddr, SEEK_SET) < 0) {
289 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
290 edit_uint64(fileAddr, ec1), ofile, strerror(errno));
298 Dmsg2(30, "Write %u bytes, total before write=%u\n", wsize, total);
299 if ((uint32_t)write(ofd, wbuf, wsize) != wsize) {
300 Dmsg0(0, "===Write error===\n");
301 Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), ofile, strerror(errno));
305 jcr->JobBytes += wsize;
309 /* GZIP data stream */
310 } else if (stream == STREAM_GZIP_DATA || stream == STREAM_SPARSE_GZIP_DATA) {
319 if (stream == STREAM_SPARSE_GZIP_DATA) {
320 wbuf = sd->msg + SPARSE_FADDR_SIZE;
321 wsize = sd->msglen - SPARSE_FADDR_SIZE;
322 ser_begin(sd->msg, SPARSE_FADDR_SIZE);
324 if (fileAddr != faddr) {
326 if (lseek(ofd, (off_t)fileAddr, SEEK_SET) < 0) {
327 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
328 edit_uint64(fileAddr, ec1), ofile, strerror(errno));
336 compress_len = compress_buf_size;
337 Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
338 if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
339 (const Byte *)wbuf, (uLong)wsize)) != Z_OK) {
340 Jmsg(jcr, M_ERROR, 0, _("Uncompression error. ERR=%d\n"), stat);
344 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
345 if ((uLong)write(ofd, jcr->compress_buf, compress_len) != compress_len) {
346 Dmsg0(0, "===Write error===\n");
347 Jmsg2(jcr, M_ERROR, 0, "Write error on %s: %s\n", ofile, strerror(errno));
350 total += compress_len;
351 jcr->JobBytes += compress_len;
352 fileAddr += compress_len;
356 Jmsg(jcr, M_ERROR, 0, "GZIP data stream found, but GZIP not configured!\n");
360 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
361 } else if (extract) {
362 Dmsg1(30, "Found wierd stream %d\n", stream);
364 Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open\n"));
366 set_attributes(jcr, fname, ofile, lname, type, stream,
367 &statp, attribsEx, &ofd);
369 } else if (!(stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE)) {
370 Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
374 /* If output file is still open, it was the last one in the
375 * archive since we just hit an end of file, so close the file.
378 set_attributes(jcr, fname, ofile, lname, type, stream,
379 &statp, attribsEx, &ofd);
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;
391 free_pool_memory(fname);
392 free_pool_memory(ofile);
393 free_pool_memory(lname);
394 free_pool_memory(attribsEx);
395 Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
399 extern char *getuser(uid_t uid);
400 extern char *getgroup(gid_t gid);
403 * Print an ls style message, also send INFO
405 static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp)
412 p = encode_mode(statp->st_mode, buf);
413 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
415 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
417 n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
419 p = encode_time(statp->st_ctime, p);
422 for (f=fname; *f && (p-buf) < (int)sizeof(buf)-10; ) {
425 if (type == FT_LNK) {
431 for (f=lname; *f && (p-buf) < (int)sizeof(buf)-10; ) {
438 Jmsg(jcr, M_INFO, 0, "%s", buf);