]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/find.c
code-extenstions for VSS support on Win32 (preliminary, use #define WIN32_VSS for...
[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  * For VSS we need to know which windows drives
99  * are used, because we create a snapshot of all used
100  * drives before operation
101  *
102  * the function returns the number of used drives and
103  * fills "drives" with up to 26 (A..Z) drive names
104  *
105  */
106 int
107 get_win32_driveletters(FF_PKT *ff, char* szDrives)
108 {
109    /* szDrives must be at least 27 bytes long */
110
111 #ifndef WIN32
112    return 0;
113 #endif
114
115    szDrives[0] = 0; /* make empty */
116    int nCount = 0;
117     
118    findFILESET *fileset = ff->fileset;
119    if (fileset) {
120       int i, j;
121       
122       for (i=0; i<fileset->include_list.size(); i++) {
123          findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
124          
125          /* look through all files and check */
126          for (j=0; j<incexe->name_list.size(); j++) {
127             char *fname = (char *)incexe->name_list.get(j);
128             /* fname should match x:/ */
129             if (strlen (fname) > 3 && B_ISALPHA (fname[0]) 
130                && fname[1] == ':' && fname[2] == '/') {
131                
132                /* always add in uppercase */
133                char ch = toupper(fname[0]);
134                /* if not found in string, add drive letter */
135                if (!strchr(szDrives,ch)) {
136                   szDrives[nCount] = ch;
137                   szDrives[nCount+1] = 0;
138                   nCount++;
139                }                                
140             }            
141          }
142       }
143    }
144    return nCount;
145 }
146
147 /*
148  * Find all specified files (determined by calls to name_add()
149  * This routine calls the (handle_file) subroutine with all
150  * sorts of good information for the final disposition of
151  * the file.
152  *
153  * Call this subroutine with a callback subroutine as the first
154  * argument and a packet as the second argument, this packet
155  * will be passed back to the callback subroutine as the last
156  * argument.
157  *
158  * The callback subroutine gets called with:
159  *  arg1 -- the FF_PKT containing filename, link, stat, ftype, flags, etc
160  *  arg2 -- the user supplied packet
161  *
162  */
163 int
164 find_files(JCR *jcr, FF_PKT *ff, int callback(FF_PKT *ff_pkt, void *hpkt, bool top_level), 
165            void *his_pkt)
166 {
167    ff->callback = callback;
168
169    /* This is the new way */
170    findFILESET *fileset = ff->fileset;
171    if (fileset) {
172       int i, j;
173       ff->flags = 0;
174       ff->VerifyOpts[0] = 'V';
175       ff->VerifyOpts[1] = 0;
176       for (i=0; i<fileset->include_list.size(); i++) {
177          findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
178          fileset->incexe = incexe;
179          /*
180           * By setting all options, we in effect or the global options
181           *   which is what we want.
182           */
183          for (j=0; j<incexe->opts_list.size(); j++) {
184             findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
185             ff->flags |= fo->flags;
186             ff->GZIP_level = fo->GZIP_level;
187             ff->fstypes = fo->fstype;
188             bstrncat(ff->VerifyOpts, fo->VerifyOpts, sizeof(ff->VerifyOpts));
189          }
190          for (j=0; j<incexe->name_list.size(); j++) {
191             Dmsg1(100, "F %s\n", (char *)incexe->name_list.get(j));
192             char *fname = (char *)incexe->name_list.get(j);
193             if (find_one_file(jcr, ff, our_callback, his_pkt, fname, (dev_t)-1, true) == 0) {
194                return 0;                  /* error return */
195             }
196          }
197       }
198    }
199    return 1;
200 }
201
202 static bool accept_file(FF_PKT *ff)
203 {
204    int i, j, k;
205    int ic;
206    findFILESET *fileset = ff->fileset;
207    findINCEXE *incexe = fileset->incexe;
208
209    for (j=0; j<incexe->opts_list.size(); j++) {
210       findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
211       ff->flags = fo->flags;
212       ff->GZIP_level = fo->GZIP_level;
213       ff->reader = fo->reader;
214       ff->writer = fo->writer;
215       ff->fstypes = fo->fstype;
216       ic = (ff->flags & FO_IGNORECASE) ? FNM_CASEFOLD : 0;
217       if (S_ISDIR(ff->statp.st_mode)) {
218          for (k=0; k<fo->wilddir.size(); k++) {
219             if (fnmatch((char *)fo->wilddir.get(k), ff->fname, fnmode|ic) == 0) {
220                if (ff->flags & FO_EXCLUDE) {
221                   Dmsg2(100, "Exclude wilddir: %s file=%s\n", (char *)fo->wilddir.get(k),
222                      ff->fname);
223                   return false;       /* reject file */
224                }
225                return true;           /* accept file */
226             }
227          }
228       } else {
229          for (k=0; k<fo->wildfile.size(); k++) {
230             if (fnmatch((char *)fo->wildfile.get(k), ff->fname, fnmode|ic) == 0) {
231                if (ff->flags & FO_EXCLUDE) {
232                   Dmsg2(100, "Exclude wildfile: %s file=%s\n", (char *)fo->wildfile.get(k),
233                      ff->fname);
234                   return false;       /* reject file */
235                }
236                return true;           /* accept file */
237             }
238          }
239       }
240       for (k=0; k<fo->wild.size(); k++) {
241          if (fnmatch((char *)fo->wild.get(k), ff->fname, fnmode|ic) == 0) {
242             if (ff->flags & FO_EXCLUDE) {
243                Dmsg2(100, "Exclude wild: %s file=%s\n", (char *)fo->wild.get(k),
244                   ff->fname);
245                return false;          /* reject file */
246             }
247             return true;              /* accept file */
248          }
249       }
250 #ifndef WIN32
251       if (S_ISDIR(ff->statp.st_mode)) {
252          for (k=0; k<fo->regexdir.size(); k++) {
253             const int nmatch = 30;
254             regmatch_t pmatch[nmatch];
255             if (regexec((regex_t *)fo->regexdir.get(k), ff->fname, nmatch, pmatch,  0) == 0) {
256                if (ff->flags & FO_EXCLUDE) {
257                   return false;       /* reject file */
258                }
259                return true;           /* accept file */
260             }
261          }
262       } else {
263          for (k=0; k<fo->regexfile.size(); k++) {
264             const int nmatch = 30;
265             regmatch_t pmatch[nmatch];
266             if (regexec((regex_t *)fo->regexfile.get(k), ff->fname, nmatch, pmatch,  0) == 0) {
267                if (ff->flags & FO_EXCLUDE) {
268                   return false;       /* reject file */
269                }
270                return true;           /* accept file */
271             }
272          }
273       }
274       for (k=0; k<fo->regex.size(); k++) {
275          const int nmatch = 30;
276          regmatch_t pmatch[nmatch];
277          if (regexec((regex_t *)fo->regex.get(k), ff->fname, nmatch, pmatch,  0) == 0) {
278             if (ff->flags & FO_EXCLUDE) {
279                return false;          /* reject file */
280             }
281             return true;              /* accept file */
282          }
283       }
284 #endif
285       /*
286        * If we have an empty Options clause with exclude, then
287        *  exclude the file
288        */
289       if (ff->flags & FO_EXCLUDE &&
290           fo->regex.size() == 0     && fo->wild.size() == 0 &&
291           fo->regexdir.size() == 0  && fo->wilddir.size() == 0 &&
292           fo->regexfile.size() == 0 && fo->wildfile.size() == 0) {
293          return false;              /* reject file */
294       }
295    }
296
297    /* Now apply the Exclude { } directive */
298    for (i=0; i<fileset->exclude_list.size(); i++) {
299       findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
300       for (j=0; j<incexe->opts_list.size(); j++) {
301          findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
302          ic = (fo->flags & FO_IGNORECASE) ? FNM_CASEFOLD : 0;
303          for (k=0; k<fo->wild.size(); k++) {
304             if (fnmatch((char *)fo->wild.get(k), ff->fname, fnmode|ic) == 0) {
305                Dmsg1(100, "Reject wild1: %s\n", ff->fname);
306                return false;          /* reject file */
307             }
308          }
309       }
310       ic = (incexe->current_opts != NULL && incexe->current_opts->flags & FO_IGNORECASE)
311              ? FNM_CASEFOLD : 0;
312       for (j=0; j<incexe->name_list.size(); j++) {
313          if (fnmatch((char *)incexe->name_list.get(j), ff->fname, fnmode|ic) == 0) {
314             Dmsg1(100, "Reject wild2: %s\n", ff->fname);
315             return false;          /* reject file */
316          }
317       }
318    }
319    return true;
320 }
321
322 /*
323  * The code comes here for each file examined.
324  * We filter the files, then call the user's callback if
325  *    the file is included.
326  */
327 static int our_callback(FF_PKT *ff, void *hpkt, bool top_level)
328 {
329    if (top_level) {
330       return ff->callback(ff, hpkt, top_level);   /* accept file */
331    }
332    switch (ff->type) {
333    case FT_NOACCESS:
334    case FT_NOFOLLOW:
335    case FT_NOSTAT:
336    case FT_NOCHG:
337    case FT_ISARCH:
338    case FT_NORECURSE:
339    case FT_NOFSCHG:
340    case FT_INVALIDFS:
341    case FT_NOOPEN:
342 //    return ff->callback(ff, hpkt, top_level);
343
344    /* These items can be filtered */
345    case FT_LNKSAVED:
346    case FT_REGE:
347    case FT_REG:
348    case FT_LNK:
349    case FT_DIRBEGIN:
350    case FT_DIREND:
351    case FT_RAW:
352    case FT_FIFO:
353    case FT_SPEC:
354    case FT_DIRNOCHG:
355       if (accept_file(ff)) {
356 //       Dmsg2(000, "Accept file %s; reader=%s\n", ff->fname, NPRT(ff->reader));
357          return ff->callback(ff, hpkt, top_level);
358       } else {
359          Dmsg1(100, "Skip file %s\n", ff->fname);
360          return -1;                   /* ignore this file */
361       }
362
363    default:
364       Dmsg1(000, "Unknown FT code %d\n", ff->type);
365       return 0;
366    }
367 }
368
369
370 /*
371  * Terminate find_files() and release
372  * all allocated memory
373  */
374 int
375 term_find_files(FF_PKT *ff)
376 {
377   int hard_links;
378
379   free_pool_memory(ff->sys_fname);
380   hard_links = term_find_one(ff);
381   free(ff);
382   return hard_links;
383 }