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