2 * Bacula Catalog Database Create record interface routines
4 * Kern Sibbald, March 2000
10 Copyright (C) 2000-2005 Kern Sibbald
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 /* 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_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL
39 /* -----------------------------------------------------------------------
41 * Generic Routines (or almost generic)
43 * -----------------------------------------------------------------------
46 /* Forward referenced subroutines */
47 static int db_create_file_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
48 static int db_create_filename_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
49 static int db_create_path_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
52 /* Imported subroutines */
53 extern void print_dashes(B_DB *mdb);
54 extern void print_result(B_DB *mdb);
55 extern int QueryDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
56 extern int InsertDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
57 extern int UpdateDB(const char *file, int line, JCR *jcr, B_DB *db, char *update_cmd);
58 extern void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname);
61 /* Create a new record for the Job
62 * Returns: 0 on failure
66 db_create_job_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
68 char dt[MAX_TIME_LENGTH];
77 stime = jr->SchedTime;
80 localtime_r(&stime, &tm);
81 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
82 JobTDate = (utime_t)stime;
86 "INSERT INTO Job (Job,Name,Type,Level,JobStatus,SchedTime,JobTDate) VALUES "
87 "('%s','%s','%c','%c','%c','%s',%s)",
88 jr->Job, jr->Name, (char)(jr->JobType), (char)(jr->JobLevel),
89 (char)(jr->JobStatus), dt, edit_uint64(JobTDate, ed1));
91 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
92 Mmsg2(&mdb->errmsg, _("Create DB Job record %s failed. ERR=%s\n"),
93 mdb->cmd, sql_strerror(mdb));
97 jr->JobId = sql_insert_id(mdb, _("Job"));
104 /* Create a JobMedia record for medium used this job
105 * Returns: false on failure
109 db_create_jobmedia_record(JCR *jcr, B_DB *mdb, JOBMEDIA_DBR *jm)
116 /* Now get count for VolIndex */
117 Mmsg(mdb->cmd, "SELECT count(*) from JobMedia");
118 count = get_sql_record_max(jcr, mdb);
125 "INSERT INTO JobMedia (JobId,MediaId,FirstIndex,LastIndex,"
126 "StartFile,EndFile,StartBlock,EndBlock,VolIndex) "
127 "VALUES (%u,%u,%u,%u,%u,%u,%u,%u,%u)",
128 jm->JobId, jm->MediaId, jm->FirstIndex, jm->LastIndex,
129 jm->StartFile, jm->EndFile, jm->StartBlock, jm->EndBlock,count);
131 Dmsg0(300, mdb->cmd);
132 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
133 Mmsg2(&mdb->errmsg, _("Create JobMedia record %s failed: ERR=%s\n"), mdb->cmd,
137 /* Worked, now update the Media record with the EndFile and EndBlock */
139 "UPDATE Media SET EndFile=%u, EndBlock=%u WHERE MediaId=%u",
140 jm->EndFile, jm->EndBlock, jm->MediaId);
141 if (!UPDATE_DB(jcr, mdb, mdb->cmd)) {
142 Mmsg2(&mdb->errmsg, _("Update Media record %s failed: ERR=%s\n"), mdb->cmd,
148 Dmsg0(300, "Return from JobMedia\n");
154 /* Create Unique Pool record
155 * Returns: false on failure
159 db_create_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
162 char ed1[30], ed2[30], ed3[50];
164 Dmsg0(200, "In create pool\n");
166 Mmsg(mdb->cmd, "SELECT PoolId,Name FROM Pool WHERE Name='%s'", pr->Name);
167 Dmsg1(200, "selectpool: %s\n", mdb->cmd);
169 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
170 mdb->num_rows = sql_num_rows(mdb);
171 if (mdb->num_rows > 0) {
172 Mmsg1(&mdb->errmsg, _("pool record %s already exists\n"), pr->Name);
173 sql_free_result(mdb);
177 sql_free_result(mdb);
182 "INSERT INTO Pool (Name,NumVols,MaxVols,UseOnce,UseCatalog,"
183 "AcceptAnyVolume,AutoPrune,Recycle,VolRetention,VolUseDuration,"
184 "MaxVolJobs,MaxVolFiles,MaxVolBytes,PoolType,LabelType,LabelFormat) "
185 "VALUES ('%s',%u,%u,%d,%d,%d,%d,%d,%s,%s,%u,%u,%s,'%s',%d,'%s')",
187 pr->NumVols, pr->MaxVols,
188 pr->UseOnce, pr->UseCatalog,
190 pr->AutoPrune, pr->Recycle,
191 edit_uint64(pr->VolRetention, ed1),
192 edit_uint64(pr->VolUseDuration, ed2),
193 pr->MaxVolJobs, pr->MaxVolFiles,
194 edit_uint64(pr->MaxVolBytes, ed3),
195 pr->PoolType, pr->LabelType, pr->LabelFormat);
196 Dmsg1(200, "Create Pool: %s\n", mdb->cmd);
197 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
198 Mmsg2(&mdb->errmsg, _("Create db Pool record %s failed: ERR=%s\n"),
199 mdb->cmd, sql_strerror(mdb));
203 pr->PoolId = sql_insert_id(mdb, _("Pool"));
211 * Create Unique Device record
212 * Returns: false on failure
216 db_create_device_record(JCR *jcr, B_DB *mdb, DEVICE_DBR *dr)
219 char ed1[30], ed2[30];
221 Dmsg0(200, "In create Device\n");
223 Mmsg(mdb->cmd, "SELECT DeviceId,Name FROM Device WHERE Name='%s'", dr->Name);
224 Dmsg1(200, "selectdevice: %s\n", mdb->cmd);
226 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
227 mdb->num_rows = sql_num_rows(mdb);
228 if (mdb->num_rows > 0) {
229 Mmsg1(&mdb->errmsg, _("Device record %s already exists\n"), dr->Name);
230 sql_free_result(mdb);
234 sql_free_result(mdb);
239 "INSERT INTO Device (Name,MediaTypeId,StorageId) "
240 "VALUES ('%s',%s,%s)",
242 edit_uint64(dr->MediaTypeId, ed1),
243 edit_uint64(dr->StorageId, ed2));
244 Dmsg1(200, "Create Device: %s\n", mdb->cmd);
245 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
246 Mmsg2(&mdb->errmsg, _("Create db Device record %s failed: ERR=%s\n"),
247 mdb->cmd, sql_strerror(mdb));
251 dr->DeviceId = sql_insert_id(mdb, _("Device"));
259 * Create Unique storage record
260 * Returns: false on failure
264 db_create_storage_record(JCR *jcr, B_DB *mdb, STORAGE_DBR *sr)
268 Dmsg0(200, "In create storage\n");
270 Mmsg(mdb->cmd, "SELECT StorageId,Name FROM Storage WHERE Name='%s'", sr->Name);
271 Dmsg1(200, "selectstorage: %s\n", mdb->cmd);
273 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
274 mdb->num_rows = sql_num_rows(mdb);
275 if (mdb->num_rows > 0) {
276 Mmsg1(&mdb->errmsg, _("Storage record %s already exists\n"), sr->Name);
277 sql_free_result(mdb);
281 sql_free_result(mdb);
286 "INSERT INTO Storage (Name,AutoChanger) "
290 Dmsg1(200, "Create storage: %s\n", mdb->cmd);
291 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
292 Mmsg2(&mdb->errmsg, _("Create db storage record %s failed: ERR=%s\n"),
293 mdb->cmd, sql_strerror(mdb));
297 sr->StorageId = sql_insert_id(mdb, _("Storage"));
306 * Create Unique MediaType record
307 * Returns: false on failure
311 db_create_mediatype_record(JCR *jcr, B_DB *mdb, MEDIATYPE_DBR *mr)
315 Dmsg0(200, "In create mediatype\n");
317 Mmsg(mdb->cmd, "SELECT MediaTypeId,MediaType FROM MediaType WHERE MediaType='%s'", mr->MediaType);
318 Dmsg1(200, "selectmediatype: %s\n", mdb->cmd);
320 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
321 mdb->num_rows = sql_num_rows(mdb);
322 if (mdb->num_rows > 0) {
323 Mmsg1(&mdb->errmsg, _("mediatype record %s already exists\n"), mr->MediaType);
324 sql_free_result(mdb);
328 sql_free_result(mdb);
333 "INSERT INTO MediaType (MediaType,ReadOnly) "
337 Dmsg1(200, "Create mediatype: %s\n", mdb->cmd);
338 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
339 Mmsg2(&mdb->errmsg, _("Create db mediatype record %s failed: ERR=%s\n"),
340 mdb->cmd, sql_strerror(mdb));
344 mr->MediaTypeId = sql_insert_id(mdb, _("MediaType"));
355 * Create Media record. VolumeName and non-zero Slot must be unique
357 * Returns: 0 on failure
361 db_create_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
364 char ed1[30], ed2[30], ed3[30], ed4[30], ed5[30], ed6[50], ed7[50];
368 Mmsg(mdb->cmd, "SELECT MediaId FROM Media WHERE VolumeName='%s'",
370 Dmsg1(300, "selectpool: %s\n", mdb->cmd);
372 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
373 mdb->num_rows = sql_num_rows(mdb);
374 if (mdb->num_rows > 0) {
375 Mmsg1(&mdb->errmsg, _("Volume \"%s\" already exists.\n"), mr->VolumeName);
376 sql_free_result(mdb);
380 sql_free_result(mdb);
385 "INSERT INTO Media (VolumeName,MediaType,PoolId,MaxVolBytes,VolCapacityBytes,"
386 "Recycle,VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,"
387 "VolStatus,Slot,VolBytes,InChanger,VolReadTime,VolWriteTime,VolParts,"
388 "EndFile,EndBlock,LabelType) "
389 "VALUES ('%s','%s',%u,%s,%s,%d,%s,%s,%u,%u,'%s',%d,%s,%d,%s,%s,%d,0,0,%d)",
391 mr->MediaType, mr->PoolId,
392 edit_uint64(mr->MaxVolBytes,ed1),
393 edit_uint64(mr->VolCapacityBytes, ed2),
395 edit_uint64(mr->VolRetention, ed3),
396 edit_uint64(mr->VolUseDuration, ed4),
401 edit_uint64(mr->VolBytes, ed5),
403 edit_uint64(mr->VolReadTime, ed6),
404 edit_uint64(mr->VolWriteTime, ed7),
410 Dmsg1(500, "Create Volume: %s\n", mdb->cmd);
411 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
412 Mmsg2(&mdb->errmsg, _("Create DB Media record %s failed. ERR=%s\n"),
413 mdb->cmd, sql_strerror(mdb));
416 mr->MediaId = sql_insert_id(mdb, _("Media"));
419 char dt[MAX_TIME_LENGTH];
420 localtime_r(&mr->LabelDate, &tm);
421 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
422 Mmsg(mdb->cmd, "UPDATE Media SET LabelDate='%s' "
423 "WHERE MediaId=%d", dt, mr->MediaId);
424 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
429 * Make sure that if InChanger is non-zero any other identical slot
430 * has InChanger zero.
432 db_make_inchanger_unique(jcr, mdb, mr);
441 * Create a Unique record for the client -- no duplicates
442 * Returns: 0 on failure
443 * 1 on success with id in cr->ClientId
445 int db_create_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
449 char ed1[50], ed2[50];
452 Mmsg(mdb->cmd, "SELECT ClientId,Uname FROM Client WHERE Name='%s'", cr->Name);
455 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
456 mdb->num_rows = sql_num_rows(mdb);
457 /* If more than one, report error, but return first row */
458 if (mdb->num_rows > 1) {
459 Mmsg1(&mdb->errmsg, _("More than one Client!: %d\n"), (int)(mdb->num_rows));
460 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
462 if (mdb->num_rows >= 1) {
463 if ((row = sql_fetch_row(mdb)) == NULL) {
464 Mmsg1(&mdb->errmsg, _("error fetching Client row: %s\n"), sql_strerror(mdb));
465 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
466 sql_free_result(mdb);
470 cr->ClientId = str_to_int64(row[0]);
472 bstrncpy(cr->Uname, row[1], sizeof(cr->Uname));
474 cr->Uname[0] = 0; /* no name */
476 sql_free_result(mdb);
480 sql_free_result(mdb);
484 Mmsg(mdb->cmd, "INSERT INTO Client (Name,Uname,AutoPrune,"
485 "FileRetention,JobRetention) VALUES "
486 "('%s','%s',%d,%s,%s)", cr->Name, cr->Uname, cr->AutoPrune,
487 edit_uint64(cr->FileRetention, ed1),
488 edit_uint64(cr->JobRetention, ed2));
490 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
491 Mmsg2(&mdb->errmsg, _("Create DB Client record %s failed. ERR=%s\n"),
492 mdb->cmd, sql_strerror(mdb));
493 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
497 cr->ClientId = sql_insert_id(mdb, _("Client"));
506 * Create a Unique record for the counter -- no duplicates
507 * Returns: 0 on failure
508 * 1 on success with counter filled in
510 int db_create_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
516 memset(&mcr, 0, sizeof(mcr));
517 bstrncpy(mcr.Counter, cr->Counter, sizeof(mcr.Counter));
518 if (db_get_counter_record(jcr, mdb, &mcr)) {
519 memcpy(cr, &mcr, sizeof(COUNTER_DBR));
525 Mmsg(mdb->cmd, "INSERT INTO Counters (Counter,MinValue,MaxValue,CurrentValue,"
526 "WrapCounter) VALUES ('%s','%d','%d','%d','%s')",
527 cr->Counter, cr->MinValue, cr->MaxValue, cr->CurrentValue,
530 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
531 Mmsg2(&mdb->errmsg, _("Create DB Counters record %s failed. ERR=%s\n"),
532 mdb->cmd, sql_strerror(mdb));
533 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
544 * Create a FileSet record. This record is unique in the
545 * name and the MD5 signature of the include/exclude sets.
546 * Returns: 0 on failure
547 * 1 on success with FileSetId in record
549 bool db_create_fileset_record(JCR *jcr, B_DB *mdb, FILESET_DBR *fsr)
556 fsr->created = false;
557 Mmsg(mdb->cmd, "SELECT FileSetId,CreateTime FROM FileSet WHERE "
558 "FileSet='%s' AND MD5='%s'", fsr->FileSet, fsr->MD5);
561 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
562 mdb->num_rows = sql_num_rows(mdb);
563 if (mdb->num_rows > 1) {
564 Mmsg1(&mdb->errmsg, _("More than one FileSet!: %d\n"), (int)(mdb->num_rows));
565 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
567 if (mdb->num_rows >= 1) {
568 if ((row = sql_fetch_row(mdb)) == NULL) {
569 Mmsg1(&mdb->errmsg, _("error fetching FileSet row: ERR=%s\n"), sql_strerror(mdb));
570 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
571 sql_free_result(mdb);
575 fsr->FileSetId = str_to_int64(row[0]);
576 if (row[1] == NULL) {
577 fsr->cCreateTime[0] = 0;
579 bstrncpy(fsr->cCreateTime, row[1], sizeof(fsr->cCreateTime));
581 sql_free_result(mdb);
585 sql_free_result(mdb);
588 if (fsr->CreateTime == 0 && fsr->cCreateTime[0] == 0) {
589 fsr->CreateTime = time(NULL);
591 localtime_r(&fsr->CreateTime, &tm);
592 strftime(fsr->cCreateTime, sizeof(fsr->cCreateTime), "%Y-%m-%d %T", &tm);
595 Mmsg(mdb->cmd, "INSERT INTO FileSet (FileSet,MD5,CreateTime) "
596 "VALUES ('%s','%s','%s')", fsr->FileSet, fsr->MD5, fsr->cCreateTime);
598 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
599 Mmsg2(&mdb->errmsg, _("Create DB FileSet record %s failed. ERR=%s\n"),
600 mdb->cmd, sql_strerror(mdb));
601 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
605 fsr->FileSetId = sql_insert_id(mdb, _("FileSet"));
618 * dev_t st_dev; * device *
619 * ino_t st_ino; * inode *
620 * mode_t st_mode; * protection *
621 * nlink_t st_nlink; * number of hard links *
622 * uid_t st_uid; * user ID of owner *
623 * gid_t st_gid; * group ID of owner *
624 * dev_t st_rdev; * device type (if inode device) *
625 * off_t st_size; * total size, in bytes *
626 * unsigned long st_blksize; * blocksize for filesystem I/O *
627 * unsigned long st_blocks; * number of blocks allocated *
628 * time_t st_atime; * time of last access *
629 * time_t st_mtime; * time of last modification *
630 * time_t st_ctime; * time of last inode change *
637 * Create File record in B_DB
639 * In order to reduce database size, we store the File attributes,
640 * the FileName, and the Path separately. In principle, there
641 * is a single FileName record and a single Path record, no matter
642 * how many times it occurs. This is this subroutine, we separate
643 * the file and the path and create three database records.
645 int db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
649 Dmsg1(300, "Fname=%s\n", ar->fname);
650 Dmsg0(500, "put_file_into_catalog\n");
652 * Make sure we have an acceptable attributes record.
654 if (!(ar->Stream == STREAM_UNIX_ATTRIBUTES ||
655 ar->Stream == STREAM_UNIX_ATTRIBUTES_EX)) {
656 Mmsg0(&mdb->errmsg, _("Attempt to put non-attributes into catalog\n"));
657 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
662 split_path_and_file(jcr, mdb, ar->fname);
664 if (!db_create_filename_record(jcr, mdb, ar)) {
667 Dmsg1(500, "db_create_filename_record: %s\n", mdb->esc_name);
670 if (!db_create_path_record(jcr, mdb, ar)) {
673 Dmsg1(500, "db_create_path_record: %s\n", mdb->esc_name);
675 /* Now create master File record */
676 if (!db_create_file_record(jcr, mdb, ar)) {
679 Dmsg0(500, "db_create_file_record OK\n");
681 Dmsg3(300, "CreateAttributes Path=%s File=%s FilenameId=%d\n", mdb->path, mdb->fname, ar->FilenameId);
691 * This is the master File entry containing the attributes.
692 * The filename and path records have already been created.
694 static int db_create_file_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
700 ASSERT(ar->FilenameId);
704 "INSERT INTO File (FileIndex,JobId,PathId,FilenameId,"
705 "LStat,MD5) VALUES (%u,%u,%u,%u,'%s','0')",
706 ar->FileIndex, ar->JobId, ar->PathId, ar->FilenameId,
709 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
710 Mmsg2(&mdb->errmsg, _("Create db File record %s failed. ERR=%s"),
711 mdb->cmd, sql_strerror(mdb));
712 Jmsg(jcr, M_FATAL, 0, "%s", mdb->errmsg);
716 ar->FileId = sql_insert_id(mdb, _("File"));
722 /* Create a Unique record for the Path -- no duplicates */
723 static int db_create_path_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
728 mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->pnl+2);
729 db_escape_string(mdb->esc_name, mdb->path, mdb->pnl);
731 if (mdb->cached_path_id != 0 && mdb->cached_path_len == mdb->pnl &&
732 strcmp(mdb->cached_path, mdb->path) == 0) {
733 ar->PathId = mdb->cached_path_id;
737 Mmsg(mdb->cmd, "SELECT PathId FROM Path WHERE Path='%s'", mdb->esc_name);
739 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
740 mdb->num_rows = sql_num_rows(mdb);
741 if (mdb->num_rows > 1) {
743 Mmsg2(&mdb->errmsg, _("More than one Path!: %s for path: %s\n"),
744 edit_uint64(mdb->num_rows, ed1), mdb->path);
745 Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
747 /* Even if there are multiple paths, take the first one */
748 if (mdb->num_rows >= 1) {
749 if ((row = sql_fetch_row(mdb)) == NULL) {
750 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
751 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
752 sql_free_result(mdb);
757 ar->PathId = str_to_int64(row[0]);
758 sql_free_result(mdb);
760 if (ar->PathId != mdb->cached_path_id) {
761 mdb->cached_path_id = ar->PathId;
762 mdb->cached_path_len = mdb->pnl;
763 pm_strcpy(mdb->cached_path, mdb->path);
768 sql_free_result(mdb);
771 Mmsg(mdb->cmd, "INSERT INTO Path (Path) VALUES ('%s')", mdb->esc_name);
773 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
774 Mmsg2(&mdb->errmsg, _("Create db Path record %s failed. ERR=%s\n"),
775 mdb->cmd, sql_strerror(mdb));
776 Jmsg(jcr, M_FATAL, 0, "%s", mdb->errmsg);
780 ar->PathId = sql_insert_id(mdb, _("Path"));
785 if (stat && ar->PathId != mdb->cached_path_id) {
786 mdb->cached_path_id = ar->PathId;
787 mdb->cached_path_len = mdb->pnl;
788 pm_strcpy(mdb->cached_path, mdb->path);
793 /* Create a Unique record for the filename -- no duplicates */
794 static int db_create_filename_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
798 mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->fnl+2);
799 db_escape_string(mdb->esc_name, mdb->fname, mdb->fnl);
801 Mmsg(mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name='%s'", mdb->esc_name);
803 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
804 mdb->num_rows = sql_num_rows(mdb);
805 if (mdb->num_rows > 1) {
807 Mmsg2(&mdb->errmsg, _("More than one Filename! %s for file: %s\n"),
808 edit_uint64(mdb->num_rows, ed1), mdb->fname);
809 Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
811 if (mdb->num_rows >= 1) {
812 if ((row = sql_fetch_row(mdb)) == NULL) {
813 Mmsg2(&mdb->errmsg, _("Error fetching row for file=%s: ERR=%s\n"),
814 mdb->fname, sql_strerror(mdb));
815 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
818 ar->FilenameId = str_to_int64(row[0]);
820 sql_free_result(mdb);
821 return ar->FilenameId > 0;
823 sql_free_result(mdb);
826 Mmsg(mdb->cmd, "INSERT INTO Filename (Name) VALUES ('%s')", mdb->esc_name);
828 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
829 Mmsg2(&mdb->errmsg, _("Create db Filename record %s failed. ERR=%s\n"),
830 mdb->cmd, sql_strerror(mdb));
831 Jmsg(jcr, M_FATAL, 0, "%s", mdb->errmsg);
834 ar->FilenameId = sql_insert_id(mdb, _("Filename"));
836 return ar->FilenameId > 0;
839 #endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL */