]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/find.c
Integrate Nicolas' patch for direct DVD support.
[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
40 /* ****FIXME**** debug until stable */
41 #undef bmalloc
42 #define bmalloc(x) sm_malloc(__FILE__, __LINE__, x)
43 static int our_callback(FF_PKT *ff, void *hpkt);
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   init_include_exclude_files(ff);           /* init lists */
67
68    /* Get system path and filename maximum lengths */
69    path_max = pathconf(".", _PC_PATH_MAX);
70    if (path_max < 1024) {
71       path_max = 1024;
72    }
73
74    name_max = pathconf(".", _PC_NAME_MAX);
75    if (name_max < 1024) {
76       name_max = 1024;
77    }
78    path_max++;                        /* add for EOS */
79    name_max++;                        /* add for EOS */
80
81   Dmsg1(100, "init_find_files ff=%p\n", ff);
82   return ff;
83 }
84
85 /*
86  * Set find_files options. For the moment, we only
87  * provide for full/incremental saves, and setting
88  * of save_time. For additional options, see above
89  */
90 void
91 set_find_options(FF_PKT *ff, int incremental, time_t save_time)
92 {
93   Dmsg0(100, "Enter set_find_options()\n");
94   ff->incremental = incremental;
95   ff->save_time = save_time;
96   Dmsg0(100, "Leave set_find_options()\n");
97 }
98
99
100 /*
101  * Find all specified files (determined by calls to name_add()
102  * This routine calls the (handle_file) subroutine with all
103  * sorts of good information for the final disposition of
104  * the file.
105  *
106  * Call this subroutine with a callback subroutine as the first
107  * argument and a packet as the second argument, this packet
108  * will be passed back to the callback subroutine as the last
109  * argument.
110  *
111  * The callback subroutine gets called with:
112  *  arg1 -- the FF_PKT containing filename, link, stat, ftype, flags, etc
113  *  arg2 -- the user supplied packet
114  *
115  */
116 int
117 find_files(JCR *jcr, FF_PKT *ff, int callback(FF_PKT *ff_pkt, void *hpkt), void *his_pkt)
118 {
119    ff->callback = callback;
120
121    /* This is the new way */
122    findFILESET *fileset = ff->fileset;
123    if (fileset) {
124       int i, j;
125       ff->flags = 0;
126       ff->VerifyOpts[0] = 'V';
127       ff->VerifyOpts[1] = 0;
128       for (i=0; i<fileset->include_list.size(); i++) {
129          findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
130          fileset->incexe = incexe;
131          /*
132           * By setting all options, we in effect or the global options
133           *   which is what we want.
134           */
135          for (j=0; j<incexe->opts_list.size(); j++) {
136             findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
137             ff->flags |= fo->flags;
138             ff->GZIP_level = fo->GZIP_level;
139             ff->fstypes = fo->fstype;
140             bstrncat(ff->VerifyOpts, fo->VerifyOpts, sizeof(ff->VerifyOpts));
141          }
142          for (j=0; j<incexe->name_list.size(); j++) {
143             Dmsg1(100, "F %s\n", (char *)incexe->name_list.get(j));
144             char *fname = (char *)incexe->name_list.get(j);
145             if (find_one_file(jcr, ff, our_callback, his_pkt, fname, (dev_t)-1, 1) == 0) {
146                return 0;                  /* error return */
147             }
148          }
149       }
150    } else {
151       struct s_included_file *inc = NULL;
152
153       /* This is the old deprecated way */
154       while (!job_canceled(jcr) && (inc = get_next_included_file(ff, inc))) {
155          /* Copy options for this file */
156          bstrncat(ff->VerifyOpts, inc->VerifyOpts, sizeof(ff->VerifyOpts));
157          Dmsg1(100, "find_files: file=%s\n", inc->fname);
158          if (!file_is_excluded(ff, inc->fname)) {
159             if (find_one_file(jcr, ff, callback, his_pkt, inc->fname, (dev_t)-1, 1) ==0) {
160                return 0;                  /* error return */
161             }
162          }
163       }
164    }
165    return 1;
166 }
167
168 static bool accept_file(FF_PKT *ff)
169 {
170    int i, j, k;
171    int ic;
172    findFILESET *fileset = ff->fileset;
173    findINCEXE *incexe = fileset->incexe;
174
175    for (j=0; j<incexe->opts_list.size(); j++) {
176       findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
177       ff->flags = fo->flags;
178       ff->GZIP_level = fo->GZIP_level;
179       ff->reader = fo->reader;
180       ff->writer = fo->writer;
181       ff->fstypes = fo->fstype;
182       ic = (ff->flags & FO_IGNORECASE) ? FNM_CASEFOLD : 0;
183       if (S_ISDIR(ff->statp.st_mode)) {
184          for (k=0; k<fo->wilddir.size(); k++) {
185             if (fnmatch((char *)fo->wilddir.get(k), ff->fname, fnmode|ic) == 0) {
186                if (ff->flags & FO_EXCLUDE) {
187                   Dmsg2(100, "Exclude wilddir: %s file=%s\n", (char *)fo->wilddir.get(k),
188                      ff->fname);
189                   return false;       /* reject file */
190                }
191                return true;           /* accept file */
192             }
193          }
194       } else {
195          for (k=0; k<fo->wildfile.size(); k++) {
196             if (fnmatch((char *)fo->wildfile.get(k), ff->fname, fnmode|ic) == 0) {
197                if (ff->flags & FO_EXCLUDE) {
198                   Dmsg2(100, "Exclude wildfile: %s file=%s\n", (char *)fo->wildfile.get(k),
199                      ff->fname);
200                   return false;       /* reject file */
201                }
202                return true;           /* accept file */
203             }
204          }
205       }
206       for (k=0; k<fo->wild.size(); k++) {
207          if (fnmatch((char *)fo->wild.get(k), ff->fname, fnmode|ic) == 0) {
208             if (ff->flags & FO_EXCLUDE) {
209                Dmsg2(100, "Exclude wild: %s file=%s\n", (char *)fo->wild.get(k),
210                   ff->fname);
211                return false;          /* reject file */
212             }
213             return true;              /* accept file */
214          }
215       }
216 #ifndef WIN32
217       if (S_ISDIR(ff->statp.st_mode)) {
218          for (k=0; k<fo->regexdir.size(); k++) {
219             const int nmatch = 30;
220             regmatch_t pmatch[nmatch];
221             if (regexec((regex_t *)fo->regexdir.get(k), ff->fname, nmatch, pmatch,  0) == 0) {
222                if (ff->flags & FO_EXCLUDE) {
223                   return false;       /* reject file */
224                }
225                return true;           /* accept file */
226             }
227          }
228       } else {
229          for (k=0; k<fo->regexfile.size(); k++) {
230             const int nmatch = 30;
231             regmatch_t pmatch[nmatch];
232             if (regexec((regex_t *)fo->regexfile.get(k), ff->fname, nmatch, pmatch,  0) == 0) {
233                if (ff->flags & FO_EXCLUDE) {
234                   return false;       /* reject file */
235                }
236                return true;           /* accept file */
237             }
238          }
239       }
240       for (k=0; k<fo->regex.size(); k++) {
241          const int nmatch = 30;
242          regmatch_t pmatch[nmatch];
243          if (regexec((regex_t *)fo->regex.get(k), ff->fname, nmatch, pmatch,  0) == 0) {
244             if (ff->flags & FO_EXCLUDE) {
245                return false;          /* reject file */
246             }
247             return true;              /* accept file */
248          }
249       }
250 #endif
251    }
252
253    for (i=0; i<fileset->exclude_list.size(); i++) {
254       findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
255       for (j=0; j<incexe->opts_list.size(); j++) {
256          findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
257          ic = (fo->flags & FO_IGNORECASE) ? FNM_CASEFOLD : 0;
258          for (k=0; k<fo->wild.size(); k++) {
259             if (fnmatch((char *)fo->wild.get(k), ff->fname, fnmode|ic) == 0) {
260                Dmsg1(100, "Reject wild1: %s\n", ff->fname);
261                return false;          /* reject file */
262             }
263          }
264       }
265       ic = (incexe->current_opts != NULL && incexe->current_opts->flags & FO_IGNORECASE)
266              ? FNM_CASEFOLD : 0;
267       for (j=0; j<incexe->name_list.size(); j++) {
268          if (fnmatch((char *)incexe->name_list.get(j), ff->fname, fnmode|ic) == 0) {
269             Dmsg1(100, "Reject wild2: %s\n", ff->fname);
270             return false;          /* reject file */
271          }
272       }
273    }
274    return true;
275 }
276
277 /*
278  * The code comes here for each file examined.
279  * We filter the files, then call the user's callback if
280  *    the file is included.
281  */
282 static int our_callback(FF_PKT *ff, void *hpkt)
283 {
284    switch (ff->type) {
285    case FT_NOACCESS:
286    case FT_NOFOLLOW:
287    case FT_NOSTAT:
288    case FT_NOCHG:
289    case FT_ISARCH:
290    case FT_NORECURSE:
291    case FT_NOFSCHG:
292    case FT_INVALIDFS:
293    case FT_NOOPEN:
294 //    return ff->callback(ff, hpkt);
295
296    /* These items can be filtered */
297    case FT_LNKSAVED:
298    case FT_REGE:
299    case FT_REG:
300    case FT_LNK:
301    case FT_DIRBEGIN:
302    case FT_DIREND:
303    case FT_RAW:
304    case FT_FIFO:
305    case FT_SPEC:
306    case FT_DIRNOCHG:
307       if (accept_file(ff)) {
308          return ff->callback(ff, hpkt);
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   term_include_exclude_files(ff);
331   free_pool_memory(ff->sys_fname);
332   hard_links = term_find_one(ff);
333   free(ff);
334   return hard_links;
335 }