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(void *jcr, B_DB *mdb, ATTR_DBR *ar);
48 static int db_create_filename_record(void *jcr, B_DB *mdb, ATTR_DBR *ar);
49 static int db_create_path_record(void *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, void *jcr, B_DB *db, char *select_cmd);
56 extern int InsertDB(char *file, int line, void *jcr, B_DB *db, char *select_cmd);
57 extern void split_path_and_filename(void *jcr, B_DB *mdb, char *fname);
60 /* Create a new record for the Job
61 * Returns: 0 on failure
65 db_create_job_record(void *jcr, B_DB *mdb, JOB_DBR *jr)
67 char dt[MAX_TIME_LENGTH];
75 stime = jr->SchedTime;
78 localtime_r(&stime, &tm);
79 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
80 JobTDate = (utime_t)stime;
83 if (!db_next_index(jcr, mdb, "Job", JobId)) {
90 "INSERT INTO Job (JobId,Job,Name,Type,Level,JobStatus,SchedTime,JobTDate) VALUES \
91 (%s,'%s','%s','%c','%c','%c','%s',%s)",
92 JobId, jr->Job, jr->Name, (char)(jr->Type), (char)(jr->Level),
93 (char)(jr->JobStatus), dt, edit_uint64(JobTDate, ed1));
95 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
96 Mmsg2(&mdb->errmsg, _("Create DB Job record %s failed. ERR=%s\n"),
97 mdb->cmd, sql_strerror(mdb));
101 jr->JobId = sql_insert_id(mdb);
108 /* Create a JobMedia record for medium used this job
109 * Returns: 0 on failure
113 db_create_jobmedia_record(void *jcr, B_DB *mdb, JOBMEDIA_DBR *jm)
118 Mmsg(&mdb->cmd, "SELECT JobId, MediaId FROM JobMedia WHERE \
119 JobId=%d AND MediaId=%d", jm->JobId, jm->MediaId);
122 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
123 mdb->num_rows = sql_num_rows(mdb);
124 if (mdb->num_rows > 0) {
125 Mmsg0(&mdb->errmsg, _("Create JobMedia failed. Record already exists.\n"));
126 sql_free_result(mdb);
128 Dmsg0(0, "Already have JobMedia record\n");
131 sql_free_result(mdb);
136 "INSERT INTO JobMedia (JobId,MediaId,FirstIndex,LastIndex,\
137 StartFile,EndFile,StartBlock,EndBlock) \
138 VALUES (%u,%u,%u,%u,%u,%u,%u,%u)",
139 jm->JobId, jm->MediaId, jm->FirstIndex, jm->LastIndex,
140 jm->StartFile, jm->EndFile, jm->StartBlock, jm->EndBlock);
143 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
144 Mmsg2(&mdb->errmsg, _("Create db JobMedia record %s failed. ERR=%s\n"), mdb->cmd,
151 Dmsg0(30, "Return from JobMedia\n");
157 /* Create Unique Pool record
158 * Returns: 0 on failure
162 db_create_pool_record(void *jcr, B_DB *mdb, POOL_DBR *pr)
165 char ed1[30], ed2[30], ed3[50];
167 Dmsg0(200, "In create pool\n");
169 Mmsg(&mdb->cmd, "SELECT PoolId,Name FROM Pool WHERE Name='%s'", pr->Name);
170 Dmsg1(200, "selectpool: %s\n", mdb->cmd);
172 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
174 mdb->num_rows = sql_num_rows(mdb);
176 if (mdb->num_rows > 0) {
177 Mmsg1(&mdb->errmsg, _("pool record %s already exists\n"), pr->Name);
178 sql_free_result(mdb);
182 sql_free_result(mdb);
187 "INSERT INTO Pool (Name,NumVols,MaxVols,UseOnce,UseCatalog,\
188 AcceptAnyVolume,AutoPrune,Recycle,VolRetention,VolUseDuration,\
189 MaxVolJobs,MaxVolFiles,MaxVolBytes,PoolType,LabelFormat) \
190 VALUES ('%s',%u,%u,%d,%d,%d,%d,%d,%s,%s,%u,%u,%s,'%s','%s')",
192 pr->NumVols, pr->MaxVols,
193 pr->UseOnce, pr->UseCatalog,
195 pr->AutoPrune, pr->Recycle,
196 edit_uint64(pr->VolRetention, ed1),
197 edit_uint64(pr->VolUseDuration, ed2),
198 pr->MaxVolJobs, pr->MaxVolFiles,
199 edit_uint64(pr->MaxVolBytes, ed3),
200 pr->PoolType, pr->LabelFormat);
201 Dmsg1(200, "Create Pool: %s\n", mdb->cmd);
202 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
203 Mmsg2(&mdb->errmsg, _("Create db Pool record %s failed: ERR=%s\n"),
204 mdb->cmd, sql_strerror(mdb));
208 pr->PoolId = sql_insert_id(mdb);
218 * Create Unique Media record
219 * Returns: 0 on failure
223 db_create_media_record(void *jcr, B_DB *mdb, MEDIA_DBR *mr)
226 char ed1[30], ed2[30], ed3[30], ed4[30];
227 char dt[MAX_TIME_LENGTH];
231 Mmsg(&mdb->cmd, "SELECT MediaId FROM Media WHERE VolumeName='%s'",
233 Dmsg1(110, "selectpool: %s\n", mdb->cmd);
235 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
236 mdb->num_rows = sql_num_rows(mdb);
237 if (mdb->num_rows > 0) {
238 Mmsg1(&mdb->errmsg, _("Volume \"%s\" already exists\n."), mr->VolumeName);
239 sql_free_result(mdb);
243 sql_free_result(mdb);
248 localtime_r(&mr->LabelDate, &tm);
249 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
251 strcpy(dt, "0000-00-00 00:00:00");
254 "INSERT INTO Media (VolumeName,MediaType,PoolId,MaxVolBytes,VolCapacityBytes,"
255 "Recycle,VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,"
256 "VolStatus,LabelDate,Slot) "
257 "VALUES ('%s','%s',%u,%s,%s,%d,%s,%s,%u,%u,'%s','%s',%d)",
259 mr->MediaType, mr->PoolId,
260 edit_uint64(mr->MaxVolBytes,ed1),
261 edit_uint64(mr->VolCapacityBytes, ed2),
263 edit_uint64(mr->VolRetention, ed3),
264 edit_uint64(mr->VolUseDuration, ed4),
270 Dmsg1(500, "Create Volume: %s\n", mdb->cmd);
271 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
272 Mmsg2(&mdb->errmsg, _("Create DB Media record %s failed. ERR=%s\n"),
273 mdb->cmd, sql_strerror(mdb));
276 mr->MediaId = sql_insert_id(mdb);
286 * Create a Unique record for the client -- no duplicates
287 * Returns: 0 on failure
288 * 1 on success with id in cr->ClientId
290 int db_create_client_record(void *jcr, B_DB *mdb, CLIENT_DBR *cr)
294 char ed1[30], ed2[30];
297 Mmsg(&mdb->cmd, "SELECT ClientId,Uname FROM Client WHERE Name='%s'", cr->Name);
300 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
302 mdb->num_rows = sql_num_rows(mdb);
304 /* If more than one, report error, but return first row */
305 if (mdb->num_rows > 1) {
306 Mmsg1(&mdb->errmsg, _("More than one Client!: %d\n"), (int)(mdb->num_rows));
307 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
309 if (mdb->num_rows >= 1) {
310 if ((row = sql_fetch_row(mdb)) == NULL) {
311 Mmsg1(&mdb->errmsg, _("error fetching Client row: %s\n"), sql_strerror(mdb));
312 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
313 sql_free_result(mdb);
317 cr->ClientId = atoi(row[0]);
319 bstrncpy(cr->Uname, row[1], sizeof(cr->Uname));
321 cr->Uname[0] = 0; /* no name */
323 sql_free_result(mdb);
327 sql_free_result(mdb);
331 Mmsg(&mdb->cmd, "INSERT INTO Client (Name, Uname, AutoPrune, \
332 FileRetention, JobRetention) VALUES \
333 ('%s', '%s', %d, %s, %s)", cr->Name, cr->Uname, cr->AutoPrune,
334 edit_uint64(cr->FileRetention, ed1),
335 edit_uint64(cr->JobRetention, ed2));
337 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
338 Mmsg2(&mdb->errmsg, _("Create DB Client record %s failed. ERR=%s\n"),
339 mdb->cmd, sql_strerror(mdb));
340 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
344 cr->ClientId = sql_insert_id(mdb);
353 * Create a FileSet record. This record is unique in the
354 * name and the MD5 signature of the include/exclude sets.
355 * Returns: 0 on failure
356 * 1 on success with FileSetId in record
358 int db_create_fileset_record(void *jcr, B_DB *mdb, FILESET_DBR *fsr)
364 Mmsg(&mdb->cmd, "SELECT FileSetId FROM FileSet WHERE \
365 FileSet='%s' and MD5='%s'", fsr->FileSet, fsr->MD5);
368 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
370 mdb->num_rows = sql_num_rows(mdb);
372 if (mdb->num_rows > 1) {
373 Mmsg1(&mdb->errmsg, _("More than one FileSet!: %d\n"), (int)(mdb->num_rows));
374 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
376 if (mdb->num_rows >= 1) {
377 if ((row = sql_fetch_row(mdb)) == NULL) {
378 Mmsg1(&mdb->errmsg, _("error fetching FileSet row: ERR=%s\n"), sql_strerror(mdb));
379 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
380 sql_free_result(mdb);
384 fsr->FileSetId = atoi(row[0]);
385 sql_free_result(mdb);
389 sql_free_result(mdb);
393 Mmsg(&mdb->cmd, "INSERT INTO FileSet (FileSet, MD5) VALUES \
394 ('%s', '%s')", fsr->FileSet, fsr->MD5);
396 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
397 Mmsg2(&mdb->errmsg, _("Create DB FileSet record %s failed. ERR=%s\n"),
398 mdb->cmd, sql_strerror(mdb));
399 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
403 fsr->FileSetId = sql_insert_id(mdb);
415 * dev_t st_dev; * device *
416 * ino_t st_ino; * inode *
417 * mode_t st_mode; * protection *
418 * nlink_t st_nlink; * number of hard links *
419 * uid_t st_uid; * user ID of owner *
420 * gid_t st_gid; * group ID of owner *
421 * dev_t st_rdev; * device type (if inode device) *
422 * off_t st_size; * total size, in bytes *
423 * unsigned long st_blksize; * blocksize for filesystem I/O *
424 * unsigned long st_blocks; * number of blocks allocated *
425 * time_t st_atime; * time of last access *
426 * time_t st_mtime; * time of last modification *
427 * time_t st_ctime; * time of last inode change *
434 * Create File record in B_DB
436 * In order to reduce database size, we store the File attributes,
437 * the FileName, and the Path separately. In principle, there
438 * is a single FileName record and a single Path record, no matter
439 * how many times it occurs. This is this subroutine, we separate
440 * the file and the path and create three database records.
442 int db_create_file_attributes_record(void *jcr, B_DB *mdb, ATTR_DBR *ar)
445 Dmsg1(100, "Fname=%s\n", ar->fname);
446 Dmsg0(50, "put_file_into_catalog\n");
448 * Make sure we have an acceptable attributes record.
450 if (!(ar->Stream == STREAM_UNIX_ATTRIBUTES || ar->Stream == STREAM_WIN32_ATTRIBUTES)) {
451 Mmsg0(&mdb->errmsg, _("Attempt to put non-attributes into catalog\n"));
452 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
458 split_path_and_filename(jcr, mdb, ar->fname);
460 if (!db_create_filename_record(jcr, mdb, ar)) {
464 Dmsg1(500, "db_create_filename_record: %s\n", mdb->esc_name);
467 if (!db_create_path_record(jcr, mdb, ar)) {
471 Dmsg1(500, "db_create_path_record: %s\n", mdb->esc_name);
473 /* Now create master File record */
474 if (!db_create_file_record(jcr, mdb, ar)) {
478 Dmsg0(500, "db_create_file_record OK\n");
480 Dmsg3(100, "CreateAttributes Path=%s File=%s FilenameId=%d\n", mdb->path, mdb->fname, ar->FilenameId);
486 * This is the master File entry containing the attributes.
487 * The filename and path records have already been created.
489 static int db_create_file_record(void *jcr, B_DB *mdb, ATTR_DBR *ar)
495 ASSERT(ar->FilenameId);
499 "INSERT INTO File (FileIndex, JobId, PathId, FilenameId, \
500 LStat, MD5) VALUES (%u, %u, %u, %u, '%s', '0')",
501 ar->FileIndex, ar->JobId, ar->PathId, ar->FilenameId,
504 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
505 Mmsg2(&mdb->errmsg, _("Create db File record %s failed. ERR=%s"),
506 mdb->cmd, sql_strerror(mdb));
507 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
511 ar->FileId = sql_insert_id(mdb);
517 /* Create a Unique record for the Path -- no duplicates */
518 static int db_create_path_record(void *jcr, B_DB *mdb, ATTR_DBR *ar)
523 mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->pnl+2);
524 db_escape_string(mdb->esc_name, mdb->path, mdb->pnl);
526 if (mdb->cached_path_id != 0 && mdb->cached_path_len == mdb->pnl &&
527 strcmp(mdb->cached_path, mdb->path) == 0) {
528 ar->PathId = mdb->cached_path_id;
532 Mmsg(&mdb->cmd, "SELECT PathId FROM Path WHERE Path='%s'", mdb->esc_name);
534 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
535 mdb->num_rows = sql_num_rows(mdb);
536 if (mdb->num_rows > 1) {
538 Mmsg2(&mdb->errmsg, _("More than one Path!: %s for path: %s\n"),
539 edit_uint64(mdb->num_rows, ed1), mdb->path);
540 Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
542 /* Even if there are multiple paths, take the first one */
543 if (mdb->num_rows >= 1) {
544 if ((row = sql_fetch_row(mdb)) == NULL) {
545 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
546 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
547 sql_free_result(mdb);
552 ar->PathId = atoi(row[0]);
553 sql_free_result(mdb);
555 if (ar->PathId != mdb->cached_path_id) {
556 mdb->cached_path_id = ar->PathId;
557 mdb->cached_path_len = mdb->pnl;
558 pm_strcpy(&mdb->cached_path, mdb->path);
564 sql_free_result(mdb);
567 Mmsg(&mdb->cmd, "INSERT INTO Path (Path) VALUES ('%s')", mdb->esc_name);
569 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
570 Mmsg2(&mdb->errmsg, _("Create db Path record %s failed. ERR=%s\n"),
571 mdb->cmd, sql_strerror(mdb));
572 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
576 ar->PathId = sql_insert_id(mdb);
581 if (stat && ar->PathId != mdb->cached_path_id) {
582 mdb->cached_path_id = ar->PathId;
583 mdb->cached_path_len = mdb->pnl;
584 pm_strcpy(&mdb->cached_path, mdb->path);
589 /* Create a Unique record for the filename -- no duplicates */
590 static int db_create_filename_record(void *jcr, B_DB *mdb, ATTR_DBR *ar)
594 mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->fnl+2);
595 db_escape_string(mdb->esc_name, mdb->fname, mdb->fnl);
597 Mmsg(&mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name='%s'", mdb->esc_name);
599 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
600 mdb->num_rows = sql_num_rows(mdb);
601 if (mdb->num_rows > 1) {
603 Mmsg2(&mdb->errmsg, _("More than one Filename! %s for file: %s\n"),
604 edit_uint64(mdb->num_rows, ed1), mdb->fname);
605 Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
607 if (mdb->num_rows >= 1) {
608 if ((row = sql_fetch_row(mdb)) == NULL) {
609 Mmsg2(&mdb->errmsg, _("Error fetching row for file=%s: ERR=%s\n"),
610 mdb->fname, sql_strerror(mdb));
611 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
614 ar->FilenameId = atoi(row[0]);
616 sql_free_result(mdb);
617 return ar->FilenameId > 0;
619 sql_free_result(mdb);
622 Mmsg(&mdb->cmd, "INSERT INTO Filename (Name) VALUES ('%s')", mdb->esc_name);
624 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
625 Mmsg2(&mdb->errmsg, _("Create db Filename record %s failed. ERR=%s\n"),
626 mdb->cmd, sql_strerror(mdb));
627 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
630 ar->FilenameId = sql_insert_id(mdb);
632 return ar->FilenameId > 0;
635 #endif /* HAVE_MYSQL || HAVE_SQLITE */