2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.67 1996-11-15 15:02:14 adam
8 * Minor changes regarding logging.
10 * Revision 1.66 1996/11/14 09:52:21 adam
11 * Strings in record keys bound by IT_MAX_WORD.
13 * Revision 1.65 1996/11/14 08:57:56 adam
14 * Reduction of storeKeys area.
16 * Revision 1.64 1996/11/08 11:10:16 adam
17 * Buffers used during file match got bigger.
18 * Compressed ISAM support everywhere.
19 * Bug fixes regarding masking characters in queries.
20 * Redesigned Regexp-2 queries.
22 * Revision 1.63 1996/10/29 14:09:39 adam
23 * Use of cisam system - enabled if setting isamc is 1.
25 * Revision 1.62 1996/10/11 10:57:01 adam
26 * New module recctrl. Used to manage records (extract/retrieval).
27 * Several files have been moved to the recctrl sub directory.
29 * Revision 1.61 1996/06/06 12:08:37 quinn
30 * Added showRecord function
32 * Revision 1.60 1996/06/04 10:18:12 adam
33 * Search/scan uses character mapping module.
35 * Revision 1.59 1996/05/14 15:47:07 adam
36 * Cleanup of various buffer size entities.
38 * Revision 1.58 1996/05/14 06:16:38 adam
39 * Compact use/set bytes used in search service.
41 * Revision 1.57 1996/05/13 14:23:04 adam
42 * Work on compaction of set/use bytes in dictionary.
44 * Revision 1.56 1996/05/09 09:54:42 adam
45 * Server supports maps from one logical attributes to a list of physical
47 * The extraction process doesn't make space consuming 'any' keys.
49 * Revision 1.55 1996/05/09 07:28:55 quinn
50 * Work towards phrases and multiple registers
52 * Revision 1.54 1996/05/01 13:46:35 adam
53 * First work on multiple records in one file.
54 * New option, -offset, to the "unread" command in the filter module.
56 * Revision 1.53 1996/04/26 12:09:43 adam
57 * Added a few comments.
59 * Revision 1.52 1996/04/25 13:27:57 adam
60 * Function recordExtract modified so that files with no keys (possibly empty)
63 * Revision 1.51 1996/03/19 11:08:42 adam
64 * Bug fix: Log preamble wasn't always turned off after recordExtract.
66 * Revision 1.50 1996/02/12 18:45:36 adam
67 * New fileVerboseFlag in record group control.
69 * Revision 1.49 1996/02/05 12:29:57 adam
70 * Logging reduced a bit.
71 * The remaining running time is estimated during register merge.
73 * Revision 1.48 1996/02/01 20:53:26 adam
74 * The temporary per-record keys are compacted a little, and duplication
75 * of the per-records keys are avoided when they are saved in the record
78 * Revision 1.47 1996/01/17 14:57:48 adam
79 * Prototype changed for reader functions in extract/retrieve. File
80 * is identified by 'void *' instead of 'int.
82 * Revision 1.46 1995/12/15 14:57:16 adam
85 * Revision 1.45 1995/12/15 12:37:41 adam
86 * In addRecordKeyAny: Writes key only when attrSet != -1.
88 * Revision 1.44 1995/12/12 16:00:54 adam
89 * System call sync(2) used after update/commit.
90 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
93 * Revision 1.43 1995/12/11 09:12:46 adam
94 * The rec_get function returns NULL if record doesn't exist - will
95 * happen in the server if the result set records have been deleted since
96 * the creation of the set (i.e. the search).
97 * The server saves a result temporarily if it is 'volatile', i.e. the
98 * set is register dependent.
100 * Revision 1.42 1995/12/07 17:38:46 adam
101 * Work locking mechanisms for concurrent updates/commit.
103 * Revision 1.41 1995/12/06 16:06:42 adam
104 * Better diagnostics. Work on 'real' dictionary deletion.
106 * Revision 1.40 1995/12/05 16:57:40 adam
107 * More work on regular patterns.
109 * Revision 1.39 1995/12/05 13:20:18 adam
110 * Bug fix: file_read sometimes returned early EOF.
112 * Revision 1.38 1995/12/04 17:59:21 adam
113 * More work on regular expression conversion.
115 * Revision 1.37 1995/12/04 14:22:27 adam
116 * Extra arg to recType_byName.
117 * Started work on new regular expression parsed input to
118 * structured records.
120 * Revision 1.36 1995/11/30 08:34:29 adam
121 * Started work on commit facility.
122 * Changed a few malloc/free to xmalloc/xfree.
124 * Revision 1.35 1995/11/28 14:26:21 adam
125 * Bug fix: recordId with constant wasn't right.
126 * Bug fix: recordId dictionary entry wasn't deleted when needed.
128 * Revision 1.34 1995/11/28 09:09:38 adam
129 * Zebra config renamed.
130 * Use setting 'recordId' to identify record now.
131 * Bug fix in recindex.c: rec_release_blocks was invokeded even
132 * though the blocks were already released.
133 * File traversal properly deletes records when needed.
135 * Revision 1.33 1995/11/27 09:56:20 adam
136 * Record info elements better enumerated. Internal store of records.
138 * Revision 1.32 1995/11/25 10:24:05 adam
139 * More record fields - they are enumerated now.
140 * New options: flagStoreData flagStoreKey.
142 * Revision 1.31 1995/11/24 11:31:35 adam
143 * Commands add & del read filenames from stdin if source directory is
145 * Match criteria supports 'constant' strings.
147 * Revision 1.30 1995/11/22 17:19:16 adam
148 * Record management uses the bfile system.
150 * Revision 1.29 1995/11/21 15:01:14 adam
151 * New general match criteria implemented.
152 * New feature: document groups.
154 * Revision 1.28 1995/11/21 09:20:30 adam
155 * Yet more work on record match.
157 * Revision 1.27 1995/11/20 16:59:45 adam
158 * New update method: the 'old' keys are saved for each records.
160 * Revision 1.26 1995/11/20 11:56:24 adam
161 * Work on new traversal.
163 * Revision 1.25 1995/11/16 15:34:54 adam
164 * Uses new record management system in both indexer and server.
166 * Revision 1.24 1995/11/15 19:13:08 adam
167 * Work on record management.
169 * Revision 1.23 1995/10/27 14:00:10 adam
170 * Implemented detection of database availability.
172 * Revision 1.22 1995/10/17 18:02:07 adam
173 * New feature: databases. Implemented as prefix to words in dictionary.
175 * Revision 1.21 1995/10/10 12:24:38 adam
176 * Temporary sort files are compressed.
178 * Revision 1.20 1995/10/06 13:52:05 adam
179 * Bug fixes. Handler may abort further scanning.
181 * Revision 1.19 1995/10/04 12:55:16 adam
182 * Bug fix in ranked search. Use=Any keys inserted.
184 * Revision 1.18 1995/10/04 09:37:08 quinn
187 * Revision 1.17 1995/10/03 14:28:57 adam
188 * Buffered read in extract works.
190 * Revision 1.16 1995/10/03 14:28:45 adam
191 * Work on more effecient read handler in extract.
193 * Revision 1.15 1995/10/02 15:42:53 adam
194 * Extract uses file descriptors instead of FILE pointers.
196 * Revision 1.14 1995/10/02 15:29:13 adam
197 * More logging in file_extract.
199 * Revision 1.13 1995/09/29 14:01:39 adam
202 * Revision 1.12 1995/09/28 14:22:56 adam
203 * Sort uses smaller temporary files.
205 * Revision 1.11 1995/09/28 12:10:31 adam
206 * Bug fixes. Field prefix used in queries.
208 * Revision 1.10 1995/09/28 09:19:41 adam
209 * xfree/xmalloc used everywhere.
210 * Extract/retrieve method seems to work for text records.
212 * Revision 1.9 1995/09/27 12:22:28 adam
213 * More work on extract in record control.
214 * Field name is not in isam keys but in prefix in dictionary words.
216 * Revision 1.8 1995/09/14 07:48:22 adam
217 * Record control management.
219 * Revision 1.7 1995/09/11 13:09:32 adam
220 * More work on relevance feedback.
222 * Revision 1.6 1995/09/08 14:52:27 adam
223 * Minor changes. Dictionary is lower case now.
225 * Revision 1.5 1995/09/06 16:11:16 adam
226 * Option: only one word key per file.
228 * Revision 1.4 1995/09/05 15:28:39 adam
229 * More work on search engine.
231 * Revision 1.3 1995/09/04 12:33:41 adam
232 * Various cleanup. YAZ util used instead.
234 * Revision 1.2 1995/09/04 09:10:34 adam
235 * More work on index add/del/update.
236 * Merge sort implemented.
237 * Initial work on z39 server.
239 * Revision 1.1 1995/09/01 14:06:35 adam
240 * Split of work into more files.
253 static Dict matchDict;
255 static Records records = NULL;
257 static char **key_buf;
258 static size_t ptr_top;
260 static size_t key_buf_used;
261 static int key_file_no;
263 static int records_inserted = 0;
264 static int records_updated = 0;
265 static int records_deleted = 0;
266 static int records_processed = 0;
268 static ZebTargetInfo *zti = NULL;
270 static void logRecord (int showFlag)
274 if (showFlag || !(records_processed % 1000))
276 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
277 records_processed, records_inserted, records_updated,
282 void key_open (int mem)
285 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
288 key_buf = xmalloc (mem);
289 ptr_top = mem/sizeof(char*);
295 if (!(matchDict = dict_open (GMATCH_DICT, 50, 1)))
297 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
301 records = rec_open (1);
303 zti = zebTargetInfo_open (records, 1);
313 void encode_key_init (struct encode_info *i)
319 char *encode_key_int (int d, char *bp)
328 else if (d <= 4194303)
330 *bp++ = 128 + (d>>16);
331 *bp++ = (d>>8) & 255;
336 *bp++ = 192 + (d>>24);
337 *bp++ = (d>>16) & 255;
338 *bp++ = (d>>8) & 255;
344 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
349 while ((*bp++ = *k++))
351 memcpy (&key, k+1, sizeof(struct it_key));
352 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
353 if (i->sysno != key.sysno)
355 i->sysno = key.sysno;
358 bp = encode_key_int (key.seqno - i->seqno, bp);
359 i->seqno = key.seqno;
360 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
362 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
370 static int key_y_len;
372 static int key_y_compare (const void *p1, const void *p2)
376 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
377 *(char**) p2 + key_y_len + 1)))
379 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
382 static int key_x_compare (const void *p1, const void *p2)
384 return strcmp (*(char**) p1, *(char**) p2);
388 void key_flush (void)
393 struct encode_info encode_info;
402 logf (LOG_LOG, "sorting section %d", key_file_no);
404 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
405 getFnameTmp (out_fname, key_file_no);
407 if (!(outf = fopen (out_fname, "w")))
409 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
412 logf (LOG_LOG, "writing section %d", key_file_no);
413 prevcp = cp = key_buf[ptr_top-ptr_i];
415 encode_key_init (&encode_info);
416 encode_key_write (cp, &encode_info, outf);
419 cp = key_buf[ptr_top-ptr_i];
420 if (strcmp (cp, prevcp))
422 encode_key_init (&encode_info);
423 encode_key_write (cp, &encode_info, outf);
427 encode_key_write (cp + strlen(cp), &encode_info, outf);
430 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
431 getFnameTmp (out_fname, key_file_no);
433 if (!(outf = fopen (out_fname, "w")))
435 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
438 logf (LOG_LOG, "writing section %d", key_file_no);
440 prevcp = key_buf[ptr_top-i];
442 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
444 key_y_len = strlen(prevcp)+1;
446 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
447 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
449 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
450 sizeof(char*), key_y_compare);
451 cp = key_buf[ptr_top-ptr_i];
453 encode_key_init (&encode_info);
454 encode_key_write (cp, &encode_info, outf);
457 cp = key_buf[ptr_top-ptr_i];
458 encode_key_write (cp+key_y_len, &encode_info, outf);
462 prevcp = key_buf[ptr_top-ptr_i];
467 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
470 logf (LOG_LOG, "finished section %d", key_file_no);
480 zebTargetInfo_close (zti, 1);
482 rec_close (&records);
483 dict_close (matchDict);
489 static void wordInit (RecWord *p)
493 p->which = Word_String;
505 static void addRecordKey (const RecWord *p)
514 if (reckeys.buf_used+1024 > reckeys.buf_max)
518 b = xmalloc (reckeys.buf_max += 128000);
519 if (reckeys.buf_used > 0)
520 memcpy (b, reckeys.buf, reckeys.buf_used);
524 dst = reckeys.buf + reckeys.buf_used;
526 attrSet = p->attrSet;
527 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
530 reckeys.prevAttrSet = attrSet;
531 attrUse = p->attrUse;
532 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
535 reckeys.prevAttrUse = attrUse;
537 diff = 1 + p->seqno - reckeys.prevSeqNo;
538 if (diff >= 1 && diff <= 15)
543 reckeys.prevSeqNo = p->seqno;
549 memcpy (dst, &attrSet, sizeof(attrSet));
550 dst += sizeof(attrSet);
554 memcpy (dst, &attrUse, sizeof(attrUse));
555 dst += sizeof(attrUse);
568 for (i = 0; p->u.string[i] && i < IT_MAX_WORD-3; i++)
569 *dst++ = p->u.string[i];
574 memcpy (dst, &p->seqno, sizeof(p->seqno));
575 dst += sizeof(p->seqno);
577 reckeys.buf_used = dst - reckeys.buf;
580 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
581 const char *databaseName)
588 if (zebTargetInfo_curDatabase (zti, databaseName))
590 if (zebTargetInfo_newDatabase (zti, databaseName))
593 while (off < reckeys->buf_used)
595 const char *src = reckeys->buf + off;
603 memcpy (&attrSet, src, sizeof(attrSet));
604 src += sizeof(attrSet);
608 memcpy (&attrUse, src, sizeof(attrUse));
609 src += sizeof(attrUse);
611 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
614 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
616 ch = zebTargetInfo_lookupSU (zti, attrSet, attrUse);
618 ch = zebTargetInfo_addSU (zti, attrSet, attrUse);
620 ((char*) key_buf) [key_buf_used++] = ch;
622 ((char*)key_buf) [key_buf_used++] = *src++;
624 ((char*)key_buf) [key_buf_used++] = '\0';
625 ((char*) key_buf)[key_buf_used++] = cmd;
628 seqno += ((lead>>2) & 15)-1;
631 memcpy (&seqno, src, sizeof(seqno));
632 src += sizeof(seqno);
636 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
637 key_buf_used += sizeof(key);
638 off = src - reckeys->buf;
640 assert (off == reckeys->buf_used);
643 static const char **searchRecordKey (struct recKeys *reckeys,
644 int attrSetS, int attrUseS)
646 static const char *ws[32];
651 for (i = 0; i<32; i++)
654 while (off < reckeys->buf_used)
656 const char *src = reckeys->buf + off;
662 memcpy (&attrSet, src, sizeof(attrSet));
663 src += sizeof(attrSet);
665 memcpy (&attrUse, src, sizeof(attrUse));
666 src += sizeof(attrUse);
672 memcpy (&seqno, src, sizeof(seqno));
673 src += sizeof(seqno);
676 logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
678 if (attrUseS == attrUse && attrSetS == attrSet)
685 woff = seqno - startSeq;
686 if (woff >= 0 && woff < 31)
690 off = src - reckeys->buf;
692 assert (off == reckeys->buf_used);
696 struct file_read_info {
704 static struct file_read_info *file_read_start (int fd)
706 struct file_read_info *fi = xmalloc (sizeof(*fi));
710 fi->file_moffset = 0;
714 static void file_read_stop (struct file_read_info *fi)
720 static off_t file_seek (void *handle, off_t offset)
722 struct file_read_info *p = handle;
723 p->file_offset = offset;
724 return lseek (p->fd, offset, SEEK_SET);
727 static int file_read (void *handle, char *buf, size_t count)
729 struct file_read_info *p = handle;
732 r = read (fd, buf, count);
736 if (p->file_offset > p->file_max)
737 p->file_max = p->file_offset;
742 static void file_begin (void *handle)
744 struct file_read_info *p = handle;
746 p->file_offset = p->file_moffset;
748 lseek (p->fd, p->file_moffset, SEEK_SET);
752 static void file_end (void *handle, off_t offset)
754 struct file_read_info *p = handle;
756 assert (p->file_more == 0);
758 p->file_moffset = offset;
761 static int atois (const char **s)
764 while ( (c=**s) >= '0' && c <= '9')
766 val = val*10 + c - '0';
772 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
776 static char dstBuf[2048];
778 const char *s = spec;
779 static const char **w;
784 while (*s == ' ' || *s == '\t')
791 int attrSet, attrUse;
795 attrSet = atois (&s);
798 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
799 spec, rGroup->groupName ? rGroup->groupName : "none");
803 attrUse = atois (&s);
804 w = searchRecordKey (reckeys, attrSet, attrUse);
809 for (i = 0; i<32; i++)
814 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
815 spec, rGroup->groupName ? rGroup->groupName : "none");
820 for (i = 0; i<32; i++)
821 if (matchFlag[i] && w[i])
833 logf (LOG_WARN, "Record didn't contain match"
834 " fields in (%d,%d)", attrSet, attrUse);
842 const char *spec_src = NULL;
843 const char *s1 = ++s;
844 while (*s1 && *s1 != ' ' && *s1 != '\t')
850 memcpy (special, s, spec_len);
851 special[spec_len] = '\0';
854 if (!strcmp (special, "group"))
855 spec_src = rGroup->groupName;
856 else if (!strcmp (special, "database"))
857 spec_src = rGroup->databaseName;
858 else if (!strcmp (special, "filename"))
860 else if (!strcmp (special, "type"))
861 spec_src = rGroup->recordType;
866 strcpy (dst, spec_src);
867 dst += strlen (spec_src);
870 else if (*s == '\"' || *s == '\'')
872 int stopMarker = *s++;
876 while (*s && *s != stopMarker)
879 tmpString[i++] = *s++;
884 strcpy (dst, tmpString);
885 dst += strlen (tmpString);
889 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
890 spec, rGroup->groupName ? rGroup->groupName : "none");
897 logf (LOG_WARN, "No match criteria for record %s in group %s",
898 fname, rGroup->groupName ? rGroup->groupName : "none");
904 struct recordLogInfo {
907 struct recordGroup *rGroup;
910 static void recordLogPreamble (int level, const char *msg, void *info)
912 struct recordLogInfo *p = info;
913 FILE *outf = log_file ();
918 fprintf (outf, "%s of ", p->op);
919 fprintf (outf, "%s type %s\n", p->rGroup->recordType, p->fname);
920 log_event_start (NULL, NULL);
923 static int recordExtract (SYSNO *sysno, const char *fname,
924 struct recordGroup *rGroup, int deleteFlag,
925 struct file_read_info *fi, RecType recType,
928 struct recExtractCtrl extractCtrl;
932 off_t recordOffset = 0;
934 struct recordLogInfo logInfo;
936 logInfo.fname = fname;
938 logInfo.rGroup = rGroup;
939 log_event_start (recordLogPreamble, &logInfo);
943 /* we are going to read from a file, so prepare the extraction */
945 extractCtrl.subType = subType;
946 extractCtrl.init = wordInit;
947 extractCtrl.add = addRecordKey;
949 reckeys.buf_used = 0;
950 reckeys.prevAttrUse = -1;
951 reckeys.prevAttrSet = -1;
952 reckeys.prevSeqNo = 0;
954 recordOffset = fi->file_moffset;
955 extractCtrl.offset = recordOffset;
956 extractCtrl.readf = file_read;
957 extractCtrl.seekf = file_seek;
958 extractCtrl.endf = file_end;
959 extractCtrl.map_chrs_input = map_chrs_input;
960 extractCtrl.flagShowRecords = rGroup->flagShowRecords;
961 if (rGroup->flagShowRecords)
962 printf ("File: %s %ld\n", fname, (long) recordOffset);
963 r = (*recType->extract)(&extractCtrl);
967 /* error occured during extraction ... */
968 if (!rGroup->flagShowRecords)
969 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
972 if (reckeys.buf_used == 0)
974 /* the extraction process returned no information - the record
976 if (!rGroup->flagShowRecords)
978 logf (LOG_WARN, "No keys generated for file %s", fname);
979 logf (LOG_WARN, " The file is probably empty");
985 /* perform match if sysno not known and if match criteria is specified */
992 if (rGroup->recordId && *rGroup->recordId)
996 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1000 rinfo = dict_lookup (matchDict, matchStr);
1002 memcpy (sysno, rinfo+1, sizeof(*sysno));
1006 logf (LOG_WARN, "Bad match criteria");
1017 logf (LOG_LOG, "Cannot delete new record");
1021 if (rGroup->fileVerboseFlag)
1022 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1023 fname, (long) recordOffset);
1024 rec = rec_new (records);
1025 *sysno = rec->sysno;
1029 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1031 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1037 /* record already exists */
1038 struct recKeys delkeys;
1040 rec = rec_get (records, *sysno);
1042 delkeys.buf_used = rec->size[recInfo_delKeys];
1043 delkeys.buf = rec->info[recInfo_delKeys];
1044 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
1047 /* record going to be deleted */
1048 logInfo.op = "delete";
1049 if (!delkeys.buf_used)
1051 logf (LOG_WARN, "cannot delete; storeKeys false");
1055 if (rGroup->fileVerboseFlag)
1056 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1057 fname, (long) recordOffset);
1060 dict_delete (matchDict, matchStr);
1061 rec_del (records, &rec);
1068 /* record going to be updated */
1069 logInfo.op = "update";
1070 if (!delkeys.buf_used)
1072 logf (LOG_WARN, "cannot update; storeKeys false");
1076 if (rGroup->fileVerboseFlag)
1077 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1078 fname, (long) recordOffset);
1079 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1084 /* update file type */
1085 xfree (rec->info[recInfo_fileType]);
1086 rec->info[recInfo_fileType] =
1087 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1089 /* update filename */
1090 xfree (rec->info[recInfo_filename]);
1091 rec->info[recInfo_filename] =
1092 rec_strdup (fname, &rec->size[recInfo_filename]);
1094 /* update delete keys */
1095 xfree (rec->info[recInfo_delKeys]);
1096 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1099 rec->size[recInfo_delKeys] = reckeys.buf_used;
1100 rec->info[recInfo_delKeys] = reckeys.buf;
1102 reckeys.buf_max = 0;
1104 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1105 rec->size[recInfo_delKeys] = reckeys.buf_used;
1106 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1107 rec->size[recInfo_delKeys]);
1112 rec->info[recInfo_delKeys] = NULL;
1113 rec->size[recInfo_delKeys] = 0;
1116 /* update store data */
1117 xfree (rec->info[recInfo_storeData]);
1118 if (rGroup->flagStoreData == 1)
1120 rec->size[recInfo_storeData] = fi->file_max;
1121 rec->info[recInfo_storeData] = xmalloc (fi->file_max);
1122 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1124 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1125 (long) recordOffset);
1128 if (read (fi->fd, rec->info[recInfo_storeData], fi->file_max)
1131 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1132 fi->file_max, fname);
1138 rec->info[recInfo_storeData] = NULL;
1139 rec->size[recInfo_storeData] = 0;
1141 /* update database name */
1142 xfree (rec->info[recInfo_databaseName]);
1143 rec->info[recInfo_databaseName] =
1144 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1147 xfree (rec->info[recInfo_offset]);
1149 rec->size[recInfo_offset] = sizeof(recordOffset);
1150 rec->info[recInfo_offset] = xmalloc (sizeof(recordOffset));
1151 memcpy (rec->info[recInfo_offset], &recordOffset, sizeof(recordOffset));
1153 /* commit this record */
1154 rec_put (records, &rec);
1159 int fileExtract (SYSNO *sysno, const char *fname,
1160 const struct recordGroup *rGroupP, int deleteFlag)
1168 struct recordGroup rGroupM;
1169 struct recordGroup *rGroup = &rGroupM;
1170 struct file_read_info *fi;
1172 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1174 if (!rGroup->groupName || !*rGroup->groupName)
1177 sprintf (gprefix, "%s.", rGroup->groupName);
1179 logf (LOG_DEBUG, "fileExtract %s", fname);
1181 /* determine file extension */
1182 for (i = strlen(fname); --i >= 0; )
1183 if (fname[i] == '/')
1188 else if (fname[i] == '.')
1190 strcpy (ext, fname+i+1);
1193 /* determine file type - depending on extension */
1194 if (!rGroup->recordType)
1196 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1197 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1199 sprintf (ext_res, "%srecordType", gprefix);
1200 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1202 if (rGroup->fileVerboseFlag)
1203 logf (LOG_LOG, "? %s", fname);
1208 if (!rGroup->recordType)
1210 if (rGroup->fileVerboseFlag)
1211 logf (LOG_LOG, "? record %s", fname);
1214 if (!(recType = recType_byName (rGroup->recordType, subType)))
1216 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1220 /* determine match criteria */
1221 if (!rGroup->recordId)
1223 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1224 rGroup->recordId = res_get (common_resource, ext_res);
1227 /* determine database name */
1228 if (!rGroup->databaseName)
1230 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1231 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1233 sprintf (ext_res, "%sdatabase", gprefix);
1234 rGroup->databaseName = res_get (common_resource, ext_res);
1237 if (!rGroup->databaseName)
1238 rGroup->databaseName = "Default";
1240 if (rGroup->flagStoreData == -1)
1243 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1244 if (!(sval = res_get (common_resource, ext_res)))
1246 sprintf (ext_res, "%sstoreData", gprefix);
1247 sval = res_get (common_resource, ext_res);
1250 rGroup->flagStoreData = atoi (sval);
1252 if (rGroup->flagStoreData == -1)
1253 rGroup->flagStoreData = 0;
1255 if (rGroup->flagStoreKeys == -1)
1259 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1260 if (!(sval = res_get (common_resource, ext_res)))
1262 sprintf (ext_res, "%sstoreKeys", gprefix);
1263 sval = res_get (common_resource, ext_res);
1266 rGroup->flagStoreKeys = atoi (sval);
1268 if (rGroup->flagStoreKeys == -1)
1269 rGroup->flagStoreKeys = 0;
1271 if (sysno && deleteFlag)
1275 if ((fd = open (fname, O_RDONLY)) == -1)
1277 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1281 fi = file_read_start (fd);
1285 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1287 } while (r && !sysno && fi->file_more);
1288 log_event_start (NULL, NULL);
1289 file_read_stop (fi);