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