]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/restore.c
See kes-1.31
[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-2003 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    BFILE bfd;
59    int type, stat;
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    binit(&bfd);
68    sd = jcr->store_bsock;
69    set_jcr_job_status(jcr, JS_Running);
70
71    if (!bnet_set_buffer_size(sd, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_READ)) {
72       set_jcr_job_status(jcr, JS_ErrorTerminated);
73       return;
74    }
75    jcr->buf_size = sd->msglen;
76
77    fname = get_pool_memory(PM_FNAME);
78    ofile = get_pool_memory(PM_FNAME);
79    lname = get_pool_memory(PM_FNAME);
80    attribsEx = get_pool_memory(PM_FNAME);
81
82 #ifdef HAVE_LIBZ
83    uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
84    jcr->compress_buf = (char *)bmalloc(compress_buf_size);
85 #endif
86
87    /* 
88     * Get a record from the Storage daemon. We are guaranteed to 
89     *   receive records in the following order:
90     *   1. Stream record header
91     *   2. Stream data
92     *        a. Attributes (Unix or Win32)
93     *    or  b. File data for the file
94     *    or  c. Possibly MD5 or SHA1 record
95     *   3. Repeat step 1
96     */
97    while (bget_msg(sd) >= 0 && !job_canceled(jcr)) {
98       /*
99        * First we expect a Stream Record Header 
100        */
101       if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
102           &stream, &size) != 5) {
103          Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
104          goto bail_out;
105       }
106       Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
107
108       /* 
109        * Now we expect the Stream Data
110        */
111       if (bget_msg(sd) < 0) {
112          Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
113          goto bail_out;
114       }
115       if (size != (uint32_t)sd->msglen) {
116          Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
117          goto bail_out;
118       }
119       Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
120
121       /* File Attributes stream */
122       if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_WIN32_ATTRIBUTES) {
123          char *ap, *lp, *fp, *apex;
124          uint32_t LinkFI;
125
126          Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
127          /* If extracting, it was from previous stream, so
128           * close the output file.
129           */
130          if (extract) {
131             if (!is_bopen(&bfd)) {
132                Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open\n"));
133             }
134             set_attributes(jcr, fname, ofile, lname, type, stream, 
135                            &statp, attribsEx, &bfd);
136             extract = FALSE;
137             Dmsg0(30, "Stop extracting.\n");
138          }
139
140          if ((int)sizeof_pool_memory(fname) <  sd->msglen) {
141             fname = realloc_pool_memory(fname, sd->msglen + 1);
142          }
143          if ((int)sizeof_pool_memory(ofile) < sd->msglen + wherelen + 1) {
144             ofile = realloc_pool_memory(ofile, sd->msglen + wherelen + 1);
145          }
146          if ((int)sizeof_pool_memory(lname) < sd->msglen + wherelen + 1) {
147             lname = realloc_pool_memory(lname, sd->msglen + wherelen + 1);
148          }
149          *fname = 0;
150          *lname = 0;
151
152          /*              
153           * An Attributes record consists of:
154           *    File_index
155           *    Type   (FT_types)
156           *    Filename
157           *    Attributes
158           *    Link name (if file linked i.e. FT_LNK)
159           *    Extended attributes (Win32)
160           *
161           */
162          Dmsg1(100, "Attr: %s\n", sd->msg);
163          if (sscanf(sd->msg, "%d %d", &record_file_index, &type) != 2) {
164             Jmsg(jcr, M_FATAL, 0, _("Error scanning attributes: %s\n"), sd->msg);
165             Dmsg1(100, "\nError scanning attributes. %s\n", sd->msg);
166             goto bail_out;
167          }
168          Dmsg2(100, "Got Attr: FilInx=%d type=%d\n", record_file_index, type);
169          if (record_file_index != file_index) {
170             Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
171                file_index, record_file_index);
172             Dmsg0(100, "File index error\n");
173             goto bail_out;
174          }
175          ap = sd->msg;
176          while (*ap++ != ' ')         /* skip record file index */
177             ;
178          while (*ap++ != ' ')         /* skip type */
179             ;
180          /* Save filename and position to attributes */
181          fp = fname;
182          while (*ap != 0) {
183             *fp++  = *ap++;           /* copy filename to fname */
184          }
185          *fp = *ap++;                 /* terminate filename & point to attribs */
186
187          /* Skip to Link name */
188          if (type == FT_LNK || type == FT_LNKSAVED) {
189             lp = ap;
190             while (*lp++ != 0) {
191                ;
192             }
193          } else {
194             lp = "";
195          }
196
197          if (stream == STREAM_WIN32_ATTRIBUTES) {
198             apex = ap;                   /* start at attributes */
199             while (*apex++ != 0) {       /* skip attributes */
200                ;
201             }
202             while (*apex++ != 0) {       /* skip link name */
203                ;
204             }
205             pm_strcpy(&attribsEx, apex); /* make a copy */
206          } else {
207             *attribsEx = 0;              /* no extended attributes */
208          }
209
210          Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", fname, ap, attribsEx);
211
212          decode_stat(ap, &statp, &LinkFI);
213          /*
214           * Prepend the where directory so that the
215           * files are put where the user wants.
216           *
217           * We do a little jig here to handle Win32 files with
218           *   a drive letter -- we simply strip the drive: from
219           *   every filename if a prefix is supplied.
220           *     
221           */
222          if (jcr->where[0] == 0) {
223             strcpy(ofile, fname);
224             strcpy(lname, lp);
225          } else {
226             char *fn;
227             strcpy(ofile, jcr->where);  /* copy prefix */
228             if (win32_client && fname[1] == ':') {
229                fn = fname+2;          /* skip over drive: */
230             } else {
231                fn = fname;            /* take whole name */
232             }
233             /* Ensure where is terminated with a slash */
234             if (jcr->where[wherelen-1] != '/' && fn[0] != '/') {
235                strcat(ofile, "/");
236             }   
237             strcat(ofile, fn);        /* copy rest of name */
238             /*
239              * Fixup link name -- if it is an absolute path
240              */
241             if (type == FT_LNKSAVED || type == FT_LNK) {
242                int add_link;
243                /* Always add prefix to hard links (FT_LNKSAVED) and
244                 *  on user request to soft links
245                 */
246                if (lp[0] == '/' &&
247                    (type == FT_LNKSAVED || jcr->prefix_links)) {
248                   strcpy(lname, jcr->where);
249                   add_link = 1;
250                } else {
251                   lname[0] = 0;
252                   add_link = 0;
253                }
254                if (win32_client && lp[1] == ':') {
255                   fn = lp+2;             /* skip over drive: */
256                } else {
257                   fn = lp;               /* take whole name */
258                }
259                /* Ensure where is terminated with a slash */
260                if (add_link && jcr->where[wherelen-1] != '/' && fn[0] != '/') {
261                   strcat(lname, "/");
262                }   
263                strcat(lname, fn);     /* copy rest of link */
264             }
265          }
266
267          P(jcr->mutex);
268          pm_strcpy(&jcr->last_fname, fname);
269          V(jcr->mutex);
270          jcr->num_files_examined++;
271
272          Dmsg1(30, "Outfile=%s\n", ofile);
273          extract = FALSE;
274          stat = create_file(jcr, fname, ofile, lname, type, 
275                             stream, &statp, attribsEx, &bfd, 
276                             jcr->replace);
277          switch (stat) {
278          case CF_ERROR:
279          case CF_SKIP:
280             break;
281          case CF_EXTRACT:
282             extract = TRUE;
283             P(jcr->mutex);
284             pm_strcpy(&jcr->last_fname, ofile);
285             V(jcr->mutex);
286             jcr->JobFiles++;
287             fileAddr = 0;
288             print_ls_output(jcr, ofile, lname, type, &statp);
289             /* Set attributes after file extracted */
290             break;
291          case CF_CREATED:
292             jcr->JobFiles++;
293             fileAddr = 0;
294             print_ls_output(jcr, ofile, lname, type, &statp);
295             /* set attributes now because file will not be extracted */
296             set_attributes(jcr, fname, ofile, lname, type, stream, 
297                            &statp, attribsEx, &bfd);
298             break;
299          }  
300
301
302       /* Data stream */
303       } else if (stream == STREAM_FILE_DATA || stream == STREAM_SPARSE_DATA ||
304                  stream == STREAM_WIN32_DATA) {
305          if (stream == STREAM_WIN32_DATA && !is_win32_backup()) {
306             Jmsg(jcr, M_ERROR, 0, _("Win32 backup data not supported on this Client.\n"));
307             extract = FALSE;
308             continue;
309          }
310          if (extract) {
311             if (stream == STREAM_SPARSE_DATA) {
312                ser_declare;
313                uint64_t faddr;
314                char ec1[50];
315
316                wbuf = sd->msg + SPARSE_FADDR_SIZE;
317                wsize = sd->msglen - SPARSE_FADDR_SIZE;
318                ser_begin(sd->msg, SPARSE_FADDR_SIZE);
319                unser_uint64(faddr);
320                if (fileAddr != faddr) {
321                   fileAddr = faddr;
322                   if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
323                      Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
324                          edit_uint64(fileAddr, ec1), ofile, berror(&bfd));
325                      extract = FALSE;
326                      continue;
327                   }
328                }
329             } else {
330                wbuf = sd->msg;
331                wsize = sd->msglen;
332             }
333             Dmsg2(30, "Write %u bytes, total before write=%u\n", wsize, total);
334             if ((uint32_t)bwrite(&bfd, wbuf, wsize) != wsize) {
335                Dmsg0(0, "===Write error===\n");
336                Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: ERR=%s\n"), ofile, berror(&bfd));
337                extract = FALSE;
338                continue;
339             } 
340             total += wsize;
341             jcr->JobBytes += wsize;
342             jcr->ReadBytes += wsize;
343             fileAddr += wsize;
344          }
345         
346       /* GZIP data stream */
347       } else if (stream == STREAM_GZIP_DATA || stream == STREAM_SPARSE_GZIP_DATA ||
348                  stream == STREAM_WIN32_GZIP_DATA) {
349          if (stream == STREAM_WIN32_GZIP_DATA && !is_win32_backup()) {
350             Jmsg(jcr, M_ERROR, 0, _("Win32 GZIP backup data not supported on this Client.\n"));
351             extract = FALSE;
352             continue;
353          }
354 #ifdef HAVE_LIBZ
355          if (extract) {
356             ser_declare;
357             uLong compress_len;
358             uint64_t faddr;
359             char ec1[50];
360             int stat;
361
362             if (stream == STREAM_SPARSE_GZIP_DATA) {
363                wbuf = sd->msg + SPARSE_FADDR_SIZE;
364                wsize = sd->msglen - SPARSE_FADDR_SIZE;
365                ser_begin(sd->msg, SPARSE_FADDR_SIZE);
366                unser_uint64(faddr);
367                if (fileAddr != faddr) {
368                   fileAddr = faddr;
369                   if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
370                      Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
371                          edit_uint64(fileAddr, ec1), ofile, berror(&bfd));
372                      extract = FALSE;
373                      continue;
374                   }
375                }
376             } else {
377                wbuf = sd->msg;
378                wsize = sd->msglen;
379             }
380             compress_len = compress_buf_size;
381             Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
382             if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len, 
383                   (const Byte *)wbuf, (uLong)wsize)) != Z_OK) {
384                Jmsg(jcr, M_ERROR, 0, _("Uncompression error. ERR=%d\n"), stat);
385                extract = FALSE;
386                continue;
387             }
388
389             Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
390             if ((uLong)bwrite(&bfd, jcr->compress_buf, compress_len) != compress_len) {
391                Dmsg0(0, "===Write error===\n");
392                Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), ofile, berror(&bfd));
393                extract = FALSE;
394                continue;
395             }
396             total += compress_len;
397             jcr->JobBytes += compress_len;
398             jcr->ReadBytes += wsize;
399             fileAddr += compress_len;
400          }
401 #else
402          if (extract) {
403             Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
404             extract = FALSE;
405             continue;
406          }
407 #endif
408       /* If extracting, wierd stream (not 1 or 2), close output file anyway */
409       } else if (extract) {
410          Dmsg1(30, "Found wierd stream %d\n", stream);
411          if (!is_bopen(&bfd)) {
412             Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
413          }
414          set_attributes(jcr, fname, ofile, lname, type, stream, 
415                         &statp, attribsEx, &bfd);
416          extract = FALSE;
417       } else if (!(stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE)) {
418          Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
419       }
420    }
421
422    /* If output file is still open, it was the last one in the
423     * archive since we just hit an end of file, so close the file. 
424     */
425    if (is_bopen(&bfd)) {
426       set_attributes(jcr, fname, ofile, lname, type, stream, 
427                      &statp, attribsEx, &bfd);
428    }
429    set_jcr_job_status(jcr, JS_Terminated);
430    goto ok_out;
431
432 bail_out:
433    set_jcr_job_status(jcr, JS_ErrorTerminated);
434 ok_out:
435    if (jcr->compress_buf) {
436       free(jcr->compress_buf);
437       jcr->compress_buf = NULL;
438    }
439    free_pool_memory(fname);
440    free_pool_memory(ofile);
441    free_pool_memory(lname);
442    free_pool_memory(attribsEx);
443    Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
444       jcr->JobBytes);
445 }          
446
447 extern char *getuser(uid_t uid);
448 extern char *getgroup(gid_t gid);
449
450 /*
451  * Print an ls style message, also send INFO
452  */
453 static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp)
454 {
455    char buf[5000]; 
456    char ec1[30];
457    char *p, *f;
458    int n;
459
460    p = encode_mode(statp->st_mode, buf);
461    n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
462    p += n;
463    n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
464    p += n;
465    n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
466    p += n;
467    p = encode_time(statp->st_ctime, p);
468    *p++ = ' ';
469    *p++ = ' ';
470    for (f=fname; *f && (p-buf) < (int)sizeof(buf)-10; ) {
471       *p++ = *f++;
472    }
473    if (type == FT_LNK) {
474       *p++ = ' ';
475       *p++ = '-';
476       *p++ = '>';
477       *p++ = ' ';
478       /* Copy link name */
479       for (f=lname; *f && (p-buf) < (int)sizeof(buf)-10; ) {
480          *p++ = *f++;
481       }
482    }
483    *p++ = '\n';
484    *p = 0;
485    Dmsg1(20, "%s", buf);
486    Jmsg(jcr, M_RESTORED, 0, "%s", buf);
487 }