]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/backup.c
Initial revision
[bacula/bacula] / bacula / src / filed / backup.c
1 /*
2  *  Bacula File Daemon  backup.c  send file attributes and data
3  *   to the Storage daemon.
4  *
5  *    Kern Sibbald, March MM
6  *
7  */
8 /*
9    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
10
11    This program is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License as
13    published by the Free Software Foundation; either version 2 of
14    the License, or (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public
22    License along with this program; if not, write to the Free
23    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24    MA 02111-1307, USA.
25
26  */
27
28 #include "bacula.h"
29 #include "filed.h"
30
31 static int save_file(FF_PKT *ff_pkt, void *pkt);
32
33 /* 
34  * Find all the requested files and send them
35  * to the Storage daemon.
36  * 
37  */
38 int blast_data_to_storage_daemon(JCR *jcr, char *addr, int port)
39 {
40    BSOCK *sd;
41    int stat = 1;
42
43    sd = jcr->store_bsock;
44
45    jcr->JobStatus = JS_Running;
46
47    Dmsg1(10, "bfiled: opened data connection %d to stored\n", sd->fd);
48
49    if (!bnet_set_buffer_size(sd, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_WRITE)) {
50       return 0;
51    }
52    jcr->buf_size = sd->msglen;             
53
54    jcr->compress_buf = (char *) bmalloc(jcr->buf_size);
55
56    Dmsg1(100, "set_find_options ff=%p\n", jcr->ff);
57    set_find_options(jcr->ff, jcr->incremental, jcr->mtime);
58    Dmsg0(10, "start find files\n");
59
60    /* Subroutine save_file() is called for each file */
61    /* ***FIXME**** add FSM code */
62    if (!find_files(jcr->ff, save_file, (void *)jcr)) {
63       stat = 0;                       /* error */
64    }
65
66    bnet_sig(sd, BNET_EOF);            /* terminate data connection */
67
68    if (jcr->big_buf) {
69       free(jcr->big_buf);
70       jcr->big_buf = NULL;
71    }
72    if (jcr->compress_buf) {
73       free(jcr->compress_buf);
74       jcr->compress_buf = NULL;
75    }
76    return stat;
77 }          
78
79 /* 
80  * Called here by find() for each file included.
81  *
82  *  *****FIXME*****   add FSMs File System Modules
83  *
84  *  Send the file and its data to the Storage daemon.
85  */
86 static int save_file(FF_PKT *ff_pkt, void *ijcr)
87 {
88    char attribs[MAXSTRING];
89    int fid, stat, stream;
90    size_t read_size;
91    struct MD5Context md5c;
92    int gotMD5 = 0;
93    unsigned char signature[16];
94    BSOCK *sd, *dir;
95    JCR *jcr = (JCR *)ijcr;
96    char *msgsave;
97
98    sd = jcr->store_bsock;
99    dir = jcr->dir_bsock;
100    jcr->num_files_examined++;         /* bump total file count */
101
102    switch (ff_pkt->type) {
103    case FT_LNKSAVED:                  /* Hard linked, file already saved */
104       break;
105    case FT_REGE:
106       Dmsg1(30, "FT_REGE saving: %s\n", ff_pkt->fname);
107       break;
108    case FT_REG:
109       Dmsg1(30, "FT_REG saving: %s\n", ff_pkt->fname);
110       break;
111    case FT_LNK:
112       Dmsg2(30, "FT_LNK saving: %s -> %s\n", ff_pkt->fname, ff_pkt->link);
113       break;
114    case FT_DIR:
115       Dmsg1(30, "FT_DIR saving: %s\n", ff_pkt->link);
116       break;
117    case FT_SPEC:
118       Dmsg1(30, "FT_SPEC saving: %s\n", ff_pkt->fname);
119       break;
120    case FT_NOACCESS:
121       Jmsg(jcr, M_NOTSAVED, -1, _("     Could not access %s: ERR=%s"), ff_pkt->fname, 
122          strerror(ff_pkt->ff_errno));
123       return 1;
124    case FT_NOFOLLOW:
125       Jmsg(jcr, M_NOTSAVED, -1, _("     Could not follow link %s: ERR=%s\n"), ff_pkt->fname, 
126          strerror(ff_pkt->ff_errno));
127       return 1;
128    case FT_NOSTAT:
129       Jmsg(jcr, M_NOTSAVED, -1, _("     Could not stat %s: ERR=%s\n"), ff_pkt->fname, 
130          strerror(ff_pkt->ff_errno));
131       return 1;
132    case FT_DIRNOCHG:
133    case FT_NOCHG:
134       Jmsg(jcr, M_SKIPPED, -1,  _("     Unchanged file skipped: %s\n"), ff_pkt->fname);
135       return 1;
136    case FT_ISARCH:
137       Jmsg(jcr, M_NOTSAVED, -1, _("     Archive file not saved: %s\n"), ff_pkt->fname);
138       return 1;
139    case FT_NORECURSE:
140       Jmsg(jcr, M_SKIPPED, -1,  _("     Recursion turned off. Directory skipped: %s\n"), 
141          ff_pkt->fname);
142       return 1;
143    case FT_NOFSCHG:
144       Jmsg(jcr, M_SKIPPED, -1,  _("     File system change prohibited. Directory skipped. %s\n"), 
145          ff_pkt->fname);
146       return 1;
147    case FT_NOOPEN:
148       Jmsg(jcr, M_NOTSAVED, -1, _("     Could not open directory %s: ERR=%s\n"), ff_pkt->fname, 
149          strerror(ff_pkt->ff_errno));
150       return 1;
151    default:
152       Jmsg(jcr, M_ERROR, 0, _("Unknown file type %d; not saved: %s\n"), ff_pkt->type, ff_pkt->fname);
153       return 1;
154    }
155
156    if (ff_pkt->type != FT_LNKSAVED && S_ISREG(ff_pkt->statp.st_mode) && 
157          ff_pkt->statp.st_size > 0) {
158       if ((fid = open(ff_pkt->fname, O_RDONLY | O_BINARY)) < 0) {
159          ff_pkt->ff_errno = errno;
160          Jmsg(jcr, M_NOTSAVED, -1, _("Cannot open %s: ERR=%s.\n"), ff_pkt->fname, strerror(ff_pkt->ff_errno));
161          return 1;
162       }
163    } else {
164       fid = -1;
165    }
166
167    Dmsg1(30, "bfiled: sending %s to stored\n", ff_pkt->fname);
168    encode_stat(attribs, &ff_pkt->statp);
169      
170    jcr->JobFiles++;                    /* increment number of files sent */
171    jcr->last_fname = (char *) check_pool_memory_size(jcr->last_fname, strlen(ff_pkt->fname) + 1);
172    strcpy(jcr->last_fname, ff_pkt->fname);
173     
174    /*
175     * Send Attributes header to Storage daemon
176     *    <file-index> <stream> <info>
177     */
178    if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, STREAM_UNIX_ATTRIBUTES)) {
179       if (fid >= 0) {
180          close(fid);
181       }
182       return 0;
183    }
184    Dmsg1(10, ">stored: attrhdr %s\n", sd->msg);
185
186    /* 
187     * Send file attributes to Storage daemon   
188     *   File_index
189     *   File type
190     *   Filename (full path)
191     *   Encoded attributes
192     *   Link name (if type==FT_LNK)
193     * For a directory, link is the same as fname, but with trailing
194     * slash. For a linked file, link is the link.
195     */
196    if (ff_pkt->type == FT_LNK) {
197       stat = bnet_fsend(sd, "%ld %d %s%c%s%c%s%c", jcr->JobFiles, 
198                ff_pkt->type, ff_pkt->fname, 0, attribs, 0, ff_pkt->link, 0);
199    } else if (ff_pkt->type == FT_DIR) {
200       stat = bnet_fsend(sd, "%ld %d %s%c%s%c%c", jcr->JobFiles, 
201                ff_pkt->type, ff_pkt->link, 0, attribs, 0, 0);
202    } else {
203       stat = bnet_fsend(sd, "%ld %d %s%c%s%c%c", jcr->JobFiles, 
204                ff_pkt->type, ff_pkt->fname, 0, attribs, 0, 0);
205    }
206
207    Dmsg2(20, ">stored: attr len=%d: %s\n", sd->msglen, sd->msg);
208    if (!stat) {
209       if (fid >= 0) {
210          close(fid);
211       }
212       return 0;
213    }
214    /* send data termination sentinel */
215    bnet_sig(sd, BNET_EOD);
216
217    /* 
218     * If the file has data, read it and send to the Storage daemon
219     *
220     */
221    if (fid >= 0) {
222
223       Dmsg1(60, "Saving data, type=%d\n", ff_pkt->type);
224       /*
225        * Send Data header to Storage daemon
226        *    <file-index> <stream> <info>
227        */
228       if (ff_pkt->flags & FO_GZIP) {
229          stream = STREAM_GZIP_DATA;
230          /* Adjust for compression so that output buffer is
231           * 12 bytes + 0.1% larger than input buffer
232           */
233          read_size = jcr->buf_size - 12 - (jcr->buf_size / 1000) - 1;
234       } else {
235          stream = STREAM_FILE_DATA;
236          read_size = jcr->buf_size;
237       }
238       if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, stream)) {
239          close(fid);
240          return 0;
241       }
242       Dmsg1(10, ">stored: datahdr %s\n", sd->msg);
243
244       if (ff_pkt->flags & FO_MD5) {
245          MD5Init(&md5c);
246       }
247
248       msgsave = sd->msg;
249       while ((sd->msglen=read(fid, sd->msg, read_size)) > 0) {
250 #ifdef HAVE_LIBZ
251          uLongf compress_len;
252 #endif
253
254          if (ff_pkt->flags & FO_MD5) {
255             MD5Update(&md5c, (unsigned char *) (sd->msg), sd->msglen);
256             gotMD5 = 1;
257          }
258          /* ***FIXME*** add compression level options */
259 #ifdef HAVE_LIBZ
260          if (ff_pkt->flags & FO_GZIP) {
261             if (compress((Bytef *)jcr->compress_buf, &compress_len, 
262                   (const Bytef *)sd->msg, (uLong)sd->msglen) != Z_OK) {
263                Jmsg(jcr, M_ERROR, 0, _("Compression error\n"));
264                sd->msg = msgsave;
265                sd->msglen = 0;
266                close(fid);
267                return 0;
268             }
269             sd->msg = jcr->compress_buf;
270             sd->msglen = compress_len;
271          }
272 #endif
273          if (!bnet_send(sd)) {
274             sd->msg = msgsave;
275             sd->msglen = 0;
276             close(fid);
277             return 0;
278          }
279          Dmsg1(30, "Send data to FD len=%d\n", sd->msglen);
280          jcr->JobBytes += sd->msglen;
281       }
282       if (sd->msglen < 0) {
283          Jmsg(jcr, M_ERROR, 0, _("Error during save reading ERR=%s\n"), ff_pkt->fname, 
284             strerror(ff_pkt->ff_errno));
285       }
286       sd->msg = msgsave;
287
288       /* Send data termination poll signal to Storage daemon.
289        *  NOTE possibly put this poll on a counter as specified
290        *  by the user to improve efficiency (i.e. poll every
291        *  other file, every third file, ... 
292        */
293       bnet_sig(sd, BNET_EOD_POLL);
294       Dmsg0(30, "Send EndData_Poll\n");
295       /* ***FIXME**** change to use bget_msg() */
296       if (bnet_recv(sd) <= 0) {
297          close(fid);
298          return 0;
299       } else {
300          if (strcmp(sd->msg, "3000 OK\n") != 0) {
301            Jmsg1(jcr, M_ERROR, 0, _("Job aborted by Storage daemon: %s\n"), sd->msg);
302            close(fid);
303            return 0;
304          }
305       }
306       close(fid);                        /* close file */
307    }
308
309
310    /* Terminate any MD5 signature and send it to Storage daemon and the Director */
311    if (gotMD5 && ff_pkt->flags & FO_MD5) {
312 #ifdef really_needed
313       char MD5buf[50];                /* 24 bytes should do */
314 #endif
315
316       MD5Final(signature, &md5c);
317
318       /* First do Storage daemon */
319       bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, STREAM_MD5_SIGNATURE);
320       Dmsg1(10, "bfiled>stored:header %s\n", sd->msg);
321       memcpy(sd->msg, signature, 16);
322       sd->msglen = 16;
323       bnet_send(sd);
324       bnet_sig(sd, BNET_EOD);         /* end of MD5 */
325
326 #ifdef really_needed
327       /* Now do Director (single record) */
328       bin_to_base64(MD5buf, (char *)signature, 16); /* encode 16 bytes */
329       bnet_fsend(dir, "%ld %d %s X", jcr->JobFiles, STREAM_MD5_SIGNATURE, 
330                  MD5buf);
331 #endif
332       gotMD5 = 0;
333    }
334 #ifdef really_needed
335    if (ff_pkt->type == FT_DIR) {
336       Jmsg(jcr, M_SAVED, -1, _("     Directory saved normally: %s\n"), ff_pkt->link);
337    } else {
338       Jmsg(jcr, M_SAVED, -1, _("     File saved normally: %s\n"), ff_pkt->fname);
339    }
340 #endif
341    return 1;
342 }