]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/testfind.c
Fix UseDuration (hopefully)
[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, hard_links;
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   hard_links = 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 Hard links     : %d\n"),
99      num_files, max_file_len, max_path_len,
100      trunc_fname, trunc_path, hard_links);
101   
102   close_memory_pool();
103   sm_dump(False);
104   exit(0);
105 }
106
107 static int print_file(FF_PKT *ff, void *pkt)
108 {
109
110    switch (ff->type) {
111    case FT_LNKSAVED:
112       if (debug_level == 1) {
113          printf("%s\n", ff->fname);
114       } else if (debug_level > 1) {
115          printf("Lnka: %s -> %s\n", ff->fname, ff->link);
116       }
117       break;
118    case FT_REGE:
119       if (debug_level == 1) {
120          printf("%s\n", ff->fname);
121       } else if (debug_level > 1) {
122          printf("Empty: %s\n", ff->fname);
123       }
124       count_files(ff);
125       break; 
126    case FT_REG:
127       if (debug_level == 1) {
128          printf("%s\n", ff->fname);
129       } else if (debug_level > 1) {
130          printf("Reg: %s\n", ff->fname);
131       }
132       count_files(ff);
133       break;
134    case FT_LNK:
135       if (debug_level == 1) {
136          printf("%s\n", ff->fname);
137       } else if (debug_level > 1) {
138          printf("Lnk: %s -> %s\n", ff->fname, ff->link);
139       }
140       count_files(ff);
141       break;
142    case FT_DIR:
143       if (debug_level == 1) {
144          printf("%s\n", ff->fname);
145       } else if (debug_level > 1) {
146          printf("Dir: %s\n", ff->fname);
147       }
148       count_files(ff);
149       break;
150    case FT_SPEC:
151       if (debug_level == 1) {
152          printf("%s\n", ff->fname);
153       } else if (debug_level > 1) {
154          printf("Spec: %s\n", ff->fname);
155       }
156       count_files(ff);
157       break;
158    case FT_NOACCESS:
159       printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno));
160       break;
161    case FT_NOFOLLOW:
162       printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno));
163       break;
164    case FT_NOSTAT:
165       printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno));
166       break;
167    case FT_NOCHG:
168       printf(_("Skip: File not saved. No change. %s\n"), ff->fname);
169       break;
170    case FT_ISARCH:
171       printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
172       break;
173    case FT_NORECURSE:
174       printf(_("Recursion turned off. Directory not entered. %s\n"), ff->fname);
175       break;
176    case FT_NOFSCHG:
177       printf(_("Skip: File system change prohibited. Directory not entered. %s\n"), ff->fname);
178       break;
179    case FT_NOOPEN:
180       printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
181       break;
182    default:
183       printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname);
184       break;
185    }
186    if (attrs) {
187       char attr[200];
188       encode_attribsEx(NULL, attr, ff);
189       if (*attr != 0) {
190          printf("AttrEx=%s\n", attr);
191       }
192 //    set_attribsEx(NULL, ff->fname, NULL, NULL, ff->type, attr);
193    }
194    return 1;
195 }
196
197 static void count_files(FF_PKT *ar) 
198 {
199    int fnl, pnl;
200    char *l, *p;
201    char file[MAXSTRING];
202    char spath[MAXSTRING];
203
204    num_files++;
205
206    /* Find path without the filename.  
207     * I.e. everything after the last / is a "filename".
208     * OK, maybe it is a directory name, but we treat it like
209     * a filename. If we don't find a / then the whole name
210     * must be a path name (e.g. c:).
211     */
212    for (p=l=ar->fname; *p; p++) {
213       if (*p == '/') {
214          l = p;                       /* set pos of last slash */
215       }
216    }
217    if (*l == '/') {                   /* did we find a slash? */
218       l++;                            /* yes, point to filename */
219    } else {                           /* no, whole thing must be path name */
220       l = p;
221    }
222
223    /* If filename doesn't exist (i.e. root directory), we
224     * simply create a blank name consisting of a single 
225     * space. This makes handling zero length filenames
226     * easier.
227     */
228    fnl = p - l;
229    if (fnl > max_file_len) {
230       max_file_len = fnl;
231    }
232    if (fnl > 255) {
233       printf(_("===== Filename truncated to 255 chars: %s\n"), l);
234       fnl = 255;
235       trunc_fname++;
236    }
237    if (fnl > 0) {
238       strncpy(file, l, fnl);          /* copy filename */
239       file[fnl] = 0;
240    } else {
241       file[0] = ' ';                  /* blank filename */
242       file[1] = 0;
243    }
244
245    pnl = l - ar->fname;    
246    if (pnl > max_path_len) {
247       max_path_len = pnl;
248    }
249    if (pnl > 255) {
250       printf(_("========== Path name truncated to 255 chars: %s\n"), ar->fname);
251       pnl = 255;
252       trunc_path++;
253    }
254    strncpy(spath, ar->fname, pnl);
255    spath[pnl] = 0;
256    if (pnl == 0) {
257       spath[0] = ' ';
258       spath[1] = 0;
259       printf(_("========== Path length is zero. File=%s\n"), ar->fname);
260    }
261    if (debug_level >= 10) {
262       printf("Path: %s\n", spath);
263       printf("File: %s\n", file);
264    }
265
266 }