]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/cats.h
3ae8a38b2f0772f0b88feb662fe85799d1954183
[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 5
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 nrow;                          /* nrow returned from sqlite */
83    int ncolumn;                       /* ncolum returned from sqlite */
84    int num_rows;                      /* used by code */
85    int row;                           /* seek row */
86    int have_insert_id;                /* do not have insert id */
87    int fields_defined;                /* set when fields defined */
88    int field;                         /* seek field */
89    SQL_FIELD **fields;                /* defined fields */
90    int ref_count;
91    char *db_name;
92    char *db_user;
93    char *db_password;
94    int connected;
95    char *sqlite_errmsg;               /* error message returned by sqlite */
96    POOLMEM *errmsg;                   /* nicely edited error message */
97    POOLMEM *cmd;                      /* SQL command string */
98    POOLMEM *cached_path;              /* cached path name */
99    int cached_path_len;               /* length of cached path */
100    uint32_t cached_path_id;           /* cached path id */
101    int transaction;                   /* transaction started */
102    int changes;                       /* changes during transaction */
103    void *jcr;                         /* JCR or NULL */
104    POOLMEM *fname;                    /* Filename only */
105    POOLMEM *path;                     /* Path only */
106    POOLMEM *esc_name;                 /* Escaped file/path name */
107    int fnl;                           /* file name length */
108    int pnl;                           /* path name length */
109 } B_DB;
110
111
112 /* 
113  * "Generic" names for easier conversion   
114  *
115  *                    S Q L I T E
116  */
117 #define sql_store_result(x)   x->result
118 #define sql_free_result(x)    my_sqlite_free_table(x)
119 #define sql_fetch_row(x)      my_sqlite_fetch_row(x)
120 #define sql_query(x, y)       my_sqlite_query(x, y)
121 #define sql_close(x)          sqlite_close(x->db)  
122 #define sql_strerror(x)       x->sqlite_errmsg?x->sqlite_errmsg:"unknown"
123 #define sql_num_rows(x)       x->nrow
124 #define sql_data_seek(x, i)   x->row = i
125 #define sql_affected_rows(x)  1
126 #define sql_insert_id(x)      sqlite_last_insert_rowid(x->db)
127 #define sql_field_seek(x, y)  my_sqlite_field_seek(x, y)
128 #define sql_fetch_field(x)    my_sqlite_fetch_field(x)
129 #define sql_num_fields(x)     (unsigned)((x)->ncolumn)
130 #define SQL_ROW               char**   
131
132
133
134 /* In cats/sqlite.c */
135 extern int     my_sqlite_query(B_DB *mdb, char *cmd);
136 extern SQL_ROW my_sqlite_fetch_row(B_DB *mdb);
137 extern void my_sqlite_free_table(B_DB *mdb);
138
139 #else
140
141 #ifdef HAVE_MYSQL
142
143 #define BDB_VERSION 5
144
145 #include <mysql.h>
146
147 /*
148  * This is the "real" definition that should only be
149  * used inside sql.c and associated database interface
150  * subroutines.
151  *
152  *                     M Y S Q L
153  */
154 typedef struct s_db {
155    BQUEUE bq;                         /* queue control */
156    brwlock_t lock;                    /* transaction lock */
157    MYSQL mysql;
158    MYSQL *db;
159    MYSQL_RES *result;
160    my_ulonglong num_rows;
161    int ref_count;
162    char *db_name;
163    char *db_user;
164    char *db_password;
165    int have_insert_id;                /* do have insert_id() */
166    int connected;
167    POOLMEM *errmsg;                   /* nicely edited error message */
168    POOLMEM *cmd;                      /* SQL command string */
169    POOLMEM *cached_path;
170    int cached_path_len;               /* length of cached path */
171    uint32_t cached_path_id;
172    int changes;                       /* changes made to db */
173    void *jcr;                         /* JCR or NULL */
174    POOLMEM *fname;                    /* Filename only */
175    POOLMEM *path;                     /* Path only */
176    POOLMEM *esc_name;                 /* Escaped file/path name */
177    int fnl;                           /* file name length */
178    int pnl;                           /* path name length */
179 } B_DB;
180
181
182 /* "Generic" names for easier conversion */
183 #define sql_store_result(x)   mysql_store_result(x->db)
184 #define sql_free_result(x)    mysql_free_result(x->result)
185 #define sql_fetch_row(x)      mysql_fetch_row(x->result)
186 #define sql_query(x, y)       mysql_query(x->db, y)
187 #define sql_close(x)          mysql_close(x->db)  
188 #define sql_strerror(x)       mysql_error(x->db)
189 #define sql_num_rows(x)       mysql_num_rows(x->result)
190 #define sql_data_seek(x, i)   mysql_data_seek(x->result, i)
191 #define sql_affected_rows(x)  mysql_affected_rows(x->db)
192 #define sql_insert_id(x)      mysql_insert_id(x->db)
193 #define sql_field_seek(x, y)  mysql_field_seek(x->result, y)
194 #define sql_fetch_field(x)    mysql_fetch_field(x->result)
195 #define sql_num_fields(x)     mysql_num_fields(x->result)
196 #define SQL_ROW               MYSQL_ROW
197 #define SQL_FIELD             MYSQL_FIELD
198
199 #else  /* USE BACULA DB routines */
200
201 #define HAVE_BACULA_DB 1
202
203 /* Change this each time there is some incompatible
204  * file format change!!!!
205  */
206 #define BDB_VERSION 11                /* file version number */
207
208 struct s_control {
209    int bdb_version;                   /* Version number */
210    uint32_t JobId;                    /* next Job Id */
211    uint32_t PoolId;                   /* next Pool Id */
212    uint32_t MediaId;                  /* next Media Id */
213    uint32_t JobMediaId;               /* next JobMedia Id */
214    uint32_t ClientId;                 /* next Client Id */
215    uint32_t FileSetId;                /* nest FileSet Id */
216    time_t time;                       /* time file written */
217 };
218
219
220 /* This is the REAL definition for using the
221  *  Bacula internal DB
222  */
223 typedef struct s_db {
224    BQUEUE bq;                         /* queue control */
225 /* pthread_mutex_t mutex;  */         /* single thread lock */
226    brwlock_t lock;                    /* transaction lock */
227    int ref_count;                     /* number of times opened */
228    struct s_control control;          /* control file structure */
229    int cfd;                           /* control file device */
230    FILE *jobfd;                       /* Jobs records file descriptor */
231    FILE *poolfd;                      /* Pool records fd */
232    FILE *mediafd;                     /* Media records fd */
233    FILE *jobmediafd;                  /* JobMedia records fd */
234    FILE *clientfd;                    /* Client records fd */
235    FILE *filesetfd;                   /* FileSet records fd */
236    char *db_name;                     /* name of database */
237    POOLMEM *errmsg;                   /* nicely edited error message */
238    POOLMEM *cmd;                      /* Command string */
239    POOLMEM *cached_path;
240    int cached_path_len;               /* length of cached path */
241    uint32_t cached_path_id;
242    void *jcr;                         /* JCR or NULL */
243 } B_DB;
244
245 #endif /* HAVE_MYSQL */
246 #endif /* HAVE_SQLITE */
247
248 /* Use for better error location printing */
249 #define UPDATE_DB(db, cmd) UpdateDB(__FILE__, __LINE__, db, cmd)
250 #define INSERT_DB(db, cmd) InsertDB(__FILE__, __LINE__, db, cmd)
251 #define QUERY_DB(db, cmd) QueryDB(__FILE__, __LINE__, db, cmd)
252 #define DELETE_DB(db, cmd) DeleteDB(__FILE__, __LINE__, db, cmd)
253
254
255 #else    /* not __SQL_C */
256
257 /* This is a "dummy" definition for use outside of sql.c
258  */
259 typedef struct s_db {     
260    int dummy;                         /* for SunOS compiler */
261 } B_DB;  
262
263 #endif /*  __SQL_C */
264
265 /* ***FIXME*** FileId_t should be uint64_t */
266 typedef uint32_t FileId_t;
267 typedef uint32_t DBId_t;              /* general DB id type */
268 typedef uint32_t JobId_t;
269
270
271 /* Job information passed to create job record and update
272  * job record at end of job. Note, although this record
273  * contains all the fields found in the Job database record,
274  * it also contains fields found in the JobMedia record.
275  */
276 /* Job record */
277 typedef struct {
278    JobId_t JobId;
279    char Job[MAX_NAME_LENGTH];         /* Job unique name */
280    char Name[MAX_NAME_LENGTH];        /* Job base name */
281    int Type;                          /* actually char(1) */
282    int Level;                         /* actually char(1) */
283    int JobStatus;                     /* actually char(1) */
284    uint32_t ClientId;                 /* Id of client */
285    uint32_t PoolId;                   /* Id of pool */
286    uint32_t FileSetId;                /* Id of FileSet */
287    time_t SchedTime;                  /* Time job scheduled */
288    time_t StartTime;                  /* Job start time */
289    time_t EndTime;                    /* Job termination time */
290    utime_t JobTDate;                  /* Backup time/date in seconds */
291    uint32_t VolSessionId;
292    uint32_t VolSessionTime;
293    uint32_t JobFiles;
294    uint32_t JobErrors;
295    uint32_t JobMissingFiles;
296    uint64_t JobBytes;
297
298    /* Note, FirstIndex, LastIndex, Start/End File and Block
299     * are only used in the JobMedia record.
300     */
301    uint32_t FirstIndex;               /* First index this Volume */
302    uint32_t LastIndex;                /* Last index this Volume */
303    uint32_t StartFile;
304    uint32_t EndFile;
305    uint32_t StartBlock;
306    uint32_t EndBlock;
307
308    char cSchedTime[MAX_TIME_LENGTH];
309    char cStartTime[MAX_TIME_LENGTH];
310    char cEndTime[MAX_TIME_LENGTH];
311    /* Extra stuff not in DB */
312    faddr_t rec_addr;
313 } JOB_DBR;
314
315 /* Job Media information used to create the media records
316  * for each Volume used for the job.
317  */
318 /* JobMedia record */
319 typedef struct {
320    uint32_t JobMediaId;               /* record id */
321    JobId_t  JobId;                    /* JobId */
322    uint32_t MediaId;                  /* MediaId */
323    uint32_t FirstIndex;               /* First index this Volume */
324    uint32_t LastIndex;                /* Last index this Volume */
325    uint32_t StartFile;                /* File for start of data */
326    uint32_t EndFile;                  /* End file on Volume */
327    uint32_t StartBlock;               /* start block on tape */
328    uint32_t EndBlock;                 /* last block */
329 } JOBMEDIA_DBR;
330
331 /* Volume Parameter structure */
332 typedef struct {
333    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
334    uint32_t FirstIndex;               /* First index this Volume */
335    uint32_t LastIndex;                /* Last index this Volume */
336    uint32_t StartFile;                /* File for start of data */
337    uint32_t EndFile;                  /* End file on Volume */
338    uint32_t StartBlock;               /* start block on tape */
339    uint32_t EndBlock;                 /* last block */
340 } VOL_PARAMS;
341
342
343 /* Attributes record -- NOT same as in database because
344  *  in general, this "record" creates multiple database
345  *  records (e.g. pathname, filename, fileattributes).
346  */
347 typedef struct {
348    char *fname;                       /* full path & filename */
349    char *link;                        /* link if any */
350    char *attr;                        /* attributes statp */
351    uint32_t FileIndex;
352    uint32_t Stream;
353    JobId_t  JobId;
354    uint32_t ClientId;
355    uint32_t PathId;
356    uint32_t FilenameId;
357    FileId_t FileId;
358 } ATTR_DBR;
359
360
361 /* File record -- same format as database */
362 typedef struct {
363    FileId_t FileId;
364    uint32_t FileIndex;
365    JobId_t  JobId;
366    uint32_t FilenameId;
367    uint32_t PathId;
368    JobId_t  MarkId;
369    char LStat[256];
370 /*   int Status; */
371    char MD5[50];
372 } FILE_DBR;
373
374 /* Pool record -- same format as database */
375 typedef struct {
376    uint32_t PoolId;
377    char Name[MAX_NAME_LENGTH];        /* Pool name */
378    uint32_t NumVols;                  /* total number of volumes */
379    uint32_t MaxVols;                  /* max allowed volumes */
380    int UseOnce;                       /* set to use once only */
381    int UseCatalog;                    /* set to use catalog */
382    int AcceptAnyVolume;               /* set to accept any volume sequence */
383    int AutoPrune;                     /* set to prune automatically */
384    int Recycle;                       /* default Vol recycle flag */
385    utime_t  VolRetention;             /* retention period in seconds */
386    utime_t  VolUseDuration;           /* time in secs volume can be used */
387    uint32_t MaxVolJobs;               /* Max Jobs on Volume */
388    uint32_t MaxVolFiles;              /* Max files on Volume */
389    uint64_t MaxVolBytes;              /* Max bytes on Volume */
390    char PoolType[MAX_NAME_LENGTH];             
391    char LabelFormat[MAX_NAME_LENGTH];
392    /* Extra stuff not in DB */
393    faddr_t rec_addr;
394 } POOL_DBR;
395
396 /* Media record -- same as the database */
397 typedef struct {
398    uint32_t MediaId;                  /* Unique volume id */
399    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
400    char MediaType[MAX_NAME_LENGTH];   /* Media type */
401    uint32_t PoolId;                   /* Pool id */
402    time_t   FirstWritten;             /* Time Volume first written */
403    time_t   LastWritten;              /* Time Volume last written */
404    time_t   LabelDate;                /* Date/Time Volume labeled */
405    uint32_t VolJobs;                  /* number of jobs on this medium */
406    uint32_t VolFiles;                 /* Number of files */
407    uint32_t VolBlocks;                /* Number of blocks */
408    uint32_t VolMounts;                /* Number of times mounted */
409    uint32_t VolErrors;                /* Number of read/write errors */
410    uint32_t VolWrites;                /* Number of writes */
411    uint32_t VolReads;                 /* Number of reads */
412    uint64_t VolBytes;                 /* Number of bytes written */
413    uint64_t MaxVolBytes;              /* Max bytes to write to Volume */
414    uint64_t VolCapacityBytes;         /* capacity estimate */
415    utime_t  VolRetention;             /* Volume retention in seconds */
416    utime_t  VolUseDuration;           /* time in secs volume can be used */
417    uint32_t MaxVolJobs;               /* Max Jobs on Volume */
418    uint32_t MaxVolFiles;              /* Max files on Volume */
419    int      Recycle;                  /* recycle yes/no */
420    int32_t  Slot;                     /* slot in changer */
421    char VolStatus[20];                /* Volume status */
422    /* Extra stuff not in DB */
423    faddr_t rec_addr;                  /* found record address */
424    /* Since the database returns times as strings, this is how we pass
425     *   them back.
426     */
427    char    cFirstWritten[MAX_TIME_LENGTH];  /* FirstWritten returned from DB */
428    char    cLastWritten[MAX_TIME_LENGTH];  /* LastWritten returned from DB */
429    char    cLabelData[MAX_TIME_LENGTH];  /* LabelData returned from DB */
430 } MEDIA_DBR;
431
432 /* Client record -- same as the database */
433 typedef struct {
434    uint32_t ClientId;                 /* Unique Client id */
435    int AutoPrune;
436    utime_t FileRetention;
437    utime_t JobRetention;
438    char Name[MAX_NAME_LENGTH];        /* Client name */
439    char Uname[256];                   /* Uname for client */
440 } CLIENT_DBR;
441
442 /* FileSet record -- same as the database */
443 typedef struct {
444    uint32_t FileSetId;                /* Unique FileSet id */
445    char FileSet[MAX_NAME_LENGTH];     /* FileSet name */
446    char MD5[50];                      /* MD5 signature of include/exclude */
447 } FILESET_DBR;
448
449
450 #include "protos.h"
451 #include "jcr.h"
452  
453 #endif /* __SQL_H_ */