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