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