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