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