]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/testfind.c
JobStatus updates
[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 "       -           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    jcr = new_jcr(sizeof(JCR), NULL);
94
95    ff = init_find_files();
96    if (argc == 0) {
97       add_fname_to_include_list(ff, 0, "/"); /* default to / */
98    } else {   
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); 
104               }
105               continue;
106          }
107          add_fname_to_include_list(ff, 0, argv[i]); 
108       }
109    }
110
111   find_files(jcr, ff, print_file, NULL);
112   hard_links = term_find_files(ff);
113   
114    printf(_("\
115 Total files    : %d\n\
116 Max file length: %d\n\
117 Max path length: %d\n\
118 Files truncated: %d\n\
119 Paths truncated: %d\n\
120 Hard links     : %d\n"),
121      num_files, max_file_len, max_path_len,
122      trunc_fname, trunc_path, hard_links);
123   
124   free_jcr(jcr);
125   close_memory_pool();
126   sm_dump(False);
127   exit(0);
128 }
129
130 static int print_file(FF_PKT *ff, void *pkt)
131 {
132
133    switch (ff->type) {
134    case FT_LNKSAVED:
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);
139       }
140       break;
141    case FT_REGE:
142       if (debug_level == 1) {
143          printf("%s\n", ff->fname);
144       } else if (debug_level > 1) {
145          printf("Empty: %s\n", ff->fname);
146       }
147       count_files(ff);
148       break; 
149    case FT_REG:
150       if (debug_level == 1) {
151          printf("%s\n", ff->fname);
152       } else if (debug_level > 1) {
153          printf("Reg: %s\n", ff->fname);
154       }
155       count_files(ff);
156       break;
157    case FT_LNK:
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);
162       }
163       count_files(ff);
164       break;
165    case FT_DIR:
166       if (debug_level == 1) {
167          printf("%s\n", ff->fname);
168       } else if (debug_level > 1) {
169          printf("Dir: %s\n", ff->fname);
170       }
171       count_files(ff);
172       break;
173    case FT_SPEC:
174       if (debug_level == 1) {
175          printf("%s\n", ff->fname);
176       } else if (debug_level > 1) {
177          printf("Spec: %s\n", ff->fname);
178       }
179       count_files(ff);
180       break;
181    case FT_NOACCESS:
182       printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno));
183       break;
184    case FT_NOFOLLOW:
185       printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno));
186       break;
187    case FT_NOSTAT:
188       printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno));
189       break;
190    case FT_NOCHG:
191       printf(_("Skip: File not saved. No change. %s\n"), ff->fname);
192       break;
193    case FT_ISARCH:
194       printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
195       break;
196    case FT_NORECURSE:
197       printf(_("Recursion turned off. Directory not entered. %s\n"), ff->fname);
198       break;
199    case FT_NOFSCHG:
200       printf(_("Skip: File system change prohibited. Directory not entered. %s\n"), ff->fname);
201       break;
202    case FT_NOOPEN:
203       printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
204       break;
205    default:
206       printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname);
207       break;
208    }
209    if (attrs) {
210       char attr[200];
211       encode_attribsEx(NULL, attr, ff);
212       if (*attr != 0) {
213          printf("AttrEx=%s\n", attr);
214       }
215 //    set_attribsEx(NULL, ff->fname, NULL, NULL, ff->type, attr);
216    }
217    return 1;
218 }
219
220 static void count_files(FF_PKT *ar) 
221 {
222    int fnl, pnl;
223    char *l, *p;
224    char file[MAXSTRING];
225    char spath[MAXSTRING];
226
227    num_files++;
228
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:).
234     */
235    for (p=l=ar->fname; *p; p++) {
236       if (*p == '/') {
237          l = p;                       /* set pos of last slash */
238       }
239    }
240    if (*l == '/') {                   /* did we find a slash? */
241       l++;                            /* yes, point to filename */
242    } else {                           /* no, whole thing must be path name */
243       l = p;
244    }
245
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
249     * easier.
250     */
251    fnl = p - l;
252    if (fnl > max_file_len) {
253       max_file_len = fnl;
254    }
255    if (fnl > 255) {
256       printf(_("===== Filename truncated to 255 chars: %s\n"), l);
257       fnl = 255;
258       trunc_fname++;
259    }
260    if (fnl > 0) {
261       strncpy(file, l, fnl);          /* copy filename */
262       file[fnl] = 0;
263    } else {
264       file[0] = ' ';                  /* blank filename */
265       file[1] = 0;
266    }
267
268    pnl = l - ar->fname;    
269    if (pnl > max_path_len) {
270       max_path_len = pnl;
271    }
272    if (pnl > 255) {
273       printf(_("========== Path name truncated to 255 chars: %s\n"), ar->fname);
274       pnl = 255;
275       trunc_path++;
276    }
277    strncpy(spath, ar->fname, pnl);
278    spath[pnl] = 0;
279    if (pnl == 0) {
280       spath[0] = ' ';
281       spath[1] = 0;
282       printf(_("========== Path length is zero. File=%s\n"), ar->fname);
283    }
284    if (debug_level >= 10) {
285       printf("Path: %s\n", spath);
286       printf("File: %s\n", file);
287    }
288
289 }