]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bvfs_test.c
Switch from GPLv2 to AGPLv3
[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/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 (bvfs_is_dir(row) || bvfs_is_file(row))
84    {
85       /* display clean stuffs */
86
87       if (bvfs_is_dir(row)) {
88          pm_strcpy(attr->ofname, bvfs_basename_dir(row[BVFS_Name]));   
89       } else {
90          /* if we see the requested file, note his filenameid */
91          if (bstrcmp(row[BVFS_Name], file)) {
92             fnid = str_to_int64(row[BVFS_FilenameId]);
93          }
94          pm_strcpy(attr->ofname, row[BVFS_Name]);   
95       }
96       print_ls_output(vfs->get_jcr(), attr);
97
98    } else {
99       Pmsg5(0, "JobId=%s FileId=%s\tMd5=%s\tVolName=%s\tVolInChanger=%s\n",
100             row[BVFS_JobId], row[BVFS_FileId], row[BVFS_Md5], row[BVFS_VolName],
101             row[BVFS_VolInchanger]);
102
103       pm_strcpy(attr->ofname, file);
104       print_ls_output(vfs->get_jcr(), attr);
105    }
106    return 0;
107 }
108
109
110 /* number of thread started */
111
112 int main (int argc, char *argv[])
113 {
114    int ch;
115    char *jobids = (char *)"1";
116    char *path=NULL, *client=NULL;
117    uint64_t limit=0;
118    bool clean=false;
119    setlocale(LC_ALL, "");
120    bindtextdomain("bacula", LOCALEDIR);
121    textdomain("bacula");
122    init_stack_dump();
123
124    Dmsg0(0, "Starting bvfs_test tool\n");
125    
126    my_name_is(argc, argv, "bvfs_test");
127    init_msg(NULL, NULL);
128
129    OSDependentInit();
130
131    while ((ch = getopt(argc, argv, "h:c:l:d:n:P:Su:vf:w:?j:p:f:T")) != -1) {
132       switch (ch) {
133       case 'd':                    /* debug level */
134          if (*optarg == 't') {
135             dbg_timestamp = true;
136          } else {
137             debug_level = atoi(optarg);
138             if (debug_level <= 0) {
139                debug_level = 1;
140             }
141          }
142          break;
143       case 'l':
144          limit = str_to_int64(optarg);
145          break;
146
147       case 'c':
148          client = optarg;
149          break;
150
151       case 'h':
152          db_host = optarg;
153          break;
154
155       case 'n':
156          db_name = optarg;
157          break;
158
159       case 'w':
160          working_directory = optarg;
161          break;
162
163       case 'u':
164          db_user = optarg;
165          break;
166
167       case 'P':
168          db_password = optarg;
169          break;
170
171       case 'v':
172          verbose++;
173          break;
174
175       case 'p':
176          path = optarg;
177          break;
178
179       case 'f':
180          file = optarg;
181          break;
182
183       case 'j':
184          jobids = optarg;
185          break;
186
187       case 'T':
188          clean = true;
189          break;
190
191       case '?':
192       default:
193          usage();
194
195       }
196    }
197    argc -= optind;
198    argv += optind;
199
200    if (argc != 0) {
201       Pmsg0(0, _("Wrong number of arguments: \n"));
202       usage();
203    }
204    JCR *bjcr = new_jcr(sizeof(JCR), NULL);
205    bjcr->JobId = getpid();
206    bjcr->set_JobType(JT_CONSOLE);
207    bjcr->set_JobLevel(L_FULL);
208    bjcr->JobStatus = JS_Running;
209    bjcr->client_name = get_pool_memory(PM_FNAME);
210    pm_strcpy(bjcr->client_name, "Dummy.Client.Name");
211    bstrncpy(bjcr->Job, "bvfs_test", sizeof(bjcr->Job));
212    
213    if ((db=db_init_database(NULL, db_name, db_user, db_password,
214                             db_host, 0, NULL, 0)) == NULL) {
215       Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
216    }
217    Dmsg1(0, "db_type=%s\n", db_get_type());
218
219    if (!db_open_database(NULL, db)) {
220       Emsg0(M_ERROR_TERM, 0, db_strerror(db));
221    }
222    Dmsg0(200, "Database opened\n");
223    if (verbose) {
224       Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
225    }
226    
227    bjcr->db = db;
228
229    if (clean) {
230       Pmsg0(0, "Clean old table\n");
231       db_sql_query(db, "DELETE FROM PathHierarchy", NULL, NULL);
232       db_sql_query(db, "UPDATE Job SET HasCache=0", NULL, NULL);
233       db_sql_query(db, "DELETE FROM PathVisibility", NULL, NULL);
234       bvfs_update_cache(bjcr, db);
235    }
236
237    Bvfs fs(bjcr, db);
238    fs.set_handler(result_handler, &fs);
239
240    fs.set_jobids(jobids);
241    fs.update_cache();
242    if (limit)
243       fs.set_limit(limit);
244
245    if (path) {
246       fs.ch_dir(path);
247       fs.ls_special_dirs();
248       fs.ls_dirs();
249       while (fs.ls_files()) {
250          fs.next_offset();
251       }
252
253       if (fnid && client) {
254          Pmsg0(0, "---------------------------------------------\n");
255          Pmsg1(0, "Getting file version for %s\n", file);
256          fs.get_all_file_versions(fs.get_pwd(), fnid, client);
257       }
258
259       exit (0);
260    }
261
262    
263    Pmsg0(0, "list /\n");
264    fs.ch_dir("/");
265    fs.ls_special_dirs();
266    fs.ls_dirs();
267    fs.ls_files();
268
269    Pmsg0(0, "list /tmp/\n");
270    fs.ch_dir("/tmp/");
271    fs.ls_special_dirs();
272    fs.ls_dirs();
273    fs.ls_files();
274
275    Pmsg0(0, "list /tmp/regress/\n");
276    fs.ch_dir("/tmp/regress/");
277    fs.ls_special_dirs();
278    fs.ls_files();
279    fs.ls_dirs();
280
281    Pmsg0(0, "list /tmp/regress/build/\n");
282    fs.ch_dir("/tmp/regress/build/");
283    fs.ls_special_dirs();
284    fs.ls_dirs();
285    fs.ls_files();
286
287    fs.get_all_file_versions(1, 347, "zog4-fd");
288
289    char p[200];
290    strcpy(p, "/tmp/toto/rep/");
291    bvfs_parent_dir(p);
292    if(strcmp(p, "/tmp/toto/")) {
293       Pmsg0(000, "Error in bvfs_parent_dir\n");
294    }
295    bvfs_parent_dir(p);
296    if(strcmp(p, "/tmp/")) {
297       Pmsg0(000, "Error in bvfs_parent_dir\n");
298    }
299    bvfs_parent_dir(p);
300    if(strcmp(p, "/")) {
301       Pmsg0(000, "Error in bvfs_parent_dir\n");
302    }
303    bvfs_parent_dir(p);
304    if(strcmp(p, "")) {
305       Pmsg0(000, "Error in bvfs_parent_dir\n");
306    }
307    bvfs_parent_dir(p);
308    if(strcmp(p, "")) {
309       Pmsg0(000, "Error in bvfs_parent_dir\n");
310    }
311
312    return 0;
313 }