+ static char dstBuf[2048]; /* static here ??? */
+ char *dst = dstBuf;
+ const char *s = spec;
+ static const char **w;
+
+ while (1)
+ {
+ while (*s == ' ' || *s == '\t')
+ s++;
+ if (!*s)
+ break;
+ if (*s == '(')
+ {
+ char attset_str[64], attname_str[64];
+ data1_attset *attset;
+ int i;
+ char matchFlag[32];
+ int attSet = 1, attUse = 1;
+ int first = 1;
+
+ s++;
+ for (i = 0; *s && *s != ',' && *s != ')'; s++)
+ if (i < 63)
+ attset_str[i++] = *s;
+ attset_str[i] = '\0';
+
+ if (*s == ',')
+ {
+ s++;
+ for (i = 0; *s && *s != ')'; s++)
+ if (i < 63)
+ attname_str[i++] = *s;
+ attname_str[i] = '\0';
+ }
+
+ if ((attset = data1_get_attset (zh->reg->dh, attset_str)))
+ {
+ data1_att *att;
+ attSet = attset->reference;
+ att = data1_getattbyname(zh->reg->dh, attset, attname_str);
+ if (att)
+ attUse = att->value;
+ else
+ attUse = atoi (attname_str);
+ }
+ w = searchRecordKey (zh, reckeys, attSet, attUse);
+ 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 (%s,%s)", attset_str, attname_str);
+ 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;
+ }
+ *dst = '\0';
+ return dstBuf;
+}
+
+struct recordLogInfo {
+ const char *fname;
+ int recordOffset;
+ struct recordGroup *rGroup;
+};
+
+static int recordExtract (ZebraHandle zh,
+ SYSNO *sysno, const char *fname,
+ struct recordGroup *rGroup, int deleteFlag,
+ struct file_read_info *fi,
+ RecType recType, char *subType, void *clientData,
+ int force_update)
+{
+ RecordAttr *recordAttr;
+ int r;
+ char *matchStr;
+ SYSNO sysnotmp;
+ Record rec;
+ 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;
+
+ zh->reg->keys.buf_used = 0;
+ zh->reg->keys.prevAttrUse = -1;
+ zh->reg->keys.prevAttrSet = -1;
+ zh->reg->keys.prevSeqNo = 0;
+ zh->reg->sortKeys.buf_used = 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 = extract_init;
+ extractCtrl.tokenAdd = extract_token_add;
+ extractCtrl.schemaAdd = extract_schema_add;
+ extractCtrl.dh = zh->reg->dh;
+ extractCtrl.handle = zh;
+ for (i = 0; i<256; i++)
+ {
+ if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
+ extractCtrl.seqno[i] = 1;
+ else
+ extractCtrl.seqno[i] = 0;
+ }
+ extractCtrl.zebra_maps = zh->reg->zebra_maps;
+ extractCtrl.flagShowRecords = !rGroup->flagRw;
+
+ if (!rGroup->flagRw)
+ printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
+ if (rGroup->flagRw)
+ {
+ char msg[512];
+ sprintf (msg, "%s:" PRINTF_OFF_T , fname, recordOffset);
+ yaz_log_init_prefix2 (msg);
+ }
+
+ r = (*recType->extract)(clientData, &extractCtrl);
+
+ yaz_log_init_prefix2 (0);
+ if (r == RECCTRL_EXTRACT_EOF)
+ return 0;
+ else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
+ {
+ /* error occured during extraction ... */
+ if (rGroup->flagRw &&
+ zh->records_processed < rGroup->fileVerboseLimit)
+ {
+ logf (LOG_WARN, "fail %s %s " PRINTF_OFF_T, rGroup->recordType,
+ fname, recordOffset);
+ }
+ return 0;
+ }
+ else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
+ {
+ /* error occured during extraction ... */
+ if (rGroup->flagRw &&
+ zh->records_processed < rGroup->fileVerboseLimit)
+ {
+ logf (LOG_WARN, "no filter for %s %s "
+ PRINTF_OFF_T, rGroup->recordType,
+ fname, recordOffset);
+ }
+ return 0;
+ }
+ if (zh->reg->keys.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, "empty %s %s " PRINTF_OFF_T, rGroup->recordType,
+ fname, recordOffset);
+ 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 (zh, &zh->reg->keys, rGroup, fname,
+ rGroup->recordId);
+ if (matchStr)
+ {
+ rinfo = dict_lookup (zh->reg->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 " PRINTF_OFF_T, rGroup->recordType,
+ fname, recordOffset);
+ logf (LOG_WARN, "cannot delete record above (seems new)");
+ return 1;
+ }
+ if (zh->records_processed < rGroup->fileVerboseLimit)
+ logf (LOG_LOG, "add %s %s " PRINTF_OFF_T, rGroup->recordType,
+ fname, recordOffset);
+ rec = rec_new (zh->reg->records);
+
+ *sysno = rec->sysno;
+
+ recordAttr = rec_init_attr (zh->reg->zei, rec);
+
+ if (matchStr)
+ {
+ dict_insert (zh->reg->matchDict, matchStr, sizeof(*sysno), sysno);
+ }
+ extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
+ extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
+
+ zh->records_inserted++;
+ }
+ else
+ {
+ /* record already exists */
+ struct recKeys delkeys;
+ struct sortKeys sortKeys;
+
+ rec = rec_get (zh->reg->records, *sysno);
+ assert (rec);
+
+ recordAttr = rec_init_attr (zh->reg->zei, rec);
+
+ if (!force_update && recordAttr->runNumber ==
+ zebraExplain_runNumberIncrement (zh->reg->zei, 0))
+ {
+ yaz_log (LOG_LOG, "run number = %d", recordAttr->runNumber);
+ yaz_log (LOG_LOG, "skipped %s %s " PRINTF_OFF_T,
+ rGroup->recordType, fname, recordOffset);
+ extract_flushSortKeys (zh, *sysno, -1, &zh->reg->sortKeys);
+ rec_rm (&rec);
+ logRecord (zh);
+ return 1;
+ }
+ delkeys.buf_used = rec->size[recInfo_delKeys];
+ delkeys.buf = rec->info[recInfo_delKeys];
+
+ sortKeys.buf_used = rec->size[recInfo_sortKeys];
+ sortKeys.buf = rec->info[recInfo_sortKeys];
+
+ extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
+ extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
+ if (deleteFlag)
+ {
+ /* record going to be deleted */
+ if (!delkeys.buf_used)
+ {
+ logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
+ rGroup->recordType, fname, recordOffset);
+ logf (LOG_WARN, "cannot delete file above, storeKeys false");
+ }
+ else
+ {
+ if (zh->records_processed < rGroup->fileVerboseLimit)
+ logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
+ rGroup->recordType, fname, recordOffset);
+ zh->records_deleted++;
+ if (matchStr)
+ dict_delete (zh->reg->matchDict, matchStr);
+ rec_del (zh->reg->records, &rec);
+ }
+ rec_rm (&rec);
+ logRecord (zh);
+ return 1;
+ }
+ else
+ {
+ /* record going to be updated */
+ if (!delkeys.buf_used)
+ {
+ logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
+ rGroup->recordType, fname, recordOffset);
+ logf (LOG_WARN, "cannot update file above, storeKeys false");
+ }
+ else
+ {
+ if (zh->records_processed < rGroup->fileVerboseLimit)
+ logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
+ rGroup->recordType, fname, recordOffset);
+ extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
+ extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
+ zh->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 (zh->reg->keys.buf_used > 0 && rGroup->flagStoreKeys == 1)
+ {
+ rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
+ rec->info[recInfo_delKeys] = zh->reg->keys.buf;
+ zh->reg->keys.buf = NULL;
+ zh->reg->keys.buf_max = 0;
+ }
+ else
+ {
+ rec->info[recInfo_delKeys] = NULL;
+ rec->size[recInfo_delKeys] = 0;
+ }
+
+ /* update sort keys */
+ xfree (rec->info[recInfo_sortKeys]);
+
+ rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
+ rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
+ zh->reg->sortKeys.buf = NULL;
+ zh->reg->sortKeys.buf_max = 0;
+
+ /* save file size of original record */
+ zebraExplain_recordBytesIncrement (zh->reg->zei,
+ - recordAttr->recordSize);
+ recordAttr->recordSize = fi->file_moffset - recordOffset;
+ if (!recordAttr->recordSize)
+ recordAttr->recordSize = fi->file_max - recordOffset;
+ zebraExplain_recordBytesIncrement (zh->reg->zei,
+ recordAttr->recordSize);
+
+ /* set run-number for this record */
+ recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
+ 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 " PRINTF_OFF_T " in %s",
+ 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 (zh->reg->records, &rec);
+ logRecord (zh);
+ return 1;
+}
+
+int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
+ const struct recordGroup *rGroupP, int deleteFlag)
+{
+ int r, i, fd;
+ char gprefix[128];
+ char ext[128];
+ char ext_res[128];
+ char subType[128];
+ RecType recType;
+ struct recordGroup rGroupM;
+ struct recordGroup *rGroup = &rGroupM;
+ struct file_read_info *fi;
+ void *clientData;
+
+ memcpy (rGroup, rGroupP, sizeof(*rGroupP));
+
+ if (!rGroup->groupName || !*rGroup->groupName)
+ *gprefix = '\0';
+ else
+ sprintf (gprefix, "%s.", rGroup->groupName);
+
+ logf (LOG_DEBUG, "fileExtract %s", fname);