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