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 || HAVE_POSTGRESQL
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(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
56 extern int InsertDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
57 extern int UpdateDB(const char *file, int line, JCR *jcr, B_DB *db, char *update_cmd);
58 extern void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname);
61 /* Create a new record for the Job
62 * Returns: 0 on failure
66 db_create_job_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
68 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;
86 "INSERT INTO Job (Job,Name,Type,Level,JobStatus,SchedTime,JobTDate) VALUES \
87 ('%s','%s','%c','%c','%c','%s',%s)",
88 jr->Job, jr->Name, (char)(jr->JobType), (char)(jr->JobLevel),
89 (char)(jr->JobStatus), dt, edit_uint64(JobTDate, ed1));
91 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
92 Mmsg2(&mdb->errmsg, _("Create DB Job record %s failed. ERR=%s\n"),
93 mdb->cmd, sql_strerror(mdb));
97 jr->JobId = sql_insert_id(mdb, _("Job"));
104 /* Create a JobMedia record for medium used this job
105 * Returns: false on failure
109 db_create_jobmedia_record(JCR *jcr, B_DB *mdb, JOBMEDIA_DBR *jm)
115 #ifdef not_used_in_new_code
116 Mmsg(mdb->cmd, "SELECT JobId, MediaId FROM JobMedia WHERE \
117 JobId=%d AND MediaId=%d", jm->JobId, jm->MediaId);
119 Dmsg0(300, mdb->cmd);
120 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
121 mdb->num_rows = sql_num_rows(mdb);
122 if (mdb->num_rows > 0) {
123 Mmsg0(&mdb->errmsg, _("Create JobMedia failed. Record already exists.\n"));
124 sql_free_result(mdb);
126 Dmsg0(0, "Already have JobMedia record\n");
129 sql_free_result(mdb);
133 /* Now get count for VolIndex */
134 Mmsg(mdb->cmd, "SELECT count(*) from JobMedia");
135 count = get_sql_record_max(jcr, mdb);
143 "INSERT INTO JobMedia (JobId,MediaId,FirstIndex,LastIndex,"
144 "StartFile,EndFile,StartBlock,EndBlock,VolIndex) "
145 "VALUES (%u,%u,%u,%u,%u,%u,%u,%u,%u)",
146 jm->JobId, jm->MediaId, jm->FirstIndex, jm->LastIndex,
147 jm->StartFile, jm->EndFile, jm->StartBlock, jm->EndBlock,count);
149 Dmsg0(300, mdb->cmd);
150 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
151 Mmsg2(&mdb->errmsg, _("Create JobMedia record %s failed: ERR=%s\n"), mdb->cmd,
156 "UPDATE Media SET EndFile=%u, EndBlock=%u WHERE MediaId=%u",
157 jm->EndFile, jm->EndBlock, jm->MediaId);
158 if (!UPDATE_DB(jcr, mdb, mdb->cmd)) {
159 Mmsg2(&mdb->errmsg, _("Update Media record %s failed: ERR=%s\n"), mdb->cmd,
165 Dmsg0(300, "Return from JobMedia\n");
171 /* Create Unique Pool record
172 * Returns: 0 on failure
176 db_create_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
179 char ed1[30], ed2[30], ed3[50];
181 Dmsg0(200, "In create pool\n");
183 Mmsg(mdb->cmd, "SELECT PoolId,Name FROM Pool WHERE Name='%s'", pr->Name);
184 Dmsg1(200, "selectpool: %s\n", mdb->cmd);
186 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
187 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, _("Pool"));
230 * Create Media record. VolumeName and non-zero Slot must be unique
232 * Returns: 0 on failure
236 db_create_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
239 char ed1[30], ed2[30], ed3[30], ed4[30], ed5[30], ed6[50], ed7[50];
243 Mmsg(mdb->cmd, "SELECT MediaId FROM Media WHERE VolumeName='%s'",
245 Dmsg1(300, "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 "INSERT INTO Media (VolumeName,MediaType,PoolId,MaxVolBytes,VolCapacityBytes,"
261 "Recycle,VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,"
262 "VolStatus,Slot,VolBytes,InChanger,VolReadTime,VolWriteTime) "
263 "VALUES ('%s','%s',%u,%s,%s,%d,%s,%s,%u,%u,'%s',%d,%s,%d,%s,%s)",
265 mr->MediaType, mr->PoolId,
266 edit_uint64(mr->MaxVolBytes,ed1),
267 edit_uint64(mr->VolCapacityBytes, ed2),
269 edit_uint64(mr->VolRetention, ed3),
270 edit_uint64(mr->VolUseDuration, ed4),
275 edit_uint64(mr->VolBytes, ed5),
277 edit_uint64(mr->VolReadTime, ed6),
278 edit_uint64(mr->VolWriteTime, ed7));
281 Dmsg1(500, "Create Volume: %s\n", mdb->cmd);
282 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
283 Mmsg2(&mdb->errmsg, _("Create DB Media record %s failed. ERR=%s\n"),
284 mdb->cmd, sql_strerror(mdb));
287 mr->MediaId = sql_insert_id(mdb, _("Media"));
290 char dt[MAX_TIME_LENGTH];
291 localtime_r(&mr->LabelDate, &tm);
292 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
293 Mmsg(mdb->cmd, "UPDATE Media SET LabelDate='%s' "
294 "WHERE MediaId=%d", dt, mr->MediaId);
295 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
300 * Make sure that if InChanger is non-zero any other identical slot
301 * has InChanger zero.
303 db_make_inchanger_unique(jcr, mdb, mr);
312 * Create a Unique record for the client -- no duplicates
313 * Returns: 0 on failure
314 * 1 on success with id in cr->ClientId
316 int db_create_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
320 char ed1[30], ed2[30];
323 Mmsg(mdb->cmd, "SELECT ClientId,Uname FROM Client WHERE Name='%s'", cr->Name);
326 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
327 mdb->num_rows = sql_num_rows(mdb);
328 /* If more than one, report error, but return first row */
329 if (mdb->num_rows > 1) {
330 Mmsg1(&mdb->errmsg, _("More than one Client!: %d\n"), (int)(mdb->num_rows));
331 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
333 if (mdb->num_rows >= 1) {
334 if ((row = sql_fetch_row(mdb)) == NULL) {
335 Mmsg1(&mdb->errmsg, _("error fetching Client row: %s\n"), sql_strerror(mdb));
336 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
337 sql_free_result(mdb);
341 cr->ClientId = atoi(row[0]);
343 bstrncpy(cr->Uname, row[1], sizeof(cr->Uname));
345 cr->Uname[0] = 0; /* no name */
347 sql_free_result(mdb);
351 sql_free_result(mdb);
355 Mmsg(mdb->cmd, "INSERT INTO Client (Name, Uname, AutoPrune, \
356 FileRetention, JobRetention) VALUES \
357 ('%s', '%s', %d, %s, %s)", cr->Name, cr->Uname, cr->AutoPrune,
358 edit_uint64(cr->FileRetention, ed1),
359 edit_uint64(cr->JobRetention, ed2));
361 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
362 Mmsg2(&mdb->errmsg, _("Create DB Client record %s failed. ERR=%s\n"),
363 mdb->cmd, sql_strerror(mdb));
364 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
368 cr->ClientId = sql_insert_id(mdb, _("Client"));
377 * Create a Unique record for the counter -- no duplicates
378 * Returns: 0 on failure
379 * 1 on success with counter filled in
381 int db_create_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
387 memset(&mcr, 0, sizeof(mcr));
388 bstrncpy(mcr.Counter, cr->Counter, sizeof(mcr.Counter));
389 if (db_get_counter_record(jcr, mdb, &mcr)) {
390 memcpy(cr, &mcr, sizeof(COUNTER_DBR));
396 Mmsg(mdb->cmd, "INSERT INTO Counters (Counter,MinValue,MaxValue,CurrentValue,"
397 "WrapCounter) VALUES ('%s','%d','%d','%d','%s')",
398 cr->Counter, cr->MinValue, cr->MaxValue, cr->CurrentValue,
401 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
402 Mmsg2(&mdb->errmsg, _("Create DB Counters record %s failed. ERR=%s\n"),
403 mdb->cmd, sql_strerror(mdb));
404 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
415 * Create a FileSet record. This record is unique in the
416 * name and the MD5 signature of the include/exclude sets.
417 * Returns: 0 on failure
418 * 1 on success with FileSetId in record
420 int db_create_fileset_record(JCR *jcr, B_DB *mdb, FILESET_DBR *fsr)
427 fsr->created = false;
428 Mmsg(mdb->cmd, "SELECT FileSetId,CreateTime FROM FileSet WHERE "
429 "FileSet='%s' AND MD5='%s'", fsr->FileSet, fsr->MD5);
432 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
433 mdb->num_rows = sql_num_rows(mdb);
434 if (mdb->num_rows > 1) {
435 Mmsg1(&mdb->errmsg, _("More than one FileSet!: %d\n"), (int)(mdb->num_rows));
436 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
438 if (mdb->num_rows >= 1) {
439 if ((row = sql_fetch_row(mdb)) == NULL) {
440 Mmsg1(&mdb->errmsg, _("error fetching FileSet row: ERR=%s\n"), sql_strerror(mdb));
441 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
442 sql_free_result(mdb);
446 fsr->FileSetId = atoi(row[0]);
447 if (row[1] == NULL) {
448 fsr->cCreateTime[0] = 0;
450 bstrncpy(fsr->cCreateTime, row[1], sizeof(fsr->cCreateTime));
452 sql_free_result(mdb);
456 sql_free_result(mdb);
459 if (fsr->CreateTime == 0 && fsr->cCreateTime[0] == 0) {
460 fsr->CreateTime = time(NULL);
462 localtime_r(&fsr->CreateTime, &tm);
463 strftime(fsr->cCreateTime, sizeof(fsr->cCreateTime), "%Y-%m-%d %T", &tm);
466 Mmsg(mdb->cmd, "INSERT INTO FileSet (FileSet,MD5,CreateTime) "
467 "VALUES ('%s','%s','%s')", fsr->FileSet, fsr->MD5, fsr->cCreateTime);
469 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
470 Mmsg2(&mdb->errmsg, _("Create DB FileSet record %s failed. ERR=%s\n"),
471 mdb->cmd, sql_strerror(mdb));
472 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
476 fsr->FileSetId = sql_insert_id(mdb, _("FileSet"));
489 * dev_t st_dev; * device *
490 * ino_t st_ino; * inode *
491 * mode_t st_mode; * protection *
492 * nlink_t st_nlink; * number of hard links *
493 * uid_t st_uid; * user ID of owner *
494 * gid_t st_gid; * group ID of owner *
495 * dev_t st_rdev; * device type (if inode device) *
496 * off_t st_size; * total size, in bytes *
497 * unsigned long st_blksize; * blocksize for filesystem I/O *
498 * unsigned long st_blocks; * number of blocks allocated *
499 * time_t st_atime; * time of last access *
500 * time_t st_mtime; * time of last modification *
501 * time_t st_ctime; * time of last inode change *
508 * Create File record in B_DB
510 * In order to reduce database size, we store the File attributes,
511 * the FileName, and the Path separately. In principle, there
512 * is a single FileName record and a single Path record, no matter
513 * how many times it occurs. This is this subroutine, we separate
514 * the file and the path and create three database records.
516 int db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
520 Dmsg1(300, "Fname=%s\n", ar->fname);
521 Dmsg0(500, "put_file_into_catalog\n");
523 * Make sure we have an acceptable attributes record.
525 if (!(ar->Stream == STREAM_UNIX_ATTRIBUTES ||
526 ar->Stream == STREAM_UNIX_ATTRIBUTES_EX)) {
527 Mmsg0(&mdb->errmsg, _("Attempt to put non-attributes into catalog\n"));
528 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
533 split_path_and_file(jcr, mdb, ar->fname);
535 if (!db_create_filename_record(jcr, mdb, ar)) {
538 Dmsg1(500, "db_create_filename_record: %s\n", mdb->esc_name);
541 if (!db_create_path_record(jcr, mdb, ar)) {
544 Dmsg1(500, "db_create_path_record: %s\n", mdb->esc_name);
546 /* Now create master File record */
547 if (!db_create_file_record(jcr, mdb, ar)) {
550 Dmsg0(500, "db_create_file_record OK\n");
552 Dmsg3(300, "CreateAttributes Path=%s File=%s FilenameId=%d\n", mdb->path, mdb->fname, ar->FilenameId);
562 * This is the master File entry containing the attributes.
563 * The filename and path records have already been created.
565 static int db_create_file_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
571 ASSERT(ar->FilenameId);
575 "INSERT INTO File (FileIndex,JobId,PathId,FilenameId,"
576 "LStat,MD5) VALUES (%u,%u,%u,%u,'%s','0')",
577 ar->FileIndex, ar->JobId, ar->PathId, ar->FilenameId,
580 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
581 Mmsg2(&mdb->errmsg, _("Create db File record %s failed. ERR=%s"),
582 mdb->cmd, sql_strerror(mdb));
583 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
587 ar->FileId = sql_insert_id(mdb, _("File"));
593 /* Create a Unique record for the Path -- no duplicates */
594 static int db_create_path_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
599 mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->pnl+2);
600 db_escape_string(mdb->esc_name, mdb->path, mdb->pnl);
602 if (mdb->cached_path_id != 0 && mdb->cached_path_len == mdb->pnl &&
603 strcmp(mdb->cached_path, mdb->path) == 0) {
604 ar->PathId = mdb->cached_path_id;
608 Mmsg(mdb->cmd, "SELECT PathId FROM Path WHERE Path='%s'", mdb->esc_name);
610 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
611 mdb->num_rows = sql_num_rows(mdb);
612 if (mdb->num_rows > 1) {
614 Mmsg2(&mdb->errmsg, _("More than one Path!: %s for path: %s\n"),
615 edit_uint64(mdb->num_rows, ed1), mdb->path);
616 Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
618 /* Even if there are multiple paths, take the first one */
619 if (mdb->num_rows >= 1) {
620 if ((row = sql_fetch_row(mdb)) == NULL) {
621 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
622 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
623 sql_free_result(mdb);
628 ar->PathId = atoi(row[0]);
629 sql_free_result(mdb);
631 if (ar->PathId != mdb->cached_path_id) {
632 mdb->cached_path_id = ar->PathId;
633 mdb->cached_path_len = mdb->pnl;
634 pm_strcpy(&mdb->cached_path, mdb->path);
639 sql_free_result(mdb);
642 Mmsg(mdb->cmd, "INSERT INTO Path (Path) VALUES ('%s')", mdb->esc_name);
644 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
645 Mmsg2(&mdb->errmsg, _("Create db Path record %s failed. ERR=%s\n"),
646 mdb->cmd, sql_strerror(mdb));
647 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
651 ar->PathId = sql_insert_id(mdb, _("Path"));
656 if (stat && ar->PathId != mdb->cached_path_id) {
657 mdb->cached_path_id = ar->PathId;
658 mdb->cached_path_len = mdb->pnl;
659 pm_strcpy(&mdb->cached_path, mdb->path);
664 /* Create a Unique record for the filename -- no duplicates */
665 static int db_create_filename_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
669 mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->fnl+2);
670 db_escape_string(mdb->esc_name, mdb->fname, mdb->fnl);
672 Mmsg(mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name='%s'", mdb->esc_name);
674 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
675 mdb->num_rows = sql_num_rows(mdb);
676 if (mdb->num_rows > 1) {
678 Mmsg2(&mdb->errmsg, _("More than one Filename! %s for file: %s\n"),
679 edit_uint64(mdb->num_rows, ed1), mdb->fname);
680 Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
682 if (mdb->num_rows >= 1) {
683 if ((row = sql_fetch_row(mdb)) == NULL) {
684 Mmsg2(&mdb->errmsg, _("Error fetching row for file=%s: ERR=%s\n"),
685 mdb->fname, sql_strerror(mdb));
686 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
689 ar->FilenameId = atoi(row[0]);
691 sql_free_result(mdb);
692 return ar->FilenameId > 0;
694 sql_free_result(mdb);
697 Mmsg(mdb->cmd, "INSERT INTO Filename (Name) VALUES ('%s')", mdb->esc_name);
699 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
700 Mmsg2(&mdb->errmsg, _("Create db Filename record %s failed. ERR=%s\n"),
701 mdb->cmd, sql_strerror(mdb));
702 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
705 ar->FilenameId = sql_insert_id(mdb, _("Filename"));
707 return ar->FilenameId > 0;
710 #endif /* HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL */