]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/accurate.c
ebl Cleanup function prototype
[bacula/bacula] / bacula / src / filed / accurate.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2008 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 John Walker.
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 typedef struct PrivateCurFile {
37 #ifndef USE_TCHDB
38    hlink link;
39 #endif
40    char *fname;                 /* not stored with tchdb mode */
41    time_t ctime;
42    time_t mtime;
43    bool seen;
44 } CurFile;
45
46 bool accurate_add_file(JCR *jcr, char *fname, char *lstat)
47 {
48    CurFile elt;
49    struct stat statp;
50    int LinkFIc;
51    decode_stat(lstat, &statp, &LinkFIc); /* decode catalog stat */
52    elt.ctime = statp.st_ctime;
53    elt.mtime = statp.st_mtime;
54    elt.seen = 0;
55
56 #ifdef USE_TCHDB
57    if (!tchdbputasync(jcr->file_list,
58                       fname, strlen(fname)+1,
59                       &elt, sizeof(CurFile)))
60    {
61       /* TODO: check error */
62    }
63 #else  /* HTABLE */
64    CurFile *item;
65    /* we store CurFile, fname and ctime/mtime in the same chunk */
66    item = (CurFile *)jcr->file_list->hash_malloc(sizeof(CurFile)+strlen(fname)+1);
67    memcpy(item, &elt, sizeof(CurFile));
68    item->fname  = (char *)item+sizeof(CurFile);
69    strcpy(item->fname, fname);
70    jcr->file_list->insert(item->fname, item); 
71 #endif
72
73    Dmsg2(500, "add fname=%s lstat=%s\n", fname, lstat);
74    return true;
75 }
76
77 bool accurate_mark_file_as_seen(JCR *jcr, CurFile *elt)
78 {
79    bool ret=true;
80
81 #ifdef USE_TCHDB
82    elt->seen = 1;
83    if (!tchdbputasync(jcr->file_list, 
84                       elt->fname, strlen(elt->fname)+1, 
85                       elt, sizeof(CurFile)))
86    {
87       ret = false;              /* TODO: add error message */
88    }
89 #else
90    CurFile *temp = (CurFile *)jcr->file_list->lookup(elt->fname);
91    temp->seen = 1;
92 #endif    
93    return ret;
94 }
95
96 bool accurate_lookup(JCR *jcr, char *fname, CurFile *ret)
97 {
98    bool found=false;
99    ret->seen = 0;
100
101 #ifdef USE_TCHDB
102    if (tchdbget3(jcr->file_list, 
103                  fname, strlen(fname)+1, 
104                  ret, sizeof(CurFile)) != -1)
105    {
106       found = true;
107       ret->fname = fname;
108    }
109
110 #else  /* HTABLE */
111    CurFile *temp = (CurFile *)jcr->file_list->lookup(fname);
112    if (temp) {
113       memcpy(ret, temp, sizeof(CurFile));
114       found=true;
115    }
116 #endif
117
118    return found;
119 }
120
121 bool accurate_init(JCR *jcr, int nbfile)
122 {
123 #ifdef USE_TCHDB
124    jcr->file_list = tchdbnew();
125    tchdbsetcache(jcr->file_list, 300000);
126    tchdbtune(jcr->file_list,
127              nbfile,            /* nb bucket 0.5n to 4n */
128              7,                 /* size of element 2^x */
129              16,
130              0);                /* options like compression */
131    /* TODO: make accurate file unique */
132    if(!tchdbopen(jcr->file_list, "/tmp/casket.hdb", HDBOWRITER | HDBOCREAT)){
133       /* TODO: handle error creation */
134       //ecode = tchdbecode(hdb);
135       //fprintf(stderr, "open error: %s\n", tchdberrmsg(ecode));
136    }
137
138 #else  /* HTABLE */
139    CurFile *elt=NULL;
140    jcr->file_list = (htable *)malloc(sizeof(htable));
141    jcr->file_list->init(elt, &elt->link, nbfile);
142 #endif
143
144    return true;
145 }
146
147 /*
148  * This function is called for each file seen in fileset.
149  * We check in file_list hash if fname have been backuped
150  * the last time. After we can compare Lstat field. 
151  * Full Lstat usage have been removed on 6612 
152  */
153 bool accurate_check_file(JCR *jcr, FF_PKT *ff_pkt)
154 {
155    bool stat = false;
156    char *fname;
157    CurFile elt;
158
159    if (!jcr->accurate || jcr->JobLevel == L_FULL) {
160       return true;
161    }
162
163    strip_path(ff_pkt);
164  
165    if (S_ISDIR(ff_pkt->statp.st_mode)) {
166       fname = ff_pkt->link;
167    } else {
168       fname = ff_pkt->fname;
169    } 
170
171    if (!accurate_lookup(jcr, fname, &elt)) {
172       Dmsg1(2, "accurate %s (not found)\n", fname);
173       stat = true;
174       goto bail_out;
175    }
176
177    if (elt.seen) { /* file has been seen ? */
178       Dmsg1(2, "accurate %s (already seen)\n", fname);
179       goto bail_out;
180    }
181
182    if (elt.mtime != ff_pkt->statp.st_mtime) {
183      Jmsg(jcr, M_SAVED, 0, _("%s      st_mtime differs\n"), fname);
184      stat = true;
185    } else if (elt.ctime != ff_pkt->statp.st_ctime) {
186      Jmsg(jcr, M_SAVED, 0, _("%s      st_ctime differs\n"), fname);
187      stat = true;
188    }
189
190    if (stat) {
191       Dmsg4(1, "%i = %i\t%i = %i\n", elt.mtime, ff_pkt->statp.st_mtime,
192             elt.ctime, ff_pkt->statp.st_ctime);
193    }
194
195    accurate_mark_file_as_seen(jcr, &elt);
196    Dmsg2(500, "accurate %s = %i\n", fname, stat);
197
198 bail_out:
199    unstrip_path(ff_pkt);
200    return stat;
201 }
202
203 /* 
204  * TODO: use bigbuffer from htable
205  */
206 int accurate_cmd(JCR *jcr)
207 {
208    BSOCK *dir = jcr->dir_bsock;
209    int len;
210    int32_t nb;
211
212    if (!jcr->accurate || job_canceled(jcr) || jcr->JobLevel==L_FULL) {
213       return true;
214    }
215
216    if (sscanf(dir->msg, "accurate files=%ld", &nb) != 1) {
217       dir->fsend(_("2991 Bad accurate command\n"));
218       return false;
219    }
220    Dmsg2(2, "nb=%d msg=%s\n", nb, dir->msg);
221
222    accurate_init(jcr, nb);
223
224    /*
225     * buffer = sizeof(CurFile) + dirmsg
226     * dirmsg = fname + \0 + lstat
227     */
228    /* get current files */
229    while (dir->recv() >= 0) {
230       Dmsg1(2, "accurate_cmd fname=%s\n", dir->msg);
231       len = strlen(dir->msg) + 1;
232       if (len < dir->msglen) {
233          accurate_add_file(jcr, dir->msg, dir->msg + len);
234       }
235    }
236
237 #ifdef DEBUG
238    extern void *start_heap;
239
240    char b1[50], b2[50], b3[50], b4[50], b5[50];
241    Dmsg5(1," Heap: heap=%s smbytes=%s max_bytes=%s bufs=%s max_bufs=%s\n",
242          edit_uint64_with_commas((char *)sbrk(0)-(char *)start_heap, b1),
243          edit_uint64_with_commas(sm_bytes, b2),
244          edit_uint64_with_commas(sm_max_bytes, b3),
245          edit_uint64_with_commas(sm_buffers, b4),
246          edit_uint64_with_commas(sm_max_buffers, b5));
247
248 #endif
249
250    return true;
251 }
252
253 bool accurate_send_deleted_list(JCR *jcr)
254 {
255    CurFile *elt;
256    FF_PKT *ff_pkt;
257    int stream = STREAM_UNIX_ATTRIBUTES;
258
259    if (!jcr->accurate || jcr->JobLevel == L_FULL) {
260       goto bail_out;
261    }
262
263    if (jcr->file_list == NULL) {
264       goto bail_out;
265    }
266
267    ff_pkt = init_find_files();
268    ff_pkt->type = FT_DELETED;
269
270 #ifdef USE_TCHDB
271    char *key;
272    CurFile item;
273    elt = &item;
274   /* traverse records */
275    tchdbiterinit(jcr->file_list);
276    while((key = tchdbiternext2(jcr->file_list)) != NULL){
277       tchdbget3(jcr->file_list, key, strlen(key), elt, sizeof(CurFile));
278       ff_pkt->fname = key;
279       ff_pkt->statp.st_mtime = elt->mtime;
280       ff_pkt->statp.st_ctime = elt->ctime;
281       encode_and_send_attributes(jcr, ff_pkt, stream);
282 //      free(key);
283    }
284 #else
285    foreach_htable (elt, jcr->file_list) {
286       if (!elt->seen) { /* already seen */
287          Dmsg2(1, "deleted fname=%s seen=%i\n", elt->fname, elt->seen);
288          ff_pkt->fname = elt->fname;
289          ff_pkt->statp.st_mtime = elt->mtime;
290          ff_pkt->statp.st_ctime = elt->ctime;
291          encode_and_send_attributes(jcr, ff_pkt, stream);
292       }
293 //      free(elt->fname);
294    }
295 #endif
296
297    term_find_files(ff_pkt);
298 bail_out:
299    /* TODO: clean htable when this function is not reached ? */
300    if (jcr->file_list) {
301 #ifdef USE_TCHDB
302       if(!tchdbclose(jcr->file_list)){
303 //       ecode = tchdbecode(hdb);
304 //       fprintf(stderr, "close error: %s\n", tchdberrmsg(ecode));
305       }
306
307       /* delete the object */
308       tchdbdel(jcr->file_list);
309       unlink("/tmp/casket.hdb");
310 #else
311       jcr->file_list->destroy();
312       free(jcr->file_list);
313 #endif
314       jcr->file_list = NULL;
315    }
316    return true;
317 }