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