]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/testls.c
Change old get_Jobxxx to getJobxxx
[bacula/bacula] / bacula / src / tools / testls.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2008 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
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
53 static void usage()
54 {
55    fprintf(stderr, _(
56 "\n"
57 "Usage: testls [-d debug_level] [-] [pattern1 ...]\n"
58 "       -a          print extended attributes (Win32 debug)\n"
59 "       -d <nn>     set debug level to <nn>\n"
60 "       -dt         print timestamp in debug output\n"
61 "       -e          specify file of exclude patterns\n"
62 "       -i          specify file of include patterns\n"
63 "       -           read pattern(s) from stdin\n"
64 "       -?          print this message.\n"
65 "\n"
66 "Patterns are file inclusion -- normally directories.\n"
67 "Debug level >= 1 prints each file found.\n"
68 "Debug level >= 10 prints path/file for catalog.\n"
69 "Errors always printed.\n"
70 "Files/paths truncated is number with len > 255.\n"
71 "Truncation is only in catalog.\n"
72 "\n"));
73
74    exit(1);
75 }
76
77
78 int
79 main (int argc, char *const *argv)
80 {
81    FF_PKT *ff;
82    char name[1000];
83    int i, ch, hard_links;
84    char *inc = NULL;
85    char *exc = NULL;
86    FILE *fd;
87
88    setlocale(LC_ALL, "");
89    bindtextdomain("bacula", LOCALEDIR);
90    textdomain("bacula");
91    lmgr_init_thread();
92
93    while ((ch = getopt(argc, argv, "ad:e:i:?")) != -1) {
94       switch (ch) {
95       case 'a':                       /* print extended attributes *debug* */
96          attrs = 1;
97          break;
98
99       case 'd':                       /* set debug level */
100          if (*optarg == 't') {
101             dbg_timestamp = true;
102          } else {
103             debug_level = atoi(optarg);
104             if (debug_level <= 0) {
105                debug_level = 1;
106             }
107          }
108          break;
109
110       case 'e':                       /* exclude patterns */
111          exc = optarg;
112          break;
113
114       case 'i':                       /* include patterns */
115          inc = optarg;
116          break;
117
118       case '?':
119       default:
120          usage();
121
122       }
123    }
124    argc -= optind;
125    argv += optind;
126
127    jcr = new_jcr(sizeof(JCR), NULL);
128
129    ff = init_find_files();
130    if (argc == 0 && !inc) {
131       add_fname_to_include_list(ff, 0, "/"); /* default to / */
132    } else {
133       for (i=0; i < argc; i++) {
134          if (strcmp(argv[i], "-") == 0) {
135              while (fgets(name, sizeof(name)-1, stdin)) {
136                 strip_trailing_junk(name);
137                 add_fname_to_include_list(ff, 0, name);
138               }
139               continue;
140          }
141          add_fname_to_include_list(ff, 0, argv[i]);
142       }
143    }
144    if (inc) {
145       fd = fopen(inc, "rb");
146       if (!fd) {
147          printf(_("Could not open include file: %s\n"), inc);
148          exit(1);
149       }
150       while (fgets(name, sizeof(name)-1, fd)) {
151          strip_trailing_junk(name);
152          add_fname_to_include_list(ff, 0, name);
153       }
154       fclose(fd);
155    }
156
157    if (exc) {
158       fd = fopen(exc, "rb");
159       if (!fd) {
160          printf(_("Could not open exclude file: %s\n"), exc);
161          exit(1);
162       }
163       while (fgets(name, sizeof(name)-1, fd)) {
164          strip_trailing_junk(name);
165          add_fname_to_exclude_list(ff, name);
166       }
167       fclose(fd);
168    }
169    match_files(jcr, ff, print_file);
170    term_include_exclude_files(ff);
171    hard_links = term_find_files(ff);
172
173    free_jcr(jcr);
174    term_last_jobs_list();             /* free jcr chain */
175    close_memory_pool();
176    lmgr_cleanup_main();
177    sm_dump(false);
178    exit(0);
179 }
180
181 static int print_file(JCR *jcr, FF_PKT *ff, bool top_level) 
182 {
183
184    switch (ff->type) {
185    case FT_LNKSAVED:
186    case FT_REGE:
187    case FT_REG:
188    case FT_LNK:
189    case FT_DIREND:
190    case FT_SPEC:
191       print_ls_output(ff->fname, ff->link, ff->type, &ff->statp);
192       break;
193    case FT_DIRBEGIN:
194       break;
195    case FT_NOACCESS:
196       printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno));
197       break;
198    case FT_NOFOLLOW:
199       printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno));
200       break;
201    case FT_NOSTAT:
202       printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno));
203       break;
204    case FT_NOCHG:
205       printf(_("Skip: File not saved. No change. %s\n"), ff->fname);
206       break;
207    case FT_ISARCH:
208       printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
209       break;
210    case FT_NORECURSE:
211       printf(_("Recursion turned off. Directory not entered. %s\n"), ff->fname);
212       break;
213    case FT_NOFSCHG:
214       printf(_("Skip: File system change prohibited. Directory not entered. %s\n"), ff->fname);
215       break;
216    case FT_NOOPEN:
217       printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
218       break;
219    default:
220       printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname);
221       break;
222    }
223    return 1;
224 }
225
226 static void print_ls_output(char *fname, char *link, int type, struct stat *statp)
227 {
228    char buf[1000];
229    char ec1[30];
230    char *p, *f;
231    int n;
232
233    if (type == FT_LNK) {
234       statp->st_mtime = 0;
235       statp->st_mode |= 0777;
236    }
237    p = encode_mode(statp->st_mode, buf);
238    n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
239    p += n;
240    n = sprintf(p, "%-4d %-4d", (int)statp->st_uid, (int)statp->st_gid);
241    p += n;
242    n = sprintf(p, "%7.7s ", edit_uint64(statp->st_size, ec1));
243    p += n;
244    if (S_ISCHR(statp->st_mode) || S_ISBLK(statp->st_mode)) {
245       n = sprintf(p, "%4x ", (int)statp->st_rdev);
246    } else {
247       n = sprintf(p, "     ");
248    }
249    p += n;
250    p = encode_time(statp->st_mtime, p);
251    *p++ = ' ';
252    /* Copy file name */
253    for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
254       *p++ = *f++;
255    if (type == FT_LNK) {
256       *p++ = '-';
257       *p++ = '>';
258       *p++ = ' ';
259       /* Copy link name */
260       for (f=link; *f && (p-buf) < (int)sizeof(buf); )
261          *p++ = *f++;
262    }
263    *p++ = '\n';
264    *p = 0;
265    fputs(buf, stdout);
266 }
267
268 bool python_set_prog(JCR*, char const*) { return false; }