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