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