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