]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/cats.h
Solaris fixes -- kes21Jun02
[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, 2001, 2002 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 xxxxx_old_way_of_doing_it
48 #define db_lock(mdb)   P(mdb->mutex)
49 #define db_unlock(mdb) V(mdb->mutex)
50 #define db_lock(mdb) \
51    do { int errstat; if ((errstat=rwl_writelock(&(mdb->lock)))) \
52       e_msg(__FILE__, __LINE__, M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",\
53            strerror(errstat)); \
54    } while(0)
55
56 #define db_unlock(x) \
57    do { int errstat; if ((errstat=rwl_writeunlock(&(mdb->lock)))) \
58          e_msg(__FILE__, __LINE__, M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",\
59            strerror(errstat)); \
60    } while(0)
61 #endif
62
63  
64 #ifdef __SQL_C
65
66 #ifdef HAVE_SQLITE
67
68 #define BDB_VERSION 1
69
70 #include <sqlite.h>
71
72 /* Define opaque structure for sqlite */
73 struct sqlite {
74    char dummy;
75 };
76
77 #define IS_NUM(x)             ((x) == 1)
78 #define IS_NOT_NULL(x)        ((x) == 1)
79
80 typedef struct s_sql_field {
81    char *name;                        /* name of column */
82    uint32_t length;                   /* length */
83    uint32_t max_length;               /* max length */
84    uint32_t type;                     /* type */
85    uint32_t flags;                    /* flags */
86 } SQL_FIELD;
87
88 /*
89  * This is the "real" definition that should only be
90  * used inside sql.c and associated database interface
91  * subroutines.
92  *                    S Q L I T E
93  */
94 typedef struct s_db {
95    BQUEUE bq;                         /* queue control */
96 /* pthread_mutex_t mutex; */
97    brwlock_t lock;                    /* transaction lock */
98    struct sqlite *db;
99    char **result;
100    int nrow;                          /* nrow returned from sqlite */
101    int ncolumn;                       /* ncolum returned from sqlite */
102    int num_rows;                      /* used by code */
103    int row;                           /* seek row */
104    int have_insert_id;                /* do not have insert id */
105    int fields_defined;                /* set when fields defined */
106    int field;                         /* seek field */
107    SQL_FIELD **fields;                /* defined fields */
108    int ref_count;
109    char *db_name;
110    char *db_user;
111    char *db_password;
112    int connected;
113    char *sqlite_errmsg;               /* error message returned by sqlite */
114    POOLMEM *errmsg;                   /* nicely edited error message */
115    POOLMEM *cmd;                      /* SQL command string */
116    POOLMEM *cached_path;
117    uint32_t cached_path_id;
118 } B_DB;
119
120
121 /* 
122  * "Generic" names for easier conversion   
123  *
124  *                    S Q L I T E
125  */
126 #define sql_store_result(x)   x->result
127 #define sql_free_result(x)    my_sqlite_free_table(x)
128 #define sql_fetch_row(x)      my_sqlite_fetch_row(x)
129 #define sql_query(x, y)       my_sqlite_query(x, y)
130 #define sql_close(x)          sqlite_close(x->db)  
131 #define sql_strerror(x)       x->sqlite_errmsg?x->sqlite_errmsg:"unknown"
132 #define sql_num_rows(x)       x->nrow
133 #define sql_data_seek(x, i)   x->row = i
134 #define sql_affected_rows(x)  1
135 #define sql_insert_id(x)      sqlite_last_insert_rowid(x->db)
136 #define sql_field_seek(x, y)  my_sqlite_field_seek(x, y)
137 #define sql_fetch_field(x)    my_sqlite_fetch_field(x)
138 #define sql_num_fields(x)     (unsigned)((x)->ncolumn)
139 #define SQL_ROW               char**   
140
141
142
143 /* In cats/sqlite.c */
144 extern int     my_sqlite_query(B_DB *mdb, char *cmd);
145 extern SQL_ROW my_sqlite_fetch_row(B_DB *mdb);
146 extern void my_sqlite_free_table(B_DB *mdb);
147
148 #else
149
150 #ifdef HAVE_MYSQL
151
152 #define BDB_VERSION 1
153
154 #include <mysql.h>
155
156 /*
157  * This is the "real" definition that should only be
158  * used inside sql.c and associated database interface
159  * subroutines.
160  *
161  *                     M Y S Q L
162  */
163 typedef struct s_db {
164    BQUEUE bq;                         /* queue control */
165 /* pthread_mutex_t mutex; */
166    brwlock_t lock;                    /* transaction lock */
167    MYSQL mysql;
168    MYSQL *db;
169    MYSQL_RES *result;
170    my_ulonglong num_rows;
171    int ref_count;
172    char *db_name;
173    char *db_user;
174    char *db_password;
175    int have_insert_id;                /* do have insert_id() */
176    int connected;
177    POOLMEM *errmsg;                   /* nicely edited error message */
178    POOLMEM *cmd;                      /* SQL command string */
179    POOLMEM *cached_path;
180    uint32_t cached_path_id;
181 } B_DB;
182
183
184 /* "Generic" names for easier conversion */
185 #define sql_store_result(x)   mysql_store_result(x->db)
186 #define sql_free_result(x)    mysql_free_result(x->result)
187 #define sql_fetch_row(x)      mysql_fetch_row(x->result)
188 #define sql_query(x, y)       mysql_query(x->db, y)
189 #define sql_close(x)          mysql_close(x->db)  
190 #define sql_strerror(x)       mysql_error(x->db)
191 #define sql_num_rows(x)       mysql_num_rows(x->result)
192 #define sql_data_seek(x, i)   mysql_data_seek(x->result, i)
193 #define sql_affected_rows(x)  mysql_affected_rows(x->db)
194 #define sql_insert_id(x)      mysql_insert_id(x->db)
195 #define sql_field_seek(x, y)  mysql_field_seek(x->result, y)
196 #define sql_fetch_field(x)    mysql_fetch_field(x->result)
197 #define sql_num_fields(x)     mysql_num_fields(x->result)
198 #define SQL_ROW               MYSQL_ROW
199 #define SQL_FIELD             MYSQL_FIELD
200
201 #else  /* USE BACULA DB routines */
202
203 #define HAVE_BACULA_DB 1
204
205 /* Change this each time there is some incompatible
206  * file format change!!!!
207  */
208 #define BDB_VERSION 9                 /* file version number */
209
210 struct s_control {
211    int bdb_version;                   /* Version number */
212    uint32_t JobId;                    /* next Job Id */
213    uint32_t PoolId;                   /* next Pool Id */
214    uint32_t MediaId;                  /* next Media Id */
215    uint32_t JobMediaId;               /* next JobMedia Id */
216    uint32_t ClientId;                 /* next Client Id */
217    uint32_t FileSetId;                /* nest FileSet Id */
218    time_t time;                       /* time file written */
219 };
220
221
222 /* This is the REAL definition for using the
223  *  Bacula internal DB
224  */
225 typedef struct s_db {
226    BQUEUE bq;                         /* queue control */
227 /* pthread_mutex_t mutex;  */         /* single thread lock */
228    brwlock_t lock;                    /* transaction lock */
229    int ref_count;                     /* number of times opened */
230    struct s_control control;          /* control file structure */
231    int cfd;                           /* control file device */
232    FILE *jobfd;                       /* Jobs records file descriptor */
233    FILE *poolfd;                      /* Pool records fd */
234    FILE *mediafd;                     /* Media records fd */
235    FILE *jobmediafd;                  /* JobMedia records fd */
236    FILE *clientfd;                    /* Client records fd */
237    FILE *filesetfd;                   /* FileSet records fd */
238    char *db_name;                     /* name of database */
239    POOLMEM *errmsg;                   /* nicely edited error message */
240    POOLMEM *cmd;                      /* Command string */
241    POOLMEM *cached_path;
242    uint32_t cached_path_id;
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    btime_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_NAME_LENGTH];
309    char cStartTime[MAX_NAME_LENGTH];
310    char cEndTime[MAX_NAME_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
332 /* Attributes record -- NOT same as in database because
333  *  in general, this "record" creates multiple database
334  *  records (e.g. pathname, filename, fileattributes).
335  */
336 typedef struct {
337    char *fname;                       /* full path & filename */
338    char *link;                        /* link if any */
339    char *attr;                        /* attributes statp */
340    uint32_t FileIndex;
341    uint32_t Stream;
342    JobId_t  JobId;
343    uint32_t ClientId;
344    uint32_t PathId;
345    uint32_t FilenameId;
346    FileId_t FileId;
347 } ATTR_DBR;
348
349
350 /* File record -- same format as database */
351 typedef struct {
352    FileId_t FileId;
353    uint32_t FileIndex;
354    JobId_t  JobId;
355    uint32_t FilenameId;
356    uint32_t PathId;
357    char LStat[256];
358 /*   int Status; */
359    char MD5[50];
360 } FILE_DBR;
361
362 /* Pool record -- same format as database */
363 typedef struct {
364    uint32_t PoolId;
365    char Name[MAX_NAME_LENGTH];        /* Pool name */
366    uint32_t NumVols;                  /* total number of volumes */
367    uint32_t MaxVols;                  /* max allowed volumes */
368    int UseOnce;                       /* set to use once only */
369    int UseCatalog;                    /* set to use catalog */
370    int AcceptAnyVolume;               /* set to accept any volume sequence */
371    int AutoPrune;                     /* set to prune automatically */
372    int Recycle;                       /* default Vol recycle flag */
373    btime_t VolRetention;              /* retention period in seconds */
374    char PoolType[MAX_NAME_LENGTH];             
375    char LabelFormat[MAX_NAME_LENGTH];
376    /* Extra stuff not in DB */
377    faddr_t rec_addr;
378 } POOL_DBR;
379
380 /* Media record -- same as the database */
381 typedef struct {
382    uint32_t MediaId;                  /* Unique volume id */
383    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
384    char MediaType[MAX_NAME_LENGTH];   /* Media type */
385    uint32_t PoolId;                   /* Pool id */
386    time_t FirstWritten;               /* Time Volume first written */
387    time_t LastWritten;                /* Time Volume last written */
388    time_t LabelDate;                  /* Date/Time Volume labelled */
389    uint32_t VolJobs;                  /* number of jobs on this medium */
390    uint32_t VolFiles;                 /* Number of files */
391    uint32_t VolBlocks;                /* Number of blocks */
392    uint32_t VolMounts;                /* Number of times mounted */
393    uint32_t VolErrors;                /* Number of read/write errors */
394    uint32_t VolWrites;                /* Number of writes */
395    uint32_t VolReads;                 /* Number of reads */
396    uint64_t VolBytes;                 /* Number of bytes written */
397    uint64_t VolMaxBytes;              /* max bytes to write */
398    uint64_t VolCapacityBytes;         /* capacity estimate */
399    btime_t  VolRetention;             /* Volume retention in seconds */
400    int Recycle;                       /* recycle yes/no */
401    char VolStatus[20];                /* Volume status */
402    /* Extra stuff not in DB */
403    faddr_t rec_addr;                  /* found record address */
404 } MEDIA_DBR;
405
406 /* Client record -- same as the database */
407 typedef struct {
408    uint32_t ClientId;                 /* Unique Client id */
409    int AutoPrune;
410    btime_t FileRetention;
411    btime_t JobRetention;
412    char Name[MAX_NAME_LENGTH];        /* Client name */
413    char Uname[256];                   /* Uname for client */
414 } CLIENT_DBR;
415
416 /* FileSet record -- same as the database */
417 typedef struct {
418    uint32_t FileSetId;                /* Unique FileSet id */
419    char FileSet[MAX_NAME_LENGTH];     /* FileSet name */
420    char MD5[50];                      /* MD5 signature of include/exclude */
421 } FILESET_DBR;
422
423
424 #include "protos.h"
425 #include "jcr.h"
426  
427 #endif /* __SQL_H_ */