75fabaa36517d05e0a6cbfc285c5ebd9d4a553d2
[idzebra-moved-to-github.git] / index / zebrasrv.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #ifdef WIN32
25 #include <io.h>
26 #include <process.h>
27 #include <sys/locking.h>
28 #endif
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 #include <errno.h>
34 #include <yaz/log.h>
35 #include <yaz/ill.h>
36 #include <yaz/yaz-util.h>
37 #include <yaz/diagbib1.h>
38 #include <yaz/querytowrbuf.h>
39 #include <yaz/pquery.h>
40 #include <sys/types.h>
41
42 #include <yaz/backend.h>
43 #include <yaz/charneg.h>
44 #include <idzebra/api.h>
45
46 static int bend_sort(void *handle, bend_sort_rr *rr);
47 static int bend_delete(void *handle, bend_delete_rr *rr);
48 static int bend_esrequest(void *handle, bend_esrequest_rr *rr);
49 static int bend_segment(void *handle, bend_segment_rr *rr);
50 static int bend_search(void *handle, bend_search_rr *r);
51 static int bend_fetch(void *handle, bend_fetch_rr *r);
52 static int bend_scan(void *handle, bend_scan_rr *r);
53
54 bend_initresult *bend_init(bend_initrequest *q)
55 {
56     bend_initresult *r = (bend_initresult *)
57         odr_malloc(q->stream, sizeof(*r));
58     ZebraHandle zh;
59     struct statserv_options_block *sob;
60     char *user = NULL;
61     char *passwd = NULL;
62     char version_str[16];
63
64     r->errcode = 0;
65     r->errstring = 0;
66     q->bend_sort = bend_sort;
67     q->bend_delete = bend_delete;
68     q->bend_esrequest = bend_esrequest;
69     q->bend_segment = bend_segment;
70     q->bend_search = bend_search;
71     q->bend_fetch = bend_fetch;
72     q->bend_scan = bend_scan;
73
74     zebra_get_version(version_str, 0);
75
76     q->implementation_name = "Zebra Information Server";
77     q->implementation_version = odr_strdup(q->stream, version_str);
78
79     yaz_log(YLOG_DEBUG, "bend_init");
80
81     sob = statserv_getcontrol();
82     if (!(zh = zebra_open(sob->handle, 0)))
83     {
84         yaz_log(YLOG_WARN, "Failed to read config `%s'", sob->configname);
85         r->errcode = YAZ_BIB1_PERMANENT_SYSTEM_ERROR;
86         return r;
87     }
88     r->handle = zh;
89
90     q->query_charset = odr_strdup(q->stream, zebra_get_encoding(zh));
91     if (q->auth)
92     {
93         if (q->auth->which == Z_IdAuthentication_open)
94         {
95             char *openpass = xstrdup(q->auth->u.open);
96             char *cp = strchr(openpass, '/');
97             if (cp)
98             {
99                 *cp = '\0';
100                 user = nmem_strdup(odr_getmem(q->stream), openpass);
101                 passwd = nmem_strdup(odr_getmem(q->stream), cp+1);
102             }
103             xfree(openpass);
104         }
105         else if (q->auth->which == Z_IdAuthentication_idPass)
106         {
107             Z_IdPass *idPass = q->auth->u.idPass;
108
109             user = idPass->userId;
110             passwd = idPass->password;
111         }
112     }
113     if (zebra_auth(zh, user, passwd) != ZEBRA_OK)
114     {
115         r->errcode = YAZ_BIB1_INIT_AC_BAD_USERID_AND_OR_PASSWORD;
116         r->errstring = user;
117         return r;
118     }
119     if (q->charneg_request) /* characater set and language negotiation? */
120     {
121         char **charsets = 0;
122         int num_charsets;
123         char **langs = 0;
124         int num_langs = 0;
125         int selected = 0;
126         int i;
127         NMEM nmem = nmem_create();
128
129         yaz_get_proposal_charneg(nmem, q->charneg_request,
130                                  &charsets, &num_charsets,
131                                  &langs, &num_langs, &selected);
132         
133         for (i = 0; i < num_charsets; i++)
134         {
135             const char *right_name = "";
136             /*
137              * FIXME! It is like rudiment :-))
138              * We have to support this short names of character sets,
139              * because a lot servers in Russia to use own in during
140              * character set and language negotiation still.
141              */
142             
143             if (!yaz_matchstr(charsets[i], "win")) {
144                 right_name = "WINDOWS-1251";
145             } else if (!yaz_matchstr(charsets[i], "koi")) {
146                 right_name = "KOI8-R";
147             } else if (!yaz_matchstr(charsets[i], "iso")) {
148                 right_name = "ISO-8859-5";
149             } else if (!yaz_matchstr(charsets[i], "dos")) {
150                 right_name = "CP866";
151             } else if (!yaz_matchstr(charsets[i], "uni")) {
152                 right_name = "UTF-8";
153             } else {
154                 right_name = charsets[i];
155             }
156             if (odr_set_charset(q->decode, "UTF-8", right_name) == 0)
157             {
158                 yaz_log(YLOG_LOG, "charset %d %s (proper name %s): OK", i,
159                         charsets[i], right_name);
160                 odr_set_charset(q->stream, right_name, "UTF-8");
161                 if (selected)
162                     zebra_record_encoding(zh, right_name);
163                 zebra_octet_term_encoding(zh, right_name);
164                 q->charneg_response =
165                     yaz_set_response_charneg(q->stream, charsets[i],
166                                              0, selected);
167                 break;
168             } else {
169                 yaz_log(YLOG_LOG, "charset %d %s (proper name %s): unsupported", i,
170                         charsets[i], right_name);
171             }
172         }
173         nmem_destroy(nmem);
174     }
175     return r;
176 }
177
178 static void search_terms(ZebraHandle zh, bend_search_rr *r)
179 {
180     int no_terms;
181     int i;
182     int type = Z_Term_general;
183     struct Z_External *ext;
184     Z_SearchInfoReport *sr;
185
186     zebra_result_set_term_no(zh, r->setname, &no_terms);
187     if (!no_terms)
188         return;
189
190     r->search_info = odr_malloc(r->stream, sizeof(*r->search_info));
191
192     r->search_info->num_elements = 1;
193     r->search_info->list =
194         odr_malloc(r->stream, sizeof(*r->search_info->list));
195     r->search_info->list[0] =
196         odr_malloc(r->stream, sizeof(**r->search_info->list));
197     r->search_info->list[0]->category = 0;
198     r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
199     ext = odr_malloc(r->stream, sizeof(*ext));
200     r->search_info->list[0]->information.externallyDefinedInfo = ext;
201     ext->direct_reference = odr_oiddup(r->stream,
202                                        yaz_oid_userinfo_searchresult_1);
203     ext->indirect_reference = 0;
204     ext->descriptor = 0;
205     ext->which = Z_External_searchResult1;
206     sr = odr_malloc(r->stream, sizeof(Z_SearchInfoReport));
207     ext->u.searchResult1 = sr;
208     sr->num = no_terms;
209     sr->elements = odr_malloc(r->stream, sr->num *
210                                sizeof(*sr->elements));
211     for (i = 0; i<no_terms; i++)
212     {
213         Z_SearchInfoReport_s *se;
214         Z_Term *term;
215         zint count;
216         int approx;
217         char outbuf[1024];
218         size_t len = sizeof(outbuf);
219         const char *term_ref_id = 0;
220
221         zebra_result_set_term_info(zh, r->setname, i,
222                                    &count, &approx, outbuf, &len,
223                                    &term_ref_id);
224         se = sr->elements[i] = odr_malloc(r->stream, sizeof(**sr->elements));
225         se->subqueryId = term_ref_id ? 
226             odr_strdup(r->stream, term_ref_id) : 0;
227             
228         se->fullQuery = odr_booldup(r->stream, 0);
229         se->subqueryExpression = 
230             odr_malloc(r->stream, sizeof(Z_QueryExpression));
231         se->subqueryExpression->which = 
232             Z_QueryExpression_term;
233         se->subqueryExpression->u.term =
234             odr_malloc(r->stream, sizeof(Z_QueryExpressionTerm));
235         term = odr_malloc(r->stream, sizeof(Z_Term));
236         se->subqueryExpression->u.term->queryTerm = term;
237         switch (type)
238         {
239         case Z_Term_characterString:
240             term->which = Z_Term_characterString;
241             term->u.characterString = odr_strdup(r->stream, outbuf);
242             break;
243         case Z_Term_general:
244             term->which = Z_Term_general;
245             term->u.general = odr_malloc(r->stream, sizeof(*term->u.general));
246             term->u.general->size = term->u.general->len = len;
247             term->u.general->buf = odr_malloc(r->stream, len);
248             memcpy(term->u.general->buf, outbuf, len);
249             break;
250         default:
251             term->which = Z_Term_general;
252             term->u.null = odr_nullval();
253         }
254         se->subqueryExpression->u.term->termComment = 0;
255         se->subqueryInterpretation = 0;
256         se->subqueryRecommendation = 0;
257         se->subqueryCount = odr_intdup(r->stream, count);
258         se->subqueryWeight = 0;
259         se->resultsByDB = 0;
260     }
261 }
262
263 static int break_handler(void *client_data)
264 {
265     bend_association assoc =(bend_association) client_data;    
266     if (!bend_assoc_is_alive(assoc))
267         return 1;
268     return 0;
269 }
270
271 static Z_RPNQuery *query_add_sortkeys(ODR o, Z_RPNQuery *query,
272                                       const char *sortKeys)
273 {
274     /* sortkey layour: path,schema,ascending,caseSensitive,missingValue */
275     /* see cql_sortby_to_sortkeys of YAZ. */
276     char **sortspec;
277     int num_sortspec;
278     int i;
279
280     nmem_strsplit_blank(odr_getmem(o), sortKeys, &sortspec, &num_sortspec);
281     if (num_sortspec > 0)
282     {
283         Z_RPNQuery *nquery;
284         WRBUF w = wrbuf_alloc();
285
286         /* left operand 'd' will be replaced by original query */
287         wrbuf_puts(w, "@or d");
288         for (i = 0; i < num_sortspec; i++)
289         {
290             char **arg;
291             int num_arg;
292             int ascending = 1;
293             nmem_strsplitx(odr_getmem(o), ",", sortspec[i], &arg, &num_arg, 0);
294             
295             if (num_arg != 5)
296             {
297                 yaz_log(YLOG_WARN, "Invalid sort spec '%s' num_arg=%d",
298                         sortspec[i], num_arg);
299                 break;
300             }
301             if (arg[2][0])
302                 ascending = atoi(arg[2]);
303             
304             if (i < num_sortspec-1)
305                 wrbuf_puts(w, " @or");
306             wrbuf_puts(w, " @attr 1=");
307             yaz_encode_pqf_term(w, arg[0], strlen(arg[0]));
308             wrbuf_printf(w, "@attr 7=%d %d", ascending ? 1 : 2, i);
309         }
310         nquery = p_query_rpn(o, wrbuf_cstr(w));
311         if (!nquery)
312         {
313             yaz_log(YLOG_WARN, "query_add_sortkeys: bad RPN: '%s'",
314                     wrbuf_cstr(w));
315         }
316         else
317         {
318             Z_RPNStructure *s = nquery->RPNStructure;
319             
320             if (s->which != Z_RPNStructure_complex)
321             {
322                 yaz_log(YLOG_WARN, "query_add_sortkeys: not complex operand");
323             }
324             else
325             {
326                 /* left operand 'd' is replaced by origial query */
327                 s->u.complex->s1 = query->RPNStructure;
328                 /* and original query points to our new or+sort things */
329                 query->RPNStructure = s;
330             }
331         }
332         wrbuf_destroy(w);
333     }
334     return query;
335 }
336
337 int bend_search(void *handle, bend_search_rr *r)
338 {
339     ZebraHandle zh = (ZebraHandle) handle;
340     zint zhits = 0;
341     ZEBRA_RES res;
342     Z_RPNQuery *q2;
343
344     res = zebra_select_databases(zh, r->num_bases,
345                                   (const char **) r->basenames);
346     if (res != ZEBRA_OK)
347     {
348         zebra_result(zh, &r->errcode, &r->errstring);
349         return 0;
350     }
351     zebra_set_break_handler(zh, break_handler, r->association);
352     yaz_log(YLOG_DEBUG, "ResultSet '%s'", r->setname);
353     switch (r->query->which)
354     {
355     case Z_Query_type_1: case Z_Query_type_101:
356         q2 = query_add_sortkeys(r->stream, r->query->u.type_1, r->srw_sortKeys);
357         res = zebra_search_RPN_x(zh, r->stream, q2,
358                                  r->setname, &zhits,
359                                  &r->estimated_hit_count,
360                                  &r->partial_resultset);
361         if (res != ZEBRA_OK)
362             zebra_result(zh, &r->errcode, &r->errstring);
363         else
364         {
365             r->hits = zhits;
366             search_terms(zh, r);
367         }
368         break;
369     case Z_Query_type_2:
370         r->errcode = YAZ_BIB1_QUERY_TYPE_UNSUPP;
371         r->errstring = "type-2";
372         break;
373     default:
374         r->errcode = YAZ_BIB1_QUERY_TYPE_UNSUPP;
375     }
376     zebra_set_break_handler(zh, 0, 0);
377     return 0;
378 }
379
380
381 int bend_fetch(void *handle, bend_fetch_rr *r)
382 {
383     ZebraHandle zh = (ZebraHandle) handle;
384     ZebraRetrievalRecord retrievalRecord;
385     ZEBRA_RES res;
386
387     retrievalRecord.position = r->number;
388     
389     r->last_in_set = 0;
390     res = zebra_records_retrieve(zh, r->stream, r->setname, r->comp,
391                                   r->request_format, 1, &retrievalRecord);
392     if (res != ZEBRA_OK)
393     {
394         /* non-surrogate diagnostic */
395         zebra_result(zh, &r->errcode, &r->errstring);
396     }
397     else if (retrievalRecord.errCode)
398     {
399         /* surrogate diagnostic (diagnostic per record) */
400         r->surrogate_flag = 1;
401         r->errcode = retrievalRecord.errCode;
402         r->errstring = retrievalRecord.errString;
403         r->basename = retrievalRecord.base;
404     }
405     else
406     {
407         r->basename = retrievalRecord.base;
408         r->record = retrievalRecord.buf;
409         r->len = retrievalRecord.len;
410         r->output_format = odr_oiddup(r->stream, retrievalRecord.format);
411     }
412     return 0;
413 }
414
415 static int bend_scan(void *handle, bend_scan_rr *r)
416 {
417     ZebraScanEntry *entries;
418     ZebraHandle zh = (ZebraHandle) handle;
419     int is_partial, i;
420     ZEBRA_RES res;
421
422     res = zebra_select_databases(zh, r->num_bases, 
423                                  (const char **) r->basenames);
424     if (res != ZEBRA_OK)
425     {
426         zebra_result(zh, &r->errcode, &r->errstring);
427         return 0;
428     }
429     if (r->step_size != 0 && *r->step_size != 0) {
430         r->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
431         r->errstring = 0;
432         return 0;
433     }
434     res = zebra_scan(zh, r->stream, r->term,
435                      r->attributeset,
436                      &r->term_position,
437                      &r->num_entries, &entries, &is_partial, 
438                      0 /* setname */);
439     if (res == ZEBRA_OK)
440     {
441         if (is_partial)
442             r->status = BEND_SCAN_PARTIAL;
443         else
444             r->status = BEND_SCAN_SUCCESS;
445         for (i = 0; i < r->num_entries; i++)
446         {
447             r->entries[i].term = entries[i].term;
448             r->entries[i].display_term = entries[i].display_term;
449             r->entries[i].occurrences = entries[i].occurrences;
450         }
451     }
452     else
453     {
454         r->status = BEND_SCAN_PARTIAL;
455         zebra_result(zh, &r->errcode, &r->errstring);
456     }
457     return 0;
458 }
459
460 void bend_close(void *handle)
461 {
462     zebra_close((ZebraHandle) handle);
463     xmalloc_trav("bend_close");
464 }
465
466 int bend_sort(void *handle, bend_sort_rr *rr)
467 {
468     ZebraHandle zh = (ZebraHandle) handle;
469
470     if (zebra_sort(zh, rr->stream,
471                     rr->num_input_setnames, (const char **) rr->input_setnames,
472                     rr->output_setname, rr->sort_sequence, &rr->sort_status)
473         != ZEBRA_OK)
474         zebra_result(zh, &rr->errcode, &rr->errstring);
475     return 0;
476 }
477
478 int bend_delete(void *handle, bend_delete_rr *rr)
479 {
480     ZebraHandle zh = (ZebraHandle) handle;
481
482     rr->delete_status = zebra_deleteResultSet(zh, rr->function,
483                                               rr->num_setnames, rr->setnames,
484                                               rr->statuses);
485     return 0;
486 }
487
488 static void es_admin_request(bend_esrequest_rr *rr, ZebraHandle zh, Z_AdminEsRequest *r)
489 {
490     ZEBRA_RES res = ZEBRA_OK;
491     if (r->toKeep->databaseName)
492     {
493         yaz_log(YLOG_LOG, "adm request database %s", r->toKeep->databaseName);
494     }
495     switch (r->toKeep->which)
496     {
497     case Z_ESAdminOriginPartToKeep_reIndex:
498         yaz_log(YLOG_LOG, "adm-reindex");
499         rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
500         rr->errstring = "adm-reindex not implemented yet";
501         break;
502     case Z_ESAdminOriginPartToKeep_truncate:
503         rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
504         rr->errstring = "adm-reindex not implemented yet";
505         yaz_log(YLOG_LOG, "adm-truncate");
506         break;
507     case Z_ESAdminOriginPartToKeep_drop:
508         yaz_log(YLOG_LOG, "adm-drop");
509         res = zebra_drop_database(zh, r->toKeep->databaseName);
510         break;
511     case Z_ESAdminOriginPartToKeep_create:
512         yaz_log(YLOG_LOG, "adm-create %s", r->toKeep->databaseName);
513         res = zebra_create_database(zh, r->toKeep->databaseName);
514         break;
515     case Z_ESAdminOriginPartToKeep_import:
516         yaz_log(YLOG_LOG, "adm-import");
517         res = zebra_admin_import_begin(zh, r->toKeep->databaseName,
518                                         r->toKeep->u.import->recordType);
519         break;
520     case Z_ESAdminOriginPartToKeep_refresh:
521         yaz_log(YLOG_LOG, "adm-refresh");
522         break;
523     case Z_ESAdminOriginPartToKeep_commit:
524         yaz_log(YLOG_LOG, "adm-commit");
525         if (r->toKeep->databaseName)
526         {
527             if (zebra_select_database(zh, r->toKeep->databaseName) !=
528                 ZEBRA_OK)
529                 yaz_log(YLOG_WARN, "zebra_select_database failed in "
530                         "adm-commit");
531         }
532         zebra_commit(zh);
533         break;
534     case Z_ESAdminOriginPartToKeep_shutdown:
535         yaz_log(YLOG_LOG, "shutdown");
536         res = zebra_admin_shutdown(zh);
537         break;
538     case Z_ESAdminOriginPartToKeep_start:
539         yaz_log(YLOG_LOG, "start");
540         zebra_admin_start(zh);
541         break;
542     default:
543         yaz_log(YLOG_LOG, "unknown admin");
544         rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
545         rr->errstring = "adm-reindex not implemented yet";
546     }
547     if (res != ZEBRA_OK)
548         zebra_result(zh, &rr->errcode, &rr->errstring);
549 }
550
551 static void es_admin(bend_esrequest_rr *rr, ZebraHandle zh, Z_Admin *r)
552 {
553     switch (r->which)
554     {
555     case Z_Admin_esRequest:
556         es_admin_request(rr, zh, r->u.esRequest);
557         return;
558     default:
559         break;
560     }
561         yaz_log(YLOG_WARN, "adm taskpackage (unhandled)");
562     rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
563     rr->errstring = "adm-task package (unhandled)";
564 }
565
566 int bend_segment(void *handle, bend_segment_rr *rr)
567 {
568     ZebraHandle zh = (ZebraHandle) handle;
569     Z_Segment *segment = rr->segment;
570
571     if (segment->num_segmentRecords)
572         zebra_admin_import_segment(zh, rr->segment);
573     else
574         zebra_admin_import_end(zh);
575     return 0;
576 }
577
578 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
579 {
580     ZebraHandle zh = (ZebraHandle) handle;
581     
582     yaz_log(YLOG_LOG, "function: " ODR_INT_PRINTF, *rr->esr->function);
583     if (rr->esr->packageName)
584         yaz_log(YLOG_LOG, "packagename: %s", rr->esr->packageName);
585     yaz_log(YLOG_LOG, "Waitaction: " ODR_INT_PRINTF, *rr->esr->waitAction);
586
587     if (!rr->esr->taskSpecificParameters)
588     {
589         yaz_log(YLOG_WARN, "No task specific parameters");
590     }
591     else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
592     {
593         es_admin(rr, zh, rr->esr->taskSpecificParameters->u.adminService);
594     }
595     else if (rr->esr->taskSpecificParameters->which == Z_External_update)
596     {
597         Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
598         yaz_log(YLOG_LOG, "Received DB Update");
599         if (up->which == Z_IUUpdate_esRequest)
600         {
601             Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
602             Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
603             Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
604             
605             yaz_log(YLOG_LOG, "action");
606             if (toKeep->action)
607             {
608                 switch (*toKeep->action)
609                 {
610                 case Z_IUOriginPartToKeep_recordInsert:
611                     yaz_log(YLOG_LOG, "recordInsert");
612                     break;
613                 case Z_IUOriginPartToKeep_recordReplace:
614                     yaz_log(YLOG_LOG, "recordUpdate");
615                     break;
616                 case Z_IUOriginPartToKeep_recordDelete:
617                     yaz_log(YLOG_LOG, "recordDelete");
618                     break;
619                 case Z_IUOriginPartToKeep_elementUpdate:
620                     yaz_log(YLOG_LOG, "elementUpdate");
621                     break;
622                 case Z_IUOriginPartToKeep_specialUpdate:
623                     yaz_log(YLOG_LOG, "specialUpdate");
624                     break;
625                 case Z_ESAdminOriginPartToKeep_shutdown:
626                     yaz_log(YLOG_LOG, "shutDown");
627                     break;
628                 case Z_ESAdminOriginPartToKeep_start:
629                     yaz_log(YLOG_LOG, "start");
630                     break;
631                 default:
632                     yaz_log(YLOG_LOG, " unknown (" ODR_INT_PRINTF ")",
633                             *toKeep->action);
634                 }
635             }
636             if (toKeep->databaseName)
637             {
638                 yaz_log(YLOG_LOG, "database: %s", toKeep->databaseName);
639
640                 if (zebra_select_database(zh, toKeep->databaseName))
641                     return 0;
642             }
643             else
644             {
645                 yaz_log(YLOG_WARN, "no database supplied for ES Update");
646                 rr->errcode =
647                     YAZ_BIB1_ES_MISSING_MANDATORY_PARAMETER_FOR_SPECIFIED_FUNCTION_;
648                 rr->errstring = "database";
649                 return 0;
650             }
651             if (zebra_begin_trans(zh, 1) != ZEBRA_OK)
652             {
653                 zebra_result(zh, &rr->errcode, &rr->errstring);
654             }
655             else
656             {
657                 int i;
658                 for (i = 0; notToKeep && i < notToKeep->num; i++)
659                 {
660                     Z_External *rec = notToKeep->elements[i]->record;
661                     Odr_oct *opaque_recid = 0;
662                     zint *sysno = 0;
663                     zint sysno_tmp;
664
665                     if (notToKeep->elements[i]->u.opaque)
666                     {
667                         switch(notToKeep->elements[i]->which)
668                         {
669                         case Z_IUSuppliedRecords_elem_opaque:
670                             opaque_recid = notToKeep->elements[i]->u.opaque;
671                             break; /* OK, recid already set */
672                         case Z_IUSuppliedRecords_elem_number:
673                             sysno_tmp = *notToKeep->elements[i]->u.number;
674                             sysno = &sysno_tmp;
675                             break;
676                         }
677                     }
678                     if (rec->direct_reference)
679                     {
680                         char oid_name_str[OID_STR_MAX];
681                         const char *oid_name =
682                             yaz_oid_to_string_buf(
683                                 rec->direct_reference,
684                                 0, oid_name_str);
685                         if (oid_name)
686                             yaz_log(YLOG_LOG, "record %d type %s", i,
687                                      oid_name);
688                     }
689                     switch (rec->which)
690                     {
691                     case Z_External_sutrs:
692                         if (rec->u.octet_aligned->len > 170)
693                             yaz_log(YLOG_LOG, "%d bytes:\n%.168s ...",
694                                      rec->u.sutrs->len,
695                                      rec->u.sutrs->buf);
696                         else
697                             yaz_log(YLOG_LOG, "%d bytes:\n%s",
698                                      rec->u.sutrs->len,
699                                      rec->u.sutrs->buf);
700                         break;
701                     case Z_External_octet:
702                         if (rec->u.octet_aligned->len > 170)
703                             yaz_log(YLOG_LOG, "%d bytes:\n%.168s ...",
704                                      rec->u.octet_aligned->len,
705                                      rec->u.octet_aligned->buf);
706                         else
707                             yaz_log(YLOG_LOG, "%d bytes\n%s",
708                                      rec->u.octet_aligned->len,
709                                      rec->u.octet_aligned->buf);
710                     }
711                     if (rec->which == Z_External_octet)
712                     {
713                         enum zebra_recctrl_action_t action = action_update;
714                         char recid_str[256];
715                         const char *match_criteria = 0;
716                         ZEBRA_RES res;
717
718                         if (*toKeep->action ==
719                             Z_IUOriginPartToKeep_recordInsert)
720                             action = action_insert;
721                         else if (*toKeep->action ==
722                             Z_IUOriginPartToKeep_recordReplace)
723                             action = action_replace;
724                         else if (*toKeep->action ==
725                             Z_IUOriginPartToKeep_recordDelete)
726                             action = action_delete;
727                         else if (*toKeep->action ==
728                             Z_IUOriginPartToKeep_specialUpdate)
729                             action = action_update;
730                         else
731                         {
732                             rr->errcode =
733                                 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
734                             rr->errstring = "unsupported ES Update action";
735                             break;
736                         }
737                         
738                         if (opaque_recid)
739                         {
740                             size_t l = opaque_recid->len;
741                             if (l >= sizeof(recid_str))
742                             {
743                                 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
744                                 rr->errstring = "opaque record ID too large";
745                                 break;
746                             }
747                             memcpy(recid_str, opaque_recid->buf, l);
748                             recid_str[l] = '\0';
749                             match_criteria = recid_str;
750                         }
751                         res = zebra_update_record(
752                             zh, action,
753                             0, /* recordType */
754                             sysno, match_criteria, 0, /* fname */
755                             (const char *) rec->u.octet_aligned->buf,
756                             rec->u.octet_aligned->len);
757                         if (res == ZEBRA_FAIL)
758                         {
759                             rr->errcode =
760                                 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
761                             rr->errstring = "update_record failed";
762                         }
763                     }
764                 }
765                 if (zebra_end_trans(zh) != ZEBRA_OK)
766                 {
767                     yaz_log(YLOG_WARN, "zebra_end_trans failed for"
768                             " extended service operation");
769                 }
770             }
771         }
772     }
773     else
774     {
775         yaz_log(YLOG_WARN, "Unknown Extended Service(%d)",
776                  rr->esr->taskSpecificParameters->which);
777         rr->errcode = YAZ_BIB1_ES_EXTENDED_SERVICE_TYPE_UNSUPP;
778         
779     }
780     return 0;
781 }
782
783 static void bend_start(struct statserv_options_block *sob)
784 {
785     Res default_res = res_open(0, 0);
786
787     if (sob->handle)
788         zebra_stop((ZebraService) sob->handle);
789     res_set(default_res, "profilePath", DEFAULT_PROFILE_PATH);
790     res_set(default_res, "modulePath", DEFAULT_MODULE_PATH);
791     sob->handle = zebra_start_res(sob->configname, default_res, 0);
792     res_close(default_res);
793     if (!sob->handle)
794     {
795         yaz_log(YLOG_FATAL, "Failed to read config `%s'", sob->configname);
796         exit(1);
797     }
798 #ifdef WIN32
799     
800 #else
801     if (!sob->inetd && !sob->background) 
802     {
803         char pidfname[4096];
804         struct flock area;
805         int fd;
806
807         zebra_pidfname(sob->handle, pidfname);
808
809         fd = open(pidfname, O_EXCL|O_WRONLY|O_CREAT, 0666);
810         if (fd == -1)
811         {
812             if (errno != EEXIST)
813             {
814                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
815                 exit(1);
816             }
817             fd = open(pidfname, O_RDWR, 0666);
818             if (fd == -1)
819             {
820                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
821                 exit(1);
822             }
823         }
824         area.l_type = F_WRLCK;
825         area.l_whence = SEEK_SET;
826         area.l_len = area.l_start = 0L;
827         if (fcntl(fd, F_SETLK, &area) == -1)
828         {
829             yaz_log(YLOG_ERRNO|YLOG_FATAL, "Zebra server already running");
830             exit(1);
831         }
832         else
833         {
834             char pidstr[30];
835         
836             sprintf(pidstr, "%ld", (long) getpid());
837             if (write(fd, pidstr, strlen(pidstr)) != strlen(pidstr))
838             {
839                 yaz_log(YLOG_ERRNO|YLOG_FATAL, "write fail %s", pidfname);
840                 exit(1);
841             }
842         }
843     }
844 #endif
845 }
846
847 static void bend_stop(struct statserv_options_block *sob)
848 {
849 #ifdef WIN32
850
851 #else
852     if (!sob->inetd && !sob->background && sob->handle) 
853     {
854         char pidfname[4096];
855         zebra_pidfname(sob->handle, pidfname);
856         unlink(pidfname);
857     }
858 #endif
859     if (sob->handle)
860     {
861         ZebraService service = sob->handle;
862         zebra_stop(service);
863     }
864 }
865
866 int main(int argc, char **argv)
867 {
868     struct statserv_options_block *sob;
869
870     sob = statserv_getcontrol();
871     strcpy(sob->configname, "zebra.cfg");
872     sob->bend_start = bend_start;
873     sob->bend_stop = bend_stop;
874 #ifdef WIN32
875     strcpy(sob->service_name, "zebrasrv");
876     strcpy(sob->service_display_name, "Zebra Server");
877 #endif
878     statserv_setcontrol(sob);
879
880     return statserv_main(argc, argv, bend_init, bend_close);
881 }
882 /*
883  * Local variables:
884  * c-basic-offset: 4
885  * c-file-style: "Stroustrup"
886  * indent-tabs-mode: nil
887  * End:
888  * vim: shiftwidth=4 tabstop=8 expandtab
889  */
890