2 * Bacula File Daemon restore.c Restorefiles.
4 * Kern Sibbald, November MM
10 Copyright (C) 2000, 2001, 2002 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 jcr->JobStatus = JS_Running;
70 if (!bnet_set_buffer_size(sd, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_READ)) {
73 jcr->buf_size = sd->msglen;
75 fname = get_pool_memory(PM_FNAME);
76 ofile = get_pool_memory(PM_FNAME);
77 lname = get_pool_memory(PM_FNAME);
78 attribsEx = get_pool_memory(PM_FNAME);
81 uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
82 jcr->compress_buf = (char *)bmalloc(compress_buf_size);
86 * Get a record from the Storage daemon. We are guaranteed to
87 * receive records in the following order:
88 * 1. Stream record header
90 * a. Attributes (Unix or Win32)
91 * or b. File data for the file
92 * or c. Possibly MD5 record
95 while (bnet_recv(sd) >= 0 && !job_cancelled(jcr)) {
97 * First we expect a Stream Record Header
99 if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
100 &stream, &size) != 5) {
101 Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
104 Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
107 * Now we expect the Stream Data
109 if (bnet_recv(sd) < 0 && !job_cancelled(jcr)) {
110 Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
112 if (size != (uint32_t)sd->msglen) {
113 Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
116 Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
118 /* File Attributes stream */
119 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_WIN32_ATTRIBUTES) {
120 char *ap, *lp, *fp, *apex;
122 Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
123 /* If extracting, it was from previous stream, so
124 * close the output file.
128 Emsg0(M_ERROR, 0, _("Logic error output file should be open\n"));
130 set_attributes(jcr, fname, ofile, lname, type, stream,
131 &statp, attribsEx, &ofd);
133 Dmsg0(30, "Stop extracting.\n");
136 if ((int)sizeof_pool_memory(fname) < sd->msglen) {
137 fname = realloc_pool_memory(fname, sd->msglen + 1);
139 if ((int)sizeof_pool_memory(ofile) < sd->msglen + wherelen + 1) {
140 ofile = realloc_pool_memory(ofile, sd->msglen + wherelen + 1);
142 if ((int)sizeof_pool_memory(lname) < sd->msglen + wherelen + 1) {
143 lname = realloc_pool_memory(lname, sd->msglen + wherelen + 1);
149 * An Attributes record consists of:
154 * Link name (if file linked i.e. FT_LNK)
155 * Extended attributes (Win32)
158 Dmsg1(100, "Attr: %s\n", sd->msg);
159 if (sscanf(sd->msg, "%d %d", &record_file_index, &type) != 2) {
160 Jmsg(jcr, M_FATAL, 0, _("Error scanning attributes: %s\n"), sd->msg);
161 Dmsg1(100, "\nError scanning attributes. %s\n", sd->msg);
164 Dmsg2(100, "Got Attr: FilInx=%d type=%d\n", record_file_index, type);
165 if (record_file_index != file_index) {
166 Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
167 file_index, record_file_index);
168 Dmsg0(100, "File index error\n");
172 while (*ap++ != ' ') /* skip record file index */
174 while (*ap++ != ' ') /* skip type */
176 /* Save filename and position to attributes */
179 *fp++ = *ap++; /* copy filename to fname */
181 *fp = *ap++; /* terminate filename & point to attribs */
183 /* Skip to Link name */
184 if (type == FT_LNK || type == FT_LNKSAVED) {
193 if (stream == STREAM_WIN32_ATTRIBUTES) {
194 apex = ap; /* start at attributes */
195 while (*apex++ != 0) { /* skip attributes */
198 while (*apex++ != 0) { /* skip link name */
201 pm_strcpy(&attribsEx, apex); /* make a copy */
203 *attribsEx = 0; /* no extended attributes */
206 Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", fname, ap, attribsEx);
208 decode_stat(ap, &statp);
210 * Prepend the where directory so that the
211 * files are put where the user wants.
213 * We do a little jig here to handle Win32 files with
214 * a drive letter -- we simply strip the drive: from
215 * every filename if a prefix is supplied.
218 if (jcr->where[0] == 0) {
219 strcpy(ofile, fname);
223 strcpy(ofile, jcr->where); /* copy prefix */
224 if (win32_client && fname[1] == ':') {
225 fn = fname+2; /* skip over drive: */
227 fn = fname; /* take whole name */
229 /* Ensure where is terminated with a slash */
230 if (jcr->where[wherelen-1] != '/' && fn[0] != '/') {
233 strcat(ofile, fn); /* copy rest of name */
234 /* Fixup link name */
235 if (type == FT_LNK || type == FT_LNKSAVED) {
236 if (lp[0] == '/') { /* if absolute path */
237 strcpy(lname, jcr->where);
239 if (win32_client && lp[1] == ':') {
240 strcat(lname, lp+2); /* copy rest of name */
242 strcat(lname, lp); /* On Unix systems we take everything */
247 Dmsg1(30, "Outfile=%s\n", ofile);
248 print_ls_output(jcr, ofile, lname, type, &statp);
250 extract = create_file(jcr, fname, ofile, lname, type,
251 stream, &statp, attribsEx, &ofd, jcr->replace);
252 Dmsg1(40, "Extract=%d\n", extract);
257 jcr->num_files_examined++;
258 pm_strcpy(&jcr->last_fname, ofile);
261 } else if (stream == STREAM_FILE_DATA || stream == STREAM_SPARSE_DATA) {
263 if (stream == STREAM_SPARSE_DATA) {
268 wbuf = sd->msg + SPARSE_FADDR_SIZE;
269 wsize = sd->msglen - SPARSE_FADDR_SIZE;
270 ser_begin(sd->msg, SPARSE_FADDR_SIZE);
272 if (fileAddr != faddr) {
274 if (lseek(ofd, (off_t)fileAddr, SEEK_SET) < 0) {
275 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
276 edit_uint64(fileAddr, ec1), ofile, strerror(errno));
284 Dmsg2(30, "Write %u bytes, total before write=%u\n", wsize, total);
285 if ((uint32_t)write(ofd, wbuf, wsize) != wsize) {
286 Dmsg0(0, "===Write error===\n");
287 Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), ofile, strerror(errno));
291 jcr->JobBytes += wsize;
295 /* GZIP data stream */
296 } else if (stream == STREAM_GZIP_DATA || stream == STREAM_SPARSE_GZIP_DATA) {
305 if (stream == STREAM_SPARSE_GZIP_DATA) {
306 wbuf = sd->msg + SPARSE_FADDR_SIZE;
307 wsize = sd->msglen - SPARSE_FADDR_SIZE;
308 ser_begin(sd->msg, SPARSE_FADDR_SIZE);
310 if (fileAddr != faddr) {
312 if (lseek(ofd, (off_t)fileAddr, SEEK_SET) < 0) {
313 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
314 edit_uint64(fileAddr, ec1), ofile, strerror(errno));
322 compress_len = compress_buf_size;
323 Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
324 if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
325 (const Byte *)wbuf, (uLong)wsize)) != Z_OK) {
326 Jmsg(jcr, M_ERROR, 0, _("Uncompression error. ERR=%d\n"), stat);
330 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
331 if ((uLong)write(ofd, jcr->compress_buf, compress_len) != compress_len) {
332 Dmsg0(0, "===Write error===\n");
333 Jmsg2(jcr, M_ERROR, 0, "Write error on %s: %s\n", ofile, strerror(errno));
336 total += compress_len;
337 jcr->JobBytes += compress_len;
338 fileAddr += compress_len;
342 Jmsg(jcr, M_ERROR, 0, "GZIP data stream found, but GZIP not configured!\n");
346 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
347 } else if (extract) {
348 Dmsg1(30, "Found wierd stream %d\n", stream);
350 Emsg0(M_ERROR, 0, _("Logic error output file should be open\n"));
352 set_attributes(jcr, fname, ofile, lname, type, stream,
353 &statp, attribsEx, &ofd);
355 } else if (stream != STREAM_MD5_SIGNATURE) {
356 Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
360 /* If output file is still open, it was the last one in the
361 * archive since we just hit an end of file, so close the file.
364 set_attributes(jcr, fname, ofile, lname, type, stream,
365 &statp, attribsEx, &ofd);
367 jcr->JobStatus = JS_Terminated;
371 jcr->JobStatus = JS_ErrorTerminated;
373 if (jcr->compress_buf) {
374 free(jcr->compress_buf);
375 jcr->compress_buf = NULL;
377 free_pool_memory(fname);
378 free_pool_memory(ofile);
379 free_pool_memory(lname);
380 free_pool_memory(attribsEx);
381 Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
385 extern char *getuser(uid_t uid);
386 extern char *getgroup(gid_t gid);
389 * Print an ls style message, also send INFO
391 static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp)
398 p = encode_mode(statp->st_mode, buf);
399 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
401 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
403 n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
405 p = encode_time(statp->st_ctime, p);
408 for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
410 if (type == FT_LNK) {
416 for (f=lname; *f && (p-buf) < (int)sizeof(buf); )
422 Jmsg(jcr, M_INFO, 0, buf);