+ struct file_read_info *fi = xmalloc (sizeof(*fi));
+
+ fi->fd = fd;
+ fi->file_max = 0;
+ fi->file_moffset = 0;
+ return fi;
+}
+
+static void file_read_stop (struct file_read_info *fi)
+{
+ assert (fi);
+ xfree (fi);
+}
+
+static off_t file_seek (void *handle, off_t offset)
+{
+ struct file_read_info *p = handle;
+ p->file_offset = offset;
+ return lseek (p->fd, offset, SEEK_SET);
+}
+
+static int file_read (void *handle, char *buf, size_t count)
+{
+ struct file_read_info *p = handle;
+ int fd = p->fd;
+ int r;
+ r = read (fd, buf, count);
+ if (r > 0)
+ {
+ p->file_offset += r;
+ if (p->file_offset > p->file_max)
+ p->file_max = p->file_offset;
+ }
+ return r;
+}
+
+static void file_begin (void *handle)
+{
+ struct file_read_info *p = handle;
+
+ p->file_offset = p->file_moffset;
+ if (p->file_moffset)
+ lseek (p->fd, p->file_moffset, SEEK_SET);
+ p->file_more = 0;
+}
+
+static void file_end (void *handle, off_t offset)
+{
+ struct file_read_info *p = handle;
+
+ assert (p->file_more == 0);
+ p->file_more = 1;
+ p->file_moffset = offset;
+}
+
+static int atois (const char **s)
+{
+ int val = 0, c;
+ while ( (c=**s) >= '0' && c <= '9')
+ {
+ val = val*10 + c - '0';
+ ++(*s);
+ }
+ return val;
+}
+
+static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
+ const char *fname,
+ const char *spec)
+{
+ static char dstBuf[2048];
+ char *dst = dstBuf;
+ const char *s = spec;
+ static const char **w;
+ int i;
+
+ while (1)
+ {
+ while (*s == ' ' || *s == '\t')
+ s++;
+ if (!*s)
+ break;
+ if (*s == '(')
+ {
+ char matchFlag[32];
+ int attrSet, attrUse;
+ int first = 1;
+
+ s++;
+ attrSet = atois (&s);
+ if (*s != ',')
+ {
+ logf (LOG_WARN, "Missing , in match criteria %s in group %s",
+ spec, rGroup->groupName ? rGroup->groupName : "none");
+ return NULL;
+ }
+ s++;
+ attrUse = atois (&s);
+ w = searchRecordKey (reckeys, attrSet, attrUse);
+ assert (w);
+
+ if (*s == ')')
+ {
+ for (i = 0; i<32; i++)
+ matchFlag[i] = 1;
+ }
+ else
+ {
+ logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
+ spec, rGroup->groupName ? rGroup->groupName : "none");
+ return NULL;
+ }
+ s++;
+
+ for (i = 0; i<32; i++)
+ if (matchFlag[i] && w[i])
+ {
+ if (first)
+ {
+ *dst++ = ' ';
+ first = 0;
+ }
+ strcpy (dst, w[i]);
+ dst += strlen(w[i]);
+ }
+ if (first)
+ {
+ logf (LOG_WARN, "Record didn't contain match"
+ " fields in (%d,%d)", attrSet, attrUse);
+ return NULL;
+ }
+ }
+ else if (*s == '$')
+ {
+ int spec_len;
+ char special[64];
+ const char *spec_src = NULL;
+ const char *s1 = ++s;
+ while (*s1 && *s1 != ' ' && *s1 != '\t')
+ s1++;
+
+ spec_len = s1 - s;
+ if (spec_len > 63)
+ spec_len = 63;
+ memcpy (special, s, spec_len);
+ special[spec_len] = '\0';
+ s = s1;
+
+ if (!strcmp (special, "group"))
+ spec_src = rGroup->groupName;
+ else if (!strcmp (special, "database"))
+ spec_src = rGroup->databaseName;
+ else if (!strcmp (special, "filename"))
+ spec_src = fname;
+ else if (!strcmp (special, "type"))
+ spec_src = rGroup->recordType;
+ else
+ spec_src = NULL;
+ if (spec_src)
+ {
+ strcpy (dst, spec_src);
+ dst += strlen (spec_src);
+ }
+ }
+ else if (*s == '\"' || *s == '\'')
+ {
+ int stopMarker = *s++;
+ char tmpString[64];
+ int i = 0;
+
+ while (*s && *s != stopMarker)
+ {
+ if (i < 63)
+ tmpString[i++] = *s++;
+ }
+ if (*s)
+ s++;
+ tmpString[i] = '\0';
+ strcpy (dst, tmpString);
+ dst += strlen (tmpString);
+ }
+ else
+ {
+ logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
+ spec, rGroup->groupName ? rGroup->groupName : "none");
+ return NULL;
+ }
+ *dst++ = 1;
+ }
+ if (dst == dstBuf)
+ {
+ logf (LOG_WARN, "No match criteria for record %s in group %s",
+ fname, rGroup->groupName ? rGroup->groupName : "none");
+ return NULL;
+ }
+ return dstBuf;
+}
+
+struct recordLogInfo {
+ const char *fname;
+ int recordOffset;
+ struct recordGroup *rGroup;
+};
+
+static void recordLogPreamble (int level, const char *msg, void *info)
+{
+ struct recordLogInfo *p = info;
+ FILE *outf = log_file ();
+
+ if (level & LOG_LOG)
+ return ;
+ fprintf (outf, "File %s, offset %d, type %s\n",
+ p->rGroup->recordType, p->recordOffset, p->fname);
+ log_event_start (NULL, NULL);
+}
+
+static int recordExtract (SYSNO *sysno, const char *fname,
+ struct recordGroup *rGroup, int deleteFlag,
+ struct file_read_info *fi, RecType recType,
+ char *subType)
+{
+ struct recExtractCtrl extractCtrl;
+ int r;
+ char *matchStr;
+ SYSNO sysnotmp;
+ off_t recordOffset = 0;
+ Record rec;
+ struct recordLogInfo logInfo;
+
+ if (fi->fd != -1)
+ {
+ /* we are going to read from a file, so prepare the extraction */
+ extractCtrl.fh = fi;
+ extractCtrl.subType = subType;
+ extractCtrl.init = wordInit;
+ extractCtrl.add = addRecordKey;
+
+ reckeys.buf_used = 0;
+ reckeys.prevAttrUse = -1;
+ reckeys.prevAttrSet = -1;
+ reckeys.prevSeqNo = 0;
+
+ recordOffset = fi->file_moffset;
+ extractCtrl.offset = recordOffset;
+ extractCtrl.readf = file_read;
+ extractCtrl.seekf = file_seek;
+ extractCtrl.endf = file_end;
+ extractCtrl.map_chrs_input = map_chrs_input;
+ extractCtrl.flagShowRecords = rGroup->flagShowRecords;
+ if (rGroup->flagShowRecords)
+ printf ("File: %s %ld\n", fname, (long) recordOffset);
+
+ logInfo.fname = fname;
+ logInfo.recordOffset = recordOffset;
+ logInfo.rGroup = rGroup;
+ log_event_start (recordLogPreamble, &logInfo);
+
+ r = (*recType->extract)(&extractCtrl);
+
+ log_event_start (NULL, NULL);
+
+ if (r)
+ {
+ /* error occured during extraction ... */
+ if (!rGroup->flagShowRecords &&
+ records_processed < rGroup->fileVerboseLimit)
+ {
+ logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
+ fname, (long) recordOffset, r);
+ }
+ return 0;
+ }
+ if (reckeys.buf_used == 0)
+ {
+ /* the extraction process returned no information - the record
+ is probably empty - unless flagShowRecords is in use */
+ if (rGroup->flagShowRecords)
+ return 1;
+ logf (LOG_WARN, "No keys generated for file %s", fname);
+ logf (LOG_WARN, " The file is probably empty");
+ return 0;
+ }
+ }
+
+ /* perform match if sysno not known and if match criteria is specified */
+
+ matchStr = NULL;
+ if (!sysno)
+ {
+ sysnotmp = 0;
+ sysno = &sysnotmp;
+ if (rGroup->recordId && *rGroup->recordId)
+ {
+ char *rinfo;
+
+ matchStr = fileMatchStr (&reckeys, rGroup, fname,
+ rGroup->recordId);
+ if (matchStr)
+ {
+ rinfo = dict_lookup (matchDict, matchStr);
+ if (rinfo)
+ memcpy (sysno, rinfo+1, sizeof(*sysno));
+ }
+ else
+ {
+ logf (LOG_WARN, "Bad match criteria");
+ return 0;
+ }
+ }
+ }
+
+ if (! *sysno)
+ {
+ /* new record */
+ if (deleteFlag)
+ {
+ logf (LOG_LOG, "Cannot delete new record");
+ return 1;
+ }
+ if (records_processed < rGroup->fileVerboseLimit)
+ logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
+ fname, (long) recordOffset);
+ rec = rec_new (records);
+ *sysno = rec->sysno;
+
+ if (matchStr)
+ {
+ dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
+ }
+ flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
+
+ records_inserted++;
+ }
+ else
+ {
+ /* record already exists */
+ struct recKeys delkeys;
+
+ rec = rec_get (records, *sysno);
+ assert (rec);
+ delkeys.buf_used = rec->size[recInfo_delKeys];
+ delkeys.buf = rec->info[recInfo_delKeys];
+ flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
+ if (deleteFlag)
+ {
+ /* record going to be deleted */
+ if (!delkeys.buf_used)
+ {
+ logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
+ fname, (long) recordOffset);
+ logf (LOG_WARN, "cannot delete file above, storeKeys false");
+ }
+ else
+ {
+ if (records_processed < rGroup->fileVerboseLimit)
+ logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
+ fname, (long) recordOffset);
+ records_deleted++;
+ if (matchStr)
+ dict_delete (matchDict, matchStr);
+ rec_del (records, &rec);
+ }
+ logRecord (0);
+ return 1;
+ }
+ else
+ {
+ /* record going to be updated */
+ if (!delkeys.buf_used)
+ {
+ logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
+ fname, (long) recordOffset);
+ logf (LOG_WARN, "cannot update file above, storeKeys false");
+ }
+ else
+ {
+ if (records_processed < rGroup->fileVerboseLimit)
+ logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
+ fname, (long) recordOffset);
+ flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
+ records_updated++;
+ }
+ }
+ }
+ /* update file type */
+ xfree (rec->info[recInfo_fileType]);
+ rec->info[recInfo_fileType] =
+ rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
+
+ /* update filename */
+ xfree (rec->info[recInfo_filename]);
+ rec->info[recInfo_filename] =
+ rec_strdup (fname, &rec->size[recInfo_filename]);
+
+ /* update delete keys */
+ xfree (rec->info[recInfo_delKeys]);
+ if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
+ {
+#if 1
+ rec->size[recInfo_delKeys] = reckeys.buf_used;
+ rec->info[recInfo_delKeys] = reckeys.buf;
+ reckeys.buf = NULL;
+ reckeys.buf_max = 0;
+#else
+ rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
+ rec->size[recInfo_delKeys] = reckeys.buf_used;
+ memcpy (rec->info[recInfo_delKeys], reckeys.buf,
+ rec->size[recInfo_delKeys]);
+#endif
+ }
+ else
+ {
+ rec->info[recInfo_delKeys] = NULL;
+ rec->size[recInfo_delKeys] = 0;
+ }
+
+ /* update store data */
+ xfree (rec->info[recInfo_storeData]);
+ if (rGroup->flagStoreData == 1)
+ {
+ rec->size[recInfo_storeData] = fi->file_max;
+ rec->info[recInfo_storeData] = xmalloc (fi->file_max);
+ if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
+ {
+ logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
+ (long) recordOffset);
+ exit (1);
+ }
+ if (read (fi->fd, rec->info[recInfo_storeData], fi->file_max)
+ < fi->file_max)
+ {
+ logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
+ fi->file_max, fname);
+ exit (1);
+ }
+ }
+ else
+ {
+ rec->info[recInfo_storeData] = NULL;
+ rec->size[recInfo_storeData] = 0;
+ }
+ /* update database name */
+ xfree (rec->info[recInfo_databaseName]);
+ rec->info[recInfo_databaseName] =
+ rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
+
+ /* update offset */
+ xfree (rec->info[recInfo_offset]);
+
+ rec->size[recInfo_offset] = sizeof(recordOffset);
+ rec->info[recInfo_offset] = xmalloc (sizeof(recordOffset));
+ memcpy (rec->info[recInfo_offset], &recordOffset, sizeof(recordOffset));
+
+ /* commit this record */
+ rec_put (records, &rec);
+ logRecord (0);
+ return 1;
+}
+
+int fileExtract (SYSNO *sysno, const char *fname,
+ const struct recordGroup *rGroupP, int deleteFlag)
+{
+ int r, i, fd;
+ char gprefix[128];