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