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