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