]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bbatch.c
ebl compilation fix
[bacula/bacula] / bacula / src / tools / bbatch.c
1 /*
2  *
3  *  Program to test batch mode
4  *
5  *   Eric Bollengier, March 2007
6  *
7  *
8  *   Version $Id$
9  */
10 /*
11    Bacula® - The Network Backup Solution
12
13    Copyright (C) 2001-2006 Free Software Foundation Europe e.V.
14
15    The main author of Bacula is Kern Sibbald, with contributions from
16    many others, a complete list can be found in the file AUTHORS.
17    This program is Free Software; you can redistribute it and/or
18    modify it under the terms of version two of the GNU General Public
19    License as published by the Free Software Foundation plus additions
20    that are listed in the file LICENSE.
21
22    This program is distributed in the hope that it will be useful, but
23    WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25    General Public License for more details.
26
27    You should have received a copy of the GNU General Public License
28    along with this program; if not, write to the Free Software
29    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30    02110-1301, USA.
31
32    Bacula® is a registered trademark of John Walker.
33    The licensor of Bacula is the Free Software Foundation Europe
34    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
35    Switzerland, email:ftf@fsfeurope.org.
36 */
37
38 /*
39   to create datafile
40  
41   for i in $(seq 10000 99999) ; do
42      j=$((($i % 1000) + 555))
43      echo "$i;/tmp/totabofds$j/fiddddle${j}$i;xxxLSTATxxxx;xxxxxxxMD5xxxxxx"
44   done  > dat1
45  
46   or
47
48   j=0
49   find / | while read a; do
50    j=$(($j+1))
51    echo "$j;$a;xxxLSTATxxxx;xxxxxxxMD5xxxxxx"
52   done
53  */
54
55 #include "bacula.h"
56 #include "stored/stored.h"
57 #include "findlib/find.h"
58 #include "cats/cats.h"
59  
60 /* Forward referenced functions */
61 static void do_batch();
62
63
64 /* Local variables */
65 static B_DB *db;
66
67 static const char *db_name = "bacula";
68 static const char *db_user = "bacula";
69 static const char *db_password = "";
70 static const char *db_host = NULL;
71
72 static JCR *bjcr;
73
74 char *datafile=NULL;
75
76 static void usage()
77 {
78    fprintf(stderr, _(
79 PROG_COPYRIGHT
80 "\nVersion: %s (%s)\n\n"
81 "Usage: bbatch [ options ] -w working/dir -f datafile\n"
82 "       -d <nn>           set debug level to nn\n"
83 "       -n <name>         specify the database name (default bacula)\n"
84 "       -u <user>         specify database user name (default bacula)\n"
85 "       -P <password      specify database password (default none)\n"
86 "       -h <host>         specify database host (default NULL)\n"
87 "       -w <working>      specify working directory\n"
88 "       -v                verbose\n"
89 "       -f <file>         specify data file\n"
90 "       -?                print this message\n\n"), 2001, VERSION, BDATE);
91    exit(1);
92 }
93
94 int main (int argc, char *argv[])
95 {
96    int ch;
97    setlocale(LC_ALL, "");
98    bindtextdomain("bacula", LOCALEDIR);
99    textdomain("bacula");
100    init_stack_dump();
101
102    my_name_is(argc, argv, "bbatch");
103    init_msg(NULL, NULL);
104
105    OSDependentInit();
106
107    while ((ch = getopt(argc, argv, "c:d:n:P:Su:vf:w:?")) != -1) {
108       switch (ch) {
109       case 'd':                    /* debug level */
110          debug_level = atoi(optarg);
111          if (debug_level <= 0)
112             debug_level = 1;
113          break;
114
115       case 'h':
116          db_host = optarg;
117          break;
118
119       case 'n':
120          db_name = optarg;
121          break;
122
123       case 'w':
124          working_directory = optarg;
125          break;
126
127       case 'u':
128          db_user = optarg;
129          break;
130
131       case 'P':
132          db_password = optarg;
133          break;
134
135       case 'v':
136          verbose++;
137          break;
138
139       case 'f':
140          datafile = optarg;
141          break;
142
143       case '?':
144       default:
145          usage();
146
147       }
148    }
149    argc -= optind;
150    argv += optind;
151
152    if (argc != 0) {
153       Pmsg0(0, _("Wrong number of arguments: \n"));
154       usage();
155    }
156
157    if (!datafile) {
158       Pmsg0(0, _("You must specified a data file\n"));
159       usage();
160    }
161
162 #ifdef HAVE_BATCH_FILE_INSERT
163    printf("With new Batch mode\n");
164 #else
165    printf("Without new Batch mode\n");
166 #endif
167
168    bjcr = new_jcr(sizeof(JCR), NULL);
169    bjcr->bsr = NULL;
170    bjcr->VolSessionId = 1;
171    bjcr->VolSessionTime = (uint32_t)time(NULL);
172    bjcr->NumReadVolumes = 0;
173    bjcr->NumWriteVolumes = 0;
174    bjcr->JobId = getpid();
175    bjcr->JobType = JT_CONSOLE;
176    bjcr->JobLevel = L_FULL;
177    bjcr->JobStatus = JS_Running;
178    bjcr->where = bstrdup("");
179    bjcr->job_name = get_pool_memory(PM_FNAME);
180    pm_strcpy(bjcr->job_name, "Dummy.Job.Name");
181    bjcr->client_name = get_pool_memory(PM_FNAME);
182    pm_strcpy(bjcr->client_name, "Dummy.Client.Name");
183    bstrncpy(bjcr->Job, "bbatch", sizeof(bjcr->Job));
184    bjcr->fileset_name = get_pool_memory(PM_FNAME);
185    pm_strcpy(bjcr->fileset_name, "Dummy.fileset.name");
186    bjcr->fileset_md5 = get_pool_memory(PM_FNAME);
187    pm_strcpy(bjcr->fileset_md5, "Dummy.fileset.md5");
188
189    if ((db=db_init_database(NULL, db_name, db_user, db_password,
190         db_host, 0, NULL, 0)) == NULL) {
191       Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
192    }
193    if (!db_open_database(NULL, db)) {
194       Emsg0(M_ERROR_TERM, 0, db_strerror(db));
195    }
196    Dmsg0(200, "Database opened\n");
197    if (verbose) {
198       Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
199    }
200
201    bjcr->db = db;
202
203    do_batch();
204
205    return 0;
206 }
207
208 static void fill_attr(ATTR_DBR *ar, char *data)
209 {
210    char *p;
211    char *b;
212    int index=0;
213    ar->Stream = STREAM_UNIX_ATTRIBUTES;
214    ar->JobId = getpid();
215
216    for(p = b = data; *p; p++) {
217       if (*p == ';') {
218          *p = '\0';
219          switch (index) {
220          case 0:
221             ar->FileIndex = str_to_int64(b);
222             break;
223          case 1:
224             ar->fname = b;
225             break;
226          case 2:
227             ar->attr = b;
228             break;
229          case 3:
230             ar->Digest = b;
231             break;
232          }
233          index++;
234          p++;
235       }
236    }
237 }
238
239 static void do_batch()
240 {
241    char data[1024];
242    int lineno = 0;
243    struct ATTR_DBR ar;
244    memset(&ar, 0, sizeof(ar));
245    btime_t begin = get_current_btime();
246
247    FILE *fd = fopen(datafile, "r");
248    if (!fd) {
249       Emsg1(M_ERROR_TERM, 0, _("Error opening datafile %s\n"), datafile);
250    }
251    while (fgets(data, sizeof(data)-1, fd)) {
252       strip_trailing_newline(data);
253       lineno++;
254       if (verbose && ((lineno % 5000) == 1)) {
255          printf("\r%i", lineno);
256       }
257       fill_attr(&ar, data);
258       if (!db_create_file_attributes_record(bjcr, bjcr->db, &ar)) {
259          Emsg0(M_ERROR_TERM, 0, _("Error while inserting file\n"));
260       }
261    }
262    fclose(fd);
263    db_write_batch_file_records(bjcr);
264    btime_t end = get_current_btime();
265    
266    char ed1[200], ed2[200];
267    printf("\rbegin = %s, end = %s\n", edit_int64(begin, ed1),edit_int64(end, ed2));
268    printf("Insert time = %llims\n", (end - begin) / 10000);
269    printf("Create %u files at %.2f/s\n", lineno, 
270           (lineno / ((float)((end - begin) / 1000000))));
271 }