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-2004 Kern Sibbald and John Walker
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 */
40 /* ****FIXME**** debug until stable */
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);
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 init_include_exclude_files(ff); /* init lists */
68 /* Get system path and filename maximum lengths */
69 path_max = pathconf(".", _PC_PATH_MAX);
70 if (path_max < 1024) {
74 name_max = pathconf(".", _PC_NAME_MAX);
75 if (name_max < 1024) {
78 path_max++; /* add for EOS */
79 name_max++; /* add for EOS */
81 Dmsg1(100, "init_find_files ff=%p\n", ff);
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
91 set_find_options(FF_PKT *ff, int incremental, time_t save_time)
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");
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
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
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
117 find_files(JCR *jcr, FF_PKT *ff, int callback(FF_PKT *ff_pkt, void *hpkt), void *his_pkt)
119 ff->callback = callback;
121 /* This is the new way */
122 findFILESET *fileset = ff->fileset;
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;
132 * By setting all options, we in effect or the global options
133 * which is what we want.
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));
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 */
151 struct s_included_file *inc = NULL;
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 */
168 static bool accept_file(FF_PKT *ff)
172 findFILESET *fileset = ff->fileset;
173 findINCEXE *incexe = fileset->incexe;
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),
188 return false; /* reject file */
190 return true; /* accept file */
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 */
201 return true; /* accept file */
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 */
219 ic = (incexe->current_opts != NULL && incexe->current_opts->flags & FO_IGNORECASE)
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 */
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.
236 static int our_callback(FF_PKT *ff, void *hpkt)
248 // return ff->callback(ff, hpkt);
250 /* These items can be filtered */
261 if (accept_file(ff)) {
262 return ff->callback(ff, hpkt);
264 Dmsg1(100, "Skip file %s\n", ff->fname);
265 return -1; /* ignore this file */
269 Dmsg1(000, "Unknown FT code %d\n", ff->type);
276 * Terminate find_files() and release
277 * all allocated memory
280 term_find_files(FF_PKT *ff)
284 term_include_exclude_files(ff);
285 free_pool_memory(ff->sys_fname);
286 hard_links = term_find_one(ff);