+ /* determine file type - depending on extension */
+ original_record_type = zh->m_record_type;
+ if (!zh->m_record_type)
+ {
+ sprintf (ext_res, "%srecordType.%s", gprefix, ext);
+ zh->m_record_type = res_get (zh->res, ext_res);
+ }
+ if (!zh->m_record_type)
+ {
+ if (zh->records_processed < zh->m_file_verbose_limit)
+ yaz_log (YLOG_LOG, "? %s", fname);
+ return 0;
+ }
+ /* determine match criteria */
+ if (!zh->m_record_id)
+ {
+ sprintf (ext_res, "%srecordId.%s", gprefix, ext);
+ zh->m_record_id = res_get (zh->res, ext_res);
+ }
+
+ if (sysno && deleteFlag)
+ fd = -1;
+ else
+ {
+ char full_rep[1024];
+
+ if (zh->path_reg && !yaz_is_abspath (fname))
+ {
+ strcpy (full_rep, zh->path_reg);
+ strcat (full_rep, "/");
+ strcat (full_rep, fname);
+ }
+ else
+ strcpy (full_rep, fname);
+
+
+ if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
+ {
+ yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
+ zh->m_record_type = original_record_type;
+ return 0;
+ }
+ }
+ fi = file_read_start (fd);
+ do
+ {
+ file_begin (fi);
+ r = file_extract_record (zh, sysno, fname, deleteFlag, fi, 1);
+ } while (r && !sysno && fi->file_more);
+ file_read_stop (fi);
+ if (fd != -1)
+ close (fd);
+ zh->m_record_type = original_record_type;
+ return r;
+}
+
+/*
+ If sysno is provided, then it's used to identify the reocord.
+ If not, and match_criteria is provided, then sysno is guessed
+ If not, and a record is provided, then sysno is got from there
+
+ */
+int buffer_extract_record (ZebraHandle zh,
+ const char *buf, size_t buf_size,
+ int delete_flag,
+ int test_mode,
+ const char *recordType,
+ SYSNO *sysno,
+ const char *match_criteria,
+ const char *fname,
+ int force_update,
+ int allow_update)
+{
+ RecordAttr *recordAttr;
+ struct recExtractCtrl extractCtrl;
+ int i, r;
+ const char *matchStr = 0;
+ RecType recType = NULL;
+ void *clientData;
+ Record rec;
+ long recordOffset = 0;
+ struct zebra_fetch_control fc;
+ const char *pr_fname = fname; /* filename to print .. */
+
+ if (!pr_fname)
+ pr_fname = "<no file>"; /* make it printable if file is omitted */
+
+ fc.fd = -1;
+ fc.record_int_buf = buf;
+ fc.record_int_len = buf_size;
+ fc.record_int_pos = 0;
+ fc.offset_end = 0;
+ fc.record_offset = 0;
+
+ extractCtrl.offset = 0;
+ extractCtrl.readf = zebra_record_int_read;
+ extractCtrl.seekf = zebra_record_int_seek;
+ extractCtrl.tellf = zebra_record_int_tell;
+ extractCtrl.endf = zebra_record_int_end;
+ extractCtrl.fh = &fc;
+
+ create_rec_keys_codec(&zh->reg->keys);
+
+ zh->reg->sortKeys.buf_used = 0;
+
+ if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
+ {
+ if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
+ zh->m_explain_database))
+ return 0;
+ }
+
+ if (recordType && *recordType) {
+ yaz_log (YLOG_DEBUG, "Record type explicitly specified: %s", recordType);
+ recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
+ &clientData);
+ } else {
+ if (!(zh->m_record_type)) {
+ yaz_log (YLOG_WARN, "No such record type defined");
+ return 0;
+ }
+ yaz_log (YLOG_DEBUG, "Get record type from rgroup: %s",zh->m_record_type);
+ recType = recType_byName (zh->reg->recTypes, zh->res,
+ zh->m_record_type, &clientData);
+ recordType = zh->m_record_type;
+ }
+
+ if (!recType) {
+ yaz_log (YLOG_WARN, "No such record type: %s", zh->m_record_type);
+ return 0;
+ }
+
+ extractCtrl.init = extract_init;
+ extractCtrl.tokenAdd = extract_token_add;
+ extractCtrl.schemaAdd = extract_schema_add;
+ extractCtrl.dh = zh->reg->dh;
+ extractCtrl.handle = zh;
+ extractCtrl.zebra_maps = zh->reg->zebra_maps;
+ extractCtrl.flagShowRecords = 0;
+ 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;
+ }
+
+ r = (*recType->extract)(clientData, &extractCtrl);
+
+ if (r == RECCTRL_EXTRACT_EOF)
+ return 0;
+ else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
+ {
+ /* error occured during extraction ... */
+ yaz_log (YLOG_WARN, "extract error: generic");
+ return 0;
+ }
+ else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
+ {
+ /* error occured during extraction ... */
+ yaz_log (YLOG_WARN, "extract error: no such filter");
+ 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 (test_mode)
+ return 1;
+ yaz_log (YLOG_WARN, "No keys generated for record");
+ yaz_log (YLOG_WARN, " The file is probably empty");
+ return 1;
+ }
+ /* match criteria */
+ matchStr = NULL;
+
+ if (! *sysno) {
+ char *rinfo;
+ if (match_criteria && *match_criteria) {
+ matchStr = match_criteria;
+ } else {
+ if (zh->m_record_id && *zh->m_record_id) {
+ matchStr = fileMatchStr (zh, &zh->reg->keys, pr_fname,
+ zh->m_record_id);
+ if (!matchStr)
+ {
+ yaz_log (YLOG_WARN, "Bad match criteria (recordID)");
+ return 1;
+ }
+ }
+ }
+ if (matchStr) {
+ rinfo = dict_lookup (zh->reg->matchDict, matchStr);
+ if (rinfo)
+ {
+ assert(*rinfo == sizeof(*sysno));
+ memcpy (sysno, rinfo+1, sizeof(*sysno));
+ }
+ }
+ }
+
+ if (! *sysno)
+ {
+ /* new record */
+ if (delete_flag)
+ {
+ yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
+ pr_fname, (long) recordOffset);
+ yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
+ return 1;
+ }
+ yaz_log (YLOG_LOG, "add %s %s %ld", recordType, pr_fname,
+ (long) 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