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 = (btime_t)stime;
83 JobId = db_next_index(mdb, "Job");
91 "INSERT INTO Job (JobId, Job, Name, Type, Level, SchedTime, JobTDate) VALUES \
92 (%s, \"%s\", \"%s\", \"%c\", \"%c\", \"%s\", %s)",
93 JobId, jr->Job, jr->Name, (char)(jr->Type), (char)(jr->Level), dt,
94 edit_uint64(JobTDate, ed1));
96 if (!INSERT_DB(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(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(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 VALUES (%d, %d, %u, %u)",
139 jm->JobId, jm->MediaId, jm->FirstIndex, jm->LastIndex);
142 if (!INSERT_DB(mdb, mdb->cmd)) {
143 Mmsg2(&mdb->errmsg, _("Create db JobMedia record %s failed. ERR=%s\n"), mdb->cmd,
150 Dmsg0(30, "Return from JobMedia\n");
156 /* Create Unique Pool record
157 * Returns: 0 on failure
161 db_create_pool_record(B_DB *mdb, POOL_DBR *pr)
167 Mmsg(&mdb->cmd, "SELECT PoolId,Name FROM Pool WHERE Name=\"%s\"", pr->Name);
168 Dmsg1(20, "selectpool: %s\n", mdb->cmd);
170 if (QUERY_DB(mdb, mdb->cmd)) {
172 mdb->num_rows = sql_num_rows(mdb);
174 if (mdb->num_rows > 0) {
175 Mmsg1(&mdb->errmsg, _("pool record %s already exists\n"), pr->Name);
176 sql_free_result(mdb);
180 sql_free_result(mdb);
185 "INSERT INTO Pool (Name, NumVols, MaxVols, UseOnce, UseCatalog, \
186 AcceptAnyVolume, AutoPrune, Recycle, VolRetention, PoolType, LabelFormat) \
187 VALUES (\"%s\", %d, %d, %d, %d, %d, %d, %d, %s, \"%s\", \"%s\")",
189 pr->NumVols, pr->MaxVols,
190 pr->UseOnce, pr->UseCatalog,
192 pr->AutoPrune, pr->Recycle,
193 edit_uint64(pr->VolRetention, ed1),
194 pr->PoolType, pr->LabelFormat);
195 Dmsg1(500, "Create Pool: %s\n", mdb->cmd);
196 if (!INSERT_DB(mdb, mdb->cmd)) {
197 Mmsg2(&mdb->errmsg, _("Create db Pool record %s failed: ERR=%s\n"),
198 mdb->cmd, sql_strerror(mdb));
202 pr->PoolId = sql_insert_id(mdb);
212 * Create Unique Media record
213 * Returns: 0 on failure
217 db_create_media_record(B_DB *mdb, MEDIA_DBR *mr)
220 char ed1[30], ed2[30], ed3[30];
223 Mmsg(&mdb->cmd, "SELECT MediaId FROM Media WHERE VolumeName=\"%s\"",
225 Dmsg1(110, "selectpool: %s\n", mdb->cmd);
227 if (QUERY_DB(mdb, mdb->cmd)) {
228 mdb->num_rows = sql_num_rows(mdb);
229 if (mdb->num_rows > 0) {
230 Mmsg1(&mdb->errmsg, _("Media record %s already exists\n"), mr->VolumeName);
231 sql_free_result(mdb);
235 sql_free_result(mdb);
240 "INSERT INTO Media (VolumeName, MediaType, PoolId, VolMaxBytes, VolCapacityBytes, \
241 Recycle, VolRetention, VolStatus) VALUES (\"%s\", \"%s\", %d, %s, %s, %d, %s, \"%s\")",
243 mr->MediaType, mr->PoolId,
244 edit_uint64(mr->VolMaxBytes,ed1),
245 edit_uint64(mr->VolCapacityBytes, ed2),
247 edit_uint64(mr->VolRetention, ed3),
250 Dmsg1(500, "Create Volume: %s\n", mdb->cmd);
251 if (!INSERT_DB(mdb, mdb->cmd)) {
252 Mmsg2(&mdb->errmsg, _("Create DB Media record %s failed. ERR=%s\n"),
253 mdb->cmd, sql_strerror(mdb));
256 mr->MediaId = sql_insert_id(mdb);
266 * Create a Unique record for the client -- no duplicates
267 * Returns: 0 on failure
268 * 1 on success with id in cr->ClientId
270 int db_create_client_record(B_DB *mdb, CLIENT_DBR *cr)
274 char ed1[30], ed2[30];
277 Mmsg(&mdb->cmd, "SELECT ClientId FROM Client WHERE Name=\"%s\"", cr->Name);
280 if (QUERY_DB(mdb, mdb->cmd)) {
282 mdb->num_rows = sql_num_rows(mdb);
284 /* If more than one, report error, but return first row */
285 if (mdb->num_rows > 1) {
286 Mmsg1(&mdb->errmsg, _("More than one Client!: %d\n"), (int)(mdb->num_rows));
287 Emsg0(M_ERROR, 0, mdb->errmsg);
289 if (mdb->num_rows >= 1) {
290 if ((row = sql_fetch_row(mdb)) == NULL) {
291 Mmsg1(&mdb->errmsg, _("error fetching Client row: %s\n"), sql_strerror(mdb));
292 sql_free_result(mdb);
296 cr->ClientId = atoi(row[0]);
297 sql_free_result(mdb);
301 sql_free_result(mdb);
305 Mmsg(&mdb->cmd, "INSERT INTO Client (Name, Uname, AutoPrune, \
306 FileRetention, JobRetention) VALUES \
307 (\"%s\", \"%s\", %d, %s, %s)", cr->Name, cr->Uname, cr->AutoPrune,
308 edit_uint64(cr->FileRetention, ed1),
309 edit_uint64(cr->JobRetention, ed2));
311 if (!INSERT_DB(mdb, mdb->cmd)) {
312 Mmsg2(&mdb->errmsg, _("Create DB Client record %s failed. ERR=%s\n"),
313 mdb->cmd, sql_strerror(mdb));
317 cr->ClientId = sql_insert_id(mdb);
326 * Create a FileSet record. This record is unique in the
327 * name and the MD5 signature of the include/exclude sets.
328 * Returns: 0 on failure
329 * 1 on success with FileSetId in record
331 int db_create_fileset_record(B_DB *mdb, FILESET_DBR *fsr)
337 Mmsg(&mdb->cmd, "SELECT FileSetId FROM FileSet WHERE \
338 FileSet=\"%s\" and MD5=\"%s\"", fsr->FileSet, fsr->MD5);
341 if (QUERY_DB(mdb, mdb->cmd)) {
343 mdb->num_rows = sql_num_rows(mdb);
345 if (mdb->num_rows > 1) {
346 Mmsg1(&mdb->errmsg, _("More than one FileSet!: %d\n"), (int)(mdb->num_rows));
347 Emsg0(M_ERROR, 0, mdb->errmsg);
349 if (mdb->num_rows >= 1) {
350 if ((row = sql_fetch_row(mdb)) == NULL) {
351 Mmsg1(&mdb->errmsg, _("error fetching FileSet row: ERR=%s\n"), sql_strerror(mdb));
352 sql_free_result(mdb);
356 fsr->FileSetId = atoi(row[0]);
357 sql_free_result(mdb);
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)
515 ASSERT(ar->FilenameId);
520 "INSERT INTO File (FileIndex, JobId, PathId, FilenameId, \
521 LStat, MD5) VALUES (%d, %d, %d, %d, \"%s\", \"0\")",
522 (int)ar->FileIndex, ar->JobId, ar->PathId, ar->FilenameId,
525 if (!INSERT_DB(mdb, mdb->cmd)) {
526 Mmsg2(&mdb->errmsg, _("Create db File record %s failed. ERR=%s"),
527 mdb->cmd, sql_strerror(mdb));
531 ar->FileId = sql_insert_id(mdb);
538 /* Create a Unique record for the Path -- no duplicates */
539 static int db_create_path_record(B_DB *mdb, ATTR_DBR *ar, char *path)
545 Mmsg0(&mdb->errmsg, _("Null path given to db_create_path_record\n"));
553 if (mdb->cached_path_id != 0 && strcmp(mdb->cached_path, path) == 0) {
554 ar->PathId = mdb->cached_path_id;
560 Mmsg(&mdb->cmd, "SELECT PathId FROM Path WHERE Path=\"%s\"", path);
562 if (QUERY_DB(mdb, mdb->cmd)) {
564 mdb->num_rows = sql_num_rows(mdb);
566 if (mdb->num_rows > 1) {
568 Mmsg2(&mdb->errmsg, _("More than one Path!: %s for Path=%s\n"),
569 edit_uint64(mdb->num_rows, ed1), path);
570 Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
571 Emsg1(M_ERROR, 0, "%s\n", mdb->cmd);
573 if (mdb->num_rows >= 1) {
574 if ((row = sql_fetch_row(mdb)) == NULL) {
576 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
577 sql_free_result(mdb);
582 ar->PathId = atoi(row[0]);
583 sql_free_result(mdb);
585 if (ar->PathId != mdb->cached_path_id) {
586 mdb->cached_path_id = ar->PathId;
587 mdb->cached_path = check_pool_memory_size(mdb->cached_path,
589 strcpy(mdb->cached_path, path);
596 sql_free_result(mdb);
599 Mmsg(&mdb->cmd, "INSERT INTO Path (Path) VALUES (\"%s\")", path);
601 if (!INSERT_DB(mdb, mdb->cmd)) {
602 Mmsg2(&mdb->errmsg, _("Create db Path record %s failed. ERR=%s\n"),
603 mdb->cmd, sql_strerror(mdb));
607 ar->PathId = sql_insert_id(mdb);
612 if (ar->PathId != mdb->cached_path_id) {
613 mdb->cached_path_id = ar->PathId;
614 mdb->cached_path = check_pool_memory_size(mdb->cached_path,
616 strcpy(mdb->cached_path, path);
623 /* Create a Unique record for the filename -- no duplicates */
624 static int db_create_filename_record(B_DB *mdb, ATTR_DBR *ar, char *fname)
629 Mmsg(&mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name=\"%s\"", fname);
631 if (QUERY_DB(mdb, mdb->cmd)) {
632 mdb->num_rows = sql_num_rows(mdb);
633 if (mdb->num_rows > 1) {
634 Mmsg2(&mdb->errmsg, _("More than one Filename!: %d File=%s\n"),
635 (int)(mdb->num_rows), fname);
636 Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
637 Emsg1(M_ERROR, 0, "%s\n", mdb->cmd);
639 if (mdb->num_rows >= 1) {
640 if ((row = sql_fetch_row(mdb)) == NULL) {
641 Mmsg2(&mdb->errmsg, _("error fetching row for file=%s: ERR=%s\n"),
642 fname, sql_strerror(mdb));
645 ar->FilenameId = atoi(row[0]);
647 sql_free_result(mdb);
649 return ar->FilenameId > 0;
651 sql_free_result(mdb);
654 Mmsg(&mdb->cmd, "INSERT INTO Filename (Name) \
655 VALUES (\"%s\")", fname);
657 if (!INSERT_DB(mdb, mdb->cmd)) {
658 Mmsg2(&mdb->errmsg, _("Create db Filename record %s failed. ERR=%s\n"),
659 mdb->cmd, sql_strerror(mdb));
662 ar->FilenameId = sql_insert_id(mdb);
666 return ar->FilenameId > 0;
669 #endif /* HAVE_MYSQL || HAVE_SQLITE */