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