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