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 StartFile,EndFile,StartBlock,EndBlock) \
139 VALUES (%u,%u,%u,%u,%u,%u,%u,%u)",
140 jm->JobId, jm->MediaId, jm->FirstIndex, jm->LastIndex,
141 jm->StartFile, jm->EndFile, jm->StartBlock, jm->EndBlock);
144 if (!INSERT_DB(mdb, mdb->cmd)) {
145 Mmsg2(&mdb->errmsg, _("Create db JobMedia record %s failed. ERR=%s\n"), mdb->cmd,
152 Dmsg0(30, "Return from JobMedia\n");
158 /* Create Unique Pool record
159 * Returns: 0 on failure
163 db_create_pool_record(B_DB *mdb, POOL_DBR *pr)
169 Mmsg(&mdb->cmd, "SELECT PoolId,Name FROM Pool WHERE Name=\"%s\"", pr->Name);
170 Dmsg1(20, "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, PoolType, LabelFormat) \
189 VALUES (\"%s\", %d, %d, %d, %d, %d, %d, %d, %s, \"%s\", \"%s\")",
191 pr->NumVols, pr->MaxVols,
192 pr->UseOnce, pr->UseCatalog,
194 pr->AutoPrune, pr->Recycle,
195 edit_uint64(pr->VolRetention, ed1),
196 pr->PoolType, pr->LabelFormat);
197 Dmsg1(500, "Create Pool: %s\n", mdb->cmd);
198 if (!INSERT_DB(mdb, mdb->cmd)) {
199 Mmsg2(&mdb->errmsg, _("Create db Pool record %s failed: ERR=%s\n"),
200 mdb->cmd, sql_strerror(mdb));
204 pr->PoolId = sql_insert_id(mdb);
214 * Create Unique Media record
215 * Returns: 0 on failure
219 db_create_media_record(B_DB *mdb, MEDIA_DBR *mr)
222 char ed1[30], ed2[30], ed3[30];
225 Mmsg(&mdb->cmd, "SELECT MediaId FROM Media WHERE VolumeName=\"%s\"",
227 Dmsg1(110, "selectpool: %s\n", mdb->cmd);
229 if (QUERY_DB(mdb, mdb->cmd)) {
230 mdb->num_rows = sql_num_rows(mdb);
231 if (mdb->num_rows > 0) {
232 Mmsg1(&mdb->errmsg, _("Media record %s already exists\n"), mr->VolumeName);
233 sql_free_result(mdb);
237 sql_free_result(mdb);
242 "INSERT INTO Media (VolumeName, MediaType, PoolId, VolMaxBytes, VolCapacityBytes, \
243 Recycle, VolRetention, VolStatus) VALUES (\"%s\", \"%s\", %d, %s, %s, %d, %s, \"%s\")",
245 mr->MediaType, mr->PoolId,
246 edit_uint64(mr->VolMaxBytes,ed1),
247 edit_uint64(mr->VolCapacityBytes, ed2),
249 edit_uint64(mr->VolRetention, ed3),
252 Dmsg1(500, "Create Volume: %s\n", mdb->cmd);
253 if (!INSERT_DB(mdb, mdb->cmd)) {
254 Mmsg2(&mdb->errmsg, _("Create DB Media record %s failed. ERR=%s\n"),
255 mdb->cmd, sql_strerror(mdb));
258 mr->MediaId = sql_insert_id(mdb);
268 * Create a Unique record for the client -- no duplicates
269 * Returns: 0 on failure
270 * 1 on success with id in cr->ClientId
272 int db_create_client_record(B_DB *mdb, CLIENT_DBR *cr)
276 char ed1[30], ed2[30];
279 Mmsg(&mdb->cmd, "SELECT ClientId FROM Client WHERE Name=\"%s\"", cr->Name);
282 if (QUERY_DB(mdb, mdb->cmd)) {
284 mdb->num_rows = sql_num_rows(mdb);
286 /* If more than one, report error, but return first row */
287 if (mdb->num_rows > 1) {
288 Mmsg1(&mdb->errmsg, _("More than one Client!: %d\n"), (int)(mdb->num_rows));
289 Emsg0(M_ERROR, 0, mdb->errmsg);
291 if (mdb->num_rows >= 1) {
292 if ((row = sql_fetch_row(mdb)) == NULL) {
293 Mmsg1(&mdb->errmsg, _("error fetching Client row: %s\n"), sql_strerror(mdb));
294 sql_free_result(mdb);
298 cr->ClientId = atoi(row[0]);
299 sql_free_result(mdb);
303 sql_free_result(mdb);
307 Mmsg(&mdb->cmd, "INSERT INTO Client (Name, Uname, AutoPrune, \
308 FileRetention, JobRetention) VALUES \
309 (\"%s\", \"%s\", %d, %s, %s)", cr->Name, cr->Uname, cr->AutoPrune,
310 edit_uint64(cr->FileRetention, ed1),
311 edit_uint64(cr->JobRetention, ed2));
313 if (!INSERT_DB(mdb, mdb->cmd)) {
314 Mmsg2(&mdb->errmsg, _("Create DB Client record %s failed. ERR=%s\n"),
315 mdb->cmd, sql_strerror(mdb));
319 cr->ClientId = sql_insert_id(mdb);
328 * Create a FileSet record. This record is unique in the
329 * name and the MD5 signature of the include/exclude sets.
330 * Returns: 0 on failure
331 * 1 on success with FileSetId in record
333 int db_create_fileset_record(B_DB *mdb, FILESET_DBR *fsr)
339 Mmsg(&mdb->cmd, "SELECT FileSetId FROM FileSet WHERE \
340 FileSet=\"%s\" and MD5=\"%s\"", fsr->FileSet, fsr->MD5);
343 if (QUERY_DB(mdb, mdb->cmd)) {
345 mdb->num_rows = sql_num_rows(mdb);
347 if (mdb->num_rows > 1) {
348 Mmsg1(&mdb->errmsg, _("More than one FileSet!: %d\n"), (int)(mdb->num_rows));
349 Emsg0(M_ERROR, 0, mdb->errmsg);
351 if (mdb->num_rows >= 1) {
352 if ((row = sql_fetch_row(mdb)) == NULL) {
353 Mmsg1(&mdb->errmsg, _("error fetching FileSet row: ERR=%s\n"), sql_strerror(mdb));
354 sql_free_result(mdb);
358 fsr->FileSetId = atoi(row[0]);
359 sql_free_result(mdb);
363 sql_free_result(mdb);
367 Mmsg(&mdb->cmd, "INSERT INTO FileSet (FileSet, MD5) VALUES \
368 (\"%s\", \"%s\")", fsr->FileSet, fsr->MD5);
370 if (!INSERT_DB(mdb, mdb->cmd)) {
371 Mmsg2(&mdb->errmsg, _("Create DB FileSet record %s failed. ERR=%s\n"),
372 mdb->cmd, sql_strerror(mdb));
376 fsr->FileSetId = sql_insert_id(mdb);
388 * dev_t st_dev; * device *
389 * ino_t st_ino; * inode *
390 * mode_t st_mode; * protection *
391 * nlink_t st_nlink; * number of hard links *
392 * uid_t st_uid; * user ID of owner *
393 * gid_t st_gid; * group ID of owner *
394 * dev_t st_rdev; * device type (if inode device) *
395 * off_t st_size; * total size, in bytes *
396 * unsigned long st_blksize; * blocksize for filesystem I/O *
397 * unsigned long st_blocks; * number of blocks allocated *
398 * time_t st_atime; * time of last access *
399 * time_t st_mtime; * time of last modification *
400 * time_t st_ctime; * time of last inode change *
407 * Create File record in B_DB
409 * In order to reduce database size, we store the File attributes,
410 * the FileName, and the Path separately. In principle, there
411 * is a single FileName record and a single Path record, no matter
412 * how many times it occurs. This is this subroutine, we separate
413 * the file and the path and create three database records.
415 int db_create_file_attributes_record(B_DB *mdb, ATTR_DBR *ar)
419 /* ****FIXME***** malloc these */
420 char file[MAXSTRING];
421 char spath[MAXSTRING];
424 Dmsg1(100, "Fname=%s\n", ar->fname);
425 Dmsg0(50, "put_file_into_catalog\n");
426 /* For the moment, we only handle Unix attributes. Note, we are
427 * also getting any MD5 signature that was computed.
429 if (ar->Stream != STREAM_UNIX_ATTRIBUTES) {
430 Mmsg0(&mdb->errmsg, _("Attempt to put non-attributes into catalog\n"));
434 /* Find path without the filename.
435 * I.e. everything after the last / is a "filename".
436 * OK, maybe it is a directory name, but we treat it like
437 * a filename. If we don't find a / then the whole name
438 * must be a path name (e.g. c:).
440 for (p=l=ar->fname; *p; p++) {
442 l = p; /* set pos of last slash */
445 if (*l == '/') { /* did we find a slash? */
446 l++; /* yes, point to filename */
447 } else { /* no, whole thing must be path name */
451 /* If filename doesn't exist (i.e. root directory), we
452 * simply create a blank name consisting of a single
453 * space. This makes handling zero length filenames
458 Emsg1(M_WARNING, 0, _("Filename truncated to 255 chars: %s\n"), l);
462 strncpy(file, l, fnl); /* copy filename */
465 file[0] = ' '; /* blank filename */
472 Emsg1(M_WARNING, 0, _("Path name truncated to 255 chars: %s\n"), ar->fname);
475 strncpy(spath, ar->fname, pnl);
479 Mmsg1(&mdb->errmsg, _("Path length is zero. File=%s\n"), ar->fname);
480 Emsg0(M_ERROR, 0, mdb->errmsg);
486 Dmsg1(100, "spath=%s\n", spath);
487 Dmsg1(100, "file=%s\n", file);
489 db_escape_string(buf, file, fnl);
491 if (!db_create_filename_record(mdb, ar, buf)) {
494 Dmsg1(100, "db_create_filename_record: %s\n", buf);
496 db_escape_string(buf, spath, pnl);
498 if (!db_create_path_record(mdb, ar, buf)) {
501 Dmsg1(100, "db_create_path_record\n", buf);
503 if (!db_create_file_record(mdb, ar)) {
506 Dmsg0(50, "db_create_file_record\n");
508 Dmsg3(100, "Path=%s File=%s FilenameId=%d\n", spath, file, ar->FilenameId);
513 static int db_create_file_record(B_DB *mdb, ATTR_DBR *ar)
519 ASSERT(ar->FilenameId);
524 "INSERT INTO File (FileIndex, JobId, PathId, FilenameId, \
525 LStat, MD5) VALUES (%d, %d, %d, %d, \"%s\", \"0\")",
526 (int)ar->FileIndex, ar->JobId, ar->PathId, ar->FilenameId,
529 if (!INSERT_DB(mdb, mdb->cmd)) {
530 Mmsg2(&mdb->errmsg, _("Create db File record %s failed. ERR=%s"),
531 mdb->cmd, sql_strerror(mdb));
535 ar->FileId = sql_insert_id(mdb);
542 /* Create a Unique record for the Path -- no duplicates */
543 static int db_create_path_record(B_DB *mdb, ATTR_DBR *ar, char *path)
549 Mmsg0(&mdb->errmsg, _("Null path given to db_create_path_record\n"));
557 if (mdb->cached_path_id != 0 && strcmp(mdb->cached_path, path) == 0) {
558 ar->PathId = mdb->cached_path_id;
564 Mmsg(&mdb->cmd, "SELECT PathId FROM Path WHERE Path=\"%s\"", path);
566 if (QUERY_DB(mdb, mdb->cmd)) {
568 mdb->num_rows = sql_num_rows(mdb);
570 if (mdb->num_rows > 1) {
572 Mmsg2(&mdb->errmsg, _("More than one Path!: %s for Path=%s\n"),
573 edit_uint64(mdb->num_rows, ed1), path);
574 Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
575 Emsg1(M_ERROR, 0, "%s\n", mdb->cmd);
577 if (mdb->num_rows >= 1) {
578 if ((row = sql_fetch_row(mdb)) == NULL) {
580 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
581 sql_free_result(mdb);
586 ar->PathId = atoi(row[0]);
587 sql_free_result(mdb);
589 if (ar->PathId != mdb->cached_path_id) {
590 mdb->cached_path_id = ar->PathId;
591 mdb->cached_path = check_pool_memory_size(mdb->cached_path,
593 strcpy(mdb->cached_path, path);
600 sql_free_result(mdb);
603 Mmsg(&mdb->cmd, "INSERT INTO Path (Path) VALUES (\"%s\")", path);
605 if (!INSERT_DB(mdb, mdb->cmd)) {
606 Mmsg2(&mdb->errmsg, _("Create db Path record %s failed. ERR=%s\n"),
607 mdb->cmd, sql_strerror(mdb));
611 ar->PathId = sql_insert_id(mdb);
616 if (ar->PathId != mdb->cached_path_id) {
617 mdb->cached_path_id = ar->PathId;
618 mdb->cached_path = check_pool_memory_size(mdb->cached_path,
620 strcpy(mdb->cached_path, path);
627 /* Create a Unique record for the filename -- no duplicates */
628 static int db_create_filename_record(B_DB *mdb, ATTR_DBR *ar, char *fname)
633 Mmsg(&mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name=\"%s\"", fname);
635 if (QUERY_DB(mdb, mdb->cmd)) {
636 mdb->num_rows = sql_num_rows(mdb);
637 if (mdb->num_rows > 1) {
638 Mmsg2(&mdb->errmsg, _("More than one Filename!: %d File=%s\n"),
639 (int)(mdb->num_rows), fname);
640 Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
641 Emsg1(M_ERROR, 0, "%s\n", mdb->cmd);
643 if (mdb->num_rows >= 1) {
644 if ((row = sql_fetch_row(mdb)) == NULL) {
645 Mmsg2(&mdb->errmsg, _("error fetching row for file=%s: ERR=%s\n"),
646 fname, sql_strerror(mdb));
649 ar->FilenameId = atoi(row[0]);
651 sql_free_result(mdb);
653 return ar->FilenameId > 0;
655 sql_free_result(mdb);
658 Mmsg(&mdb->cmd, "INSERT INTO Filename (Name) \
659 VALUES (\"%s\")", fname);
661 if (!INSERT_DB(mdb, mdb->cmd)) {
662 Mmsg2(&mdb->errmsg, _("Create db Filename record %s failed. ERR=%s\n"),
663 mdb->cmd, sql_strerror(mdb));
666 ar->FilenameId = sql_insert_id(mdb);
670 return ar->FilenameId > 0;
673 #endif /* HAVE_MYSQL || HAVE_SQLITE */