]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/restore.c
b4d2ec631aa162444dce6dd5fe80411cac2fb24a
[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    int ofd = -1;
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    sd = jcr->store_bsock;
68    set_jcr_job_status(jcr, 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) {
110          Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
111          goto bail_out;
112       }
113       if (size != (uint32_t)sd->msglen) {
114          Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
115          goto bail_out;
116       }
117       Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
118
119       /* File Attributes stream */
120       if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_WIN32_ATTRIBUTES) {
121          char *ap, *lp, *fp, *apex;
122
123          Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
124          /* If extracting, it was from previous stream, so
125           * close the output file.
126           */
127          if (extract) {
128             if (ofd < 0) {
129                Emsg0(M_ERROR, 0, _("Logic error output file should be open\n"));
130             }
131             set_attributes(jcr, fname, ofile, lname, type, stream, 
132                            &statp, attribsEx, &ofd);
133             extract = FALSE;
134             Dmsg0(30, "Stop extracting.\n");
135          }
136
137          if ((int)sizeof_pool_memory(fname) <  sd->msglen) {
138             fname = realloc_pool_memory(fname, sd->msglen + 1);
139          }
140          if ((int)sizeof_pool_memory(ofile) < sd->msglen + wherelen + 1) {
141             ofile = realloc_pool_memory(ofile, sd->msglen + wherelen + 1);
142          }
143          if ((int)sizeof_pool_memory(lname) < sd->msglen + wherelen + 1) {
144             lname = realloc_pool_memory(lname, sd->msglen + wherelen + 1);
145          }
146          *fname = 0;
147          *lname = 0;
148
149          /*              
150           * An Attributes record consists of:
151           *    File_index
152           *    Type   (FT_types)
153           *    Filename
154           *    Attributes
155           *    Link name (if file linked i.e. FT_LNK)
156           *    Extended attributes (Win32)
157           *
158           */
159          Dmsg1(100, "Attr: %s\n", sd->msg);
160          if (sscanf(sd->msg, "%d %d", &record_file_index, &type) != 2) {
161             Jmsg(jcr, M_FATAL, 0, _("Error scanning attributes: %s\n"), sd->msg);
162             Dmsg1(100, "\nError scanning attributes. %s\n", sd->msg);
163             goto bail_out;
164          }
165          Dmsg2(100, "Got Attr: FilInx=%d type=%d\n", record_file_index, type);
166          if (record_file_index != file_index) {
167             Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
168                file_index, record_file_index);
169             Dmsg0(100, "File index error\n");
170             goto bail_out;
171          }
172          ap = sd->msg;
173          while (*ap++ != ' ')         /* skip record file index */
174             ;
175          while (*ap++ != ' ')         /* skip type */
176             ;
177          /* Save filename and position to attributes */
178          fp = fname;
179          while (*ap != 0) {
180             *fp++  = *ap++;           /* copy filename to fname */
181          }
182          *fp = *ap++;                 /* terminate filename & point to attribs */
183
184          /* Skip to Link name */
185          if (type == FT_LNK || type == FT_LNKSAVED) {
186             lp = ap;
187             while (*lp++ != 0) {
188                ;
189             }
190          } else {
191             lp = "";
192          }
193
194          if (stream == STREAM_WIN32_ATTRIBUTES) {
195             apex = ap;                   /* start at attributes */
196             while (*apex++ != 0) {       /* skip attributes */
197                ;
198             }
199             while (*apex++ != 0) {       /* skip link name */
200                ;
201             }
202             pm_strcpy(&attribsEx, apex); /* make a copy */
203          } else {
204             *attribsEx = 0;              /* no extended attributes */
205          }
206
207          Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", fname, ap, attribsEx);
208
209          decode_stat(ap, &statp);
210          /*
211           * Prepend the where directory so that the
212           * files are put where the user wants.
213           *
214           * We do a little jig here to handle Win32 files with
215           *   a drive letter -- we simply strip the drive: from
216           *   every filename if a prefix is supplied.
217           *     
218           */
219          if (jcr->where[0] == 0) {
220             strcpy(ofile, fname);
221             strcpy(lname, lp);
222          } else {
223             char *fn;
224             strcpy(ofile, jcr->where);  /* copy prefix */
225             if (win32_client && fname[1] == ':') {
226                fn = fname+2;          /* skip over drive: */
227             } else {
228                fn = fname;            /* take whole name */
229             }
230             /* Ensure where is terminated with a slash */
231             if (jcr->where[wherelen-1] != '/' && fn[0] != '/') {
232                strcat(ofile, "/");
233             }
234             strcat(ofile, fn);        /* copy rest of name */
235             /* Fixup link name */
236             if (type == FT_LNK || type == FT_LNKSAVED) {
237                if (lp[0] == '/') {      /* if absolute path */
238                   strcpy(lname, jcr->where);
239                }       
240                if (win32_client && lp[1] == ':') {
241                   strcat(lname, lp+2); /* copy rest of name */
242                } else {
243                   strcat(lname, lp);   /* On Unix systems we take everything */
244                }
245             }
246          }
247
248          Dmsg1(30, "Outfile=%s\n", ofile);
249
250          extract = FALSE;
251          stat = create_file(jcr, fname, ofile, lname, type, 
252                             stream, &statp, attribsEx, &ofd, jcr->replace);
253          switch (stat) {
254          case CF_ERROR:
255          case CF_SKIP:
256             break;
257          case CF_EXTRACT:
258             extract = TRUE;
259             P(jcr->mutex);
260             pm_strcpy(&jcr->last_fname, ofile);
261             V(jcr->mutex);
262             /* Fall-through wanted */
263          case CF_CREATED:
264             jcr->JobFiles++;
265             fileAddr = 0;
266             print_ls_output(jcr, ofile, lname, type, &statp);
267             break;
268          }  
269
270          jcr->num_files_examined++;
271
272       /* Data stream */
273       } else if (stream == STREAM_FILE_DATA || stream == STREAM_SPARSE_DATA) {
274          if (extract) {
275             if (stream == STREAM_SPARSE_DATA) {
276                ser_declare;
277                uint64_t faddr;
278                char ec1[50];
279
280                wbuf = sd->msg + SPARSE_FADDR_SIZE;
281                wsize = sd->msglen - SPARSE_FADDR_SIZE;
282                ser_begin(sd->msg, SPARSE_FADDR_SIZE);
283                unser_uint64(faddr);
284                if (fileAddr != faddr) {
285                   fileAddr = faddr;
286                   if (lseek(ofd, (off_t)fileAddr, SEEK_SET) < 0) {
287                      Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
288                          edit_uint64(fileAddr, ec1), ofile, strerror(errno));
289                      goto bail_out;
290                   }
291                }
292             } else {
293                wbuf = sd->msg;
294                wsize = sd->msglen;
295             }
296             Dmsg2(30, "Write %u bytes, total before write=%u\n", wsize, total);
297             if ((uint32_t)write(ofd, wbuf, wsize) != wsize) {
298                Dmsg0(0, "===Write error===\n");
299                Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), ofile, strerror(errno));
300                goto bail_out;
301             }
302             total += wsize;
303             jcr->JobBytes += wsize;
304             fileAddr += wsize;
305          }
306         
307       /* GZIP data stream */
308       } else if (stream == STREAM_GZIP_DATA || stream == STREAM_SPARSE_GZIP_DATA) {
309 #ifdef HAVE_LIBZ
310          if (extract) {
311             ser_declare;
312             uLong compress_len;
313             uint64_t faddr;
314             char ec1[50];
315             int stat;
316
317             if (stream == STREAM_SPARSE_GZIP_DATA) {
318                wbuf = sd->msg + SPARSE_FADDR_SIZE;
319                wsize = sd->msglen - SPARSE_FADDR_SIZE;
320                ser_begin(sd->msg, SPARSE_FADDR_SIZE);
321                unser_uint64(faddr);
322                if (fileAddr != faddr) {
323                   fileAddr = faddr;
324                   if (lseek(ofd, (off_t)fileAddr, SEEK_SET) < 0) {
325                      Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
326                          edit_uint64(fileAddr, ec1), ofile, strerror(errno));
327                      goto bail_out;
328                   }
329                }
330             } else {
331                wbuf = sd->msg;
332                wsize = sd->msglen;
333             }
334             compress_len = compress_buf_size;
335             Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
336             if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len, 
337                   (const Byte *)wbuf, (uLong)wsize)) != Z_OK) {
338                Jmsg(jcr, M_ERROR, 0, _("Uncompression error. ERR=%d\n"), stat);
339                goto bail_out;
340             }
341
342             Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
343             if ((uLong)write(ofd, jcr->compress_buf, compress_len) != compress_len) {
344                Dmsg0(0, "===Write error===\n");
345                Jmsg2(jcr, M_ERROR, 0, "Write error on %s: %s\n", ofile, strerror(errno));
346                goto bail_out;
347             }
348             total += compress_len;
349             jcr->JobBytes += compress_len;
350             fileAddr += compress_len;
351          }
352 #else
353          if (extract) {
354             Jmsg(jcr, M_ERROR, 0, "GZIP data stream found, but GZIP not configured!\n");
355             goto bail_out;
356          }
357 #endif
358       /* If extracting, wierd stream (not 1 or 2), close output file anyway */
359       } else if (extract) {
360          Dmsg1(30, "Found wierd stream %d\n", stream);
361          if (ofd < 0) {
362             Emsg0(M_ERROR, 0, _("Logic error output file should be open\n"));
363          }
364          set_attributes(jcr, fname, ofile, lname, type, stream, 
365                         &statp, attribsEx, &ofd);
366          extract = FALSE;
367       } else if (stream != STREAM_MD5_SIGNATURE) {
368          Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
369       }
370    }
371
372    /* If output file is still open, it was the last one in the
373     * archive since we just hit an end of file, so close the file. 
374     */
375    if (ofd >= 0) {
376       set_attributes(jcr, fname, ofile, lname, type, stream, 
377                      &statp, attribsEx, &ofd);
378    }
379    set_jcr_job_status(jcr, JS_Terminated);
380    goto ok_out;
381
382 bail_out:
383    set_jcr_job_status(jcr, JS_ErrorTerminated);
384 ok_out:
385    if (jcr->compress_buf) {
386       free(jcr->compress_buf);
387       jcr->compress_buf = NULL;
388    }
389    free_pool_memory(fname);
390    free_pool_memory(ofile);
391    free_pool_memory(lname);
392    free_pool_memory(attribsEx);
393    Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
394       jcr->JobBytes);
395 }          
396
397 extern char *getuser(uid_t uid);
398 extern char *getgroup(gid_t gid);
399
400 /*
401  * Print an ls style message, also send INFO
402  */
403 static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp)
404 {
405    char buf[2000]; 
406    char ec1[30];
407    char *p, *f;
408    int n;
409
410    p = encode_mode(statp->st_mode, buf);
411    n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
412    p += n;
413    n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
414    p += n;
415    n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
416    p += n;
417    p = encode_time(statp->st_ctime, p);
418    *p++ = ' ';
419    *p++ = ' ';
420    for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
421       *p++ = *f++;
422    if (type == FT_LNK) {
423       *p++ = ' ';
424       *p++ = '-';
425       *p++ = '>';
426       *p++ = ' ';
427       /* Copy link name */
428       for (f=lname; *f && (p-buf) < (int)sizeof(buf); )
429          *p++ = *f++;
430    }
431    *p++ = '\n';
432    *p = 0;
433    Dmsg0(20, buf);
434    Jmsg(jcr, M_INFO, 0, "%s", buf);
435 }