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.
13 Copyright (C) 2000-2005 Kern Sibbald
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.
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.
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,
36 int32_t name_max; /* filename max length */
37 int32_t path_max; /* path name max length */
41 #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);
46 /* Fold case in fnmatch() on Win32 */
48 static const int fnmode = FNM_CASEFOLD;
50 static const int fnmode = 0;
55 * Initialize the find files "global" variables
57 FF_PKT *init_find_files()
61 ff = (FF_PKT *)bmalloc(sizeof(FF_PKT));
62 memset(ff, 0, sizeof(FF_PKT));
64 ff->sys_fname = get_pool_memory(PM_FNAME);
66 /* Get system path and filename maximum lengths */
67 path_max = pathconf(".", _PC_PATH_MAX);
68 if (path_max < 1024) {
72 name_max = pathconf(".", _PC_NAME_MAX);
73 if (name_max < 1024) {
76 path_max++; /* add for EOS */
77 name_max++; /* add for EOS */
79 Dmsg1(100, "init_find_files ff=%p\n", ff);
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
89 set_find_options(FF_PKT *ff, int incremental, time_t save_time)
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");
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
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
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
115 find_files(JCR *jcr, FF_PKT *ff, int callback(FF_PKT *ff_pkt, void *hpkt), void *his_pkt)
117 ff->callback = callback;
119 /* This is the new way */
120 findFILESET *fileset = ff->fileset;
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;
130 * By setting all options, we in effect or the global options
131 * which is what we want.
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 ff->fstypes = fo->fstype;
138 bstrncat(ff->VerifyOpts, fo->VerifyOpts, sizeof(ff->VerifyOpts));
140 for (j=0; j<incexe->name_list.size(); j++) {
141 Dmsg1(100, "F %s\n", (char *)incexe->name_list.get(j));
142 char *fname = (char *)incexe->name_list.get(j);
143 if (find_one_file(jcr, ff, our_callback, his_pkt, fname, (dev_t)-1, 1) == 0) {
144 return 0; /* error return */
152 static bool accept_file(FF_PKT *ff)
156 findFILESET *fileset = ff->fileset;
157 findINCEXE *incexe = fileset->incexe;
159 for (j=0; j<incexe->opts_list.size(); j++) {
160 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
161 ff->flags = fo->flags;
162 ff->GZIP_level = fo->GZIP_level;
163 ff->reader = fo->reader;
164 ff->writer = fo->writer;
165 ff->fstypes = fo->fstype;
166 ic = (ff->flags & FO_IGNORECASE) ? FNM_CASEFOLD : 0;
167 if (S_ISDIR(ff->statp.st_mode)) {
168 for (k=0; k<fo->wilddir.size(); k++) {
169 if (fnmatch((char *)fo->wilddir.get(k), ff->fname, fnmode|ic) == 0) {
170 if (ff->flags & FO_EXCLUDE) {
171 Dmsg2(100, "Exclude wilddir: %s file=%s\n", (char *)fo->wilddir.get(k),
173 return false; /* reject file */
175 return true; /* accept file */
179 for (k=0; k<fo->wildfile.size(); k++) {
180 if (fnmatch((char *)fo->wildfile.get(k), ff->fname, fnmode|ic) == 0) {
181 if (ff->flags & FO_EXCLUDE) {
182 Dmsg2(100, "Exclude wildfile: %s file=%s\n", (char *)fo->wildfile.get(k),
184 return false; /* reject file */
186 return true; /* accept file */
190 for (k=0; k<fo->wild.size(); k++) {
191 if (fnmatch((char *)fo->wild.get(k), ff->fname, fnmode|ic) == 0) {
192 if (ff->flags & FO_EXCLUDE) {
193 Dmsg2(100, "Exclude wild: %s file=%s\n", (char *)fo->wild.get(k),
195 return false; /* reject file */
197 return true; /* accept file */
201 if (S_ISDIR(ff->statp.st_mode)) {
202 for (k=0; k<fo->regexdir.size(); k++) {
203 const int nmatch = 30;
204 regmatch_t pmatch[nmatch];
205 if (regexec((regex_t *)fo->regexdir.get(k), ff->fname, nmatch, pmatch, 0) == 0) {
206 if (ff->flags & FO_EXCLUDE) {
207 return false; /* reject file */
209 return true; /* accept file */
213 for (k=0; k<fo->regexfile.size(); k++) {
214 const int nmatch = 30;
215 regmatch_t pmatch[nmatch];
216 if (regexec((regex_t *)fo->regexfile.get(k), ff->fname, nmatch, pmatch, 0) == 0) {
217 if (ff->flags & FO_EXCLUDE) {
218 return false; /* reject file */
220 return true; /* accept file */
224 for (k=0; k<fo->regex.size(); k++) {
225 const int nmatch = 30;
226 regmatch_t pmatch[nmatch];
227 if (regexec((regex_t *)fo->regex.get(k), ff->fname, nmatch, pmatch, 0) == 0) {
228 if (ff->flags & FO_EXCLUDE) {
229 return false; /* reject file */
231 return true; /* accept file */
237 for (i=0; i<fileset->exclude_list.size(); i++) {
238 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
239 for (j=0; j<incexe->opts_list.size(); j++) {
240 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
241 ic = (fo->flags & FO_IGNORECASE) ? FNM_CASEFOLD : 0;
242 for (k=0; k<fo->wild.size(); k++) {
243 if (fnmatch((char *)fo->wild.get(k), ff->fname, fnmode|ic) == 0) {
244 Dmsg1(100, "Reject wild1: %s\n", ff->fname);
245 return false; /* reject file */
249 ic = (incexe->current_opts != NULL && incexe->current_opts->flags & FO_IGNORECASE)
251 for (j=0; j<incexe->name_list.size(); j++) {
252 if (fnmatch((char *)incexe->name_list.get(j), ff->fname, fnmode|ic) == 0) {
253 Dmsg1(100, "Reject wild2: %s\n", ff->fname);
254 return false; /* reject file */
262 * The code comes here for each file examined.
263 * We filter the files, then call the user's callback if
264 * the file is included.
266 static int our_callback(FF_PKT *ff, void *hpkt)
278 // return ff->callback(ff, hpkt);
280 /* These items can be filtered */
291 if (accept_file(ff)) {
292 return ff->callback(ff, hpkt);
294 Dmsg1(100, "Skip file %s\n", ff->fname);
295 return -1; /* ignore this file */
299 Dmsg1(000, "Unknown FT code %d\n", ff->type);
306 * Terminate find_files() and release
307 * all allocated memory
310 term_find_files(FF_PKT *ff)
314 free_pool_memory(ff->sys_fname);
315 hard_links = term_find_one(ff);