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