]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/find.c
Update projects
[bacula/bacula] / bacula / src / findlib / find.c
1 /*
2  * Main routine for finding files on a file system.
3  *  The heart of the work to find the files on the
4  *    system is done in find_one.c. Here we have the
5  *    higher level control as well as the matching
6  *    routines for the new syntax Options resource.
7  *
8  *  Kern E. Sibbald, MM
9  *
10  *   Version $Id$
11  */
12 /*
13    Copyright (C) 2000-2005 Kern Sibbald
14
15    This program is free software; you can redistribute it and/or
16    modify it under the terms of the GNU General Public License as
17    published by the Free Software Foundation; either version 2 of
18    the License, or (at your option) any later version.
19
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public
26    License along with this program; if not, write to the Free
27    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28    MA 02111-1307, USA.
29
30  */
31
32
33 #include "bacula.h"
34 #include "find.h"
35
36 int32_t name_max;              /* filename max length */
37 int32_t path_max;              /* path name max length */
38
39 #ifdef DEBUG
40 #undef bmalloc
41 #define bmalloc(x) sm_malloc(__FILE__, __LINE__, x)
42 #endif
43 static int our_callback(FF_PKT *ff, void *hpkt, bool top_level);
44 static bool accept_file(FF_PKT *ff);
45
46 /* Fold case in fnmatch() on Win32 */
47 #ifdef WIN32
48 static const int fnmode = FNM_CASEFOLD;
49 #else
50 static const int fnmode = 0;
51 #endif
52
53
54 /*
55  * Initialize the find files "global" variables
56  */
57 FF_PKT *init_find_files()
58 {
59   FF_PKT *ff;
60
61   ff = (FF_PKT *)bmalloc(sizeof(FF_PKT));
62   memset(ff, 0, sizeof(FF_PKT));
63
64   ff->sys_fname = get_pool_memory(PM_FNAME);
65
66    /* Get system path and filename maximum lengths */
67    path_max = pathconf(".", _PC_PATH_MAX);
68    if (path_max < 1024) {
69       path_max = 1024;
70    }
71
72    name_max = pathconf(".", _PC_NAME_MAX);
73    if (name_max < 1024) {
74       name_max = 1024;
75    }
76    path_max++;                        /* add for EOS */
77    name_max++;                        /* add for EOS */
78
79   Dmsg1(100, "init_find_files ff=%p\n", ff);
80   return ff;
81 }
82
83 /*
84  * Set find_files options. For the moment, we only
85  * provide for full/incremental saves, and setting
86  * of save_time. For additional options, see above
87  */
88 void
89 set_find_options(FF_PKT *ff, int incremental, time_t save_time)
90 {
91   Dmsg0(100, "Enter set_find_options()\n");
92   ff->incremental = incremental;
93   ff->save_time = save_time;
94   Dmsg0(100, "Leave set_find_options()\n");
95 }
96
97
98 /*
99  * Find all specified files (determined by calls to name_add()
100  * This routine calls the (handle_file) subroutine with all
101  * sorts of good information for the final disposition of
102  * the file.
103  *
104  * Call this subroutine with a callback subroutine as the first
105  * argument and a packet as the second argument, this packet
106  * will be passed back to the callback subroutine as the last
107  * argument.
108  *
109  * The callback subroutine gets called with:
110  *  arg1 -- the FF_PKT containing filename, link, stat, ftype, flags, etc
111  *  arg2 -- the user supplied packet
112  *
113  */
114 int
115 find_files(JCR *jcr, FF_PKT *ff, int callback(FF_PKT *ff_pkt, void *hpkt, bool top_level), 
116            void *his_pkt)
117 {
118    ff->callback = callback;
119
120    /* This is the new way */
121    findFILESET *fileset = ff->fileset;
122    if (fileset) {
123       int i, j;
124       ff->flags = 0;
125       ff->VerifyOpts[0] = 'V';
126       ff->VerifyOpts[1] = 0;
127       for (i=0; i<fileset->include_list.size(); i++) {
128          findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
129          fileset->incexe = incexe;
130          /*
131           * By setting all options, we in effect or the global options
132           *   which is what we want.
133           */
134          for (j=0; j<incexe->opts_list.size(); j++) {
135             findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
136             ff->flags |= fo->flags;
137             ff->GZIP_level = fo->GZIP_level;
138             ff->fstypes = fo->fstype;
139             bstrncat(ff->VerifyOpts, fo->VerifyOpts, sizeof(ff->VerifyOpts));
140          }
141          for (j=0; j<incexe->name_list.size(); j++) {
142             Dmsg1(100, "F %s\n", (char *)incexe->name_list.get(j));
143             char *fname = (char *)incexe->name_list.get(j);
144             if (find_one_file(jcr, ff, our_callback, his_pkt, fname, (dev_t)-1, true) == 0) {
145                return 0;                  /* error return */
146             }
147          }
148       }
149    }
150    return 1;
151 }
152
153 static bool accept_file(FF_PKT *ff)
154 {
155    int i, j, k;
156    int ic;
157    findFILESET *fileset = ff->fileset;
158    findINCEXE *incexe = fileset->incexe;
159
160    for (j=0; j<incexe->opts_list.size(); j++) {
161       findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
162       ff->flags = fo->flags;
163       ff->GZIP_level = fo->GZIP_level;
164       ff->reader = fo->reader;
165       ff->writer = fo->writer;
166       ff->fstypes = fo->fstype;
167       ic = (ff->flags & FO_IGNORECASE) ? FNM_CASEFOLD : 0;
168       if (S_ISDIR(ff->statp.st_mode)) {
169          for (k=0; k<fo->wilddir.size(); k++) {
170             if (fnmatch((char *)fo->wilddir.get(k), ff->fname, fnmode|ic) == 0) {
171                if (ff->flags & FO_EXCLUDE) {
172                   Dmsg2(100, "Exclude wilddir: %s file=%s\n", (char *)fo->wilddir.get(k),
173                      ff->fname);
174                   return false;       /* reject file */
175                }
176                return true;           /* accept file */
177             }
178          }
179       } else {
180          for (k=0; k<fo->wildfile.size(); k++) {
181             if (fnmatch((char *)fo->wildfile.get(k), ff->fname, fnmode|ic) == 0) {
182                if (ff->flags & FO_EXCLUDE) {
183                   Dmsg2(100, "Exclude wildfile: %s file=%s\n", (char *)fo->wildfile.get(k),
184                      ff->fname);
185                   return false;       /* reject file */
186                }
187                return true;           /* accept file */
188             }
189          }
190       }
191       for (k=0; k<fo->wild.size(); k++) {
192          if (fnmatch((char *)fo->wild.get(k), ff->fname, fnmode|ic) == 0) {
193             if (ff->flags & FO_EXCLUDE) {
194                Dmsg2(100, "Exclude wild: %s file=%s\n", (char *)fo->wild.get(k),
195                   ff->fname);
196                return false;          /* reject file */
197             }
198             return true;              /* accept file */
199          }
200       }
201 #ifndef WIN32
202       if (S_ISDIR(ff->statp.st_mode)) {
203          for (k=0; k<fo->regexdir.size(); k++) {
204             const int nmatch = 30;
205             regmatch_t pmatch[nmatch];
206             if (regexec((regex_t *)fo->regexdir.get(k), ff->fname, nmatch, pmatch,  0) == 0) {
207                if (ff->flags & FO_EXCLUDE) {
208                   return false;       /* reject file */
209                }
210                return true;           /* accept file */
211             }
212          }
213       } else {
214          for (k=0; k<fo->regexfile.size(); k++) {
215             const int nmatch = 30;
216             regmatch_t pmatch[nmatch];
217             if (regexec((regex_t *)fo->regexfile.get(k), ff->fname, nmatch, pmatch,  0) == 0) {
218                if (ff->flags & FO_EXCLUDE) {
219                   return false;       /* reject file */
220                }
221                return true;           /* accept file */
222             }
223          }
224       }
225       for (k=0; k<fo->regex.size(); k++) {
226          const int nmatch = 30;
227          regmatch_t pmatch[nmatch];
228          if (regexec((regex_t *)fo->regex.get(k), ff->fname, nmatch, pmatch,  0) == 0) {
229             if (ff->flags & FO_EXCLUDE) {
230                return false;          /* reject file */
231             }
232             return true;              /* accept file */
233          }
234       }
235 #endif
236       /*
237        * If we have an empty Options clause with exclude, then
238        *  exclude the file
239        */
240       if (ff->flags & FO_EXCLUDE &&
241           fo->regex.size() == 0     && fo->wild.size() == 0 &&
242           fo->regexdir.size() == 0  && fo->wilddir.size() == 0 &&
243           fo->regexfile.size() == 0 && fo->wildfile.size() == 0) {
244          return false;              /* reject file */
245       }
246    }
247
248    /* Now apply the Exclude { } directive */
249    for (i=0; i<fileset->exclude_list.size(); i++) {
250       findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
251       for (j=0; j<incexe->opts_list.size(); j++) {
252          findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
253          ic = (fo->flags & FO_IGNORECASE) ? FNM_CASEFOLD : 0;
254          for (k=0; k<fo->wild.size(); k++) {
255             if (fnmatch((char *)fo->wild.get(k), ff->fname, fnmode|ic) == 0) {
256                Dmsg1(100, "Reject wild1: %s\n", ff->fname);
257                return false;          /* reject file */
258             }
259          }
260       }
261       ic = (incexe->current_opts != NULL && incexe->current_opts->flags & FO_IGNORECASE)
262              ? FNM_CASEFOLD : 0;
263       for (j=0; j<incexe->name_list.size(); j++) {
264          if (fnmatch((char *)incexe->name_list.get(j), ff->fname, fnmode|ic) == 0) {
265             Dmsg1(100, "Reject wild2: %s\n", ff->fname);
266             return false;          /* reject file */
267          }
268       }
269    }
270    return true;
271 }
272
273 /*
274  * The code comes here for each file examined.
275  * We filter the files, then call the user's callback if
276  *    the file is included.
277  */
278 static int our_callback(FF_PKT *ff, void *hpkt, bool top_level)
279 {
280    if (top_level) {
281       return ff->callback(ff, hpkt, top_level);   /* accept file */
282    }
283    switch (ff->type) {
284    case FT_NOACCESS:
285    case FT_NOFOLLOW:
286    case FT_NOSTAT:
287    case FT_NOCHG:
288    case FT_ISARCH:
289    case FT_NORECURSE:
290    case FT_NOFSCHG:
291    case FT_INVALIDFS:
292    case FT_NOOPEN:
293 //    return ff->callback(ff, hpkt, top_level);
294
295    /* These items can be filtered */
296    case FT_LNKSAVED:
297    case FT_REGE:
298    case FT_REG:
299    case FT_LNK:
300    case FT_DIRBEGIN:
301    case FT_DIREND:
302    case FT_RAW:
303    case FT_FIFO:
304    case FT_SPEC:
305    case FT_DIRNOCHG:
306       if (accept_file(ff)) {
307 //       Dmsg2(000, "Accept file %s; reader=%s\n", ff->fname, NPRT(ff->reader));
308          return ff->callback(ff, hpkt, top_level);
309       } else {
310          Dmsg1(100, "Skip file %s\n", ff->fname);
311          return -1;                   /* ignore this file */
312       }
313
314    default:
315       Dmsg1(000, "Unknown FT code %d\n", ff->type);
316       return 0;
317    }
318 }
319
320
321 /*
322  * Terminate find_files() and release
323  * all allocated memory
324  */
325 int
326 term_find_files(FF_PKT *ff)
327 {
328   int hard_links;
329
330   free_pool_memory(ff->sys_fname);
331   hard_links = term_find_one(ff);
332   free(ff);
333   return hard_links;
334 }