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