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 StartDay = (btime_t)(date_encode(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday) -
79 date_encode(2000, 1, 1));
82 JobId = db_next_index(mdb, "Job");
90 "INSERT INTO Job (JobId, Job, Name, Type, Level, SchedTime, StartDay) VALUES \
91 (%s, \"%s\", \"%s\", \"%c\", \"%c\", \"%s\", %s)",
92 JobId, jr->Job, jr->Name, (char)(jr->Type), (char)(jr->Level), dt,
93 edit_uint64(StartDay, 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 VALUES (%d, %d, %u, %u)",
138 jm->JobId, jm->MediaId, jm->FirstIndex, jm->LastIndex);
141 if (!INSERT_DB(mdb, mdb->cmd)) {
142 Mmsg2(&mdb->errmsg, _("Create db JobMedia record %s failed. ERR=%s\n"), mdb->cmd,
149 Dmsg0(30, "Return from JobMedia\n");
155 /* Create Unique Pool record
156 * Returns: 0 on failure
160 db_create_pool_record(B_DB *mdb, POOL_DBR *pool_dbr)
165 Mmsg(&mdb->cmd, "SELECT PoolId,Name FROM Pool WHERE Name=\"%s\"", pool_dbr->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"), pool_dbr->Name);
174 sql_free_result(mdb);
178 sql_free_result(mdb);
183 "INSERT INTO Pool (Name, NumVols, MaxVols, UseOnce, UseCatalog, \
184 AcceptAnyVolume, PoolType, LabelFormat) \
185 VALUES (\"%s\", %d, %d, %d, %d, %d, \"%s\", \"%s\")",
187 pool_dbr->NumVols, pool_dbr->MaxVols,
188 pool_dbr->UseOnce, pool_dbr->UseCatalog,
189 pool_dbr->AcceptAnyVolume,
190 pool_dbr->PoolType, pool_dbr->LabelFormat);
192 if (!INSERT_DB(mdb, mdb->cmd)) {
193 Mmsg2(&mdb->errmsg, _("Create db Pool record %s failed: ERR=%s\n"),
194 mdb->cmd, sql_strerror(mdb));
195 pool_dbr->PoolId = 0;
198 pool_dbr->PoolId = sql_insert_id(mdb);
208 * Create Unique Media record
209 * Returns: 0 on failure
213 db_create_media_record(B_DB *mdb, MEDIA_DBR *mr)
216 char ed1[30], ed2[30];
219 Mmsg(&mdb->cmd, "SELECT MediaId FROM Media WHERE VolumeName=\"%s\"",
221 Dmsg1(110, "selectpool: %s\n", mdb->cmd);
223 if (QUERY_DB(mdb, mdb->cmd)) {
224 mdb->num_rows = sql_num_rows(mdb);
225 if (mdb->num_rows > 0) {
226 Mmsg1(&mdb->errmsg, _("Media record %s already exists\n"), mr->VolumeName);
227 sql_free_result(mdb);
231 sql_free_result(mdb);
236 "INSERT INTO Media (VolumeName, MediaType, PoolId, VolMaxBytes, VolCapacityBytes, \
237 VolStatus, Recycle) VALUES (\"%s\", \"%s\", %d, %s, %s, \"%s\", \"%s\")",
239 mr->MediaType, mr->PoolId,
240 edit_uint64(mr->VolMaxBytes,ed1),
241 edit_uint64(mr->VolCapacityBytes, ed2),
242 mr->VolStatus, mr->Recycle);
244 if (!INSERT_DB(mdb, mdb->cmd)) {
245 Mmsg2(&mdb->errmsg, _("Create DB Media record %s failed. ERR=%s\n"),
246 mdb->cmd, sql_strerror(mdb));
249 mr->MediaId = sql_insert_id(mdb);
259 * Create a Unique record for the client -- no duplicates
260 * Returns: 0 on failure
261 * 1 on success with id in cr->ClientId
263 int db_create_client_record(B_DB *mdb, CLIENT_DBR *cr)
269 Mmsg(&mdb->cmd, "SELECT ClientId FROM Client WHERE Name=\"%s\"", cr->Name);
272 if (QUERY_DB(mdb, mdb->cmd)) {
274 mdb->num_rows = sql_num_rows(mdb);
276 /* If more than one, report error, but return first row */
277 if (mdb->num_rows > 1) {
278 Mmsg1(&mdb->errmsg, _("More than one Client!: %d\n"), (int)(mdb->num_rows));
279 Emsg0(M_ERROR, 0, mdb->errmsg);
281 if (mdb->num_rows >= 1) {
282 if ((row = sql_fetch_row(mdb)) == NULL) {
283 Mmsg1(&mdb->errmsg, _("error fetching Client row: %s\n"), sql_strerror(mdb));
284 Emsg0(M_ERROR, 0, mdb->errmsg);
285 sql_free_result(mdb);
289 sql_free_result(mdb);
290 cr->ClientId = atoi(row[0]);
294 sql_free_result(mdb);
298 Mmsg(&mdb->cmd, "INSERT INTO Client (Name, Uname) VALUES \
299 (\"%s\", \"%s\")", cr->Name, cr->Uname);
301 if (!INSERT_DB(mdb, mdb->cmd)) {
302 Mmsg2(&mdb->errmsg, _("Create DB Client record %s failed. ERR=%s\n"),
303 mdb->cmd, sql_strerror(mdb));
307 cr->ClientId = sql_insert_id(mdb);
316 * Create a FileSet record. This record is unique in the
317 * name and the MD5 signature of the include/exclude sets.
318 * Returns: 0 on failure
319 * 1 on success with FileSetId in record
321 int db_create_fileset_record(B_DB *mdb, FILESET_DBR *fsr)
327 Mmsg(&mdb->cmd, "SELECT FileSetId FROM FileSet WHERE \
328 FileSet=\"%s\" and MD5=\"%s\"", fsr->FileSet, fsr->MD5);
331 if (QUERY_DB(mdb, mdb->cmd)) {
333 mdb->num_rows = sql_num_rows(mdb);
335 if (mdb->num_rows > 1) {
336 Mmsg1(&mdb->errmsg, _("More than one FileSet!: %d\n"), (int)(mdb->num_rows));
337 Emsg0(M_ERROR, 0, mdb->errmsg);
339 if (mdb->num_rows >= 1) {
340 if ((row = sql_fetch_row(mdb)) == NULL) {
341 Mmsg1(&mdb->errmsg, _("error fetching FileSet row: ERR=%s\n"), sql_strerror(mdb));
342 Emsg0(M_ERROR, 0, mdb->errmsg);
343 sql_free_result(mdb);
347 sql_free_result(mdb);
348 fsr->FileSetId = atoi(row[0]);
352 sql_free_result(mdb);
356 Mmsg(&mdb->cmd, "INSERT INTO FileSet (FileSet, MD5) VALUES \
357 (\"%s\", \"%s\")", fsr->FileSet, fsr->MD5);
359 if (!INSERT_DB(mdb, mdb->cmd)) {
360 Mmsg2(&mdb->errmsg, _("Create DB FileSet record %s failed. ERR=%s\n"),
361 mdb->cmd, sql_strerror(mdb));
365 fsr->FileSetId = sql_insert_id(mdb);
377 * dev_t st_dev; * device *
378 * ino_t st_ino; * inode *
379 * mode_t st_mode; * protection *
380 * nlink_t st_nlink; * number of hard links *
381 * uid_t st_uid; * user ID of owner *
382 * gid_t st_gid; * group ID of owner *
383 * dev_t st_rdev; * device type (if inode device) *
384 * off_t st_size; * total size, in bytes *
385 * unsigned long st_blksize; * blocksize for filesystem I/O *
386 * unsigned long st_blocks; * number of blocks allocated *
387 * time_t st_atime; * time of last access *
388 * time_t st_mtime; * time of last modification *
389 * time_t st_ctime; * time of last inode change *
396 * Create File record in B_DB
398 * In order to reduce database size, we store the File attributes,
399 * the FileName, and the Path separately. In principle, there
400 * is a single FileName record and a single Path record, no matter
401 * how many times it occurs. This is this subroutine, we separate
402 * the file and the path and create three database records.
404 int db_create_file_attributes_record(B_DB *mdb, ATTR_DBR *ar)
408 /* ****FIXME***** malloc these */
409 char file[MAXSTRING];
410 char spath[MAXSTRING];
413 Dmsg1(100, "Fname=%s\n", ar->fname);
414 Dmsg0(50, "put_file_into_catalog\n");
415 /* For the moment, we only handle Unix attributes. Note, we are
416 * also getting any MD5 signature that was computed.
418 if (ar->Stream != STREAM_UNIX_ATTRIBUTES) {
419 Mmsg0(&mdb->errmsg, _("Attempt to put non-attributes into catalog\n"));
423 /* Find path without the filename.
424 * I.e. everything after the last / is a "filename".
425 * OK, maybe it is a directory name, but we treat it like
426 * a filename. If we don't find a / then the whole name
427 * must be a path name (e.g. c:).
429 for (p=l=ar->fname; *p; p++) {
431 l = p; /* set pos of last slash */
434 if (*l == '/') { /* did we find a slash? */
435 l++; /* yes, point to filename */
436 } else { /* no, whole thing must be path name */
440 /* If filename doesn't exist (i.e. root directory), we
441 * simply create a blank name consisting of a single
442 * space. This makes handling zero length filenames
447 Emsg1(M_WARNING, 0, _("Filename truncated to 255 chars: %s\n"), l);
451 strncpy(file, l, fnl); /* copy filename */
454 file[0] = ' '; /* blank filename */
460 Emsg1(M_WARNING, 0, _("Path name truncated to 255 chars: %s\n"), ar->fname);
463 strncpy(spath, ar->fname, pnl);
467 Mmsg1(&mdb->errmsg, _("Path length is zero. File=%s\n"), ar->fname);
468 Emsg0(M_ERROR, 0, mdb->errmsg);
473 Dmsg1(100, "spath=%s\n", spath);
474 Dmsg1(100, "file=%s\n", file);
476 db_escape_string(buf, file, fnl);
478 if (!db_create_filename_record(mdb, ar, buf)) {
481 Dmsg1(100, "db_create_filename_record: %s\n", buf);
483 db_escape_string(buf, spath, pnl);
485 if (!db_create_path_record(mdb, ar, buf)) {
488 Dmsg1(100, "db_create_path_record\n", buf);
490 if (!db_create_file_record(mdb, ar)) {
493 Dmsg0(50, "db_create_file_record\n");
495 Dmsg3(100, "Path=%s File=%s FilenameId=%d\n", spath, file, ar->FilenameId);
500 static int db_create_file_record(B_DB *mdb, ATTR_DBR *ar)
507 "INSERT INTO File (FileIndex, JobId, PathId, FilenameId, \
508 LStat, MD5) VALUES (%d, %d, %d, %d, \"%s\", \"0\")",
509 (int)ar->FileIndex, ar->JobId, ar->PathId, ar->FilenameId,
512 if (!INSERT_DB(mdb, mdb->cmd)) {
513 Mmsg2(&mdb->errmsg, _("Create db File record %s failed. ERR=%s"),
514 mdb->cmd, sql_strerror(mdb));
515 Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
519 ar->FileId = sql_insert_id(mdb);
526 /* Create a Unique record for the Path -- no duplicates */
527 static int db_create_path_record(B_DB *mdb, ATTR_DBR *ar, char *path)
530 static uint32_t cached_id = 0;
531 static char cached_path[MAXSTRING];
535 Mmsg0(&mdb->errmsg, _("Null path given to db_create_path_record\n"));
536 Emsg0(M_ERROR, 0, mdb->errmsg);
543 if (cached_id != 0 && strcmp(cached_path, path) == 0) {
544 ar->PathId = cached_id;
549 Mmsg(&mdb->cmd, "SELECT PathId FROM Path WHERE Path=\"%s\"", path);
551 if (QUERY_DB(mdb, mdb->cmd)) {
553 mdb->num_rows = sql_num_rows(mdb);
555 if (mdb->num_rows > 1) {
557 Mmsg2(&mdb->errmsg, _("More than one Path!: %s for Path=%s\n"),
558 edit_uint64(mdb->num_rows, ed1), path);
559 Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
560 Emsg1(M_ERROR, 0, "%s\n", mdb->cmd);
562 if (mdb->num_rows >= 1) {
563 if ((row = sql_fetch_row(mdb)) == NULL) {
565 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
566 Emsg0(M_ERROR, 0, mdb->errmsg);
567 sql_free_result(mdb);
571 sql_free_result(mdb);
572 ar->PathId = atoi(row[0]);
573 if (ar->PathId != cached_id) {
574 cached_id = ar->PathId;
575 strncpy(cached_path, path, sizeof(cached_path));
576 cached_path[sizeof(cached_path)-1] = 0;
582 sql_free_result(mdb);
585 Mmsg(&mdb->cmd, "INSERT INTO Path (Path) VALUES (\"%s\")", path);
587 if (!INSERT_DB(mdb, mdb->cmd)) {
588 Mmsg2(&mdb->errmsg, _("Create db Path record %s failed. ERR=%s\n"),
589 mdb->cmd, sql_strerror(mdb));
590 Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
594 ar->PathId = sql_insert_id(mdb);
598 if (ar->PathId != cached_id) {
599 cached_id = ar->PathId;
600 strncpy(cached_path, path, sizeof(cached_path));
601 cached_path[sizeof(cached_path)-1] = 0;
607 /* Create a Unique record for the filename -- no duplicates */
608 static int db_create_filename_record(B_DB *mdb, ATTR_DBR *ar, char *fname)
614 Mmsg(&mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name=\"%s\"", fname);
616 if (QUERY_DB(mdb, mdb->cmd)) {
617 mdb->num_rows = sql_num_rows(mdb);
618 if (mdb->num_rows > 1) {
619 Mmsg2(&mdb->errmsg, _("More than one Filename!: %d File=%s\n"),
620 (int)(mdb->num_rows), fname);
621 Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
622 Emsg1(M_ERROR, 0, "%s\n", mdb->cmd);
624 if (mdb->num_rows >= 1) {
625 if ((row = sql_fetch_row(mdb)) == NULL) {
626 Mmsg2(&mdb->errmsg, _("error fetching row for file=%s: ERR=%s\n"),
627 fname, sql_strerror(mdb));
628 sql_free_result(mdb);
630 Emsg0(M_ERROR, 0, mdb->errmsg);
634 sql_free_result(mdb);
635 ar->FilenameId = atoi(row[0]);
639 sql_free_result(mdb);
642 Mmsg(&mdb->cmd, "INSERT INTO Filename (Name) \
643 VALUES (\"%s\")", fname);
645 if (!INSERT_DB(mdb, mdb->cmd)) {
646 Mmsg2(&mdb->errmsg, _("Create db Filename record %s failed. ERR=%s\n"),
647 mdb->cmd, sql_strerror(mdb));
648 Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
652 ar->FilenameId = sql_insert_id(mdb);
660 #endif /* HAVE_MYSQL || HAVE_SQLITE */