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