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