]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/restore.c
a0acc19d5127be7063524b7e6ce1dd48cf38410e
[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, 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    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
249          extract = FALSE;
250          stat = create_file(jcr, fname, ofile, lname, type, 
251                             stream, &statp, attribsEx, &ofd, jcr->replace);
252          switch (stat) {
253          case CF_ERROR:
254          case CF_SKIP:
255             break;
256          case CF_EXTRACT:
257             extract = TRUE;
258             pm_strcpy(&jcr->last_fname, ofile);
259             /* Fall-through wanted */
260          case CF_CREATED:
261             jcr->JobFiles++;
262             fileAddr = 0;
263             print_ls_output(jcr, ofile, lname, type, &statp);
264             break;
265          }  
266
267          jcr->num_files_examined++;
268
269       /* Data stream */
270       } else if (stream == STREAM_FILE_DATA || stream == STREAM_SPARSE_DATA) {
271          if (extract) {
272             if (stream == STREAM_SPARSE_DATA) {
273                ser_declare;
274                uint64_t faddr;
275                char ec1[50];
276
277                wbuf = sd->msg + SPARSE_FADDR_SIZE;
278                wsize = sd->msglen - SPARSE_FADDR_SIZE;
279                ser_begin(sd->msg, SPARSE_FADDR_SIZE);
280                unser_uint64(faddr);
281                if (fileAddr != faddr) {
282                   fileAddr = faddr;
283                   if (lseek(ofd, (off_t)fileAddr, SEEK_SET) < 0) {
284                      Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
285                          edit_uint64(fileAddr, ec1), ofile, strerror(errno));
286                      goto bail_out;
287                   }
288                }
289             } else {
290                wbuf = sd->msg;
291                wsize = sd->msglen;
292             }
293             Dmsg2(30, "Write %u bytes, total before write=%u\n", wsize, total);
294             if ((uint32_t)write(ofd, wbuf, wsize) != wsize) {
295                Dmsg0(0, "===Write error===\n");
296                Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), ofile, strerror(errno));
297                goto bail_out;
298             }
299             total += wsize;
300             jcr->JobBytes += wsize;
301             fileAddr += wsize;
302          }
303         
304       /* GZIP data stream */
305       } else if (stream == STREAM_GZIP_DATA || stream == STREAM_SPARSE_GZIP_DATA) {
306 #ifdef HAVE_LIBZ
307          if (extract) {
308             ser_declare;
309             uLong compress_len;
310             uint64_t faddr;
311             char ec1[50];
312             int stat;
313
314             if (stream == STREAM_SPARSE_GZIP_DATA) {
315                wbuf = sd->msg + SPARSE_FADDR_SIZE;
316                wsize = sd->msglen - SPARSE_FADDR_SIZE;
317                ser_begin(sd->msg, SPARSE_FADDR_SIZE);
318                unser_uint64(faddr);
319                if (fileAddr != faddr) {
320                   fileAddr = faddr;
321                   if (lseek(ofd, (off_t)fileAddr, SEEK_SET) < 0) {
322                      Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
323                          edit_uint64(fileAddr, ec1), ofile, strerror(errno));
324                      goto bail_out;
325                   }
326                }
327             } else {
328                wbuf = sd->msg;
329                wsize = sd->msglen;
330             }
331             compress_len = compress_buf_size;
332             Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
333             if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len, 
334                   (const Byte *)wbuf, (uLong)wsize)) != Z_OK) {
335                Jmsg(jcr, M_ERROR, 0, _("Uncompression error. ERR=%d\n"), stat);
336                goto bail_out;
337             }
338
339             Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
340             if ((uLong)write(ofd, jcr->compress_buf, compress_len) != compress_len) {
341                Dmsg0(0, "===Write error===\n");
342                Jmsg2(jcr, M_ERROR, 0, "Write error on %s: %s\n", ofile, strerror(errno));
343                goto bail_out;
344             }
345             total += compress_len;
346             jcr->JobBytes += compress_len;
347             fileAddr += compress_len;
348          }
349 #else
350          if (extract) {
351             Jmsg(jcr, M_ERROR, 0, "GZIP data stream found, but GZIP not configured!\n");
352             goto bail_out;
353          }
354 #endif
355       /* If extracting, wierd stream (not 1 or 2), close output file anyway */
356       } else if (extract) {
357          Dmsg1(30, "Found wierd stream %d\n", stream);
358          if (ofd < 0) {
359             Emsg0(M_ERROR, 0, _("Logic error output file should be open\n"));
360          }
361          set_attributes(jcr, fname, ofile, lname, type, stream, 
362                         &statp, attribsEx, &ofd);
363          extract = FALSE;
364       } else if (stream != STREAM_MD5_SIGNATURE) {
365          Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
366       }
367    }
368
369    /* If output file is still open, it was the last one in the
370     * archive since we just hit an end of file, so close the file. 
371     */
372    if (ofd >= 0) {
373       set_attributes(jcr, fname, ofile, lname, type, stream, 
374                      &statp, attribsEx, &ofd);
375    }
376    jcr->JobStatus = JS_Terminated;
377    goto ok_out;
378
379 bail_out:
380    jcr->JobStatus = JS_ErrorTerminated;
381 ok_out:
382    if (jcr->compress_buf) {
383       free(jcr->compress_buf);
384       jcr->compress_buf = NULL;
385    }
386    free_pool_memory(fname);
387    free_pool_memory(ofile);
388    free_pool_memory(lname);
389    free_pool_memory(attribsEx);
390    Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
391       jcr->JobBytes);
392 }          
393
394 extern char *getuser(uid_t uid);
395 extern char *getgroup(gid_t gid);
396
397 /*
398  * Print an ls style message, also send INFO
399  */
400 static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp)
401 {
402    char buf[2000]; 
403    char ec1[30];
404    char *p, *f;
405    int n;
406
407    p = encode_mode(statp->st_mode, buf);
408    n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
409    p += n;
410    n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
411    p += n;
412    n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
413    p += n;
414    p = encode_time(statp->st_ctime, p);
415    *p++ = ' ';
416    *p++ = ' ';
417    for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
418       *p++ = *f++;
419    if (type == FT_LNK) {
420       *p++ = ' ';
421       *p++ = '-';
422       *p++ = '>';
423       *p++ = ' ';
424       /* Copy link name */
425       for (f=lname; *f && (p-buf) < (int)sizeof(buf); )
426          *p++ = *f++;
427    }
428    *p++ = '\n';
429    *p = 0;
430    Dmsg0(20, buf);
431    Jmsg(jcr, M_INFO, 0, buf);
432 }