]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/accurate.c
ebl Cleanup accurate code (remove tcdbm parts) and use red/black
[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 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=200;
37
38 typedef struct PrivateCurFile {
39    rblink link;
40    char *fname;
41    time_t ctime;
42    time_t mtime;
43    bool seen;
44 } CurFile;
45
46 static int my_cmp(void *item1, void *item2)
47 {
48    CurFile *elt1, *elt2;
49    elt1 = (CurFile *) item1;
50    elt2 = (CurFile *) item2;
51    return strcmp(elt1->fname, elt2->fname);
52 }
53
54 static bool accurate_mark_file_as_seen(JCR *jcr, CurFile *elt)
55 {
56    /* TODO: just use elt->seen = 1 */
57    CurFile *temp = (CurFile *)jcr->file_list->search(elt, my_cmp);
58    if (temp) {
59       temp->seen = 1;              /* records are in memory */
60    }
61    return true;
62 }
63
64 static bool accurate_lookup(JCR *jcr, char *fname, CurFile *ret)
65 {
66    bool found=false;
67    ret->seen = 0;
68
69    CurFile search;
70    search.fname = fname;
71    CurFile *temp = (CurFile *)jcr->file_list->search(&search, my_cmp);
72    if (temp) {
73       memcpy(ret, temp, sizeof(CurFile));
74       found=true;
75 //      Dmsg1(dbglvl, "lookup <%s> ok\n", fname);
76    }
77
78    return found;
79 }
80
81 static bool accurate_init(JCR *jcr, int nbfile)
82 {
83    CurFile *elt = NULL;
84    jcr->file_list = New(rblist(elt, &elt->link));
85    return true;
86 }
87
88 /* This function is called at the end of backup
89  * We walk over all hash disk element, and we check
90  * for elt.seen.
91  */
92 bool accurate_send_deleted_list(JCR *jcr)
93 {
94    CurFile *elt;
95    FF_PKT *ff_pkt;
96    int stream = STREAM_UNIX_ATTRIBUTES;
97
98    if (!jcr->accurate || jcr->get_JobLevel() == L_FULL) {
99       goto bail_out;
100    }
101
102    if (jcr->file_list == NULL) {
103       goto bail_out;
104    }
105
106    ff_pkt = init_find_files();
107    ff_pkt->type = FT_DELETED;
108
109    foreach_rblist(elt, jcr->file_list) {
110       if (!elt->seen) { /* already seen */
111 //         Dmsg2(dbglvl, "deleted fname=%s seen=%i\n", elt->fname, elt->seen);
112          ff_pkt->fname = elt->fname;
113          ff_pkt->statp.st_mtime = elt->mtime;
114          ff_pkt->statp.st_ctime = elt->ctime;
115          encode_and_send_attributes(jcr, ff_pkt, stream);
116       }
117 //      free(elt->fname);
118    }
119
120    term_find_files(ff_pkt);
121 bail_out:
122    /* TODO: clean htable when this function is not reached ? */
123    if (jcr->file_list) {
124       delete jcr->file_list;
125       jcr->file_list = NULL;
126    }
127    return true;
128 }
129
130 static bool accurate_add_file(JCR *jcr, char *fname, char *lstat)
131 {
132    bool ret = true;
133    CurFile elt;
134    struct stat statp;
135    int LinkFIc;
136    decode_stat(lstat, &statp, &LinkFIc); /* decode catalog stat */
137    elt.ctime = statp.st_ctime;
138    elt.mtime = statp.st_mtime;
139    elt.seen = 0;
140
141    CurFile *item;
142    /* we store CurFile, fname and ctime/mtime in the same chunk */
143    item = (CurFile *)malloc(sizeof(CurFile)+strlen(fname)+1);
144    memcpy(item, &elt, sizeof(CurFile));
145    item->fname  = (char *)item+sizeof(CurFile);
146    strcpy(item->fname, fname);
147    jcr->file_list->insert(item, my_cmp); 
148
149 //   Dmsg2(dbglvl, "add fname=<%s> lstat=%s\n", fname, lstat);
150    return ret;
151 }
152
153 /*
154  * This function is called for each file seen in fileset.
155  * We check in file_list hash if fname have been backuped
156  * the last time. After we can compare Lstat field. 
157  * Full Lstat usage have been removed on 6612 
158  */
159 bool accurate_check_file(JCR *jcr, FF_PKT *ff_pkt)
160 {
161    bool stat = false;
162    char *fname;
163    CurFile elt;
164
165    if (!jcr->accurate || jcr->get_JobLevel() == L_FULL) {
166       return true;
167    }
168
169    strip_path(ff_pkt);
170  
171    if (S_ISDIR(ff_pkt->statp.st_mode)) {
172       fname = ff_pkt->link;
173    } else {
174       fname = ff_pkt->fname;
175    } 
176
177    if (!accurate_lookup(jcr, fname, &elt)) {
178       Dmsg1(dbglvl, "accurate %s (not found)\n", fname);
179       stat = true;
180       goto bail_out;
181    }
182
183    if (elt.seen) { /* file has been seen ? */
184       Dmsg1(dbglvl, "accurate %s (already seen)\n", fname);
185       goto bail_out;
186    }
187
188    if (elt.mtime != ff_pkt->statp.st_mtime) {
189 //   Jmsg(jcr, M_SAVED, 0, _("%s      st_mtime differs\n"), fname);
190      Dmsg1(dbglvl, "%s      st_mtime differs\n", fname);
191      stat = true;
192    } else if (elt.ctime != ff_pkt->statp.st_ctime) {
193 //   Jmsg(jcr, M_SAVED, 0, _("%s      st_ctime differs\n"), fname);
194      Dmsg1(dbglvl, "%s      st_ctime differs\n", fname);
195      stat = true;
196    }
197
198    accurate_mark_file_as_seen(jcr, &elt);
199 //   Dmsg2(dbglvl, "accurate %s = %i\n", fname, stat);
200
201 bail_out:
202    unstrip_path(ff_pkt);
203    return stat;
204 }
205
206 /* 
207  * TODO: use big buffer from htable
208  */
209 int accurate_cmd(JCR *jcr)
210 {
211    BSOCK *dir = jcr->dir_bsock;
212    int len;
213    int32_t nb;
214
215    if (!jcr->accurate || job_canceled(jcr) || jcr->get_JobLevel()==L_FULL) {
216       return true;
217    }
218
219    if (sscanf(dir->msg, "accurate files=%ld", &nb) != 1) {
220       dir->fsend(_("2991 Bad accurate command\n"));
221       return false;
222    }
223
224    accurate_init(jcr, nb);
225
226    /*
227     * buffer = sizeof(CurFile) + dirmsg
228     * dirmsg = fname + \0 + lstat
229     */
230    /* get current files */
231    while (dir->recv() >= 0) {
232       len = strlen(dir->msg) + 1;
233       if (len < dir->msglen) {
234          accurate_add_file(jcr, dir->msg, dir->msg + len);
235       }
236    }
237
238 #ifdef DEBUG
239    extern void *start_heap;
240
241    char b1[50], b2[50], b3[50], b4[50], b5[50];
242    Dmsg5(dbglvl," Heap: heap=%s smbytes=%s max_bytes=%s bufs=%s max_bufs=%s\n",
243          edit_uint64_with_commas((char *)sbrk(0)-(char *)start_heap, b1),
244          edit_uint64_with_commas(sm_bytes, b2),
245          edit_uint64_with_commas(sm_max_bytes, b3),
246          edit_uint64_with_commas(sm_buffers, b4),
247          edit_uint64_with_commas(sm_max_buffers, b5));
248 #endif
249
250    return true;
251 }