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