+ else if (rr->esr->taskSpecificParameters->which == Z_External_update)
+ {
+ Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
+ yaz_log (LOG_LOG, "Received DB Update");
+ if (up->which == Z_IUUpdate_esRequest)
+ {
+ Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
+ Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
+ Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
+
+ yaz_log (LOG_LOG, "action");
+ if (toKeep->action)
+ {
+ switch (*toKeep->action)
+ {
+ case Z_IUOriginPartToKeep_recordInsert:
+ yaz_log (LOG_LOG, "recordInsert");
+ break;
+ case Z_IUOriginPartToKeep_recordReplace:
+ yaz_log (LOG_LOG, "recordUpdate");
+ break;
+ case Z_IUOriginPartToKeep_recordDelete:
+ yaz_log (LOG_LOG, "recordDelete");
+ break;
+ case Z_IUOriginPartToKeep_elementUpdate:
+ yaz_log (LOG_LOG, "elementUpdate");
+ break;
+ case Z_IUOriginPartToKeep_specialUpdate:
+ yaz_log (LOG_LOG, "specialUpdate");
+ break;
+ case Z_ESAdminOriginPartToKeep_shutdown:
+ yaz_log (LOG_LOG, "shutDown");
+ break;
+ case Z_ESAdminOriginPartToKeep_start:
+ yaz_log (LOG_LOG, "start");
+ break;
+ default:
+ yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
+ }
+ }
+ if (toKeep->databaseName)
+ {
+ yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
+
+ if (zebra_select_database(zh, toKeep->databaseName))
+ return 0;
+ }
+ else
+ {
+ yaz_log (LOG_WARN, "no database supplied for ES Update");
+ rr->errcode = 1008;
+ rr->errstring = "database";
+ return 0;
+ }
+ if (notToKeep)
+ {
+ int i;
+ zebra_begin_trans (zh);
+ for (i = 0; i < notToKeep->num; i++)
+ {
+ Z_External *rec = notToKeep->elements[i]->record;
+ struct oident *oident = 0;
+ Odr_oct *recid = notToKeep->elements[i]->u.opaque;
+
+ if (!recid)
+ {
+ rr->errcode = 224;
+ rr->errstring = "record Id not supplied";
+ break;
+ }
+ if (notToKeep->elements[i]->which !=
+ Z_IUSuppliedRecords_elem_opaque)
+ {
+ rr->errcode = 224;
+ rr->errstring = "only opaque record ID supported";
+ break;
+ }
+
+ if (rec->direct_reference)
+ {
+ oident = oid_getentbyoid(rec->direct_reference);
+ if (oident)
+ yaz_log (LOG_LOG, "record %d type %s", i,
+ oident->desc);
+ }
+ switch (rec->which)
+ {
+ case Z_External_sutrs:
+ if (rec->u.octet_aligned->len > 170)
+ yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
+ rec->u.sutrs->len,
+ rec->u.sutrs->buf);
+ else
+ yaz_log (LOG_LOG, "%d bytes:\n%s",
+ rec->u.sutrs->len,
+ rec->u.sutrs->buf);
+ break;
+ case Z_External_octet:
+ if (rec->u.octet_aligned->len > 170)
+ yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
+ rec->u.octet_aligned->len,
+ rec->u.octet_aligned->buf);
+ else
+ yaz_log (LOG_LOG, "%d bytes\n%s",
+ rec->u.octet_aligned->len,
+ rec->u.octet_aligned->buf);
+ }
+ if (oident && oident->value != VAL_TEXT_XML)
+ {
+ rr->errcode = 224;
+ rr->errstring = "only XML update supported";
+ break;
+ }
+ if (rec->which == Z_External_octet)
+ {
+ int action = 0;
+
+ if (*toKeep->action ==
+ Z_IUOriginPartToKeep_recordInsert)
+ action = 1;
+ if (*toKeep->action ==
+ Z_IUOriginPartToKeep_recordReplace)
+ action = 2;
+ if (*toKeep->action ==
+ Z_IUOriginPartToKeep_recordDelete)
+ action = 3;
+ if (*toKeep->action ==
+ Z_IUOriginPartToKeep_specialUpdate)
+ action = 1;
+
+ if (!action)
+ {
+ rr->errcode = 224;
+ rr->errstring = "unsupported ES Update action";
+ break;
+ }
+ else
+ {
+ int r = zebra_admin_exchange_record (
+ zh, toKeep->databaseName,
+ rec->u.octet_aligned->buf,
+ rec->u.octet_aligned->len,
+ recid->buf, recid->len,
+ action);
+ if (r && *toKeep->action ==
+ Z_IUOriginPartToKeep_specialUpdate)
+ {
+ r = zebra_admin_exchange_record (
+ zh, toKeep->databaseName,
+ rec->u.octet_aligned->buf,
+ rec->u.octet_aligned->len,
+ recid->buf, recid->len,
+ 2);
+ }
+ if (r)
+ {
+ rr->errcode = 224;
+ rr->errstring = "record exchange failed";
+ break;
+ }
+ }
+ }
+ }
+ zebra_end_trans (zh);
+ }
+ }
+ }
+ else
+ {
+ yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
+ rr->esr->taskSpecificParameters->which);
+ rr->errcode = 221;
+
+ }
+ return 0;
+}
+
+static void bend_start (struct statserv_options_block *sob)
+{
+#ifdef WIN32
+
+#else
+ if (!sob->inetd)
+ {
+ char *pidfile = "zebrasrv.pid";
+ int fd = creat (pidfile, 0666);
+ if (fd == -1)
+ logf (LOG_WARN|LOG_ERRNO, "creat %s", pidfile);
+ else
+ {
+ char pidstr[30];
+
+ sprintf (pidstr, "%ld", (long) getpid ());
+ write (fd, pidstr, strlen(pidstr));
+ close (fd);
+ }
+ }
+#endif
+ if (sob->handle)
+ zebra_stop((ZebraService) sob->handle);
+ sob->handle = zebra_start(sob->configname);
+ if (!sob->handle)
+ {
+ yaz_log (LOG_FATAL, "Failed to read config `%s'", sob->configname);
+ exit (1);
+ }
+}
+
+static void bend_stop(struct statserv_options_block *sob)
+{
+#ifdef WIN32
+
+#else
+ if (!sob->inetd)
+ unlink ("zebrasrv.pid");
+#endif
+ if (sob->handle)
+ {
+ ZebraService service = sob->handle;
+ zebra_stop(service);
+ }
+}
+
+int main (int argc, char **argv)
+{
+ struct statserv_options_block *sob;
+
+ sob = statserv_getcontrol ();
+ strcpy (sob->configname, "zebra.cfg");
+ sob->bend_start = bend_start;
+ sob->bend_stop = bend_stop;
+#ifdef WIN32
+ strcpy (sob->service_display_name, "Zebra Server");
+#endif
+ statserv_setcontrol (sob);
+
+ return statserv_main (argc, argv, bend_init, bend_close);