]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bvfs_test.c
Fix compiler warning in bvfs code
[bacula/bacula] / bacula / src / tools / bvfs_test.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2009-2009 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    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 License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *
30  *  Program to test cache path
31  *
32  *   Eric Bollengier, August 2009
33  *
34  *
35  */
36
37 #include "bacula.h"
38 #include "cats/cats.h"
39 #include "cats/bvfs.h"
40 #include "findlib/find.h"
41  
42 /* Local variables */
43 static B_DB *db;
44 static const char *file = "COPYRIGHT";
45 static DBId_t fnid=0;
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 "       -l <limit>        maximum tuple to fetch\n"
67 "       -T                truncate cache table before starting\n"
68 "       -v                verbose\n"
69 "       -?                print this message\n\n"), 2001, VERSION, BDATE);
70    exit(1);
71 }
72
73 static int result_handler(void *ctx, int fields, char **row)
74 {
75    Bvfs *vfs = (Bvfs *)ctx;
76    ATTR *attr = vfs->get_attr();
77    char empty[] = "A A A A A A A A A A A A A A";
78
79    memset(&attr->statp, 0, sizeof(struct stat));
80    decode_stat((row[BVFS_LStat] && row[BVFS_LStat][0])?row[BVFS_LStat]:empty,
81                &attr->statp, &attr->LinkFI);
82
83    if (fields == BVFS_DIR_RECORD || fields == BVFS_FILE_RECORD) {
84       /* display clean stuffs */
85
86       if (fields == BVFS_DIR_RECORD) {
87          pm_strcpy(attr->ofname, bvfs_basename_dir(row[BVFS_Name]));   
88       } else {
89          /* if we see the requested file, note his filenameid */
90          if (bstrcmp(row[BVFS_Name], file)) {
91             fnid = str_to_int64(row[BVFS_Id]);
92          }
93          pm_strcpy(attr->ofname, row[BVFS_Name]);   
94       }
95       print_ls_output(vfs->get_jcr(), attr);
96
97    } else {
98       Pmsg5(0, "JobId=%s FileId=%s\tMd5=%s\tVolName=%s\tVolInChanger=%s\n",
99             row[BVFS_JobId], row[BVFS_Id], row[BVFS_Md5], row[BVFS_VolName],
100             row[BVFS_VolInchanger]);
101
102       pm_strcpy(attr->ofname, file);
103       print_ls_output(vfs->get_jcr(), attr);
104    }
105    return 0;
106 }
107
108
109 /* number of thread started */
110
111 int main (int argc, char *argv[])
112 {
113    int ch;
114    char *jobids = (char *)"1";
115    char *path=NULL, *client=NULL;
116    uint64_t limit=0;
117    bool clean=false;
118    setlocale(LC_ALL, "");
119    bindtextdomain("bacula", LOCALEDIR);
120    textdomain("bacula");
121    init_stack_dump();
122
123    Dmsg0(0, "Starting bvfs_test tool\n");
124    
125    my_name_is(argc, argv, "bvfs_test");
126    init_msg(NULL, NULL);
127
128    OSDependentInit();
129
130    while ((ch = getopt(argc, argv, "h:c:l:d:n:P:Su:vf:w:?j:p:f:T")) != -1) {
131       switch (ch) {
132       case 'd':                    /* debug level */
133          if (*optarg == 't') {
134             dbg_timestamp = true;
135          } else {
136             debug_level = atoi(optarg);
137             if (debug_level <= 0) {
138                debug_level = 1;
139             }
140          }
141          break;
142       case 'l':
143          limit = str_to_int64(optarg);
144          break;
145
146       case 'c':
147          client = optarg;
148          break;
149
150       case 'h':
151          db_host = optarg;
152          break;
153
154       case 'n':
155          db_name = optarg;
156          break;
157
158       case 'w':
159          working_directory = optarg;
160          break;
161
162       case 'u':
163          db_user = optarg;
164          break;
165
166       case 'P':
167          db_password = optarg;
168          break;
169
170       case 'v':
171          verbose++;
172          break;
173
174       case 'p':
175          path = optarg;
176          break;
177
178       case 'f':
179          file = optarg;
180          break;
181
182       case 'j':
183          jobids = optarg;
184          break;
185
186       case 'T':
187          clean = true;
188          break;
189
190       case '?':
191       default:
192          usage();
193
194       }
195    }
196    argc -= optind;
197    argv += optind;
198
199    if (argc != 0) {
200       Pmsg0(0, _("Wrong number of arguments: \n"));
201       usage();
202    }
203    JCR *bjcr = new_jcr(sizeof(JCR), NULL);
204    bjcr->JobId = getpid();
205    bjcr->set_JobType(JT_CONSOLE);
206    bjcr->set_JobLevel(L_FULL);
207    bjcr->JobStatus = JS_Running;
208    bjcr->client_name = get_pool_memory(PM_FNAME);
209    pm_strcpy(bjcr->client_name, "Dummy.Client.Name");
210    bstrncpy(bjcr->Job, "bvfs_test", sizeof(bjcr->Job));
211    
212    if ((db=db_init_database(NULL, db_name, db_user, db_password,
213                             db_host, 0, NULL, 0)) == NULL) {
214       Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
215    }
216    Dmsg1(0, "db_type=%s\n", db_get_type());
217
218    if (!db_open_database(NULL, db)) {
219       Emsg0(M_ERROR_TERM, 0, db_strerror(db));
220    }
221    Dmsg0(200, "Database opened\n");
222    if (verbose) {
223       Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
224    }
225    
226    bjcr->db = db;
227
228    if (clean) {
229       Pmsg0(0, "Clean old table\n");
230       db_sql_query(db, "DELETE FROM brestore_pathhierarchy", NULL, NULL);
231       db_sql_query(db, "DELETE FROM brestore_knownjobid", NULL, NULL);
232       db_sql_query(db, "DELETE FROM brestore_pathvisibility", NULL, NULL);
233       bvfs_update_cache(bjcr, db);
234    }
235
236    Bvfs fs(bjcr, db);
237    fs.set_handler(result_handler, &fs);
238
239    fs.set_jobids(jobids);
240    fs.update_cache();
241    if (limit)
242       fs.set_limit(limit);
243
244    if (path) {
245       fs.ch_dir(path);
246       fs.ls_special_dirs();
247       fs.ls_dirs();
248       while (fs.ls_files()) {
249          fs.next_offset();
250       }
251
252       if (fnid && client) {
253          Pmsg0(0, "---------------------------------------------\n");
254          Pmsg1(0, "Getting file version for %s\n", file);
255          fs.get_all_file_versions(fs.get_pwd(), fnid, client);
256       }
257
258       exit (0);
259    }
260
261    
262    Pmsg0(0, "list /\n");
263    fs.ch_dir("/");
264    fs.ls_special_dirs();
265    fs.ls_dirs();
266    fs.ls_files();
267
268    Pmsg0(0, "list /tmp/\n");
269    fs.ch_dir("/tmp/");
270    fs.ls_special_dirs();
271    fs.ls_dirs();
272    fs.ls_files();
273
274    Pmsg0(0, "list /tmp/regress/\n");
275    fs.ch_dir("/tmp/regress/");
276    fs.ls_special_dirs();
277    fs.ls_files();
278    fs.ls_dirs();
279
280    Pmsg0(0, "list /tmp/regress/build/\n");
281    fs.ch_dir("/tmp/regress/build/");
282    fs.ls_special_dirs();
283    fs.ls_dirs();
284    fs.ls_files();
285
286    fs.get_all_file_versions(1, 347, "zog4-fd");
287
288    char p[200];
289    strcpy(p, "/tmp/toto/rep/");
290    bvfs_parent_dir(p);
291    if(strcmp(p, "/tmp/toto/")) {
292       Pmsg0(000, "Error in bvfs_parent_dir\n");
293    }
294    bvfs_parent_dir(p);
295    if(strcmp(p, "/tmp/")) {
296       Pmsg0(000, "Error in bvfs_parent_dir\n");
297    }
298    bvfs_parent_dir(p);
299    if(strcmp(p, "/")) {
300       Pmsg0(000, "Error in bvfs_parent_dir\n");
301    }
302    bvfs_parent_dir(p);
303    if(strcmp(p, "")) {
304       Pmsg0(000, "Error in bvfs_parent_dir\n");
305    }
306    bvfs_parent_dir(p);
307    if(strcmp(p, "")) {
308       Pmsg0(000, "Error in bvfs_parent_dir\n");
309    }
310
311    return 0;
312 }