]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bvfs_test.c
Patch to add MySQL ssl access
[bacula/bacula] / bacula / src / tools / bvfs_test.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2009-2014 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20 /*
21  *
22  *  Program to test cache path
23  *
24  *   Eric Bollengier, August 2009
25  *
26  *
27  */
28
29 #include "bacula.h"
30 #include "cats/cats.h"
31 #include "cats/bvfs.h"
32 #include "findlib/find.h"
33  
34 /* Local variables */
35 static BDB *db;
36 static const char *file = "COPYRIGHT";
37 static DBId_t fnid=0;
38 static const char *db_name = "regress";
39 static const char *db_user = "regress";
40 static const char *db_password = "";
41 static const char *db_host = NULL;
42 static const char *db_ssl_key = NULL;
43 static const char *db_ssl_cert = NULL;
44 static const char *db_ssl_ca = NULL;
45 static const char *db_ssl_capath = NULL;
46 static const char *db_ssl_cipher = NULL;
47
48 static void usage()
49 {
50    fprintf(stderr, _(
51 PROG_COPYRIGHT
52 "\n%sVersion: %s (%s)\n"
53 "       -d <nn>           set debug level to <nn>\n"
54 "       -dt               print timestamp in debug output\n"
55 "       -n <name>         specify the database name (default bacula)\n"
56 "       -u <user>         specify database user name (default bacula)\n"
57 "       -P <password      specify database password (default none)\n"
58 "       -h <host>         specify database host (default NULL)\n"
59 "       -k <sslkey>       path name to the key file (default NULL)\n"
60 "       -e <sslcert>      path name to the certificate file (default NULL)\n"
61 "       -a <sslca>        path name to the CA certificate file (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, sizeof(attr->statp),  &attr->LinkFI);
82
83    if (bvfs_is_dir(row) || bvfs_is_file(row)) {
84       /* display clean stuffs */
85
86       if (bvfs_is_dir(row)) {
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_FilenameId]);
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_FileId], 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:k:e:a: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 'k':
155          db_ssl_key= optarg;
156          break;
157
158       case 'e':
159          db_ssl_cert= optarg;
160          break;
161
162       case 'a':
163          db_ssl_ca= optarg;
164          break;
165
166       case 'n':
167          db_name = optarg;
168          break;
169
170       case 'w':
171          working_directory = optarg;
172          break;
173
174       case 'u':
175          db_user = optarg;
176          break;
177
178       case 'P':
179          db_password = optarg;
180          break;
181
182       case 'v':
183          verbose++;
184          break;
185
186       case 'p':
187          path = optarg;
188          break;
189
190       case 'f':
191          file = optarg;
192          break;
193
194       case 'j':
195          jobids = optarg;
196          break;
197
198       case 'T':
199          clean = true;
200          break;
201
202       case '?':
203       default:
204          usage();
205
206       }
207    }
208    argc -= optind;
209    argv += optind;
210
211    if (argc != 0) {
212       Pmsg0(0, _("Wrong number of arguments: \n"));
213       usage();
214    }
215    JCR *bjcr = new_jcr(sizeof(JCR), NULL);
216    bjcr->JobId = getpid();
217    bjcr->setJobType(JT_CONSOLE);
218    bjcr->setJobLevel(L_FULL);
219    bjcr->JobStatus = JS_Running;
220    bjcr->client_name = get_pool_memory(PM_FNAME);
221    pm_strcpy(bjcr->client_name, "Dummy.Client.Name");
222    bstrncpy(bjcr->Job, "bvfs_test", sizeof(bjcr->Job));
223    
224    if ((db = db_init_database(NULL, NULL, db_name, db_user, db_password,
225                               db_host, 0, NULL,
226                               db_ssl_key, db_ssl_cert, db_ssl_ca,
227                               db_ssl_capath, db_ssl_cipher,
228                               false, false)) == NULL) {
229       Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
230    }
231    Dmsg1(0, "db_type=%s\n", db_get_engine_name(db));
232
233    if (!db_open_database(NULL, db)) {
234       Emsg0(M_ERROR_TERM, 0, db_strerror(db));
235    }
236    Dmsg0(200, "Database opened\n");
237    if (verbose) {
238       Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
239    }
240    
241    bjcr->db = db;
242
243    if (clean) {
244       Pmsg0(0, "Clean old table\n");
245       db_sql_query(db, "DELETE FROM PathHierarchy", NULL, NULL);
246       db_sql_query(db, "UPDATE Job SET HasCache=0", NULL, NULL);
247       db_sql_query(db, "DELETE FROM PathVisibility", NULL, NULL);
248       bvfs_update_cache(bjcr, db);
249    }
250
251    Bvfs fs(bjcr, db);
252    fs.set_handler(result_handler, &fs);
253
254    fs.set_jobids(jobids);
255    fs.update_cache();
256    if (limit)
257       fs.set_limit(limit);
258
259    if (path) {
260       fs.ch_dir(path);
261       fs.ls_special_dirs();
262       fs.ls_dirs();
263       while (fs.ls_files()) {
264          fs.next_offset();
265       }
266
267       if (fnid && client) {
268          Pmsg0(0, "---------------------------------------------\n");
269          Pmsg1(0, "Getting file version for %s\n", file);
270          fs.get_all_file_versions(fs.get_pwd(), fnid, client);
271       }
272
273       exit (0);
274    }
275
276    
277    Pmsg0(0, "list /\n");
278    fs.ch_dir("/");
279    fs.ls_special_dirs();
280    fs.ls_dirs();
281    fs.ls_files();
282
283    Pmsg0(0, "list /tmp/\n");
284    fs.ch_dir("/tmp/");
285    fs.ls_special_dirs();
286    fs.ls_dirs();
287    fs.ls_files();
288
289    Pmsg0(0, "list /tmp/regress/\n");
290    fs.ch_dir("/tmp/regress/");
291    fs.ls_special_dirs();
292    fs.ls_files();
293    fs.ls_dirs();
294
295    Pmsg0(0, "list /tmp/regress/build/\n");
296    fs.ch_dir("/tmp/regress/build/");
297    fs.ls_special_dirs();
298    fs.ls_dirs();
299    fs.ls_files();
300
301    fs.get_all_file_versions(1, 347, "zog4-fd");
302
303    char p[200];
304    strcpy(p, "/tmp/toto/rep/");
305    bvfs_parent_dir(p);
306    if(strcmp(p, "/tmp/toto/")) {
307       Pmsg0(000, "Error in bvfs_parent_dir\n");
308    }
309    bvfs_parent_dir(p);
310    if(strcmp(p, "/tmp/")) {
311       Pmsg0(000, "Error in bvfs_parent_dir\n");
312    }
313    bvfs_parent_dir(p);
314    if(strcmp(p, "/")) {
315       Pmsg0(000, "Error in bvfs_parent_dir\n");
316    }
317    bvfs_parent_dir(p);
318    if(strcmp(p, "")) {
319       Pmsg0(000, "Error in bvfs_parent_dir\n");
320    }
321    bvfs_parent_dir(p);
322    if(strcmp(p, "")) {
323       Pmsg0(000, "Error in bvfs_parent_dir\n");
324    }
325
326    return 0;
327 }