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