aa9ed43d873d985065f8b16a86150f2a0925fe1d
[yaz-moved-to-github.git] / client / admin.c
1 /*
2  * $Log: admin.c,v $
3  * Revision 1.2  2000-03-14 14:06:04  ian
4  * Minor change to order of debugging output for send_apdu,
5  * fixed encoding of admin request.
6  *
7  * Revision 1.1  2000/03/14 09:27:07  ian
8  * Added code to enable sending of admin extended service requests
9  *
10  *
11  */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <time.h>
16
17 #include <yaz/yaz-util.h>
18
19 #include <yaz/tcpip.h>
20 #ifdef USE_XTIMOSI
21 #include <yaz/xmosi.h>
22 #endif
23
24 #include <yaz/proto.h>
25 #include <yaz/marcdisp.h>
26 #include <yaz/diagbib1.h>
27
28 #include <yaz/pquery.h>
29
30 #ifdef ASN_COMPILED
31 /* #include <yaz/esadmin.h> */
32 #endif
33
34
35 /* Helper functions to get to various statics in the client */
36 ODR getODROutputStream();
37 void send_apdu(Z_APDU *a);
38
39
40
41 int sendAdminES(int type, char* dbname)
42 {
43     ODR out = getODROutputStream();
44
45     /* Type: 1=reindex, 2=truncate, 3=delete, 4=create, 5=import, 6=refresh, 7=commit */
46     Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
47     Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
48     Z_External *r;
49     int oid[OID_SIZE];
50     Z_ESAdminOriginPartToKeep  *toKeep;
51     Z_ESAdminOriginPartNotToKeep  *notToKeep;
52     oident update_oid;
53     printf ("Admin request\n");
54     fflush(stdout);
55
56     /* Set up the OID for the external */
57     update_oid.proto = PROTO_Z3950;
58     update_oid.oclass = CLASS_EXTSERV;
59     update_oid.value = VAL_ADMINSERVICE;
60
61     oid_ent_to_oid (&update_oid, oid);
62     req->packageType = odr_oiddup(out,oid);
63     req->packageName = "1.Extendedserveq";
64
65     /* Allocate the external */
66     r = req->taskSpecificParameters = (Z_External *) odr_malloc (out, sizeof(*r));
67     r->direct_reference = odr_oiddup(out,oid);
68     r->indirect_reference = 0;
69     r->descriptor = 0;
70     r->which = Z_External_ESAdmin;
71     r->u.adminService = (Z_Admin *) odr_malloc(out, sizeof(*r->u.adminService));
72     r->u.adminService->which = Z_Admin_esRequest;
73     r->u.adminService->u.esRequest = (Z_AdminEsRequest *) odr_malloc(out, sizeof(*r->u.adminService->u.esRequest));
74
75     toKeep = r->u.adminService->u.esRequest->toKeep = (Z_ESAdminOriginPartToKeep *) 
76                      odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->toKeep));
77
78     switch ( type )
79     {
80         case 1:
81             toKeep->which=Z_ESAdminOriginPartToKeep_reIndex;
82             toKeep->u.reIndex=odr_nullval();
83             break;
84         case 2:
85             toKeep->which=Z_ESAdminOriginPartToKeep_truncate;
86             toKeep->u.truncate=odr_nullval();
87             break;
88         case 3:
89             toKeep->which=Z_ESAdminOriginPartToKeep_delete;
90             toKeep->u.delete=odr_nullval();
91             break;
92         case 4:
93             toKeep->which=Z_ESAdminOriginPartToKeep_create;
94             toKeep->u.create=odr_nullval();
95             break;
96         case 5:
97             toKeep->which=Z_ESAdminOriginPartToKeep_import;
98             toKeep->u.import=odr_nullval();
99             break;
100         case 6:
101             toKeep->which=Z_ESAdminOriginPartToKeep_refresh;
102             toKeep->u.refresh=odr_nullval();
103             break;
104         case 7:
105             toKeep->which=Z_ESAdminOriginPartToKeep_commit;
106             toKeep->u.commit=odr_nullval();
107             break;
108     }
109
110     toKeep->databaseName = dbname;
111
112
113     notToKeep = r->u.adminService->u.esRequest->notToKeep = (Z_ESAdminOriginPartNotToKeep *)
114         odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->notToKeep));
115     notToKeep->which=Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
116     notToKeep->u.recordsWillFollow=odr_nullval();
117     
118     send_apdu(apdu);
119
120     return 2;
121 }
122
123 /* cmd_adm_reindex <dbname>
124    Ask the specified database to fully reindex itself */
125 int cmd_adm_reindex(char* arg)
126 {
127     sendAdminES(1,arg);
128 }
129
130 /* cmd_adm_truncate <dbname>
131    Truncate the specified database, removing all records and index entries, but leaving 
132    the database & it's explain information intact ready for new records */
133 int cmd_adm_truncate(char* arg)
134 {
135     sendAdminES(2,arg);
136 }
137
138 /* cmd_adm_create <dbname>
139    Create a new database */
140 int cmd_adm_create(char* arg)
141 {
142     sendAdminES(4,arg);
143 }
144
145 /* cmd_adm_delete <dbname>
146    Delete a database */
147 int cmd_adm_delete(char* arg)
148 {
149     sendAdminES(3,arg);
150 }
151
152 /* cmd_adm_import <dbname> <rectype> <sourcefile>
153    Import the specified updated into the database
154    N.B. That in this case, the import may contain instructions to delete records as well as new or updates
155    to existing records */
156 int cmd_adm_import(char* arg)
157 {
158     sendAdminES(5,arg);
159 }
160
161 /* "Freshen" the specified database, by checking metadata records against the sources from which they were 
162    generated, and creating a new record if the source has been touched since the last extraction */
163 int cmd_adm_refresh(char* arg)
164 {
165     sendAdminES(6,arg);
166 }
167
168 /* cmd_adm_commit 
169    Make imported records a permenant & visible to the live system */
170 int cmd_adm_commit(char* arg)
171 {
172     sendAdminES(7,NULL);
173 }
174