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