]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/testls.c
Fix compiler warnings in tools directory
[bacula/bacula] / bacula / src / tools / testls.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    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 License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  * Test program for listing files during regression testing
30  *
31  *  Kern Sibbald, MM
32  *
33  */
34
35 #include "bacula.h"
36 #include "findlib/find.h"
37
38 /* Dummy functions */
39 int generate_daemon_event(JCR *jcr, const char *event) { return 1; }
40 int generate_job_event(JCR *jcr, const char *event) { return 1; }
41 void generate_plugin_event(JCR *jcr, bEventType eventType, void *value) { }
42
43
44 /* Global variables */
45 int attrs = 0;
46
47 static JCR *jcr;
48 static int num_files = 0;
49
50 static int print_file(JCR *jcr, FF_PKT *ff, bool);
51 static void print_ls_output(char *fname, char *link, int type, struct stat *statp);
52 static int count_files(JCR *jcr, FF_PKT *ff, bool top_level);
53
54 static void usage()
55 {
56    fprintf(stderr, _(
57 "\n"
58 "Usage: testls [-d debug_level] [-] [pattern1 ...]\n"
59 "       -a          print extended attributes (Win32 debug)\n"
60 "       -d <nn>     set debug level to <nn>\n"
61 "       -dt         print timestamp in debug output\n"
62 "       -e          specify file of exclude patterns\n"
63 "       -i          specify file of include patterns\n"
64 "       -q          quiet, don't print filenames (debug)\n"
65 "       -           read pattern(s) from stdin\n"
66 "       -?          print this message.\n"
67 "\n"
68 "Patterns are file inclusion -- normally directories.\n"
69 "Debug level >= 1 prints each file found.\n"
70 "Debug level >= 10 prints path/file for catalog.\n"
71 "Errors always printed.\n"
72 "Files/paths truncated is number with len > 255.\n"
73 "Truncation is only in catalog.\n"
74 "\n"));
75
76    exit(1);
77 }
78
79
80 int main(int argc, char *const *argv)
81 {
82    FF_PKT *ff;
83    char name[1000];
84    bool quiet = false;
85    int i, ch, hard_links;
86    char *inc = NULL;
87    char *exc = NULL;
88    FILE *fd;
89
90    setlocale(LC_ALL, "");
91    bindtextdomain("bacula", LOCALEDIR);
92    textdomain("bacula");
93    lmgr_init_thread();
94
95    while ((ch = getopt(argc, argv, "ad:e:i:q?")) != -1) {
96       switch (ch) {
97       case 'a':                       /* print extended attributes *debug* */
98          attrs = 1;
99          break;
100
101       case 'd':                       /* set debug level */
102          if (*optarg == 't') {
103             dbg_timestamp = true;
104          } else {
105             debug_level = atoi(optarg);
106             if (debug_level <= 0) {
107                debug_level = 1;
108             }
109          }
110          break;
111
112       case 'e':                       /* exclude patterns */
113          exc = optarg;
114          break;
115
116       case 'i':                       /* include patterns */
117          inc = optarg;
118          break;
119
120       case 'q':
121          quiet = true;
122          break;
123
124       case '?':
125       default:
126          usage();
127
128       }
129    }
130    argc -= optind;
131    argv += optind;
132
133    jcr = new_jcr(sizeof(JCR), NULL);
134
135    ff = init_find_files();
136    if (argc == 0 && !inc) {
137       add_fname_to_include_list(ff, 0, "/"); /* default to / */
138    } else {
139       for (i=0; i < argc; i++) {
140          if (strcmp(argv[i], "-") == 0) {
141              while (fgets(name, sizeof(name)-1, stdin)) {
142                 strip_trailing_junk(name);
143                 add_fname_to_include_list(ff, 0, name);
144               }
145               continue;
146          }
147          add_fname_to_include_list(ff, 0, argv[i]);
148       }
149    }
150    if (inc) {
151       fd = fopen(inc, "rb");
152       if (!fd) {
153          printf(_("Could not open include file: %s\n"), inc);
154          exit(1);
155       }
156       while (fgets(name, sizeof(name)-1, fd)) {
157          strip_trailing_junk(name);
158          add_fname_to_include_list(ff, 0, name);
159       }
160       fclose(fd);
161    }
162
163    if (exc) {
164       fd = fopen(exc, "rb");
165       if (!fd) {
166          printf(_("Could not open exclude file: %s\n"), exc);
167          exit(1);
168       }
169       while (fgets(name, sizeof(name)-1, fd)) {
170          strip_trailing_junk(name);
171          add_fname_to_exclude_list(ff, name);
172       }
173       fclose(fd);
174    }
175    if (quiet) {
176       match_files(jcr, ff, count_files);
177    } else {
178       match_files(jcr, ff, print_file);
179    }
180    printf(_("Files seen = %d\n"), num_files);
181    term_include_exclude_files(ff);
182    hard_links = term_find_files(ff);
183
184    free_jcr(jcr);
185    term_last_jobs_list();             /* free jcr chain */
186    close_memory_pool();
187    lmgr_cleanup_main();
188    sm_dump(false);
189    exit(0);
190 }
191
192 static int count_files(JCR *jcr, FF_PKT *ff, bool top_level)
193 {
194    num_files++;
195    return 1;
196 }
197
198 static int print_file(JCR *jcr, FF_PKT *ff, bool top_level) 
199 {
200
201    switch (ff->type) {
202    case FT_LNKSAVED:
203    case FT_REGE:
204    case FT_REG:
205    case FT_LNK:
206    case FT_DIREND:
207    case FT_SPEC:
208       print_ls_output(ff->fname, ff->link, ff->type, &ff->statp);
209       break;
210    case FT_DIRBEGIN:
211       break;
212    case FT_NOACCESS:
213       printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno));
214       break;
215    case FT_NOFOLLOW:
216       printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno));
217       break;
218    case FT_NOSTAT:
219       printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno));
220       break;
221    case FT_NOCHG:
222       printf(_("Skip: File not saved. No change. %s\n"), ff->fname);
223       break;
224    case FT_ISARCH:
225       printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
226       break;
227    case FT_NORECURSE:
228       printf(_("Recursion turned off. Directory not entered. %s\n"), ff->fname);
229       break;
230    case FT_NOFSCHG:
231       printf(_("Skip: File system change prohibited. Directory not entered. %s\n"), ff->fname);
232       break;
233    case FT_NOOPEN:
234       printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
235       break;
236    default:
237       printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname);
238       break;
239    }
240    num_files++;
241    return 1;
242 }
243
244 static void print_ls_output(char *fname, char *link, int type, struct stat *statp)
245 {
246    char buf[1000];
247    char ec1[30];
248    char *p, *f;
249    int n;
250
251    if (type == FT_LNK) {
252       statp->st_mtime = 0;
253       statp->st_mode |= 0777;
254    }
255    p = encode_mode(statp->st_mode, buf);
256    n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
257    p += n;
258    n = sprintf(p, "%-4d %-4d", (int)statp->st_uid, (int)statp->st_gid);
259    p += n;
260    n = sprintf(p, "%7.7s ", edit_uint64(statp->st_size, ec1));
261    p += n;
262    if (S_ISCHR(statp->st_mode) || S_ISBLK(statp->st_mode)) {
263       n = sprintf(p, "%4x ", (int)statp->st_rdev);
264    } else {
265       n = sprintf(p, "     ");
266    }
267    p += n;
268    p = encode_time(statp->st_mtime, p);
269    *p++ = ' ';
270    /* Copy file name */
271    for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
272       *p++ = *f++;
273    if (type == FT_LNK) {
274       *p++ = '-';
275       *p++ = '>';
276       *p++ = ' ';
277       /* Copy link name */
278       for (f=link; *f && (p-buf) < (int)sizeof(buf); )
279          *p++ = *f++;
280    }
281    *p++ = '\n';
282    *p = 0;
283    fputs(buf, stdout);
284 }
285
286 bool python_set_prog(JCR*, char const*) { return false; }