]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/find.c
826251a8f1fdc2bf0e99a14cb4844849b07ddfe8
[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-2004 Kern Sibbald and John Walker
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       for (k=0; k<fo->wild.size(); k++) {
184          if (fnmatch((char *)fo->wild.get(k), ff->fname, fnmode|ic) == 0) {
185             if (ff->flags & FO_EXCLUDE) {
186                Dmsg2(100, "Exclude wild: %s file=%s\n", (char *)fo->wild.get(k),
187                   ff->fname);
188                return false;          /* reject file */
189             }
190             return true;              /* accept file */
191          }
192       }
193 #ifndef WIN32
194       for (k=0; k<fo->regex.size(); k++) {
195          const int nmatch = 30;
196          regmatch_t pmatch[nmatch];
197          if (regexec((regex_t *)fo->regex.get(k), ff->fname, nmatch, pmatch,  0) == 0) {
198             if (ff->flags & FO_EXCLUDE) {
199                return false;          /* reject file */
200             }
201             return true;              /* accept file */
202          }
203       }
204 #endif
205    }
206
207    for (i=0; i<fileset->exclude_list.size(); i++) {
208       findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
209       for (j=0; j<incexe->opts_list.size(); j++) {
210          findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
211          ic = (fo->flags & FO_IGNORECASE) ? FNM_CASEFOLD : 0;
212          for (k=0; k<fo->wild.size(); k++) {
213             if (fnmatch((char *)fo->wild.get(k), ff->fname, fnmode|ic) == 0) {
214                Dmsg1(100, "Reject wild1: %s\n", ff->fname);
215                return false;          /* reject file */
216             }
217          }
218       }
219       ic = (incexe->current_opts != NULL && incexe->current_opts->flags & FO_IGNORECASE)
220              ? FNM_CASEFOLD : 0;
221       for (j=0; j<incexe->name_list.size(); j++) {
222          if (fnmatch((char *)incexe->name_list.get(j), ff->fname, fnmode|ic) == 0) {
223             Dmsg1(100, "Reject wild2: %s\n", ff->fname);
224             return false;          /* reject file */
225          }
226       }
227    }
228    return true;
229 }
230
231 /*
232  * The code comes here for each file examined.
233  * We filter the files, then call the user's callback if
234  *    the file is included.
235  */
236 static int our_callback(FF_PKT *ff, void *hpkt)
237 {
238    switch (ff->type) {
239    case FT_NOACCESS:
240    case FT_NOFOLLOW:
241    case FT_NOSTAT:
242    case FT_NOCHG:
243    case FT_ISARCH:
244    case FT_NORECURSE:
245    case FT_NOFSCHG:
246    case FT_INVALIDFS:
247    case FT_NOOPEN:
248 //    return ff->callback(ff, hpkt);
249
250    /* These items can be filtered */
251    case FT_LNKSAVED:
252    case FT_REGE:
253    case FT_REG:
254    case FT_LNK:
255    case FT_DIRBEGIN:
256    case FT_DIREND:
257    case FT_RAW:
258    case FT_FIFO:
259    case FT_SPEC:
260    case FT_DIRNOCHG:
261       if (accept_file(ff)) {
262          return ff->callback(ff, hpkt);
263       } else {
264          Dmsg1(100, "Skip file %s\n", ff->fname);
265          return -1;                   /* ignore this file */
266       }
267
268    default:
269       Dmsg1(000, "Unknown FT code %d\n", ff->type);
270       return 0;
271    }
272 }
273
274
275 /*
276  * Terminate find_files() and release
277  * all allocated memory
278  */
279 int
280 term_find_files(FF_PKT *ff)
281 {
282   int hard_links;
283
284   term_include_exclude_files(ff);
285   free_pool_memory(ff->sys_fname);
286   hard_links = term_find_one(ff);
287   free(ff);
288   return hard_links;
289 }