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 " - read pattern(s) from stdin\n"
50 " -? print this message.\n"
52 "Patterns are file inclusion -- normally directories.\n"
53 "Debug level >= 1 prints each file found.\n"
54 "Debug level >= 10 prints path/file for catalog.\n"
55 "Errors always printed.\n"
56 "Files/paths truncated is number with len > 255.\n"
57 "Truncation is only in catalog.\n"
65 main (int argc, char *const *argv)
69 int i, ch, hard_links;
71 while ((ch = getopt(argc, argv, "ad:?")) != -1) {
73 case 'a': /* print extended attributes *debug* */
77 case 'd': /* set debug level */
78 debug_level = atoi(optarg);
79 if (debug_level <= 0) {
93 jcr = new_jcr(sizeof(JCR), NULL);
95 ff = init_find_files();
97 add_fname_to_include_list(ff, 0, "/"); /* default to / */
99 for (i=0; i < argc; i++) {
100 if (strcmp(argv[i], "-") == 0) {
101 while (fgets(name, sizeof(name)-1, stdin)) {
102 strip_trailing_junk(name);
103 add_fname_to_include_list(ff, 0, name);
107 add_fname_to_include_list(ff, 0, argv[i]);
111 find_files(jcr, ff, print_file, NULL);
112 hard_links = term_find_files(ff);
116 Max file length: %d\n\
117 Max path length: %d\n\
118 Files truncated: %d\n\
119 Paths truncated: %d\n\
121 num_files, max_file_len, max_path_len,
122 trunc_fname, trunc_path, hard_links);
130 static int print_file(FF_PKT *ff, void *pkt)
135 if (debug_level == 1) {
136 printf("%s\n", ff->fname);
137 } else if (debug_level > 1) {
138 printf("Lnka: %s -> %s\n", ff->fname, ff->link);
142 if (debug_level == 1) {
143 printf("%s\n", ff->fname);
144 } else if (debug_level > 1) {
145 printf("Empty: %s\n", ff->fname);
150 if (debug_level == 1) {
151 printf("%s\n", ff->fname);
152 } else if (debug_level > 1) {
153 printf("Reg: %s\n", ff->fname);
158 if (debug_level == 1) {
159 printf("%s\n", ff->fname);
160 } else if (debug_level > 1) {
161 printf("Lnk: %s -> %s\n", ff->fname, ff->link);
166 if (debug_level == 1) {
167 printf("%s\n", ff->fname);
168 } else if (debug_level > 1) {
169 printf("Dir: %s\n", ff->fname);
174 if (debug_level == 1) {
175 printf("%s\n", ff->fname);
176 } else if (debug_level > 1) {
177 printf("Spec: %s\n", ff->fname);
182 printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno));
185 printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno));
188 printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno));
191 printf(_("Skip: File not saved. No change. %s\n"), ff->fname);
194 printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
197 printf(_("Recursion turned off. Directory not entered. %s\n"), ff->fname);
200 printf(_("Skip: File system change prohibited. Directory not entered. %s\n"), ff->fname);
203 printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
206 printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname);
211 encode_attribsEx(NULL, attr, ff);
213 printf("AttrEx=%s\n", attr);
215 // set_attribsEx(NULL, ff->fname, NULL, NULL, ff->type, attr);
220 static void count_files(FF_PKT *ar)
224 char file[MAXSTRING];
225 char spath[MAXSTRING];
229 /* Find path without the filename.
230 * I.e. everything after the last / is a "filename".
231 * OK, maybe it is a directory name, but we treat it like
232 * a filename. If we don't find a / then the whole name
233 * must be a path name (e.g. c:).
235 for (p=l=ar->fname; *p; p++) {
237 l = p; /* set pos of last slash */
240 if (*l == '/') { /* did we find a slash? */
241 l++; /* yes, point to filename */
242 } else { /* no, whole thing must be path name */
246 /* If filename doesn't exist (i.e. root directory), we
247 * simply create a blank name consisting of a single
248 * space. This makes handling zero length filenames
252 if (fnl > max_file_len) {
256 printf(_("===== Filename truncated to 255 chars: %s\n"), l);
261 strncpy(file, l, fnl); /* copy filename */
264 file[0] = ' '; /* blank filename */
269 if (pnl > max_path_len) {
273 printf(_("========== Path name truncated to 255 chars: %s\n"), ar->fname);
277 strncpy(spath, ar->fname, pnl);
282 printf(_("========== Path length is zero. File=%s\n"), ar->fname);
284 if (debug_level >= 10) {
285 printf("Path: %s\n", spath);
286 printf("File: %s\n", file);