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 */
53 uint32_t VolSessionId, VolSessionTime, file_index;
54 uint32_t record_file_index;
61 wherelen = strlen(jcr->where);
63 sd = jcr->store_bsock;
64 jcr->JobStatus = JS_Running;
66 if (!bnet_set_buffer_size(sd, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_READ)) {
69 jcr->buf_size = sd->msglen;
71 fname = get_pool_memory(PM_FNAME);
72 ofile = get_pool_memory(PM_FNAME);
73 lname = get_pool_memory(PM_FNAME);
76 uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
77 jcr->compress_buf = (char *)bmalloc(compress_buf_size);
81 * Get a record from the Storage daemon
83 while (bnet_recv(sd) > 0 && !job_cancelled(jcr)) {
85 * First we expect a Stream Record Header
87 if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
88 &stream, &size) != 5) {
89 Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
92 Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
95 * Now we expect the Stream Data
97 if (bnet_recv(sd) < 0 && !job_cancelled(jcr)) {
98 Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
100 if (size != ((uint32_t) sd->msglen)) {
101 Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
104 Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
106 /* File Attributes stream */
107 if (stream == STREAM_UNIX_ATTRIBUTES) {
110 Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
111 /* If extracting, it was from previous stream, so
112 * close the output file.
116 Emsg0(M_ABORT, 0, _("Logic error output file should be open\n"));
121 set_statp(jcr, fname, ofile, lname, type, &statp);
122 Dmsg0(30, "Stop extracting.\n");
125 if ((int)sizeof_pool_memory(fname) < sd->msglen) {
126 fname = realloc_pool_memory(fname, sd->msglen + 1);
128 if ((int)sizeof_pool_memory(ofile) < sd->msglen + wherelen + 1) {
129 ofile = realloc_pool_memory(ofile, sd->msglen + wherelen + 1);
131 if ((int)sizeof_pool_memory(lname) < sd->msglen + wherelen + 1) {
132 lname = realloc_pool_memory(lname, sd->msglen + wherelen + 1);
138 * An Attributes record consists of:
143 * Link name (if file linked i.e. FT_LNK)
146 Dmsg1(100, "Attr: %s\n", sd->msg);
147 if (sscanf(sd->msg, "%d %d", &record_file_index, &type) != 2) {
148 Jmsg(jcr, M_FATAL, 0, _("Error scanning attributes: %s\n"), sd->msg);
149 Dmsg1(000, "\nError scanning attributes. %s\n", sd->msg);
152 Dmsg2(100, "Got Attr: FilInx=%d type=%d\n", record_file_index, type);
153 if (record_file_index != file_index) {
154 Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
155 file_index, record_file_index);
156 Dmsg0(000, "File index error\n");
160 while (*ap++ != ' ') /* skip record file index */
162 while (*ap++ != ' ') /* skip type */
164 /* Save filename and position to attributes */
167 *fp++ = *ap++; /* copy filename to fname */
169 *fp = *ap++; /* terminate filename & point to attribs */
171 /* Skip to Link name */
172 if (type == FT_LNK || type == FT_LNKSAVED) {
181 decode_stat(ap, &statp);
183 * ***FIXME*** add REAL Win32 code to backup.c
184 * Temp kludge so that low level routines (set_statp) know
185 * we are dealing with a Win32 system.
188 statp.st_mode |= S_ISWIN32;
191 * Prepend the where directory so that the
192 * files are put where the user wants.
194 * We do a little jig here to handle Win32 files with
195 * a drive letter -- we simply strip the drive: from
196 * every filename if a prefix is supplied.
199 if (jcr->where[0] == 0) {
200 strcpy(ofile, fname);
204 strcpy(ofile, jcr->where); /* copy prefix */
205 if (win32_client && fname[1] == ':') {
206 fn = fname+2; /* skip over drive: */
208 fn = fname; /* take whole name */
210 /* Ensure where is terminated with a slash */
211 if (jcr->where[wherelen-1] != '/' && fn[0] != '/') {
214 strcat(ofile, fn); /* copy rest of name */
215 /* Fixup link name */
216 if (type == FT_LNK || type == FT_LNKSAVED) {
217 if (lp[0] == '/') { /* if absolute path */
218 strcpy(lname, jcr->where);
220 if (win32_client && lp[1] == ':') {
221 strcat(lname, lp+2); /* copy rest of name */
223 strcat(lname, lp); /* On Unix systems we take everything */
228 Dmsg1(30, "Outfile=%s\n", ofile);
229 print_ls_output(jcr, ofile, lname, type, &statp);
231 extract = create_file(jcr, fname, ofile, lname, type, &statp, &ofd);
232 Dmsg1(40, "Extract=%d\n", extract);
236 jcr->num_files_examined++;
239 } else if (stream == STREAM_FILE_DATA) {
241 Dmsg2(30, "Write %d bytes, total before write=%d\n", sd->msglen, total);
242 if (write(ofd, sd->msg, sd->msglen) != sd->msglen) {
243 Dmsg0(0, "===Write error===\n");
244 Jmsg2(jcr, M_ERROR, 0, "Write error on %s: %s\n", ofile, strerror(errno));
248 jcr->JobBytes += sd->msglen;
251 /* GZIP data stream */
252 } else if (stream == STREAM_GZIP_DATA) {
258 compress_len = compress_buf_size;
259 Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, sd->msglen);
260 if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
261 (const Byte *)sd->msg, (uLong)sd->msglen)) != Z_OK) {
262 Jmsg(jcr, M_ERROR, 0, _("Uncompression error. ERR=%d\n"), stat);
266 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
267 if ((uLong)write(ofd, jcr->compress_buf, compress_len) != compress_len) {
268 Dmsg0(0, "===Write error===\n");
269 Jmsg2(jcr, M_ERROR, 0, "Write error on %s: %s\n", ofile, strerror(errno));
272 total += compress_len;
273 jcr->JobBytes += compress_len;
277 Jmsg(jcr, M_ERROR, 0, "GZIP data stream found, but GZIP not configured!\n");
281 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
282 } else if (extract) {
283 Dmsg1(30, "Found wierd stream %d\n", stream);
285 Emsg0(M_ABORT, 0, _("Logic error output file should be open\n"));
290 set_statp(jcr, fname, ofile, lname, type, &statp);
291 } else if (stream != STREAM_MD5_SIGNATURE) {
292 Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
296 /* If output file is still open, it was the last one in the
297 * archive since we just hit an end of file, so close the file.
301 set_statp(jcr, fname, ofile, lname, type, &statp);
303 jcr->JobStatus = JS_Terminated;
307 jcr->JobStatus = JS_ErrorTerminated;
309 if (jcr->compress_buf) {
310 free(jcr->compress_buf);
311 jcr->compress_buf = NULL;
313 free_pool_memory(fname);
314 free_pool_memory(ofile);
315 free_pool_memory(lname);
316 Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
320 extern char *getuser(uid_t uid);
321 extern char *getgroup(gid_t gid);
324 * Print an ls style message, also send INFO
326 static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp)
333 p = encode_mode(statp->st_mode, buf);
334 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
336 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
338 n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
340 p = encode_time(statp->st_ctime, p);
343 for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
345 if (type == FT_LNK) {
351 for (f=lname; *f && (p-buf) < (int)sizeof(buf); )
357 Jmsg(jcr, M_INFO, 0, buf);