2 * Bacula Catalog Database Create record interface routines
4 * Kern Sibbald, March 2000
8 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public
21 License along with this program; if not, write to the Free
22 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 /* *****FIXME**** fix fixed length of select_cmd[] and insert_cmd[] */
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(B_DB *mdb, ATTR_DBR *ar);
48 static int db_create_filename_record(B_DB *mdb, ATTR_DBR *ar, char *fname);
49 static int db_create_path_record(B_DB *mdb, ATTR_DBR *ar, char *path);
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, B_DB *db, char *select_cmd);
56 extern int InsertDB(char *file, int line, B_DB *db, char *select_cmd);
59 /* Create a new record for the Job
60 * Returns: 0 on failure
64 db_create_job_record(B_DB *mdb, JOB_DBR *jr)
66 char dt[MAX_TIME_LENGTH];
74 stime = jr->SchedTime;
76 localtime_r(&stime, &tm);
77 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
78 JobTDate = (btime_t)stime;
81 JobId = db_next_index(mdb, "Job");
89 "INSERT INTO Job (JobId, Job, Name, Type, Level, SchedTime, JobTDate) VALUES \
90 (%s, \"%s\", \"%s\", \"%c\", \"%c\", \"%s\", %s)",
91 JobId, jr->Job, jr->Name, (char)(jr->Type), (char)(jr->Level), dt,
92 edit_uint64(JobTDate, ed1));
94 if (!INSERT_DB(mdb, mdb->cmd)) {
95 Mmsg2(&mdb->errmsg, _("Create DB Job record %s failed. ERR=%s\n"),
96 mdb->cmd, sql_strerror(mdb));
100 jr->JobId = sql_insert_id(mdb);
107 /* Create a JobMedia record for medium used this job
108 * Returns: 0 on failure
112 db_create_jobmedia_record(B_DB *mdb, JOBMEDIA_DBR *jm)
117 Mmsg(&mdb->cmd, "SELECT JobId, MediaId FROM JobMedia WHERE \
118 JobId=%d AND MediaId=%d", jm->JobId, jm->MediaId);
121 if (QUERY_DB(mdb, mdb->cmd)) {
122 mdb->num_rows = sql_num_rows(mdb);
123 if (mdb->num_rows > 0) {
124 Mmsg0(&mdb->errmsg, _("Create JobMedia failed. Record already exists.\n"));
125 sql_free_result(mdb);
127 Dmsg0(0, "Already have JobMedia record\n");
130 sql_free_result(mdb);
135 "INSERT INTO JobMedia (JobId, MediaId, FirstIndex, LastIndex) \
136 VALUES (%d, %d, %u, %u)",
137 jm->JobId, jm->MediaId, jm->FirstIndex, jm->LastIndex);
140 if (!INSERT_DB(mdb, mdb->cmd)) {
141 Mmsg2(&mdb->errmsg, _("Create db JobMedia record %s failed. ERR=%s\n"), mdb->cmd,
148 Dmsg0(30, "Return from JobMedia\n");
154 /* Create Unique Pool record
155 * Returns: 0 on failure
159 db_create_pool_record(B_DB *mdb, POOL_DBR *pr)
165 Mmsg(&mdb->cmd, "SELECT PoolId,Name FROM Pool WHERE Name=\"%s\"", pr->Name);
166 Dmsg1(20, "selectpool: %s\n", mdb->cmd);
168 if (QUERY_DB(mdb, mdb->cmd)) {
170 mdb->num_rows = sql_num_rows(mdb);
172 if (mdb->num_rows > 0) {
173 Mmsg1(&mdb->errmsg, _("pool record %s already exists\n"), pr->Name);
174 sql_free_result(mdb);
178 sql_free_result(mdb);
183 "INSERT INTO Pool (Name, NumVols, MaxVols, UseOnce, UseCatalog, \
184 AcceptAnyVolume, AutoPrune, Recycle, VolRetention, PoolType, LabelFormat) \
185 VALUES (\"%s\", %d, %d, %d, %d, %d, %d, %d, %s, \"%s\", \"%s\")",
187 pr->NumVols, pr->MaxVols,
188 pr->UseOnce, pr->UseCatalog,
190 pr->AutoPrune, pr->Recycle,
191 edit_uint64(pr->VolRetention, ed1),
192 pr->PoolType, pr->LabelFormat);
193 Dmsg1(500, "Create Pool: %s\n", mdb->cmd);
194 if (!INSERT_DB(mdb, mdb->cmd)) {
195 Mmsg2(&mdb->errmsg, _("Create db Pool record %s failed: ERR=%s\n"),
196 mdb->cmd, sql_strerror(mdb));
200 pr->PoolId = sql_insert_id(mdb);
210 * Create Unique Media record
211 * Returns: 0 on failure
215 db_create_media_record(B_DB *mdb, MEDIA_DBR *mr)
218 char ed1[30], ed2[30], ed3[30];
221 Mmsg(&mdb->cmd, "SELECT MediaId FROM Media WHERE VolumeName=\"%s\"",
223 Dmsg1(110, "selectpool: %s\n", mdb->cmd);
225 if (QUERY_DB(mdb, mdb->cmd)) {
226 mdb->num_rows = sql_num_rows(mdb);
227 if (mdb->num_rows > 0) {
228 Mmsg1(&mdb->errmsg, _("Media record %s already exists\n"), mr->VolumeName);
229 sql_free_result(mdb);
233 sql_free_result(mdb);
238 "INSERT INTO Media (VolumeName, MediaType, PoolId, VolMaxBytes, VolCapacityBytes, \
239 Recycle, VolRetention, VolStatus) VALUES (\"%s\", \"%s\", %d, %s, %s, %d, %s, \"%s\")",
241 mr->MediaType, mr->PoolId,
242 edit_uint64(mr->VolMaxBytes,ed1),
243 edit_uint64(mr->VolCapacityBytes, ed2),
245 edit_uint64(mr->VolRetention, ed3),
248 Dmsg1(500, "Create Volume: %s\n", mdb->cmd);
249 if (!INSERT_DB(mdb, mdb->cmd)) {
250 Mmsg2(&mdb->errmsg, _("Create DB Media record %s failed. ERR=%s\n"),
251 mdb->cmd, sql_strerror(mdb));
254 mr->MediaId = sql_insert_id(mdb);
264 * Create a Unique record for the client -- no duplicates
265 * Returns: 0 on failure
266 * 1 on success with id in cr->ClientId
268 int db_create_client_record(B_DB *mdb, CLIENT_DBR *cr)
272 char ed1[30], ed2[30];
275 Mmsg(&mdb->cmd, "SELECT ClientId FROM Client WHERE Name=\"%s\"", cr->Name);
278 if (QUERY_DB(mdb, mdb->cmd)) {
280 mdb->num_rows = sql_num_rows(mdb);
282 /* If more than one, report error, but return first row */
283 if (mdb->num_rows > 1) {
284 Mmsg1(&mdb->errmsg, _("More than one Client!: %d\n"), (int)(mdb->num_rows));
285 Emsg0(M_ERROR, 0, mdb->errmsg);
287 if (mdb->num_rows >= 1) {
288 if ((row = sql_fetch_row(mdb)) == NULL) {
289 Mmsg1(&mdb->errmsg, _("error fetching Client row: %s\n"), sql_strerror(mdb));
290 Emsg0(M_ERROR, 0, mdb->errmsg);
291 sql_free_result(mdb);
295 sql_free_result(mdb);
296 cr->ClientId = atoi(row[0]);
300 sql_free_result(mdb);
304 Mmsg(&mdb->cmd, "INSERT INTO Client (Name, Uname, AutoPrune, \
305 FileRetention, JobRetention) VALUES \
306 (\"%s\", \"%s\", %d, %s, %s)", cr->Name, cr->Uname, cr->AutoPrune,
307 edit_uint64(cr->FileRetention, ed1),
308 edit_uint64(cr->JobRetention, ed2));
310 if (!INSERT_DB(mdb, mdb->cmd)) {
311 Mmsg2(&mdb->errmsg, _("Create DB Client record %s failed. ERR=%s\n"),
312 mdb->cmd, sql_strerror(mdb));
316 cr->ClientId = sql_insert_id(mdb);
325 * Create a FileSet record. This record is unique in the
326 * name and the MD5 signature of the include/exclude sets.
327 * Returns: 0 on failure
328 * 1 on success with FileSetId in record
330 int db_create_fileset_record(B_DB *mdb, FILESET_DBR *fsr)
336 Mmsg(&mdb->cmd, "SELECT FileSetId FROM FileSet WHERE \
337 FileSet=\"%s\" and MD5=\"%s\"", fsr->FileSet, fsr->MD5);
340 if (QUERY_DB(mdb, mdb->cmd)) {
342 mdb->num_rows = sql_num_rows(mdb);
344 if (mdb->num_rows > 1) {
345 Mmsg1(&mdb->errmsg, _("More than one FileSet!: %d\n"), (int)(mdb->num_rows));
346 Emsg0(M_ERROR, 0, mdb->errmsg);
348 if (mdb->num_rows >= 1) {
349 if ((row = sql_fetch_row(mdb)) == NULL) {
350 Mmsg1(&mdb->errmsg, _("error fetching FileSet row: ERR=%s\n"), sql_strerror(mdb));
351 Emsg0(M_ERROR, 0, mdb->errmsg);
352 sql_free_result(mdb);
356 sql_free_result(mdb);
357 fsr->FileSetId = atoi(row[0]);
361 sql_free_result(mdb);
365 Mmsg(&mdb->cmd, "INSERT INTO FileSet (FileSet, MD5) VALUES \
366 (\"%s\", \"%s\")", fsr->FileSet, fsr->MD5);
368 if (!INSERT_DB(mdb, mdb->cmd)) {
369 Mmsg2(&mdb->errmsg, _("Create DB FileSet record %s failed. ERR=%s\n"),
370 mdb->cmd, sql_strerror(mdb));
374 fsr->FileSetId = sql_insert_id(mdb);
386 * dev_t st_dev; * device *
387 * ino_t st_ino; * inode *
388 * mode_t st_mode; * protection *
389 * nlink_t st_nlink; * number of hard links *
390 * uid_t st_uid; * user ID of owner *
391 * gid_t st_gid; * group ID of owner *
392 * dev_t st_rdev; * device type (if inode device) *
393 * off_t st_size; * total size, in bytes *
394 * unsigned long st_blksize; * blocksize for filesystem I/O *
395 * unsigned long st_blocks; * number of blocks allocated *
396 * time_t st_atime; * time of last access *
397 * time_t st_mtime; * time of last modification *
398 * time_t st_ctime; * time of last inode change *
405 * Create File record in B_DB
407 * In order to reduce database size, we store the File attributes,
408 * the FileName, and the Path separately. In principle, there
409 * is a single FileName record and a single Path record, no matter
410 * how many times it occurs. This is this subroutine, we separate
411 * the file and the path and create three database records.
413 int db_create_file_attributes_record(B_DB *mdb, ATTR_DBR *ar)
417 /* ****FIXME***** malloc these */
418 char file[MAXSTRING];
419 char spath[MAXSTRING];
422 Dmsg1(100, "Fname=%s\n", ar->fname);
423 Dmsg0(50, "put_file_into_catalog\n");
424 /* For the moment, we only handle Unix attributes. Note, we are
425 * also getting any MD5 signature that was computed.
427 if (ar->Stream != STREAM_UNIX_ATTRIBUTES) {
428 Mmsg0(&mdb->errmsg, _("Attempt to put non-attributes into catalog\n"));
432 /* Find path without the filename.
433 * I.e. everything after the last / is a "filename".
434 * OK, maybe it is a directory name, but we treat it like
435 * a filename. If we don't find a / then the whole name
436 * must be a path name (e.g. c:).
438 for (p=l=ar->fname; *p; p++) {
440 l = p; /* set pos of last slash */
443 if (*l == '/') { /* did we find a slash? */
444 l++; /* yes, point to filename */
445 } else { /* no, whole thing must be path name */
449 /* If filename doesn't exist (i.e. root directory), we
450 * simply create a blank name consisting of a single
451 * space. This makes handling zero length filenames
456 Emsg1(M_WARNING, 0, _("Filename truncated to 255 chars: %s\n"), l);
460 strncpy(file, l, fnl); /* copy filename */
463 file[0] = ' '; /* blank filename */
469 Emsg1(M_WARNING, 0, _("Path name truncated to 255 chars: %s\n"), ar->fname);
472 strncpy(spath, ar->fname, pnl);
476 Mmsg1(&mdb->errmsg, _("Path length is zero. File=%s\n"), ar->fname);
477 Emsg0(M_ERROR, 0, mdb->errmsg);
482 Dmsg1(100, "spath=%s\n", spath);
483 Dmsg1(100, "file=%s\n", file);
485 db_escape_string(buf, file, fnl);
487 if (!db_create_filename_record(mdb, ar, buf)) {
490 Dmsg1(100, "db_create_filename_record: %s\n", buf);
492 db_escape_string(buf, spath, pnl);
494 if (!db_create_path_record(mdb, ar, buf)) {
497 Dmsg1(100, "db_create_path_record\n", buf);
499 if (!db_create_file_record(mdb, ar)) {
502 Dmsg0(50, "db_create_file_record\n");
504 Dmsg3(100, "Path=%s File=%s FilenameId=%d\n", spath, file, ar->FilenameId);
509 static int db_create_file_record(B_DB *mdb, ATTR_DBR *ar)
516 "INSERT INTO File (FileIndex, JobId, PathId, FilenameId, \
517 LStat, MD5) VALUES (%d, %d, %d, %d, \"%s\", \"0\")",
518 (int)ar->FileIndex, ar->JobId, ar->PathId, ar->FilenameId,
521 if (!INSERT_DB(mdb, mdb->cmd)) {
522 Mmsg2(&mdb->errmsg, _("Create db File record %s failed. ERR=%s"),
523 mdb->cmd, sql_strerror(mdb));
524 Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
528 ar->FileId = sql_insert_id(mdb);
535 /* Create a Unique record for the Path -- no duplicates */
536 static int db_create_path_record(B_DB *mdb, ATTR_DBR *ar, char *path)
539 static uint32_t cached_id = 0;
540 static char cached_path[MAXSTRING];
544 Mmsg0(&mdb->errmsg, _("Null path given to db_create_path_record\n"));
545 Emsg0(M_ERROR, 0, mdb->errmsg);
552 if (cached_id != 0 && strcmp(cached_path, path) == 0) {
553 ar->PathId = cached_id;
558 Mmsg(&mdb->cmd, "SELECT PathId FROM Path WHERE Path=\"%s\"", path);
560 if (QUERY_DB(mdb, mdb->cmd)) {
562 mdb->num_rows = sql_num_rows(mdb);
564 if (mdb->num_rows > 1) {
566 Mmsg2(&mdb->errmsg, _("More than one Path!: %s for Path=%s\n"),
567 edit_uint64(mdb->num_rows, ed1), path);
568 Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
569 Emsg1(M_ERROR, 0, "%s\n", mdb->cmd);
571 if (mdb->num_rows >= 1) {
572 if ((row = sql_fetch_row(mdb)) == NULL) {
574 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
575 Emsg0(M_ERROR, 0, mdb->errmsg);
576 sql_free_result(mdb);
580 sql_free_result(mdb);
581 ar->PathId = atoi(row[0]);
582 if (ar->PathId != cached_id) {
583 cached_id = ar->PathId;
584 strncpy(cached_path, path, sizeof(cached_path));
585 cached_path[sizeof(cached_path)-1] = 0;
591 sql_free_result(mdb);
594 Mmsg(&mdb->cmd, "INSERT INTO Path (Path) VALUES (\"%s\")", path);
596 if (!INSERT_DB(mdb, mdb->cmd)) {
597 Mmsg2(&mdb->errmsg, _("Create db Path record %s failed. ERR=%s\n"),
598 mdb->cmd, sql_strerror(mdb));
599 Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
603 ar->PathId = sql_insert_id(mdb);
607 if (ar->PathId != cached_id) {
608 cached_id = ar->PathId;
609 strncpy(cached_path, path, sizeof(cached_path));
610 cached_path[sizeof(cached_path)-1] = 0;
616 /* Create a Unique record for the filename -- no duplicates */
617 static int db_create_filename_record(B_DB *mdb, ATTR_DBR *ar, char *fname)
623 Mmsg(&mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name=\"%s\"", fname);
625 if (QUERY_DB(mdb, mdb->cmd)) {
626 mdb->num_rows = sql_num_rows(mdb);
627 if (mdb->num_rows > 1) {
628 Mmsg2(&mdb->errmsg, _("More than one Filename!: %d File=%s\n"),
629 (int)(mdb->num_rows), fname);
630 Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
631 Emsg1(M_ERROR, 0, "%s\n", mdb->cmd);
633 if (mdb->num_rows >= 1) {
634 if ((row = sql_fetch_row(mdb)) == NULL) {
635 Mmsg2(&mdb->errmsg, _("error fetching row for file=%s: ERR=%s\n"),
636 fname, sql_strerror(mdb));
637 sql_free_result(mdb);
639 Emsg0(M_ERROR, 0, mdb->errmsg);
643 sql_free_result(mdb);
644 ar->FilenameId = atoi(row[0]);
648 sql_free_result(mdb);
651 Mmsg(&mdb->cmd, "INSERT INTO Filename (Name) \
652 VALUES (\"%s\")", fname);
654 if (!INSERT_DB(mdb, mdb->cmd)) {
655 Mmsg2(&mdb->errmsg, _("Create db Filename record %s failed. ERR=%s\n"),
656 mdb->cmd, sql_strerror(mdb));
657 Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
661 ar->FilenameId = sql_insert_id(mdb);
669 #endif /* HAVE_MYSQL || HAVE_SQLITE */