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