2 * Bacula Catalog Database Create record interface routines
4 * Kern Sibbald, March 2000
10 Copyright (C) 2000-2003 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29 /* The following is necessary so that we do not include
30 * the dummy external definition of DB.
32 #define __SQL_C /* indicate that this is sql.c */
37 #if HAVE_MYSQL || HAVE_SQLITE
39 /* -----------------------------------------------------------------------
41 * Generic Routines (or almost generic)
43 * -----------------------------------------------------------------------
46 /* Forward referenced subroutines */
47 static int db_create_file_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
48 static int db_create_filename_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
49 static int db_create_path_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
52 /* Imported subroutines */
53 extern void print_dashes(B_DB *mdb);
54 extern void print_result(B_DB *mdb);
55 extern int QueryDB(char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
56 extern int InsertDB(char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
57 extern void split_path_and_filename(JCR *jcr, B_DB *mdb, char *fname);
60 /* Create a new record for the Job
61 * Returns: 0 on failure
65 db_create_job_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
67 char dt[MAX_TIME_LENGTH];
77 stime = jr->SchedTime;
80 localtime_r(&stime, &tm);
81 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
82 JobTDate = (utime_t)stime;
84 if (!db_next_index(jcr, mdb, "Job", JobId)) {
91 "INSERT INTO Job (JobId,Job,Name,Type,Level,JobStatus,SchedTime,JobTDate) VALUES \
92 (%s,'%s','%s','%c','%c','%c','%s',%s)",
93 JobId, jr->Job, jr->Name, (char)(jr->Type), (char)(jr->Level),
94 (char)(jr->JobStatus), dt, edit_uint64(JobTDate, ed1));
96 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
97 Mmsg2(&mdb->errmsg, _("Create DB Job record %s failed. ERR=%s\n"),
98 mdb->cmd, sql_strerror(mdb));
102 jr->JobId = sql_insert_id(mdb);
109 /* Create a JobMedia record for medium used this job
110 * Returns: 0 on failure
114 db_create_jobmedia_record(JCR *jcr, B_DB *mdb, JOBMEDIA_DBR *jm)
120 #ifdef not_used_in_new_code
121 Mmsg(&mdb->cmd, "SELECT JobId, MediaId FROM JobMedia WHERE \
122 JobId=%d AND MediaId=%d", jm->JobId, jm->MediaId);
125 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
126 mdb->num_rows = sql_num_rows(mdb);
127 if (mdb->num_rows > 0) {
128 Mmsg0(&mdb->errmsg, _("Create JobMedia failed. Record already exists.\n"));
129 sql_free_result(mdb);
131 Dmsg0(0, "Already have JobMedia record\n");
134 sql_free_result(mdb);
138 /* Now get count for VolIndex */
139 Mmsg(&mdb->cmd, "SELECT count(*) from JobMedia");
140 count = get_sql_record_max(jcr, mdb);
148 "INSERT INTO JobMedia (JobId,MediaId,FirstIndex,LastIndex,\
149 StartFile,EndFile,StartBlock,EndBlock,VolIndex) \
150 VALUES (%u,%u,%u,%u,%u,%u,%u,%u,%u)",
151 jm->JobId, jm->MediaId, jm->FirstIndex, jm->LastIndex,
152 jm->StartFile, jm->EndFile, jm->StartBlock, jm->EndBlock,count);
155 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
156 Mmsg2(&mdb->errmsg, _("Create db JobMedia record %s failed. ERR=%s\n"), mdb->cmd,
163 Dmsg0(30, "Return from JobMedia\n");
169 /* Create Unique Pool record
170 * Returns: 0 on failure
174 db_create_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
177 char ed1[30], ed2[30], ed3[50];
179 Dmsg0(200, "In create pool\n");
181 Mmsg(&mdb->cmd, "SELECT PoolId,Name FROM Pool WHERE Name='%s'", pr->Name);
182 Dmsg1(200, "selectpool: %s\n", mdb->cmd);
184 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
186 mdb->num_rows = sql_num_rows(mdb);
188 if (mdb->num_rows > 0) {
189 Mmsg1(&mdb->errmsg, _("pool record %s already exists\n"), pr->Name);
190 sql_free_result(mdb);
194 sql_free_result(mdb);
199 "INSERT INTO Pool (Name,NumVols,MaxVols,UseOnce,UseCatalog,\
200 AcceptAnyVolume,AutoPrune,Recycle,VolRetention,VolUseDuration,\
201 MaxVolJobs,MaxVolFiles,MaxVolBytes,PoolType,LabelFormat) \
202 VALUES ('%s',%u,%u,%d,%d,%d,%d,%d,%s,%s,%u,%u,%s,'%s','%s')",
204 pr->NumVols, pr->MaxVols,
205 pr->UseOnce, pr->UseCatalog,
207 pr->AutoPrune, pr->Recycle,
208 edit_uint64(pr->VolRetention, ed1),
209 edit_uint64(pr->VolUseDuration, ed2),
210 pr->MaxVolJobs, pr->MaxVolFiles,
211 edit_uint64(pr->MaxVolBytes, ed3),
212 pr->PoolType, pr->LabelFormat);
213 Dmsg1(200, "Create Pool: %s\n", mdb->cmd);
214 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
215 Mmsg2(&mdb->errmsg, _("Create db Pool record %s failed: ERR=%s\n"),
216 mdb->cmd, sql_strerror(mdb));
220 pr->PoolId = sql_insert_id(mdb);
230 * Create Unique Media record
231 * Returns: 0 on failure
235 db_create_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
238 char ed1[30], ed2[30], ed3[30], ed4[30], ed5[30];
239 char dt[MAX_TIME_LENGTH];
243 Mmsg(&mdb->cmd, "SELECT MediaId FROM Media WHERE VolumeName='%s'",
245 Dmsg1(110, "selectpool: %s\n", mdb->cmd);
247 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
248 mdb->num_rows = sql_num_rows(mdb);
249 if (mdb->num_rows > 0) {
250 Mmsg1(&mdb->errmsg, _("Volume \"%s\" already exists\n."), mr->VolumeName);
251 sql_free_result(mdb);
255 sql_free_result(mdb);
260 localtime_r(&mr->LabelDate, &tm);
261 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
263 bstrncpy(dt, "0000-00-00 00:00:00", sizeof(dt));
266 "INSERT INTO Media (VolumeName,MediaType,PoolId,MaxVolBytes,VolCapacityBytes,"
267 "Recycle,VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,"
268 "VolStatus,LabelDate,Slot,VolBytes) "
269 "VALUES ('%s','%s',%u,%s,%s,%d,%s,%s,%u,%u,'%s','%s',%d,%s)",
271 mr->MediaType, mr->PoolId,
272 edit_uint64(mr->MaxVolBytes,ed1),
273 edit_uint64(mr->VolCapacityBytes, ed2),
275 edit_uint64(mr->VolRetention, ed3),
276 edit_uint64(mr->VolUseDuration, ed4),
281 edit_uint64(mr->VolBytes, ed5));
283 Dmsg1(500, "Create Volume: %s\n", mdb->cmd);
284 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
285 Mmsg2(&mdb->errmsg, _("Create DB Media record %s failed. ERR=%s\n"),
286 mdb->cmd, sql_strerror(mdb));
289 mr->MediaId = sql_insert_id(mdb);
299 * Create a Unique record for the client -- no duplicates
300 * Returns: 0 on failure
301 * 1 on success with id in cr->ClientId
303 int db_create_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
307 char ed1[30], ed2[30];
310 Mmsg(&mdb->cmd, "SELECT ClientId,Uname FROM Client WHERE Name='%s'", cr->Name);
313 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
315 mdb->num_rows = sql_num_rows(mdb);
317 /* If more than one, report error, but return first row */
318 if (mdb->num_rows > 1) {
319 Mmsg1(&mdb->errmsg, _("More than one Client!: %d\n"), (int)(mdb->num_rows));
320 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
322 if (mdb->num_rows >= 1) {
323 if ((row = sql_fetch_row(mdb)) == NULL) {
324 Mmsg1(&mdb->errmsg, _("error fetching Client row: %s\n"), sql_strerror(mdb));
325 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
326 sql_free_result(mdb);
330 cr->ClientId = atoi(row[0]);
332 bstrncpy(cr->Uname, row[1], sizeof(cr->Uname));
334 cr->Uname[0] = 0; /* no name */
336 sql_free_result(mdb);
340 sql_free_result(mdb);
344 Mmsg(&mdb->cmd, "INSERT INTO Client (Name, Uname, AutoPrune, \
345 FileRetention, JobRetention) VALUES \
346 ('%s', '%s', %d, %s, %s)", cr->Name, cr->Uname, cr->AutoPrune,
347 edit_uint64(cr->FileRetention, ed1),
348 edit_uint64(cr->JobRetention, ed2));
350 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
351 Mmsg2(&mdb->errmsg, _("Create DB Client record %s failed. ERR=%s\n"),
352 mdb->cmd, sql_strerror(mdb));
353 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
357 cr->ClientId = sql_insert_id(mdb);
366 * Create a Unique record for the counter -- no duplicates
367 * Returns: 0 on failure
368 * 1 on success with counter filled in
370 int db_create_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
376 memset(&mcr, 0, sizeof(mcr));
377 bstrncpy(mcr.Counter, cr->Counter, sizeof(mcr.Counter));
378 if (db_get_counter_record(jcr, mdb, &mcr)) {
379 memcpy(cr, &mcr, sizeof(COUNTER_DBR));
385 Mmsg(&mdb->cmd, "INSERT INTO Counters (Counter,MinValue,MaxValue,CurrentValue,"
386 "WrapCounter) VALUES ('%s','%d','%d','%d','%s')",
387 cr->Counter, cr->MinValue, cr->MaxValue, cr->CurrentValue,
390 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
391 Mmsg2(&mdb->errmsg, _("Create DB Counters record %s failed. ERR=%s\n"),
392 mdb->cmd, sql_strerror(mdb));
393 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
404 * Create a FileSet record. This record is unique in the
405 * name and the MD5 signature of the include/exclude sets.
406 * Returns: 0 on failure
407 * 1 on success with FileSetId in record
409 int db_create_fileset_record(JCR *jcr, B_DB *mdb, FILESET_DBR *fsr)
416 fsr->created = false;
417 Mmsg(&mdb->cmd, "SELECT FileSetId,CreateTime FROM FileSet WHERE "
418 "FileSet='%s' AND MD5='%s'", fsr->FileSet, fsr->MD5);
421 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
423 mdb->num_rows = sql_num_rows(mdb);
425 if (mdb->num_rows > 1) {
426 Mmsg1(&mdb->errmsg, _("More than one FileSet!: %d\n"), (int)(mdb->num_rows));
427 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
429 if (mdb->num_rows >= 1) {
430 if ((row = sql_fetch_row(mdb)) == NULL) {
431 Mmsg1(&mdb->errmsg, _("error fetching FileSet row: ERR=%s\n"), sql_strerror(mdb));
432 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
433 sql_free_result(mdb);
437 fsr->FileSetId = atoi(row[0]);
438 if (row[1] == NULL) {
439 fsr->cCreateTime[0] = 0;
441 bstrncpy(fsr->cCreateTime, row[1], sizeof(fsr->cCreateTime));
443 sql_free_result(mdb);
447 sql_free_result(mdb);
450 if (fsr->CreateTime == 0 && fsr->cCreateTime[0] == 0) {
451 fsr->CreateTime = time(NULL);
453 localtime_r(&fsr->CreateTime, &tm);
454 strftime(fsr->cCreateTime, sizeof(fsr->cCreateTime), "%Y-%m-%d %T", &tm);
457 Mmsg(&mdb->cmd, "INSERT INTO FileSet (FileSet,MD5,CreateTime) "
458 "VALUES ('%s','%s','%s')", fsr->FileSet, fsr->MD5, fsr->cCreateTime);
460 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
461 Mmsg2(&mdb->errmsg, _("Create DB FileSet record %s failed. ERR=%s\n"),
462 mdb->cmd, sql_strerror(mdb));
463 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
467 fsr->FileSetId = sql_insert_id(mdb);
480 * dev_t st_dev; * device *
481 * ino_t st_ino; * inode *
482 * mode_t st_mode; * protection *
483 * nlink_t st_nlink; * number of hard links *
484 * uid_t st_uid; * user ID of owner *
485 * gid_t st_gid; * group ID of owner *
486 * dev_t st_rdev; * device type (if inode device) *
487 * off_t st_size; * total size, in bytes *
488 * unsigned long st_blksize; * blocksize for filesystem I/O *
489 * unsigned long st_blocks; * number of blocks allocated *
490 * time_t st_atime; * time of last access *
491 * time_t st_mtime; * time of last modification *
492 * time_t st_ctime; * time of last inode change *
499 * Create File record in B_DB
501 * In order to reduce database size, we store the File attributes,
502 * the FileName, and the Path separately. In principle, there
503 * is a single FileName record and a single Path record, no matter
504 * how many times it occurs. This is this subroutine, we separate
505 * the file and the path and create three database records.
507 int db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
510 Dmsg1(100, "Fname=%s\n", ar->fname);
511 Dmsg0(50, "put_file_into_catalog\n");
513 * Make sure we have an acceptable attributes record.
515 if (!(ar->Stream == STREAM_UNIX_ATTRIBUTES ||
516 ar->Stream == STREAM_UNIX_ATTRIBUTES_EX)) {
517 Mmsg0(&mdb->errmsg, _("Attempt to put non-attributes into catalog\n"));
518 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
524 split_path_and_filename(jcr, mdb, ar->fname);
526 if (!db_create_filename_record(jcr, mdb, ar)) {
530 Dmsg1(500, "db_create_filename_record: %s\n", mdb->esc_name);
533 if (!db_create_path_record(jcr, mdb, ar)) {
537 Dmsg1(500, "db_create_path_record: %s\n", mdb->esc_name);
539 /* Now create master File record */
540 if (!db_create_file_record(jcr, mdb, ar)) {
544 Dmsg0(500, "db_create_file_record OK\n");
546 Dmsg3(100, "CreateAttributes Path=%s File=%s FilenameId=%d\n", mdb->path, mdb->fname, ar->FilenameId);
552 * This is the master File entry containing the attributes.
553 * The filename and path records have already been created.
555 static int db_create_file_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
561 ASSERT(ar->FilenameId);
565 "INSERT INTO File (FileIndex,JobId,PathId,FilenameId,"
566 "LStat,MD5) VALUES (%u,%u,%u,%u,'%s','0')",
567 ar->FileIndex, ar->JobId, ar->PathId, ar->FilenameId,
570 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
571 Mmsg2(&mdb->errmsg, _("Create db File record %s failed. ERR=%s"),
572 mdb->cmd, sql_strerror(mdb));
573 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
577 ar->FileId = sql_insert_id(mdb);
583 /* Create a Unique record for the Path -- no duplicates */
584 static int db_create_path_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
589 mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->pnl+2);
590 db_escape_string(mdb->esc_name, mdb->path, mdb->pnl);
592 if (mdb->cached_path_id != 0 && mdb->cached_path_len == mdb->pnl &&
593 strcmp(mdb->cached_path, mdb->path) == 0) {
594 ar->PathId = mdb->cached_path_id;
598 Mmsg(&mdb->cmd, "SELECT PathId FROM Path WHERE Path='%s'", mdb->esc_name);
600 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
601 mdb->num_rows = sql_num_rows(mdb);
602 if (mdb->num_rows > 1) {
604 Mmsg2(&mdb->errmsg, _("More than one Path!: %s for path: %s\n"),
605 edit_uint64(mdb->num_rows, ed1), mdb->path);
606 Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
608 /* Even if there are multiple paths, take the first one */
609 if (mdb->num_rows >= 1) {
610 if ((row = sql_fetch_row(mdb)) == NULL) {
611 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
612 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
613 sql_free_result(mdb);
618 ar->PathId = atoi(row[0]);
619 sql_free_result(mdb);
621 if (ar->PathId != mdb->cached_path_id) {
622 mdb->cached_path_id = ar->PathId;
623 mdb->cached_path_len = mdb->pnl;
624 pm_strcpy(&mdb->cached_path, mdb->path);
630 sql_free_result(mdb);
633 Mmsg(&mdb->cmd, "INSERT INTO Path (Path) VALUES ('%s')", mdb->esc_name);
635 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
636 Mmsg2(&mdb->errmsg, _("Create db Path record %s failed. ERR=%s\n"),
637 mdb->cmd, sql_strerror(mdb));
638 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
642 ar->PathId = sql_insert_id(mdb);
647 if (stat && ar->PathId != mdb->cached_path_id) {
648 mdb->cached_path_id = ar->PathId;
649 mdb->cached_path_len = mdb->pnl;
650 pm_strcpy(&mdb->cached_path, mdb->path);
655 /* Create a Unique record for the filename -- no duplicates */
656 static int db_create_filename_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
660 mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->fnl+2);
661 db_escape_string(mdb->esc_name, mdb->fname, mdb->fnl);
663 Mmsg(&mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name='%s'", mdb->esc_name);
665 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
666 mdb->num_rows = sql_num_rows(mdb);
667 if (mdb->num_rows > 1) {
669 Mmsg2(&mdb->errmsg, _("More than one Filename! %s for file: %s\n"),
670 edit_uint64(mdb->num_rows, ed1), mdb->fname);
671 Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
673 if (mdb->num_rows >= 1) {
674 if ((row = sql_fetch_row(mdb)) == NULL) {
675 Mmsg2(&mdb->errmsg, _("Error fetching row for file=%s: ERR=%s\n"),
676 mdb->fname, sql_strerror(mdb));
677 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
680 ar->FilenameId = atoi(row[0]);
682 sql_free_result(mdb);
683 return ar->FilenameId > 0;
685 sql_free_result(mdb);
688 Mmsg(&mdb->cmd, "INSERT INTO Filename (Name) VALUES ('%s')", mdb->esc_name);
690 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
691 Mmsg2(&mdb->errmsg, _("Create db Filename record %s failed. ERR=%s\n"),
692 mdb->cmd, sql_strerror(mdb));
693 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
696 ar->FilenameId = sql_insert_id(mdb);
698 return ar->FilenameId > 0;
701 #endif /* HAVE_MYSQL || HAVE_SQLITE */