]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/verify.c
lib/bmisc.c -> bsys.c cleanup error status a bit + working directory checks
[bacula/bacula] / bacula / src / filed / verify.c
1 /*
2  *  Bacula File Daemon  verify.c  Verify files.
3  *
4  *    Kern Sibbald, October 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 static int verify_file(FF_PKT *ff_pkt, void *my_pkt);
33
34 /* 
35  * Find all the requested files and send attributes
36  * to the Director.
37  * 
38  */
39 void do_verify(JCR *jcr)
40 {
41    BSOCK *dir = jcr->dir_bsock;
42    
43    jcr->buf_size = MAX_NETWORK_BUFFER_SIZE;
44    if ((jcr->big_buf = (char *) malloc(jcr->buf_size)) == NULL) {
45       Jmsg1(jcr, M_ABORT, 0, _("Cannot malloc %d network read buffer\n"), MAX_NETWORK_BUFFER_SIZE);
46    }
47    set_find_options((FF_PKT *)jcr->ff, jcr->incremental, jcr->mtime);
48    Dmsg0(10, "Start find files\n");
49    /* Subroutine verify_file() is called for each file */
50    find_files(jcr, (FF_PKT *)jcr->ff, verify_file, (void *)jcr);  
51    Dmsg0(10, "End find files\n");
52
53    bnet_sig(dir, BNET_EOD);           /* signal end of data */
54
55    if (jcr->big_buf) {
56       free(jcr->big_buf);
57       jcr->big_buf = NULL;
58    }
59 }          
60
61 /* 
62  * Called here by find() for each file.
63  *
64  *  Find the file, compute the MD5 or SHA1 and send it back to the Director
65  */
66 static int verify_file(FF_PKT *ff_pkt, void *pkt) 
67 {
68    char attribs[MAXSTRING];
69    int32_t n;
70    int stat;
71    BFILE bfd;
72    struct MD5Context md5c;
73    struct SHA1Context sha1c;
74    unsigned char signature[25];       /* large enough for either */
75    BSOCK *dir;
76    JCR *jcr = (JCR *)pkt;
77
78    if (job_canceled(jcr)) {
79       return 0;
80    }
81    
82    dir = jcr->dir_bsock;
83    jcr->num_files_examined++;         /* bump total file count */
84
85    switch (ff_pkt->type) {
86    case FT_LNKSAVED:                  /* Hard linked, file already saved */
87       Dmsg2(30, "FT_LNKSAVED saving: %s => %s\n", ff_pkt->fname, ff_pkt->link);
88       break;
89    case FT_REGE:
90       Dmsg1(30, "FT_REGE saving: %s\n", ff_pkt->fname);
91       break;
92    case FT_REG:
93       Dmsg1(30, "FT_REG saving: %s\n", ff_pkt->fname);
94       break;
95    case FT_LNK:
96       Dmsg2(30, "FT_LNK saving: %s -> %s\n", ff_pkt->fname, ff_pkt->link);
97       break;
98    case FT_DIR:
99       Dmsg1(30, "FT_DIR saving: %s\n", ff_pkt->fname);
100       break;
101    case FT_SPEC:
102       Dmsg1(30, "FT_SPEC saving: %s\n", ff_pkt->fname);
103       break;
104    case FT_RAW:
105       Dmsg1(30, "FT_RAW saving: %s\n", ff_pkt->fname);
106       break;
107    case FT_FIFO:
108       Dmsg1(30, "FT_FIFO saving: %s\n", ff_pkt->fname);
109       break;
110    case FT_NOACCESS:
111       Jmsg(jcr, M_NOTSAVED, -1, _("     Could not access %s: ERR=%s\n"), ff_pkt->fname, strerror(ff_pkt->ff_errno));
112       jcr->Errors++;
113       return 1;
114    case FT_NOFOLLOW:
115       Jmsg(jcr, M_NOTSAVED, -1, _("     Could not follow link %s: ERR=%s\n"), ff_pkt->fname, strerror(ff_pkt->ff_errno));
116       jcr->Errors++;
117       return 1;
118    case FT_NOSTAT:
119       Jmsg(jcr, M_NOTSAVED, -1, _("     Could not stat %s: ERR=%s\n"), ff_pkt->fname, strerror(ff_pkt->ff_errno));
120       jcr->Errors++;
121       return 1;
122    case FT_DIRNOCHG:
123    case FT_NOCHG:
124       Jmsg(jcr, M_INFO, -1, _("     Unchanged file skipped: %s\n"), ff_pkt->fname);
125       return 1;
126    case FT_ISARCH:
127       Jmsg(jcr, M_SKIPPED, -1, _("     Archive file skipped: %s\n"), ff_pkt->fname);
128       return 1;
129    case FT_NORECURSE:
130       Jmsg(jcr, M_SKIPPED, -1, _("     Recursion turned off. Directory skipped: %s\n"), ff_pkt->fname);
131       return 1;
132    case FT_NOFSCHG:
133       Jmsg(jcr, M_SKIPPED, -1, _("     File system change prohibited. Directory skipped: %s\n"), ff_pkt->fname);
134       return 1;
135    case FT_NOOPEN:
136       Jmsg(jcr, M_NOTSAVED, -1, _("     Could not open directory %s: ERR=%s\n"), ff_pkt->fname, strerror(ff_pkt->ff_errno));
137       jcr->Errors++;
138       return 1;
139    default:
140       Jmsg(jcr, M_NOTSAVED, 0, _("     Unknown file type %d: %s\n"), ff_pkt->type, ff_pkt->fname);
141       jcr->Errors++;
142       return 1;
143    }
144
145    binit(&bfd);
146
147    if (ff_pkt->type != FT_LNKSAVED && (S_ISREG(ff_pkt->statp.st_mode) && 
148          ff_pkt->statp.st_size > 0) || 
149          ff_pkt->type == FT_RAW || ff_pkt->type == FT_FIFO) {
150       if ((bopen(&bfd, ff_pkt->fname, O_RDONLY | O_BINARY, 0)) < 0) {
151          ff_pkt->ff_errno = errno;
152          Jmsg(jcr, M_NOTSAVED, -1, _("     Cannot open %s: ERR=%s.\n"),
153               ff_pkt->fname, berror(&bfd));
154          jcr->Errors++;
155          return 1;
156       }
157    }
158
159    encode_stat(attribs, &ff_pkt->statp, ff_pkt->LinkFI);
160      
161    P(jcr->mutex);
162    jcr->JobFiles++;                  /* increment number of files sent */
163    pm_strcpy(&jcr->last_fname, ff_pkt->fname);
164    V(jcr->mutex);
165
166    /* 
167     * Send file attributes to Director
168     *   File_index
169     *   Stream
170     *   Verify Options
171     *   Filename (full path)
172     *   Encoded attributes
173     *   Link name (if type==FT_LNK)
174     * For a directory, link is the same as fname, but with trailing
175     * slash. For a linked file, link is the link.
176     */
177    /* Send file attributes to Director (note different format than for Storage) */
178    Dmsg2(400, "send ATTR inx=%d fname=%s\n", jcr->JobFiles, ff_pkt->fname);
179    if (ff_pkt->type == FT_LNK || ff_pkt->type == FT_LNKSAVED) {
180       stat = bnet_fsend(dir, "%d %d %s %s%c%s%c%s%c", jcr->JobFiles,
181                     STREAM_UNIX_ATTRIBUTES, ff_pkt->VerifyOpts, ff_pkt->fname, 
182                     0, attribs, 0, ff_pkt->link, 0);
183    } else {
184       stat = bnet_fsend(dir,"%d %d %s %s%c%s%c%c", jcr->JobFiles,
185                     STREAM_UNIX_ATTRIBUTES, ff_pkt->VerifyOpts, ff_pkt->fname, 
186                     0, attribs, 0, 0);
187    }
188    Dmsg2(20, "bfiled>bdird: attribs len=%d: msg=%s\n", dir->msglen, dir->msg);
189    if (!stat) {
190       Jmsg(jcr, M_ERROR, 0, _("Network error in send to Director: ERR=%s\n"), bnet_strerror(dir));
191       if (is_bopen(&bfd)) {
192          bclose(&bfd);
193       }
194       return 0;
195    }
196
197    /* If file opened, compute MD5 */
198    if (is_bopen(&bfd)  && ff_pkt->flags & FO_MD5) {
199       char MD5buf[40];                /* 24 should do */
200       MD5Init(&md5c);
201       while ((n=bread(&bfd, jcr->big_buf, jcr->buf_size)) > 0) {
202          MD5Update(&md5c, ((unsigned char *) jcr->big_buf), n);
203          jcr->JobBytes += n;
204       }
205       if (n < 0) {
206          Jmsg(jcr, M_ERROR, -1, _("Error reading file %s: ERR=%s\n"), 
207               ff_pkt->fname, berror(&bfd));
208       }
209       MD5Final(signature, &md5c);
210
211       bin_to_base64(MD5buf, (char *)signature, 16); /* encode 16 bytes */
212       Dmsg2(400, "send inx=%d MD5=%s\n", jcr->JobFiles, MD5buf);
213       bnet_fsend(dir, "%d %d %s *MD5-%d*", jcr->JobFiles, STREAM_MD5_SIGNATURE, MD5buf,
214          jcr->JobFiles);
215       Dmsg2(20, "bfiled>bdird: MD5 len=%d: msg=%s\n", dir->msglen, dir->msg);
216    } else if (is_bopen(&bfd) && ff_pkt->flags & FO_SHA1) {
217       char SHA1buf[40];               /* 24 should do */
218       SHA1Init(&sha1c);
219       while ((n=bread(&bfd, jcr->big_buf, jcr->buf_size)) > 0) {
220          SHA1Update(&sha1c, ((unsigned char *) jcr->big_buf), n);
221          jcr->JobBytes += n;
222       }
223       if (n < 0) {
224          Jmsg(jcr, M_ERROR, -1, _("Error reading file %s: ERR=%s\n"), 
225               ff_pkt->fname, berror(&bfd));
226       }
227       SHA1Final(&sha1c, signature);
228
229       bin_to_base64(SHA1buf, (char *)signature, 20); /* encode 20 bytes */
230       Dmsg2(400, "send inx=%d SHA1=%s\n", jcr->JobFiles, SHA1buf);
231       bnet_fsend(dir, "%d %d %s *SHA1-%d*", jcr->JobFiles, STREAM_SHA1_SIGNATURE, 
232          SHA1buf, jcr->JobFiles);
233       Dmsg2(20, "bfiled>bdird: SHA1 len=%d: msg=%s\n", dir->msglen, dir->msg);
234    }
235    if (is_bopen(&bfd)) {
236       bclose(&bfd);
237    }
238    return 1;
239 }