]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/cats.h
Update ReleaseNotes
[bacula/bacula] / bacula / src / cats / cats.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2007 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 two of the GNU General Public
10    License as published by the Free Software Foundation plus additions
11    that are listed 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 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 John Walker.
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  * SQL header file
30  *
31  *   by Kern E. Sibbald
32  *
33  *   Anyone who accesses the database will need to include
34  *   this file.
35  *
36  * This file contains definitions common to sql.c and
37  * the external world, and definitions destined only
38  * for the external world. This is control with
39  * the define __SQL_C, which is defined only in sql.c
40  *
41  *    Version $Id$
42  */
43
44 /*
45    Here is how database versions work. 
46
47    While I am working on a new release with database changes, the
48    update scripts are in the src/cats directory under the names
49    update_xxx_tables.in.  Most of the time, I make database updates
50    in one go and immediately update the version, but not always.  If
51    there are going to be several updates as is the case with version
52    1.37, then I will often forgo changing the version until the last
53    update otherwise I will end up with too many versions and a lot
54    of confusion.
55
56    When I am pretty sure there will be no more updates, I will
57    change the version from 8 to 9 (in the present case), and when I
58    am 100% sure there will be no more changes, the update script
59    will be copied to the updatedb directory with the correct name
60    (in the present case 8 to 9).
61
62    Now, in principle, each of the different DB implementations 
63    can have a different version, but in practice they are all
64    the same (simplifies things). The exception is the internal
65    database, which is no longer used, and hence, no longer changes.
66  */
67
68
69 #ifndef __SQL_H_
70 #define __SQL_H_ 1
71
72
73 typedef void (DB_LIST_HANDLER)(void *, const char *);
74 typedef int (DB_RESULT_HANDLER)(void *, int, char **);
75
76 #define db_lock(mdb)   _db_lock(__FILE__, __LINE__, mdb)
77 #define db_unlock(mdb) _db_unlock(__FILE__, __LINE__, mdb)
78
79 #ifdef __SQL_C
80
81 #if defined(BUILDING_CATS)
82 #ifdef HAVE_SQLITE
83
84 #define BDB_VERSION 10
85
86 #include <sqlite.h>
87
88 /* Define opaque structure for sqlite */
89 struct sqlite {
90    char dummy;
91 };
92
93 #define IS_NUM(x)             ((x) == 1)
94 #define IS_NOT_NULL(x)        ((x) == 1)
95
96 typedef struct s_sql_field {
97    char *name;                        /* name of column */
98    int length;                        /* length */
99    int max_length;                    /* max length */
100    uint32_t type;                     /* type */
101    uint32_t flags;                    /* flags */
102 } SQL_FIELD;
103
104 /*
105  * This is the "real" definition that should only be
106  * used inside sql.c and associated database interface
107  * subroutines.
108  *                    S Q L I T E
109  */
110 struct B_DB {
111    BQUEUE bq;                         /* queue control */
112    brwlock_t lock;                    /* transaction lock */
113    struct sqlite *db;
114    char **result;
115    int status;
116    int nrow;                          /* nrow returned from sqlite */
117    int ncolumn;                       /* ncolum returned from sqlite */
118    int num_rows;                      /* used by code */
119    int row;                           /* seek row */
120    int field;                         /* seek field */
121    SQL_FIELD **fields;                /* defined fields */
122    int ref_count;
123    char *db_name;
124    char *db_user;
125    char *db_address;                  /* host name address */
126    char *db_socket;                   /* socket for local access */
127    char *db_password;
128    int  db_port;                      /* port for host name address */
129    bool connected;                    /* connection made to db */
130    bool have_insert_id;               /* do not have insert id */
131    bool fields_defined;               /* set when fields defined */
132    char *sqlite_errmsg;               /* error message returned by sqlite */
133    POOLMEM *errmsg;                   /* nicely edited error message */
134    POOLMEM *cmd;                      /* SQL command string */
135    POOLMEM *cached_path;              /* cached path name */
136    int cached_path_len;               /* length of cached path */
137    uint32_t cached_path_id;           /* cached path id */
138    bool allow_transactions;           /* transactions allowed */
139    bool transaction;                  /* transaction started */
140    int changes;                       /* changes during transaction */
141    POOLMEM *fname;                    /* Filename only */
142    POOLMEM *path;                     /* Path only */
143    POOLMEM *esc_name;                 /* Escaped file name */
144    POOLMEM *esc_path;                 /* Escaped path name */
145    int fnl;                           /* file name length */
146    int pnl;                           /* path name length */
147 };
148
149
150 /*
151  * "Generic" names for easier conversion
152  *
153  *                    S Q L I T E
154  */
155 #define sql_store_result(x)   (x)->result
156 #define sql_free_result(x)    my_sqlite_free_table(x)
157 #define sql_fetch_row(x)      my_sqlite_fetch_row(x)
158 #define sql_query(x, y)       my_sqlite_query((x), (y))
159 #ifdef HAVE_SQLITE3
160 #define sql_insert_id(x,y)    sqlite3_last_insert_rowid((x)->db)
161 #define sql_close(x)          sqlite3_close((x)->db)
162 #else
163 #define sql_insert_id(x,y)    sqlite_last_insert_rowid((x)->db)
164 #define sql_close(x)          sqlite_close((x)->db)
165 #endif
166 #define sql_strerror(x)       (x)->sqlite_errmsg?(x)->sqlite_errmsg:"unknown"
167 #define sql_num_rows(x)       (x)->nrow
168 #define sql_data_seek(x, i)   (x)->row = (i)
169 #define sql_affected_rows(x)  1
170 #define sql_field_seek(x, y)  my_sqlite_field_seek((x), (y))
171 #define sql_fetch_field(x)    my_sqlite_fetch_field(x)
172 #define sql_num_fields(x)     ((x)->ncolumn)
173 #define SQL_ROW               char**
174
175 #define sql_batch_start(x,y)    my_batch_start(x,y) 
176 #define sql_batch_end(x,y,z)    my_batch_end(x,y,z)   
177 #define sql_batch_insert(x,y,z) my_batch_insert(x,y,z)
178 #define sql_batch_lock_path_query       my_sqlite_batch_lock_query
179 #define sql_batch_lock_filename_query   my_sqlite_batch_lock_query
180 #define sql_batch_unlock_tables_query   my_sqlite_batch_unlock_query
181 #define sql_batch_fill_filename_query   my_sqlite_batch_fill_filename_query
182 #define sql_batch_fill_path_query       my_sqlite_batch_fill_path_query    
183
184 /* In cats/sqlite.c */
185 void       my_sqlite_free_table(B_DB *mdb);
186 SQL_ROW    my_sqlite_fetch_row(B_DB *mdb);
187 int        my_sqlite_query(B_DB *mdb, const char *cmd);
188 void       my_sqlite_field_seek(B_DB *mdb, int field);
189 SQL_FIELD *my_sqlite_fetch_field(B_DB *mdb);
190 extern char* my_sqlite_batch_lock_query;
191 extern char* my_sqlite_batch_unlock_query;
192 extern char* my_sqlite_batch_fill_filename_query;
193 extern char* my_sqlite_batch_fill_path_query;
194
195
196 #else
197
198 /*                    S Q L I T E 3            */
199  
200
201 #ifdef HAVE_SQLITE3
202
203
204 #define BDB_VERSION 10
205
206 #include <sqlite3.h>
207
208 /* Define opaque structure for sqlite */
209 struct sqlite3 {
210    char dummy;
211 };
212
213 #define IS_NUM(x)             ((x) == 1)
214 #define IS_NOT_NULL(x)        ((x) == 1)
215
216 typedef struct s_sql_field {
217    char *name;                        /* name of column */
218    int length;                        /* length */
219    int max_length;                    /* max length */
220    uint32_t type;                     /* type */
221    uint32_t flags;                    /* flags */
222 } SQL_FIELD;
223
224 /*
225  * This is the "real" definition that should only be
226  * used inside sql.c and associated database interface
227  * subroutines.
228  *                    S Q L I T E
229  */
230 struct B_DB {
231    BQUEUE bq;                         /* queue control */
232    brwlock_t lock;                    /* transaction lock */
233    struct sqlite3 *db;
234    char **result;
235    int status;
236    int nrow;                          /* nrow returned from sqlite */
237    int ncolumn;                       /* ncolum returned from sqlite */
238    int num_rows;                      /* used by code */
239    int row;                           /* seek row */
240    int field;                         /* seek field */
241    SQL_FIELD **fields;                /* defined fields */
242    int ref_count;
243    char *db_name;
244    char *db_user;
245    char *db_address;                  /* host name address */
246    char *db_socket;                   /* socket for local access */
247    char *db_password;
248    int  db_port;                      /* port for host name address */
249    bool connected;                    /* connection made to db */
250    bool have_insert_id;               /* do not have insert id */
251    bool fields_defined;               /* set when fields defined */
252    char *sqlite_errmsg;               /* error message returned by sqlite */
253    POOLMEM *errmsg;                   /* nicely edited error message */
254    POOLMEM *cmd;                      /* SQL command string */
255    POOLMEM *cached_path;              /* cached path name */
256    int cached_path_len;               /* length of cached path */
257    uint32_t cached_path_id;           /* cached path id */
258    bool allow_transactions;           /* transactions allowed */
259    bool transaction;                  /* transaction started */
260    int changes;                       /* changes during transaction */
261    POOLMEM *fname;                    /* Filename only */
262    POOLMEM *path;                     /* Path only */
263    POOLMEM *esc_name;                 /* Escaped file name */
264    POOLMEM *esc_path;                 /* Escaped path name */
265    int fnl;                           /* file name length */
266    int pnl;                           /* path name length */
267 };
268
269 /*
270  * Conversion of sqlite 2 names to sqlite3
271  */
272 #define sqlite_last_insert_rowid sqlite3_last_insert_rowid
273 #define sqlite_open sqlite3_open
274 #define sqlite_close sqlite3_close
275 #define sqlite_result sqlite3_result
276 #define sqlite_exec sqlite3_exec
277 #define sqlite_get_table sqlite3_get_table
278 #define sqlite_free_table sqlite3_free_table
279
280
281 /*
282  * "Generic" names for easier conversion
283  *
284  *                    S Q L I T E 3
285  */
286 #define sql_store_result(x)   (x)->result
287 #define sql_free_result(x)    my_sqlite_free_table(x)
288 #define sql_fetch_row(x)      my_sqlite_fetch_row(x)
289 #define sql_query(x, y)       my_sqlite_query((x), (y))
290 #ifdef HAVE_SQLITE3
291 #define sql_insert_id(x,y)    sqlite3_last_insert_rowid((x)->db)
292 #define sql_close(x)          sqlite3_close((x)->db)
293 #else
294 #define sql_insert_id(x,y)    sqlite_last_insert_rowid((x)->db)
295 #define sql_close(x)          sqlite_close((x)->db)
296 #endif
297 #define sql_strerror(x)       (x)->sqlite_errmsg?(x)->sqlite_errmsg:"unknown"
298 #define sql_num_rows(x)       (x)->nrow
299 #define sql_data_seek(x, i)   (x)->row = (i)
300 #define sql_affected_rows(x)  1
301 #define sql_field_seek(x, y)  my_sqlite_field_seek((x), (y))
302 #define sql_fetch_field(x)    my_sqlite_fetch_field(x)
303 #define sql_num_fields(x)     ((x)->ncolumn)
304 #define sql_batch_start(x,y)    my_batch_start(x,y)   
305 #define sql_batch_end(x,y,z)    my_batch_end(x,y,z)   
306 #define sql_batch_insert(x,y,z) my_batch_insert(x,y,z)
307 #define SQL_ROW               char**
308 #define sql_batch_lock_path_query       my_sqlite_batch_lock_query
309 #define sql_batch_lock_filename_query   my_sqlite_batch_lock_query
310 #define sql_batch_unlock_tables_query   my_sqlite_batch_unlock_query
311 #define sql_batch_fill_filename_query   my_sqlite_batch_fill_filename_query
312 #define sql_batch_fill_path_query       my_sqlite_batch_fill_path_query
313
314 /* In cats/sqlite.c */
315 void       my_sqlite_free_table(B_DB *mdb);
316 SQL_ROW    my_sqlite_fetch_row(B_DB *mdb);
317 int        my_sqlite_query(B_DB *mdb, const char *cmd);
318 void       my_sqlite_field_seek(B_DB *mdb, int field);
319 SQL_FIELD *my_sqlite_fetch_field(B_DB *mdb);
320 extern char* my_sqlite_batch_lock_query;
321 extern char* my_sqlite_batch_unlock_query;
322 extern char* my_sqlite_batch_fill_filename_query;
323 extern char* my_sqlite_batch_fill_path_query;
324
325
326 #else
327
328 #ifdef HAVE_MYSQL
329
330 #define BDB_VERSION 10
331
332 #include <mysql.h>
333
334 /*
335  * This is the "real" definition that should only be
336  * used inside sql.c and associated database interface
337  * subroutines.
338  *
339  *                     M Y S Q L
340  */
341 struct B_DB {
342    BQUEUE bq;                         /* queue control */
343    brwlock_t lock;                    /* transaction lock */
344    MYSQL mysql;
345    MYSQL *db;
346    MYSQL_RES *result;
347    int status;
348    my_ulonglong num_rows;
349    int ref_count;
350    char *db_name;
351    char *db_user;
352    char *db_password;
353    char *db_address;                  /* host address */
354    char *db_socket;                   /* socket for local access */
355    int db_port;                       /* port of host address */
356    int have_insert_id;                /* do have insert_id() */
357    bool connected;
358    POOLMEM *errmsg;                   /* nicely edited error message */
359    POOLMEM *cmd;                      /* SQL command string */
360    POOLMEM *cached_path;
361    int cached_path_len;               /* length of cached path */
362    uint32_t cached_path_id;
363    int changes;                       /* changes made to db */
364    POOLMEM *fname;                    /* Filename only */
365    POOLMEM *path;                     /* Path only */
366    POOLMEM *esc_name;                 /* Escaped file name */
367    POOLMEM *esc_path;                 /* Escaped path name */
368    int fnl;                           /* file name length */
369    int pnl;                           /* path name length */
370 };
371
372 #define DB_STATUS int
373
374 /* "Generic" names for easier conversion */
375 #define sql_store_result(x)   mysql_store_result((x)->db)
376 #define sql_use_result(x)     mysql_use_result((x)->db)
377 #define sql_free_result(x)    my_mysql_free_result(x)
378 #define sql_fetch_row(x)      mysql_fetch_row((x)->result)
379 #define sql_query(x, y)       mysql_query((x)->db, (y))
380 #define sql_close(x)          mysql_close((x)->db)
381 #define sql_strerror(x)       mysql_error((x)->db)
382 #define sql_num_rows(x)       mysql_num_rows((x)->result)
383 #define sql_data_seek(x, i)   mysql_data_seek((x)->result, (i))
384 #define sql_affected_rows(x)  mysql_affected_rows((x)->db)
385 #define sql_insert_id(x,y)    mysql_insert_id((x)->db)
386 #define sql_field_seek(x, y)  mysql_field_seek((x)->result, (y))
387 #define sql_fetch_field(x)    mysql_fetch_field((x)->result)
388 #define sql_num_fields(x)     (int)mysql_num_fields((x)->result)
389 #define SQL_ROW               MYSQL_ROW
390 #define SQL_FIELD             MYSQL_FIELD
391
392 #define sql_batch_start(x,y)    my_batch_start(x,y)
393 #define sql_batch_end(x,y,z)    my_batch_end(x,y,z)   
394 #define sql_batch_insert(x,y,z) my_batch_insert(x,y,z)   
395 #define sql_batch_lock_path_query       my_mysql_batch_lock_path_query
396 #define sql_batch_lock_filename_query   my_mysql_batch_lock_filename_query
397 #define sql_batch_unlock_tables_query   my_mysql_batch_unlock_tables_query
398 #define sql_batch_fill_filename_query   my_mysql_batch_fill_filename_query
399 #define sql_batch_fill_path_query       my_mysql_batch_fill_path_query
400
401
402 extern char* my_mysql_batch_lock_path_query;
403 extern char* my_mysql_batch_lock_filename_query;
404 extern char* my_mysql_batch_unlock_tables_query;
405 extern char* my_mysql_batch_fill_filename_query;
406 extern char* my_mysql_batch_fill_path_query;
407 extern void  my_mysql_free_result(B_DB *mdb);
408
409 #else
410
411 #ifdef HAVE_POSTGRESQL
412
413 #define BDB_VERSION 10
414
415 #include <libpq-fe.h>
416
417 /* TEMP: the following is taken from select OID, typname from pg_type; */
418 #define IS_NUM(x)        ((x) == 20 || (x) == 21 || (x) == 23 || (x) == 700 || (x) == 701)
419 #define IS_NOT_NULL(x)   ((x) == 1)
420
421 typedef char **POSTGRESQL_ROW;
422 typedef struct pg_field {
423    char         *name;
424    int           max_length;
425    unsigned int  type;
426    unsigned int  flags;       // 1 == not null
427 } POSTGRESQL_FIELD;
428
429
430 /*
431  * This is the "real" definition that should only be
432  * used inside sql.c and associated database interface
433  * subroutines.
434  *
435  *                     P O S T G R E S Q L
436  */
437 struct B_DB {
438    BQUEUE bq;                         /* queue control */
439    brwlock_t lock;                    /* transaction lock */
440    PGconn *db;
441    PGresult *result;
442    int status;
443    POSTGRESQL_ROW row;
444    POSTGRESQL_FIELD *fields;
445    int num_rows;
446    int num_fields;
447    int row_number;            /* what row number did we get via my_postgresql_data_seek? */
448    int field_number;          /* what field number did we get via my_postgresql_field_seek? */
449    int ref_count;
450    char *db_name;
451    char *db_user;
452    char *db_password;
453    char *db_address;              /* host address */
454    char *db_socket;               /* socket for local access */
455    int db_port;                   /* port of host address */
456    int have_insert_id;            /* do have insert_id() */
457    bool connected;
458    POOLMEM *errmsg;               /* nicely edited error message */
459    POOLMEM *cmd;                  /* SQL command string */
460    POOLMEM *cached_path;
461    int cached_path_len;           /* length of cached path */
462    uint32_t cached_path_id;
463    bool allow_transactions;       /* transactions allowed */
464    bool transaction;              /* transaction started */
465    int changes;                   /* changes made to db */
466    POOLMEM *fname;                /* Filename only */
467    POOLMEM *path;                 /* Path only */
468    POOLMEM *esc_name;             /* Escaped file name */
469    POOLMEM *esc_path;             /* Escaped path name */
470    int fnl;                       /* file name length */
471    int pnl;                       /* path name length */
472 };     
473
474 void               my_postgresql_free_result(B_DB *mdb);
475 POSTGRESQL_ROW     my_postgresql_fetch_row  (B_DB *mdb);
476 int                my_postgresql_query      (B_DB *mdb, const char *query);
477 void               my_postgresql_data_seek  (B_DB *mdb, int row);
478 int                my_postgresql_currval    (B_DB *mdb, char *table_name);
479 void               my_postgresql_field_seek (B_DB *mdb, int row);
480 POSTGRESQL_FIELD * my_postgresql_fetch_field(B_DB *mdb);
481
482 int my_postgresql_batch_start(JCR *jcr, B_DB *mdb);
483 int my_postgresql_batch_end(JCR *jcr, B_DB *mdb, const char *error);
484 typedef struct ATTR_DBR ATTR_DBR;
485 int my_postgresql_batch_insert(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
486 char *my_postgresql_copy_escape(char *dest, char *src, size_t len);
487
488 extern char* my_pg_batch_lock_path_query;
489 extern char* my_pg_batch_lock_filename_query;
490 extern char* my_pg_batch_unlock_tables_query;
491 extern char* my_pg_batch_fill_filename_query;
492 extern char* my_pg_batch_fill_path_query;
493
494 /* "Generic" names for easier conversion */
495 #define sql_store_result(x)   ((x)->result)
496 #define sql_free_result(x)    my_postgresql_free_result(x)
497 #define sql_fetch_row(x)      my_postgresql_fetch_row(x)
498 #define sql_query(x, y)       my_postgresql_query((x), (y))
499 #define sql_close(x)          PQfinish((x)->db)
500 #define sql_strerror(x)       PQresultErrorMessage((x)->result)
501 #define sql_num_rows(x)       ((unsigned) PQntuples((x)->result))
502 #define sql_data_seek(x, i)   my_postgresql_data_seek((x), (i))
503 #define sql_affected_rows(x)  ((unsigned) atoi(PQcmdTuples((x)->result)))
504 #define sql_insert_id(x,y)    my_postgresql_currval((x), (y))
505 #define sql_field_seek(x, y)  my_postgresql_field_seek((x), (y))
506 #define sql_fetch_field(x)    my_postgresql_fetch_field(x)
507 #define sql_num_fields(x)     ((x)->num_fields)
508
509 #define sql_batch_start(x,y)    my_postgresql_batch_start(x,y)   
510 #define sql_batch_end(x,y,z)    my_postgresql_batch_end(x,y,z)   
511 #define sql_batch_insert(x,y,z) my_postgresql_batch_insert(x,y,z)
512 #define sql_batch_lock_path_query       my_pg_batch_lock_path_query
513 #define sql_batch_lock_filename_query   my_pg_batch_lock_filename_query
514 #define sql_batch_unlock_tables_query   my_pg_batch_unlock_tables_query
515 #define sql_batch_fill_filename_query   my_pg_batch_fill_filename_query
516 #define sql_batch_fill_path_query       my_pg_batch_fill_path_query
517
518 #define SQL_ROW               POSTGRESQL_ROW
519 #define SQL_FIELD             POSTGRESQL_FIELD
520
521 #else  /* USE BACULA DB routines */
522
523 #define HAVE_BACULA_DB 1
524
525 /* Change this each time there is some incompatible
526  * file format change!!!!
527  */
528 #define BDB_VERSION 13                /* file version number */
529
530 struct s_control {
531    int bdb_version;                   /* Version number */
532    uint32_t JobId;                    /* next Job Id */
533    uint32_t PoolId;                   /* next Pool Id */
534    uint32_t MediaId;                  /* next Media Id */
535    uint32_t JobMediaId;               /* next JobMedia Id */
536    uint32_t ClientId;                 /* next Client Id */
537    uint32_t FileSetId;                /* nest FileSet Id */
538    time_t time;                       /* time file written */
539 };
540
541
542 /* This is the REAL definition for using the
543  *  Bacula internal DB
544  */
545 struct B_DB {
546    BQUEUE bq;                         /* queue control */
547 /* pthread_mutex_t mutex;  */         /* single thread lock */
548    brwlock_t lock;                    /* transaction lock */
549    int ref_count;                     /* number of times opened */
550    struct s_control control;          /* control file structure */
551    int cfd;                           /* control file device */
552    FILE *jobfd;                       /* Jobs records file descriptor */
553    FILE *poolfd;                      /* Pool records fd */
554    FILE *mediafd;                     /* Media records fd */
555    FILE *jobmediafd;                  /* JobMedia records fd */
556    FILE *clientfd;                    /* Client records fd */
557    FILE *filesetfd;                   /* FileSet records fd */
558    char *db_name;                     /* name of database */
559    POOLMEM *errmsg;                   /* nicely edited error message */
560    POOLMEM *cmd;                      /* Command string */
561    POOLMEM *cached_path;
562    int cached_path_len;               /* length of cached path */
563    uint32_t cached_path_id;
564 };
565
566 #endif /* HAVE_SQLITE3 */
567 #endif /* HAVE_MYSQL */
568 #endif /* HAVE_SQLITE */
569 #endif /* HAVE_POSTGRESQL */
570 #endif
571
572 /* Use for better error location printing */
573 #define UPDATE_DB(jcr, db, cmd) UpdateDB(__FILE__, __LINE__, jcr, db, cmd)
574 #define INSERT_DB(jcr, db, cmd) InsertDB(__FILE__, __LINE__, jcr, db, cmd)
575 #define QUERY_DB(jcr, db, cmd) QueryDB(__FILE__, __LINE__, jcr, db, cmd)
576 #define DELETE_DB(jcr, db, cmd) DeleteDB(__FILE__, __LINE__, jcr, db, cmd)
577
578
579 #else    /* not __SQL_C */
580
581 /* This is a "dummy" definition for use outside of sql.c
582  */
583 struct B_DB {
584    int dummy;                         /* for SunOS compiler */
585 };     
586
587 #endif /*  __SQL_C */
588
589 extern uint32_t bacula_db_version;
590
591 /* ***FIXME*** FileId_t should *really* be uint64_t
592  *  but at the current time, this breaks MySQL.
593  */
594 typedef uint32_t FileId_t;
595 typedef uint32_t DBId_t;              /* general DB id type */
596 typedef uint32_t JobId_t;
597
598 #define faddr_t long
599
600
601 /* Job information passed to create job record and update
602  * job record at end of job. Note, although this record
603  * contains all the fields found in the Job database record,
604  * it also contains fields found in the JobMedia record.
605  */
606 /* Job record */
607 struct JOB_DBR {
608    JobId_t JobId;
609    char Job[MAX_NAME_LENGTH];         /* Job unique name */
610    char Name[MAX_NAME_LENGTH];        /* Job base name */
611    int JobType;                       /* actually char(1) */
612    int JobLevel;                      /* actually char(1) */
613    int JobStatus;                     /* actually char(1) */
614    DBId_t ClientId;                   /* Id of client */
615    DBId_t PoolId;                     /* Id of pool */
616    DBId_t FileSetId;                  /* Id of FileSet */
617    DBId_t PriorJobId;                 /* Id of migrated (prior) job */
618    time_t SchedTime;                  /* Time job scheduled */
619    time_t StartTime;                  /* Job start time */
620    time_t EndTime;                    /* Job termination time of orig job */
621    time_t RealEndTime;                /* Job termination time of this job */
622    utime_t JobTDate;                  /* Backup time/date in seconds */
623    uint32_t VolSessionId;
624    uint32_t VolSessionTime;
625    uint32_t JobFiles;
626    uint32_t JobErrors;
627    uint32_t JobMissingFiles;
628    uint64_t JobBytes;
629    int PurgedFiles;
630    int HasBase;
631
632    /* Note, FirstIndex, LastIndex, Start/End File and Block
633     * are only used in the JobMedia record.
634     */
635    uint32_t FirstIndex;               /* First index this Volume */
636    uint32_t LastIndex;                /* Last index this Volume */
637    uint32_t StartFile;
638    uint32_t EndFile;
639    uint32_t StartBlock;
640    uint32_t EndBlock;
641
642    char cSchedTime[MAX_TIME_LENGTH];
643    char cStartTime[MAX_TIME_LENGTH];
644    char cEndTime[MAX_TIME_LENGTH];
645    char cRealEndTime[MAX_TIME_LENGTH];
646    /* Extra stuff not in DB */
647    int limit;                         /* limit records to display */
648    faddr_t rec_addr;
649 };
650
651 /* Job Media information used to create the media records
652  * for each Volume used for the job.
653  */
654 /* JobMedia record */
655 struct JOBMEDIA_DBR {
656    DBId_t JobMediaId;                 /* record id */
657    JobId_t  JobId;                    /* JobId */
658    DBId_t MediaId;                    /* MediaId */
659    uint32_t FirstIndex;               /* First index this Volume */
660    uint32_t LastIndex;                /* Last index this Volume */
661    uint32_t StartFile;                /* File for start of data */
662    uint32_t EndFile;                  /* End file on Volume */
663    uint32_t StartBlock;               /* start block on tape */
664    uint32_t EndBlock;                 /* last block */
665    uint32_t Copy;                     /* identical copy */
666 };
667
668
669 /* Volume Parameter structure */
670 struct VOL_PARAMS {
671    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
672    char MediaType[MAX_NAME_LENGTH];   /* Media Type */
673    char Storage[MAX_NAME_LENGTH];     /* Storage name */
674    uint32_t VolIndex;                 /* Volume seqence no. */
675    uint32_t FirstIndex;               /* First index this Volume */
676    uint32_t LastIndex;                /* Last index this Volume */
677    uint32_t StartFile;                /* File for start of data */
678    uint32_t EndFile;                  /* End file on Volume */
679    uint32_t StartBlock;               /* start block on tape */
680    uint32_t EndBlock;                 /* last block */
681    int32_t Slot;                      /* Slot */
682 // uint32_t Copy;                     /* identical copy */
683 // uint32_t Stripe;                   /* RAIT strip number */
684 };
685
686
687 /* Attributes record -- NOT same as in database because
688  *  in general, this "record" creates multiple database
689  *  records (e.g. pathname, filename, fileattributes).
690  */
691 struct ATTR_DBR {
692    char *fname;                       /* full path & filename */
693    char *link;                        /* link if any */
694    char *attr;                        /* attributes statp */
695    uint32_t FileIndex;
696    uint32_t Stream;
697    JobId_t  JobId;
698    DBId_t ClientId;
699    DBId_t PathId;
700    DBId_t FilenameId;
701    FileId_t FileId;
702    char *Digest;
703    int DigestType;
704 };
705
706
707 /* File record -- same format as database */
708 struct FILE_DBR {
709    FileId_t FileId;
710    uint32_t FileIndex;
711    JobId_t  JobId;
712    DBId_t FilenameId;
713    DBId_t PathId;
714    JobId_t  MarkId;
715    char LStat[256];
716    char Digest[BASE64_SIZE(CRYPTO_DIGEST_MAX_SIZE)];
717    int DigestType;                    /* NO_SIG/MD5_SIG/SHA1_SIG */
718 };
719
720 /* Pool record -- same format as database */
721 struct POOL_DBR {
722    DBId_t PoolId;
723    char Name[MAX_NAME_LENGTH];        /* Pool name */
724    uint32_t NumVols;                  /* total number of volumes */
725    uint32_t MaxVols;                  /* max allowed volumes */
726    int32_t LabelType;                 /* Bacula/ANSI/IBM */
727    int32_t UseOnce;                   /* set to use once only */
728    int32_t UseCatalog;                /* set to use catalog */
729    int32_t AcceptAnyVolume;           /* set to accept any volume sequence */
730    int32_t AutoPrune;                 /* set to prune automatically */
731    int32_t Recycle;                   /* default Vol recycle flag */
732    utime_t  VolRetention;             /* retention period in seconds */
733    utime_t  VolUseDuration;           /* time in secs volume can be used */
734    uint32_t MaxVolJobs;               /* Max Jobs on Volume */
735    uint32_t MaxVolFiles;              /* Max files on Volume */
736    uint64_t MaxVolBytes;              /* Max bytes on Volume */
737    DBId_t RecyclePoolId;              /* RecyclePool destination when media is purged */
738    char PoolType[MAX_NAME_LENGTH];
739    char LabelFormat[MAX_NAME_LENGTH];
740    /* Extra stuff not in DB */
741    faddr_t rec_addr;
742 };
743
744 class DEVICE_DBR {
745 public:
746    DBId_t DeviceId;
747    char Name[MAX_NAME_LENGTH];        /* Device name */
748    DBId_t MediaTypeId;                /* MediaType */
749    DBId_t StorageId;                  /* Storage id if autochanger */
750    uint32_t DevMounts;                /* Number of times mounted */
751    uint32_t DevErrors;                /* Number of read/write errors */
752    uint64_t DevReadBytes;             /* Number of bytes read */
753    uint64_t DevWriteBytes;            /* Number of bytew written */
754    uint64_t DevReadTime;              /* time spent reading volume */
755    uint64_t DevWriteTime;             /* time spent writing volume */
756    uint64_t DevReadTimeSincCleaning;  /* read time since cleaning */
757    uint64_t DevWriteTimeSincCleaning; /* write time since cleaning */
758    time_t   CleaningDate;             /* time last cleaned */
759    utime_t  CleaningPeriod;           /* time between cleanings */
760 };
761
762 class STORAGE_DBR {
763 public:
764    DBId_t StorageId;
765    char Name[MAX_NAME_LENGTH];        /* Device name */
766    int AutoChanger;                   /* Set if autochanger */
767
768    /* Not in database */
769    bool created;                      /* set if created by db_create ... */
770 };
771
772 class MEDIATYPE_DBR {
773 public:
774    DBId_t MediaTypeId;
775    char MediaType[MAX_NAME_LENGTH];   /* MediaType string */
776    int ReadOnly;                      /* Set if read-only */
777 };
778
779
780 /* Media record -- same as the database */
781 struct MEDIA_DBR {
782    DBId_t MediaId;                    /* Unique volume id */
783    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
784    char MediaType[MAX_NAME_LENGTH];   /* Media type */
785    DBId_t PoolId;                     /* Pool id */
786    time_t   FirstWritten;             /* Time Volume first written this usage */
787    time_t   LastWritten;              /* Time Volume last written */
788    time_t   LabelDate;                /* Date/Time Volume labeled */
789    time_t   InitialWrite;             /* Date/Time Volume first written */
790    int32_t  LabelType;                /* Label (Bacula/ANSI/IBM) */
791    uint32_t VolJobs;                  /* number of jobs on this medium */
792    uint32_t VolFiles;                 /* Number of files */
793    uint32_t VolBlocks;                /* Number of blocks */
794    uint32_t VolMounts;                /* Number of times mounted */
795    uint32_t VolErrors;                /* Number of read/write errors */
796    uint32_t VolWrites;                /* Number of writes */
797    uint32_t VolReads;                 /* Number of reads */
798    uint64_t VolBytes;                 /* Number of bytes written */
799    uint32_t VolParts;                 /* Number of parts written */
800    uint64_t MaxVolBytes;              /* Max bytes to write to Volume */
801    uint64_t VolCapacityBytes;         /* capacity estimate */
802    uint64_t VolReadTime;              /* time spent reading volume */
803    uint64_t VolWriteTime;             /* time spent writing volume */
804    utime_t  VolRetention;             /* Volume retention in seconds */
805    utime_t  VolUseDuration;           /* time in secs volume can be used */
806    uint32_t MaxVolJobs;               /* Max Jobs on Volume */
807    uint32_t MaxVolFiles;              /* Max files on Volume */
808    int32_t  Recycle;                  /* recycle yes/no */
809    int32_t  Slot;                     /* slot in changer */
810    int32_t  Enabled;                  /* 0=disabled, 1=enabled, 2=archived */
811    int32_t  InChanger;                /* Volume currently in changer */
812    DBId_t   StorageId;                /* Storage record Id */
813    uint32_t EndFile;                  /* Last file on volume */
814    uint32_t EndBlock;                 /* Last block on volume */
815    uint32_t RecycleCount;             /* Number of times recycled */
816    char     VolStatus[20];            /* Volume status */
817    DBId_t   DeviceId;                 /* Device where Vol last written */
818    DBId_t   LocationId;               /* Where Volume is -- user defined */
819    DBId_t   ScratchPoolId;            /* Where to move if scratch */
820    DBId_t   RecyclePoolId;            /* Where to move when recycled */
821    /* Extra stuff not in DB */
822    faddr_t rec_addr;                  /* found record address */
823    /* Since the database returns times as strings, this is how we pass
824     *   them back.
825     */
826    char    cFirstWritten[MAX_TIME_LENGTH]; /* FirstWritten returned from DB */
827    char    cLastWritten[MAX_TIME_LENGTH];  /* LastWritten returned from DB */
828    char    cLabelDate[MAX_TIME_LENGTH];    /* LabelData returned from DB */
829    char    cInitialWrite[MAX_TIME_LENGTH]; /* InitialWrite returned from DB */
830    bool    set_first_written;                
831    bool    set_label_date;
832 };
833
834 /* Client record -- same as the database */
835 struct CLIENT_DBR {
836    DBId_t ClientId;                   /* Unique Client id */
837    int AutoPrune;
838    utime_t FileRetention;
839    utime_t JobRetention;
840    char Name[MAX_NAME_LENGTH];        /* Client name */
841    char Uname[256];                   /* Uname for client */
842 };
843
844 /* Counter record as in database */
845 struct COUNTER_DBR {
846    char Counter[MAX_NAME_LENGTH];
847    int32_t MinValue;
848    int32_t MaxValue;
849    int32_t CurrentValue;
850    char WrapCounter[MAX_NAME_LENGTH];
851 };
852
853
854 /* FileSet record -- same as the database */
855 struct FILESET_DBR {
856    DBId_t FileSetId;                  /* Unique FileSet id */
857    char FileSet[MAX_NAME_LENGTH];     /* FileSet name */
858    char MD5[50];                      /* MD5 signature of include/exclude */
859    time_t CreateTime;                 /* date created */
860    /*
861     * This is where we return CreateTime
862     */
863    char cCreateTime[MAX_TIME_LENGTH]; /* CreateTime as returned from DB */
864    /* Not in DB but returned by db_create_fileset() */
865    bool created;                      /* set when record newly created */
866 };
867
868 /* Call back context for getting a 32/64 bit value from the database */
869 struct db_int64_ctx {
870    int64_t value;                     /* value returned */
871    int count;                         /* number of values seen */
872 };
873
874
875 #include "protos.h"
876 #include "jcr.h"
877 #include "sql_cmds.h"
878
879 /*
880  * Some functions exported by sql.c for use within the
881  *   cats directory.
882  */
883 void list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type);
884 void list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx);
885 int get_sql_record_max(JCR *jcr, B_DB *mdb);
886 bool check_tables_version(JCR *jcr, B_DB *mdb);
887 void _db_unlock(const char *file, int line, B_DB *mdb);
888 void _db_lock(const char *file, int line, B_DB *mdb);
889 const char *db_get_type(void);
890
891 void print_dashes(B_DB *mdb);
892 void print_result(B_DB *mdb);
893 int QueryDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
894 int InsertDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
895 int DeleteDB(const char *file, int line, JCR *jcr, B_DB *db, char *delete_cmd);
896 int UpdateDB(const char *file, int line, JCR *jcr, B_DB *db, char *update_cmd);
897 void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname);
898 #endif /* __SQL_H_ */