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