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