2 * Test program for find files
6 Copyright (C) 2000-2003 Kern Sibbald and John Walker
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public
19 License along with this program; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26 #include "findlib/find.h"
29 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
36 /* Global variables */
37 static int num_files = 0;
38 static int max_file_len = 0;
39 static int max_path_len = 0;
40 static int trunc_fname = 0;
41 static int trunc_path = 0;
46 static int print_file(FF_PKT *ff, void *pkt, bool);
47 static void count_files(FF_PKT *ff);
53 "Usage: testfind [-d debug_level] [-] [pattern1 ...]\n"
54 " -a print extended attributes (Win32 debug)\n"
55 " -dnn set debug level to nn\n"
56 " -e specify file of exclude patterns\n"
57 " -i specify file of include patterns\n"
58 " - read pattern(s) from stdin\n"
59 " -? print this message.\n"
61 "Patterns are used for file inclusion -- normally directories.\n"
62 "Debug level >= 1 prints each file found.\n"
63 "Debug level >= 10 prints path/file for catalog.\n"
64 "Errors are always printed.\n"
65 "Files/paths truncated is the number of files/paths with len > 255.\n"
66 "Truncation is only in the catalog.\n"
74 main (int argc, char *const *argv)
78 int i, ch, hard_links;
83 while ((ch = getopt(argc, argv, "ad:e:i:?")) != -1) {
85 case 'a': /* print extended attributes *debug* */
89 case 'd': /* set debug level */
90 debug_level = atoi(optarg);
91 if (debug_level <= 0) {
96 case 'e': /* exclude patterns */
100 case 'i': /* include patterns */
113 jcr = new_jcr(sizeof(JCR), NULL);
115 ff = init_find_files();
116 if (argc == 0 && !inc) {
117 add_fname_to_include_list(ff, 0, "/"); /* default to / */
119 for (i=0; i < argc; i++) {
120 if (strcmp(argv[i], "-") == 0) {
121 while (fgets(name, sizeof(name)-1, stdin)) {
122 strip_trailing_junk(name);
123 add_fname_to_include_list(ff, 0, name);
127 add_fname_to_include_list(ff, 0, argv[i]);
131 fd = fopen(inc, "r");
133 printf("Could not open include file: %s\n", inc);
136 while (fgets(name, sizeof(name)-1, fd)) {
137 strip_trailing_junk(name);
138 add_fname_to_include_list(ff, 0, name);
144 fd = fopen(exc, "r");
146 printf("Could not open exclude file: %s\n", exc);
149 while (fgets(name, sizeof(name)-1, fd)) {
150 strip_trailing_junk(name);
151 add_fname_to_exclude_list(ff, name);
155 match_files(jcr, ff, print_file, NULL);
156 term_include_exclude_files(ff);
157 hard_links = term_find_files(ff);
161 "Max file length: %d\n"
162 "Max path length: %d\n"
163 "Files truncated: %d\n"
164 "Paths truncated: %d\n"
165 "Hard links : %d\n"),
166 num_files, max_file_len, max_path_len,
167 trunc_fname, trunc_path, hard_links);
175 static int print_file(FF_PKT *ff, void *pkt, bool top_level)
180 if (debug_level == 1) {
181 printf("%s\n", ff->fname);
182 } else if (debug_level > 1) {
183 printf("Lnka: %s -> %s\n", ff->fname, ff->link);
187 if (debug_level == 1) {
188 printf("%s\n", ff->fname);
189 } else if (debug_level > 1) {
190 printf("Empty: %s\n", ff->fname);
195 if (debug_level == 1) {
196 printf("%s\n", ff->fname);
197 } else if (debug_level > 1) {
198 printf("Reg: %s\n", ff->fname);
203 if (debug_level == 1) {
204 printf("%s\n", ff->fname);
205 } else if (debug_level > 1) {
206 printf("Lnk: %s -> %s\n", ff->fname, ff->link);
217 char errmsg[100] = "";
218 if (ff->type == FT_NORECURSE) {
219 bstrncpy(errmsg, "\t[will not descend: recursion turned off]", sizeof(errmsg));
220 } else if (ff->type == FT_NOFSCHG) {
221 bstrncpy(errmsg, "\t[will not descend: file system change not allowed]", sizeof(errmsg));
222 } else if (ff->type == FT_INVALIDFS) {
223 bstrncpy(errmsg, "\t[will not descend: disallowed file system]", sizeof(errmsg));
225 printf("%s%s%s\n", (debug_level > 1 ? "Dir: " : ""), ff->fname, errmsg);
227 ff->type = FT_DIREND;
231 if (debug_level == 1) {
232 printf("%s\n", ff->fname);
233 } else if (debug_level > 1) {
234 printf("Spec: %s\n", ff->fname);
239 printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno));
242 printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno));
245 printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno));
248 printf(_("Skip: File not saved. No change. %s\n"), ff->fname);
251 printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
254 printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
257 printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname);
262 encode_attribsEx(NULL, attr, ff);
264 printf("AttrEx=%s\n", attr);
266 // set_attribsEx(NULL, ff->fname, NULL, NULL, ff->type, attr);
271 static void count_files(FF_PKT *ar)
275 char file[MAXSTRING];
276 char spath[MAXSTRING];
280 /* Find path without the filename.
281 * I.e. everything after the last / is a "filename".
282 * OK, maybe it is a directory name, but we treat it like
283 * a filename. If we don't find a / then the whole name
284 * must be a path name (e.g. c:).
286 for (p=l=ar->fname; *p; p++) {
288 l = p; /* set pos of last slash */
291 if (*l == '/') { /* did we find a slash? */
292 l++; /* yes, point to filename */
293 } else { /* no, whole thing must be path name */
297 /* If filename doesn't exist (i.e. root directory), we
298 * simply create a blank name consisting of a single
299 * space. This makes handling zero length filenames
303 if (fnl > max_file_len) {
307 printf(_("===== Filename truncated to 255 chars: %s\n"), l);
312 strncpy(file, l, fnl); /* copy filename */
315 file[0] = ' '; /* blank filename */
320 if (pnl > max_path_len) {
324 printf(_("========== Path name truncated to 255 chars: %s\n"), ar->fname);
328 strncpy(spath, ar->fname, pnl);
333 printf(_("========== Path length is zero. File=%s\n"), ar->fname);
335 if (debug_level >= 10) {
336 printf("Path: %s\n", spath);
337 printf("File: %s\n", file);