]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/testls.c
Added patch from Stefan Reddig -- improved ingres db test
[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
81 main (int argc, char *const *argv)
82 {
83    FF_PKT *ff;
84    char name[1000];
85    bool quiet = false;
86    int i, ch, hard_links;
87    char *inc = NULL;
88    char *exc = NULL;
89    FILE *fd;
90
91    setlocale(LC_ALL, "");
92    bindtextdomain("bacula", LOCALEDIR);
93    textdomain("bacula");
94    lmgr_init_thread();
95
96    while ((ch = getopt(argc, argv, "ad:e:i:q?")) != -1) {
97       switch (ch) {
98       case 'a':                       /* print extended attributes *debug* */
99          attrs = 1;
100          break;
101
102       case 'd':                       /* set debug level */
103          if (*optarg == 't') {
104             dbg_timestamp = true;
105          } else {
106             debug_level = atoi(optarg);
107             if (debug_level <= 0) {
108                debug_level = 1;
109             }
110          }
111          break;
112
113       case 'e':                       /* exclude patterns */
114          exc = optarg;
115          break;
116
117       case 'i':                       /* include patterns */
118          inc = optarg;
119          break;
120
121       case 'q':
122          quiet = true;
123          break;
124
125       case '?':
126       default:
127          usage();
128
129       }
130    }
131    argc -= optind;
132    argv += optind;
133
134    jcr = new_jcr(sizeof(JCR), NULL);
135
136    ff = init_find_files();
137    if (argc == 0 && !inc) {
138       add_fname_to_include_list(ff, 0, "/"); /* default to / */
139    } else {
140       for (i=0; i < argc; i++) {
141          if (strcmp(argv[i], "-") == 0) {
142              while (fgets(name, sizeof(name)-1, stdin)) {
143                 strip_trailing_junk(name);
144                 add_fname_to_include_list(ff, 0, name);
145               }
146               continue;
147          }
148          add_fname_to_include_list(ff, 0, argv[i]);
149       }
150    }
151    if (inc) {
152       fd = fopen(inc, "rb");
153       if (!fd) {
154          printf(_("Could not open include file: %s\n"), inc);
155          exit(1);
156       }
157       while (fgets(name, sizeof(name)-1, fd)) {
158          strip_trailing_junk(name);
159          add_fname_to_include_list(ff, 0, name);
160       }
161       fclose(fd);
162    }
163
164    if (exc) {
165       fd = fopen(exc, "rb");
166       if (!fd) {
167          printf(_("Could not open exclude file: %s\n"), exc);
168          exit(1);
169       }
170       while (fgets(name, sizeof(name)-1, fd)) {
171          strip_trailing_junk(name);
172          add_fname_to_exclude_list(ff, name);
173       }
174       fclose(fd);
175    }
176    if (quiet) {
177       match_files(jcr, ff, count_files);
178    } else {
179       match_files(jcr, ff, print_file);
180    }
181    printf(_("Files seen = %d\n"), num_files);
182    term_include_exclude_files(ff);
183    hard_links = term_find_files(ff);
184
185    free_jcr(jcr);
186    term_last_jobs_list();             /* free jcr chain */
187    close_memory_pool();
188    lmgr_cleanup_main();
189    sm_dump(false);
190    exit(0);
191 }
192
193 static int count_files(JCR *jcr, FF_PKT *ff, bool top_level)
194 {
195    num_files++;
196    return 1;
197 }
198
199 static int print_file(JCR *jcr, FF_PKT *ff, bool top_level) 
200 {
201
202    switch (ff->type) {
203    case FT_LNKSAVED:
204    case FT_REGE:
205    case FT_REG:
206    case FT_LNK:
207    case FT_DIREND:
208    case FT_SPEC:
209       print_ls_output(ff->fname, ff->link, ff->type, &ff->statp);
210       break;
211    case FT_DIRBEGIN:
212       break;
213    case FT_NOACCESS:
214       printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno));
215       break;
216    case FT_NOFOLLOW:
217       printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno));
218       break;
219    case FT_NOSTAT:
220       printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno));
221       break;
222    case FT_NOCHG:
223       printf(_("Skip: File not saved. No change. %s\n"), ff->fname);
224       break;
225    case FT_ISARCH:
226       printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
227       break;
228    case FT_NORECURSE:
229       printf(_("Recursion turned off. Directory not entered. %s\n"), ff->fname);
230       break;
231    case FT_NOFSCHG:
232       printf(_("Skip: File system change prohibited. Directory not entered. %s\n"), ff->fname);
233       break;
234    case FT_NOOPEN:
235       printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
236       break;
237    default:
238       printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname);
239       break;
240    }
241    num_files++;
242    return 1;
243 }
244
245 static void print_ls_output(char *fname, char *link, int type, struct stat *statp)
246 {
247    char buf[1000];
248    char ec1[30];
249    char *p, *f;
250    int n;
251
252    if (type == FT_LNK) {
253       statp->st_mtime = 0;
254       statp->st_mode |= 0777;
255    }
256    p = encode_mode(statp->st_mode, buf);
257    n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
258    p += n;
259    n = sprintf(p, "%-4d %-4d", (int)statp->st_uid, (int)statp->st_gid);
260    p += n;
261    n = sprintf(p, "%7.7s ", edit_uint64(statp->st_size, ec1));
262    p += n;
263    if (S_ISCHR(statp->st_mode) || S_ISBLK(statp->st_mode)) {
264       n = sprintf(p, "%4x ", (int)statp->st_rdev);
265    } else {
266       n = sprintf(p, "     ");
267    }
268    p += n;
269    p = encode_time(statp->st_mtime, p);
270    *p++ = ' ';
271    /* Copy file name */
272    for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
273       *p++ = *f++;
274    if (type == FT_LNK) {
275       *p++ = '-';
276       *p++ = '>';
277       *p++ = ' ';
278       /* Copy link name */
279       for (f=link; *f && (p-buf) < (int)sizeof(buf); )
280          *p++ = *f++;
281    }
282    *p++ = '\n';
283    *p = 0;
284    fputs(buf, stdout);
285 }
286
287 bool python_set_prog(JCR*, char const*) { return false; }