CCL: split-list deals with use attr YAZ-844
[yaz-moved-to-github.git] / client / admin.c
index 30afef2..54167f6 100644 (file)
@@ -1,9 +1,10 @@
-/*
- * Copyright (C) 1995-2007, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) Index Data
  * See the file LICENSE for details.
- *
- * $Id: admin.c,v 1.23 2007-01-03 08:42:13 adam Exp $
  */
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -27,7 +28,7 @@
 #include <yaz/proto.h>
 #include <yaz/marcdisp.h>
 #include <yaz/diagbib1.h>
-
+#include <yaz/oid_db.h>
 #include <yaz/pquery.h>
 
 #include "admin.h"
@@ -38,29 +39,24 @@ ODR getODROutputStream(void);
 extern char *databaseNames[];
 extern int num_databaseNames;
 
-int sendAdminES(int type, char* param1)
+static int sendAdminES(int type, char* param1)
 {
     ODR out = getODROutputStream();
     char *dbname = odr_strdup (out, databaseNames[0]);
-    
+
     /* Type: 1=reindex, 2=truncate, 3=delete, 4=create, 5=import, 6=refresh, 7=commit */
     Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
     Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
     Z_External *r;
-    int oid[OID_SIZE];
+    Odr_oid *oid;
     Z_ESAdminOriginPartToKeep  *toKeep;
     Z_ESAdminOriginPartNotToKeep  *notToKeep;
-    oident update_oid;
     printf ("Admin request\n");
     fflush(stdout);
 
-    /* Set up the OID for the external */
-    update_oid.proto = PROTO_Z3950;
-    update_oid.oclass = CLASS_EXTSERV;
-    update_oid.value = VAL_ADMINSERVICE;
+    oid = odr_oiddup(out, yaz_oid_extserv_admin);
 
-    oid_ent_to_oid (&update_oid, oid);
-    req->packageType = odr_oiddup(out,oid);
+    req->packageType = oid;
     req->packageName = "1.Extendedserveq";
 
     /* Allocate the external */
@@ -75,11 +71,11 @@ int sendAdminES(int type, char* param1)
     r->u.adminService->which = Z_Admin_esRequest;
     r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
         odr_malloc(out, sizeof(*r->u.adminService->u.esRequest));
-    
+
     toKeep = r->u.adminService->u.esRequest->toKeep =
-        (Z_ESAdminOriginPartToKeep *) 
+        (Z_ESAdminOriginPartToKeep *)
         odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->toKeep));
-    
+
     toKeep->which=type;
     toKeep->databaseName = dbname;
     switch ( type )
@@ -87,7 +83,7 @@ int sendAdminES(int type, char* param1)
     case Z_ESAdminOriginPartToKeep_reIndex:
         toKeep->u.reIndex=odr_nullval();
         break;
-        
+
     case Z_ESAdminOriginPartToKeep_truncate:
         toKeep->u.truncate=odr_nullval();
         break;
@@ -119,15 +115,15 @@ int sendAdminES(int type, char* param1)
         /* Unknown admin service */
         break;
     }
-    
+
     notToKeep = r->u.adminService->u.esRequest->notToKeep =
         (Z_ESAdminOriginPartNotToKeep *)
         odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->notToKeep));
     notToKeep->which=Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
     notToKeep->u.recordsWillFollow=odr_nullval();
-    
+
     send_apdu(apdu);
-    
+
     return 0;
 }
 
@@ -140,7 +136,7 @@ int cmd_adm_reindex(const char *arg)
 }
 
 /* cmd_adm_truncate
-   Truncate the specified database, removing all records and index entries, but leaving 
+   Truncate the specified database, removing all records and index entries, but leaving
    the database & it's explain information intact ready for new records */
 int cmd_adm_truncate(const char *arg)
 {
@@ -202,13 +198,13 @@ int cmd_adm_import(const char *arg)
     dir = opendir(dir_str);
     if (!dir)
         return 0;
-    
+
     sendAdminES(Z_ESAdminOriginPartToKeep_import, type_str);
-    
+
     printf ("sent es request\n");
     if ((cp=strrchr(dir_str, '/')) && cp[1] == 0)
         sep="";
-        
+
     while ((ent = readdir(dir)))
     {
         if (fnmatch (pattern_str, ent->d_name, 0) == 0)
@@ -216,7 +212,7 @@ int cmd_adm_import(const char *arg)
             char fname[1024];
             struct stat status;
             FILE *inf;
-                
+
             sprintf (fname, "%s%s%s", dir_str, sep, ent->d_name);
             stat (fname, &status);
 
@@ -240,12 +236,18 @@ int cmd_adm_import(const char *arg)
                 rec->u.intermediateFragment->which =
                     Z_FragmentSyntax_notExternallyTagged;
                 rec->u.intermediateFragment->u.notExternallyTagged = oct;
-                
-                oct->len = oct->size = status.st_size;
-                oct->buf = (unsigned char *) odr_malloc (out, oct->size);
-                fread (oct->buf, 1, oct->size, inf);
-                fclose (inf);
-                
+
+                oct->len = status.st_size;
+                oct->buf = (char *) odr_malloc (out, oct->len);
+                if (fread(oct->buf, 1, oct->len, inf) != (size_t) oct->len)
+                {
+                    printf("Incomplete read of file %s\n", fname);
+                }
+                if (fclose(inf))
+                {
+                    printf("Close failed for file %s\n", fname);
+                }
+
                 segment->segmentRecords[segment->num_segmentRecords++] = rec;
 
                 if (segment->num_segmentRecords == chunk)
@@ -253,7 +255,7 @@ int cmd_adm_import(const char *arg)
                     send_apdu (apdu);
                     apdu = 0;
                 }
-            }   
+            }
         }
     }
     if (apdu)
@@ -272,7 +274,7 @@ int cmd_adm_import(const char *arg)
 #endif
 
 
-/* "Freshen" the specified database, by checking metadata records against the sources from which they were 
+/* "Freshen" the specified database, by checking metadata records against the sources from which they were
    generated, and creating a new record if the source has been touched since the last extraction */
 int cmd_adm_refresh(const char *arg)
 {
@@ -284,7 +286,7 @@ int cmd_adm_refresh(const char *arg)
     return 0;
 }
 
-/* cmd_adm_commit 
+/* cmd_adm_commit
    Make imported records a permenant & visible to the live system */
 int cmd_adm_commit(const char *arg)
 {
@@ -306,6 +308,7 @@ int cmd_adm_startup(const char *arg)
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab