]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bvfs_test.c
b2a7ed04eee9567f6643c3b19c497f2a50633942
[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
43 static void usage()
44 {
45    fprintf(stderr, _(
46 PROG_COPYRIGHT
47 "\n%sVersion: %s (%s)\n"
48 "       -d <nn>           set debug level to <nn>\n"
49 "       -dt               print timestamp in debug output\n"
50 "       -n <name>         specify the database name (default bacula)\n"
51 "       -u <user>         specify database user name (default bacula)\n"
52 "       -P <password      specify database password (default none)\n"
53 "       -h <host>         specify database host (default NULL)\n"
54 "       -w <working>      specify working directory\n"
55 "       -j <jobids>       specify jobids\n"
56 "       -p <path>         specify path\n"
57 "       -f <file>         specify file\n"
58 "       -l <limit>        maximum tuple to fetch\n"
59 "       -T                truncate cache table before starting\n"
60 "       -v                verbose\n"
61 "       -?                print this message\n\n"), 2001, "", VERSION, BDATE);
62    exit(1);
63 }
64
65 static int result_handler(void *ctx, int fields, char **row)
66 {
67    Bvfs *vfs = (Bvfs *)ctx;
68    ATTR *attr = vfs->get_attr();
69    char empty[] = "A A A A A A A A A A A A A A";
70
71    memset(&attr->statp, 0, sizeof(struct stat));
72    decode_stat((row[BVFS_LStat] && row[BVFS_LStat][0])?row[BVFS_LStat]:empty,
73                &attr->statp, sizeof(attr->statp),  &attr->LinkFI);
74
75    if (bvfs_is_dir(row) || bvfs_is_file(row)) {
76       /* display clean stuffs */
77
78       if (bvfs_is_dir(row)) {
79          pm_strcpy(attr->ofname, bvfs_basename_dir(row[BVFS_Name]));   
80       } else {
81          /* if we see the requested file, note his filenameid */
82          if (bstrcmp(row[BVFS_Name], file)) {
83             fnid = str_to_int64(row[BVFS_FilenameId]);
84          }
85          pm_strcpy(attr->ofname, row[BVFS_Name]);   
86       }
87       print_ls_output(vfs->get_jcr(), attr);
88
89    } else {
90       Pmsg5(0, "JobId=%s FileId=%s\tMd5=%s\tVolName=%s\tVolInChanger=%s\n",
91             row[BVFS_JobId], row[BVFS_FileId], row[BVFS_Md5], row[BVFS_VolName],
92             row[BVFS_VolInchanger]);
93
94       pm_strcpy(attr->ofname, file);
95       print_ls_output(vfs->get_jcr(), attr);
96    }
97    return 0;
98 }
99
100
101 /* number of thread started */
102
103 int main (int argc, char *argv[])
104 {
105    int ch;
106    char *jobids = (char *)"1";
107    char *path=NULL, *client=NULL;
108    uint64_t limit=0;
109    bool clean=false;
110    setlocale(LC_ALL, "");
111    bindtextdomain("bacula", LOCALEDIR);
112    textdomain("bacula");
113    init_stack_dump();
114
115    Dmsg0(0, "Starting bvfs_test tool\n");
116    
117    my_name_is(argc, argv, "bvfs_test");
118    init_msg(NULL, NULL);
119
120    OSDependentInit();
121
122    while ((ch = getopt(argc, argv, "h:c:l:d:n:P:Su:vf:w:?j:p:f:T")) != -1) {
123       switch (ch) {
124       case 'd':                    /* debug level */
125          if (*optarg == 't') {
126             dbg_timestamp = true;
127          } else {
128             debug_level = atoi(optarg);
129             if (debug_level <= 0) {
130                debug_level = 1;
131             }
132          }
133          break;
134       case 'l':
135          limit = str_to_int64(optarg);
136          break;
137
138       case 'c':
139          client = optarg;
140          break;
141
142       case 'h':
143          db_host = optarg;
144          break;
145
146       case 'n':
147          db_name = optarg;
148          break;
149
150       case 'w':
151          working_directory = optarg;
152          break;
153
154       case 'u':
155          db_user = optarg;
156          break;
157
158       case 'P':
159          db_password = optarg;
160          break;
161
162       case 'v':
163          verbose++;
164          break;
165
166       case 'p':
167          path = optarg;
168          break;
169
170       case 'f':
171          file = optarg;
172          break;
173
174       case 'j':
175          jobids = optarg;
176          break;
177
178       case 'T':
179          clean = true;
180          break;
181
182       case '?':
183       default:
184          usage();
185
186       }
187    }
188    argc -= optind;
189    argv += optind;
190
191    if (argc != 0) {
192       Pmsg0(0, _("Wrong number of arguments: \n"));
193       usage();
194    }
195    JCR *bjcr = new_jcr(sizeof(JCR), NULL);
196    bjcr->JobId = getpid();
197    bjcr->setJobType(JT_CONSOLE);
198    bjcr->setJobLevel(L_FULL);
199    bjcr->JobStatus = JS_Running;
200    bjcr->client_name = get_pool_memory(PM_FNAME);
201    pm_strcpy(bjcr->client_name, "Dummy.Client.Name");
202    bstrncpy(bjcr->Job, "bvfs_test", sizeof(bjcr->Job));
203    
204    if ((db = db_init_database(NULL, NULL, db_name, db_user, db_password,
205                               db_host, 0, NULL, false, false)) == NULL) {
206       Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
207    }
208    Dmsg1(0, "db_type=%s\n", db_get_engine_name(db));
209
210    if (!db_open_database(NULL, db)) {
211       Emsg0(M_ERROR_TERM, 0, db_strerror(db));
212    }
213    Dmsg0(200, "Database opened\n");
214    if (verbose) {
215       Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
216    }
217    
218    bjcr->db = db;
219
220    if (clean) {
221       Pmsg0(0, "Clean old table\n");
222       db_sql_query(db, "DELETE FROM PathHierarchy", NULL, NULL);
223       db_sql_query(db, "UPDATE Job SET HasCache=0", NULL, NULL);
224       db_sql_query(db, "DELETE FROM PathVisibility", NULL, NULL);
225       bvfs_update_cache(bjcr, db);
226    }
227
228    Bvfs fs(bjcr, db);
229    fs.set_handler(result_handler, &fs);
230
231    fs.set_jobids(jobids);
232    fs.update_cache();
233    if (limit)
234       fs.set_limit(limit);
235
236    if (path) {
237       fs.ch_dir(path);
238       fs.ls_special_dirs();
239       fs.ls_dirs();
240       while (fs.ls_files()) {
241          fs.next_offset();
242       }
243
244       if (fnid && client) {
245          Pmsg0(0, "---------------------------------------------\n");
246          Pmsg1(0, "Getting file version for %s\n", file);
247          fs.get_all_file_versions(fs.get_pwd(), fnid, client);
248       }
249
250       exit (0);
251    }
252
253    
254    Pmsg0(0, "list /\n");
255    fs.ch_dir("/");
256    fs.ls_special_dirs();
257    fs.ls_dirs();
258    fs.ls_files();
259
260    Pmsg0(0, "list /tmp/\n");
261    fs.ch_dir("/tmp/");
262    fs.ls_special_dirs();
263    fs.ls_dirs();
264    fs.ls_files();
265
266    Pmsg0(0, "list /tmp/regress/\n");
267    fs.ch_dir("/tmp/regress/");
268    fs.ls_special_dirs();
269    fs.ls_files();
270    fs.ls_dirs();
271
272    Pmsg0(0, "list /tmp/regress/build/\n");
273    fs.ch_dir("/tmp/regress/build/");
274    fs.ls_special_dirs();
275    fs.ls_dirs();
276    fs.ls_files();
277
278    fs.get_all_file_versions(1, 347, "zog4-fd");
279
280    char p[200];
281    strcpy(p, "/tmp/toto/rep/");
282    bvfs_parent_dir(p);
283    if(strcmp(p, "/tmp/toto/")) {
284       Pmsg0(000, "Error in bvfs_parent_dir\n");
285    }
286    bvfs_parent_dir(p);
287    if(strcmp(p, "/tmp/")) {
288       Pmsg0(000, "Error in bvfs_parent_dir\n");
289    }
290    bvfs_parent_dir(p);
291    if(strcmp(p, "/")) {
292       Pmsg0(000, "Error in bvfs_parent_dir\n");
293    }
294    bvfs_parent_dir(p);
295    if(strcmp(p, "")) {
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
303    return 0;
304 }