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