2 * Bacula Catalog Database Create record interface routines
4 * Kern Sibbald, March 2000
10 Copyright (C) 2000, 2001, 2002 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 /* *****FIXME**** fix fixed length of select_cmd[] and insert_cmd[] */
31 /* The following is necessary so that we do not include
32 * the dummy external definition of DB.
34 #define __SQL_C /* indicate that this is sql.c */
39 #if HAVE_MYSQL || HAVE_SQLITE
41 /* -----------------------------------------------------------------------
43 * Generic Routines (or almost generic)
45 * -----------------------------------------------------------------------
48 /* Forward referenced subroutines */
49 static int db_create_file_record(B_DB *mdb, ATTR_DBR *ar);
50 static int db_create_filename_record(B_DB *mdb, ATTR_DBR *ar, char *fname);
51 static int db_create_path_record(B_DB *mdb, ATTR_DBR *ar, char *path);
54 /* Imported subroutines */
55 extern void print_dashes(B_DB *mdb);
56 extern void print_result(B_DB *mdb);
57 extern int QueryDB(char *file, int line, B_DB *db, char *select_cmd);
58 extern int InsertDB(char *file, int line, B_DB *db, char *select_cmd);
61 /* Create a new record for the Job
62 * Returns: 0 on failure
66 db_create_job_record(B_DB *mdb, JOB_DBR *jr)
68 char dt[MAX_TIME_LENGTH];
76 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(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(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(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(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(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(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(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(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(B_DB *mdb, MEDIA_DBR *mr)
226 char ed1[30], ed2[30], ed3[30];
229 Mmsg(&mdb->cmd, "SELECT MediaId FROM Media WHERE VolumeName='%s'",
231 Dmsg1(110, "selectpool: %s\n", mdb->cmd);
233 if (QUERY_DB(mdb, mdb->cmd)) {
234 mdb->num_rows = sql_num_rows(mdb);
235 if (mdb->num_rows > 0) {
236 Mmsg1(&mdb->errmsg, _("Media record %s already exists\n"), mr->VolumeName);
237 sql_free_result(mdb);
241 sql_free_result(mdb);
246 "INSERT INTO Media (VolumeName,MediaType,PoolId,MaxVolBytes,VolCapacityBytes, \
247 Recycle,VolRetention,VolStatus,Slot) VALUES ('%s','%s',%u,%s,%s,%d,%s,'%s',%d)",
249 mr->MediaType, mr->PoolId,
250 edit_uint64(mr->MaxVolBytes,ed1),
251 edit_uint64(mr->VolCapacityBytes, ed2),
253 edit_uint64(mr->VolRetention, ed3),
257 Dmsg1(500, "Create Volume: %s\n", mdb->cmd);
258 if (!INSERT_DB(mdb, mdb->cmd)) {
259 Mmsg2(&mdb->errmsg, _("Create DB Media record %s failed. ERR=%s\n"),
260 mdb->cmd, sql_strerror(mdb));
263 mr->MediaId = sql_insert_id(mdb);
273 * Create a Unique record for the client -- no duplicates
274 * Returns: 0 on failure
275 * 1 on success with id in cr->ClientId
277 int db_create_client_record(B_DB *mdb, CLIENT_DBR *cr)
281 char ed1[30], ed2[30];
284 Mmsg(&mdb->cmd, "SELECT ClientId,Uname FROM Client WHERE Name='%s'", cr->Name);
287 if (QUERY_DB(mdb, mdb->cmd)) {
289 mdb->num_rows = sql_num_rows(mdb);
291 /* If more than one, report error, but return first row */
292 if (mdb->num_rows > 1) {
293 Mmsg1(&mdb->errmsg, _("More than one Client!: %d\n"), (int)(mdb->num_rows));
294 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
296 if (mdb->num_rows >= 1) {
297 if ((row = sql_fetch_row(mdb)) == NULL) {
298 Mmsg1(&mdb->errmsg, _("error fetching Client row: %s\n"), sql_strerror(mdb));
299 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
300 sql_free_result(mdb);
304 cr->ClientId = atoi(row[0]);
306 strncpy(cr->Uname, row[1], sizeof(cr->Uname)-2);
307 cr->Uname[sizeof(cr->Uname)-1] = 0;
309 cr->Uname[0] = 0; /* no name */
311 sql_free_result(mdb);
315 sql_free_result(mdb);
319 Mmsg(&mdb->cmd, "INSERT INTO Client (Name, Uname, AutoPrune, \
320 FileRetention, JobRetention) VALUES \
321 ('%s', '%s', %d, %s, %s)", cr->Name, cr->Uname, cr->AutoPrune,
322 edit_uint64(cr->FileRetention, ed1),
323 edit_uint64(cr->JobRetention, ed2));
325 if (!INSERT_DB(mdb, mdb->cmd)) {
326 Mmsg2(&mdb->errmsg, _("Create DB Client record %s failed. ERR=%s\n"),
327 mdb->cmd, sql_strerror(mdb));
328 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
332 cr->ClientId = sql_insert_id(mdb);
341 * Create a FileSet record. This record is unique in the
342 * name and the MD5 signature of the include/exclude sets.
343 * Returns: 0 on failure
344 * 1 on success with FileSetId in record
346 int db_create_fileset_record(B_DB *mdb, FILESET_DBR *fsr)
352 Mmsg(&mdb->cmd, "SELECT FileSetId FROM FileSet WHERE \
353 FileSet='%s' and MD5='%s'", fsr->FileSet, fsr->MD5);
356 if (QUERY_DB(mdb, mdb->cmd)) {
358 mdb->num_rows = sql_num_rows(mdb);
360 if (mdb->num_rows > 1) {
361 Mmsg1(&mdb->errmsg, _("More than one FileSet!: %d\n"), (int)(mdb->num_rows));
362 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
364 if (mdb->num_rows >= 1) {
365 if ((row = sql_fetch_row(mdb)) == NULL) {
366 Mmsg1(&mdb->errmsg, _("error fetching FileSet row: ERR=%s\n"), sql_strerror(mdb));
367 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
368 sql_free_result(mdb);
372 fsr->FileSetId = atoi(row[0]);
373 sql_free_result(mdb);
377 sql_free_result(mdb);
381 Mmsg(&mdb->cmd, "INSERT INTO FileSet (FileSet, MD5) VALUES \
382 ('%s', '%s')", fsr->FileSet, fsr->MD5);
384 if (!INSERT_DB(mdb, mdb->cmd)) {
385 Mmsg2(&mdb->errmsg, _("Create DB FileSet record %s failed. ERR=%s\n"),
386 mdb->cmd, sql_strerror(mdb));
387 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
391 fsr->FileSetId = sql_insert_id(mdb);
403 * dev_t st_dev; * device *
404 * ino_t st_ino; * inode *
405 * mode_t st_mode; * protection *
406 * nlink_t st_nlink; * number of hard links *
407 * uid_t st_uid; * user ID of owner *
408 * gid_t st_gid; * group ID of owner *
409 * dev_t st_rdev; * device type (if inode device) *
410 * off_t st_size; * total size, in bytes *
411 * unsigned long st_blksize; * blocksize for filesystem I/O *
412 * unsigned long st_blocks; * number of blocks allocated *
413 * time_t st_atime; * time of last access *
414 * time_t st_mtime; * time of last modification *
415 * time_t st_ctime; * time of last inode change *
422 * Create File record in B_DB
424 * In order to reduce database size, we store the File attributes,
425 * the FileName, and the Path separately. In principle, there
426 * is a single FileName record and a single Path record, no matter
427 * how many times it occurs. This is this subroutine, we separate
428 * the file and the path and create three database records.
430 int db_create_file_attributes_record(B_DB *mdb, ATTR_DBR *ar)
434 /* ****FIXME***** malloc these */
435 char file[MAXSTRING];
436 char spath[MAXSTRING];
439 Dmsg1(100, "Fname=%s\n", ar->fname);
440 Dmsg0(50, "put_file_into_catalog\n");
441 /* For the moment, we only handle Unix attributes. Note, we are
442 * also getting any MD5 signature that was computed.
444 if (!(ar->Stream == STREAM_UNIX_ATTRIBUTES || ar->Stream == STREAM_WIN32_ATTRIBUTES)) {
445 Mmsg0(&mdb->errmsg, _("Attempt to put non-attributes into catalog\n"));
446 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
450 /* Find path without the filename.
451 * I.e. everything after the last / is a "filename".
452 * OK, maybe it is a directory name, but we treat it like
453 * a filename. If we don't find a / then the whole name
454 * must be a path name (e.g. c:).
456 for (p=l=ar->fname; *p; p++) {
458 l = p; /* set pos of last slash */
461 if (*l == '/') { /* did we find a slash? */
462 l++; /* yes, point to filename */
463 } else { /* no, whole thing must be path name */
467 /* If filename doesn't exist (i.e. root directory), we
468 * simply create a blank name consisting of a single
469 * space. This makes handling zero length filenames
474 Jmsg(mdb->jcr, M_WARNING, 0, _("Filename truncated to 255 chars: %s\n"), l);
478 strncpy(file, l, fnl); /* copy filename */
481 file[0] = ' '; /* blank filename */
488 Jmsg(mdb->jcr, M_WARNING, 0, _("Path name truncated to 255 chars: %s\n"), ar->fname);
491 strncpy(spath, ar->fname, pnl);
495 Mmsg1(&mdb->errmsg, _("Path length is zero. File=%s\n"), ar->fname);
496 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
502 Dmsg1(100, "spath=%s\n", spath);
503 Dmsg1(100, "file=%s\n", file);
505 db_escape_string(buf, file, fnl);
507 if (!db_create_filename_record(mdb, ar, buf)) {
510 Dmsg1(100, "db_create_filename_record: %s\n", buf);
512 db_escape_string(buf, spath, pnl);
514 if (!db_create_path_record(mdb, ar, buf)) {
517 Dmsg1(100, "db_create_path_record\n", buf);
519 if (!db_create_file_record(mdb, ar)) {
522 Dmsg0(50, "db_create_file_record\n");
524 Dmsg3(100, "Path=%s File=%s FilenameId=%d\n", spath, file, ar->FilenameId);
528 static int db_create_file_record(B_DB *mdb, ATTR_DBR *ar)
534 ASSERT(ar->FilenameId);
539 "INSERT INTO File (FileIndex, JobId, PathId, FilenameId, \
540 LStat, MD5) VALUES (%u, %u, %u, %u, '%s', '0')",
541 ar->FileIndex, ar->JobId, ar->PathId, ar->FilenameId,
544 if (!INSERT_DB(mdb, mdb->cmd)) {
545 Mmsg2(&mdb->errmsg, _("Create db File record %s failed. ERR=%s"),
546 mdb->cmd, sql_strerror(mdb));
547 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
551 ar->FileId = sql_insert_id(mdb);
558 /* Create a Unique record for the Path -- no duplicates */
559 static int db_create_path_record(B_DB *mdb, ATTR_DBR *ar, char *path)
565 Mmsg0(&mdb->errmsg, _("Null path given to db_create_path_record\n"));
566 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
574 if (mdb->cached_path_id != 0 && strcmp(mdb->cached_path, path) == 0) {
575 ar->PathId = mdb->cached_path_id;
581 Mmsg(&mdb->cmd, "SELECT PathId FROM Path WHERE Path='%s'", path);
583 if (QUERY_DB(mdb, mdb->cmd)) {
585 mdb->num_rows = sql_num_rows(mdb);
587 if (mdb->num_rows > 1) {
589 Mmsg2(&mdb->errmsg, _("More than one Path!: %s for Path=%s\n"),
590 edit_uint64(mdb->num_rows, ed1), path);
591 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
593 if (mdb->num_rows >= 1) {
594 if ((row = sql_fetch_row(mdb)) == NULL) {
596 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
597 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
598 sql_free_result(mdb);
603 ar->PathId = atoi(row[0]);
604 sql_free_result(mdb);
606 if (ar->PathId != mdb->cached_path_id) {
607 mdb->cached_path_id = ar->PathId;
608 mdb->cached_path = check_pool_memory_size(mdb->cached_path,
610 strcpy(mdb->cached_path, path);
617 sql_free_result(mdb);
620 Mmsg(&mdb->cmd, "INSERT INTO Path (Path) VALUES ('%s')", path);
622 if (!INSERT_DB(mdb, mdb->cmd)) {
623 Mmsg2(&mdb->errmsg, _("Create db Path record %s failed. ERR=%s\n"),
624 mdb->cmd, sql_strerror(mdb));
625 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
629 ar->PathId = sql_insert_id(mdb);
634 if (ar->PathId != mdb->cached_path_id) {
635 mdb->cached_path_id = ar->PathId;
636 mdb->cached_path = check_pool_memory_size(mdb->cached_path,
638 strcpy(mdb->cached_path, path);
645 /* Create a Unique record for the filename -- no duplicates */
646 static int db_create_filename_record(B_DB *mdb, ATTR_DBR *ar, char *fname)
651 Mmsg(&mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name='%s'", fname);
653 if (QUERY_DB(mdb, mdb->cmd)) {
654 mdb->num_rows = sql_num_rows(mdb);
655 if (mdb->num_rows > 1) {
656 Mmsg2(&mdb->errmsg, _("More than one Filename!: %d File=%s\n"),
657 (int)(mdb->num_rows), fname);
658 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
660 if (mdb->num_rows >= 1) {
661 if ((row = sql_fetch_row(mdb)) == NULL) {
662 Mmsg2(&mdb->errmsg, _("error fetching row for file=%s: ERR=%s\n"),
663 fname, sql_strerror(mdb));
664 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
667 ar->FilenameId = atoi(row[0]);
669 sql_free_result(mdb);
671 return ar->FilenameId > 0;
673 sql_free_result(mdb);
676 Mmsg(&mdb->cmd, "INSERT INTO Filename (Name) \
677 VALUES ('%s')", fname);
679 if (!INSERT_DB(mdb, mdb->cmd)) {
680 Mmsg2(&mdb->errmsg, _("Create db Filename record %s failed. ERR=%s\n"),
681 mdb->cmd, sql_strerror(mdb));
682 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
685 ar->FilenameId = sql_insert_id(mdb);
689 return ar->FilenameId > 0;
692 #endif /* HAVE_MYSQL || HAVE_SQLITE */