]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_create.c
kes15Jun02
[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, 2001, 2002 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 /* *****FIXME**** fix fixed length of select_cmd[] and insert_cmd[] */
30
31 /* The following is necessary so that we do not include
32  * the dummy external definition of DB.
33  */
34 #define __SQL_C                       /* indicate that this is sql.c */
35
36 #include "bacula.h"
37 #include "cats.h"
38
39 #if    HAVE_MYSQL || HAVE_SQLITE
40
41 /* -----------------------------------------------------------------------
42  *
43  *   Generic Routines (or almost generic)
44  *
45  * -----------------------------------------------------------------------
46  */
47
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);
52
53
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);
59
60
61 /* Create a new record for the Job
62  * Returns: 0 on failure
63  *          1 on success
64  */
65 int
66 db_create_job_record(B_DB *mdb, JOB_DBR *jr)
67 {
68    char dt[MAX_TIME_LENGTH];
69    time_t stime;
70    struct tm tm;
71    int stat;
72    char *JobId;
73    btime_t JobTDate;
74    char ed1[30];
75
76    stime = jr->SchedTime;
77
78    localtime_r(&stime, &tm); 
79    strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
80    JobTDate = (btime_t)stime;
81
82    db_lock(mdb);
83    JobId = db_next_index(mdb, "Job");
84    if (!JobId) {
85       jr->JobId = 0;
86       db_unlock(mdb);
87       return 0;
88    }
89    /* Must create it */
90    Mmsg(&mdb->cmd,
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));
95
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));
99       jr->JobId = 0;
100       stat = 0;
101    } else {
102       jr->JobId = sql_insert_id(mdb);
103       stat = 1;
104    }
105    db_unlock(mdb);
106    return stat;
107 }
108
109 /* Create a JobMedia record for medium used this job   
110  * Returns: 0 on failure
111  *          1 on success
112  */
113 int
114 db_create_jobmedia_record(B_DB *mdb, JOBMEDIA_DBR *jm)
115 {
116    int stat;
117
118    db_lock(mdb);
119    Mmsg(&mdb->cmd, "SELECT JobId, MediaId FROM JobMedia WHERE \
120 JobId=%d AND MediaId=%d", jm->JobId, jm->MediaId);
121
122    Dmsg0(30, mdb->cmd);
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);
128          db_unlock(mdb);
129          Dmsg0(0, "Already have JobMedia record\n");
130          return 0;
131       }
132       sql_free_result(mdb);
133    }
134
135    /* Must create it */
136    Mmsg(&mdb->cmd, 
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);
142
143    Dmsg0(30, mdb->cmd);
144    if (!INSERT_DB(mdb, mdb->cmd)) {
145       Mmsg2(&mdb->errmsg, _("Create db JobMedia record %s failed. ERR=%s\n"), mdb->cmd, 
146          sql_strerror(mdb));
147       stat = 0;
148    } else {
149       stat = 1;
150    }
151    db_unlock(mdb);
152    Dmsg0(30, "Return from JobMedia\n");
153    return stat;
154 }
155
156
157
158 /* Create Unique Pool record
159  * Returns: 0 on failure
160  *          1 on success
161  */
162 int
163 db_create_pool_record(B_DB *mdb, POOL_DBR *pr)
164 {
165    int stat;
166    char ed1[30];
167
168    db_lock(mdb);
169    Mmsg(&mdb->cmd, "SELECT PoolId,Name FROM Pool WHERE Name=\"%s\"", pr->Name);
170    Dmsg1(20, "selectpool: %s\n", mdb->cmd);
171
172    if (QUERY_DB(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, PoolType, LabelFormat) \
189 VALUES (\"%s\", %d, %d, %d, %d, %d, %d, %d, %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                   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));
201       pr->PoolId = 0;
202       stat = 0;
203    } else {
204       pr->PoolId = sql_insert_id(mdb);
205       stat = 1;
206    }
207    db_unlock(mdb);
208    
209    return stat;
210 }
211
212
213 /* 
214  * Create Unique Media record   
215  * Returns: 0 on failure
216  *          1 on success
217  */ 
218 int
219 db_create_media_record(B_DB *mdb, MEDIA_DBR *mr)
220 {
221    int stat;
222    char ed1[30], ed2[30], ed3[30];
223
224    db_lock(mdb);
225    Mmsg(&mdb->cmd, "SELECT MediaId FROM Media WHERE VolumeName=\"%s\"", 
226            mr->VolumeName);
227    Dmsg1(110, "selectpool: %s\n", mdb->cmd);
228
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);
234          db_unlock(mdb);
235          return 0;
236       }
237       sql_free_result(mdb);
238    }
239
240    /* Must create it */
241    Mmsg(&mdb->cmd, 
242 "INSERT INTO Media (VolumeName, MediaType, PoolId, VolMaxBytes, VolCapacityBytes, \
243 Recycle, VolRetention, VolStatus) VALUES (\"%s\", \"%s\", %d, %s, %s, %d, %s, \"%s\")", 
244                   mr->VolumeName,
245                   mr->MediaType, mr->PoolId, 
246                   edit_uint64(mr->VolMaxBytes,ed1),
247                   edit_uint64(mr->VolCapacityBytes, ed2),
248                   mr->Recycle,
249                   edit_uint64(mr->VolRetention, ed3),
250                   mr->VolStatus);
251
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));
256       stat = 0;
257    } else {
258       mr->MediaId = sql_insert_id(mdb);
259       stat = 1;
260    }
261    db_unlock(mdb);
262    return stat;
263 }
264
265
266
267 /* 
268  * Create a Unique record for the client -- no duplicates 
269  * Returns: 0 on failure
270  *          1 on success with id in cr->ClientId
271  */
272 int db_create_client_record(B_DB *mdb, CLIENT_DBR *cr)
273 {
274    SQL_ROW row;
275    int stat;
276    char ed1[30], ed2[30];
277
278    db_lock(mdb);
279    Mmsg(&mdb->cmd, "SELECT ClientId FROM Client WHERE Name=\"%s\"", cr->Name);
280
281    cr->ClientId = 0;
282    if (QUERY_DB(mdb, mdb->cmd)) {
283
284       mdb->num_rows = sql_num_rows(mdb);
285       
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);
290       }
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);
295             db_unlock(mdb);
296             return 0;
297          }
298          cr->ClientId = atoi(row[0]);
299          sql_free_result(mdb);
300          db_unlock(mdb);
301          return 1;
302       }
303       sql_free_result(mdb);
304    }
305
306    /* Must create it */
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));
312
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));
316       cr->ClientId = 0;
317       stat = 0;
318    } else {
319       cr->ClientId = sql_insert_id(mdb);
320       stat = 1;
321    }
322    db_unlock(mdb);
323    return stat;
324 }
325
326
327 /* 
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
332  */
333 int db_create_fileset_record(B_DB *mdb, FILESET_DBR *fsr)
334 {
335    SQL_ROW row;
336    int stat;
337
338    db_lock(mdb);
339    Mmsg(&mdb->cmd, "SELECT FileSetId FROM FileSet WHERE \
340 FileSet=\"%s\" and MD5=\"%s\"", fsr->FileSet, fsr->MD5);
341
342    fsr->FileSetId = 0;
343    if (QUERY_DB(mdb, mdb->cmd)) {
344
345       mdb->num_rows = sql_num_rows(mdb);
346       
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);
350       }
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);
355             db_unlock(mdb);
356             return 0;
357          }
358          fsr->FileSetId = atoi(row[0]);
359          sql_free_result(mdb);
360          db_unlock(mdb);
361          return 1;
362       }
363       sql_free_result(mdb);
364    }
365
366    /* Must create it */
367    Mmsg(&mdb->cmd, "INSERT INTO FileSet (FileSet, MD5) VALUES \
368 (\"%s\", \"%s\")", fsr->FileSet, fsr->MD5);
369
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));
373       fsr->FileSetId = 0;
374       stat = 0;
375    } else {
376       fsr->FileSetId = sql_insert_id(mdb);
377       stat = 1;
378    }
379
380    db_unlock(mdb);
381    return stat;
382 }
383
384
385 /*
386  *  struct stat
387  *  {
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 *            
401  *  };
402  */
403
404
405
406 /* 
407  * Create File record in B_DB   
408  *
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.
414  */
415 int db_create_file_attributes_record(B_DB *mdb, ATTR_DBR *ar)
416 {
417    int fnl, pnl;
418    char *l, *p;
419    /* ****FIXME***** malloc these */
420    char file[MAXSTRING];
421    char spath[MAXSTRING];
422    char buf[MAXSTRING];
423
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.
428     */
429    if (ar->Stream != STREAM_UNIX_ATTRIBUTES) {
430       Mmsg0(&mdb->errmsg, _("Attempt to put non-attributes into catalog\n"));
431       return 0;
432    }
433
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:).
439     */
440    for (p=l=ar->fname; *p; p++) {
441       if (*p == '/') {
442          l = p;                       /* set pos of last slash */
443       }
444    }
445    if (*l == '/') {                   /* did we find a slash? */
446       l++;                            /* yes, point to filename */
447    } else {                           /* no, whole thing must be path name */
448       l = p;
449    }
450
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
454     * easier.
455     */
456    fnl = p - l;
457    if (fnl > 255) {
458       Emsg1(M_WARNING, 0, _("Filename truncated to 255 chars: %s\n"), l);
459       fnl = 255;
460    }
461    if (fnl > 0) {
462       strncpy(file, l, fnl);          /* copy filename */
463       file[fnl] = 0;
464    } else {
465       file[0] = ' ';                  /* blank filename */
466       file[1] = 0;
467       fnl = 1;
468    }
469
470    pnl = l - ar->fname;    
471    if (pnl > 255) {
472       Emsg1(M_WARNING, 0, _("Path name truncated to 255 chars: %s\n"), ar->fname);
473       pnl = 255;
474    }
475    strncpy(spath, ar->fname, pnl);
476    spath[pnl] = 0;
477
478    if (pnl == 0) {
479       Mmsg1(&mdb->errmsg, _("Path length is zero. File=%s\n"), ar->fname);
480       Emsg0(M_ERROR, 0, mdb->errmsg);
481       spath[0] = ' ';
482       spath[1] = 0;
483       pnl = 1;
484    }
485
486    Dmsg1(100, "spath=%s\n", spath);
487    Dmsg1(100, "file=%s\n", file);
488
489    db_escape_string(buf, file, fnl);
490
491    if (!db_create_filename_record(mdb, ar, buf)) {
492       return 0;
493    }
494    Dmsg1(100, "db_create_filename_record: %s\n", buf);
495
496    db_escape_string(buf, spath, pnl);
497
498    if (!db_create_path_record(mdb, ar, buf)) {
499       return 0;
500    }
501    Dmsg1(100, "db_create_path_record\n", buf);
502
503    if (!db_create_file_record(mdb, ar)) {
504       return 0;
505    }
506    Dmsg0(50, "db_create_file_record\n");
507
508    Dmsg3(100, "Path=%s File=%s FilenameId=%d\n", spath, file, ar->FilenameId);
509
510    return 1;
511 }
512
513 static int db_create_file_record(B_DB *mdb, ATTR_DBR *ar)
514 {
515    int stat;
516
517    ASSERT(ar->JobId);
518    ASSERT(ar->PathId);
519    ASSERT(ar->FilenameId);
520
521    db_lock(mdb);
522    /* Must create it */
523    Mmsg(&mdb->cmd,
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, 
527       ar->attr);
528
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));
532       ar->FileId = 0;
533       stat = 0;
534    } else {
535       ar->FileId = sql_insert_id(mdb);
536       stat = 1;
537    }
538    db_unlock(mdb);
539    return stat;
540 }
541
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)
544 {
545    SQL_ROW row;
546    int stat;
547
548    if (*path == 0) {
549       Mmsg0(&mdb->errmsg, _("Null path given to db_create_path_record\n"));
550       ar->PathId = 0;
551       ASSERT(ar->PathId);
552       return 0;
553    }
554
555    db_lock(mdb);
556
557    if (mdb->cached_path_id != 0 && strcmp(mdb->cached_path, path) == 0) {
558       ar->PathId = mdb->cached_path_id;
559       ASSERT(ar->PathId);
560       db_unlock(mdb);
561       return 1;
562    }          
563
564    Mmsg(&mdb->cmd, "SELECT PathId FROM Path WHERE Path=\"%s\"", path);
565
566    if (QUERY_DB(mdb, mdb->cmd)) {
567
568       mdb->num_rows = sql_num_rows(mdb);
569
570       if (mdb->num_rows > 1) {
571          char ed1[30];
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);
576       }
577       if (mdb->num_rows >= 1) {
578          if ((row = sql_fetch_row(mdb)) == NULL) {
579             db_unlock(mdb);
580             Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
581             sql_free_result(mdb);
582             ar->PathId = 0;
583             ASSERT(ar->PathId);
584             return 0;
585          }
586          ar->PathId = atoi(row[0]);
587          sql_free_result(mdb);
588          /* Cache path */
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,
592                strlen(path)+1);
593             strcpy(mdb->cached_path, path);
594          }
595          ASSERT(ar->PathId);
596          db_unlock(mdb);
597          return 1;
598       }
599
600       sql_free_result(mdb);
601    }
602
603    Mmsg(&mdb->cmd, "INSERT INTO Path (Path)  VALUES (\"%s\")", path);
604
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));
608       ar->PathId = 0;
609       stat = 0;
610    } else {
611       ar->PathId = sql_insert_id(mdb);
612       stat = 1;
613    }
614
615    /* Cache path */
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,
619          strlen(path)+1);
620       strcpy(mdb->cached_path, path);
621    }
622    ASSERT(ar->PathId);
623    db_unlock(mdb);
624    return stat;
625 }
626
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) 
629 {
630    SQL_ROW row;
631
632    db_lock(mdb);
633    Mmsg(&mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name=\"%s\"", fname);
634
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);
642       }
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));
647             ar->FilenameId = 0;
648          } else {
649             ar->FilenameId = atoi(row[0]);
650          }
651          sql_free_result(mdb);
652          db_unlock(mdb);
653          return ar->FilenameId > 0;
654       }
655       sql_free_result(mdb);
656    }
657
658    Mmsg(&mdb->cmd, "INSERT INTO Filename (Name) \
659 VALUES (\"%s\")", fname);
660
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));
664       ar->FilenameId = 0;
665    } else {
666       ar->FilenameId = sql_insert_id(mdb);
667    }
668
669    db_unlock(mdb);
670    return ar->FilenameId > 0;
671 }
672
673 #endif /* HAVE_MYSQL || HAVE_SQLITE */