]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/verify_vol.c
839874e633b81ddffd394d30afe4804becd8b04c
[bacula/bacula] / bacula / src / filed / verify_vol.c
1 /*
2  *  Bacula File Daemon  verify-vol.c Verify files on a Volume
3  *    versus attributes in Catalog
4  *
5  *    Kern Sibbald, July MMII
6  *
7  *   Version $Id$
8  *
9  */
10 /*
11    Copyright (C) 2000-2003 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"
31 #include "filed.h"
32
33 /* Data received from Storage Daemon */
34 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
35
36 /* Forward referenced functions */
37 #ifdef needed
38 static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp);
39 #endif
40
41
42 /* 
43  * Verify attributes of the requested files on the Volume
44  * 
45  */
46 void do_verify_volume(JCR *jcr)
47 {
48    BSOCK *sd, *dir;
49    POOLMEM *fname;                    /* original file name */
50    POOLMEM *lname;                    /* link name */
51    int32_t stream;
52    uint32_t size;
53    uint32_t VolSessionId, VolSessionTime, file_index;
54    uint32_t record_file_index;
55    int type, stat;
56    
57    sd = jcr->store_bsock;
58    if (!sd) {
59       Jmsg(jcr, M_FATAL, 0, _("Storage command not issued before Verify.\n"));
60       set_jcr_job_status(jcr, JS_FatalError);
61       return;
62    }
63    dir = jcr->dir_bsock;
64    set_jcr_job_status(jcr, JS_Running);
65
66    if (!bnet_set_buffer_size(sd, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_READ)) {
67       set_jcr_job_status(jcr, JS_FatalError);
68       return;
69    }
70    jcr->buf_size = sd->msglen;
71
72    fname = get_pool_memory(PM_FNAME);
73    lname = get_pool_memory(PM_FNAME);
74
75    /* 
76     * Get a record from the Storage daemon
77     */
78    while (bget_msg(sd) >= 0 && !job_canceled(jcr)) {
79       /*
80        * First we expect a Stream Record Header 
81        */
82       if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
83           &stream, &size) != 5) {
84          Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
85          goto bail_out;
86       }
87       Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
88
89       /* 
90        * Now we expect the Stream Data
91        */
92       if (bget_msg(sd) < 0) {
93          Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
94          goto bail_out;
95       }
96       if (size != ((uint32_t)sd->msglen)) {
97          Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
98          goto bail_out;
99       }
100       Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
101
102       /* File Attributes stream */
103       switch (stream) {
104       case STREAM_UNIX_ATTRIBUTES:
105       case STREAM_WIN32_ATTRIBUTES:
106          char *ap, *lp, *fp;
107
108          Dmsg0(400, "Stream=Unix Attributes.\n");
109
110          if ((int)sizeof_pool_memory(fname) < sd->msglen) {
111             fname = realloc_pool_memory(fname, sd->msglen + 1);
112          }
113
114          if ((int)sizeof_pool_memory(lname) < sd->msglen) {
115             lname = realloc_pool_memory(lname, sd->msglen + 1);
116          }
117          *fname = 0;
118          *lname = 0;
119
120          /*              
121           * An Attributes record consists of:
122           *    File_index
123           *    Type   (FT_types)
124           *    Filename
125           *    Attributes
126           *    Link name (if file linked i.e. FT_LNK)
127           *    Extended Attributes (if Win32)
128           */
129          if (sscanf(sd->msg, "%d %d", &record_file_index, &type) != 2) {
130             Jmsg(jcr, M_FATAL, 0, _("Error scanning record header: %s\n"), sd->msg);
131             Dmsg0(0, "\nError scanning header\n");
132             goto bail_out;
133          }
134          Dmsg2(30, "Got Attr: FilInx=%d type=%d\n", record_file_index, type);
135          if (record_file_index != file_index) {
136             Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
137                file_index, record_file_index);
138             Dmsg0(0, "File index error\n");
139             goto bail_out;
140          }
141          ap = sd->msg;
142          while (*ap++ != ' ')         /* skip record file index */
143             ;
144          while (*ap++ != ' ')         /* skip type */
145             ;
146          /* Save filename and position to attributes */
147          fp = fname;     
148          while (*ap != 0) {
149             *fp++  = *ap++;           /* copy filename to fname */
150          }
151          *fp = *ap++;                 /* terminate filename & point to attribs */
152
153          Dmsg1(200, "Attr=%s\n", ap);
154          /* Skip to Link name */
155          if (type == FT_LNK || type == FT_LNKSAVED) {
156             lp = ap;
157             while (*lp++ != 0) {
158                ;
159             }
160             strcat(lname, lp);        /* "save" link name */
161          } else {
162             *lname = 0;
163          }
164          P(jcr->mutex);
165          jcr->JobFiles++;
166          jcr->num_files_examined++;
167          pm_strcpy(&jcr->last_fname, fname); /* last file examined */
168          V(jcr->mutex);
169
170          /* 
171           * Send file attributes to Director
172           *   File_index
173           *   Stream
174           *   Verify Options
175           *   Filename (full path)
176           *   Encoded attributes
177           *   Link name (if type==FT_LNK)
178           * For a directory, link is the same as fname, but with trailing
179           * slash. For a linked file, link is the link.
180           */
181          /* Send file attributes to Director */
182          Dmsg2(200, "send ATTR inx=%d fname=%s\n", jcr->JobFiles, fname);
183          if (type == FT_LNK || type == FT_LNKSAVED) {
184             stat = bnet_fsend(dir, "%d %d %s %s%c%s%c%s%c", jcr->JobFiles,
185                           STREAM_UNIX_ATTRIBUTES, "pinsug5", fname, 
186                           0, ap, 0, lname, 0);
187          } else {
188             stat = bnet_fsend(dir,"%d %d %s %s%c%s%c%c", jcr->JobFiles,
189                           STREAM_UNIX_ATTRIBUTES, "pinsug5", fname, 
190                           0, ap, 0, 0);
191          }
192          Dmsg2(200, "bfiled>bdird: attribs len=%d: msg=%s\n", dir->msglen, dir->msg);
193          if (!stat) {
194             Jmsg(jcr, M_FATAL, 0, _("Network error in send to Director: ERR=%s\n"), bnet_strerror(dir));
195             goto bail_out;
196          }
197          break;
198
199       /* Data streams to ignore */
200       case STREAM_FILE_DATA:
201       case STREAM_SPARSE_DATA:
202       case STREAM_WIN32_DATA:
203       case STREAM_WIN32_GZIP_DATA:
204       case STREAM_GZIP_DATA:
205       case STREAM_SPARSE_GZIP_DATA:
206
207         /* Do nothing */
208         break;
209
210       case STREAM_MD5_SIGNATURE:
211          char MD5buf[30];
212          bin_to_base64(MD5buf, (char *)sd->msg, 16); /* encode 16 bytes */
213          Dmsg2(400, "send inx=%d MD5=%s\n", jcr->JobFiles, MD5buf);
214          bnet_fsend(dir, "%d %d %s *MD5-%d*", jcr->JobFiles, STREAM_MD5_SIGNATURE, MD5buf,
215             jcr->JobFiles);
216          Dmsg2(20, "bfiled>bdird: MD5 len=%d: msg=%s\n", dir->msglen, dir->msg);
217          break;
218   
219       case STREAM_SHA1_SIGNATURE:
220          char SHA1buf[30];
221          bin_to_base64(SHA1buf, (char *)sd->msg, 20); /* encode 20 bytes */
222          Dmsg2(400, "send inx=%d SHA1=%s\n", jcr->JobFiles, SHA1buf);
223          bnet_fsend(dir, "%d %d %s *SHA1-%d*", jcr->JobFiles, STREAM_SHA1_SIGNATURE, 
224             SHA1buf, jcr->JobFiles);
225          Dmsg2(20, "bfiled>bdird: SHA1 len=%d: msg=%s\n", dir->msglen, dir->msg);
226          break;
227
228       default:
229          Pmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
230          break;
231       } /* end switch */
232    } /* end while bnet_get */
233    set_jcr_job_status(jcr, JS_Terminated);
234    goto ok_out;
235
236 bail_out:
237    set_jcr_job_status(jcr, JS_ErrorTerminated);
238
239 ok_out:
240    if (jcr->compress_buf) {
241       free(jcr->compress_buf);
242       jcr->compress_buf = NULL;
243    }
244    free_pool_memory(fname);
245    free_pool_memory(lname);
246    Dmsg2(050, "End Verify-Vol. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
247       jcr->JobBytes);
248 }          
249
250 extern char *getuser(uid_t uid);
251 extern char *getgroup(gid_t gid);
252
253 /*
254  * Print an ls style message, also send INFO
255  */
256 #ifdef needed
257 static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp)
258 {
259    char buf[2000]; 
260    char ec1[30];
261    char *p, *f;
262    int n;
263
264    p = encode_mode(statp->st_mode, buf);
265    n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
266    p += n;
267    n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
268    p += n;
269    n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
270    p += n;
271    p = encode_time(statp->st_ctime, p);
272    *p++ = ' ';
273    *p++ = ' ';
274    for (f=fname; *f && (p-buf) < (int)sizeof(buf)-10; )
275       *p++ = *f++;
276    if (type == FT_LNK) {
277       *p++ = ' ';
278       *p++ = '-';
279       *p++ = '>';
280       *p++ = ' ';
281       /* Copy link name */
282       for (f=lname; *f && (p-buf) < (int)sizeof(buf)-10; )
283          *p++ = *f++;
284    }
285    *p++ = '\n';
286    *p = 0;
287    Dmsg0(20, buf);
288    Jmsg(jcr, M_INFO, 0, "%s", buf);
289 }
290 #endif