ae8a6c44d2d555d4f4f1b19962f92daa0012fb46
[idzebra-moved-to-github.git] / index / extract.c
1 /*
2  * Copyright (C) 1994-1998, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: extract.c,v $
7  * Revision 1.79  1998-02-17 10:32:52  adam
8  * Fixed bug: binary files weren't opened with flag b on NT.
9  *
10  * Revision 1.78  1998/02/10 12:03:05  adam
11  * Implemented Sort.
12  *
13  * Revision 1.77  1998/01/12 15:04:08  adam
14  * The test option (-s) only uses read-lock (and not write lock).
15  *
16  * Revision 1.76  1997/10/27 14:33:04  adam
17  * Moved towards generic character mapping depending on "structure"
18  * field in abstract syntax file. Fixed a few memory leaks. Fixed
19  * bug with negative integers when doing searches with relational
20  * operators.
21  *
22  * Revision 1.75  1997/09/17 12:19:12  adam
23  * Zebra version corresponds to YAZ version 1.4.
24  * Changed Zebra server so that it doesn't depend on global common_resource.
25  *
26  * Revision 1.74  1997/09/09 13:38:06  adam
27  * Partial port to WIN95/NT.
28  *
29  * Revision 1.73  1997/09/04 13:57:20  adam
30  * New file extract/retrieve method tellf (added).
31  * Added O_BINARY for open calls.
32  *
33  * Revision 1.72  1997/07/15 16:32:29  adam
34  * Bug fix: Match handler didn't terminate the resulting string!
35  *
36  * Revision 1.71  1997/07/15 16:28:41  adam
37  * Bug fix: storeData didn't work with files with multiple records.
38  * Bug fix: fixed memory management with records; not really well
39  *  thought through.
40  *
41  * Revision 1.70  1997/07/01 13:00:42  adam
42  * Bug fix in routine searchRecordKey: uninitialized variables.
43  *
44  * Revision 1.69  1997/04/29 09:26:03  adam
45  * Bug fix: generic recordId handling didn't work for compressed internal
46  * keys.
47  *
48  * Revision 1.68  1997/02/12 20:39:45  adam
49  * Implemented options -f <n> that limits the log to the first <n>
50  * records.
51  * Changed some log messages also.
52  *
53  * Revision 1.67  1996/11/15 15:02:14  adam
54  * Minor changes regarding logging.
55  *
56  * Revision 1.66  1996/11/14  09:52:21  adam
57  * Strings in record keys bound by IT_MAX_WORD.
58  *
59  * Revision 1.65  1996/11/14  08:57:56  adam
60  * Reduction of storeKeys area.
61  *
62  * Revision 1.64  1996/11/08 11:10:16  adam
63  * Buffers used during file match got bigger.
64  * Compressed ISAM support everywhere.
65  * Bug fixes regarding masking characters in queries.
66  * Redesigned Regexp-2 queries.
67  *
68  * Revision 1.63  1996/10/29 14:09:39  adam
69  * Use of cisam system - enabled if setting isamc is 1.
70  *
71  * Revision 1.62  1996/10/11 10:57:01  adam
72  * New module recctrl. Used to manage records (extract/retrieval).
73  * Several files have been moved to the recctrl sub directory.
74  *
75  * Revision 1.61  1996/06/06 12:08:37  quinn
76  * Added showRecord function
77  *
78  * Revision 1.60  1996/06/04  10:18:12  adam
79  * Search/scan uses character mapping module.
80  *
81  * Revision 1.59  1996/05/14  15:47:07  adam
82  * Cleanup of various buffer size entities.
83  *
84  * Revision 1.58  1996/05/14  06:16:38  adam
85  * Compact use/set bytes used in search service.
86  *
87  * Revision 1.57  1996/05/13 14:23:04  adam
88  * Work on compaction of set/use bytes in dictionary.
89  *
90  * Revision 1.56  1996/05/09  09:54:42  adam
91  * Server supports maps from one logical attributes to a list of physical
92  * attributes.
93  * The extraction process doesn't make space consuming 'any' keys.
94  *
95  * Revision 1.55  1996/05/09  07:28:55  quinn
96  * Work towards phrases and multiple registers
97  *
98  * Revision 1.54  1996/05/01  13:46:35  adam
99  * First work on multiple records in one file.
100  * New option, -offset, to the "unread" command in the filter module.
101  *
102  * Revision 1.53  1996/04/26  12:09:43  adam
103  * Added a few comments.
104  *
105  * Revision 1.52  1996/04/25  13:27:57  adam
106  * Function recordExtract modified so that files with no keys (possibly empty)
107  * are ignored.
108  *
109  * Revision 1.51  1996/03/19  11:08:42  adam
110  * Bug fix: Log preamble wasn't always turned off after recordExtract.
111  *
112  * Revision 1.50  1996/02/12  18:45:36  adam
113  * New fileVerboseFlag in record group control.
114  *
115  * Revision 1.49  1996/02/05  12:29:57  adam
116  * Logging reduced a bit.
117  * The remaining running time is estimated during register merge.
118  *
119  * Revision 1.48  1996/02/01  20:53:26  adam
120  * The temporary per-record keys are compacted a little, and duplication
121  * of the per-records keys are avoided when they are saved in the record
122  * information buffer.
123  *
124  * Revision 1.47  1996/01/17  14:57:48  adam
125  * Prototype changed for reader functions in extract/retrieve. File
126  *  is identified by 'void *' instead of 'int.
127  *
128  * Revision 1.46  1995/12/15  14:57:16  adam
129  * Bug fix.
130  *
131  * Revision 1.45  1995/12/15  12:37:41  adam
132  * In addRecordKeyAny: Writes key only when attrSet != -1.
133  *
134  * Revision 1.44  1995/12/12  16:00:54  adam
135  * System call sync(2) used after update/commit.
136  * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
137  * and F_RDLCK.
138  *
139  * Revision 1.43  1995/12/11  09:12:46  adam
140  * The rec_get function returns NULL if record doesn't exist - will
141  * happen in the server if the result set records have been deleted since
142  * the creation of the set (i.e. the search).
143  * The server saves a result temporarily if it is 'volatile', i.e. the
144  * set is register dependent.
145  *
146  * Revision 1.42  1995/12/07  17:38:46  adam
147  * Work locking mechanisms for concurrent updates/commit.
148  *
149  * Revision 1.41  1995/12/06  16:06:42  adam
150  * Better diagnostics. Work on 'real' dictionary deletion.
151  *
152  * Revision 1.40  1995/12/05  16:57:40  adam
153  * More work on regular patterns.
154  *
155  * Revision 1.39  1995/12/05  13:20:18  adam
156  * Bug fix: file_read sometimes returned early EOF.
157  *
158  * Revision 1.38  1995/12/04  17:59:21  adam
159  * More work on regular expression conversion.
160  *
161  * Revision 1.37  1995/12/04  14:22:27  adam
162  * Extra arg to recType_byName.
163  * Started work on new regular expression parsed input to
164  * structured records.
165  *
166  * Revision 1.36  1995/11/30  08:34:29  adam
167  * Started work on commit facility.
168  * Changed a few malloc/free to xmalloc/xfree.
169  *
170  * Revision 1.35  1995/11/28  14:26:21  adam
171  * Bug fix: recordId with constant wasn't right.
172  * Bug fix: recordId dictionary entry wasn't deleted when needed.
173  *
174  * Revision 1.34  1995/11/28  09:09:38  adam
175  * Zebra config renamed.
176  * Use setting 'recordId' to identify record now.
177  * Bug fix in recindex.c: rec_release_blocks was invokeded even
178  * though the blocks were already released.
179  * File traversal properly deletes records when needed.
180  *
181  * Revision 1.33  1995/11/27  09:56:20  adam
182  * Record info elements better enumerated. Internal store of records.
183  *
184  * Revision 1.32  1995/11/25  10:24:05  adam
185  * More record fields - they are enumerated now.
186  * New options: flagStoreData flagStoreKey.
187  *
188  * Revision 1.31  1995/11/24  11:31:35  adam
189  * Commands add & del read filenames from stdin if source directory is
190  * empty.
191  * Match criteria supports 'constant' strings.
192  *
193  * Revision 1.30  1995/11/22  17:19:16  adam
194  * Record management uses the bfile system.
195  *
196  * Revision 1.29  1995/11/21  15:01:14  adam
197  * New general match criteria implemented.
198  * New feature: document groups.
199  *
200  * Revision 1.28  1995/11/21  09:20:30  adam
201  * Yet more work on record match.
202  *
203  * Revision 1.27  1995/11/20  16:59:45  adam
204  * New update method: the 'old' keys are saved for each records.
205  *
206  * Revision 1.26  1995/11/20  11:56:24  adam
207  * Work on new traversal.
208  *
209  * Revision 1.25  1995/11/16  15:34:54  adam
210  * Uses new record management system in both indexer and server.
211  *
212  * Revision 1.24  1995/11/15  19:13:08  adam
213  * Work on record management.
214  *
215  * Revision 1.23  1995/10/27  14:00:10  adam
216  * Implemented detection of database availability.
217  *
218  * Revision 1.22  1995/10/17  18:02:07  adam
219  * New feature: databases. Implemented as prefix to words in dictionary.
220  *
221  * Revision 1.21  1995/10/10  12:24:38  adam
222  * Temporary sort files are compressed.
223  *
224  * Revision 1.20  1995/10/06  13:52:05  adam
225  * Bug fixes. Handler may abort further scanning.
226  *
227  * Revision 1.19  1995/10/04  12:55:16  adam
228  * Bug fix in ranked search. Use=Any keys inserted.
229  *
230  * Revision 1.18  1995/10/04  09:37:08  quinn
231  * Fixed bug.
232  *
233  * Revision 1.17  1995/10/03  14:28:57  adam
234  * Buffered read in extract works.
235  *
236  * Revision 1.16  1995/10/03  14:28:45  adam
237  * Work on more effecient read handler in extract.
238  *
239  * Revision 1.15  1995/10/02  15:42:53  adam
240  * Extract uses file descriptors instead of FILE pointers.
241  *
242  * Revision 1.14  1995/10/02  15:29:13  adam
243  * More logging in file_extract.
244  *
245  * Revision 1.13  1995/09/29  14:01:39  adam
246  * Bug fixes.
247  *
248  * Revision 1.12  1995/09/28  14:22:56  adam
249  * Sort uses smaller temporary files.
250  *
251  * Revision 1.11  1995/09/28  12:10:31  adam
252  * Bug fixes. Field prefix used in queries.
253  *
254  * Revision 1.10  1995/09/28  09:19:41  adam
255  * xfree/xmalloc used everywhere.
256  * Extract/retrieve method seems to work for text records.
257  *
258  * Revision 1.9  1995/09/27  12:22:28  adam
259  * More work on extract in record control.
260  * Field name is not in isam keys but in prefix in dictionary words.
261  *
262  * Revision 1.8  1995/09/14  07:48:22  adam
263  * Record control management.
264  *
265  * Revision 1.7  1995/09/11  13:09:32  adam
266  * More work on relevance feedback.
267  *
268  * Revision 1.6  1995/09/08  14:52:27  adam
269  * Minor changes. Dictionary is lower case now.
270  *
271  * Revision 1.5  1995/09/06  16:11:16  adam
272  * Option: only one word key per file.
273  *
274  * Revision 1.4  1995/09/05  15:28:39  adam
275  * More work on search engine.
276  *
277  * Revision 1.3  1995/09/04  12:33:41  adam
278  * Various cleanup. YAZ util used instead.
279  *
280  * Revision 1.2  1995/09/04  09:10:34  adam
281  * More work on index add/del/update.
282  * Merge sort implemented.
283  * Initial work on z39 server.
284  *
285  * Revision 1.1  1995/09/01  14:06:35  adam
286  * Split of work into more files.
287  *
288  */
289 #include <stdio.h>
290 #include <assert.h>
291 #ifdef WINDOWS
292 #include <io.h>
293 #else
294 #include <unistd.h>
295 #endif
296 #include <fcntl.h>
297
298 #include <recctrl.h>
299 #include <charmap.h>
300 #include <sortidx.h>
301 #include "index.h"
302
303 #include "zinfo.h"
304
305 static Dict matchDict;
306
307 static Records records = NULL;
308 static SortIdx sortIdx = NULL;
309
310 static char **key_buf;
311 static size_t ptr_top;
312 static size_t ptr_i;
313 static size_t key_buf_used;
314 static int key_file_no;
315
316 static int records_inserted = 0;
317 static int records_updated = 0;
318 static int records_deleted = 0;
319 static int records_processed = 0;
320
321 static ZebTargetInfo *zti = NULL;
322
323 static void logRecord (int showFlag)
324 {
325     if (!showFlag)
326         ++records_processed;
327     if (showFlag || !(records_processed % 1000))
328     {
329         logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d", 
330               records_processed, records_inserted, records_updated,
331               records_deleted);
332     }
333 }
334
335 int key_open (BFiles bfs, int mem, int rw)
336 {
337     if (!mem)
338         mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
339     if (mem < 50000)
340         mem = 50000;
341     key_buf = xmalloc (mem);
342     ptr_top = mem/sizeof(char*);
343     ptr_i = 0;
344
345     key_buf_used = 0;
346     key_file_no = 0;
347
348     if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw)))
349     {
350         logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
351         return -1;
352     }
353     assert (!records);
354     records = rec_open (bfs, rw);
355     if (!records)
356     {
357         dict_close (matchDict);
358         return -1;
359     }
360     zti = zebTargetInfo_open (records, rw);
361     if (!zti)
362     {
363         rec_close (&records);
364         dict_close (matchDict);
365         return -1;      
366     }
367     sortIdx = sortIdx_open (bfs, 1);
368     return 0;
369 }
370
371 struct encode_info {
372     int  sysno;
373     int  seqno;
374     char buf[768];
375 };
376
377 void encode_key_init (struct encode_info *i)
378 {
379     i->sysno = 0;
380     i->seqno = 0;
381 }
382
383 char *encode_key_int (int d, char *bp)
384 {
385     if (d <= 63)
386         *bp++ = d;
387     else if (d <= 16383)
388     {
389         *bp++ = 64 + (d>>8);
390         *bp++ = d  & 255;
391     }
392     else if (d <= 4194303)
393     {
394         *bp++ = 128 + (d>>16);
395         *bp++ = (d>>8) & 255;
396         *bp++ = d & 255;
397     }
398     else
399     {
400         *bp++ = 192 + (d>>24);
401         *bp++ = (d>>16) & 255;
402         *bp++ = (d>>8) & 255;
403         *bp++ = d & 255;
404     }
405     return bp;
406 }
407
408 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
409 {
410     struct it_key key;
411     char *bp = i->buf;
412
413     while ((*bp++ = *k++))
414         ;
415     memcpy (&key, k+1, sizeof(struct it_key));
416     bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
417     if (i->sysno != key.sysno)
418     {
419         i->sysno = key.sysno;
420         i->seqno = 0;
421     }
422     bp = encode_key_int (key.seqno - i->seqno, bp);
423     i->seqno = key.seqno;
424     if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
425     {
426         logf (LOG_FATAL|LOG_ERRNO, "fwrite");
427         exit (1);
428     }
429 }
430
431 #define SORT_EXTRA 0
432
433 #if SORT_EXTRA
434 static int key_y_len;
435
436 static int key_y_compare (const void *p1, const void *p2)
437 {
438     int r;
439
440     if ((r = key_compare (*(char**) p1 + key_y_len + 1,
441                           *(char**) p2 + key_y_len + 1)))
442          return r;
443     return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
444 }
445
446 static int key_x_compare (const void *p1, const void *p2)
447 {
448     return strcmp (*(char**) p1, *(char**) p2);
449 }
450 #endif
451
452 void key_flush (void)
453 {
454     FILE *outf;
455     char out_fname[200];
456     char *prevcp, *cp;
457     struct encode_info encode_info;
458 #if SORT_EXTRA
459     int i;
460 #endif
461     
462     if (ptr_i <= 0)
463         return;
464
465     key_file_no++;
466     logf (LOG_LOG, "sorting section %d", key_file_no);
467 #if !SORT_EXTRA
468     qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
469     getFnameTmp (out_fname, key_file_no);
470
471     if (!(outf = fopen (out_fname, "wb")))
472     {
473         logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
474         exit (1);
475     }
476     logf (LOG_LOG, "writing section %d", key_file_no);
477     prevcp = cp = key_buf[ptr_top-ptr_i];
478     
479     encode_key_init (&encode_info);
480     encode_key_write (cp, &encode_info, outf);
481     while (--ptr_i > 0)
482     {
483         cp = key_buf[ptr_top-ptr_i];
484         if (strcmp (cp, prevcp))
485         {
486             encode_key_init (&encode_info);
487             encode_key_write (cp, &encode_info, outf);
488             prevcp = cp;
489         }
490         else
491             encode_key_write (cp + strlen(cp), &encode_info, outf);
492     }
493 #else
494     qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
495     getFnameTmp (out_fname, key_file_no);
496
497     if (!(outf = fopen (out_fname, "wb")))
498     {
499         logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
500         exit (1);
501     }
502     logf (LOG_LOG, "writing section %d", key_file_no);
503     i = ptr_i;
504     prevcp =  key_buf[ptr_top-i];
505     while (1)
506         if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
507         {
508             key_y_len = strlen(prevcp)+1;
509 #if 0
510             logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
511                       key_y_len, prevcp[0], prevcp[1], 2+prevcp);
512 #endif
513             qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
514                                    sizeof(char*), key_y_compare);
515             cp = key_buf[ptr_top-ptr_i];
516             --key_y_len;
517             encode_key_init (&encode_info);
518             encode_key_write (cp, &encode_info, outf);
519             while (--ptr_i > i)
520             {
521                 cp = key_buf[ptr_top-ptr_i];
522                 encode_key_write (cp+key_y_len, &encode_info, outf);
523             }
524             if (!i)
525                 break;
526             prevcp = key_buf[ptr_top-ptr_i];
527         }
528 #endif
529     if (fclose (outf))
530     {
531         logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
532         exit (1);
533     }
534     logf (LOG_LOG, "finished section %d", key_file_no);
535     ptr_i = 0;
536     key_buf_used = 0;
537 }
538
539 int key_close ()
540 {
541     key_flush ();
542     xfree (key_buf);
543 #if 1
544     zebTargetInfo_close (zti, 1);
545 #endif
546     rec_close (&records);
547     dict_close (matchDict);
548     sortIdx_close (sortIdx);
549
550     logRecord (1);
551     return key_file_no;
552 }
553
554 static void wordInit (struct recExtractCtrl *p, RecWord *w)
555 {
556     w->zebra_maps = p->zebra_maps;
557     w->attrSet = 1;
558     w->attrUse = 1016;
559     w->reg_type = 'w';
560 }
561
562 static struct sortKey {
563     char *string;
564     int length;
565     int attrSet;
566     int attrUse;
567     struct sortKey *next;
568 } *sortKeys = NULL;
569
570 static struct recKeys {
571     int buf_used;
572     int buf_max;
573     char *buf;
574     char prevAttrSet;
575     short prevAttrUse;
576     int prevSeqNo;
577 } reckeys;
578
579 static void addIndexString (RecWord *p, const char *string, int length)
580 {
581     char *dst;
582     char attrSet;
583     short attrUse;
584     int lead = 0;
585     int diff = 0;
586
587     if (reckeys.buf_used+1024 > reckeys.buf_max)
588     {
589         char *b;
590
591         b = xmalloc (reckeys.buf_max += 128000);
592         if (reckeys.buf_used > 0)
593             memcpy (b, reckeys.buf, reckeys.buf_used);
594         xfree (reckeys.buf);
595         reckeys.buf = b;
596     }
597     dst = reckeys.buf + reckeys.buf_used;
598
599     attrSet = p->attrSet;
600     if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
601         lead |= 1;
602     else
603         reckeys.prevAttrSet = attrSet;
604     attrUse = p->attrUse;
605     if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
606         lead |= 2;
607     else
608         reckeys.prevAttrUse = attrUse;
609 #if 1
610     diff = 1 + p->seqno - reckeys.prevSeqNo;
611     if (diff >= 1 && diff <= 15)
612         lead |= (diff << 2);
613     else
614         diff = 0;
615 #endif
616     reckeys.prevSeqNo = p->seqno;
617
618     *dst++ = lead;
619
620     if (!(lead & 1))
621     {
622         memcpy (dst, &attrSet, sizeof(attrSet));
623         dst += sizeof(attrSet);
624     }
625     if (!(lead & 2))
626     {
627         memcpy (dst, &attrUse, sizeof(attrUse));
628         dst += sizeof(attrUse);
629     }
630     *dst++ = p->reg_type;
631     memcpy (dst, string, length);
632     dst += length;
633     *dst++ = '\0';
634
635     if (!diff)
636     {
637         memcpy (dst, &p->seqno, sizeof(p->seqno));
638         dst += sizeof(p->seqno);
639     }
640     reckeys.buf_used = dst - reckeys.buf;
641     (p->seqno)++;
642 }
643
644 static void addSortString (RecWord *p, const char *string, int length)
645 {
646     struct sortKey *sk;
647
648     for (sk = sortKeys; sk; sk = sk->next)
649         if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
650             return;
651
652     sk = xmalloc (sizeof(*sk));
653     sk->next = sortKeys;
654     sortKeys = sk;
655
656     sk->string = xmalloc (p->length);
657     sk->length = p->length;
658     memcpy (sk->string, p->string, p->length);
659     sk->attrSet = p->attrSet;
660     sk->attrUse = p->attrUse;
661 }
662
663 static void addString (RecWord *p, const char *string, int length)
664 {
665     assert (length > 0);
666     if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
667         addSortString (p, string, length);
668     else
669         addIndexString (p, string, length);
670 }
671
672 static void addIncompleteField (RecWord *p)
673 {
674     const char *b = p->string;
675     int remain = p->length;
676     const char **map = 0;
677
678     if (remain > 0)
679         map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
680
681     while (map)
682     {
683         char buf[IT_MAX_WORD+1];
684         int i, remain;
685
686         /* Skip spaces */
687         while (map && *map && **map == *CHR_SPACE)
688         {
689             remain = p->length - (b - p->string);
690             if (remain > 0)
691                 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
692             else
693                 map = 0;
694         }
695         if (!map)
696             break;
697         i = 0;
698         while (map && *map && **map != *CHR_SPACE)
699         {
700             const char *cp = *map;
701
702             while (i < IT_MAX_WORD && *cp)
703                 buf[i++] = *(cp++);
704             remain = p->length - (b - p->string);
705             if (remain > 0)
706                 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
707             else
708                 map = 0;
709         }
710         if (!i)
711             return;
712         addString (p, buf, i);
713     }
714 }
715
716 static void addCompleteField (RecWord *p)
717 {
718     const char *b = p->string;
719     char buf[IT_MAX_WORD+1];
720     const char **map = 0;
721     int i = 0, remain = p->length;
722
723     if (remain > 0)
724         map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
725
726     while (remain > 0 && i < IT_MAX_WORD)
727     {
728         while (map && *map && **map == *CHR_SPACE)
729         {
730             remain = p->length - (b - p->string);
731             if (remain > 0)
732                 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
733             else
734                 map = 0;
735         }
736         if (!map)
737             break;
738
739         if (i && i < IT_MAX_WORD)
740             buf[i++] = *CHR_SPACE;
741         while (map && *map && **map != *CHR_SPACE)
742         {
743             const char *cp = *map;
744
745             if (i >= IT_MAX_WORD)
746                 break;
747             while (i < IT_MAX_WORD && *cp)
748                 buf[i++] = *(cp++);
749             remain = p->length  - (b - p->string);
750             if (remain > 0)
751                 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
752                                         remain);
753             else
754                 map = 0;
755         }
756     }
757     if (!i)
758         return;
759     addString (p, buf, i);
760 }
761
762 static void addRecordKey (RecWord *p)
763 {
764     if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
765         addCompleteField (p);
766     else
767         addIncompleteField(p);
768 }
769
770 static void flushSortKeys (SYSNO sysno, int cmd)
771 {
772     struct sortKey *sk = sortKeys;
773
774     sortIdx_sysno (sortIdx, sysno);
775     while (sk)
776     {
777         struct sortKey *sk_next = sk->next;
778         sortIdx_type (sortIdx, sk->attrUse);
779         sortIdx_add (sortIdx, sk->string, sk->length);
780         xfree (sk->string);
781         xfree (sk);
782         sk = sk_next;
783     }
784     sortKeys = NULL;
785 }
786
787 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys, 
788                              const char *databaseName)
789 {
790     char attrSet = -1;
791     short attrUse = -1;
792     int seqno = 0;
793     int off = 0;
794
795     if (zebTargetInfo_curDatabase (zti, databaseName))
796     {
797         if (zebTargetInfo_newDatabase (zti, databaseName))
798             abort ();
799     }
800     while (off < reckeys->buf_used)
801     {
802         const char *src = reckeys->buf + off;
803         struct it_key key;
804         int lead, ch;
805     
806         lead = *src++;
807
808         if (!(lead & 1))
809         {
810             memcpy (&attrSet, src, sizeof(attrSet));
811             src += sizeof(attrSet);
812         }
813         if (!(lead & 2))
814         {
815             memcpy (&attrUse, src, sizeof(attrUse));
816             src += sizeof(attrUse);
817         }
818         if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
819             key_flush ();
820         ++ptr_i;
821         key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
822
823         ch = zebTargetInfo_lookupSU (zti, attrSet, attrUse);
824         if (ch < 0)
825             ch = zebTargetInfo_addSU (zti, attrSet, attrUse);
826         assert (ch > 0);
827         ((char*) key_buf) [key_buf_used++] = ch;
828         while (*src)
829             ((char*)key_buf) [key_buf_used++] = *src++;
830         src++;
831         ((char*)key_buf) [key_buf_used++] = '\0';
832         ((char*) key_buf)[key_buf_used++] = cmd;
833
834         if (lead & 60)
835             seqno += ((lead>>2) & 15)-1;
836         else
837         {
838             memcpy (&seqno, src, sizeof(seqno));
839             src += sizeof(seqno);
840         }
841         key.seqno = seqno;
842         key.sysno = sysno;
843         memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
844         key_buf_used += sizeof(key);
845         off = src - reckeys->buf;
846     }
847     assert (off == reckeys->buf_used);
848 }
849
850 static const char **searchRecordKey (struct recKeys *reckeys,
851                                int attrSetS, int attrUseS)
852 {
853     static const char *ws[32];
854     int off = 0;
855     int startSeq = -1;
856     int i;
857     short attrUse;
858     char attrSet;
859     int seqno = 0;
860
861     for (i = 0; i<32; i++)
862         ws[i] = NULL;
863     
864     while (off < reckeys->buf_used)
865     {
866
867         const char *src = reckeys->buf + off;
868         const char *wstart;
869         int lead;
870     
871         lead = *src++;
872
873         if (!(lead & 1))
874         {
875             memcpy (&attrSet, src, sizeof(attrSet));
876             src += sizeof(attrSet);
877         }
878         if (!(lead & 2))
879         {
880             memcpy (&attrUse, src, sizeof(attrUse));
881             src += sizeof(attrUse);
882         }
883         wstart = src;
884         while (*src++)
885             ;
886         if (lead & 60)
887             seqno += ((lead>>2) & 15)-1;
888         else
889         {
890             memcpy (&seqno, src, sizeof(seqno));
891             src += sizeof(seqno);
892         }
893         if (attrUseS == attrUse && attrSetS == attrSet)
894         {
895             int woff;
896
897
898             if (startSeq == -1)
899                 startSeq = seqno;
900             woff = seqno - startSeq;
901             if (woff >= 0 && woff < 31)
902                 ws[woff] = wstart;
903         }
904
905         off = src - reckeys->buf;
906     }
907     assert (off == reckeys->buf_used);
908     return ws;
909 }
910
911 struct file_read_info {
912     off_t file_max;
913     off_t file_offset;
914     off_t file_moffset;
915     int file_more;
916     int fd;
917 };
918
919 static struct file_read_info *file_read_start (int fd)
920 {
921     struct file_read_info *fi = xmalloc (sizeof(*fi));
922
923     fi->fd = fd;
924     fi->file_max = 0;
925     fi->file_moffset = 0;
926     return fi;
927 }
928
929 static void file_read_stop (struct file_read_info *fi)
930 {
931     assert (fi);
932     xfree (fi);
933 }
934
935 static off_t file_seek (void *handle, off_t offset)
936 {
937     struct file_read_info *p = handle;
938     p->file_offset = offset;
939     return lseek (p->fd, offset, SEEK_SET);
940 }
941
942 static off_t file_tell (void *handle)
943 {
944     struct file_read_info *p = handle;
945     return p->file_offset;
946 }
947
948 static int file_read (void *handle, char *buf, size_t count)
949 {
950     struct file_read_info *p = handle;
951     int fd = p->fd;
952     int r;
953     r = read (fd, buf, count);
954     if (r > 0)
955     {
956         p->file_offset += r;
957         if (p->file_offset > p->file_max)
958             p->file_max = p->file_offset;
959     }
960     return r;
961 }
962
963 static void file_begin (void *handle)
964 {
965     struct file_read_info *p = handle;
966
967     p->file_offset = p->file_moffset;
968     if (p->file_moffset)
969         lseek (p->fd, p->file_moffset, SEEK_SET);
970     p->file_more = 0;
971 }
972
973 static void file_end (void *handle, off_t offset)
974 {
975     struct file_read_info *p = handle;
976
977     assert (p->file_more == 0);
978     p->file_more = 1;
979     p->file_moffset = offset;
980 }
981
982 static int atois (const char **s)
983 {
984     int val = 0, c;
985     while ( (c=**s) >= '0' && c <= '9')
986     {
987         val = val*10 + c - '0';
988         ++(*s);
989     }
990     return val;
991 }
992
993 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
994                            const char *fname,
995                            const char *spec)
996 {
997     static char dstBuf[2048];
998     char *dst = dstBuf;
999     const char *s = spec;
1000     static const char **w;
1001     int i;
1002
1003     while (1)
1004     {
1005         while (*s == ' ' || *s == '\t')
1006             s++;
1007         if (!*s)
1008             break;
1009         if (*s == '(')
1010         {
1011             char matchFlag[32];
1012             int attrSet, attrUse;
1013             int first = 1;
1014
1015             s++;
1016             attrSet = atois (&s);
1017             if (*s != ',')
1018             {
1019                 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
1020                       spec, rGroup->groupName ? rGroup->groupName : "none");
1021                 return NULL;
1022             }
1023             s++;
1024             attrUse = atois (&s);
1025             w = searchRecordKey (reckeys, attrSet, attrUse);
1026             assert (w);
1027
1028             if (*s == ')')
1029             {
1030                 for (i = 0; i<32; i++)
1031                     matchFlag[i] = 1;
1032             }
1033             else
1034             {
1035                 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1036                       spec, rGroup->groupName ? rGroup->groupName : "none");
1037                 return NULL;
1038             }
1039             s++;
1040
1041             for (i = 0; i<32; i++)
1042                 if (matchFlag[i] && w[i])
1043                 {
1044                     if (first)
1045                     {
1046                         *dst++ = ' ';
1047                         first = 0;
1048                     }
1049                     strcpy (dst, w[i]);
1050                     dst += strlen(w[i]);
1051                 }
1052             if (first)
1053             {
1054                 logf (LOG_WARN, "Record didn't contain match"
1055                       " fields in (%d,%d)", attrSet, attrUse);
1056                 return NULL;
1057             }
1058         }
1059         else if (*s == '$')
1060         {
1061             int spec_len;
1062             char special[64];
1063             const char *spec_src = NULL;
1064             const char *s1 = ++s;
1065             while (*s1 && *s1 != ' ' && *s1 != '\t')
1066                 s1++;
1067
1068             spec_len = s1 - s;
1069             if (spec_len > 63)
1070                 spec_len = 63;
1071             memcpy (special, s, spec_len);
1072             special[spec_len] = '\0';
1073             s = s1;
1074
1075             if (!strcmp (special, "group"))
1076                 spec_src = rGroup->groupName;
1077             else if (!strcmp (special, "database"))
1078                 spec_src = rGroup->databaseName;
1079             else if (!strcmp (special, "filename"))
1080                 spec_src = fname;
1081             else if (!strcmp (special, "type"))
1082                 spec_src = rGroup->recordType;
1083             else 
1084                 spec_src = NULL;
1085             if (spec_src)
1086             {
1087                 strcpy (dst, spec_src);
1088                 dst += strlen (spec_src);
1089             }
1090         }
1091         else if (*s == '\"' || *s == '\'')
1092         {
1093             int stopMarker = *s++;
1094             char tmpString[64];
1095             int i = 0;
1096
1097             while (*s && *s != stopMarker)
1098             {
1099                 if (i < 63)
1100                     tmpString[i++] = *s++;
1101             }
1102             if (*s)
1103                 s++;
1104             tmpString[i] = '\0';
1105             strcpy (dst, tmpString);
1106             dst += strlen (tmpString);
1107         }
1108         else
1109         {
1110             logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1111                   spec, rGroup->groupName ? rGroup->groupName : "none");
1112             return NULL;
1113         }
1114         *dst++ = 1;
1115     }
1116     if (dst == dstBuf)
1117     {
1118         logf (LOG_WARN, "No match criteria for record %s in group %s",
1119               fname, rGroup->groupName ? rGroup->groupName : "none");
1120         return NULL;
1121     }
1122     *dst = '\0';
1123     return dstBuf;
1124 }
1125
1126 struct recordLogInfo {
1127     const char *fname;
1128     int recordOffset;
1129     struct recordGroup *rGroup;
1130 };
1131      
1132 static void recordLogPreamble (int level, const char *msg, void *info)
1133 {
1134     struct recordLogInfo *p = info;
1135     FILE *outf = log_file ();
1136
1137     if (level & LOG_LOG)
1138         return ;
1139     fprintf (outf, "File %s, offset %d, type %s\n",
1140              p->rGroup->recordType, p->recordOffset, p->fname);
1141     log_event_start (NULL, NULL);
1142 }
1143
1144 static int recordExtract (SYSNO *sysno, const char *fname,
1145                           struct recordGroup *rGroup, int deleteFlag,
1146                           struct file_read_info *fi, RecType recType,
1147                           char *subType)
1148 {
1149     struct recExtractCtrl extractCtrl;
1150     int r;
1151     char *matchStr;
1152     SYSNO sysnotmp;
1153     off_t recordOffset = 0;
1154     Record rec;
1155     struct recordLogInfo logInfo;
1156
1157     if (fi->fd != -1)
1158     {
1159         /* we are going to read from a file, so prepare the extraction */
1160         extractCtrl.fh = fi;
1161         extractCtrl.subType = subType;
1162         extractCtrl.init = wordInit;
1163         extractCtrl.add = addRecordKey;
1164         extractCtrl.dh = rGroup->dh;
1165
1166         reckeys.buf_used = 0;
1167         reckeys.prevAttrUse = -1;
1168         reckeys.prevAttrSet = -1;
1169         reckeys.prevSeqNo = 0;
1170
1171         recordOffset = fi->file_moffset;
1172         extractCtrl.offset = recordOffset;
1173         extractCtrl.readf = file_read;
1174         extractCtrl.seekf = file_seek;
1175         extractCtrl.tellf = file_tell;
1176         extractCtrl.endf = file_end;
1177         extractCtrl.zebra_maps = rGroup->zebra_maps;
1178         extractCtrl.flagShowRecords = !rGroup->flagRw;
1179         if (!rGroup->flagRw)
1180             printf ("File: %s %ld\n", fname, (long) recordOffset);
1181
1182         logInfo.fname = fname;
1183         logInfo.recordOffset = recordOffset;
1184         logInfo.rGroup = rGroup;
1185         log_event_start (recordLogPreamble, &logInfo);
1186
1187         r = (*recType->extract)(&extractCtrl);
1188
1189         log_event_start (NULL, NULL);
1190
1191         if (r)      
1192         {
1193             /* error occured during extraction ... */
1194             if (rGroup->flagRw &&
1195                 records_processed < rGroup->fileVerboseLimit)
1196             {
1197                 logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
1198                       fname, (long) recordOffset, r);
1199             }
1200             return 0;
1201         }
1202         if (reckeys.buf_used == 0)
1203         {
1204             /* the extraction process returned no information - the record
1205                is probably empty - unless flagShowRecords is in use */
1206             if (!rGroup->flagRw)
1207                 return 1;
1208             logf (LOG_WARN, "No keys generated for file %s", fname);
1209             logf (LOG_WARN, " The file is probably empty");
1210             return 0;
1211         }
1212     }
1213
1214     /* perform match if sysno not known and if match criteria is specified */
1215        
1216     matchStr = NULL;
1217     if (!sysno) 
1218     {
1219         sysnotmp = 0;
1220         sysno = &sysnotmp;
1221         if (rGroup->recordId && *rGroup->recordId)
1222         {
1223             char *rinfo;
1224         
1225             matchStr = fileMatchStr (&reckeys, rGroup, fname, 
1226                                      rGroup->recordId);
1227             if (matchStr)
1228             {
1229                 rinfo = dict_lookup (matchDict, matchStr);
1230                 if (rinfo)
1231                     memcpy (sysno, rinfo+1, sizeof(*sysno));
1232             }
1233             else
1234             {
1235                 logf (LOG_WARN, "Bad match criteria");
1236                 return 0;
1237             }
1238         }
1239     }
1240
1241     if (! *sysno)
1242     {
1243         /* new record */
1244         if (deleteFlag)
1245         {
1246             logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1247                   fname, (long) recordOffset);
1248             logf (LOG_WARN, "cannot delete record above (seems new)");
1249             return 1;
1250         }
1251         if (records_processed < rGroup->fileVerboseLimit)
1252             logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1253                   fname, (long) recordOffset);
1254         rec = rec_new (records);
1255         *sysno = rec->sysno;
1256
1257         if (matchStr)
1258         {
1259             dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1260         }
1261         flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1262         flushSortKeys (*sysno, 1);
1263
1264         records_inserted++;
1265     }
1266     else
1267     {
1268         /* record already exists */
1269         struct recKeys delkeys;
1270
1271         rec = rec_get (records, *sysno);
1272         assert (rec);
1273         delkeys.buf_used = rec->size[recInfo_delKeys];
1274         delkeys.buf = rec->info[recInfo_delKeys];
1275         flushSortKeys (*sysno, 0);
1276         flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
1277         if (deleteFlag)
1278         {
1279             /* record going to be deleted */
1280             if (!delkeys.buf_used)
1281             {
1282                 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1283                       fname, (long) recordOffset);
1284                 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1285             }
1286             else
1287             {
1288                 if (records_processed < rGroup->fileVerboseLimit)
1289                     logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1290                           fname, (long) recordOffset);
1291                 records_deleted++;
1292                 if (matchStr)
1293                     dict_delete (matchDict, matchStr);
1294                 rec_del (records, &rec);
1295             }
1296             logRecord (0);
1297             return 1;
1298         }
1299         else
1300         {
1301             /* record going to be updated */
1302             if (!delkeys.buf_used)
1303             {
1304                 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1305                       fname, (long) recordOffset);
1306                 logf (LOG_WARN, "cannot update file above, storeKeys false");
1307             }
1308             else
1309             {
1310                 if (records_processed < rGroup->fileVerboseLimit)
1311                     logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1312                           fname, (long) recordOffset);
1313                 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName); 
1314                 records_updated++;
1315             }
1316         }
1317     }
1318     /* update file type */
1319     xfree (rec->info[recInfo_fileType]);
1320     rec->info[recInfo_fileType] =
1321         rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1322
1323     /* update filename */
1324     xfree (rec->info[recInfo_filename]);
1325     rec->info[recInfo_filename] =
1326         rec_strdup (fname, &rec->size[recInfo_filename]);
1327
1328     /* update delete keys */
1329     xfree (rec->info[recInfo_delKeys]);
1330     if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1331     {
1332 #if 1
1333         rec->size[recInfo_delKeys] = reckeys.buf_used;
1334         rec->info[recInfo_delKeys] = reckeys.buf;
1335         reckeys.buf = NULL;
1336         reckeys.buf_max = 0;
1337 #else
1338         rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1339         rec->size[recInfo_delKeys] = reckeys.buf_used;
1340         memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1341                 rec->size[recInfo_delKeys]);
1342 #endif
1343     }
1344     else
1345     {
1346         rec->info[recInfo_delKeys] = NULL;
1347         rec->size[recInfo_delKeys] = 0;
1348     }
1349
1350     /* update store data */
1351     xfree (rec->info[recInfo_storeData]);
1352     if (rGroup->flagStoreData == 1)
1353     {
1354         int size = fi->file_moffset - recordOffset; 
1355         if (!size)
1356             size = fi->file_max - recordOffset;
1357         rec->size[recInfo_storeData] = size;
1358         rec->info[recInfo_storeData] = xmalloc (size);
1359         if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1360         {
1361             logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1362                   (long) recordOffset);
1363             exit (1);
1364         }
1365         if (read (fi->fd, rec->info[recInfo_storeData], size) < size)
1366         {
1367             logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1368                   fi->file_max, fname);
1369             exit (1);
1370         }
1371     }
1372     else
1373     {
1374         rec->info[recInfo_storeData] = NULL;
1375         rec->size[recInfo_storeData] = 0;
1376     }
1377     /* update database name */
1378     xfree (rec->info[recInfo_databaseName]);
1379     rec->info[recInfo_databaseName] =
1380         rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]); 
1381
1382     /* update offset */
1383     xfree (rec->info[recInfo_offset]);
1384
1385     rec->size[recInfo_offset] = sizeof(recordOffset);
1386     rec->info[recInfo_offset] = xmalloc (sizeof(recordOffset));
1387     memcpy (rec->info[recInfo_offset], &recordOffset, sizeof(recordOffset));
1388     
1389     /* commit this record */
1390     rec_put (records, &rec);
1391     logRecord (0);
1392     return 1;
1393 }
1394
1395 int fileExtract (SYSNO *sysno, const char *fname, 
1396                  const struct recordGroup *rGroupP, int deleteFlag)
1397 {
1398     int r, i, fd;
1399     char gprefix[128];
1400     char ext[128];
1401     char ext_res[128];
1402     char subType[128];
1403     RecType recType;
1404     struct recordGroup rGroupM;
1405     struct recordGroup *rGroup = &rGroupM;
1406     struct file_read_info *fi;
1407
1408     memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1409    
1410     if (!rGroup->groupName || !*rGroup->groupName)
1411         *gprefix = '\0';
1412     else
1413         sprintf (gprefix, "%s.", rGroup->groupName);
1414
1415     logf (LOG_DEBUG, "fileExtract %s", fname);
1416
1417     /* determine file extension */
1418     for (i = strlen(fname); --i >= 0; )
1419         if (fname[i] == '/')
1420         {
1421             strcpy (ext, "");
1422             break;
1423         }
1424         else if (fname[i] == '.')
1425         {
1426             strcpy (ext, fname+i+1);
1427             break;
1428         }
1429     /* determine file type - depending on extension */
1430     if (!rGroup->recordType)
1431     {
1432         sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1433         if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1434         {
1435             sprintf (ext_res, "%srecordType", gprefix);
1436             if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1437             {
1438                 if (records_processed < rGroup->fileVerboseLimit)
1439                     logf (LOG_LOG, "? %s", fname);
1440                 return 0;
1441             }
1442         }
1443     }
1444     if (!rGroup->recordType)
1445     {
1446         if (records_processed < rGroup->fileVerboseLimit)
1447             logf (LOG_LOG, "? record %s", fname);
1448         return 0;
1449     }
1450     if (!(recType = recType_byName (rGroup->recordType, subType)))
1451     {
1452         logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1453         return 0;
1454     }
1455
1456     /* determine match criteria */
1457     if (!rGroup->recordId)
1458     {
1459         sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1460         rGroup->recordId = res_get (common_resource, ext_res);
1461     }
1462
1463     /* determine database name */
1464     if (!rGroup->databaseName)
1465     {
1466         sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1467         if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1468         {
1469             sprintf (ext_res, "%sdatabase", gprefix);
1470             rGroup->databaseName = res_get (common_resource, ext_res);
1471         }
1472     }
1473     if (!rGroup->databaseName)
1474         rGroup->databaseName = "Default";
1475
1476     if (rGroup->flagStoreData == -1)
1477     {
1478         const char *sval;
1479         sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1480         if (!(sval = res_get (common_resource, ext_res)))
1481         {
1482             sprintf (ext_res, "%sstoreData", gprefix);
1483             sval = res_get (common_resource, ext_res);
1484         }
1485         if (sval)
1486             rGroup->flagStoreData = atoi (sval);
1487     }
1488     if (rGroup->flagStoreData == -1)
1489         rGroup->flagStoreData = 0;
1490
1491     if (rGroup->flagStoreKeys == -1)
1492     {
1493         const char *sval;
1494
1495         sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1496         if (!(sval = res_get (common_resource, ext_res)))
1497         {
1498             sprintf (ext_res, "%sstoreKeys", gprefix);
1499             sval = res_get (common_resource, ext_res);
1500         }
1501         if (sval)
1502             rGroup->flagStoreKeys = atoi (sval);
1503     }
1504     if (rGroup->flagStoreKeys == -1)
1505         rGroup->flagStoreKeys = 0;
1506
1507     if (sysno && deleteFlag)
1508         fd = -1;
1509     else
1510     {
1511         if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1512         {
1513             logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1514             return 0;
1515         }
1516     }
1517     fi = file_read_start (fd);
1518     do
1519     {
1520         file_begin (fi);
1521         r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1522                            recType, subType);
1523     } while (r && !sysno && fi->file_more);
1524     file_read_stop (fi);
1525     if (fd != -1)
1526         close (fd);
1527     return r;
1528 }
1529