]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/find.c
- Update all the db update scripts to include the new multiple
[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 /*
11    Copyright (C) 2000-2004 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30
31 #include "bacula.h"
32 #include "find.h"
33
34 int32_t name_max;              /* filename max length */
35 int32_t path_max;              /* path name max length */
36
37
38 /* ****FIXME**** debug until stable */
39 #undef bmalloc
40 #define bmalloc(x) sm_malloc(__FILE__, __LINE__, x)
41 static int our_callback(FF_PKT *ff, void *hpkt);
42 static bool accept_file(FF_PKT *ff);
43
44 /* Fold case in fnmatch() on Win32 */
45 #ifdef WIN32
46 static const int fnmode = FNM_CASEFOLD;
47 #else
48 static const int fnmode = 0;
49 #endif
50
51
52 /* 
53  * Initialize the find files "global" variables
54  */
55 FF_PKT *init_find_files()
56 {
57   FF_PKT *ff;    
58
59   ff = (FF_PKT *)bmalloc(sizeof(FF_PKT));
60   memset(ff, 0, sizeof(FF_PKT));
61
62   ff->sys_fname = get_pool_memory(PM_FNAME);
63
64   init_include_exclude_files(ff);           /* init lists */
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), void *his_pkt) 
116 {
117    ff->callback = callback;
118
119    /* This is the new way */
120    findFILESET *fileset = ff->fileset;
121    if (fileset) {
122       int i, j;
123       ff->flags = 0;
124       ff->VerifyOpts[0] = 'V';
125       ff->VerifyOpts[1] = 0;
126       for (i=0; i<fileset->include_list.size(); i++) {
127          findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
128          fileset->incexe = incexe;
129          /*
130           * By setting all options, we in effect or the global options
131           *   which is what we want.
132           */
133          for (j=0; j<incexe->opts_list.size(); j++) {
134             findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
135             ff->flags |= fo->flags;
136             ff->GZIP_level = fo->GZIP_level;
137             bstrncat(ff->VerifyOpts, fo->VerifyOpts, sizeof(ff->VerifyOpts)); 
138          }
139          for (j=0; j<incexe->name_list.size(); j++) {
140             Dmsg1(400, "F %s\n", (char *)incexe->name_list.get(j));
141             char *fname = (char *)incexe->name_list.get(j);
142             if (find_one_file(jcr, ff, our_callback, his_pkt, fname, (dev_t)-1, 1) == 0) {
143                return 0;                  /* error return */
144             }
145          }
146       }
147    } else {
148       struct s_included_file *inc = NULL;
149
150       /* This is the old deprecated way */
151       while (!job_canceled(jcr) && (inc = get_next_included_file(ff, inc))) {
152          /* Copy options for this file */
153          bstrncat(ff->VerifyOpts, inc->VerifyOpts, sizeof(ff->VerifyOpts)); 
154          Dmsg1(50, "find_files: file=%s\n", inc->fname);
155          if (!file_is_excluded(ff, inc->fname)) {
156             if (find_one_file(jcr, ff, callback, his_pkt, inc->fname, (dev_t)-1, 1) ==0) {
157                return 0;                  /* error return */
158             }
159          }
160       }
161    }
162    return 1;
163 }
164
165 static bool accept_file(FF_PKT *ff)
166 {
167    int i, j, k;
168    findFILESET *fileset = ff->fileset;
169    findINCEXE *incexe = fileset->incexe;
170
171    for (j=0; j<incexe->opts_list.size(); j++) {
172       findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
173       ff->flags = fo->flags;
174       ff->GZIP_level = fo->GZIP_level;
175       ff->reader = fo->reader;
176       ff->writer = fo->writer;
177       for (k=0; k<fo->wild.size(); k++) {
178          if (fnmatch((char *)fo->wild.get(k), ff->fname, fnmode) == 0) {
179             if (ff->flags & FO_EXCLUDE) {
180                return false;          /* reject file */
181             }
182             return true;              /* accept file */
183          }
184       }
185 #ifndef WIN32
186       for (k=0; k<fo->regex.size(); k++) {
187          const int nmatch = 30;
188          regmatch_t pmatch[nmatch];
189          if (regexec((regex_t *)fo->regex.get(k), ff->fname, nmatch, pmatch,  0) == 0) {
190             if (ff->flags & FO_EXCLUDE) {
191                return false;          /* reject file */
192             }
193             return true;              /* accept file */
194          }
195       }
196 #endif
197    }
198
199    for (i=0; i<fileset->exclude_list.size(); i++) {
200       findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
201       for (j=0; j<incexe->opts_list.size(); j++) {
202          findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
203          for (k=0; k<fo->wild.size(); k++) {
204             if (fnmatch((char *)fo->wild.get(k), ff->fname, fnmode) == 0) {
205                Dmsg1(400, "Reject wild1: %s\n", ff->fname);
206                return false;          /* reject file */
207             }
208          }
209       }
210       for (j=0; j<incexe->name_list.size(); j++) {
211          if (fnmatch((char *)incexe->name_list.get(j), ff->fname, fnmode) == 0) {
212             Dmsg1(400, "Reject wild2: %s\n", ff->fname);
213             return false;          /* reject file */
214          }
215       }
216    }
217    return true;
218 }
219
220 /*
221  * The code comes here for each file examined.
222  * We filter the files, then call the user's callback if
223  *    the file is included. 
224  */
225 static int our_callback(FF_PKT *ff, void *hpkt)
226 {
227    switch (ff->type) {
228    case FT_NOACCESS:
229    case FT_NOFOLLOW:
230    case FT_NOSTAT:
231    case FT_NOCHG:
232    case FT_ISARCH:
233    case FT_NORECURSE:
234    case FT_NOFSCHG:
235    case FT_NOOPEN:
236 //    return ff->callback(ff, hpkt);
237
238    /* These items can be filtered */
239    case FT_LNKSAVED:
240    case FT_REGE:
241    case FT_REG:
242    case FT_LNK:
243    case FT_DIRBEGIN:
244    case FT_DIREND:
245    case FT_RAW:
246    case FT_FIFO:
247    case FT_SPEC:
248    case FT_DIRNOCHG:
249       if (accept_file(ff)) {
250          return ff->callback(ff, hpkt);
251       } else {
252          Dmsg1(100, "Skip file %s\n", ff->fname);
253          return -1;                   /* ignore this file */
254       }
255
256    default:
257       Dmsg1(000, "Unknown FT code %d\n", ff->type);
258       return 0;
259    }
260 }
261
262
263 /*
264  * Terminate find_files() and release
265  * all allocated memory   
266  */
267 int
268 term_find_files(FF_PKT *ff)
269 {
270   int hard_links;
271
272   term_include_exclude_files(ff);
273   free_pool_memory(ff->sys_fname);
274   hard_links = term_find_one(ff);
275   free(ff);
276   return hard_links;
277 }