Record info elements better enumerated. Internal store of records.
[idzebra-moved-to-github.git] / index / extract.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: extract.c,v $
7  * Revision 1.33  1995-11-27 09:56:20  adam
8  * Record info elements better enumerated. Internal store of records.
9  *
10  * Revision 1.32  1995/11/25  10:24:05  adam
11  * More record fields - they are enumerated now.
12  * New options: flagStoreData flagStoreKey.
13  *
14  * Revision 1.31  1995/11/24  11:31:35  adam
15  * Commands add & del read filenames from stdin if source directory is
16  * empty.
17  * Match criteria supports 'constant' strings.
18  *
19  * Revision 1.30  1995/11/22  17:19:16  adam
20  * Record management uses the bfile system.
21  *
22  * Revision 1.29  1995/11/21  15:01:14  adam
23  * New general match criteria implemented.
24  * New feature: document groups.
25  *
26  * Revision 1.28  1995/11/21  09:20:30  adam
27  * Yet more work on record match.
28  *
29  * Revision 1.27  1995/11/20  16:59:45  adam
30  * New update method: the 'old' keys are saved for each records.
31  *
32  * Revision 1.26  1995/11/20  11:56:24  adam
33  * Work on new traversal.
34  *
35  * Revision 1.25  1995/11/16  15:34:54  adam
36  * Uses new record management system in both indexer and server.
37  *
38  * Revision 1.24  1995/11/15  19:13:08  adam
39  * Work on record management.
40  *
41  * Revision 1.23  1995/10/27  14:00:10  adam
42  * Implemented detection of database availability.
43  *
44  * Revision 1.22  1995/10/17  18:02:07  adam
45  * New feature: databases. Implemented as prefix to words in dictionary.
46  *
47  * Revision 1.21  1995/10/10  12:24:38  adam
48  * Temporary sort files are compressed.
49  *
50  * Revision 1.20  1995/10/06  13:52:05  adam
51  * Bug fixes. Handler may abort further scanning.
52  *
53  * Revision 1.19  1995/10/04  12:55:16  adam
54  * Bug fix in ranked search. Use=Any keys inserted.
55  *
56  * Revision 1.18  1995/10/04  09:37:08  quinn
57  * Fixed bug.
58  *
59  * Revision 1.17  1995/10/03  14:28:57  adam
60  * Buffered read in extract works.
61  *
62  * Revision 1.16  1995/10/03  14:28:45  adam
63  * Work on more effecient read handler in extract.
64  *
65  * Revision 1.15  1995/10/02  15:42:53  adam
66  * Extract uses file descriptors instead of FILE pointers.
67  *
68  * Revision 1.14  1995/10/02  15:29:13  adam
69  * More logging in file_extract.
70  *
71  * Revision 1.13  1995/09/29  14:01:39  adam
72  * Bug fixes.
73  *
74  * Revision 1.12  1995/09/28  14:22:56  adam
75  * Sort uses smaller temporary files.
76  *
77  * Revision 1.11  1995/09/28  12:10:31  adam
78  * Bug fixes. Field prefix used in queries.
79  *
80  * Revision 1.10  1995/09/28  09:19:41  adam
81  * xfree/xmalloc used everywhere.
82  * Extract/retrieve method seems to work for text records.
83  *
84  * Revision 1.9  1995/09/27  12:22:28  adam
85  * More work on extract in record control.
86  * Field name is not in isam keys but in prefix in dictionary words.
87  *
88  * Revision 1.8  1995/09/14  07:48:22  adam
89  * Record control management.
90  *
91  * Revision 1.7  1995/09/11  13:09:32  adam
92  * More work on relevance feedback.
93  *
94  * Revision 1.6  1995/09/08  14:52:27  adam
95  * Minor changes. Dictionary is lower case now.
96  *
97  * Revision 1.5  1995/09/06  16:11:16  adam
98  * Option: only one word key per file.
99  *
100  * Revision 1.4  1995/09/05  15:28:39  adam
101  * More work on search engine.
102  *
103  * Revision 1.3  1995/09/04  12:33:41  adam
104  * Various cleanup. YAZ util used instead.
105  *
106  * Revision 1.2  1995/09/04  09:10:34  adam
107  * More work on index add/del/update.
108  * Merge sort implemented.
109  * Initial work on z39 server.
110  *
111  * Revision 1.1  1995/09/01  14:06:35  adam
112  * Split of work into more files.
113  *
114  */
115 #include <stdio.h>
116 #include <assert.h>
117 #include <unistd.h>
118 #include <fcntl.h>
119 #include <ctype.h>
120
121 #include <alexutil.h>
122 #include <recctrl.h>
123 #include "index.h"
124
125 #include "recindex.h"
126
127 static Dict matchDict;
128
129 static Records records = NULL;
130
131 static char **key_buf;
132 static size_t ptr_top;
133 static size_t ptr_i;
134 static size_t key_buf_used;
135 static int key_file_no;
136
137 static int records_inserted = 0;
138 static int records_updated = 0;
139 static int records_deleted = 0;
140
141 #define MATCH_DICT "match"
142
143 void key_open (int mem)
144 {
145     if (mem < 50000)
146         mem = 50000;
147     key_buf = xmalloc (mem);
148     ptr_top = mem/sizeof(char*);
149     ptr_i = 0;
150
151     key_buf_used = 0;
152     key_file_no = 0;
153
154     if (!(matchDict = dict_open (MATCH_DICT, 20, 1)))
155     {
156         logf (LOG_FATAL, "dict_open fail of %s", MATCH_DICT);
157         exit (1);
158     }
159     assert (!records);
160     records = rec_open (1);
161 }
162
163 struct encode_info {
164     int  sysno;
165     int  seqno;
166     char buf[512];
167 };
168
169 void encode_key_init (struct encode_info *i)
170 {
171     i->sysno = 0;
172     i->seqno = 0;
173 }
174
175 char *encode_key_int (int d, char *bp)
176 {
177     if (d <= 63)
178         *bp++ = d;
179     else if (d <= 16383)
180     {
181         *bp++ = 64 + (d>>8);
182         *bp++ = d  & 255;
183     }
184     else if (d <= 4194303)
185     {
186         *bp++ = 128 + (d>>16);
187         *bp++ = (d>>8) & 255;
188         *bp++ = d & 255;
189     }
190     else
191     {
192         *bp++ = 192 + (d>>24);
193         *bp++ = (d>>16) & 255;
194         *bp++ = (d>>8) & 255;
195         *bp++ = d & 255;
196     }
197     return bp;
198 }
199
200 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
201 {
202     struct it_key key;
203     char *bp = i->buf;
204
205     while ((*bp++ = *k++))
206         ;
207     memcpy (&key, k+1, sizeof(struct it_key));
208     bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
209     if (i->sysno != key.sysno)
210     {
211         i->sysno = key.sysno;
212         i->seqno = 0;
213     }
214     bp = encode_key_int (key.seqno - i->seqno, bp);
215     i->seqno = key.seqno;
216     if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
217     {
218         logf (LOG_FATAL|LOG_ERRNO, "fwrite");
219         exit (1);
220     }
221 }
222
223 void key_flush (void)
224 {
225     FILE *outf;
226     char out_fname[200];
227     char *prevcp, *cp;
228     struct encode_info encode_info;
229     
230     if (ptr_i <= 0)
231         return;
232
233     key_file_no++;
234     logf (LOG_LOG, "sorting section %d", key_file_no);
235     qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
236     sprintf (out_fname, TEMP_FNAME, key_file_no);
237
238     if (!(outf = fopen (out_fname, "w")))
239     {
240         logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
241         exit (1);
242     }
243     logf (LOG_LOG, "writing section %d", key_file_no);
244     prevcp = cp = key_buf[ptr_top-ptr_i];
245     
246     encode_key_init (&encode_info);
247     encode_key_write (cp, &encode_info, outf);
248     while (--ptr_i > 0)
249     {
250         cp = key_buf[ptr_top-ptr_i];
251         if (strcmp (cp, prevcp))
252         {
253             encode_key_init (&encode_info);
254             encode_key_write (cp, &encode_info, outf);
255             prevcp = cp;
256         }
257         else
258             encode_key_write (cp + strlen(cp), &encode_info, outf);
259     }
260     if (fclose (outf))
261     {
262         logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
263         exit (1);
264     }
265     logf (LOG_LOG, "finished section %d", key_file_no);
266     ptr_i = 0;
267     key_buf_used = 0;
268 }
269
270 int key_close (void)
271 {
272     key_flush ();
273     xfree (key_buf);
274     rec_close (&records);
275     dict_close (matchDict);
276
277     logf (LOG_LOG, "Records inserted %6d", records_inserted);
278     logf (LOG_LOG, "Records updated  %6d", records_updated);
279     logf (LOG_LOG, "Records deleted  %6d", records_deleted);
280     return key_file_no;
281 }
282
283 static void wordInit (RecWord *p)
284 {
285     p->attrSet = 1;
286     p->attrUse = 1016;
287     p->which = Word_String;
288 }
289
290 struct recKeys {
291     int buf_used;
292     int buf_max;
293     char *buf;
294 } reckeys;
295
296 static void addRecordKey (const RecWord *p)
297 {
298     char *dst;
299     char attrSet;
300     short attrUse;
301     size_t i;
302
303     if (reckeys.buf_used+1024 > reckeys.buf_max)
304     {
305         char *b;
306
307         b = malloc (reckeys.buf_max += 65000);
308         if (reckeys.buf_used > 0)
309             memcpy (b, reckeys.buf, reckeys.buf_used);
310         free (reckeys.buf);
311         reckeys.buf = b;
312     }
313     dst = reckeys.buf + reckeys.buf_used;
314     switch (p->which)
315     {
316     case Word_String:
317         attrSet = p->attrSet;
318         memcpy (dst, &attrSet, sizeof(attrSet));
319         dst += sizeof(attrSet);
320
321         attrUse = p->attrUse;
322         memcpy (dst, &attrUse, sizeof(attrUse));
323         dst += sizeof(attrUse);
324         
325         for (i = 0; p->u.string[i]; i++)
326             *dst++ = p->u.string[i];
327         *dst++ = '\0';
328
329         memcpy (dst, &p->seqno, sizeof(p->seqno));
330         dst += sizeof(p->seqno);
331
332         break;
333     default:
334         return;
335     }
336     reckeys.buf_used = dst - reckeys.buf;
337 }
338
339 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys, 
340                              const char *databaseName)
341 {
342     int off = 0;
343     while (off < reckeys->buf_used)
344     {
345         const char *src = reckeys->buf + off;
346         char attrSet;
347         short attrUse;
348         struct it_key key;
349         
350         memcpy (&attrSet, src, sizeof(attrSet));
351         src += sizeof(attrSet);
352
353         memcpy (&attrUse, src, sizeof(attrUse));
354         src += sizeof(attrUse);
355
356         if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
357             key_flush ();
358         ++ptr_i;
359         key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
360         key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
361                                            attrSet, attrUse, databaseName);
362         while (*src)
363             ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
364         src++;
365         ((char*)key_buf) [key_buf_used++] = '\0';
366         
367         ((char*) key_buf)[key_buf_used++] = cmd;
368
369         memcpy (&key.seqno, src, sizeof(key.seqno));
370         src += sizeof(key.seqno);
371         key.sysno = sysno;
372         memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
373         key_buf_used += sizeof(key);
374         off = src - reckeys->buf;
375     }
376     assert (off == reckeys->buf_used);
377 }
378
379 static const char **searchRecordKey (struct recKeys *reckeys,
380                                int attrSetS, int attrUseS)
381 {
382     static const char *ws[32];
383     int off = 0;
384     int startSeq = -1;
385     int i;
386
387     for (i = 0; i<32; i++)
388         ws[i] = NULL;
389     
390     while (off < reckeys->buf_used)
391     {
392         const char *src = reckeys->buf + off;
393         char attrSet;
394         short attrUse;
395         int seqno;
396         const char *wstart;
397         
398         memcpy (&attrSet, src, sizeof(attrSet));
399         src += sizeof(attrSet);
400
401         memcpy (&attrUse, src, sizeof(attrUse));
402         src += sizeof(attrUse);
403
404         wstart = src;
405         while (*src++)
406             ;
407
408         memcpy (&seqno, src, sizeof(seqno));
409         src += sizeof(seqno);
410
411 #if 0
412         logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
413 #endif
414         if (attrUseS == attrUse && attrSetS == attrSet)
415         {
416             int woff;
417
418
419             if (startSeq == -1)
420                 startSeq = seqno;
421             woff = seqno - startSeq;
422             if (woff >= 0 && woff < 31)
423                 ws[woff] = wstart;
424         }
425
426         off = src - reckeys->buf;
427     }
428     assert (off == reckeys->buf_used);
429     return ws;
430 }
431
432 static void addRecordKeyAny (const RecWord *p)
433 {
434     if (p->attrSet != 1 || p->attrUse != 1016)
435     {
436         RecWord w;
437
438         memcpy (&w, p, sizeof(w));
439         w.attrSet = 1;
440         w.attrUse = 1016;
441         addRecordKey (&w);
442     }
443     addRecordKey (p);
444 }
445
446 #define FILE_READ_BUFSIZE 4096 
447
448 static char *file_buf;
449 static int file_offset;
450 static int file_bufsize;
451 static int file_noread;
452
453 static void file_read_start (int fd)
454 {
455     file_offset = 0;
456     file_buf = xmalloc (FILE_READ_BUFSIZE);
457     file_bufsize = read (fd, file_buf, FILE_READ_BUFSIZE);
458     file_noread = 0;
459 }
460
461 static void file_read_stop (int fd)
462 {
463     xfree (file_buf);
464     file_buf = NULL;
465 }
466
467 static int file_read (int fd, char *buf, size_t count)
468 {
469     int l = file_bufsize - file_offset;
470
471     if (count > l)
472     {
473         int r;
474         if (l > 0)
475             memcpy (buf, file_buf + file_offset, l);
476         count = count-l;
477         if (count > file_bufsize)
478         {
479             if ((r = read (fd, buf + l, count)) == -1)
480             {
481                 logf (LOG_FATAL|LOG_ERRNO, "read");
482                 exit (1);
483             }
484             file_bufsize = 0;
485             file_offset = 0;
486             file_noread += r;
487             return r;
488         }
489         file_bufsize = r = read (fd, file_buf, FILE_READ_BUFSIZE);
490         if (r == -1)
491         {
492             logf (LOG_FATAL|LOG_ERRNO, "read");
493             exit (1);
494         }
495         else if (r <= count)
496         {
497             file_offset = r;
498             memcpy (buf + l, file_buf, r);
499             file_noread += (l+r);
500             return l + r;
501         }
502         else
503         {
504             file_offset = count;
505             memcpy (buf + l, file_buf, count - l);
506             file_noread += count;
507             return count;
508         }
509     }
510     memcpy (buf, file_buf + file_offset, count);
511     file_offset += count;
512     file_noread += count;
513     return count;
514 }
515
516 static int atois (const char **s)
517 {
518     int val = 0, c;
519     while ( (c=**s) >= '0' && c <= '9')
520     {
521         val = val*10 + c - '0';
522         ++(*s);
523     }
524     return val;
525 }
526
527 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
528                            const char *fname,
529                            const char *recordType,
530                            const char *spec)
531 {
532     static char dstBuf[2048];
533     char *dst = dstBuf;
534     const char *s = spec;
535     static const char **w;
536     int i;
537
538     while (1)
539     {
540         while (*s == ' ' || *s == '\t')
541             s++;
542         if (!*s)
543             break;
544         if (*s == '(')
545         {
546             char matchFlag[32];
547             int attrSet, attrUse;
548             int first = 1;
549
550             s++;
551             attrSet = atois (&s);
552             if (*s != ',')
553             {
554                 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
555                       spec, rGroup->groupName ? rGroup->groupName : "none");
556                 return NULL;
557             }
558             s++;
559             attrUse = atois (&s);
560             w = searchRecordKey (reckeys, attrSet, attrUse);
561             assert (w);
562
563             if (*s == ')')
564             {
565                 for (i = 0; i<32; i++)
566                     matchFlag[i] = 1;
567             }
568             else
569             {
570                 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
571                       spec, rGroup->groupName ? rGroup->groupName : "none");
572                 return NULL;
573             }
574             s++;
575
576             for (i = 0; i<32; i++)
577                 if (matchFlag[i] && w[i])
578                 {
579                     if (first)
580                     {
581                         *dst++ = ' ';
582                         first = 0;
583                     }
584                     strcpy (dst, w[i]);
585                     dst += strlen(w[i]);
586                 }
587             if (first)
588             {
589                 logf (LOG_WARN, "Record in file %s didn't contain match"
590                       " fields in (%d,%d)", fname, attrSet, attrUse);
591                 return NULL;
592             }
593         }
594         else if (*s == '$')
595         {
596             int spec_len;
597             char special[64];
598             const char *spec_src = NULL;
599             const char *s1 = ++s;
600             while (*s1 && *s1 != ' ' && *s1 != '\t')
601                 s1++;
602
603             spec_len = s1 - s;
604             if (spec_len > 63)
605                 spec_len = 63;
606             memcpy (special, s, spec_len);
607             special[spec_len] = '\0';
608             s = s1;
609
610             if (strcmp (special, "group"))
611                 spec_src = rGroup->groupName;
612             else if (strcmp (special, "database"))
613                 spec_src = rGroup->databaseName;
614             else if (strcmp (special, "filename"))
615                 spec_src = fname;
616             else if (strcmp (special, "type"))
617                 spec_src = recordType;
618             else 
619                 spec_src = NULL;
620             if (spec_src)
621             {
622                 strcpy (dst, spec_src);
623                 dst += strlen (spec_src);
624             }
625         }
626         else if (*s == '\"' || *s == '\'')
627         {
628             int stopMarker = *s++;
629             char tmpString[64];
630             int i = 0;
631
632             while (*s && *s != stopMarker)
633             {
634                 if (i < 63)
635                     tmpString[i++] = *s;
636             }
637             if (*s)
638                 s++;
639             tmpString[i] = '\0';
640             strcpy (dst, tmpString);
641             dst += strlen (tmpString);
642         }
643         else
644         {
645             logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
646                   spec, rGroup->groupName ? rGroup->groupName : "none");
647             return NULL;
648         }
649         *dst++ = 1;
650     }
651     if (dst == dstBuf)
652     {
653         logf (LOG_WARN, "No match criteria for record %s in group %s",
654               fname, rGroup->groupName ? rGroup->groupName : "none");
655         return NULL;
656     }
657     return dstBuf;
658 }
659
660 static int recordExtract (SYSNO *sysno, const char *fname,
661                           struct recordGroup *rGroup, int deleteFlag,
662                           int fd,
663                           const char *file_type,
664                           RecType recType)
665 {
666     struct recExtractCtrl extractCtrl;
667     int r;
668     char *matchStr;
669     SYSNO sysnotmp;
670     Record rec;
671
672     extractCtrl.fd = fd;
673     
674     /* extract keys */
675     extractCtrl.subType = "";
676     extractCtrl.init = wordInit;
677     extractCtrl.add = addRecordKeyAny;
678
679     reckeys.buf_used = 0;
680     extractCtrl.readf = file_read;
681     r = (*recType->extract)(&extractCtrl);
682   
683     if (r)      
684     {
685         logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
686         return 0;
687     }
688
689     /* perform match if sysno not known and if match criteria is specified */
690        
691     matchStr = NULL;
692     if (!sysno) 
693     {
694         sysnotmp = 0;
695         sysno = &sysnotmp;
696         if (rGroup->fileMatch)
697         {
698             char *rinfo;
699         
700             matchStr = fileMatchStr(&reckeys, rGroup, fname, file_type,
701                                     rGroup->fileMatch);
702             if (matchStr)
703             {
704                 rinfo = dict_lookup (matchDict, matchStr);
705                 if (rinfo)
706                     memcpy (sysno, rinfo+1, sizeof(*sysno));
707             }
708             else
709             {
710                 logf (LOG_WARN, "Record not inserted");
711                 return 0;
712             }
713         }
714     }
715
716     /* new record ? */
717     if (! *sysno)
718     {
719         if (deleteFlag)
720         {
721             logf (LOG_LOG, "? record %s", fname);
722             return 1;
723         }
724         logf (LOG_LOG, "add record %s", fname);
725         rec = rec_new (records);
726         *sysno = rec->sysno;
727
728         if (matchStr)
729             dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
730         flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
731
732         records_inserted++;
733     }
734     else
735     {
736         struct recKeys delkeys;
737
738         rec = rec_get (records, *sysno);
739
740         delkeys.buf_used = rec->size[recInfo_delKeys];
741         delkeys.buf = rec->info[recInfo_delKeys];
742         flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
743         if (deleteFlag)
744         {
745             if (!delkeys.buf_used)
746             {
747                 logf (LOG_WARN, "cannot delete %s - no delete keys",
748                       fname);
749             }
750             else
751                 logf (LOG_LOG, "delete record %s", fname);
752             records_deleted++;
753             rec_del (records, &rec);
754             return 1;
755         }
756         else
757         {
758             if (!delkeys.buf_used)
759             {
760                 logf (LOG_WARN, "cannot update %s - no delete keys",
761                       fname);
762             }
763             else
764             {
765                 logf (LOG_LOG, "update record %s", fname);
766                 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName); 
767                 records_updated++;
768             }
769         }
770     }
771     free (rec->info[recInfo_fileType]);
772     rec->info[recInfo_fileType] =
773         rec_strdup (file_type, &rec->size[recInfo_fileType]);
774
775     free (rec->info[recInfo_filename]);
776     rec->info[recInfo_filename] =
777         rec_strdup (fname, &rec->size[recInfo_filename]);
778
779     free (rec->info[recInfo_delKeys]);
780     if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
781     {
782         rec->info[recInfo_delKeys] = malloc (reckeys.buf_used);
783         rec->size[recInfo_delKeys] = reckeys.buf_used;
784         memcpy (rec->info[recInfo_delKeys], reckeys.buf,
785                 rec->size[recInfo_delKeys]);
786     }
787     else
788     {
789         rec->info[recInfo_delKeys] = NULL;
790         rec->size[recInfo_delKeys] = 0;
791     }
792
793     free (rec->info[recInfo_storeData]);
794     if (rGroup->flagStoreData == 1)
795     {
796         rec->size[recInfo_storeData] = file_noread;
797         rec->info[recInfo_storeData] = malloc (file_noread);
798         if (file_noread < FILE_READ_BUFSIZE)
799             memcpy (rec->info[recInfo_storeData], file_buf, file_noread);
800         else
801         {
802             if (lseek (fd, 0L, SEEK_SET) < 0)
803             {
804                 logf (LOG_ERRNO|LOG_FATAL, "seek to 0 in %s", fname);
805                 exit (1);
806             }
807             if (read (fd, rec->info[recInfo_storeData], file_noread) 
808                 < file_noread)
809             {
810                 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
811                       file_noread, fname);
812                 exit (1);
813             }
814         }
815     }
816     else
817     {
818         rec->info[recInfo_storeData] = NULL;
819         rec->size[recInfo_storeData] = 0;
820     }
821     free (rec->info[recInfo_databaseName]);
822     rec->info[recInfo_databaseName] =
823         rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]); 
824
825     rec_put (records, &rec);
826     return 1;
827 }
828
829 int fileExtract (SYSNO *sysno, const char *fname, struct recordGroup *rGroup,
830                  int deleteFlag)
831 {
832     int i, fd;
833     char gprefix[128];
834     char ext[128];
835     char ext_res[128];
836     const char *file_type;
837     RecType recType;
838
839     if (!rGroup->groupName || !*rGroup->groupName)
840         *gprefix = '\0';
841     else
842         sprintf (gprefix, "%s.", rGroup->groupName);
843
844     logf (LOG_DEBUG, "fileExtract %s", fname);
845
846     /* determine file extension */
847     for (i = strlen(fname); --i >= 0; )
848         if (fname[i] == '/')
849         {
850             strcpy (ext, "");
851             break;
852         }
853         else if (fname[i] == '.')
854         {
855             strcpy (ext, fname+i+1);
856             break;
857         }
858     /* determine file type - depending on extension */
859     sprintf (ext_res, "%sfileExtension.%s", gprefix, ext);
860     if (!(file_type = res_get (common_resource, ext_res)))
861         return 0;
862     if (!(recType = recType_byName (file_type)))
863         return 0;
864
865     /* determine match criteria */
866     if (rGroup->fileMatch)
867     {
868         sprintf (ext_res, "%sfileMatch.%s", gprefix, ext);
869         rGroup->fileMatch = res_get (common_resource, ext_res);
870         if (!rGroup->fileMatch)
871         {
872             sprintf (ext_res, "%sfileMatch", gprefix);
873             rGroup->fileMatch = res_get (common_resource, ext_res);
874         }
875     }
876
877     /* determine database name */
878     if (!rGroup->databaseName)
879     {
880         sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
881         if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
882         {
883             sprintf (ext_res, "%sdatabase", gprefix);
884             rGroup->databaseName = res_get (common_resource, ext_res);
885         }
886     }
887     if (!rGroup->databaseName)
888         rGroup->databaseName = "Default";
889
890     if (rGroup->flagStoreData == -1)
891     {
892         const char *sval;
893         sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
894         if (!(sval = res_get (common_resource, ext_res)))
895         {
896             sprintf (ext_res, "%sstoreData", gprefix);
897             sval = res_get (common_resource, ext_res);
898         }
899         if (sval)
900             rGroup->flagStoreData = atoi (sval);
901     }
902     if (rGroup->flagStoreData == -1)
903         rGroup->flagStoreData = 0;
904
905     if (rGroup->flagStoreKeys == -1)
906     {
907         const char *sval;
908
909         sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
910         if (!(sval = res_get (common_resource, ext_res)))
911         {
912             sprintf (ext_res, "%sstoreKeys", gprefix);
913             sval = res_get (common_resource, ext_res);
914         }
915         if (sval)
916             rGroup->flagStoreKeys = atoi (sval);
917     }
918     if (rGroup->flagStoreKeys == -1)
919         rGroup->flagStoreKeys = 0;
920
921     /* open input file */
922     if ((fd = open (fname, O_RDONLY)) == -1)
923     {
924         logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
925         return 0;
926     }
927     file_read_start (fd);
928     recordExtract (sysno, fname, rGroup, deleteFlag, fd,
929                    file_type, recType);
930     file_read_stop (fd);
931     close (fd);
932     return 1;
933 }
934