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