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