]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/find.c
Convert to pure GPL v2 license.
[bacula/bacula] / bacula / src / findlib / find.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2007 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  * Main routine for finding files on a file system.
30  *  The heart of the work to find the files on the
31  *    system is done in find_one.c. Here we have the
32  *    higher level control as well as the matching
33  *    routines for the new syntax Options resource.
34  *
35  *  Kern E. Sibbald, MM
36  *
37  *   Version $Id$
38  */
39
40
41 #include "bacula.h"
42 #include "find.h"
43
44 int32_t name_max;              /* filename max length */
45 int32_t path_max;              /* path name max length */
46
47 #ifdef DEBUG
48 #undef bmalloc
49 #define bmalloc(x) sm_malloc(__FILE__, __LINE__, x)
50 #endif
51 static int our_callback(FF_PKT *ff, void *hpkt, bool top_level);
52 static bool accept_file(FF_PKT *ff);
53
54 static const int fnmode = 0;
55
56 /*
57  * Initialize the find files "global" variables
58  */
59 FF_PKT *init_find_files()
60 {
61   FF_PKT *ff;
62
63   ff = (FF_PKT *)bmalloc(sizeof(FF_PKT));
64   memset(ff, 0, sizeof(FF_PKT));
65
66   ff->sys_fname = get_pool_memory(PM_FNAME);
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  * For VSS we need to know which windows drives
101  * are used, because we create a snapshot of all used
102  * drives before operation
103  *
104  * the function returns the number of used drives and
105  * fills "drives" with up to 26 (A..Z) drive names
106  *
107  */
108 int
109 get_win32_driveletters(FF_PKT *ff, char* szDrives)
110 {
111    /* szDrives must be at least 27 bytes long */
112
113 #if !defined(HAVE_WIN32)
114    return 0;
115 #endif
116
117    szDrives[0] = 0; /* make empty */
118    int nCount = 0;
119     
120    findFILESET *fileset = ff->fileset;
121    if (fileset) {
122       int i;
123       dlistString *node;
124       
125       for (i=0; i<fileset->include_list.size(); i++) {
126          findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
127          
128          /* look through all files and check */
129          foreach_dlist(node, &incexe->name_list) {
130             char *fname = node->c_str();
131             /* fname should match x:/ */
132             if (strlen(fname) >= 2 && B_ISALPHA(fname[0]) 
133                && fname[1] == ':') {
134                
135                /* always add in uppercase */
136                char ch = toupper(fname[0]);
137                /* if not found in string, add drive letter */
138                if (!strchr(szDrives,ch)) {
139                   szDrives[nCount] = ch;
140                   szDrives[nCount+1] = 0;
141                   nCount++;
142                }                                
143             }            
144          }
145       }
146    }
147    return nCount;
148 }
149
150 /*
151  * Find all specified files (determined by calls to name_add()
152  * This routine calls the (handle_file) subroutine with all
153  * sorts of good information for the final disposition of
154  * the file.
155  *
156  * Call this subroutine with a callback subroutine as the first
157  * argument and a packet as the second argument, this packet
158  * will be passed back to the callback subroutine as the last
159  * argument.
160  *
161  * The callback subroutine gets called with:
162  *  arg1 -- the FF_PKT containing filename, link, stat, ftype, flags, etc
163  *  arg2 -- the user supplied packet
164  *
165  */
166 int
167 find_files(JCR *jcr, FF_PKT *ff, int callback(FF_PKT *ff_pkt, void *hpkt, bool top_level), 
168            void *his_pkt)
169 {
170    ff->callback = callback;
171
172    /* This is the new way */
173    findFILESET *fileset = ff->fileset;
174    if (fileset) {
175       int i, j;
176       ff->flags = 0;
177       ff->VerifyOpts[0] = 'V';
178       ff->VerifyOpts[1] = 0;
179       for (i=0; i<fileset->include_list.size(); i++) {
180          findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
181          fileset->incexe = incexe;
182          /*
183           * By setting all options, we in effect or the global options
184           *   which is what we want.
185           */
186          for (j=0; j<incexe->opts_list.size(); j++) {
187             findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
188             ff->flags |= fo->flags;
189             ff->GZIP_level = fo->GZIP_level;
190             ff->strip_path = fo->strip_path;
191             ff->fstypes = fo->fstype;
192             ff->drivetypes = fo->drivetype;
193             bstrncat(ff->VerifyOpts, fo->VerifyOpts, sizeof(ff->VerifyOpts));
194          }
195          dlistString *node;
196          foreach_dlist(node, &incexe->name_list) {
197             char *fname = node->c_str();
198             Dmsg1(100, "F %s\n", fname);
199             ff->top_fname = fname;
200             if (find_one_file(jcr, ff, our_callback, his_pkt, ff->top_fname, (dev_t)-1, true) == 0) {
201                return 0;                  /* error return */
202             }
203          }
204       }
205    }
206    return 1;
207 }
208
209 static bool accept_file(FF_PKT *ff)
210 {
211    int i, j, k;
212    int fnm_flags;
213    findFILESET *fileset = ff->fileset;
214    findINCEXE *incexe = fileset->incexe;
215    const char *basename;
216    int (*match_func)(const char *pattern, const char *string, int flags);
217
218    if (ff->flags & FO_ENHANCEDWILD) {
219       match_func = enh_fnmatch;
220       if ((basename = last_path_separator(ff->fname)) != NULL)
221          basename++;
222       else
223          basename = ff->fname;
224    } else {
225       match_func = fnmatch;
226       basename = ff->fname;
227    }
228
229    for (j = 0; j < incexe->opts_list.size(); j++) {
230       findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
231       ff->flags = fo->flags;
232       ff->GZIP_level = fo->GZIP_level;
233       ff->reader = fo->reader;
234       ff->writer = fo->writer;
235       ff->fstypes = fo->fstype;
236       ff->drivetypes = fo->drivetype;
237
238       fnm_flags = (ff->flags & FO_IGNORECASE) ? FNM_CASEFOLD : 0;
239       fnm_flags |= (ff->flags & FO_ENHANCEDWILD) ? FNM_PATHNAME : 0;
240
241       if (S_ISDIR(ff->statp.st_mode)) {
242          for (k=0; k<fo->wilddir.size(); k++) {
243             if (match_func((char *)fo->wilddir.get(k), ff->fname, fnmode|fnm_flags) == 0) {
244                if (ff->flags & FO_EXCLUDE) {
245                   Dmsg2(100, "Exclude wilddir: %s file=%s\n", (char *)fo->wilddir.get(k),
246                      ff->fname);
247                   return false;       /* reject dir */
248                }
249                return true;           /* accept dir */
250             }
251          }
252       } else {
253          for (k=0; k<fo->wildfile.size(); k++) {
254             if (match_func((char *)fo->wildfile.get(k), ff->fname, fnmode|fnm_flags) == 0) {
255                if (ff->flags & FO_EXCLUDE) {
256                   Dmsg2(100, "Exclude wildfile: %s file=%s\n", (char *)fo->wildfile.get(k),
257                      ff->fname);
258                   return false;       /* reject file */
259                }
260                return true;           /* accept file */
261             }
262          }
263
264          for (k=0; k<fo->wildbase.size(); k++) {
265             if (match_func((char *)fo->wildbase.get(k), basename, fnmode|fnm_flags) == 0) {
266                if (ff->flags & FO_EXCLUDE) {
267                   Dmsg2(100, "Exclude wildbase: %s file=%s\n", (char *)fo->wildbase.get(k),
268                      basename);
269                   return false;       /* reject file */
270                }
271                return true;           /* accept file */
272             }
273          }
274       }
275       for (k=0; k<fo->wild.size(); k++) {
276          if (match_func((char *)fo->wild.get(k), ff->fname, fnmode|fnm_flags) == 0) {
277             if (ff->flags & FO_EXCLUDE) {
278                Dmsg2(100, "Exclude wild: %s file=%s\n", (char *)fo->wild.get(k),
279                   ff->fname);
280                return false;          /* reject file */
281             }
282             return true;              /* accept file */
283          }
284       }
285       if (S_ISDIR(ff->statp.st_mode)) {
286          for (k=0; k<fo->regexdir.size(); k++) {
287             const int nmatch = 30;
288             regmatch_t pmatch[nmatch];
289             if (regexec((regex_t *)fo->regexdir.get(k), ff->fname, nmatch, pmatch,  0) == 0) {
290                if (ff->flags & FO_EXCLUDE) {
291                   return false;       /* reject file */
292                }
293                return true;           /* accept file */
294             }
295          }
296       } else {
297          for (k=0; k<fo->regexfile.size(); k++) {
298             const int nmatch = 30;
299             regmatch_t pmatch[nmatch];
300             if (regexec((regex_t *)fo->regexfile.get(k), ff->fname, nmatch, pmatch,  0) == 0) {
301                if (ff->flags & FO_EXCLUDE) {
302                   return false;       /* reject file */
303                }
304                return true;           /* accept file */
305             }
306          }
307       }
308       for (k=0; k<fo->regex.size(); k++) {
309          const int nmatch = 30;
310          regmatch_t pmatch[nmatch];
311          if (regexec((regex_t *)fo->regex.get(k), ff->fname, nmatch, pmatch,  0) == 0) {
312             if (ff->flags & FO_EXCLUDE) {
313                return false;          /* reject file */
314             }
315             return true;              /* accept file */
316          }
317       }
318       /*
319        * If we have an empty Options clause with exclude, then
320        *  exclude the file
321        */
322       if (ff->flags & FO_EXCLUDE &&
323           fo->regex.size() == 0     && fo->wild.size() == 0 &&
324           fo->regexdir.size() == 0  && fo->wilddir.size() == 0 &&
325           fo->regexfile.size() == 0 && fo->wildfile.size() == 0 &&
326           fo->wildbase.size() == 0) {
327          return false;              /* reject file */
328       }
329    }
330
331    /* Now apply the Exclude { } directive */
332    for (i=0; i<fileset->exclude_list.size(); i++) {
333       findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
334       for (j=0; j<incexe->opts_list.size(); j++) {
335          findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
336          fnm_flags = (fo->flags & FO_IGNORECASE) ? FNM_CASEFOLD : 0;
337          for (k=0; k<fo->wild.size(); k++) {
338             if (fnmatch((char *)fo->wild.get(k), ff->fname, fnmode|fnm_flags) == 0) {
339                Dmsg1(100, "Reject wild1: %s\n", ff->fname);
340                return false;          /* reject file */
341             }
342          }
343       }
344       fnm_flags = (incexe->current_opts != NULL && incexe->current_opts->flags & FO_IGNORECASE)
345              ? FNM_CASEFOLD : 0;
346       dlistString *node;
347       foreach_dlist(node, &incexe->name_list) {
348          char *fname = node->c_str();
349          if (fnmatch(fname, ff->fname, fnmode|fnm_flags) == 0) {
350             Dmsg1(100, "Reject wild2: %s\n", ff->fname);
351             return false;          /* reject file */
352          }
353       }
354    }
355    return true;
356 }
357
358 /*
359  * The code comes here for each file examined.
360  * We filter the files, then call the user's callback if
361  *    the file is included.
362  */
363 static int our_callback(FF_PKT *ff, void *hpkt, bool top_level)
364 {
365    if (top_level) {
366       return ff->callback(ff, hpkt, top_level);   /* accept file */
367    }
368    switch (ff->type) {
369    case FT_NOACCESS:
370    case FT_NOFOLLOW:
371    case FT_NOSTAT:
372    case FT_NOCHG:
373    case FT_ISARCH:
374    case FT_NORECURSE:
375    case FT_NOFSCHG:
376    case FT_INVALIDFS:
377    case FT_INVALIDDT:
378    case FT_NOOPEN:
379 //    return ff->callback(ff, hpkt, top_level);
380
381    /* These items can be filtered */
382    case FT_LNKSAVED:
383    case FT_REGE:
384    case FT_REG:
385    case FT_LNK:
386    case FT_DIRBEGIN:
387    case FT_DIREND:
388    case FT_RAW:
389    case FT_FIFO:
390    case FT_SPEC:
391    case FT_DIRNOCHG:
392       if (accept_file(ff)) {
393 //       Dmsg2(000, "Accept file %s; reader=%s\n", ff->fname, NPRT(ff->reader));
394          return ff->callback(ff, hpkt, top_level);
395       } else {
396          Dmsg1(100, "Skip file %s\n", ff->fname);
397          return -1;                   /* ignore this file */
398       }
399
400    default:
401       Dmsg1(000, "Unknown FT code %d\n", ff->type);
402       return 0;
403    }
404 }
405
406
407 /*
408  * Terminate find_files() and release
409  * all allocated memory
410  */
411 int
412 term_find_files(FF_PKT *ff)
413 {
414    int hard_links;
415
416    free_pool_memory(ff->sys_fname);
417    if (ff->fname_save) {
418       free_pool_memory(ff->fname_save);
419    }
420    if (ff->link_save) {
421       free_pool_memory(ff->link_save);
422    }
423    hard_links = term_find_one(ff);
424    free(ff);
425    return hard_links;
426 }