]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/accurate.c
tweak debug
[bacula/bacula] / bacula / src / filed / accurate.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *  Version $Id $
30  *
31  */
32
33 #include "bacula.h"
34 #include "filed.h"
35
36 static int dbglvl=100;
37
38 typedef struct PrivateCurFile {
39    hlink link;
40    char *fname;
41    utime_t ctime;               /* can be replaced by struct stat */
42    utime_t mtime;
43    bool seen;
44 } CurFile;
45
46 bool accurate_mark_file_as_seen(JCR *jcr, char *fname)
47 {
48    if (!jcr->accurate || !jcr->file_list) {
49       return false;
50    }
51    /* TODO: just use elt->seen = 1 */
52    CurFile *temp = (CurFile *)jcr->file_list->lookup(fname);
53    if (temp) {
54       temp->seen = 1;              /* records are in memory */
55       Dmsg1(dbglvl, "marked <%s> as seen\n", fname);
56    } else {
57       Dmsg1(dbglvl, "<%s> not found to be marked as seen\n", fname);
58    }
59    return true;
60 }
61
62 static bool accurate_mark_file_as_seen(JCR *jcr, CurFile *elt)
63 {
64    /* TODO: just use elt->seen = 1 */
65    CurFile *temp = (CurFile *)jcr->file_list->lookup(elt->fname);
66    if (temp) {
67       temp->seen = 1;              /* records are in memory */
68    }
69    return true;
70 }
71
72 static bool accurate_lookup(JCR *jcr, char *fname, CurFile *ret)
73 {
74    bool found=false;
75    ret->seen = 0;
76
77    CurFile *temp = (CurFile *)jcr->file_list->lookup(fname);
78    if (temp) {
79       memcpy(ret, temp, sizeof(CurFile));
80       found=true;
81       Dmsg1(dbglvl, "lookup <%s> ok\n", fname);
82    }
83
84    return found;
85 }
86
87 static bool accurate_init(JCR *jcr, int nbfile)
88 {
89    CurFile *elt = NULL;
90    jcr->file_list = (htable *)malloc(sizeof(htable));
91    jcr->file_list->init(elt, &elt->link, nbfile);
92    return true;
93 }
94
95 static bool accurate_send_base_file_list(JCR *jcr)
96 {
97    CurFile *elt;
98    FF_PKT *ff_pkt;
99    int stream = STREAM_UNIX_ATTRIBUTES;
100
101    if (!jcr->accurate || jcr->get_JobLevel() != L_FULL) {
102       return true;
103    }
104
105    if (jcr->file_list == NULL) {
106       return true;
107    }
108
109    ff_pkt = init_find_files();
110    ff_pkt->type = FT_BASE;
111
112    foreach_htable(elt, jcr->file_list) {
113       if (elt->seen) {
114          Dmsg2(dbglvl, "base file fname=%s seen=%i\n", elt->fname, elt->seen);
115          ff_pkt->fname = elt->fname;
116          ff_pkt->statp.st_mtime = elt->mtime;
117          ff_pkt->statp.st_ctime = elt->ctime;
118          encode_and_send_attributes(jcr, ff_pkt, stream);
119 //       free(elt->fname);
120       }
121    }
122
123    term_find_files(ff_pkt);
124    return true;
125 }
126
127
128 /* This function is called at the end of backup
129  * We walk over all hash disk element, and we check
130  * for elt.seen.
131  */
132 static bool accurate_send_deleted_list(JCR *jcr)
133 {
134    CurFile *elt;
135    FF_PKT *ff_pkt;
136    int stream = STREAM_UNIX_ATTRIBUTES;
137
138    if (!jcr->accurate) {
139       return true;
140    }
141
142    if (jcr->file_list == NULL) {
143       return true;
144    }
145
146    ff_pkt = init_find_files();
147    ff_pkt->type = FT_DELETED;
148
149    foreach_htable(elt, jcr->file_list) {
150       if (elt->seen || plugin_check_file(jcr, elt->fname)) {
151          continue;
152       }
153       Dmsg2(dbglvl, "deleted fname=%s seen=%i\n", elt->fname, elt->seen);
154       ff_pkt->fname = elt->fname;
155       ff_pkt->statp.st_mtime = elt->mtime;
156       ff_pkt->statp.st_ctime = elt->ctime;
157       encode_and_send_attributes(jcr, ff_pkt, stream);
158 //    free(elt->fname);
159    }
160
161    term_find_files(ff_pkt);
162    return true;
163 }
164
165 void accurate_free(JCR *jcr)
166 {
167    if (jcr->file_list) {
168       jcr->file_list->destroy();
169       free(jcr->file_list);
170       jcr->file_list = NULL;
171    }
172 }
173
174 /* Send the deleted or the base file list and cleanup  */
175 bool accurate_finish(JCR *jcr)
176 {
177    bool ret=true;
178    if (jcr->accurate) {
179       if (jcr->get_JobLevel() == L_FULL) {
180          ret = accurate_send_base_file_list(jcr);
181       } else {
182          ret = accurate_send_deleted_list(jcr);
183       }
184       
185       accurate_free(jcr);
186    } 
187    return ret;
188 }
189
190 static bool accurate_add_file(JCR *jcr, char *fname, char *lstat)
191 {
192    bool ret = true;
193    CurFile elt;
194    struct stat statp;
195    int32_t LinkFIc;
196    decode_stat(lstat, &statp, &LinkFIc); /* decode catalog stat */
197    elt.ctime = statp.st_ctime;
198    elt.mtime = statp.st_mtime;
199    elt.seen = 0;
200
201    CurFile *item;
202    /* we store CurFile, fname and ctime/mtime in the same chunk */
203    item = (CurFile *)jcr->file_list->hash_malloc(sizeof(CurFile)+strlen(fname)+1);
204    memcpy(item, &elt, sizeof(CurFile));
205    item->fname  = (char *)item+sizeof(CurFile);
206    strcpy(item->fname, fname);
207    jcr->file_list->insert(item->fname, item); 
208
209    Dmsg2(dbglvl, "add fname=<%s> lstat=%s\n", fname, lstat);
210    return ret;
211 }
212
213 /*
214  * This function is called for each file seen in fileset.
215  * We check in file_list hash if fname have been backuped
216  * the last time. After we can compare Lstat field. 
217  * Full Lstat usage have been removed on 6612 
218  *
219  * Returns: true   if file has changed (must be backed up)
220  *          false  file not changed
221  */
222 bool accurate_check_file(JCR *jcr, FF_PKT *ff_pkt)
223 {
224    bool stat = false;
225    char *fname;
226    CurFile elt;
227
228    if (!jcr->accurate) {
229       return true;
230    }
231
232    strip_path(ff_pkt);
233  
234    if (S_ISDIR(ff_pkt->statp.st_mode)) {
235       fname = ff_pkt->link;
236    } else {
237       fname = ff_pkt->fname;
238    } 
239
240    if (!accurate_lookup(jcr, fname, &elt)) {
241       Dmsg1(dbglvl, "accurate %s (not found)\n", fname);
242       stat = true;
243       goto bail_out;
244    }
245
246    if (elt.seen) { /* file has been seen ? */
247       Dmsg1(dbglvl, "accurate %s (already seen)\n", fname);
248       goto bail_out;
249    }
250
251 #if 0
252    /*
253     * Loop over options supplied by user and verify the
254     * fields he requests.
255     */
256    for (p=Opts_Digest; *p; p++) {
257       char ed1[30], ed2[30];
258       switch (*p) {
259       case 'i':                /* compare INODEs */
260          if (statc.st_ino != statf.st_ino) {
261             prt_fname(jcr);
262             Jmsg(jcr, M_INFO, 0, _("      st_ino   differ. Cat: %s File: %s\n"),
263                  edit_uint64((uint64_t)statc.st_ino, ed1),
264                  edit_uint64((uint64_t)statf.st_ino, ed2));
265             set_jcr_job_status(jcr, JS_Differences);
266          }
267          break;
268       case 'p':                /* permissions bits */
269          if (statc.st_mode != statf.st_mode) {
270             prt_fname(jcr);
271             Jmsg(jcr, M_INFO, 0, _("      st_mode  differ. Cat: %x File: %x\n"),
272                  (uint32_t)statc.st_mode, (uint32_t)statf.st_mode);
273             set_jcr_job_status(jcr, JS_Differences);
274          }
275          break;
276       case 'n':                /* number of links */
277          if (statc.st_nlink != statf.st_nlink) {
278             prt_fname(jcr);
279             Jmsg(jcr, M_INFO, 0, _("      st_nlink differ. Cat: %d File: %d\n"),
280                  (uint32_t)statc.st_nlink, (uint32_t)statf.st_nlink);
281             set_jcr_job_status(jcr, JS_Differences);
282          }
283          break;
284       case 'u':                /* user id */
285          if (statc.st_uid != statf.st_uid) {
286             prt_fname(jcr);
287             Jmsg(jcr, M_INFO, 0, _("      st_uid   differ. Cat: %u File: %u\n"),
288                  (uint32_t)statc.st_uid, (uint32_t)statf.st_uid);
289             set_jcr_job_status(jcr, JS_Differences);
290          }
291          break;
292       case 'g':                /* group id */
293          if (statc.st_gid != statf.st_gid) {
294             prt_fname(jcr);
295             Jmsg(jcr, M_INFO, 0, _("      st_gid   differ. Cat: %u File: %u\n"),
296                  (uint32_t)statc.st_gid, (uint32_t)statf.st_gid);
297             set_jcr_job_status(jcr, JS_Differences);
298          }
299          break;
300       case 's':                /* size */
301          if (statc.st_size != statf.st_size) {
302             prt_fname(jcr);
303             Jmsg(jcr, M_INFO, 0, _("      st_size  differ. Cat: %s File: %s\n"),
304                  edit_uint64((uint64_t)statc.st_size, ed1),
305                  edit_uint64((uint64_t)statf.st_size, ed2));
306             set_jcr_job_status(jcr, JS_Differences);
307          }
308          break;
309       case 'a':                /* access time */
310          if (statc.st_atime != statf.st_atime) {
311             prt_fname(jcr);
312             Jmsg(jcr, M_INFO, 0, _("      st_atime differs\n"));
313             set_jcr_job_status(jcr, JS_Differences);
314          }
315          break;
316       case 'm':
317          if (statc.st_mtime != statf.st_mtime) {
318             prt_fname(jcr);
319             Jmsg(jcr, M_INFO, 0, _("      st_mtime differs\n"));
320             set_jcr_job_status(jcr, JS_Differences);
321          }
322          break;
323       case 'c':                /* ctime */
324          if (statc.st_ctime != statf.st_ctime) {
325             prt_fname(jcr);
326             Jmsg(jcr, M_INFO, 0, _("      st_ctime differs\n"));
327             set_jcr_job_status(jcr, JS_Differences);
328          }
329          break;
330       case 'd':                /* file size decrease */
331          if (statc.st_size > statf.st_size) {
332             prt_fname(jcr);
333             Jmsg(jcr, M_INFO, 0, _("      st_size  decrease. Cat: %s File: %s\n"),
334                  edit_uint64((uint64_t)statc.st_size, ed1),
335                  edit_uint64((uint64_t)statf.st_size, ed2));
336             set_jcr_job_status(jcr, JS_Differences);
337          }
338          break;
339       case '5':                /* compare MD5 */
340          Dmsg1(500, "set Do_MD5 for %s\n", jcr->fname);
341          do_Digest = CRYPTO_DIGEST_MD5;
342          break;
343       case '1':                 /* compare SHA1 */
344          do_Digest = CRYPTO_DIGEST_SHA1;
345                break;
346       case ':':
347       case 'V':
348       default:
349          break;
350             }
351    }
352 #endif
353    /*
354     * We check only mtime/ctime like with the normal
355     * incremental/differential mode
356     */
357    if (elt.mtime != ff_pkt->statp.st_mtime) {
358 //   Jmsg(jcr, M_SAVED, 0, _("%s      st_mtime differs\n"), fname);
359       Dmsg3(dbglvl, "%s      st_mtime differs (%lld!=%lld)\n", 
360             fname, elt.mtime, (utime_t)ff_pkt->statp.st_mtime);
361      stat = true;
362    } else if (!(ff_pkt->flags & FO_MTIMEONLY) 
363               && (elt.ctime != ff_pkt->statp.st_ctime)) {
364 //   Jmsg(jcr, M_SAVED, 0, _("%s      st_ctime differs\n"), fname);
365       Dmsg1(dbglvl, "%s      st_ctime differs\n", fname);
366       stat = true;
367    }
368
369    accurate_mark_file_as_seen(jcr, &elt);
370 //   Dmsg2(dbglvl, "accurate %s = %d\n", fname, stat);
371
372 bail_out:
373    unstrip_path(ff_pkt);
374    return stat;
375 }
376
377 /* 
378  * TODO: use big buffer from htable
379  */
380 int accurate_cmd(JCR *jcr)
381 {
382    BSOCK *dir = jcr->dir_bsock;
383    int len;
384    int32_t nb;
385
386    if (job_canceled(jcr)) {
387       return true;
388    }
389    if (sscanf(dir->msg, "accurate files=%ld", &nb) != 1) {
390       dir->fsend(_("2991 Bad accurate command\n"));
391       return false;
392    }
393
394    jcr->accurate = true;
395
396    accurate_init(jcr, nb);
397
398    /*
399     * buffer = sizeof(CurFile) + dirmsg
400     * dirmsg = fname + \0 + lstat
401     */
402    /* get current files */
403    while (dir->recv() >= 0) {
404       len = strlen(dir->msg) + 1;
405       if (len < dir->msglen) {
406          accurate_add_file(jcr, dir->msg, dir->msg + len);
407       }
408    }
409
410 #ifdef DEBUG
411    extern void *start_heap;
412
413    char b1[50], b2[50], b3[50], b4[50], b5[50];
414    Dmsg5(dbglvl," Heap: heap=%s smbytes=%s max_bytes=%s bufs=%s max_bufs=%s\n",
415          edit_uint64_with_commas((char *)sbrk(0)-(char *)start_heap, b1),
416          edit_uint64_with_commas(sm_bytes, b2),
417          edit_uint64_with_commas(sm_max_bytes, b3),
418          edit_uint64_with_commas(sm_buffers, b4),
419          edit_uint64_with_commas(sm_max_buffers, b5));
420 #endif
421
422    return true;
423 }