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