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(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 int UpdateDB(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->Type), (char)(jr->Level),
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: 0 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 db JobMedia record %s failed. ERR=%s\n"), mdb->cmd,
158 Dmsg0(300, "Return from JobMedia\n");
164 /* Create Unique Pool record
165 * Returns: 0 on failure
169 db_create_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
172 char ed1[30], ed2[30], ed3[50];
174 Dmsg0(200, "In create pool\n");
176 Mmsg(&mdb->cmd, "SELECT PoolId,Name FROM Pool WHERE Name='%s'", pr->Name);
177 Dmsg1(200, "selectpool: %s\n", mdb->cmd);
179 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
181 mdb->num_rows = sql_num_rows(mdb);
183 if (mdb->num_rows > 0) {
184 Mmsg1(&mdb->errmsg, _("pool record %s already exists\n"), pr->Name);
185 sql_free_result(mdb);
189 sql_free_result(mdb);
194 "INSERT INTO Pool (Name,NumVols,MaxVols,UseOnce,UseCatalog,\
195 AcceptAnyVolume,AutoPrune,Recycle,VolRetention,VolUseDuration,\
196 MaxVolJobs,MaxVolFiles,MaxVolBytes,PoolType,LabelFormat) \
197 VALUES ('%s',%u,%u,%d,%d,%d,%d,%d,%s,%s,%u,%u,%s,'%s','%s')",
199 pr->NumVols, pr->MaxVols,
200 pr->UseOnce, pr->UseCatalog,
202 pr->AutoPrune, pr->Recycle,
203 edit_uint64(pr->VolRetention, ed1),
204 edit_uint64(pr->VolUseDuration, ed2),
205 pr->MaxVolJobs, pr->MaxVolFiles,
206 edit_uint64(pr->MaxVolBytes, ed3),
207 pr->PoolType, pr->LabelFormat);
208 Dmsg1(200, "Create Pool: %s\n", mdb->cmd);
209 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
210 Mmsg2(&mdb->errmsg, _("Create db Pool record %s failed: ERR=%s\n"),
211 mdb->cmd, sql_strerror(mdb));
215 pr->PoolId = sql_insert_id(mdb, _("Pool"));
225 * Create Media record. VolumeName and non-zero Slot must be unique
227 * Returns: 0 on failure
231 db_create_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
234 char ed1[30], ed2[30], ed3[30], ed4[30], ed5[30], ed6[50], ed7[50];
238 Mmsg(&mdb->cmd, "SELECT MediaId FROM Media WHERE VolumeName='%s'",
240 Dmsg1(300, "selectpool: %s\n", mdb->cmd);
242 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
243 mdb->num_rows = sql_num_rows(mdb);
244 if (mdb->num_rows > 0) {
245 Mmsg1(&mdb->errmsg, _("Volume \"%s\" already exists.\n"), mr->VolumeName);
246 sql_free_result(mdb);
250 sql_free_result(mdb);
255 "INSERT INTO Media (VolumeName,MediaType,PoolId,MaxVolBytes,VolCapacityBytes,"
256 "Recycle,VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,"
257 "VolStatus,Slot,VolBytes,InChanger,VolReadTime,VolWriteTime) "
258 "VALUES ('%s','%s',%u,%s,%s,%d,%s,%s,%u,%u,'%s',%d,%s,%d,%s,%s)",
260 mr->MediaType, mr->PoolId,
261 edit_uint64(mr->MaxVolBytes,ed1),
262 edit_uint64(mr->VolCapacityBytes, ed2),
264 edit_uint64(mr->VolRetention, ed3),
265 edit_uint64(mr->VolUseDuration, ed4),
270 edit_uint64(mr->VolBytes, ed5),
272 edit_uint64(mr->VolReadTime, ed6),
273 edit_uint64(mr->VolWriteTime, ed7));
276 Dmsg1(500, "Create Volume: %s\n", mdb->cmd);
277 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
278 Mmsg2(&mdb->errmsg, _("Create DB Media record %s failed. ERR=%s\n"),
279 mdb->cmd, sql_strerror(mdb));
282 mr->MediaId = sql_insert_id(mdb, _("Media"));
285 char dt[MAX_TIME_LENGTH];
286 localtime_r(&mr->LabelDate, &tm);
287 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
288 Mmsg(&mdb->cmd, "UPDATE Media SET LabelDate='%s' "
289 "WHERE MediaId=%d", dt, mr->MediaId);
290 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
295 * Make sure that if InChanger is non-zero any other identical slot
296 * has InChanger zero.
298 db_make_inchanger_unique(jcr, mdb, mr);
307 * Create a Unique record for the client -- no duplicates
308 * Returns: 0 on failure
309 * 1 on success with id in cr->ClientId
311 int db_create_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
315 char ed1[30], ed2[30];
318 Mmsg(&mdb->cmd, "SELECT ClientId,Uname FROM Client WHERE Name='%s'", cr->Name);
321 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
323 mdb->num_rows = sql_num_rows(mdb);
325 /* If more than one, report error, but return first row */
326 if (mdb->num_rows > 1) {
327 Mmsg1(&mdb->errmsg, _("More than one Client!: %d\n"), (int)(mdb->num_rows));
328 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
330 if (mdb->num_rows >= 1) {
331 if ((row = sql_fetch_row(mdb)) == NULL) {
332 Mmsg1(&mdb->errmsg, _("error fetching Client row: %s\n"), sql_strerror(mdb));
333 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
334 sql_free_result(mdb);
338 cr->ClientId = atoi(row[0]);
340 bstrncpy(cr->Uname, row[1], sizeof(cr->Uname));
342 cr->Uname[0] = 0; /* no name */
344 sql_free_result(mdb);
348 sql_free_result(mdb);
352 Mmsg(&mdb->cmd, "INSERT INTO Client (Name, Uname, AutoPrune, \
353 FileRetention, JobRetention) VALUES \
354 ('%s', '%s', %d, %s, %s)", cr->Name, cr->Uname, cr->AutoPrune,
355 edit_uint64(cr->FileRetention, ed1),
356 edit_uint64(cr->JobRetention, ed2));
358 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
359 Mmsg2(&mdb->errmsg, _("Create DB Client record %s failed. ERR=%s\n"),
360 mdb->cmd, sql_strerror(mdb));
361 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
365 cr->ClientId = sql_insert_id(mdb, _("Client"));
374 * Create a Unique record for the counter -- no duplicates
375 * Returns: 0 on failure
376 * 1 on success with counter filled in
378 int db_create_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
384 memset(&mcr, 0, sizeof(mcr));
385 bstrncpy(mcr.Counter, cr->Counter, sizeof(mcr.Counter));
386 if (db_get_counter_record(jcr, mdb, &mcr)) {
387 memcpy(cr, &mcr, sizeof(COUNTER_DBR));
393 Mmsg(&mdb->cmd, "INSERT INTO Counters (Counter,MinValue,MaxValue,CurrentValue,"
394 "WrapCounter) VALUES ('%s','%d','%d','%d','%s')",
395 cr->Counter, cr->MinValue, cr->MaxValue, cr->CurrentValue,
398 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
399 Mmsg2(&mdb->errmsg, _("Create DB Counters record %s failed. ERR=%s\n"),
400 mdb->cmd, sql_strerror(mdb));
401 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
412 * Create a FileSet record. This record is unique in the
413 * name and the MD5 signature of the include/exclude sets.
414 * Returns: 0 on failure
415 * 1 on success with FileSetId in record
417 int db_create_fileset_record(JCR *jcr, B_DB *mdb, FILESET_DBR *fsr)
424 fsr->created = false;
425 Mmsg(&mdb->cmd, "SELECT FileSetId,CreateTime FROM FileSet WHERE "
426 "FileSet='%s' AND MD5='%s'", fsr->FileSet, fsr->MD5);
429 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
431 mdb->num_rows = sql_num_rows(mdb);
433 if (mdb->num_rows > 1) {
434 Mmsg1(&mdb->errmsg, _("More than one FileSet!: %d\n"), (int)(mdb->num_rows));
435 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
437 if (mdb->num_rows >= 1) {
438 if ((row = sql_fetch_row(mdb)) == NULL) {
439 Mmsg1(&mdb->errmsg, _("error fetching FileSet row: ERR=%s\n"), sql_strerror(mdb));
440 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
441 sql_free_result(mdb);
445 fsr->FileSetId = atoi(row[0]);
446 if (row[1] == NULL) {
447 fsr->cCreateTime[0] = 0;
449 bstrncpy(fsr->cCreateTime, row[1], sizeof(fsr->cCreateTime));
451 sql_free_result(mdb);
455 sql_free_result(mdb);
458 if (fsr->CreateTime == 0 && fsr->cCreateTime[0] == 0) {
459 fsr->CreateTime = time(NULL);
461 localtime_r(&fsr->CreateTime, &tm);
462 strftime(fsr->cCreateTime, sizeof(fsr->cCreateTime), "%Y-%m-%d %T", &tm);
465 Mmsg(&mdb->cmd, "INSERT INTO FileSet (FileSet,MD5,CreateTime) "
466 "VALUES ('%s','%s','%s')", fsr->FileSet, fsr->MD5, fsr->cCreateTime);
468 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
469 Mmsg2(&mdb->errmsg, _("Create DB FileSet record %s failed. ERR=%s\n"),
470 mdb->cmd, sql_strerror(mdb));
471 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
475 fsr->FileSetId = sql_insert_id(mdb, _("FileSet"));
488 * dev_t st_dev; * device *
489 * ino_t st_ino; * inode *
490 * mode_t st_mode; * protection *
491 * nlink_t st_nlink; * number of hard links *
492 * uid_t st_uid; * user ID of owner *
493 * gid_t st_gid; * group ID of owner *
494 * dev_t st_rdev; * device type (if inode device) *
495 * off_t st_size; * total size, in bytes *
496 * unsigned long st_blksize; * blocksize for filesystem I/O *
497 * unsigned long st_blocks; * number of blocks allocated *
498 * time_t st_atime; * time of last access *
499 * time_t st_mtime; * time of last modification *
500 * time_t st_ctime; * time of last inode change *
507 * Create File record in B_DB
509 * In order to reduce database size, we store the File attributes,
510 * the FileName, and the Path separately. In principle, there
511 * is a single FileName record and a single Path record, no matter
512 * how many times it occurs. This is this subroutine, we separate
513 * the file and the path and create three database records.
515 int db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
519 Dmsg1(300, "Fname=%s\n", ar->fname);
520 Dmsg0(500, "put_file_into_catalog\n");
522 * Make sure we have an acceptable attributes record.
524 if (!(ar->Stream == STREAM_UNIX_ATTRIBUTES ||
525 ar->Stream == STREAM_UNIX_ATTRIBUTES_EX)) {
526 Mmsg0(&mdb->errmsg, _("Attempt to put non-attributes into catalog\n"));
527 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
532 split_path_and_file(jcr, mdb, ar->fname);
534 if (!db_create_filename_record(jcr, mdb, ar)) {
537 Dmsg1(500, "db_create_filename_record: %s\n", mdb->esc_name);
540 if (!db_create_path_record(jcr, mdb, ar)) {
543 Dmsg1(500, "db_create_path_record: %s\n", mdb->esc_name);
545 /* Now create master File record */
546 if (!db_create_file_record(jcr, mdb, ar)) {
549 Dmsg0(500, "db_create_file_record OK\n");
551 Dmsg3(300, "CreateAttributes Path=%s File=%s FilenameId=%d\n", mdb->path, mdb->fname, ar->FilenameId);
561 * This is the master File entry containing the attributes.
562 * The filename and path records have already been created.
564 static int db_create_file_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
570 ASSERT(ar->FilenameId);
574 "INSERT INTO File (FileIndex,JobId,PathId,FilenameId,"
575 "LStat,MD5) VALUES (%u,%u,%u,%u,'%s','0')",
576 ar->FileIndex, ar->JobId, ar->PathId, ar->FilenameId,
579 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
580 Mmsg2(&mdb->errmsg, _("Create db File record %s failed. ERR=%s"),
581 mdb->cmd, sql_strerror(mdb));
582 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
586 ar->FileId = sql_insert_id(mdb, _("File"));
592 /* Create a Unique record for the Path -- no duplicates */
593 static int db_create_path_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
598 mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->pnl+2);
599 db_escape_string(mdb->esc_name, mdb->path, mdb->pnl);
601 if (mdb->cached_path_id != 0 && mdb->cached_path_len == mdb->pnl &&
602 strcmp(mdb->cached_path, mdb->path) == 0) {
603 ar->PathId = mdb->cached_path_id;
607 Mmsg(&mdb->cmd, "SELECT PathId FROM Path WHERE Path='%s'", mdb->esc_name);
609 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
610 mdb->num_rows = sql_num_rows(mdb);
611 if (mdb->num_rows > 1) {
613 Mmsg2(&mdb->errmsg, _("More than one Path!: %s for path: %s\n"),
614 edit_uint64(mdb->num_rows, ed1), mdb->path);
615 Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
617 /* Even if there are multiple paths, take the first one */
618 if (mdb->num_rows >= 1) {
619 if ((row = sql_fetch_row(mdb)) == NULL) {
620 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
621 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
622 sql_free_result(mdb);
627 ar->PathId = atoi(row[0]);
628 sql_free_result(mdb);
630 if (ar->PathId != mdb->cached_path_id) {
631 mdb->cached_path_id = ar->PathId;
632 mdb->cached_path_len = mdb->pnl;
633 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 */