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