]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/testfind.c
Enhance dbcheck -- tweak Paths
[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 #include "jcr.h"
28
29
30 /* Global variables */
31 static int num_files = 0;
32 static int max_file_len = 0;
33 static int max_path_len = 0;
34 static int trunc_fname = 0;
35 static int trunc_path = 0;
36 static int attrs = 0;
37
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 "       -           read pattern(s) from stdin\n"
50 "       -?          print this message.\n"
51 "\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"
58 "\n"));
59
60    exit(1);
61 }
62
63
64 int
65 main (int argc, char *const *argv)
66 {
67    FF_PKT *ff;
68    char name[1000];
69    int i, ch, hard_links;
70
71    while ((ch = getopt(argc, argv, "ad:?")) != -1) {
72       switch (ch) {
73          case 'a':                    /* print extended attributes *debug* */
74             attrs = 1;
75             break;
76
77          case 'd':                    /* set debug level */
78             debug_level = atoi(optarg);
79             if (debug_level <= 0) {
80                debug_level = 1; 
81             }
82             break;
83
84          case '?':
85          default:
86             usage();
87
88       }  
89    }
90    argc -= optind;
91    argv += optind;
92
93   ff = init_find_files();
94    if (argc == 0) {
95      add_fname_to_include_list(ff, 0, "/"); /* default to / */
96   } else {   
97       for (i=0; i < argc; i++) {
98         if (strcmp(argv[i], "-") == 0) {
99            while (fgets(name, sizeof(name)-1, stdin)) {
100               strip_trailing_junk(name);
101               add_fname_to_include_list(ff, 0, name); 
102            }
103            continue;
104         }
105         add_fname_to_include_list(ff, 0, argv[i]); 
106      }
107   }
108
109   find_files(ff, print_file, NULL);
110   hard_links = term_find_files(ff);
111   
112    printf(_("\
113 Total files    : %d\n\
114 Max file length: %d\n\
115 Max path length: %d\n\
116 Files truncated: %d\n\
117 Paths truncated: %d\n\
118 Hard links     : %d\n"),
119      num_files, max_file_len, max_path_len,
120      trunc_fname, trunc_path, hard_links);
121   
122   close_memory_pool();
123   sm_dump(False);
124   exit(0);
125 }
126
127 static int print_file(FF_PKT *ff, void *pkt)
128 {
129
130    switch (ff->type) {
131    case FT_LNKSAVED:
132       if (debug_level == 1) {
133          printf("%s\n", ff->fname);
134       } else if (debug_level > 1) {
135          printf("Lnka: %s -> %s\n", ff->fname, ff->link);
136       }
137       break;
138    case FT_REGE:
139       if (debug_level == 1) {
140          printf("%s\n", ff->fname);
141       } else if (debug_level > 1) {
142          printf("Empty: %s\n", ff->fname);
143       }
144       count_files(ff);
145       break; 
146    case FT_REG:
147       if (debug_level == 1) {
148          printf("%s\n", ff->fname);
149       } else if (debug_level > 1) {
150          printf("Reg: %s\n", ff->fname);
151       }
152       count_files(ff);
153       break;
154    case FT_LNK:
155       if (debug_level == 1) {
156          printf("%s\n", ff->fname);
157       } else if (debug_level > 1) {
158          printf("Lnk: %s -> %s\n", ff->fname, ff->link);
159       }
160       count_files(ff);
161       break;
162    case FT_DIR:
163       if (debug_level == 1) {
164          printf("%s\n", ff->fname);
165       } else if (debug_level > 1) {
166          printf("Dir: %s\n", ff->fname);
167       }
168       count_files(ff);
169       break;
170    case FT_SPEC:
171       if (debug_level == 1) {
172          printf("%s\n", ff->fname);
173       } else if (debug_level > 1) {
174          printf("Spec: %s\n", ff->fname);
175       }
176       count_files(ff);
177       break;
178    case FT_NOACCESS:
179       printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno));
180       break;
181    case FT_NOFOLLOW:
182       printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno));
183       break;
184    case FT_NOSTAT:
185       printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno));
186       break;
187    case FT_NOCHG:
188       printf(_("Skip: File not saved. No change. %s\n"), ff->fname);
189       break;
190    case FT_ISARCH:
191       printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
192       break;
193    case FT_NORECURSE:
194       printf(_("Recursion turned off. Directory not entered. %s\n"), ff->fname);
195       break;
196    case FT_NOFSCHG:
197       printf(_("Skip: File system change prohibited. Directory not entered. %s\n"), ff->fname);
198       break;
199    case FT_NOOPEN:
200       printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
201       break;
202    default:
203       printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname);
204       break;
205    }
206    if (attrs) {
207       char attr[200];
208       encode_attribsEx(NULL, attr, ff);
209       if (*attr != 0) {
210          printf("AttrEx=%s\n", attr);
211       }
212 //    set_attribsEx(NULL, ff->fname, NULL, NULL, ff->type, attr);
213    }
214    return 1;
215 }
216
217 static void count_files(FF_PKT *ar) 
218 {
219    int fnl, pnl;
220    char *l, *p;
221    char file[MAXSTRING];
222    char spath[MAXSTRING];
223
224    num_files++;
225
226    /* Find path without the filename.  
227     * I.e. everything after the last / is a "filename".
228     * OK, maybe it is a directory name, but we treat it like
229     * a filename. If we don't find a / then the whole name
230     * must be a path name (e.g. c:).
231     */
232    for (p=l=ar->fname; *p; p++) {
233       if (*p == '/') {
234          l = p;                       /* set pos of last slash */
235       }
236    }
237    if (*l == '/') {                   /* did we find a slash? */
238       l++;                            /* yes, point to filename */
239    } else {                           /* no, whole thing must be path name */
240       l = p;
241    }
242
243    /* If filename doesn't exist (i.e. root directory), we
244     * simply create a blank name consisting of a single 
245     * space. This makes handling zero length filenames
246     * easier.
247     */
248    fnl = p - l;
249    if (fnl > max_file_len) {
250       max_file_len = fnl;
251    }
252    if (fnl > 255) {
253       printf(_("===== Filename truncated to 255 chars: %s\n"), l);
254       fnl = 255;
255       trunc_fname++;
256    }
257    if (fnl > 0) {
258       strncpy(file, l, fnl);          /* copy filename */
259       file[fnl] = 0;
260    } else {
261       file[0] = ' ';                  /* blank filename */
262       file[1] = 0;
263    }
264
265    pnl = l - ar->fname;    
266    if (pnl > max_path_len) {
267       max_path_len = pnl;
268    }
269    if (pnl > 255) {
270       printf(_("========== Path name truncated to 255 chars: %s\n"), ar->fname);
271       pnl = 255;
272       trunc_path++;
273    }
274    strncpy(spath, ar->fname, pnl);
275    spath[pnl] = 0;
276    if (pnl == 0) {
277       spath[0] = ' ';
278       spath[1] = 0;
279       printf(_("========== Path length is zero. File=%s\n"), ar->fname);
280    }
281    if (debug_level >= 10) {
282       printf("Path: %s\n", spath);
283       printf("File: %s\n", file);
284    }
285
286 }