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