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