+ Record rec;
+ struct recordLogInfo logInfo;
+ off_t recordOffset = 0;
+
+ if (fi->fd != -1)
+ {
+ struct recExtractCtrl extractCtrl;
+
+ /* we are going to read from a file, so prepare the extraction */
+ int i;
+
+ reckeys.buf_used = 0;
+ reckeys.prevAttrUse = -1;
+ reckeys.prevAttrSet = -1;
+ reckeys.prevSeqNo = 0;
+
+ recordOffset = fi->file_moffset;
+ extractCtrl.offset = fi->file_moffset;
+ extractCtrl.readf = file_read;
+ extractCtrl.seekf = file_seek;
+ extractCtrl.tellf = file_tell;
+ extractCtrl.endf = file_end;
+ extractCtrl.fh = fi;
+ extractCtrl.subType = subType;
+ extractCtrl.init = wordInit;
+ extractCtrl.addWord = addRecordKey;
+ extractCtrl.addSchema = addSchema;
+ extractCtrl.dh = rGroup->dh;
+ for (i = 0; i<256; i++)
+ {
+ if (zebra_maps_is_positioned(rGroup->zebra_maps, i))
+ extractCtrl.seqno[i] = 1;
+ else
+ extractCtrl.seqno[i] = 0;
+ }
+ extractCtrl.zebra_maps = rGroup->zebra_maps;
+ extractCtrl.flagShowRecords = !rGroup->flagRw;
+
+ if (!rGroup->flagRw)
+ 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)(clientData, &extractCtrl);
+
+ log_event_start (NULL, NULL);
+
+ if (r == RECCTRL_EXTRACT_EOF)
+ return 0;
+ else if (r == RECCTRL_EXTRACT_ERROR)
+ {
+ /* error occured during extraction ... */
+ if (rGroup->flagRw &&
+ records_processed < rGroup->fileVerboseLimit)
+ {
+ logf (LOG_WARN, "fail %s %s %ld", rGroup->recordType,
+ fname, (long) recordOffset);
+ }
+ 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->flagRw)
+ return 1;
+ logf (LOG_WARN, "No keys generated for file %s", fname);
+ logf (LOG_WARN, " The file is probably empty");
+ return 1;
+ }
+ }
+
+ /* 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, "delete %s %s %ld", rGroup->recordType,
+ fname, (long) recordOffset);
+ logf (LOG_WARN, "cannot delete record above (seems new)");
+ 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;
+
+ recordAttr = rec_init_attr (zti, rec);
+
+ if (matchStr)
+ {
+ dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
+ }
+ flushRecordKeys (*sysno, 1, &reckeys);
+ flushSortKeys (*sysno, 1);
+
+ records_inserted++;
+ }
+ else
+ {
+ /* record already exists */
+ struct recKeys delkeys;
+
+ rec = rec_get (records, *sysno);
+ assert (rec);
+
+ recordAttr = rec_init_attr (zti, rec);
+
+ if (recordAttr->runNumber == zebraExplain_runNumberIncrement (zti, 0))
+ {
+ logf (LOG_LOG, "skipped %s %s %ld", rGroup->recordType,
+ fname, (long) recordOffset);
+ rec_rm (&rec);
+ logRecord (0);
+ return 1;
+ }
+ delkeys.buf_used = rec->size[recInfo_delKeys];
+ delkeys.buf = rec->info[recInfo_delKeys];
+ flushSortKeys (*sysno, 0);
+ flushRecordKeys (*sysno, 0, &delkeys);
+ 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);
+ }
+ rec_rm (&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);
+ 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;
+ }
+
+ /* save file size of original record */
+ zebraExplain_recordBytesIncrement (zti, - recordAttr->recordSize);
+ recordAttr->recordSize = fi->file_moffset - recordOffset;
+ if (!recordAttr->recordSize)
+ recordAttr->recordSize = fi->file_max - recordOffset;
+ zebraExplain_recordBytesIncrement (zti, recordAttr->recordSize);
+
+ /* set run-number for this record */
+ recordAttr->runNumber = zebraExplain_runNumberIncrement (zti, 0);
+
+ /* update store data */
+ xfree (rec->info[recInfo_storeData]);
+ if (rGroup->flagStoreData == 1)
+ {
+ rec->size[recInfo_storeData] = recordAttr->recordSize;
+ rec->info[recInfo_storeData] = (char *)
+ xmalloc (recordAttr->recordSize);
+ if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
+ {
+ logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
+ (long) recordOffset, fname);
+ exit (1);
+ }
+ if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
+ < recordAttr->recordSize)
+ {
+ logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
+ recordAttr->recordSize, 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 */
+ recordAttr->recordOffset = 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;