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