]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/testfind.c
Apply Preben 'Peppe' Guldberg <peppe@wielders.org>
[bacula/bacula] / bacula / src / tools / testfind.c
1 /*
2  * Test program for find files
3  */
4
5 /*
6    Copyright (C) 2000-2003 Kern Sibbald and John Walker
7
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.
12
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.
17
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,
21    MA 02111-1307, USA.
22
23  */
24
25 #include "bacula.h"
26 #include "findlib/find.h"
27
28
29 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
30 int win32_client = 1;
31 #else
32 int win32_client = 0;
33 #endif
34
35
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;
42 static int attrs = 0;
43
44 static JCR *jcr;
45
46 static int print_file(FF_PKT *ff, void *pkt);
47 static void count_files(FF_PKT *ff);
48
49 static void usage()
50 {
51    fprintf(stderr, _(
52 "\n"
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"
60 "\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"
67 "\n"));
68
69    exit(1);
70 }
71
72
73 int
74 main (int argc, char *const *argv)
75 {
76    FF_PKT *ff;
77    char name[1000];
78    int i, ch, hard_links;
79    char *inc = NULL;
80    char *exc = NULL;
81    FILE *fd;
82
83    while ((ch = getopt(argc, argv, "ad:e:i:?")) != -1) {
84       switch (ch) {
85          case 'a':                    /* print extended attributes *debug* */
86             attrs = 1;
87             break;
88
89          case 'd':                    /* set debug level */
90             debug_level = atoi(optarg);
91             if (debug_level <= 0) {
92                debug_level = 1;
93             }
94             break;
95
96          case 'e':                    /* exclude patterns */
97             exc = optarg;
98             break;
99
100          case 'i':                    /* include patterns */
101             inc = optarg;
102             break;
103
104          case '?':
105          default:
106             usage();
107
108       }
109    }
110    argc -= optind;
111    argv += optind;
112
113    jcr = new_jcr(sizeof(JCR), NULL);
114
115    ff = init_find_files();
116    if (argc == 0 && !inc) {
117       add_fname_to_include_list(ff, 0, "/"); /* default to / */
118    } else {
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);
124               }
125               continue;
126          }
127          add_fname_to_include_list(ff, 0, argv[i]);
128       }
129    }
130    if (inc) {
131       fd = fopen(inc, "r");
132       if (!fd) {
133          printf("Could not open include file: %s\n", inc);
134          exit(1);
135       }
136       while (fgets(name, sizeof(name)-1, fd)) {
137          strip_trailing_junk(name);
138          add_fname_to_include_list(ff, 0, name);
139       }
140       fclose(fd);
141    }
142
143    if (exc) {
144       fd = fopen(exc, "r");
145       if (!fd) {
146          printf("Could not open exclude file: %s\n", exc);
147          exit(1);
148       }
149       while (fgets(name, sizeof(name)-1, fd)) {
150          strip_trailing_junk(name);
151          add_fname_to_exclude_list(ff, name);
152       }
153       fclose(fd);
154    }
155    find_files(jcr, ff, print_file, NULL);
156    hard_links = term_find_files(ff);
157
158    printf(_(""
159 "Total files    : %d\n"
160 "Max file length: %d\n"
161 "Max path length: %d\n"
162 "Files truncated: %d\n"
163 "Paths truncated: %d\n"
164 "Hard links     : %d\n"),
165      num_files, max_file_len, max_path_len,
166      trunc_fname, trunc_path, hard_links);
167
168   free_jcr(jcr);
169   close_memory_pool();
170   sm_dump(false);
171   exit(0);
172 }
173
174 static int print_file(FF_PKT *ff, void *pkt)
175 {
176
177    switch (ff->type) {
178    case FT_LNKSAVED:
179       if (debug_level == 1) {
180          printf("%s\n", ff->fname);
181       } else if (debug_level > 1) {
182          printf("Lnka: %s -> %s\n", ff->fname, ff->link);
183       }
184       break;
185    case FT_REGE:
186       if (debug_level == 1) {
187          printf("%s\n", ff->fname);
188       } else if (debug_level > 1) {
189          printf("Empty: %s\n", ff->fname);
190       }
191       count_files(ff);
192       break;
193    case FT_REG:
194       if (debug_level == 1) {
195          printf("%s\n", ff->fname);
196       } else if (debug_level > 1) {
197          printf("Reg: %s\n", ff->fname);
198       }
199       count_files(ff);
200       break;
201    case FT_LNK:
202       if (debug_level == 1) {
203          printf("%s\n", ff->fname);
204       } else if (debug_level > 1) {
205          printf("Lnk: %s -> %s\n", ff->fname, ff->link);
206       }
207       count_files(ff);
208       break;
209    case FT_DIRBEGIN:
210       return 1;
211    case FT_NORECURSE:
212    case FT_NOFSCHG:
213    case FT_INVALIDFS:
214    case FT_DIREND:
215       if (debug_level) {
216          char errmsg[100] = "";
217          if (ff->type == FT_NORECURSE) {
218             bstrncpy(errmsg, "\t[will not descend: recursion turned off]", sizeof(errmsg));
219          } else if (ff->type == FT_NOFSCHG) {
220             bstrncpy(errmsg, "\t[will not descend: file system change not allowed]", sizeof(errmsg));
221          } else if (ff->type == FT_INVALIDFS) {
222             bstrncpy(errmsg, "\t[will not descend: disallowed file system]", sizeof(errmsg));
223          }
224          printf("%s%s%s\n", (debug_level > 1 ? "Dir: " : ""), ff->fname, errmsg);
225       }
226       ff->type = FT_DIREND;
227       count_files(ff);
228       break;
229    case FT_SPEC:
230       if (debug_level == 1) {
231          printf("%s\n", ff->fname);
232       } else if (debug_level > 1) {
233          printf("Spec: %s\n", ff->fname);
234       }
235       count_files(ff);
236       break;
237    case FT_NOACCESS:
238       printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno));
239       break;
240    case FT_NOFOLLOW:
241       printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno));
242       break;
243    case FT_NOSTAT:
244       printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno));
245       break;
246    case FT_NOCHG:
247       printf(_("Skip: File not saved. No change. %s\n"), ff->fname);
248       break;
249    case FT_ISARCH:
250       printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
251       break;
252    case FT_NOOPEN:
253       printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
254       break;
255    default:
256       printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname);
257       break;
258    }
259    if (attrs) {
260       char attr[200];
261       encode_attribsEx(NULL, attr, ff);
262       if (*attr != 0) {
263          printf("AttrEx=%s\n", attr);
264       }
265 //    set_attribsEx(NULL, ff->fname, NULL, NULL, ff->type, attr);
266    }
267    return 1;
268 }
269
270 static void count_files(FF_PKT *ar)
271 {
272    int fnl, pnl;
273    char *l, *p;
274    char file[MAXSTRING];
275    char spath[MAXSTRING];
276
277    num_files++;
278
279    /* Find path without the filename.
280     * I.e. everything after the last / is a "filename".
281     * OK, maybe it is a directory name, but we treat it like
282     * a filename. If we don't find a / then the whole name
283     * must be a path name (e.g. c:).
284     */
285    for (p=l=ar->fname; *p; p++) {
286       if (*p == '/') {
287          l = p;                       /* set pos of last slash */
288       }
289    }
290    if (*l == '/') {                   /* did we find a slash? */
291       l++;                            /* yes, point to filename */
292    } else {                           /* no, whole thing must be path name */
293       l = p;
294    }
295
296    /* If filename doesn't exist (i.e. root directory), we
297     * simply create a blank name consisting of a single
298     * space. This makes handling zero length filenames
299     * easier.
300     */
301    fnl = p - l;
302    if (fnl > max_file_len) {
303       max_file_len = fnl;
304    }
305    if (fnl > 255) {
306       printf(_("===== Filename truncated to 255 chars: %s\n"), l);
307       fnl = 255;
308       trunc_fname++;
309    }
310    if (fnl > 0) {
311       strncpy(file, l, fnl);          /* copy filename */
312       file[fnl] = 0;
313    } else {
314       file[0] = ' ';                  /* blank filename */
315       file[1] = 0;
316    }
317
318    pnl = l - ar->fname;
319    if (pnl > max_path_len) {
320       max_path_len = pnl;
321    }
322    if (pnl > 255) {
323       printf(_("========== Path name truncated to 255 chars: %s\n"), ar->fname);
324       pnl = 255;
325       trunc_path++;
326    }
327    strncpy(spath, ar->fname, pnl);
328    spath[pnl] = 0;
329    if (pnl == 0) {
330       spath[0] = ' ';
331       spath[1] = 0;
332       printf(_("========== Path length is zero. File=%s\n"), ar->fname);
333    }
334    if (debug_level >= 10) {
335       printf("Path: %s\n", spath);
336       printf("File: %s\n", file);
337    }
338
339 }