]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/restore.c
First cut AutoPrune
[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, char *addr, int port)
45 {
46    int wherelen;
47    BSOCK *sd;
48    char *fname;                       /* original file name */
49    char *ofile;                       /* output name with possible prefix */
50    char *lname;                       /* link name */
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
70    fname = (char *) get_pool_memory(PM_FNAME);
71    ofile = (char *) get_pool_memory(PM_FNAME);
72    lname = (char *) get_pool_memory(PM_FNAME);
73
74    /* 
75     * Get a record from the Storage daemon
76     */
77    while (bnet_recv(sd) > 0) {
78       /*
79        * First we expect a Stream Record Header 
80        */
81       if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
82           &stream, &size) != 5) {
83          Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
84          free_pool_memory(fname);
85          free_pool_memory(ofile);
86          free_pool_memory(lname);
87          return;
88       }
89       Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
90
91       /* 
92        * Now we expect the Stream Data
93        */
94       if (bnet_recv(sd) < 0) {
95          Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
96       }
97       if (size != ((uint32_t) sd->msglen)) {
98          Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
99          free_pool_memory(fname);
100          free_pool_memory(ofile);
101          free_pool_memory(lname);
102          return;
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;
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 = (char *) realloc_pool_memory(fname, sd->msglen + 1);
127          }
128          if (sizeof_pool_memory(ofile) < sizeof_pool_memory(fname) + wherelen + 1) {
129             ofile = (char *) realloc_pool_memory(ofile, sizeof_pool_memory(fname) + wherelen + 1);
130          }
131          if ((int)sizeof_pool_memory(lname) < sd->msglen) {
132             ofile = (char *) realloc_pool_memory(ofile, sd->msglen + 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          if (sscanf(sd->msg, "%d %d %s", &record_file_index, &type, fname) != 3) {
147             Emsg1(M_FATAL, 0, _("Error scanning record header: %s\n"), sd->msg);
148             /** ****FIXME**** need to cleanup */
149             Dmsg0(0, "\nError scanning header\n");
150             return;  
151          }
152          Dmsg3(30, "Got Attr: FilInx=%d type=%d fname=%s\n", record_file_index,
153             type, fname);
154          if (record_file_index != file_index) {
155             Emsg2(M_ABORT, 0, _("Record header file index %ld not equal record index %ld\n"),
156                file_index, record_file_index);
157             Dmsg0(0, "File index error\n");
158          }
159          ap = sd->msg;
160          /* Skip to attributes */
161          while (*ap++ != 0) {
162             ;
163          }
164          /* Skip to Link name */
165          if (type == FT_LNK) {
166             lp = ap;
167             while (*lp++ != 0) {
168                ;
169             }
170             strcat(lname, lp);        /* "save" link name */
171          } else {
172             *lname = 0;
173          }
174
175          decode_stat(ap, &statp);
176          /*
177           * Prepend the where directory so that the
178           * files are put where the user wants.
179           *
180           * We do a little jig here to handle Win32 files with
181           * a drive letter.  
182           *   If where is null and we are running on a win32 client,
183           *      change nothing.
184           *   Otherwise, if the second character of the filename is a
185           *   colon (:), change it into a slash (/) -- this creates
186           *   a reasonable pathname on most systems.
187           */
188          if (jcr->where[0] == 0 && win32_client) {
189             strcpy(ofile, fname);
190          } else {
191             strcpy(ofile, jcr->where);
192             if (fname[1] == ':') {
193                fname[1] = '/';
194                strcat(ofile, fname);
195                fname[1] = ':';
196             } else {
197                strcat(ofile, fname);
198             }
199          }
200
201          Dmsg1(30, "Outfile=%s\n", ofile);
202          print_ls_output(jcr, ofile, lname, type, &statp);
203
204          extract = create_file(jcr, fname, ofile, lname, type, &statp, &ofd);
205          Dmsg1(40, "Extract=%d\n", extract);
206          if (extract) {
207             jcr->JobFiles++;
208          }
209          jcr->num_files_examined++;
210
211       /* Data stream */
212       } else if (stream == STREAM_FILE_DATA) {
213          if (extract) {
214             Dmsg2(30, "Write %d bytes, total before write=%d\n", sd->msglen, total);
215             if (write(ofd, sd->msg, sd->msglen) != sd->msglen) {
216                Dmsg0(0, "===Write error===\n");
217                Jmsg2(jcr, M_ERROR, 0, "Write error on %s: %s\n", ofile, strerror(errno));
218                free_pool_memory(fname);
219                free_pool_memory(ofile);
220                free_pool_memory(lname);
221                return;
222             }
223             total += sd->msglen;
224             jcr->JobBytes += sd->msglen;
225          }
226
227       /* If extracting, wierd stream (not 1 or 2), close output file anyway */
228       } else if (extract) {
229          Dmsg1(30, "Found wierd stream %d\n", stream);
230          if (ofd < 0) {
231             Emsg0(M_ABORT, 0, _("Logic error output file should be open\n"));
232          }
233          close(ofd);
234          ofd = -1;
235          extract = FALSE;
236          set_statp(jcr, fname, ofile, lname, type, &statp);
237       } else if (stream != STREAM_MD5_SIGNATURE) {
238          Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
239       }
240    }
241
242    /* If output file is still open, it was the last one in the
243     * archive since we just hit an end of file, so close the file. 
244     */
245    if (ofd >= 0) {
246       close(ofd);
247       set_statp(jcr, fname, ofile, lname, type, &statp);
248    }
249
250    free_pool_memory(fname);
251    free_pool_memory(ofile);
252    free_pool_memory(lname);
253    Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
254       jcr->JobBytes);
255 }          
256
257 extern char *getuser(uid_t uid);
258 extern char *getgroup(gid_t gid);
259
260 /*
261  * Print an ls style message, also send INFO
262  */
263 static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp)
264 {
265    /* ********FIXME******** make memory pool */
266    char buf[1000]; 
267    char *p, *f;
268    int n;
269
270    p = encode_mode(statp->st_mode, buf);
271    n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
272    p += n;
273    n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
274    p += n;
275    n = sprintf(p, "%8" lld " ", (uint64_t)statp->st_size);
276    p += n;
277    p = encode_time(statp->st_ctime, p);
278    *p++ = ' ';
279    *p++ = ' ';
280    for (f=fname; *f; )
281       *p++ = *f++;
282    if (type == FT_LNK) {
283       *p++ = ' ';
284       *p++ = '-';
285       *p++ = '>';
286       *p++ = ' ';
287       /* Copy link name */
288       for (f=lname; *f; )
289          *p++ = *f++;
290    }
291    *p++ = '\n';
292    *p = 0;
293    Dmsg0(20, buf);
294    Jmsg(jcr, M_INFO, 0, buf);
295 }