]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_create.c
Add jcr to BSOCK and BDB SpoolAttr NoAttributs finish up multiple simultaneous jobs...
[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[30];
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    if (!db_next_index(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(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(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(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(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(B_DB *mdb, POOL_DBR *pr)
163 {
164    int stat;
165    char ed1[30];
166
167    db_lock(mdb);
168    Mmsg(&mdb->cmd, "SELECT PoolId,Name FROM Pool WHERE Name='%s'", pr->Name);
169    Dmsg1(20, "selectpool: %s\n", mdb->cmd);
170
171    if (QUERY_DB(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, PoolType, LabelFormat) \
188 VALUES ('%s', %d, %d, %d, %d, %d, %d, %d, %s, '%s', '%s')", 
189                   pr->Name,
190                   pr->NumVols, pr->MaxVols,
191                   pr->UseOnce, pr->UseCatalog,
192                   pr->AcceptAnyVolume,
193                   pr->AutoPrune, pr->Recycle,
194                   edit_uint64(pr->VolRetention, ed1),
195                   pr->PoolType, pr->LabelFormat);
196    Dmsg1(500, "Create Pool: %s\n", mdb->cmd);
197    if (!INSERT_DB(mdb, mdb->cmd)) {
198       Mmsg2(&mdb->errmsg, _("Create db Pool record %s failed: ERR=%s\n"), 
199             mdb->cmd, sql_strerror(mdb));
200       pr->PoolId = 0;
201       stat = 0;
202    } else {
203       pr->PoolId = sql_insert_id(mdb);
204       stat = 1;
205    }
206    db_unlock(mdb);
207    
208    return stat;
209 }
210
211
212 /* 
213  * Create Unique Media record   
214  * Returns: 0 on failure
215  *          1 on success
216  */ 
217 int
218 db_create_media_record(B_DB *mdb, MEDIA_DBR *mr)
219 {
220    int stat;
221    char ed1[30], ed2[30], ed3[30];
222
223    db_lock(mdb);
224    Mmsg(&mdb->cmd, "SELECT MediaId FROM Media WHERE VolumeName='%s'", 
225            mr->VolumeName);
226    Dmsg1(110, "selectpool: %s\n", mdb->cmd);
227
228    if (QUERY_DB(mdb, mdb->cmd)) {
229       mdb->num_rows = sql_num_rows(mdb);
230       if (mdb->num_rows > 0) {
231          Mmsg1(&mdb->errmsg, _("Media record %s already exists\n"), mr->VolumeName);
232          sql_free_result(mdb);
233          db_unlock(mdb);
234          return 0;
235       }
236       sql_free_result(mdb);
237    }
238
239    /* Must create it */
240    Mmsg(&mdb->cmd, 
241 "INSERT INTO Media (VolumeName, MediaType, PoolId, VolMaxBytes, VolCapacityBytes, \
242 Recycle, VolRetention, VolStatus) VALUES ('%s', '%s', %d, %s, %s, %d, %s, '%s')", 
243                   mr->VolumeName,
244                   mr->MediaType, mr->PoolId, 
245                   edit_uint64(mr->VolMaxBytes,ed1),
246                   edit_uint64(mr->VolCapacityBytes, ed2),
247                   mr->Recycle,
248                   edit_uint64(mr->VolRetention, ed3),
249                   mr->VolStatus);
250
251    Dmsg1(500, "Create Volume: %s\n", mdb->cmd);
252    if (!INSERT_DB(mdb, mdb->cmd)) {
253       Mmsg2(&mdb->errmsg, _("Create DB Media record %s failed. ERR=%s\n"),
254             mdb->cmd, sql_strerror(mdb));
255       stat = 0;
256    } else {
257       mr->MediaId = sql_insert_id(mdb);
258       stat = 1;
259    }
260    db_unlock(mdb);
261    return stat;
262 }
263
264
265
266 /* 
267  * Create a Unique record for the client -- no duplicates 
268  * Returns: 0 on failure
269  *          1 on success with id in cr->ClientId
270  */
271 int db_create_client_record(B_DB *mdb, CLIENT_DBR *cr)
272 {
273    SQL_ROW row;
274    int stat;
275    char ed1[30], ed2[30];
276
277    db_lock(mdb);
278    Mmsg(&mdb->cmd, "SELECT ClientId FROM Client WHERE Name='%s'", cr->Name);
279
280    cr->ClientId = 0;
281    if (QUERY_DB(mdb, mdb->cmd)) {
282
283       mdb->num_rows = sql_num_rows(mdb);
284       
285       /* If more than one, report error, but return first row */
286       if (mdb->num_rows > 1) {
287          Mmsg1(&mdb->errmsg, _("More than one Client!: %d\n"), (int)(mdb->num_rows));
288          Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
289       }
290       if (mdb->num_rows >= 1) {
291          if ((row = sql_fetch_row(mdb)) == NULL) {
292             Mmsg1(&mdb->errmsg, _("error fetching Client row: %s\n"), sql_strerror(mdb));
293             Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
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       Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
317       cr->ClientId = 0;
318       stat = 0;
319    } else {
320       cr->ClientId = sql_insert_id(mdb);
321       stat = 1;
322    }
323    db_unlock(mdb);
324    return stat;
325 }
326
327
328 /* 
329  * Create a FileSet record. This record is unique in the
330  *  name and the MD5 signature of the include/exclude sets.
331  *  Returns: 0 on failure
332  *           1 on success with FileSetId in record
333  */
334 int db_create_fileset_record(B_DB *mdb, FILESET_DBR *fsr)
335 {
336    SQL_ROW row;
337    int stat;
338
339    db_lock(mdb);
340    Mmsg(&mdb->cmd, "SELECT FileSetId FROM FileSet WHERE \
341 FileSet='%s' and MD5='%s'", fsr->FileSet, fsr->MD5);
342
343    fsr->FileSetId = 0;
344    if (QUERY_DB(mdb, mdb->cmd)) {
345
346       mdb->num_rows = sql_num_rows(mdb);
347       
348       if (mdb->num_rows > 1) {
349          Mmsg1(&mdb->errmsg, _("More than one FileSet!: %d\n"), (int)(mdb->num_rows));
350          Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
351       }
352       if (mdb->num_rows >= 1) {
353          if ((row = sql_fetch_row(mdb)) == NULL) {
354             Mmsg1(&mdb->errmsg, _("error fetching FileSet row: ERR=%s\n"), sql_strerror(mdb));
355             Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
356             sql_free_result(mdb);
357             db_unlock(mdb);
358             return 0;
359          }
360          fsr->FileSetId = atoi(row[0]);
361          sql_free_result(mdb);
362          db_unlock(mdb);
363          return 1;
364       }
365       sql_free_result(mdb);
366    }
367
368    /* Must create it */
369    Mmsg(&mdb->cmd, "INSERT INTO FileSet (FileSet, MD5) VALUES \
370 ('%s', '%s')", fsr->FileSet, fsr->MD5);
371
372    if (!INSERT_DB(mdb, mdb->cmd)) {
373       Mmsg2(&mdb->errmsg, _("Create DB FileSet record %s failed. ERR=%s\n"),
374             mdb->cmd, sql_strerror(mdb));
375       Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
376       fsr->FileSetId = 0;
377       stat = 0;
378    } else {
379       fsr->FileSetId = sql_insert_id(mdb);
380       stat = 1;
381    }
382
383    db_unlock(mdb);
384    return stat;
385 }
386
387
388 /*
389  *  struct stat
390  *  {
391  *      dev_t         st_dev;       * device *
392  *      ino_t         st_ino;       * inode *
393  *      mode_t        st_mode;      * protection *
394  *      nlink_t       st_nlink;     * number of hard links *
395  *      uid_t         st_uid;       * user ID of owner *
396  *      gid_t         st_gid;       * group ID of owner *
397  *      dev_t         st_rdev;      * device type (if inode device) *
398  *      off_t         st_size;      * total size, in bytes *
399  *      unsigned long st_blksize;   * blocksize for filesystem I/O *
400  *      unsigned long st_blocks;    * number of blocks allocated *
401  *      time_t        st_atime;     * time of last access *
402  *      time_t        st_mtime;     * time of last modification *
403  *      time_t        st_ctime;     * time of last inode change *            
404  *  };
405  */
406
407
408
409 /* 
410  * Create File record in B_DB   
411  *
412  *  In order to reduce database size, we store the File attributes,
413  *  the FileName, and the Path separately.  In principle, there   
414  *  is a single FileName record and a single Path record, no matter
415  *  how many times it occurs.  This is this subroutine, we separate
416  *  the file and the path and create three database records.
417  */
418 int db_create_file_attributes_record(B_DB *mdb, ATTR_DBR *ar)
419 {
420    int fnl, pnl;
421    char *l, *p;
422    /* ****FIXME***** malloc these */
423    char file[MAXSTRING];
424    char spath[MAXSTRING];
425    char buf[MAXSTRING];
426
427    Dmsg1(100, "Fname=%s\n", ar->fname);
428    Dmsg0(50, "put_file_into_catalog\n");
429    /* For the moment, we only handle Unix attributes.  Note, we are
430     * also getting any MD5 signature that was computed.
431     */
432    if (ar->Stream != STREAM_UNIX_ATTRIBUTES) {
433       Mmsg0(&mdb->errmsg, _("Attempt to put non-attributes into catalog\n"));
434       Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
435       return 0;
436    }
437
438    /* Find path without the filename.  
439     * I.e. everything after the last / is a "filename".
440     * OK, maybe it is a directory name, but we treat it like
441     * a filename. If we don't find a / then the whole name
442     * must be a path name (e.g. c:).
443     */
444    for (p=l=ar->fname; *p; p++) {
445       if (*p == '/') {
446          l = p;                       /* set pos of last slash */
447       }
448    }
449    if (*l == '/') {                   /* did we find a slash? */
450       l++;                            /* yes, point to filename */
451    } else {                           /* no, whole thing must be path name */
452       l = p;
453    }
454
455    /* If filename doesn't exist (i.e. root directory), we
456     * simply create a blank name consisting of a single 
457     * space. This makes handling zero length filenames
458     * easier.
459     */
460    fnl = p - l;
461    if (fnl > 255) {
462       Jmsg(mdb->jcr, M_WARNING, 0, _("Filename truncated to 255 chars: %s\n"), l);
463       fnl = 255;
464    }
465    if (fnl > 0) {
466       strncpy(file, l, fnl);          /* copy filename */
467       file[fnl] = 0;
468    } else {
469       file[0] = ' ';                  /* blank filename */
470       file[1] = 0;
471       fnl = 1;
472    }
473
474    pnl = l - ar->fname;    
475    if (pnl > 255) {
476       Jmsg(mdb->jcr, M_WARNING, 0, _("Path name truncated to 255 chars: %s\n"), ar->fname);
477       pnl = 255;
478    }
479    strncpy(spath, ar->fname, pnl);
480    spath[pnl] = 0;
481
482    if (pnl == 0) {
483       Mmsg1(&mdb->errmsg, _("Path length is zero. File=%s\n"), ar->fname);
484       Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
485       spath[0] = ' ';
486       spath[1] = 0;
487       pnl = 1;
488    }
489
490    Dmsg1(100, "spath=%s\n", spath);
491    Dmsg1(100, "file=%s\n", file);
492
493    db_escape_string(buf, file, fnl);
494
495    if (!db_create_filename_record(mdb, ar, buf)) {
496       return 0;
497    }
498    Dmsg1(100, "db_create_filename_record: %s\n", buf);
499
500    db_escape_string(buf, spath, pnl);
501
502    if (!db_create_path_record(mdb, ar, buf)) {
503       return 0;
504    }
505    Dmsg1(100, "db_create_path_record\n", buf);
506
507    if (!db_create_file_record(mdb, ar)) {
508       return 0;
509    }
510    Dmsg0(50, "db_create_file_record\n");
511
512    Dmsg3(100, "Path=%s File=%s FilenameId=%d\n", spath, file, ar->FilenameId);
513    return 1;
514 }
515
516 static int db_create_file_record(B_DB *mdb, ATTR_DBR *ar)
517 {
518    int stat;
519
520    ASSERT(ar->JobId);
521    ASSERT(ar->PathId);
522    ASSERT(ar->FilenameId);
523
524    db_lock(mdb);
525    /* Must create it */
526    Mmsg(&mdb->cmd,
527 "INSERT INTO File (FileIndex, JobId, PathId, FilenameId, \
528 LStat, MD5) VALUES (%d, %d, %d, %d, '%s', '0')", 
529      (int)ar->FileIndex, ar->JobId, ar->PathId, ar->FilenameId, 
530       ar->attr);
531
532    if (!INSERT_DB(mdb, mdb->cmd)) {
533       Mmsg2(&mdb->errmsg, _("Create db File record %s failed. ERR=%s"),       
534          mdb->cmd, sql_strerror(mdb));
535       Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
536       ar->FileId = 0;
537       stat = 0;
538    } else {
539       ar->FileId = sql_insert_id(mdb);
540       stat = 1;
541    }
542    db_unlock(mdb);
543    return stat;
544 }
545
546 /* Create a Unique record for the Path -- no duplicates */
547 static int db_create_path_record(B_DB *mdb, ATTR_DBR *ar, char *path)
548 {
549    SQL_ROW row;
550    int stat;
551
552    if (*path == 0) {
553       Mmsg0(&mdb->errmsg, _("Null path given to db_create_path_record\n"));
554       Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
555       ar->PathId = 0;
556       ASSERT(ar->PathId);
557       return 0;
558    }
559
560    db_lock(mdb);
561
562    if (mdb->cached_path_id != 0 && strcmp(mdb->cached_path, path) == 0) {
563       ar->PathId = mdb->cached_path_id;
564       ASSERT(ar->PathId);
565       db_unlock(mdb);
566       return 1;
567    }          
568
569    Mmsg(&mdb->cmd, "SELECT PathId FROM Path WHERE Path='%s'", path);
570
571    if (QUERY_DB(mdb, mdb->cmd)) {
572
573       mdb->num_rows = sql_num_rows(mdb);
574
575       if (mdb->num_rows > 1) {
576          char ed1[30];
577          Mmsg2(&mdb->errmsg, _("More than one Path!: %s for Path=%s\n"), 
578             edit_uint64(mdb->num_rows, ed1), path);
579          Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
580       }
581       if (mdb->num_rows >= 1) {
582          if ((row = sql_fetch_row(mdb)) == NULL) {
583             db_unlock(mdb);
584             Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
585             Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
586             sql_free_result(mdb);
587             ar->PathId = 0;
588             ASSERT(ar->PathId);
589             return 0;
590          }
591          ar->PathId = atoi(row[0]);
592          sql_free_result(mdb);
593          /* Cache path */
594          if (ar->PathId != mdb->cached_path_id) {
595             mdb->cached_path_id = ar->PathId;
596             mdb->cached_path = check_pool_memory_size(mdb->cached_path,
597                strlen(path)+1);
598             strcpy(mdb->cached_path, path);
599          }
600          ASSERT(ar->PathId);
601          db_unlock(mdb);
602          return 1;
603       }
604
605       sql_free_result(mdb);
606    }
607
608    Mmsg(&mdb->cmd, "INSERT INTO Path (Path)  VALUES ('%s')", path);
609
610    if (!INSERT_DB(mdb, mdb->cmd)) {
611       Mmsg2(&mdb->errmsg, _("Create db Path record %s failed. ERR=%s\n"), 
612          mdb->cmd, sql_strerror(mdb));
613       Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
614       ar->PathId = 0;
615       stat = 0;
616    } else {
617       ar->PathId = sql_insert_id(mdb);
618       stat = 1;
619    }
620
621    /* Cache path */
622    if (ar->PathId != mdb->cached_path_id) {
623       mdb->cached_path_id = ar->PathId;
624       mdb->cached_path = check_pool_memory_size(mdb->cached_path,
625          strlen(path)+1);
626       strcpy(mdb->cached_path, path);
627    }
628    ASSERT(ar->PathId);
629    db_unlock(mdb);
630    return stat;
631 }
632
633 /* Create a Unique record for the filename -- no duplicates */
634 static int db_create_filename_record(B_DB *mdb, ATTR_DBR *ar, char *fname) 
635 {
636    SQL_ROW row;
637
638    db_lock(mdb);
639    Mmsg(&mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name='%s'", fname);
640
641    if (QUERY_DB(mdb, mdb->cmd)) {
642       mdb->num_rows = sql_num_rows(mdb);
643       if (mdb->num_rows > 1) {
644          Mmsg2(&mdb->errmsg, _("More than one Filename!: %d File=%s\n"), 
645             (int)(mdb->num_rows), fname);
646          Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
647       }
648       if (mdb->num_rows >= 1) {
649          if ((row = sql_fetch_row(mdb)) == NULL) {
650             Mmsg2(&mdb->errmsg, _("error fetching row for file=%s: ERR=%s\n"), 
651                 fname, sql_strerror(mdb));
652             Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
653             ar->FilenameId = 0;
654          } else {
655             ar->FilenameId = atoi(row[0]);
656          }
657          sql_free_result(mdb);
658          db_unlock(mdb);
659          return ar->FilenameId > 0;
660       }
661       sql_free_result(mdb);
662    }
663
664    Mmsg(&mdb->cmd, "INSERT INTO Filename (Name) \
665 VALUES ('%s')", fname);
666
667    if (!INSERT_DB(mdb, mdb->cmd)) {
668       Mmsg2(&mdb->errmsg, _("Create db Filename record %s failed. ERR=%s\n"), 
669             mdb->cmd, sql_strerror(mdb));
670       Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
671       ar->FilenameId = 0;
672    } else {
673       ar->FilenameId = sql_insert_id(mdb);
674    }
675
676    db_unlock(mdb);
677    return ar->FilenameId > 0;
678 }
679
680 #endif /* HAVE_MYSQL || HAVE_SQLITE */