]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/verify.c
7deceb5f492150a4fbe6036268f481223dc9aca9
[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-2004 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 #ifdef HAVE_DARWIN_OS
33 #include <sys/paths.h>
34 #endif
35
36 static int verify_file(FF_PKT *ff_pkt, void *my_pkt);
37
38 /* 
39  * Find all the requested files and send attributes
40  * to the Director.
41  * 
42  */
43 void do_verify(JCR *jcr)
44 {
45    set_jcr_job_status(jcr, JS_Running);
46    jcr->buf_size = DEFAULT_NETWORK_BUFFER_SIZE;
47    if ((jcr->big_buf = (char *) malloc(jcr->buf_size)) == NULL) {
48       Jmsg1(jcr, M_ABORT, 0, _("Cannot malloc %d network read buffer\n"), 
49          DEFAULT_NETWORK_BUFFER_SIZE);
50    }
51    set_find_options((FF_PKT *)jcr->ff, jcr->incremental, jcr->mtime);
52    Dmsg0(10, "Start find files\n");
53    /* Subroutine verify_file() is called for each file */
54    find_files(jcr, (FF_PKT *)jcr->ff, verify_file, (void *)jcr);  
55    Dmsg0(10, "End find files\n");
56
57    if (jcr->big_buf) {
58       free(jcr->big_buf);
59       jcr->big_buf = NULL;
60    }
61    set_jcr_job_status(jcr, JS_Terminated);
62 }          
63
64 /* 
65  * Called here by find() for each file.
66  *
67  *  Find the file, compute the MD5 or SHA1 and send it back to the Director
68  */
69 static int verify_file(FF_PKT *ff_pkt, void *pkt) 
70 {
71    char attribs[MAXSTRING];
72    char attribsEx[MAXSTRING];
73    int64_t n;
74    int stat;
75    BFILE bfd;
76    BFILE rsrc_bfd;
77    bool hfsplus = false;
78    struct MD5Context md5c;
79    struct SHA1Context sha1c;
80    unsigned char signature[25];       /* large enough for either */
81    BSOCK *dir;
82    JCR *jcr = (JCR *)pkt;
83
84    if (job_canceled(jcr)) {
85       return 0;
86    }
87    
88    dir = jcr->dir_bsock;
89    jcr->num_files_examined++;         /* bump total file count */
90
91    switch (ff_pkt->type) {
92    case FT_LNKSAVED:                  /* Hard linked, file already saved */
93       Dmsg2(30, "FT_LNKSAVED saving: %s => %s\n", ff_pkt->fname, ff_pkt->link);
94       break;
95    case FT_REGE:
96       Dmsg1(30, "FT_REGE saving: %s\n", ff_pkt->fname);
97       break;
98    case FT_REG:
99       Dmsg1(30, "FT_REG saving: %s\n", ff_pkt->fname);
100       break;
101    case FT_LNK:
102       Dmsg2(30, "FT_LNK saving: %s -> %s\n", ff_pkt->fname, ff_pkt->link);
103       break;
104    case FT_DIRBEGIN:
105       return 1;                       /* ignored */
106    case FT_DIREND:
107       Dmsg1(30, "FT_DIR saving: %s\n", ff_pkt->fname);
108       break;
109    case FT_SPEC:
110       Dmsg1(30, "FT_SPEC saving: %s\n", ff_pkt->fname);
111       break;
112    case FT_RAW:
113       Dmsg1(30, "FT_RAW saving: %s\n", ff_pkt->fname);
114       break;
115    case FT_FIFO:
116       Dmsg1(30, "FT_FIFO saving: %s\n", ff_pkt->fname);
117       break;
118    case FT_NOACCESS: {
119       berrno be;
120       be.set_errno(ff_pkt->ff_errno);
121       Jmsg(jcr, M_NOTSAVED, 1, _("     Could not access %s: ERR=%s\n"), ff_pkt->fname, be.strerror());
122       jcr->Errors++;
123       return 1;
124    }
125    case FT_NOFOLLOW: {
126       berrno be;
127       be.set_errno(ff_pkt->ff_errno);
128       Jmsg(jcr, M_NOTSAVED, 1, _("     Could not follow link %s: ERR=%s\n"), ff_pkt->fname, be.strerror());
129       jcr->Errors++;
130       return 1;
131    }
132    case FT_NOSTAT: {
133       berrno be;
134       be.set_errno(ff_pkt->ff_errno);
135       Jmsg(jcr, M_NOTSAVED, 1, _("     Could not stat %s: ERR=%s\n"), ff_pkt->fname, be.strerror());
136       jcr->Errors++;
137       return 1;
138    }
139    case FT_DIRNOCHG:
140    case FT_NOCHG:
141       Jmsg(jcr, M_SKIPPED, 1, _("     Unchanged file skipped: %s\n"), ff_pkt->fname);
142       return 1;
143    case FT_ISARCH:
144       Jmsg(jcr, M_SKIPPED, 1, _("     Archive file skipped: %s\n"), ff_pkt->fname);
145       return 1;
146    case FT_NORECURSE:
147       Jmsg(jcr, M_SKIPPED, 1, _("     Recursion turned off. Directory skipped: %s\n"), ff_pkt->fname);
148       return 1;
149    case FT_NOFSCHG:
150       Jmsg(jcr, M_SKIPPED, 1, _("     File system change prohibited. Directory skipped: %s\n"), ff_pkt->fname);
151       return 1;
152    case FT_NOOPEN: {
153       berrno be;
154       be.set_errno(ff_pkt->ff_errno);
155       Jmsg(jcr, M_NOTSAVED, 1, _("     Could not open directory %s: ERR=%s\n"), ff_pkt->fname, be.strerror());
156       jcr->Errors++;
157       return 1;
158    }
159    default:
160       Jmsg(jcr, M_NOTSAVED, 0, _("     Unknown file type %d: %s\n"), ff_pkt->type, ff_pkt->fname);
161       jcr->Errors++;
162       return 1;
163    }
164
165    binit(&bfd);
166
167    if (ff_pkt->type != FT_LNKSAVED && (S_ISREG(ff_pkt->statp.st_mode) && 
168          ff_pkt->statp.st_size > 0) || 
169          ff_pkt->type == FT_RAW || ff_pkt->type == FT_FIFO) {
170       if ((bopen(&bfd, ff_pkt->fname, O_RDONLY | O_BINARY, 0)) < 0) {
171          ff_pkt->ff_errno = errno;
172          berrno be;
173          be.set_errno(bfd.berrno);
174          Jmsg(jcr, M_NOTSAVED, 1, _("     Cannot open %s: ERR=%s.\n"),
175               ff_pkt->fname, be.strerror());
176          jcr->Errors++;
177          return 1;
178       }
179    }
180
181    binit(&rsrc_bfd);          /* we check if this is open below */
182 #ifdef HAVE_DARWIN_OS
183    /* Open resource fork if necessary */
184    if (ff_pkt->type != FT_LNKSAVED && (S_ISREG(ff_pkt->statp.st_mode) &&
185             ff_pkt->flags & FO_HFSPLUS)) {
186       /* Remember Finder Info, whether we have data or fork, or not */
187       hfsplus = true;
188       if (ff_pkt->hfsinfo.rsrclength > 0) {
189          if (bopen_rsrc(&rsrc_bfd, ff_pkt->fname, O_RDONLY | O_BINARY, 0) < 0) {
190             ff_pkt->ff_errno = errno;
191             berrno be;
192             Jmsg(jcr, M_NOTSAVED, -1, _("     Cannot open resource fork for %s: ERR=%s\n"),
193                   ff_pkt->fname, be.strerror());
194             jcr->Errors++;
195             if (is_bopen(&ff_pkt->bfd)) {
196                bclose(&ff_pkt->bfd);
197             }
198             return 1;
199          }
200       }
201    }
202 #endif
203
204       encode_stat(attribs, ff_pkt, 0);
205
206       /* Now possibly extend the attributes */
207       encode_attribsEx(jcr, attribsEx, ff_pkt);
208
209       P(jcr->mutex);
210       jcr->JobFiles++;               /* increment number of files sent */
211       pm_strcpy(jcr->last_fname, ff_pkt->fname);
212       V(jcr->mutex);
213
214       /* 
215        * Send file attributes to Director
216        *        File_index
217        *        Stream
218        *        Verify Options
219        *        Filename (full path)
220        *        Encoded attributes
221        *        Link name (if type==FT_LNK)
222        * For a directory, link is the same as fname, but with trailing
223        * slash. For a linked file, link is the link.
224        */
225       /* Send file attributes to Director (note different format than for Storage) */
226       Dmsg2(400, "send ATTR inx=%d fname=%s\n", jcr->JobFiles, ff_pkt->fname);
227       if (ff_pkt->type == FT_LNK || ff_pkt->type == FT_LNKSAVED) {
228          stat = bnet_fsend(dir, "%d %d %s %s%c%s%c%s%c", jcr->JobFiles,
229                STREAM_UNIX_ATTRIBUTES, ff_pkt->VerifyOpts, ff_pkt->fname, 
230                0, attribs, 0, ff_pkt->link, 0);
231       } else if (ff_pkt->type == FT_DIREND) {
232          /* Here link is the canonical filename (i.e. with trailing slash) */
233          stat = bnet_fsend(dir,"%d %d %s %s%c%s%c%c", jcr->JobFiles,
234                STREAM_UNIX_ATTRIBUTES, ff_pkt->VerifyOpts, ff_pkt->link, 
235                0, attribs, 0, 0);
236       } else {
237          stat = bnet_fsend(dir,"%d %d %s %s%c%s%c%c", jcr->JobFiles,
238                STREAM_UNIX_ATTRIBUTES, ff_pkt->VerifyOpts, ff_pkt->fname, 
239                0, attribs, 0, 0);
240       }
241       Dmsg2(20, "bfiled>bdird: attribs len=%d: msg=%s\n", dir->msglen, dir->msg);
242       if (!stat) {
243          Jmsg(jcr, M_FATAL, 0, _("Network error in send to Director: ERR=%s\n"), bnet_strerror(dir));
244          if (is_bopen(&bfd)) {
245             bclose(&bfd);
246          }
247          if (is_bopen(&rsrc_bfd)) {
248             bclose(&rsrc_bfd);
249          }
250          return 0;
251       }
252
253       /* compute MD5 or SHA1 hash */
254       if ((is_bopen(&bfd) || hfsplus) && ff_pkt->flags & FO_MD5) {
255          char MD5buf[40];                     /* 24 should do */
256          MD5Init(&md5c);
257          if (is_bopen(&bfd)) {
258             while ((n=bread(&bfd, jcr->big_buf, jcr->buf_size)) > 0) {
259                MD5Update(&md5c, ((unsigned char *)jcr->big_buf), (int)n);
260                jcr->JobBytes += n;
261                jcr->ReadBytes += n;
262             }
263             if (n < 0) {
264                berrno be;
265                be.set_errno(bfd.berrno);
266                Jmsg(jcr, M_ERROR, 1, _("Error reading file %s: ERR=%s\n"), 
267                      ff_pkt->fname, be.strerror());
268                jcr->Errors++;
269             }
270          }
271 #ifdef HAVE_DARWIN_OS
272          if (is_bopen(&rsrc_bfd)) {
273             while ((n=bread(&rsrc_bfd, jcr->big_buf, jcr->buf_size)) > 0) {
274                MD5Update(&md5c, ((unsigned char *)jcr->big_buf), (int)n);
275                jcr->JobBytes += n;
276                jcr->ReadBytes += n;
277             }
278             if (n < 0) {
279                berrno be;
280                be.set_errno(rsrc_bfd.berrno);
281                Jmsg(jcr, M_ERROR, 1, _("Error reading file %s: ERR=%s\n"), 
282                      ff_pkt->fname, be.strerror());
283                jcr->Errors++;
284             }
285          }
286          if (ff_pkt->flags & FO_HFSPLUS) {
287             MD5Update(&md5c, ((unsigned char *)ff_pkt->hfsinfo.fndrinfo), 32);
288          }
289 #endif
290          MD5Final(signature, &md5c);
291
292          bin_to_base64(MD5buf, (char *)signature, 16); /* encode 16 bytes */
293          Dmsg2(400, "send inx=%d MD5=%s\n", jcr->JobFiles, MD5buf);
294          bnet_fsend(dir, "%d %d %s *MD5-%d*", jcr->JobFiles, STREAM_MD5_SIGNATURE, MD5buf,
295                jcr->JobFiles);
296          Dmsg2(20, "bfiled>bdird: MD5 len=%d: msg=%s\n", dir->msglen, dir->msg);
297       } else if ((is_bopen(&bfd) || hfsplus) && ff_pkt->flags & FO_SHA1) {
298          char SHA1buf[40];            /* 24 should do */
299          SHA1Init(&sha1c);
300          if (is_bopen(&bfd)) {
301             while ((n=bread(&bfd, jcr->big_buf, jcr->buf_size)) > 0) {
302                SHA1Update(&sha1c, ((unsigned char *)jcr->big_buf), (int)n);
303                jcr->JobBytes += n;
304                jcr->ReadBytes += n;
305             }
306             if (n < 0) {
307                berrno be;
308                be.set_errno(bfd.berrno);
309                Jmsg(jcr, M_ERROR, 1, _("Error reading file %s: ERR=%s\n"), 
310                      ff_pkt->fname, be.strerror());
311                jcr->Errors++;
312             }
313          }
314 #ifdef HAVE_DARWIN_OS
315          if (is_bopen(&rsrc_bfd)) {
316             while ((n=bread(&rsrc_bfd, jcr->big_buf, jcr->buf_size)) > 0) {
317                SHA1Update(&sha1c, ((unsigned char *)jcr->big_buf), (int)n);
318                jcr->JobBytes += n;
319                jcr->ReadBytes += n;
320             }
321             if (n < 0) {
322                berrno be;
323                be.set_errno(rsrc_bfd.berrno);
324                Jmsg(jcr, M_ERROR, 1, _("Error reading file %s: ERR=%s\n"), 
325                      ff_pkt->fname, be.strerror());
326                jcr->Errors++;
327             }
328          }
329          if (ff_pkt->flags & FO_HFSPLUS) {
330             SHA1Update(&sha1c, ((unsigned char *)ff_pkt->hfsinfo.fndrinfo), 32);
331          }
332 #endif
333          SHA1Final(&sha1c, signature);
334
335          bin_to_base64(SHA1buf, (char *)signature, 20); /* encode 20 bytes */
336          Dmsg2(400, "send inx=%d SHA1=%s\n", jcr->JobFiles, SHA1buf);
337          bnet_fsend(dir, "%d %d %s *SHA1-%d*", jcr->JobFiles, STREAM_SHA1_SIGNATURE, 
338                SHA1buf, jcr->JobFiles);
339          Dmsg2(20, "bfiled>bdird: SHA1 len=%d: msg=%s\n", dir->msglen, dir->msg);
340       }
341       if (is_bopen(&bfd)) {
342          bclose(&bfd);
343       }
344       if (is_bopen(&rsrc_bfd)) {
345          bclose(&rsrc_bfd);
346       }
347       return 1;
348    }