Fixed bug regarding Options for Sort.
[yaz-moved-to-github.git] / server / seshigh.c
index 24a835d..3233f05 100644 (file)
@@ -4,7 +4,19 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: seshigh.c,v $
- * Revision 1.74  1998-03-31 15:13:20  adam
+ * Revision 1.78  1998-08-03 10:23:55  adam
+ * Fixed bug regarding Options for Sort.
+ *
+ * Revision 1.77  1998/07/20 12:38:42  adam
+ * Implemented delete result set service to server API.
+ *
+ * Revision 1.76  1998/05/27 16:57:07  adam
+ * Support for surrogate diagnostic records added for bend_fetch.
+ *
+ * Revision 1.75  1998/05/18 10:13:07  adam
+ * Fixed call to es_request handler - extra argument was passed.
+ *
+ * Revision 1.74  1998/03/31 15:13:20  adam
  * Development towards compiled ASN.1.
  *
  * Revision 1.73  1998/03/31 11:07:45  adam
@@ -296,6 +308,8 @@ static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
 static Z_APDU *process_sortRequest(association *assoc, request *reqb, int *fd);
 static void process_close(association *assoc, request *reqb);
 void save_referenceId (request *reqb, Z_ReferenceId *refid);
+static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
+    int *fd);
 
 static FILE *apduf = 0; /* for use in static mode */
 static statserv_options_block *control_block = 0;
@@ -392,12 +406,12 @@ void destroy_association(association *h)
     xfree(h);
 }
 
-static void do_close(association *a, int reason, char *message)
+static void do_close_req(association *a, int reason, char *message,
+                        request *req)
 {
     Z_APDU apdu;
     Z_Close *cls = zget_Close(a->encode);
-    request *req = request_get(&a->outgoing);
-
+    
     /* Purge request queue */
     while (request_deq(&a->incoming));
     while (request_deq(&a->outgoing));
@@ -420,6 +434,11 @@ static void do_close(association *a, int reason, char *message)
     a->state = ASSOC_DEAD;
 }
 
+static void do_close(association *a, int reason, char *message)
+{
+    do_close_req (a, reason, message, request_get(&a->outgoing));
+}
+
 /*
  * This is where PDUs from the client are read and the further
  * processing is initiated. Flow of control moves down through the
@@ -513,7 +532,8 @@ void ir_session(IOCHAN h, int event)
        {
            request_deq(&assoc->incoming);
            if (process_request(assoc, req) < 0)
-               do_close(assoc, Z_Close_systemProblem, "Unknown error");
+               do_close_req(assoc, Z_Close_systemProblem, "Unknown error",
+                            req);
        }
     }
     if (event & EVENT_OUTPUT)
@@ -570,7 +590,6 @@ static int process_request(association *assoc, request *req)
            res = process_presentRequest(assoc, req, &fd); break;
        case Z_APDU_scanRequest:
            res = process_scanRequest(assoc, req, &fd); break;
-/* Chas: Added in from DALI */
         case Z_APDU_extendedServicesRequest:
            if (assoc->bend_esrequest)
                res = process_ESRequest(assoc, req, &fd);
@@ -579,7 +598,6 @@ static int process_request(association *assoc, request *req)
                logf(LOG_WARN, "Cannot handle EXTENDED SERVICES APDU");
                return -1;
            }
-/* Chas: End of addition from DALI */
            break;
         case Z_APDU_sortRequest:
            if (assoc->bend_sort)
@@ -592,6 +610,15 @@ static int process_request(association *assoc, request *req)
            break;
        case Z_APDU_close:
            process_close(assoc, req); return 0;
+        case Z_APDU_deleteResultSetRequest:
+           if (assoc->bend_delete)
+               res = process_deleteRequest(assoc, req, &fd);
+           else
+           {
+               logf (LOG_WARN, "Cannot handle Delete APDU");
+               return -1;
+           }
+           break;
        default:
            logf(LOG_WARN, "Bad APDU received");
            return -1;
@@ -679,13 +706,13 @@ static int process_response(association *assoc, request *req, Z_APDU *res)
     req->response = odr_getbuf(assoc->encode, &req->len_response,
        &req->size_response);
     odr_setbuf(assoc->encode, 0, 0, 0); /* don'txfree if we abort later */
-    odr_reset(assoc->encode);
     if (assoc->print && !z_APDU(assoc->print, &res, 0))
     {
        logf(LOG_WARN, "ODR print error: %s", 
            odr_errmsg(odr_geterror(assoc->print)));
        odr_reset(assoc->print);
     }
+    odr_reset(assoc->encode);
     req->state = REQUEST_IDLE;
     request_enq(&assoc->outgoing, req);
     /* turn the work over to the ir_session handler */
@@ -732,6 +759,7 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb)
     binitreq.bend_search = NULL;
     binitreq.bend_present = NULL;
     binitreq.bend_esrequest = NULL;
+    binitreq.bend_delete = NULL;
     if (!(binitres = bend_init(&binitreq)))
     {
        logf(LOG_WARN, "Bad response from backend.");
@@ -747,6 +775,8 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb)
        logf (LOG_DEBUG, "Present handler installed");   
     if ((assoc->bend_esrequest = (int (*)())binitreq.bend_esrequest))
        logf (LOG_DEBUG, "ESRequest handler installed");   
+    if ((assoc->bend_delete = (int (*)())binitreq.bend_delete))
+       logf (LOG_DEBUG, "Delete handler installed");   
     
     resp->referenceId = req->referenceId;
     *options = '\0';
@@ -761,7 +791,7 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb)
        ODR_MASK_SET(resp->options, Z_Options_present);
        strcat(options, " prst");
     }
-    if (ODR_MASK_GET(req->options, Z_Options_delSet))
+    if (ODR_MASK_GET(req->options, Z_Options_delSet) && binitreq.bend_delete)
     {
        ODR_MASK_SET(resp->options, Z_Options_delSet);
        strcat(options, " del");
@@ -787,7 +817,7 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb)
        ODR_MASK_SET(resp->options, Z_Options_concurrentOperations);
        strcat(options, " concurop");
     }
-    if (ODR_MASK_GET(req->options, Z_Options_sort && binitreq.bend_sort))
+    if (ODR_MASK_GET(req->options, Z_Options_sort) && binitreq.bend_sort)
     {
        ODR_MASK_SET(resp->options, Z_Options_sort);
        strcat(options, " sort");
@@ -885,7 +915,7 @@ static Z_Records *diagrec(association *assoc, int error, char *addinfo)
  * surrogate diagnostic.
  */
 static Z_NamePlusRecord *surrogatediagrec(association *assoc, char *dbname,
-                                           int error, char *addinfo)
+                                         int error, char *addinfo)
 {
     int oid[OID_SIZE];
     Z_NamePlusRecord *rec = (Z_NamePlusRecord *)odr_malloc (assoc->encode, sizeof(*rec));
@@ -980,7 +1010,7 @@ static Z_Records *pack_records(association *a, char *setname, int start,
        bend_fetchresult *fres;
        Z_NamePlusRecord *thisrec;
        Z_DatabaseRecord *thisext;
-       int this_length;
+       int this_length = 0;
 
        /*
         * we get the number of bytes allocated on the stream before any
@@ -993,6 +1023,7 @@ static Z_Records *pack_records(association *a, char *setname, int start,
        freq.comp = comp;
        freq.format = format;
        freq.stream = a->encode;
+       freq.surrogate_flag = 0;
        if (!(fres = bend_fetch(a->backend, &freq, 0)))
        {
            *pres = Z_PRES_FAILURE;
@@ -1002,8 +1033,17 @@ static Z_Records *pack_records(association *a, char *setname, int start,
           or only pertaining to current record */
        if (fres->errcode)
        {
-           *pres = Z_PRES_FAILURE;
-           return diagrec(a, fres->errcode, fres->errstring);
+           if (!freq.surrogate_flag)
+           {
+               *pres = Z_PRES_FAILURE;
+               return diagrec(a, fres->errcode, fres->errstring);
+           }
+           reclist->records[reclist->num_records] =
+               surrogatediagrec(a, fres->basename, fres->errcode,
+                                fres->errstring);
+           reclist->num_records++;
+           *next = fres->last_in_set ? 0 : recno + 1;
+           continue;
        }
        if (fres->len >= 0)
            this_length = fres->len;
@@ -1046,6 +1086,7 @@ static Z_Records *pack_records(association *a, char *setname, int start,
                continue;
            }
        }
+
        if (!(thisrec = (Z_NamePlusRecord *)odr_malloc(a->encode, sizeof(*thisrec))))
            return 0;
        if (!(thisrec->databaseName = (char *)odr_malloc(a->encode,
@@ -1552,6 +1593,45 @@ static Z_APDU *process_sortRequest(association *assoc, request *reqb,
     return apdu;
 }
 
+static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
+    int *fd)
+{
+    Z_DeleteResultSetRequest *req = reqb->request->u.deleteResultSetRequest;
+    Z_DeleteResultSetResponse *res = (Z_DeleteResultSetResponse *)
+       odr_malloc (assoc->encode, sizeof(*res));
+    bend_delete_rr *bdrr = (bend_delete_rr *)
+       odr_malloc (assoc->encode, sizeof(*bdrr));
+    Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
+
+    logf(LOG_LOG, "Got DeleteRequest.");
+
+    bdrr->num_setnames = req->num_ids;
+    bdrr->setnames = req->resultSetList;
+    bdrr->stream = assoc->encode;
+    bdrr->function = *req->deleteFunction;
+
+    ((int (*)(void *, bend_delete_rr *))
+     (*assoc->bend_delete))(assoc->backend, bdrr);
+    
+    res->referenceId = req->referenceId;
+
+    res->deleteOperationStatus = (int *)
+       odr_malloc (assoc->encode, sizeof(*res->deleteOperationStatus));
+    *res->deleteOperationStatus = bdrr->delete_status;
+
+    res->num_statuses = 0;
+    res->deleteListStatuses = 0;
+    res->numberNotDeleted = 0;
+    res->num_bulkStatuses = 0;
+    res->bulkStatuses = 0;
+    res->deleteMessage = 0;
+    res->otherInfo = 0;
+
+    apdu->which = Z_APDU_deleteResultSetResponse;
+    apdu->u.deleteResultSetResponse = res;
+    return apdu;
+}
+
 static void process_close(association *assoc, request *reqb)
 {
     Z_Close *req = reqb->request->u.close;
@@ -1574,7 +1654,8 @@ static void process_close(association *assoc, request *reqb)
        req->diagnosticInformation : "NULL");
     if (assoc->version < 3) /* to make do_force respond with close */
        assoc->version = 3;
-    do_close(assoc, Z_Close_finished, "Association terminated by client");
+    do_close_req(assoc, Z_Close_finished, "Association terminated by client",
+                reqb);
 }
 
 void save_referenceId (request *reqb, Z_ReferenceId *refid)
@@ -1654,11 +1735,12 @@ static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd)
     esrequest.stream = assoc->encode;
     esrequest.errcode = 0;
     esrequest.errstring = NULL;
-       esrequest.request = reqb;
-       esrequest.association = assoc;
-
-    (*assoc->bend_esrequest)(assoc->backend, &esrequest, fd);
-
+    esrequest.request = reqb;
+    esrequest.association = assoc;
+    
+    ((int (*)(void *, bend_esrequest_rr *))(*assoc->bend_esrequest))(assoc->backend,
+                                                                  &esrequest);
+    
     /* If the response is being delayed, return NULL */
     if (esrequest.request == NULL)
         return(NULL);