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