]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/cats.h
Revert to using LGPLv3 for simple scripts
[bacula/bacula] / bacula / src / cats / cats.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    Bacula® is a registered trademark of Kern Sibbald.
15 */
16 /*
17  * Catalog header file
18  *
19  *   Written by Kern E. Sibbald
20  *
21  * Anyone who accesses the database will need to include
22  * this file.
23  */
24
25 /*
26    Here is how database versions work.
27
28    While I am working on a new release with database changes, the
29    update scripts are in the src/cats directory under the names
30    update_xxx_tables.in.  Most of the time, I make database updates
31    in one go and immediately update the version, but not always.  If
32    there are going to be several updates as is the case with version
33    1.37, then I will often forgo changing the version until the last
34    update otherwise I will end up with too many versions and a lot
35    of confusion.
36
37    When I am pretty sure there will be no more updates, I will
38    change the version from 8 to 9 (in the present case), and when I
39    am 100% sure there will be no more changes, the update script
40    will be copied to the updatedb directory with the correct name
41    (in the present case 8 to 9).
42  */
43 #ifndef __CATS_H_
44 #define __CATS_H_ 1
45
46 /* ==============================================================
47  *
48  *  What follows are definitions that are used "globally" for all
49  *   the different SQL engines and both inside and external to the
50  *   cats directory.
51  */
52
53 #define faddr_t long
54
55 /*
56  * Structure used when calling db_get_query_ids()
57  *  allows the subroutine to return a list of ids.
58  */
59 class dbid_list : public SMARTALLOC {
60 public:
61    DBId_t *DBId;                      /* array of DBIds */
62    char *PurgedFiles;                 /* Array of PurgedFile flags */
63    int num_ids;                       /* num of ids actually stored */
64    int max_ids;                       /* size of id array */
65    int num_seen;                      /* number of ids processed */
66    int tot_ids;                       /* total to process */
67
68    dbid_list();                       /* in sql.c */
69    ~dbid_list();                      /* in sql.c */
70 };
71
72 /* Job information passed to create job record and update
73  * job record at end of job. Note, although this record
74  * contains all the fields found in the Job database record,
75  * it also contains fields found in the JobMedia record.
76  */
77 /* Job record */
78 struct JOB_DBR {
79    JobId_t JobId;
80    char Job[MAX_NAME_LENGTH];         /* Job unique name */
81    char Name[MAX_NAME_LENGTH];        /* Job base name */
82    int JobType;                       /* actually char(1) */
83    int JobLevel;                      /* actually char(1) */
84    int JobStatus;                     /* actually char(1) */
85    DBId_t ClientId;                   /* Id of client */
86    DBId_t PoolId;                     /* Id of pool */
87    DBId_t FileSetId;                  /* Id of FileSet */
88    DBId_t PriorJobId;                 /* Id of migrated (prior) job */
89    time_t SchedTime;                  /* Time job scheduled */
90    time_t StartTime;                  /* Job start time */
91    time_t EndTime;                    /* Job termination time of orig job */
92    time_t RealEndTime;                /* Job termination time of this job */
93    utime_t JobTDate;                  /* Backup time/date in seconds */
94    uint32_t VolSessionId;
95    uint32_t VolSessionTime;
96    uint32_t JobFiles;
97    uint32_t JobErrors;
98    uint32_t JobMissingFiles;
99    uint64_t JobBytes;
100    uint64_t ReadBytes;
101    int PurgedFiles;
102    int HasBase;
103
104    /* Note, FirstIndex, LastIndex, Start/End File and Block
105     * are only used in the JobMedia record.
106     */
107    uint32_t FirstIndex;               /* First index this Volume */
108    uint32_t LastIndex;                /* Last index this Volume */
109    uint32_t StartFile;
110    uint32_t EndFile;
111    uint32_t StartBlock;
112    uint32_t EndBlock;
113
114    char cSchedTime[MAX_TIME_LENGTH];
115    char cStartTime[MAX_TIME_LENGTH];
116    char cEndTime[MAX_TIME_LENGTH];
117    char cRealEndTime[MAX_TIME_LENGTH];
118    /* Extra stuff not in DB */
119    int limit;                         /* limit records to display */
120    faddr_t rec_addr;
121    uint32_t FileIndex;                /* added during Verify */
122 };
123
124 /* Job Media information used to create the media records
125  * for each Volume used for the job.
126  */
127 /* JobMedia record */
128 struct JOBMEDIA_DBR {
129    DBId_t JobMediaId;                 /* record id */
130    JobId_t  JobId;                    /* JobId */
131    DBId_t MediaId;                    /* MediaId */
132    uint32_t FirstIndex;               /* First index this Volume */
133    uint32_t LastIndex;                /* Last index this Volume */
134    uint32_t StartFile;                /* File for start of data */
135    uint32_t EndFile;                  /* End file on Volume */
136    uint32_t StartBlock;               /* start block on tape */
137    uint32_t EndBlock;                 /* last block */
138 // uint32_t Copy;                     /* identical copy */
139 };
140
141
142 /* Volume Parameter structure */
143 struct VOL_PARAMS {
144    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
145    char MediaType[MAX_NAME_LENGTH];   /* Media Type */
146    char Storage[MAX_NAME_LENGTH];     /* Storage name */
147    uint32_t VolIndex;                 /* Volume seqence no. */
148    uint32_t FirstIndex;               /* First index this Volume */
149    uint32_t LastIndex;                /* Last index this Volume */
150    int32_t Slot;                      /* Slot */
151    uint64_t StartAddr;                /* Start address */
152    uint64_t EndAddr;                  /* End address */
153    int32_t InChanger;                 /* InChanger flag */
154 // uint32_t Copy;                     /* identical copy */
155 // uint32_t Stripe;                   /* RAIT strip number */
156 };
157
158
159 /* Attributes record -- NOT same as in database because
160  *  in general, this "record" creates multiple database
161  *  records (e.g. pathname, filename, fileattributes).
162  */
163 struct ATTR_DBR {
164    char *fname;                       /* full path & filename */
165    char *link;                        /* link if any */
166    char *attr;                        /* attributes statp */
167    uint32_t FileIndex;
168    uint32_t Stream;
169    uint32_t FileType;
170    uint32_t DeltaSeq;
171    JobId_t  JobId;
172    DBId_t ClientId;
173    DBId_t PathId;
174    DBId_t FilenameId;
175    FileId_t FileId;
176    char *Digest;
177    int DigestType;
178 };
179
180 struct ROBJECT_DBR {
181    char *object_name;
182    char *object;
183    char *plugin_name;
184    uint32_t object_len;
185    uint32_t object_full_len;
186    uint32_t object_index;
187    int32_t  object_compression;
188    uint32_t FileIndex;
189    uint32_t Stream;
190    uint32_t FileType;
191    JobId_t  JobId;
192    DBId_t RestoreObjectId;
193 };
194
195
196 /* File record -- same format as database */
197 struct FILE_DBR {
198    FileId_t FileId;
199    uint32_t FileIndex;
200    JobId_t  JobId;
201    DBId_t FilenameId;
202    DBId_t PathId;
203    JobId_t  MarkId;
204    uint32_t DeltaSeq;
205    char LStat[256];
206    char Digest[BASE64_SIZE(CRYPTO_DIGEST_MAX_SIZE)];
207    int DigestType;                    /* NO_SIG/MD5_SIG/SHA1_SIG */
208 };
209
210 /* Pool record -- same format as database */
211 struct POOL_DBR {
212    DBId_t PoolId;
213    char Name[MAX_NAME_LENGTH];        /* Pool name */
214    uint32_t NumVols;                  /* total number of volumes */
215    uint32_t MaxVols;                  /* max allowed volumes */
216    int32_t LabelType;                 /* Bacula/ANSI/IBM */
217    int32_t UseOnce;                   /* set to use once only */
218    int32_t UseCatalog;                /* set to use catalog */
219    int32_t AcceptAnyVolume;           /* set to accept any volume sequence */
220    int32_t AutoPrune;                 /* set to prune automatically */
221    int32_t Recycle;                   /* default Vol recycle flag */
222    uint32_t ActionOnPurge;            /* action on purge, e.g. truncate the disk volume */
223    utime_t  VolRetention;             /* retention period in seconds */
224    utime_t  VolUseDuration;           /* time in secs volume can be used */
225    uint32_t MaxVolJobs;               /* Max Jobs on Volume */
226    uint32_t MaxVolFiles;              /* Max files on Volume */
227    uint64_t MaxVolBytes;              /* Max bytes on Volume */
228    DBId_t RecyclePoolId;              /* RecyclePool destination when media is purged */
229    DBId_t ScratchPoolId;              /* ScratchPool source when media is needed */
230    char PoolType[MAX_NAME_LENGTH];
231    char LabelFormat[MAX_NAME_LENGTH];
232    /* Extra stuff not in DB */
233    faddr_t rec_addr;
234 };
235
236 class DEVICE_DBR {
237 public:
238    DBId_t DeviceId;
239    char Name[MAX_NAME_LENGTH];        /* Device name */
240    DBId_t MediaTypeId;                /* MediaType */
241    DBId_t StorageId;                  /* Storage id if autochanger */
242    uint32_t DevMounts;                /* Number of times mounted */
243    uint32_t DevErrors;                /* Number of read/write errors */
244    uint64_t DevReadBytes;             /* Number of bytes read */
245    uint64_t DevWriteBytes;            /* Number of bytew written */
246    uint64_t DevReadTime;              /* time spent reading volume */
247    uint64_t DevWriteTime;             /* time spent writing volume */
248    uint64_t DevReadTimeSincCleaning;  /* read time since cleaning */
249    uint64_t DevWriteTimeSincCleaning; /* write time since cleaning */
250    time_t   CleaningDate;             /* time last cleaned */
251    utime_t  CleaningPeriod;           /* time between cleanings */
252 };
253
254 class STORAGE_DBR {
255 public:
256    DBId_t StorageId;
257    char Name[MAX_NAME_LENGTH];        /* Device name */
258    int AutoChanger;                   /* Set if autochanger */
259
260    /* Not in database */
261    bool created;                      /* set if created by db_create ... */
262 };
263
264 class MEDIATYPE_DBR {
265 public:
266    DBId_t MediaTypeId;
267    char MediaType[MAX_NAME_LENGTH];   /* MediaType string */
268    int ReadOnly;                      /* Set if read-only */
269 };
270
271
272 /* Media record -- same as the database */
273 class MEDIA_DBR {
274 public:
275    MEDIA_DBR() { memset(this, 0, sizeof(MEDIA_DBR)); };
276    ~MEDIA_DBR() {  };
277    void clear() { memset(this, 0, sizeof(MEDIA_DBR)); };
278    void copy(MEDIA_DBR *omr) { memcpy(this, omr, sizeof(MEDIA_DBR)); };
279
280    DBId_t MediaId;                    /* Unique volume id */
281    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
282    char MediaType[MAX_NAME_LENGTH];   /* Media type */
283    DBId_t PoolId;                     /* Pool id */
284    time_t   FirstWritten;             /* Time Volume first written this usage */
285    time_t   LastWritten;              /* Time Volume last written */
286    time_t   LabelDate;                /* Date/Time Volume labeled */
287    time_t   InitialWrite;             /* Date/Time Volume first written */
288    int32_t  LabelType;                /* Label (Bacula/ANSI/IBM) */
289    uint32_t VolJobs;                  /* number of jobs on this medium */
290    uint32_t VolFiles;                 /* Number of files */
291    uint32_t VolBlocks;                /* Number of blocks */
292    uint32_t VolMounts;                /* Number of times mounted */
293    uint32_t VolErrors;                /* Number of read/write errors */
294    uint32_t VolWrites;                /* Number of writes */
295    uint32_t VolReads;                 /* Number of reads */
296    uint64_t VolBytes;                 /* Number of bytes written */
297    uint32_t VolParts;                 /* Number of parts written */
298    uint64_t MaxVolBytes;              /* Max bytes to write to Volume */
299    uint64_t VolCapacityBytes;         /* capacity estimate */
300    uint64_t VolReadTime;              /* time spent reading volume */
301    uint64_t VolWriteTime;             /* time spent writing volume */
302    utime_t  VolRetention;             /* Volume retention in seconds */
303    utime_t  VolUseDuration;           /* time in secs volume can be used */
304    uint32_t ActionOnPurge;            /* action on purge, e.g. truncate the disk volume */
305    uint32_t MaxVolJobs;               /* Max Jobs on Volume */
306    uint32_t MaxVolFiles;              /* Max files on Volume */
307    int32_t  Recycle;                  /* recycle yes/no */
308    int32_t  Slot;                     /* slot in changer */
309    int32_t  Enabled;                  /* 0=disabled, 1=enabled, 2=archived */
310    int32_t  InChanger;                /* Volume currently in changer */
311    DBId_t   StorageId;                /* Storage record Id */
312    uint32_t EndFile;                  /* Last file on volume */
313    uint32_t EndBlock;                 /* Last block on volume */
314    uint32_t RecycleCount;             /* Number of times recycled */
315    char     VolStatus[20];            /* Volume status */
316    DBId_t   DeviceId;                 /* Device where Vol last written */
317    DBId_t   LocationId;               /* Where Volume is -- user defined */
318    DBId_t   ScratchPoolId;            /* Where to move if scratch */
319    DBId_t   RecyclePoolId;            /* Where to move when recycled */
320    /* Extra stuff not in DB */
321    faddr_t rec_addr;                  /* found record address */
322    /* Since the database returns times as strings, this is how we pass
323     *   them back.
324     */
325    char    cFirstWritten[MAX_TIME_LENGTH]; /* FirstWritten returned from DB */
326    char    cLastWritten[MAX_TIME_LENGTH];  /* LastWritten returned from DB */
327    char    cLabelDate[MAX_TIME_LENGTH];    /* LabelData returned from DB */
328    char    cInitialWrite[MAX_TIME_LENGTH]; /* InitialWrite returned from DB */
329    bool    set_first_written;
330    bool    set_label_date;
331 };
332
333 /* Client record -- same as the database */
334 struct CLIENT_DBR {
335    DBId_t ClientId;                   /* Unique Client id */
336    int AutoPrune;
337    utime_t FileRetention;
338    utime_t JobRetention;
339    char Name[MAX_NAME_LENGTH];        /* Client name */
340    char Uname[256];                   /* Uname for client */
341 };
342
343 /* Counter record as in database */
344 struct COUNTER_DBR {
345    char Counter[MAX_NAME_LENGTH];
346    int32_t MinValue;
347    int32_t MaxValue;
348    int32_t CurrentValue;
349    char WrapCounter[MAX_NAME_LENGTH];
350 };
351
352
353 /* FileSet record -- same as the database */
354 struct FILESET_DBR {
355    DBId_t FileSetId;                  /* Unique FileSet id */
356    char FileSet[MAX_NAME_LENGTH];     /* FileSet name */
357    char MD5[50];                      /* MD5 signature of include/exclude */
358    time_t CreateTime;                 /* date created */
359    /*
360     * This is where we return CreateTime
361     */
362    char cCreateTime[MAX_TIME_LENGTH]; /* CreateTime as returned from DB */
363    /* Not in DB but returned by db_create_fileset() */
364    bool created;                      /* set when record newly created */
365 };
366
367 /* Call back context for getting a 32/64 bit value from the database */
368 class db_int64_ctx {
369 public:
370    int64_t value;                     /* value returned */
371    int count;                         /* number of values seen */
372
373    db_int64_ctx() : value(0), count(0) {};
374    ~db_int64_ctx() {};
375 private:
376    db_int64_ctx(const db_int64_ctx&);            /* prohibit pass by value */
377    db_int64_ctx &operator=(const db_int64_ctx&); /* prohibit class assignment */
378 };
379
380 /* Call back context for getting a list of comma separated strings from the
381  * database
382  */
383 class db_list_ctx {
384 public:
385    POOLMEM *list;                     /* list */
386    int count;                         /* number of values seen */
387
388    db_list_ctx() { list = get_pool_memory(PM_FNAME); reset(); }
389    ~db_list_ctx() { free_pool_memory(list); list = NULL; }
390    void reset() { *list = 0; count = 0;}
391    void add(const db_list_ctx &str) {
392       if (str.count > 0) {
393          if (*list) {
394             pm_strcat(list, ",");
395          }
396          pm_strcat(list, str.list);
397          count += str.count;
398       }
399    }
400    void add(const char *str) {
401       if (count > 0) {
402          pm_strcat(list, ",");
403       }
404       pm_strcat(list, str);
405       count++;
406    }
407 private:
408    db_list_ctx(const db_list_ctx&);            /* prohibit pass by value */
409    db_list_ctx &operator=(const db_list_ctx&); /* prohibit class assignment */
410 };
411
412 typedef enum {
413    SQL_INTERFACE_TYPE_MYSQL      = 0,
414    SQL_INTERFACE_TYPE_POSTGRESQL = 1,
415    SQL_INTERFACE_TYPE_SQLITE3    = 2,
416 } SQL_INTERFACETYPE;
417
418 typedef enum {
419    SQL_TYPE_MYSQL      = 0,
420    SQL_TYPE_POSTGRESQL = 1,
421    SQL_TYPE_SQLITE3    = 2,
422    SQL_TYPE_UNKNOWN    = 99
423 } SQL_DBTYPE;
424
425 typedef void (DB_LIST_HANDLER)(void *, const char *);
426 typedef int (DB_RESULT_HANDLER)(void *, int, char **);
427
428 #define db_lock(mdb)   mdb->_db_lock(__FILE__, __LINE__)
429 #define db_unlock(mdb) mdb->_db_unlock(__FILE__, __LINE__)
430
431 /* Current database version number for all drivers */
432 #define BDB_VERSION 14
433
434 class B_DB: public SMARTALLOC {
435 protected:
436    brwlock_t m_lock;                      /* transaction lock */
437    dlink m_link;                          /* queue control */
438    SQL_INTERFACETYPE m_db_interface_type; /* type of backend used */
439    SQL_DBTYPE m_db_type;                  /* database type */
440    int m_ref_count;                       /* reference count */
441    bool m_connected;                      /* connection made to db */
442    bool m_have_batch_insert;              /* have batch insert support ? */
443    char *m_db_driver;                     /* database driver */
444    char *m_db_driverdir;                  /* database driver dir */
445    char *m_db_name;                       /* database name */
446    char *m_db_user;                       /* database user */
447    char *m_db_address;                    /* host name address */
448    char *m_db_socket;                     /* socket for local access */
449    char *m_db_password;                   /* database password */
450    int m_db_port;                         /* port for host name address */
451    bool m_disabled_batch_insert;          /* explicitly disabled batch insert mode ? */
452    bool m_dedicated;                      /* is this connection dedicated? */
453
454 public:
455    POOLMEM *errmsg;                       /* nicely edited error message */
456    POOLMEM *cmd;                          /* SQL command string */
457    POOLMEM *cached_path;                  /* cached path name */
458    int cached_path_len;                   /* length of cached path */
459    uint32_t cached_path_id;               /* cached path id */
460    int changes;                           /* changes during transaction */
461    POOLMEM *fname;                        /* Filename only */
462    POOLMEM *path;                         /* Path only */
463    POOLMEM *esc_name;                     /* Escaped file name */
464    POOLMEM *esc_path;                     /* Escaped path name */
465    POOLMEM *esc_obj;                      /* Escaped restore object */
466    int fnl;                               /* file name length */
467    int pnl;                               /* path name length */
468
469    /* methods */
470    B_DB() {};
471    virtual ~B_DB() {};
472    const char *get_db_name(void) { return m_db_name; };
473    const char *get_db_user(void) { return m_db_user; };
474    bool is_connected(void) { return m_connected; };
475    bool batch_insert_available(void) { return m_have_batch_insert; };
476    void increment_refcount(void) { m_ref_count++; };
477
478    /* low level methods */
479    bool db_match_database(const char *db_driver, const char *db_name,
480                           const char *db_address, int db_port);
481    B_DB *db_clone_database_connection(JCR *jcr, bool mult_db_connections);
482    int db_get_type_index(void) { return m_db_type; };
483    const char *db_get_type(void);
484    void _db_lock(const char *file, int line);
485    void _db_unlock(const char *file, int line);
486    bool db_sql_query(const char *query, int flags=0);
487    void print_lock_info(FILE *fp);
488
489    /* Pure virtual low level methods */
490    virtual bool db_open_database(JCR *jcr) = 0;
491    virtual void db_close_database(JCR *jcr) = 0;
492    virtual void db_thread_cleanup(void) = 0;
493    virtual void db_escape_string(JCR *jcr, char *snew, char *old, int len) = 0;
494    virtual char *db_escape_object(JCR *jcr, char *old, int len) = 0;
495    virtual void db_unescape_object(JCR *jcr, char *from, int32_t expected_len,
496                                    POOLMEM **dest, int32_t *len) = 0;
497    virtual void db_start_transaction(JCR *jcr) = 0;
498    virtual void db_end_transaction(JCR *jcr) = 0;
499    virtual bool db_sql_query(const char *query, DB_RESULT_HANDLER *result_handler, void *ctx) = 0;
500
501    /* By default, we use db_sql_query */
502    virtual bool db_big_sql_query(const char *query,
503                                  DB_RESULT_HANDLER *result_handler, void *ctx) {
504       return db_sql_query(query, result_handler, ctx);
505    };
506 };
507
508 /* sql_query Query Flags */
509 #define QF_STORE_RESULT 0x01
510
511 /* Use for better error location printing */
512 #define UPDATE_DB(jcr, db, cmd) UpdateDB(__FILE__, __LINE__, jcr, db, cmd)
513 #define INSERT_DB(jcr, db, cmd) InsertDB(__FILE__, __LINE__, jcr, db, cmd)
514 #define QUERY_DB(jcr, db, cmd) QueryDB(__FILE__, __LINE__, jcr, db, cmd)
515 #define DELETE_DB(jcr, db, cmd) DeleteDB(__FILE__, __LINE__, jcr, db, cmd)
516
517 #include "protos.h"
518 #include "jcr.h"
519 #include "sql_cmds.h"
520
521 /* Object used in db_list_xxx function */
522 class LIST_CTX {
523 public:
524    char line[256];              /* Used to print last dash line */
525    int32_t num_rows;
526
527    e_list_type type;            /* Vertical/Horizontal */
528    DB_LIST_HANDLER *send;       /* send data back */
529    bool once;                   /* Used to print header one time */
530    void *ctx;                   /* send() user argument */
531    B_DB *mdb;
532    JCR *jcr;
533
534    void empty() {
535       once = false;
536       line[0] = '\0';
537    }
538
539    void send_dashes() {
540       if (*line) {
541          send(ctx, line);
542       }
543    }
544
545    LIST_CTX(JCR *j, B_DB *m, DB_LIST_HANDLER *h, void *c, e_list_type t) {
546       line[0] = '\0';
547       once = false;
548       num_rows = 0;
549       type = t;
550       send = h;
551       ctx = c;
552       jcr = j;
553       mdb = m;
554    }
555 };
556
557 /*
558  * Some functions exported by sql.c for use within the cats directory.
559  */
560 int list_result(void *vctx, int cols, char **row);
561 int list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type);
562 void list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx);
563 int get_sql_record_max(JCR *jcr, B_DB *mdb);
564 bool check_tables_version(JCR *jcr, B_DB *mdb);
565 bool db_check_max_connections(JCR *jcr, B_DB *mdb, uint32_t nb);
566
567 void print_dashes(B_DB *mdb);
568 void print_result(B_DB *mdb);
569 int QueryDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
570 int InsertDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
571 int DeleteDB(const char *file, int line, JCR *jcr, B_DB *db, char *delete_cmd);
572 int UpdateDB(const char *file, int line, JCR *jcr, B_DB *db, char *update_cmd);
573 void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname);
574 #endif /* __CATS_H_ */