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