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 /* Global variables */
30 static int num_files = 0;
31 static int max_file_len = 0;
32 static int max_path_len = 0;
33 static int trunc_fname = 0;
34 static int trunc_path = 0;
39 static int print_file(FF_PKT *ff, void *pkt);
40 static void count_files(FF_PKT *ff);
46 "Usage: testfind [-d debug_level] [-] [pattern1 ...]\n"
47 " -a print extended attributes (Win32 debug)\n"
48 " -dnn set debug level to nn\n"
49 " -e specify file of exclude patterns\n"
50 " -i specify file of include patterns\n"
51 " - read pattern(s) from stdin\n"
52 " -? print this message.\n"
54 "Patterns are file inclusion -- normally directories.\n"
55 "Debug level >= 1 prints each file found.\n"
56 "Debug level >= 10 prints path/file for catalog.\n"
57 "Errors always printed.\n"
58 "Files/paths truncated is number with len > 255.\n"
59 "Truncation is only in catalog.\n"
67 main (int argc, char *const *argv)
71 int i, ch, hard_links;
76 while ((ch = getopt(argc, argv, "ad:e:i:?")) != -1) {
78 case 'a': /* print extended attributes *debug* */
82 case 'd': /* set debug level */
83 debug_level = atoi(optarg);
84 if (debug_level <= 0) {
89 case 'e': /* exclude patterns */
93 case 'i': /* include patterns */
106 jcr = new_jcr(sizeof(JCR), NULL);
108 ff = init_find_files();
109 if (argc == 0 && !inc) {
110 add_fname_to_include_list(ff, 0, "/"); /* default to / */
112 for (i=0; i < argc; i++) {
113 if (strcmp(argv[i], "-") == 0) {
114 while (fgets(name, sizeof(name)-1, stdin)) {
115 strip_trailing_junk(name);
116 add_fname_to_include_list(ff, 0, name);
120 add_fname_to_include_list(ff, 0, argv[i]);
124 fd = fopen(inc, "r");
126 printf("Could not open include file: %s\n", inc);
129 while (fgets(name, sizeof(name)-1, fd)) {
130 strip_trailing_junk(name);
131 add_fname_to_include_list(ff, 0, name);
137 fd = fopen(exc, "r");
139 printf("Could not open exclude file: %s\n", exc);
142 while (fgets(name, sizeof(name)-1, fd)) {
143 strip_trailing_junk(name);
144 add_fname_to_exclude_list(ff, name);
148 find_files(jcr, ff, print_file, NULL);
149 hard_links = term_find_files(ff);
153 Max file length: %d\n\
154 Max path length: %d\n\
155 Files truncated: %d\n\
156 Paths truncated: %d\n\
158 num_files, max_file_len, max_path_len,
159 trunc_fname, trunc_path, hard_links);
167 static int print_file(FF_PKT *ff, void *pkt)
172 if (debug_level == 1) {
173 printf("%s\n", ff->fname);
174 } else if (debug_level > 1) {
175 printf("Lnka: %s -> %s\n", ff->fname, ff->link);
179 if (debug_level == 1) {
180 printf("%s\n", ff->fname);
181 } else if (debug_level > 1) {
182 printf("Empty: %s\n", ff->fname);
187 if (debug_level == 1) {
188 printf("%s\n", ff->fname);
189 } else if (debug_level > 1) {
190 printf("Reg: %s\n", ff->fname);
195 if (debug_level == 1) {
196 printf("%s\n", ff->fname);
197 } else if (debug_level > 1) {
198 printf("Lnk: %s -> %s\n", ff->fname, ff->link);
203 if (debug_level == 1) {
204 printf("%s\n", ff->fname);
205 } else if (debug_level > 1) {
206 printf("Dir: %s\n", ff->fname);
211 if (debug_level == 1) {
212 printf("%s\n", ff->fname);
213 } else if (debug_level > 1) {
214 printf("Spec: %s\n", ff->fname);
219 printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno));
222 printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno));
225 printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno));
228 printf(_("Skip: File not saved. No change. %s\n"), ff->fname);
231 printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
234 printf(_("Recursion turned off. Directory not entered. %s\n"), ff->fname);
237 printf(_("Skip: File system change prohibited. Directory not entered. %s\n"), ff->fname);
240 printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
243 printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname);
248 encode_attribsEx(NULL, attr, ff);
250 printf("AttrEx=%s\n", attr);
252 // set_attribsEx(NULL, ff->fname, NULL, NULL, ff->type, attr);
257 static void count_files(FF_PKT *ar)
261 char file[MAXSTRING];
262 char spath[MAXSTRING];
266 /* Find path without the filename.
267 * I.e. everything after the last / is a "filename".
268 * OK, maybe it is a directory name, but we treat it like
269 * a filename. If we don't find a / then the whole name
270 * must be a path name (e.g. c:).
272 for (p=l=ar->fname; *p; p++) {
274 l = p; /* set pos of last slash */
277 if (*l == '/') { /* did we find a slash? */
278 l++; /* yes, point to filename */
279 } else { /* no, whole thing must be path name */
283 /* If filename doesn't exist (i.e. root directory), we
284 * simply create a blank name consisting of a single
285 * space. This makes handling zero length filenames
289 if (fnl > max_file_len) {
293 printf(_("===== Filename truncated to 255 chars: %s\n"), l);
298 strncpy(file, l, fnl); /* copy filename */
301 file[0] = ' '; /* blank filename */
306 if (pnl > max_path_len) {
310 printf(_("========== Path name truncated to 255 chars: %s\n"), ar->fname);
314 strncpy(spath, ar->fname, pnl);
319 printf(_("========== Path length is zero. File=%s\n"), ar->fname);
321 if (debug_level >= 10) {
322 printf("Path: %s\n", spath);
323 printf("File: %s\n", file);