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