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