2 * Main routine for finding files on a file system.
3 * The heart of the work is done in find_one.c
8 Copyright (C) 2000-2004 Kern Sibbald and John Walker
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public
21 License along with this program; if not, write to the Free
22 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32 int32_t name_max; /* filename max length */
33 int32_t path_max; /* path name max length */
36 /* ****FIXME**** debug until stable */
38 #define bmalloc(x) sm_malloc(__FILE__, __LINE__, x)
39 static int our_callback(FF_PKT *ff, void *hpkt);
40 static bool accept_file(FF_PKT *ff);
44 * Initialize the find files "global" variables
46 FF_PKT *init_find_files()
50 ff = (FF_PKT *)bmalloc(sizeof(FF_PKT));
51 memset(ff, 0, sizeof(FF_PKT));
53 ff->sys_fname = get_pool_memory(PM_FNAME);
55 init_include_exclude_files(ff); /* init lists */
57 /* Get system path and filename maximum lengths */
58 path_max = pathconf(".", _PC_PATH_MAX);
59 if (path_max < 1024) {
63 name_max = pathconf(".", _PC_NAME_MAX);
64 if (name_max < 1024) {
67 path_max++; /* add for EOS */
68 name_max++; /* add for EOS */
70 Dmsg1(100, "init_find_files ff=%p\n", ff);
75 * Set find_files options. For the moment, we only
76 * provide for full/incremental saves, and setting
77 * of save_time. For additional options, see above
80 set_find_options(FF_PKT *ff, int incremental, time_t save_time)
82 Dmsg0(100, "Enter set_find_options()\n");
83 ff->incremental = incremental;
84 ff->save_time = save_time;
85 Dmsg0(100, "Leave set_find_options()\n");
90 * Find all specified files (determined by calls to name_add()
91 * This routine calls the (handle_file) subroutine with all
92 * sorts of good information for the final disposition of
95 * Call this subroutine with a callback subroutine as the first
96 * argument and a packet as the second argument, this packet
97 * will be passed back to the callback subroutine as the last
100 * The callback subroutine gets called with:
101 * arg1 -- the FF_PKT containing filename, link, stat, ftype, flags, etc
102 * arg2 -- the user supplied packet
106 find_files(JCR *jcr, FF_PKT *ff, int callback(FF_PKT *ff_pkt, void *hpkt), void *his_pkt)
108 ff->callback = callback;
110 /* This is the new way */
111 findFILESET *fileset = ff->fileset;
115 ff->VerifyOpts[0] = 0;
116 for (i=0; i<fileset->include_list.size(); i++) {
117 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
118 fileset->incexe = incexe;
120 * By setting all options, we in effect or the global options
121 * which is what we want.
123 for (j=0; j<incexe->opts_list.size(); j++) {
124 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
125 ff->flags |= fo->flags;
126 ff->GZIP_level = fo->GZIP_level;
127 bstrncpy(ff->VerifyOpts, fo->VerifyOpts, sizeof(ff->VerifyOpts));
129 for (j=0; j<incexe->name_list.size(); j++) {
130 Dmsg1(400, "F %s\n", (char *)incexe->name_list.get(j));
131 char *fname = (char *)incexe->name_list.get(j);
132 if (find_one_file(jcr, ff, our_callback, his_pkt, fname, (dev_t)-1, 1) == 0) {
133 return 0; /* error return */
138 struct s_included_file *inc = NULL;
140 /* This is the old deprecated way */
141 while (!job_canceled(jcr) && (inc = get_next_included_file(ff, inc))) {
142 /* Copy options for this file */
143 bstrncpy(ff->VerifyOpts, inc->VerifyOpts, sizeof(ff->VerifyOpts));
144 Dmsg1(50, "find_files: file=%s\n", inc->fname);
145 if (!file_is_excluded(ff, inc->fname)) {
146 if (find_one_file(jcr, ff, callback, his_pkt, inc->fname, (dev_t)-1, 1) ==0) {
147 return 0; /* error return */
155 static bool accept_file(FF_PKT *ff)
158 findFILESET *fileset = ff->fileset;
159 findINCEXE *incexe = fileset->incexe;
161 for (j=0; j<incexe->opts_list.size(); j++) {
162 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
163 for (k=0; k<fo->wild.size(); k++) {
164 if (fnmatch((char *)fo->wild.get(k), ff->fname, 0) == 0) {
165 ff->flags = fo->flags;
166 ff->GZIP_level = fo->GZIP_level;
167 if (ff->flags & FO_EXCLUDE) {
168 return false; /* reject file */
170 return true; /* accept file */
175 for (i=0; i<fileset->exclude_list.size(); i++) {
176 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
177 for (j=0; j<incexe->opts_list.size(); j++) {
178 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
179 for (k=0; k<fo->wild.size(); k++) {
180 if (fnmatch((char *)fo->wild.get(k), ff->fname, 0) == 0) {
181 Dmsg1(400, "Reject wild1: %s\n", ff->fname);
182 return false; /* reject file */
186 for (j=0; j<incexe->name_list.size(); j++) {
187 if (fnmatch((char *)incexe->name_list.get(j), ff->fname, 0) == 0) {
188 Dmsg1(400, "Reject wild2: %s\n", ff->fname);
189 return false; /* reject file */
197 * The code comes here for each file examined.
198 * We filter the files, then call the user's callback if
199 * the file is included.
201 static int our_callback(FF_PKT *ff, void *hpkt)
213 return ff->callback(ff, hpkt);
215 /* These items can be filtered */
225 if (accept_file(ff)) {
226 return ff->callback(ff, hpkt);
228 Dmsg1(100, "Skip file %s\n", ff->fname);
229 return -1; /* ignore this file */
233 Dmsg1(000, "Unknown FT code %d\n", ff->type);
240 * Terminate find_files() and release
241 * all allocated memory
244 term_find_files(FF_PKT *ff)
248 term_include_exclude_files(ff);
249 free_pool_memory(ff->sys_fname);
250 hard_links = term_find_one(ff);