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