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