]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bvfs_test.c
add option to truncate cache table during startup for bvfs_test
[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 "       -T                truncate cache table before starting\n"
67 "       -v                verbose\n"
68 "       -?                print this message\n\n"), 2001, VERSION, BDATE);
69    exit(1);
70 }
71
72 static int result_handler(void *ctx, int fields, char **row)
73 {
74    Bvfs *vfs = (Bvfs *)ctx;
75    ATTR *attr = vfs->get_attr();
76    char *empty = "";
77
78    if (fields == BVFS_DIR_RECORD || fields == BVFS_FILE_RECORD) {
79       decode_stat((row[BVFS_LStat])?row[BVFS_LStat]:empty,
80                   &attr->statp, &attr->LinkFI);
81       if (fields == BVFS_DIR_RECORD) {
82          pm_strcpy(attr->ofname, bvfs_basename_dir(row[BVFS_Name]));   
83       } else {
84          pm_strcpy(attr->ofname, row[BVFS_Name]);   
85       }
86       print_ls_output(vfs->get_jcr(), attr);
87
88    } else {
89       Pmsg6(0, "%s\t%s\t%s\t%s\t%s\t%s",
90             row[0], row[1], row[2], row[3], row[4], row[5]);
91    }
92    return 0;
93 }
94
95
96 /* number of thread started */
97
98 int main (int argc, char *argv[])
99 {
100    int ch;
101    char *jobids="1", *path=NULL, *file=NULL;
102    bool clean=false;
103    setlocale(LC_ALL, "");
104    bindtextdomain("bacula", LOCALEDIR);
105    textdomain("bacula");
106    init_stack_dump();
107
108    Dmsg0(0, "Starting bvfs_test tool\n");
109    
110    my_name_is(argc, argv, "bvfs_test");
111    init_msg(NULL, NULL);
112
113    OSDependentInit();
114
115    while ((ch = getopt(argc, argv, "h:c:d:n:P:Su:vf:w:?j:p:f:T")) != -1) {
116       switch (ch) {
117       case 'd':                    /* debug level */
118          if (*optarg == 't') {
119             dbg_timestamp = true;
120          } else {
121             debug_level = atoi(optarg);
122             if (debug_level <= 0) {
123                debug_level = 1;
124             }
125          }
126          break;
127
128       case 'h':
129          db_host = optarg;
130          break;
131
132       case 'n':
133          db_name = optarg;
134          break;
135
136       case 'w':
137          working_directory = optarg;
138          break;
139
140       case 'u':
141          db_user = optarg;
142          break;
143
144       case 'P':
145          db_password = optarg;
146          break;
147
148       case 'v':
149          verbose++;
150          break;
151
152       case 'p':
153          path = optarg;
154          break;
155
156       case 'f':
157          path = optarg;
158          break;
159
160       case 'j':
161          jobids = optarg;
162          break;
163
164       case 'T':
165          clean = true;
166          break;
167
168       case '?':
169       default:
170          usage();
171
172       }
173    }
174    argc -= optind;
175    argv += optind;
176
177    if (argc != 0) {
178       Pmsg0(0, _("Wrong number of arguments: \n"));
179       usage();
180    }
181    JCR *bjcr = new_jcr(sizeof(JCR), NULL);
182    bjcr->JobId = getpid();
183    bjcr->set_JobType(JT_CONSOLE);
184    bjcr->set_JobLevel(L_FULL);
185    bjcr->JobStatus = JS_Running;
186    bjcr->client_name = get_pool_memory(PM_FNAME);
187    pm_strcpy(bjcr->client_name, "Dummy.Client.Name");
188    bstrncpy(bjcr->Job, "bvfs_test", sizeof(bjcr->Job));
189    
190    if ((db=db_init_database(NULL, db_name, db_user, db_password,
191                             db_host, 0, NULL, 0)) == NULL) {
192       Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
193    }
194    Dmsg1(0, "db_type=%s\n", db_get_type());
195
196    if (!db_open_database(NULL, db)) {
197       Emsg0(M_ERROR_TERM, 0, db_strerror(db));
198    }
199    Dmsg0(200, "Database opened\n");
200    if (verbose) {
201       Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
202    }
203    
204    bjcr->db = db;
205
206    if (clean) {
207       Pmsg0(0, "Clean old table");
208       db_sql_query(db, "DELETE FROM brestore_pathhierarchy", NULL, NULL);
209       db_sql_query(db, "DELETE FROM brestore_knownjobid", NULL, NULL);
210       db_sql_query(db, "DELETE FROM brestore_pathvisibility", NULL, NULL);
211       bvfs_update_cache(bjcr, db);
212    }
213
214    Bvfs fs(bjcr, db);
215    fs.set_handler(result_handler, &fs);
216
217    fs.set_jobids(jobids);
218    fs.update_cache();
219
220    if (path) {
221       fs.ch_dir(path);
222       fs.ls_special_dirs();
223       fs.ls_dirs();
224       fs.ls_files();
225       exit (0);
226    }
227
228    
229    Pmsg0(0, "list /\n");
230    fs.ch_dir("/");
231    fs.ls_special_dirs();
232    fs.ls_dirs();
233    fs.ls_files();
234
235    Pmsg0(0, "list /tmp/\n");
236    fs.ch_dir("/tmp/");
237    fs.ls_special_dirs();
238    fs.ls_dirs();
239    fs.ls_files();
240
241    Pmsg0(0, "list /tmp/regress/\n");
242    fs.ch_dir("/tmp/regress/");
243    fs.ls_special_dirs();
244    fs.ls_files();
245    fs.ls_dirs();
246
247    Pmsg0(0, "list /tmp/regress/build/\n");
248    fs.ch_dir("/tmp/regress/build/");
249    fs.ls_special_dirs();
250    fs.ls_dirs();
251    fs.ls_files();
252
253    fs.get_all_file_versions(1, 347, "zog4-fd");
254
255    char p[200];
256    strcpy(p, "/tmp/toto/rep/");
257    bvfs_parent_dir(p);
258    if(strcmp(p, "/tmp/toto/")) {
259       Pmsg0(000, "Error in bvfs_parent_dir\n");
260    }
261    bvfs_parent_dir(p);
262    if(strcmp(p, "/tmp/")) {
263       Pmsg0(000, "Error in bvfs_parent_dir\n");
264    }
265    bvfs_parent_dir(p);
266    if(strcmp(p, "/")) {
267       Pmsg0(000, "Error in bvfs_parent_dir\n");
268    }
269    bvfs_parent_dir(p);
270    if(strcmp(p, "")) {
271       Pmsg0(000, "Error in bvfs_parent_dir\n");
272    }
273    bvfs_parent_dir(p);
274    if(strcmp(p, "")) {
275       Pmsg0(000, "Error in bvfs_parent_dir\n");
276    }
277
278    return 0;
279 }