]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/restore.c
9ee8f9fd9e474d91510011fa6fe2c782c0fb1514
[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    int32_t stream;
52    uint32_t size;
53    uint32_t VolSessionId, VolSessionTime, file_index;
54    uint32_t record_file_index;
55    struct stat statp;
56    int extract = FALSE;
57    int ofd = -1;
58    int type;
59    uint32_t total = 0;
60    
61    wherelen = strlen(jcr->where);
62
63    sd = jcr->store_bsock;
64    jcr->JobStatus = JS_Running;
65
66    if (!bnet_set_buffer_size(sd, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_READ)) {
67       return;
68    }
69    jcr->buf_size = sd->msglen;
70
71    fname = get_pool_memory(PM_FNAME);
72    ofile = get_pool_memory(PM_FNAME);
73    lname = get_pool_memory(PM_FNAME);
74
75 #ifdef HAVE_LIBZ
76    uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
77    jcr->compress_buf = (char *)bmalloc(compress_buf_size);
78 #endif
79
80    /* 
81     * Get a record from the Storage daemon
82     */
83    while (bnet_recv(sd) > 0 && !job_cancelled(jcr)) {
84       /*
85        * First we expect a Stream Record Header 
86        */
87       if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
88           &stream, &size) != 5) {
89          Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
90          goto bail_out;
91       }
92       Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
93
94       /* 
95        * Now we expect the Stream Data
96        */
97       if (bnet_recv(sd) < 0 && !job_cancelled(jcr)) {
98          Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
99       }
100       if (size != ((uint32_t) sd->msglen)) {
101          Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
102          goto bail_out;
103       }
104       Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
105
106       /* File Attributes stream */
107       if (stream == STREAM_UNIX_ATTRIBUTES) {
108          char *ap, *lp, *fp;
109
110          Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
111          /* If extracting, it was from previous stream, so
112           * close the output file.
113           */
114          if (extract) {
115             if (ofd < 0) {
116                Emsg0(M_ABORT, 0, _("Logic error output file should be open\n"));
117             }
118             close(ofd);
119             ofd = -1;
120             extract = FALSE;
121             set_statp(jcr, fname, ofile, lname, type, &statp);
122             Dmsg0(30, "Stop extracting.\n");
123          }
124
125          if ((int)sizeof_pool_memory(fname) <  sd->msglen) {
126             fname = realloc_pool_memory(fname, sd->msglen + 1);
127          }
128          if ((int)sizeof_pool_memory(ofile) < sd->msglen + wherelen + 1) {
129             ofile = realloc_pool_memory(ofile, sd->msglen + wherelen + 1);
130          }
131          if ((int)sizeof_pool_memory(lname) < sd->msglen + wherelen + 1) {
132             lname = realloc_pool_memory(lname, sd->msglen + wherelen + 1);
133          }
134          *fname = 0;
135          *lname = 0;
136
137          /*              
138           * An Attributes record consists of:
139           *    File_index
140           *    Type   (FT_types)
141           *    Filename
142           *    Attributes
143           *    Link name (if file linked i.e. FT_LNK)
144           *
145           */
146          Dmsg1(100, "Attr: %s\n", sd->msg);
147          if (sscanf(sd->msg, "%d %d", &record_file_index, &type) != 2) {
148             Jmsg(jcr, M_FATAL, 0, _("Error scanning attributes: %s\n"), sd->msg);
149             Dmsg1(000, "\nError scanning attributes. %s\n", sd->msg);
150             goto bail_out;
151          }
152          Dmsg2(100, "Got Attr: FilInx=%d type=%d\n", record_file_index, type);
153          if (record_file_index != file_index) {
154             Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
155                file_index, record_file_index);
156             Dmsg0(000, "File index error\n");
157             goto bail_out;
158          }
159          ap = sd->msg;
160          while (*ap++ != ' ')         /* skip record file index */
161             ;
162          while (*ap++ != ' ')         /* skip type */
163             ;
164          /* Save filename and position to attributes */
165          fp = fname;
166          while (*ap != 0) {
167             *fp++  = *ap++;           /* copy filename to fname */
168          }
169          *fp = *ap++;                 /* terminate filename & point to attribs */
170
171          /* Skip to Link name */
172          if (type == FT_LNK || type == FT_LNKSAVED) {
173             lp = ap;
174             while (*lp++ != 0) {
175                ;
176             }
177          } else {
178             lp = "";
179          }
180
181          decode_stat(ap, &statp);
182          /*
183           * Prepend the where directory so that the
184           * files are put where the user wants.
185           *
186           * We do a little jig here to handle Win32 files with
187           * a drive letter.  
188           *   If where is null and we are running on a win32 client,
189           *      change nothing.
190           *   Otherwise, if the second character of the filename is a
191           *   colon (:), change it into a slash (/) -- this creates
192           *   a reasonable pathname on most systems.
193           */
194          if (jcr->where[0] == 0 && win32_client) {
195             strcpy(ofile, fname);
196             strcpy(lname, lp);
197          } else {
198             strcpy(ofile, jcr->where);
199             if (fname[1] == ':') {
200                fname[1] = '/';
201                strcat(ofile, fname);
202                fname[1] = ':';
203             } else {
204                strcat(ofile, fname);
205             }
206             /* Fixup link name */
207             if (type == FT_LNK || type == FT_LNKSAVED) {
208                if (lp[0] == '/') {      /* if absolute path */
209                   strcpy(lname, jcr->where);
210                }       
211                /* ***FIXME**** we shouldn't have links on Windoz */
212                if (lp[1] == ':') {
213                   lp[1] = '/';
214                   strcat(lname, lp);
215                   lp[1] = ':';
216                } else {
217                   strcat(lname, lp);
218                }
219             }
220          }
221
222          Dmsg1(30, "Outfile=%s\n", ofile);
223          print_ls_output(jcr, ofile, lname, type, &statp);
224
225          extract = create_file(jcr, fname, ofile, lname, type, &statp, &ofd);
226          Dmsg1(40, "Extract=%d\n", extract);
227          if (extract) {
228             jcr->JobFiles++;
229          }
230          jcr->num_files_examined++;
231
232       /* Data stream */
233       } else if (stream == STREAM_FILE_DATA) {
234          if (extract) {
235             Dmsg2(30, "Write %d bytes, total before write=%d\n", sd->msglen, total);
236             if (write(ofd, sd->msg, sd->msglen) != sd->msglen) {
237                Dmsg0(0, "===Write error===\n");
238                Jmsg2(jcr, M_ERROR, 0, "Write error on %s: %s\n", ofile, strerror(errno));
239                goto bail_out;
240             }
241             total += sd->msglen;
242             jcr->JobBytes += sd->msglen;
243          }
244         
245       /* GZIP data stream */
246       } else if (stream == STREAM_GZIP_DATA) {
247 #ifdef HAVE_LIBZ
248          if (extract) {
249             uLong compress_len;
250             int stat;
251
252             compress_len = compress_buf_size;
253             Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, sd->msglen);
254             if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len, 
255                   (const Byte *)sd->msg, (uLong)sd->msglen)) != Z_OK) {
256                Jmsg(jcr, M_ERROR, 0, _("Uncompression error. ERR=%d\n"), stat);
257                goto bail_out;
258             }
259
260             Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
261             if ((uLong)write(ofd, jcr->compress_buf, compress_len) != compress_len) {
262                Dmsg0(0, "===Write error===\n");
263                Jmsg2(jcr, M_ERROR, 0, "Write error on %s: %s\n", ofile, strerror(errno));
264                goto bail_out;
265             }
266             total += compress_len;
267             jcr->JobBytes += compress_len;
268          }
269 #else
270          if (extract) {
271             Jmsg(jcr, M_ERROR, 0, "GZIP data stream found, but GZIP not configured!\n");
272             goto bail_out;
273          }
274 #endif
275       /* If extracting, wierd stream (not 1 or 2), close output file anyway */
276       } else if (extract) {
277          Dmsg1(30, "Found wierd stream %d\n", stream);
278          if (ofd < 0) {
279             Emsg0(M_ABORT, 0, _("Logic error output file should be open\n"));
280          }
281          close(ofd);
282          ofd = -1;
283          extract = FALSE;
284          set_statp(jcr, fname, ofile, lname, type, &statp);
285       } else if (stream != STREAM_MD5_SIGNATURE) {
286          Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
287       }
288    }
289
290    /* If output file is still open, it was the last one in the
291     * archive since we just hit an end of file, so close the file. 
292     */
293    if (ofd >= 0) {
294       close(ofd);
295       set_statp(jcr, fname, ofile, lname, type, &statp);
296    }
297    jcr->JobStatus = JS_Terminated;
298    goto ok_out;
299
300 bail_out:
301    jcr->JobStatus = JS_ErrorTerminated;
302 ok_out:
303    if (jcr->compress_buf) {
304       free(jcr->compress_buf);
305       jcr->compress_buf = NULL;
306    }
307    free_pool_memory(fname);
308    free_pool_memory(ofile);
309    free_pool_memory(lname);
310    Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
311       jcr->JobBytes);
312 }          
313
314 extern char *getuser(uid_t uid);
315 extern char *getgroup(gid_t gid);
316
317 /*
318  * Print an ls style message, also send INFO
319  */
320 static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp)
321 {
322    char buf[2000]; 
323    char ec1[30];
324    char *p, *f;
325    int n;
326
327    p = encode_mode(statp->st_mode, buf);
328    n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
329    p += n;
330    n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
331    p += n;
332    n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
333    p += n;
334    p = encode_time(statp->st_ctime, p);
335    *p++ = ' ';
336    *p++ = ' ';
337    for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
338       *p++ = *f++;
339    if (type == FT_LNK) {
340       *p++ = ' ';
341       *p++ = '-';
342       *p++ = '>';
343       *p++ = ' ';
344       /* Copy link name */
345       for (f=lname; *f && (p-buf) < (int)sizeof(buf); )
346          *p++ = *f++;
347    }
348    *p++ = '\n';
349    *p = 0;
350    Dmsg0(20, buf);
351    Jmsg(jcr, M_INFO, 0, buf);
352 }