]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/cats.h
- Incremented the release number because this version requires
[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-2004 Kern Sibbald and John Walker
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 #ifndef __SQL_H_
38 #define __SQL_H_ 1
39
40
41 typedef void (DB_LIST_HANDLER)(void *, const char *);
42 typedef int (DB_RESULT_HANDLER)(void *, int, char **);
43
44 #define db_lock(mdb)   _db_lock(__FILE__, __LINE__, mdb)
45 #define db_unlock(mdb) _db_unlock(__FILE__, __LINE__, mdb)
46
47 #ifdef __SQL_C
48
49 #ifdef HAVE_SQLITE
50
51 #define BDB_VERSION 8
52
53 #include <sqlite.h>
54
55 /* Define opaque structure for sqlite */
56 struct sqlite {
57    char dummy;
58 };
59
60 #define IS_NUM(x)             ((x) == 1)
61 #define IS_NOT_NULL(x)        ((x) == 1)
62
63 typedef struct s_sql_field {
64    char *name;                        /* name of column */
65    int length;                        /* length */
66    int max_length;                    /* max length */
67    uint32_t type;                     /* type */
68    uint32_t flags;                    /* flags */
69 } SQL_FIELD;
70
71 /*
72  * This is the "real" definition that should only be
73  * used inside sql.c and associated database interface
74  * subroutines.
75  *                    S Q L I T E
76  */
77 typedef struct s_db {
78    BQUEUE bq;                         /* queue control */
79    brwlock_t lock;                    /* transaction lock */
80    struct sqlite *db;
81    char **result;
82    int status;
83    int nrow;                          /* nrow returned from sqlite */
84    int ncolumn;                       /* ncolum returned from sqlite */
85    int num_rows;                      /* used by code */
86    int row;                           /* seek row */
87    int field;                         /* seek field */
88    SQL_FIELD **fields;                /* defined fields */
89    int ref_count;
90    char *db_name;
91    char *db_user;
92    char *db_address;                  /* host name address */
93    char *db_socket;                   /* socket for local access */
94    char *db_password;
95    int  db_port;                      /* port for host name address */
96    bool connected;                    /* connection made to db */
97    bool have_insert_id;               /* do not have insert id */
98    bool fields_defined;               /* set when fields defined */
99    char *sqlite_errmsg;               /* error message returned by sqlite */
100    POOLMEM *errmsg;                   /* nicely edited error message */
101    POOLMEM *cmd;                      /* SQL command string */
102    POOLMEM *cached_path;              /* cached path name */
103    int cached_path_len;               /* length of cached path */
104    uint32_t cached_path_id;           /* cached path id */
105    int transaction;                   /* transaction started */
106    int changes;                       /* changes during transaction */
107    POOLMEM *fname;                    /* Filename only */
108    POOLMEM *path;                     /* Path only */
109    POOLMEM *esc_name;                 /* Escaped file/path name */
110    int fnl;                           /* file name length */
111    int pnl;                           /* path name length */
112 } B_DB;
113
114
115 /* 
116  * "Generic" names for easier conversion   
117  *
118  *                    S Q L I T E
119  */
120 #define sql_store_result(x)   (x)->result
121 #define sql_free_result(x)    my_sqlite_free_table(x)
122 #define sql_fetch_row(x)      my_sqlite_fetch_row(x)
123 #define sql_query(x, y)       my_sqlite_query((x), (y))
124 #define sql_close(x)          sqlite_close((x)->db)  
125 #define sql_strerror(x)       (x)->sqlite_errmsg?(x)->sqlite_errmsg:"unknown"
126 #define sql_num_rows(x)       (x)->nrow
127 #define sql_data_seek(x, i)   (x)->row = (i)
128 #define sql_affected_rows(x)  1
129 #define sql_insert_id(x,y)    sqlite_last_insert_rowid((x)->db)
130 #define sql_field_seek(x, y)  my_sqlite_field_seek((x), (y))
131 #define sql_fetch_field(x)    my_sqlite_fetch_field(x)
132 #define sql_num_fields(x)     ((x)->ncolumn)
133 #define SQL_ROW               char**   
134
135
136
137 /* In cats/sqlite.c */
138 void       my_sqlite_free_table(B_DB *mdb);
139 SQL_ROW    my_sqlite_fetch_row(B_DB *mdb);
140 int        my_sqlite_query(B_DB *mdb, char *cmd);
141 void       my_sqlite_field_seek(B_DB *mdb, int field);
142 SQL_FIELD *my_sqlite_fetch_field(B_DB *mdb);
143
144
145 #else
146
147 #ifdef HAVE_MYSQL
148
149 #define BDB_VERSION 8
150
151 #include <mysql.h>
152
153 /*
154  * This is the "real" definition that should only be
155  * used inside sql.c and associated database interface
156  * subroutines.
157  *
158  *                     M Y S Q L
159  */
160 typedef struct s_db {
161    BQUEUE bq;                         /* queue control */
162    brwlock_t lock;                    /* transaction lock */
163    MYSQL mysql;
164    MYSQL *db;
165    MYSQL_RES *result;
166    int status;
167    my_ulonglong num_rows;
168    int ref_count;
169    char *db_name;
170    char *db_user;
171    char *db_password;
172    char *db_address;                  /* host address */
173    char *db_socket;                   /* socket for local access */
174    int db_port;                       /* port of host address */
175    int have_insert_id;                /* do have insert_id() */
176    bool connected;
177    POOLMEM *errmsg;                   /* nicely edited error message */
178    POOLMEM *cmd;                      /* SQL command string */
179    POOLMEM *cached_path;
180    int cached_path_len;               /* length of cached path */
181    uint32_t cached_path_id;
182    int changes;                       /* changes made to db */
183    POOLMEM *fname;                    /* Filename only */
184    POOLMEM *path;                     /* Path only */
185    POOLMEM *esc_name;                 /* Escaped file/path name */
186    int fnl;                           /* file name length */
187    int pnl;                           /* path name length */
188 } B_DB;
189
190 #define DB_STATUS int
191
192 /* "Generic" names for easier conversion */
193 #define sql_store_result(x)   mysql_store_result((x)->db)
194 #define sql_free_result(x)    mysql_free_result((x)->result)
195 #define sql_fetch_row(x)      mysql_fetch_row((x)->result)
196 #define sql_query(x, y)       mysql_query((x)->db, (y))
197 #define sql_close(x)          mysql_close((x)->db)  
198 #define sql_strerror(x)       mysql_error((x)->db)
199 #define sql_num_rows(x)       mysql_num_rows((x)->result)
200 #define sql_data_seek(x, i)   mysql_data_seek((x)->result, (i))
201 #define sql_affected_rows(x)  mysql_affected_rows((x)->db)
202 #define sql_insert_id(x,y)    mysql_insert_id((x)->db)
203 #define sql_field_seek(x, y)  mysql_field_seek((x)->result, (y))
204 #define sql_fetch_field(x)    mysql_fetch_field((x)->result)
205 #define sql_num_fields(x)     (int)mysql_num_fields((x)->result)
206 #define SQL_ROW               MYSQL_ROW
207 #define SQL_FIELD             MYSQL_FIELD
208
209 #else
210
211 #ifdef HAVE_POSTGRESQL
212
213 #define BDB_VERSION 8
214
215 #include <libpq-fe.h>
216
217 /* TEMP: the following is taken from select OID, typname from pg_type; */
218 #define IS_NUM(x)             ((x) == 20 || (x) == 21 || (x) == 23 || (x) == 700 || (x) == 701)
219 #define IS_NOT_NULL(x)        ((x) == 1)
220
221 typedef char **POSTGRESQL_ROW;
222 typedef struct pg_field {
223         char         *name;
224         int           max_length;
225         unsigned int  type;
226         unsigned int  flags;       // 1 == not null
227 } POSTGRESQL_FIELD;
228
229
230 /*
231  * This is the "real" definition that should only be
232  * used inside sql.c and associated database interface
233  * subroutines.
234  *
235  *                     P O S T G R E S Q L
236  */
237 typedef struct s_db {
238    BQUEUE bq;                         /* queue control */
239    brwlock_t lock;                    /* transaction lock */
240    PGconn *db;
241    PGresult *result;
242    int status;
243    POSTGRESQL_ROW row;
244    POSTGRESQL_FIELD *fields;
245    int num_rows;
246    int num_fields;
247    int row_number;            /* what row number did we get via my_postgresql_data_seek? */
248    int field_number;          /* what field number did we get via my_postgresql_field_seek? */
249    int ref_count;
250    char *db_name;
251    char *db_user;
252    char *db_password;
253    char *db_address;              /* host address */
254    char *db_socket;               /* socket for local access */
255    int db_port;                   /* port of host address */
256    int have_insert_id;            /* do have insert_id() */
257    bool connected;
258    POOLMEM *errmsg;               /* nicely edited error message */
259    POOLMEM *cmd;                  /* SQL command string */
260    POOLMEM *cached_path;
261    int cached_path_len;           /* length of cached path */
262    uint32_t cached_path_id;
263    int transaction;                   /* transaction started */
264    int changes;                   /* changes made to db */
265    POOLMEM *fname;                /* Filename only */
266    POOLMEM *path;                 /* Path only */
267    POOLMEM *esc_name;             /* Escaped file/path name */
268    int fnl;                       /* file name length */
269    int pnl;                       /* path name length */
270 } B_DB;
271
272 void               my_postgresql_free_result(B_DB *mdb);
273 POSTGRESQL_ROW     my_postgresql_fetch_row  (B_DB *mdb);
274 int                my_postgresql_query      (B_DB *mdb, const char *query);
275 void               my_postgresql_data_seek  (B_DB *mdb, int row);
276 int                my_postgresql_currval    (B_DB *mdb, char *table_name);
277 void               my_postgresql_field_seek (B_DB *mdb, int row);
278 POSTGRESQL_FIELD * my_postgresql_fetch_field(B_DB *mdb);
279
280
281 /* "Generic" names for easier conversion */
282 #define sql_store_result(x)   ((x)->result)
283 #define sql_free_result(x)    my_postgresql_free_result(x)
284 #define sql_fetch_row(x)      my_postgresql_fetch_row(x)
285 #define sql_query(x, y)       my_postgresql_query((x), (y))
286 #define sql_close(x)          PQfinish((x)->db)  
287 #define sql_strerror(x)       PQresultErrorMessage((x)->result)
288 #define sql_num_rows(x)       ((unsigned) PQntuples((x)->result))
289 #define sql_data_seek(x, i)   my_postgresql_data_seek((x), (i))
290 #define sql_affected_rows(x)  ((unsigned) atoi(PQcmdTuples((x)->result)))
291 #define sql_insert_id(x,y)    my_postgresql_currval((x), (y))
292 #define sql_field_seek(x, y)  my_postgresql_field_seek((x), (y))
293 #define sql_fetch_field(x)    my_postgresql_fetch_field(x)
294 #define sql_num_fields(x)     ((x)->num_fields)
295 #define SQL_ROW               POSTGRESQL_ROW
296 #define SQL_FIELD             POSTGRESQL_FIELD
297
298 #else  /* USE BACULA DB routines */
299
300 #define HAVE_BACULA_DB 1
301
302 /* Change this each time there is some incompatible
303  * file format change!!!!
304  */
305 #define BDB_VERSION 13                /* file version number */
306
307 struct s_control {
308    int bdb_version;                   /* Version number */
309    uint32_t JobId;                    /* next Job Id */
310    uint32_t PoolId;                   /* next Pool Id */
311    uint32_t MediaId;                  /* next Media Id */
312    uint32_t JobMediaId;               /* next JobMedia Id */
313    uint32_t ClientId;                 /* next Client Id */
314    uint32_t FileSetId;                /* nest FileSet Id */
315    time_t time;                       /* time file written */
316 };
317
318
319 /* This is the REAL definition for using the
320  *  Bacula internal DB
321  */
322 typedef struct s_db {
323    BQUEUE bq;                         /* queue control */
324 /* pthread_mutex_t mutex;  */         /* single thread lock */
325    brwlock_t lock;                    /* transaction lock */
326    int ref_count;                     /* number of times opened */
327    struct s_control control;          /* control file structure */
328    int cfd;                           /* control file device */
329    FILE *jobfd;                       /* Jobs records file descriptor */
330    FILE *poolfd;                      /* Pool records fd */
331    FILE *mediafd;                     /* Media records fd */
332    FILE *jobmediafd;                  /* JobMedia records fd */
333    FILE *clientfd;                    /* Client records fd */
334    FILE *filesetfd;                   /* FileSet records fd */
335    char *db_name;                     /* name of database */
336    POOLMEM *errmsg;                   /* nicely edited error message */
337    POOLMEM *cmd;                      /* Command string */
338    POOLMEM *cached_path;
339    int cached_path_len;               /* length of cached path */
340    uint32_t cached_path_id;
341 } B_DB;
342
343 #endif /* HAVE_MYSQL */
344 #endif /* HAVE_SQLITE */
345 #endif /* HAVE_POSTGRESQL */
346
347 /* Use for better error location printing */
348 #define UPDATE_DB(jcr, db, cmd) UpdateDB(__FILE__, __LINE__, jcr, db, cmd)
349 #define INSERT_DB(jcr, db, cmd) InsertDB(__FILE__, __LINE__, jcr, db, cmd)
350 #define QUERY_DB(jcr, db, cmd) QueryDB(__FILE__, __LINE__, jcr, db, cmd)
351 #define DELETE_DB(jcr, db, cmd) DeleteDB(__FILE__, __LINE__, jcr, db, cmd)
352
353
354 #else    /* not __SQL_C */
355
356 /* This is a "dummy" definition for use outside of sql.c
357  */
358 typedef struct s_db {     
359    int dummy;                         /* for SunOS compiler */
360 } B_DB;  
361
362 #endif /*  __SQL_C */
363
364 extern uint32_t bacula_db_version;
365
366 /* ***FIXME*** FileId_t should *really* be uint64_t
367  *  but at the current time, this breaks MySQL.
368  */
369 typedef uint32_t FileId_t;
370 typedef uint32_t DBId_t;              /* general DB id type */
371 typedef uint32_t JobId_t;
372       
373 #define faddr_t long
374
375
376 /* Job information passed to create job record and update
377  * job record at end of job. Note, although this record
378  * contains all the fields found in the Job database record,
379  * it also contains fields found in the JobMedia record.
380  */
381 /* Job record */
382 struct JOB_DBR {
383    JobId_t JobId;
384    char Job[MAX_NAME_LENGTH];         /* Job unique name */
385    char Name[MAX_NAME_LENGTH];        /* Job base name */
386    int JobType;                       /* actually char(1) */
387    int JobLevel;                      /* actually char(1) */
388    int JobStatus;                     /* actually char(1) */
389    DBId_t ClientId;                   /* Id of client */
390    DBId_t PoolId;                     /* Id of pool */
391    DBId_t FileSetId;                  /* Id of FileSet */
392    time_t SchedTime;                  /* Time job scheduled */
393    time_t StartTime;                  /* Job start time */
394    time_t EndTime;                    /* Job termination time */
395    utime_t JobTDate;                  /* Backup time/date in seconds */
396    uint32_t VolSessionId;
397    uint32_t VolSessionTime;
398    uint32_t JobFiles;
399    uint32_t JobErrors;
400    uint32_t JobMissingFiles;
401    uint64_t JobBytes;
402
403    /* Note, FirstIndex, LastIndex, Start/End File and Block
404     * are only used in the JobMedia record.
405     */
406    uint32_t FirstIndex;               /* First index this Volume */
407    uint32_t LastIndex;                /* Last index this Volume */
408    uint32_t StartFile;
409    uint32_t EndFile;
410    uint32_t StartBlock;
411    uint32_t EndBlock;
412
413    char cSchedTime[MAX_TIME_LENGTH];
414    char cStartTime[MAX_TIME_LENGTH];
415    char cEndTime[MAX_TIME_LENGTH];
416    /* Extra stuff not in DB */
417    faddr_t rec_addr;
418 };
419
420 /* Job Media information used to create the media records
421  * for each Volume used for the job.
422  */
423 /* JobMedia record */
424 struct JOBMEDIA_DBR {
425    DBId_t JobMediaId;                 /* record id */
426    JobId_t  JobId;                    /* JobId */
427    DBId_t MediaId;                    /* MediaId */
428    uint32_t FirstIndex;               /* First index this Volume */
429    uint32_t LastIndex;                /* Last index this Volume */
430    uint32_t StartFile;                /* File for start of data */
431    uint32_t EndFile;                  /* End file on Volume */
432    uint32_t StartBlock;               /* start block on tape */
433    uint32_t EndBlock;                 /* last block */
434 };
435
436
437 /* Volume Parameter structure */
438 struct VOL_PARAMS {
439    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
440    uint32_t VolIndex;                 /* Volume seqence no. */ 
441    uint32_t FirstIndex;               /* First index this Volume */
442    uint32_t LastIndex;                /* Last index this Volume */
443    uint32_t StartFile;                /* File for start of data */
444    uint32_t EndFile;                  /* End file on Volume */
445    uint32_t StartBlock;               /* start block on tape */
446    uint32_t EndBlock;                 /* last block */
447 };
448
449
450 /* Attributes record -- NOT same as in database because
451  *  in general, this "record" creates multiple database
452  *  records (e.g. pathname, filename, fileattributes).
453  */
454 struct ATTR_DBR {
455    char *fname;                       /* full path & filename */
456    char *link;                        /* link if any */
457    char *attr;                        /* attributes statp */
458    uint32_t FileIndex;
459    uint32_t Stream;
460    JobId_t  JobId;
461    DBId_t ClientId;
462    DBId_t PathId;
463    DBId_t FilenameId;
464    FileId_t FileId;
465 };
466
467
468 /* File record -- same format as database */
469 struct FILE_DBR {
470    FileId_t FileId;
471    uint32_t FileIndex;
472    JobId_t  JobId;
473    DBId_t FilenameId;
474    DBId_t PathId;
475    JobId_t  MarkId;
476    char LStat[256];
477 /*   int Status; */
478    char SIG[50];
479    int SigType;                       /* NO_SIG/MD5_SIG/SHA1_SIG */
480 };
481
482 /* Pool record -- same format as database */
483 struct POOL_DBR {
484    DBId_t PoolId;
485    char Name[MAX_NAME_LENGTH];        /* Pool name */
486    uint32_t NumVols;                  /* total number of volumes */
487    uint32_t MaxVols;                  /* max allowed volumes */
488    int32_t UseOnce;                   /* set to use once only */
489    int32_t UseCatalog;                /* set to use catalog */
490    int32_t AcceptAnyVolume;           /* set to accept any volume sequence */
491    int32_t AutoPrune;                 /* set to prune automatically */
492    int32_t Recycle;                   /* default Vol recycle flag */
493    utime_t  VolRetention;             /* retention period in seconds */
494    utime_t  VolUseDuration;           /* time in secs volume can be used */
495    uint32_t MaxVolJobs;               /* Max Jobs on Volume */
496    uint32_t MaxVolFiles;              /* Max files on Volume */
497    uint64_t MaxVolBytes;              /* Max bytes on Volume */
498    char PoolType[MAX_NAME_LENGTH];             
499    char LabelFormat[MAX_NAME_LENGTH];
500    /* Extra stuff not in DB */
501    faddr_t rec_addr;
502 };
503
504 /* Media record -- same as the database */
505 struct MEDIA_DBR {
506    DBId_t MediaId;                    /* Unique volume id */
507    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
508    char MediaType[MAX_NAME_LENGTH];   /* Media type */
509    DBId_t PoolId;                     /* Pool id */
510    time_t   FirstWritten;             /* Time Volume first written */
511    time_t   LastWritten;              /* Time Volume last written */
512    time_t   LabelDate;                /* Date/Time Volume labeled */
513    uint32_t VolJobs;                  /* number of jobs on this medium */
514    uint32_t VolFiles;                 /* Number of files */
515    uint32_t VolBlocks;                /* Number of blocks */
516    uint32_t VolMounts;                /* Number of times mounted */
517    uint32_t VolErrors;                /* Number of read/write errors */
518    uint32_t VolWrites;                /* Number of writes */
519    uint32_t VolReads;                 /* Number of reads */
520    uint64_t VolBytes;                 /* Number of bytes written */
521    uint64_t MaxVolBytes;              /* Max bytes to write to Volume */
522    uint64_t VolCapacityBytes;         /* capacity estimate */
523    uint64_t VolReadTime;              /* time spent reading volume */
524    uint64_t VolWriteTime;             /* time spent writing volume */
525    utime_t  VolRetention;             /* Volume retention in seconds */
526    utime_t  VolUseDuration;           /* time in secs volume can be used */
527    uint32_t MaxVolJobs;               /* Max Jobs on Volume */
528    uint32_t MaxVolFiles;              /* Max files on Volume */
529    int32_t  Recycle;                  /* recycle yes/no */
530    int32_t  Slot;                     /* slot in changer */
531    int32_t  InChanger;                /* Volume currently in changer */
532    uint32_t EndFile;                  /* Last file on volume */
533    uint32_t EndBlock;                 /* Last block on volume */
534    char VolStatus[20];                /* Volume status */
535    /* Extra stuff not in DB */
536    faddr_t rec_addr;                  /* found record address */
537    /* Since the database returns times as strings, this is how we pass
538     *   them back.
539     */
540    char    cFirstWritten[MAX_TIME_LENGTH]; /* FirstWritten returned from DB */
541    char    cLastWritten[MAX_TIME_LENGTH];  /* LastWritten returned from DB */
542    char    cLabelData[MAX_TIME_LENGTH];    /* LabelData returned from DB */
543 };
544
545 /* Client record -- same as the database */
546 struct CLIENT_DBR {
547    DBId_t ClientId;                   /* Unique Client id */
548    int AutoPrune;
549    utime_t FileRetention;
550    utime_t JobRetention;
551    char Name[MAX_NAME_LENGTH];        /* Client name */
552    char Uname[256];                   /* Uname for client */
553 };
554
555 /* Counter record as in database */
556 struct COUNTER_DBR {
557    char Counter[MAX_NAME_LENGTH];
558    int32_t MinValue;
559    int32_t MaxValue;
560    int32_t CurrentValue;
561    char WrapCounter[MAX_NAME_LENGTH];
562 };
563
564
565 /* FileSet record -- same as the database */
566 struct FILESET_DBR {
567    DBId_t FileSetId;                  /* Unique FileSet id */
568    char FileSet[MAX_NAME_LENGTH];     /* FileSet name */
569    char MD5[50];                      /* MD5 signature of include/exclude */
570    time_t CreateTime;                 /* date created */
571    /*
572     * This is where we return CreateTime
573     */
574    char cCreateTime[MAX_TIME_LENGTH]; /* CreateTime as returned from DB */
575    /* Not in DB but returned by db_create_fileset() */
576    bool created;                      /* set when record newly created */
577 };
578
579
580
581 #include "protos.h"
582 #include "jcr.h"
583
584 /*
585  * Some functions exported by sql.c for use withing the 
586  *   cats directory.
587  */
588 void list_result(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type);
589 void list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx);
590 int get_sql_record_max(JCR *jcr, B_DB *mdb);
591 int check_tables_version(JCR *jcr, B_DB *mdb);
592 void _db_unlock(const char *file, int line, B_DB *mdb);
593 void _db_lock(const char *file, int line, B_DB *mdb);
594  
595 #endif /* __SQL_H_ */