]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_create.c
2d78d31c361a4bad52473c0a6c1238c35d96321f
[bacula/bacula] / bacula / src / cats / sql_create.c
1 /*
2  * Bacula Catalog Database Create record interface routines
3  * 
4  *    Kern Sibbald, March 2000
5  *
6  *    Version $Id$
7  */
8
9 /*
10    Copyright (C) 2000-2003 Kern Sibbald and John Walker
11
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.
16
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.
21
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,
25    MA 02111-1307, USA.
26
27  */
28
29 /* The following is necessary so that we do not include
30  * the dummy external definition of DB.
31  */
32 #define __SQL_C                       /* indicate that this is sql.c */
33
34 #include "bacula.h"
35 #include "cats.h"
36
37 #if    HAVE_MYSQL || HAVE_SQLITE
38
39 /* -----------------------------------------------------------------------
40  *
41  *   Generic Routines (or almost generic)
42  *
43  * -----------------------------------------------------------------------
44  */
45
46 /* Forward referenced subroutines */
47 static int db_create_file_record(void *jcr, B_DB *mdb, ATTR_DBR *ar);
48 static int db_create_filename_record(void *jcr, B_DB *mdb, ATTR_DBR *ar);
49 static int db_create_path_record(void *jcr, B_DB *mdb, ATTR_DBR *ar);
50
51
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, void *jcr, B_DB *db, char *select_cmd);
56 extern int InsertDB(char *file, int line, void *jcr, B_DB *db, char *select_cmd);
57 extern void split_path_and_filename(void *jcr, B_DB *mdb, char *fname);
58
59
60 /* Create a new record for the Job
61  * Returns: 0 on failure
62  *          1 on success
63  */
64 int
65 db_create_job_record(void *jcr, B_DB *mdb, JOB_DBR *jr)
66 {
67    char dt[MAX_TIME_LENGTH];
68    time_t stime;
69    struct tm tm;
70    int stat;
71    char JobId[30];
72    utime_t JobTDate;
73    char ed1[30];
74
75    stime = jr->SchedTime;
76    ASSERT(stime != 0);
77
78    localtime_r(&stime, &tm); 
79    strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
80    JobTDate = (utime_t)stime;
81
82    db_lock(mdb);
83    if (!db_next_index(jcr, mdb, "Job", JobId)) {
84       jr->JobId = 0;
85       db_unlock(mdb);
86       return 0;
87    }
88    /* Must create it */
89    Mmsg(&mdb->cmd,
90 "INSERT INTO Job (JobId,Job,Name,Type,Level,JobStatus,SchedTime,JobTDate) VALUES \
91 (%s,'%s','%s','%c','%c','%c','%s',%s)", 
92            JobId, jr->Job, jr->Name, (char)(jr->Type), (char)(jr->Level), 
93            (char)(jr->JobStatus), dt, edit_uint64(JobTDate, ed1));
94
95    if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
96       Mmsg2(&mdb->errmsg, _("Create DB Job record %s failed. ERR=%s\n"), 
97             mdb->cmd, sql_strerror(mdb));
98       jr->JobId = 0;
99       stat = 0;
100    } else {
101       jr->JobId = sql_insert_id(mdb);
102       stat = 1;
103    }
104    db_unlock(mdb);
105    return stat;
106 }
107
108 /* Create a JobMedia record for medium used this job   
109  * Returns: 0 on failure
110  *          1 on success
111  */
112 int
113 db_create_jobmedia_record(void *jcr, B_DB *mdb, JOBMEDIA_DBR *jm)
114 {
115    int stat;
116
117    db_lock(mdb);
118    Mmsg(&mdb->cmd, "SELECT JobId, MediaId FROM JobMedia WHERE \
119 JobId=%d AND MediaId=%d", jm->JobId, jm->MediaId);
120
121    Dmsg0(30, mdb->cmd);
122    if (QUERY_DB(jcr, 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);
127          db_unlock(mdb);
128          Dmsg0(0, "Already have JobMedia record\n");
129          return 0;
130       }
131       sql_free_result(mdb);
132    }
133
134    /* Must create it */
135    Mmsg(&mdb->cmd, 
136 "INSERT INTO JobMedia (JobId,MediaId,FirstIndex,LastIndex,\
137 StartFile,EndFile,StartBlock,EndBlock) \
138 VALUES (%u,%u,%u,%u,%u,%u,%u,%u)", 
139        jm->JobId, jm->MediaId, jm->FirstIndex, jm->LastIndex,
140        jm->StartFile, jm->EndFile, jm->StartBlock, jm->EndBlock);
141
142    Dmsg0(30, mdb->cmd);
143    if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
144       Mmsg2(&mdb->errmsg, _("Create db JobMedia record %s failed. ERR=%s\n"), mdb->cmd, 
145          sql_strerror(mdb));
146       stat = 0;
147    } else {
148       stat = 1;
149    }
150    db_unlock(mdb);
151    Dmsg0(30, "Return from JobMedia\n");
152    return stat;
153 }
154
155
156
157 /* Create Unique Pool record
158  * Returns: 0 on failure
159  *          1 on success
160  */
161 int
162 db_create_pool_record(void *jcr, B_DB *mdb, POOL_DBR *pr)
163 {
164    int stat;
165    char ed1[30], ed2[30], ed3[50];
166
167    Dmsg0(200, "In create pool\n");
168    db_lock(mdb);
169    Mmsg(&mdb->cmd, "SELECT PoolId,Name FROM Pool WHERE Name='%s'", pr->Name);
170    Dmsg1(200, "selectpool: %s\n", mdb->cmd);
171
172    if (QUERY_DB(jcr, mdb, mdb->cmd)) {
173
174       mdb->num_rows = sql_num_rows(mdb);
175    
176       if (mdb->num_rows > 0) {
177          Mmsg1(&mdb->errmsg, _("pool record %s already exists\n"), pr->Name);
178          sql_free_result(mdb);
179          db_unlock(mdb);
180          return 0;
181       }
182       sql_free_result(mdb);
183    }
184
185    /* Must create it */
186    Mmsg(&mdb->cmd, 
187 "INSERT INTO Pool (Name,NumVols,MaxVols,UseOnce,UseCatalog,\
188 AcceptAnyVolume,AutoPrune,Recycle,VolRetention,VolUseDuration,\
189 MaxVolJobs,MaxVolFiles,MaxVolBytes,PoolType,LabelFormat) \
190 VALUES ('%s',%u,%u,%d,%d,%d,%d,%d,%s,%s,%u,%u,%s,'%s','%s')", 
191                   pr->Name,
192                   pr->NumVols, pr->MaxVols,
193                   pr->UseOnce, pr->UseCatalog,
194                   pr->AcceptAnyVolume,
195                   pr->AutoPrune, pr->Recycle,
196                   edit_uint64(pr->VolRetention, ed1),
197                   edit_uint64(pr->VolUseDuration, ed2),
198                   pr->MaxVolJobs, pr->MaxVolFiles,
199                   edit_uint64(pr->MaxVolBytes, ed3),
200                   pr->PoolType, pr->LabelFormat);
201    Dmsg1(200, "Create Pool: %s\n", mdb->cmd);
202    if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
203       Mmsg2(&mdb->errmsg, _("Create db Pool record %s failed: ERR=%s\n"), 
204             mdb->cmd, sql_strerror(mdb));
205       pr->PoolId = 0;
206       stat = 0;
207    } else {
208       pr->PoolId = sql_insert_id(mdb);
209       stat = 1;
210    }
211    db_unlock(mdb);
212    
213    return stat;
214 }
215
216
217 /* 
218  * Create Unique Media record   
219  * Returns: 0 on failure
220  *          1 on success
221  */ 
222 int
223 db_create_media_record(void *jcr, B_DB *mdb, MEDIA_DBR *mr)
224 {
225    int stat;
226    char ed1[30], ed2[30], ed3[30], ed4[30];
227    char dt[MAX_TIME_LENGTH];
228    struct tm tm;
229
230    db_lock(mdb);
231    Mmsg(&mdb->cmd, "SELECT MediaId FROM Media WHERE VolumeName='%s'", 
232            mr->VolumeName);
233    Dmsg1(110, "selectpool: %s\n", mdb->cmd);
234
235    if (QUERY_DB(jcr, mdb, mdb->cmd)) {
236       mdb->num_rows = sql_num_rows(mdb);
237       if (mdb->num_rows > 0) {
238          Mmsg1(&mdb->errmsg, _("Volume \"%s\" already exists\n."), mr->VolumeName);
239          sql_free_result(mdb);
240          db_unlock(mdb);
241          return 0;
242       }
243       sql_free_result(mdb);
244    }
245
246    /* Must create it */
247    if (mr->LabelDate) {
248       localtime_r(&mr->LabelDate, &tm); 
249       strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
250    } else {
251       strcpy(dt, "0000-00-00 00:00:00");
252    }
253    Mmsg(&mdb->cmd, 
254 "INSERT INTO Media (VolumeName,MediaType,PoolId,MaxVolBytes,VolCapacityBytes," 
255 "Recycle,VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,"
256 "VolStatus,LabelDate,Slot) "
257 "VALUES ('%s','%s',%u,%s,%s,%d,%s,%s,%u,%u,'%s','%s',%d)", 
258                   mr->VolumeName,
259                   mr->MediaType, mr->PoolId, 
260                   edit_uint64(mr->MaxVolBytes,ed1),
261                   edit_uint64(mr->VolCapacityBytes, ed2),
262                   mr->Recycle,
263                   edit_uint64(mr->VolRetention, ed3),
264                   edit_uint64(mr->VolUseDuration, ed4),
265                   mr->MaxVolJobs,
266                   mr->MaxVolFiles,
267                   mr->VolStatus, dt,
268                   mr->Slot);
269
270    Dmsg1(500, "Create Volume: %s\n", mdb->cmd);
271    if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
272       Mmsg2(&mdb->errmsg, _("Create DB Media record %s failed. ERR=%s\n"),
273             mdb->cmd, sql_strerror(mdb));
274       stat = 0;
275    } else {
276       mr->MediaId = sql_insert_id(mdb);
277       stat = 1;
278    }
279    db_unlock(mdb);
280    return stat;
281 }
282
283
284
285 /* 
286  * Create a Unique record for the client -- no duplicates 
287  * Returns: 0 on failure
288  *          1 on success with id in cr->ClientId
289  */
290 int db_create_client_record(void *jcr, B_DB *mdb, CLIENT_DBR *cr)
291 {
292    SQL_ROW row;
293    int stat;
294    char ed1[30], ed2[30];
295
296    db_lock(mdb);
297    Mmsg(&mdb->cmd, "SELECT ClientId,Uname FROM Client WHERE Name='%s'", cr->Name);
298
299    cr->ClientId = 0;
300    if (QUERY_DB(jcr, mdb, mdb->cmd)) {
301
302       mdb->num_rows = sql_num_rows(mdb);
303       
304       /* If more than one, report error, but return first row */
305       if (mdb->num_rows > 1) {
306          Mmsg1(&mdb->errmsg, _("More than one Client!: %d\n"), (int)(mdb->num_rows));
307          Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
308       }
309       if (mdb->num_rows >= 1) {
310          if ((row = sql_fetch_row(mdb)) == NULL) {
311             Mmsg1(&mdb->errmsg, _("error fetching Client row: %s\n"), sql_strerror(mdb));
312             Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
313             sql_free_result(mdb);
314             db_unlock(mdb);
315             return 0;
316          }
317          cr->ClientId = atoi(row[0]);
318          if (row[1]) {
319             bstrncpy(cr->Uname, row[1], sizeof(cr->Uname));
320          } else {
321             cr->Uname[0] = 0;         /* no name */
322          }
323          sql_free_result(mdb);
324          db_unlock(mdb);
325          return 1;
326       }
327       sql_free_result(mdb);
328    }
329
330    /* Must create it */
331    Mmsg(&mdb->cmd, "INSERT INTO Client (Name, Uname, AutoPrune, \
332 FileRetention, JobRetention) VALUES \
333 ('%s', '%s', %d, %s, %s)", cr->Name, cr->Uname, cr->AutoPrune,
334       edit_uint64(cr->FileRetention, ed1),
335       edit_uint64(cr->JobRetention, ed2));
336
337    if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
338       Mmsg2(&mdb->errmsg, _("Create DB Client record %s failed. ERR=%s\n"),
339             mdb->cmd, sql_strerror(mdb));
340       Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
341       cr->ClientId = 0;
342       stat = 0;
343    } else {
344       cr->ClientId = sql_insert_id(mdb);
345       stat = 1;
346    }
347    db_unlock(mdb);
348    return stat;
349 }
350
351
352 /* 
353  * Create a FileSet record. This record is unique in the
354  *  name and the MD5 signature of the include/exclude sets.
355  *  Returns: 0 on failure
356  *           1 on success with FileSetId in record
357  */
358 int db_create_fileset_record(void *jcr, B_DB *mdb, FILESET_DBR *fsr)
359 {
360    SQL_ROW row;
361    int stat;
362
363    db_lock(mdb);
364    Mmsg(&mdb->cmd, "SELECT FileSetId FROM FileSet WHERE \
365 FileSet='%s' and MD5='%s'", fsr->FileSet, fsr->MD5);
366
367    fsr->FileSetId = 0;
368    if (QUERY_DB(jcr, mdb, mdb->cmd)) {
369
370       mdb->num_rows = sql_num_rows(mdb);
371       
372       if (mdb->num_rows > 1) {
373          Mmsg1(&mdb->errmsg, _("More than one FileSet!: %d\n"), (int)(mdb->num_rows));
374          Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
375       }
376       if (mdb->num_rows >= 1) {
377          if ((row = sql_fetch_row(mdb)) == NULL) {
378             Mmsg1(&mdb->errmsg, _("error fetching FileSet row: ERR=%s\n"), sql_strerror(mdb));
379             Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
380             sql_free_result(mdb);
381             db_unlock(mdb);
382             return 0;
383          }
384          fsr->FileSetId = atoi(row[0]);
385          sql_free_result(mdb);
386          db_unlock(mdb);
387          return 1;
388       }
389       sql_free_result(mdb);
390    }
391
392    /* Must create it */
393    Mmsg(&mdb->cmd, "INSERT INTO FileSet (FileSet, MD5) VALUES \
394 ('%s', '%s')", fsr->FileSet, fsr->MD5);
395
396    if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
397       Mmsg2(&mdb->errmsg, _("Create DB FileSet record %s failed. ERR=%s\n"),
398             mdb->cmd, sql_strerror(mdb));
399       Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
400       fsr->FileSetId = 0;
401       stat = 0;
402    } else {
403       fsr->FileSetId = sql_insert_id(mdb);
404       stat = 1;
405    }
406
407    db_unlock(mdb);
408    return stat;
409 }
410
411
412 /*
413  *  struct stat
414  *  {
415  *      dev_t         st_dev;       * device *
416  *      ino_t         st_ino;       * inode *
417  *      mode_t        st_mode;      * protection *
418  *      nlink_t       st_nlink;     * number of hard links *
419  *      uid_t         st_uid;       * user ID of owner *
420  *      gid_t         st_gid;       * group ID of owner *
421  *      dev_t         st_rdev;      * device type (if inode device) *
422  *      off_t         st_size;      * total size, in bytes *
423  *      unsigned long st_blksize;   * blocksize for filesystem I/O *
424  *      unsigned long st_blocks;    * number of blocks allocated *
425  *      time_t        st_atime;     * time of last access *
426  *      time_t        st_mtime;     * time of last modification *
427  *      time_t        st_ctime;     * time of last inode change *            
428  *  };
429  */
430
431
432
433 /* 
434  * Create File record in B_DB   
435  *
436  *  In order to reduce database size, we store the File attributes,
437  *  the FileName, and the Path separately.  In principle, there   
438  *  is a single FileName record and a single Path record, no matter
439  *  how many times it occurs.  This is this subroutine, we separate
440  *  the file and the path and create three database records.
441  */
442 int db_create_file_attributes_record(void *jcr, B_DB *mdb, ATTR_DBR *ar)
443 {
444
445    Dmsg1(100, "Fname=%s\n", ar->fname);
446    Dmsg0(50, "put_file_into_catalog\n");
447    /*
448     * Make sure we have an acceptable attributes record.
449     */
450    if (!(ar->Stream == STREAM_UNIX_ATTRIBUTES || ar->Stream == STREAM_WIN32_ATTRIBUTES)) {
451       Mmsg0(&mdb->errmsg, _("Attempt to put non-attributes into catalog\n"));
452       Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
453       return 0;
454    }
455
456    db_lock(mdb);
457
458    split_path_and_filename(jcr, mdb, ar->fname);
459
460    if (!db_create_filename_record(jcr, mdb, ar)) {
461       db_unlock(mdb);
462       return 0;
463    }
464    Dmsg1(500, "db_create_filename_record: %s\n", mdb->esc_name);
465
466
467    if (!db_create_path_record(jcr, mdb, ar)) {
468       db_unlock(mdb);
469       return 0;
470    }
471    Dmsg1(500, "db_create_path_record: %s\n", mdb->esc_name);
472
473    /* Now create master File record */
474    if (!db_create_file_record(jcr, mdb, ar)) {
475       db_unlock(mdb);
476       return 0;
477    }
478    Dmsg0(500, "db_create_file_record OK\n");
479
480    Dmsg3(100, "CreateAttributes Path=%s File=%s FilenameId=%d\n", mdb->path, mdb->fname, ar->FilenameId);
481    db_unlock(mdb);
482    return 1;
483 }
484
485 /*
486  * This is the master File entry containing the attributes.
487  *  The filename and path records have already been created.
488  */
489 static int db_create_file_record(void *jcr, B_DB *mdb, ATTR_DBR *ar)
490 {
491    int stat;
492
493    ASSERT(ar->JobId);
494    ASSERT(ar->PathId);
495    ASSERT(ar->FilenameId);
496
497    /* Must create it */
498    Mmsg(&mdb->cmd,
499 "INSERT INTO File (FileIndex, JobId, PathId, FilenameId, \
500 LStat, MD5) VALUES (%u, %u, %u, %u, '%s', '0')", 
501       ar->FileIndex, ar->JobId, ar->PathId, ar->FilenameId, 
502       ar->attr);
503
504    if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
505       Mmsg2(&mdb->errmsg, _("Create db File record %s failed. ERR=%s"),       
506          mdb->cmd, sql_strerror(mdb));
507       Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
508       ar->FileId = 0;
509       stat = 0;
510    } else {
511       ar->FileId = sql_insert_id(mdb);
512       stat = 1;
513    }
514    return stat;
515 }
516
517 /* Create a Unique record for the Path -- no duplicates */
518 static int db_create_path_record(void *jcr, B_DB *mdb, ATTR_DBR *ar)
519 {
520    SQL_ROW row;
521    int stat;
522
523    mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->pnl+2);
524    db_escape_string(mdb->esc_name, mdb->path, mdb->pnl);
525
526    if (mdb->cached_path_id != 0 && mdb->cached_path_len == mdb->pnl &&
527        strcmp(mdb->cached_path, mdb->path) == 0) {
528       ar->PathId = mdb->cached_path_id;
529       return 1;
530    }          
531
532    Mmsg(&mdb->cmd, "SELECT PathId FROM Path WHERE Path='%s'", mdb->esc_name);
533
534    if (QUERY_DB(jcr, mdb, mdb->cmd)) {
535       mdb->num_rows = sql_num_rows(mdb);
536       if (mdb->num_rows > 1) {
537          char ed1[30];
538          Mmsg2(&mdb->errmsg, _("More than one Path!: %s for path: %s\n"), 
539             edit_uint64(mdb->num_rows, ed1), mdb->path);
540          Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
541       }
542       /* Even if there are multiple paths, take the first one */
543       if (mdb->num_rows >= 1) {
544          if ((row = sql_fetch_row(mdb)) == NULL) {
545             Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
546             Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
547             sql_free_result(mdb);
548             ar->PathId = 0;
549             ASSERT(ar->PathId);
550             return 0;
551          }
552          ar->PathId = atoi(row[0]);
553          sql_free_result(mdb);
554          /* Cache path */
555          if (ar->PathId != mdb->cached_path_id) {
556             mdb->cached_path_id = ar->PathId;
557             mdb->cached_path_len = mdb->pnl;
558             pm_strcpy(&mdb->cached_path, mdb->path);
559          }
560          ASSERT(ar->PathId);
561          return 1;
562       }
563
564       sql_free_result(mdb);
565    }
566
567    Mmsg(&mdb->cmd, "INSERT INTO Path (Path)  VALUES ('%s')", mdb->esc_name);
568
569    if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
570       Mmsg2(&mdb->errmsg, _("Create db Path record %s failed. ERR=%s\n"), 
571          mdb->cmd, sql_strerror(mdb));
572       Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
573       ar->PathId = 0;
574       stat = 0;
575    } else {
576       ar->PathId = sql_insert_id(mdb);
577       stat = 1;
578    }
579
580    /* Cache path */
581    if (stat && ar->PathId != mdb->cached_path_id) {
582       mdb->cached_path_id = ar->PathId;
583       mdb->cached_path_len = mdb->pnl;
584       pm_strcpy(&mdb->cached_path, mdb->path);
585    }
586    return stat;
587 }
588
589 /* Create a Unique record for the filename -- no duplicates */
590 static int db_create_filename_record(void *jcr, B_DB *mdb, ATTR_DBR *ar) 
591 {
592    SQL_ROW row;
593
594    mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->fnl+2);
595    db_escape_string(mdb->esc_name, mdb->fname, mdb->fnl);
596
597    Mmsg(&mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name='%s'", mdb->esc_name);
598
599    if (QUERY_DB(jcr, mdb, mdb->cmd)) {
600       mdb->num_rows = sql_num_rows(mdb);
601       if (mdb->num_rows > 1) {
602          char ed1[30];
603          Mmsg2(&mdb->errmsg, _("More than one Filename! %s for file: %s\n"), 
604             edit_uint64(mdb->num_rows, ed1), mdb->fname);
605          Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
606       }
607       if (mdb->num_rows >= 1) {
608          if ((row = sql_fetch_row(mdb)) == NULL) {
609             Mmsg2(&mdb->errmsg, _("Error fetching row for file=%s: ERR=%s\n"), 
610                 mdb->fname, sql_strerror(mdb));
611             Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
612             ar->FilenameId = 0;
613          } else {
614             ar->FilenameId = atoi(row[0]);
615          }
616          sql_free_result(mdb);
617          return ar->FilenameId > 0;
618       }
619       sql_free_result(mdb);
620    }
621
622    Mmsg(&mdb->cmd, "INSERT INTO Filename (Name) VALUES ('%s')", mdb->esc_name);
623
624    if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
625       Mmsg2(&mdb->errmsg, _("Create db Filename record %s failed. ERR=%s\n"), 
626             mdb->cmd, sql_strerror(mdb));
627       Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
628       ar->FilenameId = 0;
629    } else {
630       ar->FilenameId = sql_insert_id(mdb);
631    }
632    return ar->FilenameId > 0;
633 }
634
635 #endif /* HAVE_MYSQL || HAVE_SQLITE */