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