]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/restore.c
Prune SQL fix; finish Linux rescue; doc
[bacula/bacula] / bacula / src / filed / restore.c
1 /*
2  *  Bacula File Daemon  restore.c Restorefiles.
3  *
4  *    Kern Sibbald, November MM
5  *
6  *   Version $Id$
7  *
8  */
9 /*
10    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11
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.
16
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.
21
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,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "filed.h"
31
32 /* Data received from Storage Daemon */
33 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
34
35 /* Forward referenced functions */
36 static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp);
37
38 #define RETRY 10                      /* retry wait time */
39
40 /* 
41  * Restore the requested files.
42  * 
43  */
44 void do_restore(JCR *jcr)
45 {
46    int wherelen;
47    BSOCK *sd;
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) */
52    int32_t stream;
53    uint32_t size;
54    uint32_t VolSessionId, VolSessionTime, file_index;
55    uint32_t record_file_index;
56    struct stat statp;
57    int extract = FALSE;
58    int ofd = -1;
59    int type;
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 */
64    
65    wherelen = strlen(jcr->where);
66
67    sd = jcr->store_bsock;
68    jcr->JobStatus = JS_Running;
69
70    if (!bnet_set_buffer_size(sd, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_READ)) {
71       return;
72    }
73    jcr->buf_size = sd->msglen;
74
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);
79
80 #ifdef HAVE_LIBZ
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);
83 #endif
84
85    /* 
86     * Get a record from the Storage daemon. We are guaranteed to 
87     *   receive records in the following order:
88     *   1. Stream record header
89     *   2. Stream data
90     *        a. Attributes (Unix or Win32)
91     *    or  b. File data for the file
92     *    or  c. Possibly MD5 record
93     *   3. Repeat step 1
94     */
95    while (bnet_recv(sd) >= 0 && !job_cancelled(jcr)) {
96       /*
97        * First we expect a Stream Record Header 
98        */
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);
102          goto bail_out;
103       }
104       Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
105
106       /* 
107        * Now we expect the Stream Data
108        */
109       if (bnet_recv(sd) < 0 && !job_cancelled(jcr)) {
110          Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
111       }
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);
114          goto bail_out;
115       }
116       Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
117
118       /* File Attributes stream */
119       if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_WIN32_ATTRIBUTES) {
120          char *ap, *lp, *fp, *apex;
121
122          Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
123          /* If extracting, it was from previous stream, so
124           * close the output file.
125           */
126          if (extract) {
127             if (ofd < 0) {
128                Emsg0(M_ERROR, 0, _("Logic error output file should be open\n"));
129             }
130             set_attributes(jcr, fname, ofile, lname, type, stream, 
131                            &statp, attribsEx, &ofd);
132             extract = FALSE;
133             Dmsg0(30, "Stop extracting.\n");
134          }
135
136          if ((int)sizeof_pool_memory(fname) <  sd->msglen) {
137             fname = realloc_pool_memory(fname, sd->msglen + 1);
138          }
139          if ((int)sizeof_pool_memory(ofile) < sd->msglen + wherelen + 1) {
140             ofile = realloc_pool_memory(ofile, sd->msglen + wherelen + 1);
141          }
142          if ((int)sizeof_pool_memory(lname) < sd->msglen + wherelen + 1) {
143             lname = realloc_pool_memory(lname, sd->msglen + wherelen + 1);
144          }
145          *fname = 0;
146          *lname = 0;
147
148          /*              
149           * An Attributes record consists of:
150           *    File_index
151           *    Type   (FT_types)
152           *    Filename
153           *    Attributes
154           *    Link name (if file linked i.e. FT_LNK)
155           *    Extended attributes (Win32)
156           *
157           */
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);
162             goto bail_out;
163          }
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");
169             goto bail_out;
170          }
171          ap = sd->msg;
172          while (*ap++ != ' ')         /* skip record file index */
173             ;
174          while (*ap++ != ' ')         /* skip type */
175             ;
176          /* Save filename and position to attributes */
177          fp = fname;
178          while (*ap != 0) {
179             *fp++  = *ap++;           /* copy filename to fname */
180          }
181          *fp = *ap++;                 /* terminate filename & point to attribs */
182
183          /* Skip to Link name */
184          if (type == FT_LNK || type == FT_LNKSAVED) {
185             lp = ap;
186             while (*lp++ != 0) {
187                ;
188             }
189          } else {
190             lp = "";
191          }
192
193          if (stream == STREAM_WIN32_ATTRIBUTES) {
194             apex = ap;                   /* start at attributes */
195             while (*apex++ != 0) {       /* skip attributes */
196                ;
197             }
198             while (*apex++ != 0) {       /* skip link name */
199                ;
200             }
201             pm_strcpy(&attribsEx, apex); /* make a copy */
202          } else {
203             *attribsEx = 0;              /* no extended attributes */
204          }
205
206          Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", fname, ap, attribsEx);
207
208          decode_stat(ap, &statp);
209          /*
210           * Prepend the where directory so that the
211           * files are put where the user wants.
212           *
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.
216           *     
217           */
218          if (jcr->where[0] == 0) {
219             strcpy(ofile, fname);
220             strcpy(lname, lp);
221          } else {
222             char *fn;
223             strcpy(ofile, jcr->where);  /* copy prefix */
224             if (win32_client && fname[1] == ':') {
225                fn = fname+2;          /* skip over drive: */
226             } else {
227                fn = fname;            /* take whole name */
228             }
229             /* Ensure where is terminated with a slash */
230             if (jcr->where[wherelen-1] != '/' && fn[0] != '/') {
231                strcat(ofile, "/");
232             }
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);
238                }       
239                if (win32_client && lp[1] == ':') {
240                   strcat(lname, lp+2); /* copy rest of name */
241                } else {
242                   strcat(lname, lp);   /* On Unix systems we take everything */
243                }
244             }
245          }
246
247          Dmsg1(30, "Outfile=%s\n", ofile);
248          print_ls_output(jcr, ofile, lname, type, &statp);
249
250          extract = create_file(jcr, fname, ofile, lname, type, 
251                                stream, &statp, attribsEx, &ofd, jcr->replace);
252          Dmsg1(40, "Extract=%d\n", extract);
253          if (extract) {
254             jcr->JobFiles++;
255             fileAddr = 0;
256          }
257          jcr->num_files_examined++;
258          pm_strcpy(&jcr->last_fname, ofile);
259
260       /* Data stream */
261       } else if (stream == STREAM_FILE_DATA || stream == STREAM_SPARSE_DATA) {
262          if (extract) {
263             if (stream == STREAM_SPARSE_DATA) {
264                ser_declare;
265                uint64_t faddr;
266                char ec1[50];
267
268                wbuf = sd->msg + SPARSE_FADDR_SIZE;
269                wsize = sd->msglen - SPARSE_FADDR_SIZE;
270                ser_begin(sd->msg, SPARSE_FADDR_SIZE);
271                unser_uint64(faddr);
272                if (fileAddr != faddr) {
273                   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));
277                      goto bail_out;
278                   }
279                }
280             } else {
281                wbuf = sd->msg;
282                wsize = sd->msglen;
283             }
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));
288                goto bail_out;
289             }
290             total += wsize;
291             jcr->JobBytes += wsize;
292             fileAddr += wsize;
293          }
294         
295       /* GZIP data stream */
296       } else if (stream == STREAM_GZIP_DATA || stream == STREAM_SPARSE_GZIP_DATA) {
297 #ifdef HAVE_LIBZ
298          if (extract) {
299             ser_declare;
300             uLong compress_len;
301             uint64_t faddr;
302             char ec1[50];
303             int stat;
304
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);
309                unser_uint64(faddr);
310                if (fileAddr != faddr) {
311                   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));
315                      goto bail_out;
316                   }
317                }
318             } else {
319                wbuf = sd->msg;
320                wsize = sd->msglen;
321             }
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);
327                goto bail_out;
328             }
329
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));
334                goto bail_out;
335             }
336             total += compress_len;
337             jcr->JobBytes += compress_len;
338             fileAddr += compress_len;
339          }
340 #else
341          if (extract) {
342             Jmsg(jcr, M_ERROR, 0, "GZIP data stream found, but GZIP not configured!\n");
343             goto bail_out;
344          }
345 #endif
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);
349          if (ofd < 0) {
350             Emsg0(M_ERROR, 0, _("Logic error output file should be open\n"));
351          }
352          set_attributes(jcr, fname, ofile, lname, type, stream, 
353                         &statp, attribsEx, &ofd);
354          extract = FALSE;
355       } else if (stream != STREAM_MD5_SIGNATURE) {
356          Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
357       }
358    }
359
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. 
362     */
363    if (ofd >= 0) {
364       set_attributes(jcr, fname, ofile, lname, type, stream, 
365                      &statp, attribsEx, &ofd);
366    }
367    jcr->JobStatus = JS_Terminated;
368    goto ok_out;
369
370 bail_out:
371    jcr->JobStatus = JS_ErrorTerminated;
372 ok_out:
373    if (jcr->compress_buf) {
374       free(jcr->compress_buf);
375       jcr->compress_buf = NULL;
376    }
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,
382       jcr->JobBytes);
383 }          
384
385 extern char *getuser(uid_t uid);
386 extern char *getgroup(gid_t gid);
387
388 /*
389  * Print an ls style message, also send INFO
390  */
391 static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp)
392 {
393    char buf[2000]; 
394    char ec1[30];
395    char *p, *f;
396    int n;
397
398    p = encode_mode(statp->st_mode, buf);
399    n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
400    p += n;
401    n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
402    p += n;
403    n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
404    p += n;
405    p = encode_time(statp->st_ctime, p);
406    *p++ = ' ';
407    *p++ = ' ';
408    for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
409       *p++ = *f++;
410    if (type == FT_LNK) {
411       *p++ = ' ';
412       *p++ = '-';
413       *p++ = '>';
414       *p++ = ' ';
415       /* Copy link name */
416       for (f=lname; *f && (p-buf) < (int)sizeof(buf); )
417          *p++ = *f++;
418    }
419    *p++ = '\n';
420    *p = 0;
421    Dmsg0(20, buf);
422    Jmsg(jcr, M_INFO, 0, buf);
423 }