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