]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/testfind.c
This commit was manufactured by cvs2svn to create tag
[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 /* 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;
35 static int attrs = 0;
36
37 static JCR *jcr;
38
39 static int print_file(FF_PKT *ff, void *pkt);
40 static void count_files(FF_PKT *ff);
41
42 static void usage()
43 {
44    fprintf(stderr, _(
45 "\n"
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"
53 "\n"
54 "Patterns are used for 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 are always printed.\n"
58 "Files/paths truncated is the number of files/paths with len > 255.\n"
59 "Truncation is only in the catalog.\n"
60 "\n"));
61
62    exit(1);
63 }
64
65
66 int
67 main (int argc, char *const *argv)
68 {
69    FF_PKT *ff;
70    char name[1000];
71    int i, ch, hard_links;
72    char *inc = NULL;
73    char *exc = NULL;
74    FILE *fd;
75
76    while ((ch = getopt(argc, argv, "ad:e:i:?")) != -1) {
77       switch (ch) {
78          case 'a':                    /* print extended attributes *debug* */
79             attrs = 1;
80             break;
81
82          case 'd':                    /* set debug level */
83             debug_level = atoi(optarg);
84             if (debug_level <= 0) {
85                debug_level = 1; 
86             }
87             break;
88
89          case 'e':                    /* exclude patterns */
90             exc = optarg;
91             break;
92
93          case 'i':                    /* include patterns */
94             inc = optarg;
95             break;
96
97          case '?':
98          default:
99             usage();
100
101       }  
102    }
103    argc -= optind;
104    argv += optind;
105
106    jcr = new_jcr(sizeof(JCR), NULL);
107
108    ff = init_find_files();
109    if (argc == 0 && !inc) {
110       add_fname_to_include_list(ff, 0, "/"); /* default to / */
111    } else {   
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); 
117               }
118               continue;
119          }
120          add_fname_to_include_list(ff, 0, argv[i]); 
121       }
122    }
123    if (inc) {
124       fd = fopen(inc, "r");
125       if (!fd) {
126          printf("Could not open include file: %s\n", inc);
127          exit(1);
128       }
129       while (fgets(name, sizeof(name)-1, fd)) {
130          strip_trailing_junk(name);
131          add_fname_to_include_list(ff, 0, name);
132       }
133       fclose(fd);
134    }
135
136    if (exc) {
137       fd = fopen(exc, "r");
138       if (!fd) {
139          printf("Could not open exclude file: %s\n", exc);
140          exit(1);
141       }
142       while (fgets(name, sizeof(name)-1, fd)) {
143          strip_trailing_junk(name);
144          add_fname_to_exclude_list(ff, name);
145       }
146       fclose(fd);
147    }
148    find_files(jcr, ff, print_file, NULL);
149    hard_links = term_find_files(ff);
150   
151    printf(_("\
152 Total files    : %d\n\
153 Max file length: %d\n\
154 Max path length: %d\n\
155 Files truncated: %d\n\
156 Paths truncated: %d\n\
157 Hard links     : %d\n"),
158      num_files, max_file_len, max_path_len,
159      trunc_fname, trunc_path, hard_links);
160   
161   free_jcr(jcr);
162   close_memory_pool();
163   sm_dump(False);
164   exit(0);
165 }
166
167 static int print_file(FF_PKT *ff, void *pkt)
168 {
169
170    switch (ff->type) {
171    case FT_LNKSAVED:
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);
176       }
177       break;
178    case FT_REGE:
179       if (debug_level == 1) {
180          printf("%s\n", ff->fname);
181       } else if (debug_level > 1) {
182          printf("Empty: %s\n", ff->fname);
183       }
184       count_files(ff);
185       break; 
186    case FT_REG:
187       if (debug_level == 1) {
188          printf("%s\n", ff->fname);
189       } else if (debug_level > 1) {
190          printf("Reg: %s\n", ff->fname);
191       }
192       count_files(ff);
193       break;
194    case FT_LNK:
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);
199       }
200       count_files(ff);
201       break;
202    case FT_DIR:
203       if (debug_level == 1) {
204          printf("%s\n", ff->fname);
205       } else if (debug_level > 1) {
206          printf("Dir: %s\n", ff->fname);
207       }
208       count_files(ff);
209       break;
210    case FT_SPEC:
211       if (debug_level == 1) {
212          printf("%s\n", ff->fname);
213       } else if (debug_level > 1) {
214          printf("Spec: %s\n", ff->fname);
215       }
216       count_files(ff);
217       break;
218    case FT_NOACCESS:
219       printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno));
220       break;
221    case FT_NOFOLLOW:
222       printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno));
223       break;
224    case FT_NOSTAT:
225       printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno));
226       break;
227    case FT_NOCHG:
228       printf(_("Skip: File not saved. No change. %s\n"), ff->fname);
229       break;
230    case FT_ISARCH:
231       printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
232       break;
233    case FT_NORECURSE:
234       printf(_("Recursion turned off. Directory not entered. %s\n"), ff->fname);
235       break;
236    case FT_NOFSCHG:
237       printf(_("Skip: File system change prohibited. Directory not entered. %s\n"), ff->fname);
238       break;
239    case FT_NOOPEN:
240       printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
241       break;
242    default:
243       printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname);
244       break;
245    }
246    if (attrs) {
247       char attr[200];
248       encode_attribsEx(NULL, attr, ff);
249       if (*attr != 0) {
250          printf("AttrEx=%s\n", attr);
251       }
252 //    set_attribsEx(NULL, ff->fname, NULL, NULL, ff->type, attr);
253    }
254    return 1;
255 }
256
257 static void count_files(FF_PKT *ar) 
258 {
259    int fnl, pnl;
260    char *l, *p;
261    char file[MAXSTRING];
262    char spath[MAXSTRING];
263
264    num_files++;
265
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:).
271     */
272    for (p=l=ar->fname; *p; p++) {
273       if (*p == '/') {
274          l = p;                       /* set pos of last slash */
275       }
276    }
277    if (*l == '/') {                   /* did we find a slash? */
278       l++;                            /* yes, point to filename */
279    } else {                           /* no, whole thing must be path name */
280       l = p;
281    }
282
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
286     * easier.
287     */
288    fnl = p - l;
289    if (fnl > max_file_len) {
290       max_file_len = fnl;
291    }
292    if (fnl > 255) {
293       printf(_("===== Filename truncated to 255 chars: %s\n"), l);
294       fnl = 255;
295       trunc_fname++;
296    }
297    if (fnl > 0) {
298       strncpy(file, l, fnl);          /* copy filename */
299       file[fnl] = 0;
300    } else {
301       file[0] = ' ';                  /* blank filename */
302       file[1] = 0;
303    }
304
305    pnl = l - ar->fname;    
306    if (pnl > max_path_len) {
307       max_path_len = pnl;
308    }
309    if (pnl > 255) {
310       printf(_("========== Path name truncated to 255 chars: %s\n"), ar->fname);
311       pnl = 255;
312       trunc_path++;
313    }
314    strncpy(spath, ar->fname, pnl);
315    spath[pnl] = 0;
316    if (pnl == 0) {
317       spath[0] = ' ';
318       spath[1] = 0;
319       printf(_("========== Path length is zero. File=%s\n"), ar->fname);
320    }
321    if (debug_level >= 10) {
322       printf("Path: %s\n", spath);
323       printf("File: %s\n", file);
324    }
325
326 }