]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bvfs_test.c
add user handler to print directory
[bacula/bacula] / bacula / src / tools / bvfs_test.c
1 /*
2  *
3  *  Program to test cache path
4  *
5  *   Eric Bollengier, March 2007
6  *
7  *
8  *   Version $Id$
9  */
10 /*
11    Bacula® - The Network Backup Solution
12
13    Copyright (C) 2001-2006 Free Software Foundation Europe e.V.
14
15    The main author of Bacula is Kern Sibbald, with contributions from
16    many others, a complete list can be found in the file AUTHORS.
17    This program is Free Software; you can redistribute it and/or
18    modify it under the terms of version two of the GNU General Public
19    License as published by the Free Software Foundation and included
20    in the file LICENSE.
21
22    This program is distributed in the hope that it will be useful, but
23    WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25    General Public License for more details.
26
27    You should have received a copy of the GNU General Public License
28    along with this program; if not, write to the Free Software
29    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30    02110-1301, USA.
31
32    Bacula® is a registered trademark of Kern Sibbald.
33    The licensor of Bacula is the Free Software Foundation Europe
34    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
35    Switzerland, email:ftf@fsfeurope.org.
36 */
37
38 #include "bacula.h"
39 #include "cats/cats.h"
40 #include "cats/bvfs.h"
41 #include "findlib/find.h"
42  
43 /* Local variables */
44 static B_DB *db;
45
46 static const char *db_name = "regress";
47 static const char *db_user = "regress";
48 static const char *db_password = "";
49 static const char *db_host = NULL;
50
51 static void usage()
52 {
53    fprintf(stderr, _(
54 PROG_COPYRIGHT
55 "\nVersion: %s (%s)\n"
56 "       -d <nn>           set debug level to <nn>\n"
57 "       -dt               print timestamp in debug output\n"
58 "       -n <name>         specify the database name (default bacula)\n"
59 "       -u <user>         specify database user name (default bacula)\n"
60 "       -P <password      specify database password (default none)\n"
61 "       -h <host>         specify database host (default NULL)\n"
62 "       -w <working>      specify working directory\n"
63 "       -j <jobids>       specify jobids\n"
64 "       -p <path>         specify path\n"
65 "       -f <file>         specify file\n"
66 "       -v                verbose\n"
67 "       -?                print this message\n\n"), 2001, VERSION, BDATE);
68    exit(1);
69 }
70
71 static int result_handler(void *ctx, int fields, char **row)
72 {
73    Bvfs *vfs = (Bvfs *)ctx;
74    ATTR *attr = vfs->get_attr();
75    char *empty = "";
76
77    if (fields == BVFS_DIR_RECORD || fields == BVFS_FILE_RECORD) {
78       decode_stat((row[BVFS_LStat])?row[BVFS_LStat]:empty,
79                   &attr->statp, &attr->LinkFI);
80       if (fields == BVFS_DIR_RECORD) {
81          pm_strcpy(attr->ofname, bvfs_basename_dir(row[BVFS_Name]));   
82       } else {
83          pm_strcpy(attr->ofname, row[BVFS_Name]);   
84       }
85       print_ls_output(vfs->get_jcr(), attr);
86
87    } else {
88       Pmsg6(0, "%s\t%s\t%s\t%s\t%s\t%s",
89             row[0], row[1], row[2], row[3], row[4], row[5]);
90    }
91    return 0;
92 }
93
94
95 /* number of thread started */
96
97 int main (int argc, char *argv[])
98 {
99    int ch;
100    char *jobids="1", *path=NULL, *file=NULL;
101    setlocale(LC_ALL, "");
102    bindtextdomain("bacula", LOCALEDIR);
103    textdomain("bacula");
104    init_stack_dump();
105
106    Dmsg0(0, "Starting bvfs_test tool\n");
107    
108    my_name_is(argc, argv, "bvfs_test");
109    init_msg(NULL, NULL);
110
111    OSDependentInit();
112
113    while ((ch = getopt(argc, argv, "h:c:d:n:P:Su:vf:w:?j:p:f:")) != -1) {
114       switch (ch) {
115       case 'd':                    /* debug level */
116          if (*optarg == 't') {
117             dbg_timestamp = true;
118          } else {
119             debug_level = atoi(optarg);
120             if (debug_level <= 0) {
121                debug_level = 1;
122             }
123          }
124          break;
125
126       case 'h':
127          db_host = optarg;
128          break;
129
130       case 'n':
131          db_name = optarg;
132          break;
133
134       case 'w':
135          working_directory = optarg;
136          break;
137
138       case 'u':
139          db_user = optarg;
140          break;
141
142       case 'P':
143          db_password = optarg;
144          break;
145
146       case 'v':
147          verbose++;
148          break;
149
150       case 'p':
151          path = optarg;
152          break;
153
154       case 'f':
155          path = optarg;
156          break;
157
158       case 'j':
159          jobids = optarg;
160          break;
161
162       case '?':
163       default:
164          usage();
165
166       }
167    }
168    argc -= optind;
169    argv += optind;
170
171    if (argc != 0) {
172       Pmsg0(0, _("Wrong number of arguments: \n"));
173       usage();
174    }
175    JCR *bjcr = new_jcr(sizeof(JCR), NULL);
176    bjcr->JobId = getpid();
177    bjcr->set_JobType(JT_CONSOLE);
178    bjcr->set_JobLevel(L_FULL);
179    bjcr->JobStatus = JS_Running;
180    bjcr->client_name = get_pool_memory(PM_FNAME);
181    pm_strcpy(bjcr->client_name, "Dummy.Client.Name");
182    bstrncpy(bjcr->Job, "bvfs_test", sizeof(bjcr->Job));
183    
184    if ((db=db_init_database(NULL, db_name, db_user, db_password,
185                             db_host, 0, NULL, 0)) == NULL) {
186       Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
187    }
188    Dmsg1(0, "db_type=%s\n", db_get_type());
189
190    if (!db_open_database(NULL, db)) {
191       Emsg0(M_ERROR_TERM, 0, db_strerror(db));
192    }
193    Dmsg0(200, "Database opened\n");
194    if (verbose) {
195       Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
196    }
197    
198    bjcr->db = db;
199    
200    db_sql_query(db, "DELETE FROM brestore_pathhierarchy", NULL, NULL);
201    db_sql_query(db, "DELETE FROM brestore_knownjobid", NULL, NULL);
202    db_sql_query(db, "DELETE FROM brestore_pathvisibility", NULL, NULL);
203
204    bvfs_update_cache(bjcr, db);
205    Bvfs fs(bjcr, db);
206    fs.set_handler(result_handler, &fs);
207
208    fs.set_jobids(jobids);
209    fs.update_cache();
210
211    if (path) {
212       fs.ch_dir(path);
213       fs.ls_special_dirs();
214       fs.ls_dirs();
215       fs.ls_files();
216       exit (0);
217    }
218
219    
220    Pmsg0(0, "list /\n");
221    fs.ch_dir("/");
222    fs.ls_special_dirs();
223    fs.ls_dirs();
224    fs.ls_files();
225
226    Pmsg0(0, "list /tmp/\n");
227    fs.ch_dir("/tmp/");
228    fs.ls_special_dirs();
229    fs.ls_dirs();
230    fs.ls_files();
231
232    Pmsg0(0, "list /tmp/regress/\n");
233    fs.ch_dir("/tmp/regress/");
234    fs.ls_special_dirs();
235    fs.ls_files();
236    fs.ls_dirs();
237
238    Pmsg0(0, "list /tmp/regress/build/\n");
239    fs.ch_dir("/tmp/regress/build/");
240    fs.ls_special_dirs();
241    fs.ls_dirs();
242    fs.ls_files();
243
244    fs.get_all_file_versions(1, 347, "zog4-fd");
245
246    char p[200];
247    strcpy(p, "/tmp/toto/rep/");
248    bvfs_parent_dir(p);
249    if(strcmp(p, "/tmp/toto/")) {
250       Pmsg0(000, "Error in bvfs_parent_dir\n");
251    }
252    bvfs_parent_dir(p);
253    if(strcmp(p, "/tmp/")) {
254       Pmsg0(000, "Error in bvfs_parent_dir\n");
255    }
256    bvfs_parent_dir(p);
257    if(strcmp(p, "/")) {
258       Pmsg0(000, "Error in bvfs_parent_dir\n");
259    }
260    bvfs_parent_dir(p);
261    if(strcmp(p, "")) {
262       Pmsg0(000, "Error in bvfs_parent_dir\n");
263    }
264    bvfs_parent_dir(p);
265    if(strcmp(p, "")) {
266       Pmsg0(000, "Error in bvfs_parent_dir\n");
267    }
268
269    return 0;
270 }