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)
119 Mmsg(&mdb->cmd, "SELECT JobId, MediaId FROM JobMedia WHERE \
120 JobId=%d AND MediaId=%d", jm->JobId, jm->MediaId);
123 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
124 mdb->num_rows = sql_num_rows(mdb);
125 if (mdb->num_rows > 0) {
126 Mmsg0(&mdb->errmsg, _("Create JobMedia failed. Record already exists.\n"));
127 sql_free_result(mdb);
129 Dmsg0(0, "Already have JobMedia record\n");
132 sql_free_result(mdb);
137 "INSERT INTO JobMedia (JobId,MediaId,FirstIndex,LastIndex,\
138 StartFile,EndFile,StartBlock,EndBlock) \
139 VALUES (%u,%u,%u,%u,%u,%u,%u,%u)",
140 jm->JobId, jm->MediaId, jm->FirstIndex, jm->LastIndex,
141 jm->StartFile, jm->EndFile, jm->StartBlock, jm->EndBlock);
144 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
145 Mmsg2(&mdb->errmsg, _("Create db JobMedia record %s failed. ERR=%s\n"), mdb->cmd,
152 Dmsg0(30, "Return from JobMedia\n");
158 /* Create Unique Pool record
159 * Returns: 0 on failure
163 db_create_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
166 char ed1[30], ed2[30], ed3[50];
168 Dmsg0(200, "In create pool\n");
170 Mmsg(&mdb->cmd, "SELECT PoolId,Name FROM Pool WHERE Name='%s'", pr->Name);
171 Dmsg1(200, "selectpool: %s\n", mdb->cmd);
173 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
175 mdb->num_rows = sql_num_rows(mdb);
177 if (mdb->num_rows > 0) {
178 Mmsg1(&mdb->errmsg, _("pool record %s already exists\n"), pr->Name);
179 sql_free_result(mdb);
183 sql_free_result(mdb);
188 "INSERT INTO Pool (Name,NumVols,MaxVols,UseOnce,UseCatalog,\
189 AcceptAnyVolume,AutoPrune,Recycle,VolRetention,VolUseDuration,\
190 MaxVolJobs,MaxVolFiles,MaxVolBytes,PoolType,LabelFormat) \
191 VALUES ('%s',%u,%u,%d,%d,%d,%d,%d,%s,%s,%u,%u,%s,'%s','%s')",
193 pr->NumVols, pr->MaxVols,
194 pr->UseOnce, pr->UseCatalog,
196 pr->AutoPrune, pr->Recycle,
197 edit_uint64(pr->VolRetention, ed1),
198 edit_uint64(pr->VolUseDuration, ed2),
199 pr->MaxVolJobs, pr->MaxVolFiles,
200 edit_uint64(pr->MaxVolBytes, ed3),
201 pr->PoolType, pr->LabelFormat);
202 Dmsg1(200, "Create Pool: %s\n", mdb->cmd);
203 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
204 Mmsg2(&mdb->errmsg, _("Create db Pool record %s failed: ERR=%s\n"),
205 mdb->cmd, sql_strerror(mdb));
209 pr->PoolId = sql_insert_id(mdb);
219 * Create Unique Media record
220 * Returns: 0 on failure
224 db_create_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
227 char ed1[30], ed2[30], ed3[30], ed4[30];
228 char dt[MAX_TIME_LENGTH];
232 Mmsg(&mdb->cmd, "SELECT MediaId FROM Media WHERE VolumeName='%s'",
234 Dmsg1(110, "selectpool: %s\n", mdb->cmd);
236 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
237 mdb->num_rows = sql_num_rows(mdb);
238 if (mdb->num_rows > 0) {
239 Mmsg1(&mdb->errmsg, _("Volume \"%s\" already exists\n."), mr->VolumeName);
240 sql_free_result(mdb);
244 sql_free_result(mdb);
249 localtime_r(&mr->LabelDate, &tm);
250 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
252 strcpy(dt, "0000-00-00 00:00:00");
255 "INSERT INTO Media (VolumeName,MediaType,PoolId,MaxVolBytes,VolCapacityBytes,"
256 "Recycle,VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,"
257 "VolStatus,LabelDate,Slot) "
258 "VALUES ('%s','%s',%u,%s,%s,%d,%s,%s,%u,%u,'%s','%s',%d)",
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),
271 Dmsg1(500, "Create Volume: %s\n", mdb->cmd);
272 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
273 Mmsg2(&mdb->errmsg, _("Create DB Media record %s failed. ERR=%s\n"),
274 mdb->cmd, sql_strerror(mdb));
277 mr->MediaId = sql_insert_id(mdb);
287 * Create a Unique record for the client -- no duplicates
288 * Returns: 0 on failure
289 * 1 on success with id in cr->ClientId
291 int db_create_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
295 char ed1[30], ed2[30];
298 Mmsg(&mdb->cmd, "SELECT ClientId,Uname FROM Client WHERE Name='%s'", cr->Name);
301 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
303 mdb->num_rows = sql_num_rows(mdb);
305 /* If more than one, report error, but return first row */
306 if (mdb->num_rows > 1) {
307 Mmsg1(&mdb->errmsg, _("More than one Client!: %d\n"), (int)(mdb->num_rows));
308 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
310 if (mdb->num_rows >= 1) {
311 if ((row = sql_fetch_row(mdb)) == NULL) {
312 Mmsg1(&mdb->errmsg, _("error fetching Client row: %s\n"), sql_strerror(mdb));
313 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
314 sql_free_result(mdb);
318 cr->ClientId = atoi(row[0]);
320 bstrncpy(cr->Uname, row[1], sizeof(cr->Uname));
322 cr->Uname[0] = 0; /* no name */
324 sql_free_result(mdb);
328 sql_free_result(mdb);
332 Mmsg(&mdb->cmd, "INSERT INTO Client (Name, Uname, AutoPrune, \
333 FileRetention, JobRetention) VALUES \
334 ('%s', '%s', %d, %s, %s)", cr->Name, cr->Uname, cr->AutoPrune,
335 edit_uint64(cr->FileRetention, ed1),
336 edit_uint64(cr->JobRetention, ed2));
338 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
339 Mmsg2(&mdb->errmsg, _("Create DB Client record %s failed. ERR=%s\n"),
340 mdb->cmd, sql_strerror(mdb));
341 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
345 cr->ClientId = sql_insert_id(mdb);
354 * Create a FileSet record. This record is unique in the
355 * name and the MD5 signature of the include/exclude sets.
356 * Returns: 0 on failure
357 * 1 on success with FileSetId in record
359 int db_create_fileset_record(JCR *jcr, B_DB *mdb, FILESET_DBR *fsr)
366 fsr->created = false;
367 Mmsg(&mdb->cmd, "SELECT FileSetId,CreateTime FROM FileSet WHERE "
368 "FileSet='%s' AND MD5='%s'", fsr->FileSet, fsr->MD5);
371 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
373 mdb->num_rows = sql_num_rows(mdb);
375 if (mdb->num_rows > 1) {
376 Mmsg1(&mdb->errmsg, _("More than one FileSet!: %d\n"), (int)(mdb->num_rows));
377 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
379 if (mdb->num_rows >= 1) {
380 if ((row = sql_fetch_row(mdb)) == NULL) {
381 Mmsg1(&mdb->errmsg, _("error fetching FileSet row: ERR=%s\n"), sql_strerror(mdb));
382 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
383 sql_free_result(mdb);
387 fsr->FileSetId = atoi(row[0]);
388 if (row[1] == NULL) {
389 fsr->cCreateTime[0] = 0;
391 bstrncpy(fsr->cCreateTime, row[1], sizeof(fsr->cCreateTime));
393 sql_free_result(mdb);
397 sql_free_result(mdb);
400 if (fsr->CreateTime == 0 && fsr->cCreateTime[0] == 0) {
401 fsr->CreateTime = time(NULL);
403 localtime_r(&fsr->CreateTime, &tm);
404 strftime(fsr->cCreateTime, sizeof(fsr->cCreateTime), "%Y-%m-%d %T", &tm);
407 Mmsg(&mdb->cmd, "INSERT INTO FileSet (FileSet,MD5,CreateTime) "
408 "VALUES ('%s','%s','%s')", fsr->FileSet, fsr->MD5, fsr->cCreateTime);
410 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
411 Mmsg2(&mdb->errmsg, _("Create DB FileSet record %s failed. ERR=%s\n"),
412 mdb->cmd, sql_strerror(mdb));
413 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
417 fsr->FileSetId = sql_insert_id(mdb);
430 * dev_t st_dev; * device *
431 * ino_t st_ino; * inode *
432 * mode_t st_mode; * protection *
433 * nlink_t st_nlink; * number of hard links *
434 * uid_t st_uid; * user ID of owner *
435 * gid_t st_gid; * group ID of owner *
436 * dev_t st_rdev; * device type (if inode device) *
437 * off_t st_size; * total size, in bytes *
438 * unsigned long st_blksize; * blocksize for filesystem I/O *
439 * unsigned long st_blocks; * number of blocks allocated *
440 * time_t st_atime; * time of last access *
441 * time_t st_mtime; * time of last modification *
442 * time_t st_ctime; * time of last inode change *
449 * Create File record in B_DB
451 * In order to reduce database size, we store the File attributes,
452 * the FileName, and the Path separately. In principle, there
453 * is a single FileName record and a single Path record, no matter
454 * how many times it occurs. This is this subroutine, we separate
455 * the file and the path and create three database records.
457 int db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
460 Dmsg1(100, "Fname=%s\n", ar->fname);
461 Dmsg0(50, "put_file_into_catalog\n");
463 * Make sure we have an acceptable attributes record.
465 if (!(ar->Stream == STREAM_UNIX_ATTRIBUTES || ar->Stream == STREAM_WIN32_ATTRIBUTES)) {
466 Mmsg0(&mdb->errmsg, _("Attempt to put non-attributes into catalog\n"));
467 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
473 split_path_and_filename(jcr, mdb, ar->fname);
475 if (!db_create_filename_record(jcr, mdb, ar)) {
479 Dmsg1(500, "db_create_filename_record: %s\n", mdb->esc_name);
482 if (!db_create_path_record(jcr, mdb, ar)) {
486 Dmsg1(500, "db_create_path_record: %s\n", mdb->esc_name);
488 /* Now create master File record */
489 if (!db_create_file_record(jcr, mdb, ar)) {
493 Dmsg0(500, "db_create_file_record OK\n");
495 Dmsg3(100, "CreateAttributes Path=%s File=%s FilenameId=%d\n", mdb->path, mdb->fname, ar->FilenameId);
501 * This is the master File entry containing the attributes.
502 * The filename and path records have already been created.
504 static int db_create_file_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
510 ASSERT(ar->FilenameId);
514 "INSERT INTO File (FileIndex, JobId, PathId, FilenameId, \
515 LStat, MD5) VALUES (%u, %u, %u, %u, '%s', '0')",
516 ar->FileIndex, ar->JobId, ar->PathId, ar->FilenameId,
519 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
520 Mmsg2(&mdb->errmsg, _("Create db File record %s failed. ERR=%s"),
521 mdb->cmd, sql_strerror(mdb));
522 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
526 ar->FileId = sql_insert_id(mdb);
532 /* Create a Unique record for the Path -- no duplicates */
533 static int db_create_path_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
538 mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->pnl+2);
539 db_escape_string(mdb->esc_name, mdb->path, mdb->pnl);
541 if (mdb->cached_path_id != 0 && mdb->cached_path_len == mdb->pnl &&
542 strcmp(mdb->cached_path, mdb->path) == 0) {
543 ar->PathId = mdb->cached_path_id;
547 Mmsg(&mdb->cmd, "SELECT PathId FROM Path WHERE Path='%s'", mdb->esc_name);
549 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
550 mdb->num_rows = sql_num_rows(mdb);
551 if (mdb->num_rows > 1) {
553 Mmsg2(&mdb->errmsg, _("More than one Path!: %s for path: %s\n"),
554 edit_uint64(mdb->num_rows, ed1), mdb->path);
555 Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
557 /* Even if there are multiple paths, take the first one */
558 if (mdb->num_rows >= 1) {
559 if ((row = sql_fetch_row(mdb)) == NULL) {
560 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
561 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
562 sql_free_result(mdb);
567 ar->PathId = atoi(row[0]);
568 sql_free_result(mdb);
570 if (ar->PathId != mdb->cached_path_id) {
571 mdb->cached_path_id = ar->PathId;
572 mdb->cached_path_len = mdb->pnl;
573 pm_strcpy(&mdb->cached_path, mdb->path);
579 sql_free_result(mdb);
582 Mmsg(&mdb->cmd, "INSERT INTO Path (Path) VALUES ('%s')", mdb->esc_name);
584 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
585 Mmsg2(&mdb->errmsg, _("Create db Path record %s failed. ERR=%s\n"),
586 mdb->cmd, sql_strerror(mdb));
587 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
591 ar->PathId = sql_insert_id(mdb);
596 if (stat && ar->PathId != mdb->cached_path_id) {
597 mdb->cached_path_id = ar->PathId;
598 mdb->cached_path_len = mdb->pnl;
599 pm_strcpy(&mdb->cached_path, mdb->path);
604 /* Create a Unique record for the filename -- no duplicates */
605 static int db_create_filename_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
609 mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->fnl+2);
610 db_escape_string(mdb->esc_name, mdb->fname, mdb->fnl);
612 Mmsg(&mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name='%s'", mdb->esc_name);
614 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
615 mdb->num_rows = sql_num_rows(mdb);
616 if (mdb->num_rows > 1) {
618 Mmsg2(&mdb->errmsg, _("More than one Filename! %s for file: %s\n"),
619 edit_uint64(mdb->num_rows, ed1), mdb->fname);
620 Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
622 if (mdb->num_rows >= 1) {
623 if ((row = sql_fetch_row(mdb)) == NULL) {
624 Mmsg2(&mdb->errmsg, _("Error fetching row for file=%s: ERR=%s\n"),
625 mdb->fname, sql_strerror(mdb));
626 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
629 ar->FilenameId = atoi(row[0]);
631 sql_free_result(mdb);
632 return ar->FilenameId > 0;
634 sql_free_result(mdb);
637 Mmsg(&mdb->cmd, "INSERT INTO Filename (Name) VALUES ('%s')", mdb->esc_name);
639 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
640 Mmsg2(&mdb->errmsg, _("Create db Filename record %s failed. ERR=%s\n"),
641 mdb->cmd, sql_strerror(mdb));
642 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
645 ar->FilenameId = sql_insert_id(mdb);
647 return ar->FilenameId > 0;
650 #endif /* HAVE_MYSQL || HAVE_SQLITE */