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