]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/verify.c
a083c0b1e0394e312376be95368ba1f3cf627d79
[bacula/bacula] / bacula / src / filed / verify.c
1 /*
2  *  Bacula File Daemon  verify.c  Verify files.
3  *
4  *    Kern Sibbald, October MM
5  *
6  */
7 /*
8    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2 of
13    the License, or (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public
21    License along with this program; if not, write to the Free
22    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23    MA 02111-1307, USA.
24
25  */
26
27 #include "bacula.h"
28 #include "filed.h"
29
30 static int verify_file(FF_PKT *ff_pkt, void *my_pkt);
31
32 /* 
33  * Find all the requested files and send attributes
34  * to the Director.
35  * 
36  */
37 void do_verify(JCR *jcr)
38 {
39    BSOCK *dir = jcr->dir_bsock;
40    
41    jcr->buf_size = MAX_NETWORK_BUFFER_SIZE;
42    if ((jcr->big_buf = (char *) malloc(jcr->buf_size)) == NULL) {
43       Emsg1(M_ABORT, 0, _("Cannot malloc %d network read buffer\n"), MAX_NETWORK_BUFFER_SIZE);
44    }
45    set_find_options(jcr->ff, jcr->incremental, jcr->mtime);
46    Dmsg0(10, "Start find files\n");
47    /* Subroutine verify_file() is called for each file */
48    find_files(jcr->ff, verify_file, (void *)jcr);  
49    Dmsg0(10, "End find files\n");
50
51    bnet_sig(dir, BNET_EOD);           /* signal end of data */
52
53    if (jcr->big_buf) {
54       free(jcr->big_buf);
55       jcr->big_buf = NULL;
56    }
57 }          
58
59 /* 
60  * Called here by find() for each file.
61
62     *****FIXME*****   add FSMs File System Modules
63  
64  *
65  *  Find the file, compute the MD5 and send it back to the Director
66  */
67 static int verify_file(FF_PKT *ff_pkt, void *pkt) 
68 {
69    char attribs[MAXSTRING];
70    int32_t n;
71    int fid, stat;
72    struct MD5Context md5c;
73    unsigned char signature[16];
74    BSOCK *sd, *dir;
75    JCR *jcr = (JCR *)pkt;
76
77    sd = jcr->store_bsock;
78    dir = jcr->dir_bsock;
79
80    switch (ff_pkt->type) {
81    case FT_LNKSAVED:                  /* Hard linked, file already saved */
82       break;
83    case FT_REGE:
84       Dmsg1(30, "FT_REGE saving: %s\n", ff_pkt->fname);
85       break;
86    case FT_REG:
87       Dmsg1(30, "FT_REG saving: %s\n", ff_pkt->fname);
88       break;
89    case FT_LNK:
90       Dmsg2(30, "FT_LNK saving: %s -> %s\n", ff_pkt->fname, ff_pkt->link);
91       break;
92    case FT_DIR:
93       Dmsg1(30, "FT_DIR saving: %s\n", ff_pkt->fname);
94       break;
95    case FT_SPEC:
96       Dmsg1(30, "FT_SPEC saving: %s\n", ff_pkt->fname);
97       break;
98    case FT_NOACCESS:
99       Jmsg(jcr, M_ERROR, -1, _("     Could not access %s: ERR=%s\n"), ff_pkt->fname, strerror(ff_pkt->ff_errno));
100       return 1;
101    case FT_NOFOLLOW:
102       Jmsg(jcr, M_ERROR, -1, _("     Could not follow link %s: ERR=%s\n"), ff_pkt->fname, strerror(ff_pkt->ff_errno));
103       return 1;
104    case FT_NOSTAT:
105       Jmsg(jcr, M_ERROR, -1, _("      Could not stat %s: ERR=%s\n"), ff_pkt->fname, strerror(ff_pkt->ff_errno));
106       return 1;
107    case FT_DIRNOCHG:
108    case FT_NOCHG:
109       Jmsg(jcr, M_INFO, -1, _("     Unchanged file skipped: %s\n"), ff_pkt->fname);
110       return 1;
111    case FT_ISARCH:
112       Jmsg(jcr, M_SKIPPED, -1, _("     Archive file skipped: %s\n"), ff_pkt->fname);
113       return 1;
114    case FT_NORECURSE:
115       Jmsg(jcr, M_SKIPPED, -1, _("     Recursion turned off. Directory skipped: %s\n"), ff_pkt->fname);
116       return 1;
117    case FT_NOFSCHG:
118       Jmsg(jcr, M_SKIPPED, -1, _("     File system change prohibited. Directory skipped: %s\n"), ff_pkt->fname);
119       return 1;
120    case FT_NOOPEN:
121       Jmsg(jcr, M_ERROR, -1, _("     Could not open directory %s: ERR=%s\n"), ff_pkt->fname, strerror(ff_pkt->ff_errno));
122       return 1;
123    default:
124       Jmsg(jcr, M_ERROR, 0, _("Unknown file type %d: %s\n"), ff_pkt->type, ff_pkt->fname);
125       return 1;
126    }
127
128
129    if (ff_pkt->type != FT_LNKSAVED && S_ISREG(ff_pkt->statp.st_mode) && 
130          ff_pkt->statp.st_size > 0) {
131       if ((fid = open(ff_pkt->fname, O_RDONLY | O_BINARY)) < 0) {
132          ff_pkt->ff_errno = errno;
133          Jmsg(jcr, M_NOTSAVED, -1, _("Cannot open %s: ERR=%s.\n"), ff_pkt->fname, strerror(ff_pkt->ff_errno));
134          return 1;
135       }
136    } else {
137       fid = -1;
138    }
139
140    encode_stat(attribs, &ff_pkt->statp);
141      
142    jcr->JobFiles++;                  /* increment number of files sent */
143     
144    if (ff_pkt->VerifyOpts[0] == 0) {
145       ff_pkt->VerifyOpts[0] = 'V';
146       ff_pkt->VerifyOpts[1] = 0;
147    }
148
149
150    /* 
151     * Send file attributes to Director
152     *   File_index
153     *   Stream
154     *   Verify Options
155     *   Filename (full path)
156     *   Encoded attributes
157     *   Link name (if type==FT_LNK)
158     * For a directory, link is the same as fname, but with trailing
159     * slash. For a linked file, link is the link.
160     */
161    /* Send file attributes to Director (note different format than for Storage) */
162    Dmsg2(400, "send ATTR inx=%d fname=%s\n", jcr->JobFiles, ff_pkt->fname);
163    if (ff_pkt->type == FT_LNK) {
164       stat = bnet_fsend(dir, "%d %d %s %s%c%s%c%s%c", jcr->JobFiles,
165                     STREAM_UNIX_ATTRIBUTES, ff_pkt->VerifyOpts, ff_pkt->fname, 
166                     0, attribs, 0, ff_pkt->link, 0);
167    } else {
168       stat = bnet_fsend(dir,"%d %d %s %s%c%s%c%c", jcr->JobFiles,
169                     STREAM_UNIX_ATTRIBUTES, ff_pkt->VerifyOpts, ff_pkt->fname, 
170                     0, attribs, 0, 0);
171    }
172    Dmsg2(20, "bfiled>bdird: attribs len=%d: msg=%s\n", dir->msglen, dir->msg);
173    if (!stat) {
174       Jmsg(jcr, M_ERROR, 0, _("Network error in send to Director: ERR=%s\n"), bnet_strerror(dir));
175       if (fid >= 0) {
176          close(fid);
177       }
178       return 0;
179    }
180
181    /* If file opened, compute MD5 */
182    if (fid >= 0  && ff_pkt->flags & FO_MD5) {
183       char MD5buf[50];                /* 24 should do */
184
185       MD5Init(&md5c);
186
187       while ((n=read(fid, jcr->big_buf, jcr->buf_size)) > 0) {
188          MD5Update(&md5c, ((unsigned char *) jcr->big_buf), n);
189          jcr->JobBytes += n;
190       }
191       if (n < 0) {
192          Jmsg(jcr, M_WARNING, -1, _("  Error reading file %s: ERR=%s\n"), ff_pkt->fname, strerror(ff_pkt->ff_errno));
193       }
194
195       MD5Final(signature, &md5c);
196
197       bin_to_base64(MD5buf, (char *)signature, 16); /* encode 16 bytes */
198       Dmsg2(400, "send inx=%d MD5=%s\n", jcr->JobFiles, MD5buf);
199       bnet_fsend(dir, "%d %d %s *MD5-%d*", jcr->JobFiles, STREAM_MD5_SIGNATURE, MD5buf,
200          jcr->JobFiles);
201       Dmsg2(20, "bfiled>bdird: MD5 len=%d: msg=%s\n", dir->msglen, dir->msg);
202    }
203    if (fid >= 0) {
204       close(fid);
205    }
206    return 1;
207 }