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 bstrncat(ff->VerifyOpts, fo->VerifyOpts, sizeof(ff->VerifyOpts));
141 for (j=0; j<incexe->name_list.size(); j++) {
142 Dmsg1(100, "F %s\n", (char *)incexe->name_list.get(j));
143 char *fname = (char *)incexe->name_list.get(j);
144 if (find_one_file(jcr, ff, our_callback, his_pkt, fname, (dev_t)-1, 1) == 0) {
145 return 0; /* error return */
150 struct s_included_file *inc = NULL;
152 /* This is the old deprecated way */
153 while (!job_canceled(jcr) && (inc = get_next_included_file(ff, inc))) {
154 /* Copy options for this file */
155 bstrncat(ff->VerifyOpts, inc->VerifyOpts, sizeof(ff->VerifyOpts));
156 Dmsg1(100, "find_files: file=%s\n", inc->fname);
157 if (!file_is_excluded(ff, inc->fname)) {
158 if (find_one_file(jcr, ff, callback, his_pkt, inc->fname, (dev_t)-1, 1) ==0) {
159 return 0; /* error return */
167 static bool accept_file(FF_PKT *ff)
171 findFILESET *fileset = ff->fileset;
172 findINCEXE *incexe = fileset->incexe;
174 for (j=0; j<incexe->opts_list.size(); j++) {
175 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
176 ff->flags = fo->flags;
177 ff->GZIP_level = fo->GZIP_level;
178 ff->reader = fo->reader;
179 ff->writer = fo->writer;
180 ff->fstypes = &(fo->fstype);
181 ic = (ff->flags & FO_IGNORECASE) ? FNM_CASEFOLD : 0;
182 for (k=0; k<fo->wild.size(); k++) {
183 if (fnmatch((char *)fo->wild.get(k), ff->fname, fnmode|ic) == 0) {
184 if (ff->flags & FO_EXCLUDE) {
185 Dmsg2(100, "Exclude wild: %s file=%s\n", (char *)fo->wild.get(k),
187 return false; /* reject file */
189 return true; /* accept file */
193 for (k=0; k<fo->regex.size(); k++) {
194 const int nmatch = 30;
195 regmatch_t pmatch[nmatch];
196 if (regexec((regex_t *)fo->regex.get(k), ff->fname, nmatch, pmatch, 0) == 0) {
197 if (ff->flags & FO_EXCLUDE) {
198 return false; /* reject file */
200 return true; /* accept file */
206 for (i=0; i<fileset->exclude_list.size(); i++) {
207 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
208 for (j=0; j<incexe->opts_list.size(); j++) {
209 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
210 ic = (fo->flags & FO_IGNORECASE) ? FNM_CASEFOLD : 0;
211 for (k=0; k<fo->wild.size(); k++) {
212 if (fnmatch((char *)fo->wild.get(k), ff->fname, fnmode|ic) == 0) {
213 Dmsg1(100, "Reject wild1: %s\n", ff->fname);
214 return false; /* reject file */
218 ic = (incexe->current_opts != NULL && incexe->current_opts->flags & FO_IGNORECASE)
220 for (j=0; j<incexe->name_list.size(); j++) {
221 if (fnmatch((char *)incexe->name_list.get(j), ff->fname, fnmode|ic) == 0) {
222 Dmsg1(100, "Reject wild2: %s\n", ff->fname);
223 return false; /* reject file */
231 * The code comes here for each file examined.
232 * We filter the files, then call the user's callback if
233 * the file is included.
235 static int our_callback(FF_PKT *ff, void *hpkt)
247 // return ff->callback(ff, hpkt);
249 /* These items can be filtered */
260 if (accept_file(ff)) {
261 return ff->callback(ff, hpkt);
263 Dmsg1(100, "Skip file %s\n", ff->fname);
264 return -1; /* ignore this file */
268 Dmsg1(000, "Unknown FT code %d\n", ff->type);
275 * Terminate find_files() and release
276 * all allocated memory
279 term_find_files(FF_PKT *ff)
283 term_include_exclude_files(ff);
284 free_pool_memory(ff->sys_fname);
285 hard_links = term_find_one(ff);