Minor changes to admin client.
[yaz-moved-to-github.git] / client / admin.c
1 /*
2  * $Log: admin.c,v $
3  * Revision 1.5  2000-03-17 12:47:02  adam
4  * Minor changes to admin client.
5  *
6  * Revision 1.4  2000/03/16 13:55:49  ian
7  * Added commands for sending shutdown and startup admin requests via the admin ES.
8  *
9  * Revision 1.3  2000/03/14 15:23:17  ian
10  * Removed unwanted ifdef and include of zes-admin.h
11  *
12  * Revision 1.2  2000/03/14 14:06:04  ian
13  * Minor change to order of debugging output for send_apdu,
14  * fixed encoding of admin request.
15  *
16  * Revision 1.1  2000/03/14 09:27:07  ian
17  * Added code to enable sending of admin extended service requests
18  *
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <time.h>
25
26 #include <yaz/yaz-util.h>
27
28 #include <yaz/tcpip.h>
29 #ifdef USE_XTIMOSI
30 #include <yaz/xmosi.h>
31 #endif
32
33 #include <yaz/proto.h>
34 #include <yaz/marcdisp.h>
35 #include <yaz/diagbib1.h>
36
37 #include <yaz/pquery.h>
38
39 /* Helper functions to get to various statics in the client */
40 ODR getODROutputStream();
41 void send_apdu(Z_APDU *a);
42
43
44
45 int sendAdminES(int type, char* dbname, char* param1)
46 {
47     ODR out = getODROutputStream();
48
49     /* Type: 1=reindex, 2=truncate, 3=delete, 4=create, 5=import, 6=refresh, 7=commit */
50     Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
51     Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
52     Z_External *r;
53     int oid[OID_SIZE];
54     Z_ESAdminOriginPartToKeep  *toKeep;
55     Z_ESAdminOriginPartNotToKeep  *notToKeep;
56     oident update_oid;
57     printf ("Admin request\n");
58     fflush(stdout);
59
60     /* Set up the OID for the external */
61     update_oid.proto = PROTO_Z3950;
62     update_oid.oclass = CLASS_EXTSERV;
63     update_oid.value = VAL_ADMINSERVICE;
64
65     oid_ent_to_oid (&update_oid, oid);
66     req->packageType = odr_oiddup(out,oid);
67     req->packageName = "1.Extendedserveq";
68
69     /* Allocate the external */
70     r = req->taskSpecificParameters = (Z_External *) odr_malloc (out, sizeof(*r));
71     r->direct_reference = odr_oiddup(out,oid);
72     r->indirect_reference = 0;
73     r->descriptor = 0;
74     r->which = Z_External_ESAdmin;
75     r->u.adminService = (Z_Admin *) odr_malloc(out, sizeof(*r->u.adminService));
76     r->u.adminService->which = Z_Admin_esRequest;
77     r->u.adminService->u.esRequest = (Z_AdminEsRequest *) odr_malloc(out, sizeof(*r->u.adminService->u.esRequest));
78
79     toKeep = r->u.adminService->u.esRequest->toKeep = (Z_ESAdminOriginPartToKeep *) 
80                      odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->toKeep));
81
82     toKeep->which=type;
83
84     switch ( type )
85     {
86         case Z_ESAdminOriginPartToKeep_reIndex:
87             toKeep->u.reIndex=odr_nullval();
88             toKeep->databaseName = dbname;
89             break;
90
91         case Z_ESAdminOriginPartToKeep_truncate:
92             toKeep->u.truncate=odr_nullval();
93             toKeep->databaseName = dbname;
94             break;
95
96         case Z_ESAdminOriginPartToKeep_delete:
97             toKeep->u.delete=odr_nullval();
98             toKeep->databaseName = dbname;
99             break;
100
101         case Z_ESAdminOriginPartToKeep_create:
102             toKeep->u.create=odr_nullval();
103             toKeep->databaseName = dbname;
104             break;
105
106         case Z_ESAdminOriginPartToKeep_import:
107             toKeep->u.import = (Z_ImportParameters*)odr_malloc(out, sizeof(*toKeep->u.import));
108             toKeep->u.import->recordType=param1;
109             toKeep->databaseName = dbname;
110             /* Need to add additional setup of records here */
111             break;
112
113         case Z_ESAdminOriginPartToKeep_refresh:
114             toKeep->u.refresh=odr_nullval();
115             toKeep->databaseName = dbname;
116             break;
117
118         case Z_ESAdminOriginPartToKeep_commit:
119             toKeep->u.commit=odr_nullval();
120             break;
121
122         case Z_ESAdminOriginPartToKeep_shutdown:
123             toKeep->u.commit=odr_nullval();
124             break;
125             
126         case Z_ESAdminOriginPartToKeep_start:
127             toKeep->u.commit=odr_nullval();
128             break;
129
130         default:
131             /* Unknown admin service */
132             break;
133     }
134
135     notToKeep = r->u.adminService->u.esRequest->notToKeep = (Z_ESAdminOriginPartNotToKeep *)
136         odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->notToKeep));
137     notToKeep->which=Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
138     notToKeep->u.recordsWillFollow=odr_nullval();
139     
140     send_apdu(apdu);
141
142     return 0;
143 }
144
145 /* cmd_adm_reindex <dbname>
146    Ask the specified database to fully reindex itself */
147 int cmd_adm_reindex(char* arg)
148 {
149     sendAdminES(Z_ESAdminOriginPartToKeep_reIndex,arg,NULL);
150     return 2;
151 }
152
153 /* cmd_adm_truncate <dbname>
154    Truncate the specified database, removing all records and index entries, but leaving 
155    the database & it's explain information intact ready for new records */
156 int cmd_adm_truncate(char* arg)
157 {
158     if ( arg )
159     {
160         sendAdminES(Z_ESAdminOriginPartToKeep_truncate,arg,NULL);
161         return 2;
162     }
163     return 0;
164 }
165
166 /* cmd_adm_create <dbname>
167    Create a new database */
168 int cmd_adm_create(char* arg)
169 {
170     if ( arg )
171     {
172         sendAdminES(Z_ESAdminOriginPartToKeep_create,arg,NULL);
173         return 2;
174     }
175     return 0;
176 }
177
178 /* cmd_adm_delete <dbname>
179    Delete a database */
180 int cmd_adm_delete(char* arg)
181 {
182     if ( arg )
183     {
184         sendAdminES(Z_ESAdminOriginPartToKeep_delete,arg,NULL);
185         return 2;
186     }
187     return 0;
188 }
189
190 /* cmd_adm_import <dbname> <rectype> <sourcefile>
191    Import the specified updated into the database
192    N.B. That in this case, the import may contain instructions to delete records as well as new or updates
193    to existing records */
194 int cmd_adm_import(char* arg)
195 {
196     /* Size of chunks we wish to read from import file */
197     size_t chunk_size = 8192;
198
199     /* Buffer for reading chunks of data from import file */
200     char chunk_buffer[chunk_size];
201
202     if ( arg )
203     {
204         char dbname_buff[32];
205         char rectype_buff[32];
206         char filename_buff[32];
207         FILE* pImportFile = NULL;
208
209         if (sscanf (arg, "%s %s %s", dbname_buff, rectype_buff, filename_buff) != 3)
210         {
211             printf("Must specify database-name, record-type and filename for import\n");
212             return 0;
213         }
214
215         /* Attempt to open the file */
216
217         pImportFile = fopen(filename_buff,"r");
218
219         /* This chunk of code should move into client.c sometime soon for sending files via the update es */
220         /* This function will then refer to the standard client.c one for uploading a file using es update */
221         if ( pImportFile )
222         {
223             int iTotalWritten = 0;
224
225             /* We opened the import file without problems... So no we send the es request, ready to 
226                start sending fragments of the import file as segment messages */
227             sendAdminES(Z_ESAdminOriginPartToKeep_import,arg,rectype_buff);
228
229             while ( ! feof(pImportFile ) )
230             {
231                 /* Read buffer_size bytes from the file */
232                 size_t num_items = fread((void*)chunk_buffer, 1, sizeof(chunk_buffer),  pImportFile);
233
234                 /* Write num_bytes of data to */
235
236                 if ( feof(pImportFile ) )
237                 {
238                     /* This is the last chunk... Write it as the last fragment */
239                     printf("Last segment of %d bytes\n", num_items);
240                 }
241                 else if ( iTotalWritten == 0 )
242                 {
243                     printf("First segment of %d bytes\n",num_items);
244                 }
245                 else
246                 {
247                     printf("Writing %d bytes\n", num_items);
248                 }
249
250                 iTotalWritten += num_items;
251             }
252         }
253         return 2;
254     }
255     return 0;
256 }
257
258 /* "Freshen" the specified database, by checking metadata records against the sources from which they were 
259    generated, and creating a new record if the source has been touched since the last extraction */
260 int cmd_adm_refresh(char* arg)
261 {
262     if ( arg )
263     {
264         sendAdminES(Z_ESAdminOriginPartToKeep_refresh,arg,NULL);
265         return 2;
266     }
267     return 0;
268 }
269
270 /* cmd_adm_commit 
271    Make imported records a permenant & visible to the live system */
272 int cmd_adm_commit(char* arg)
273 {
274     sendAdminES(Z_ESAdminOriginPartToKeep_commit,NULL,NULL);
275     return 2;
276 }
277
278 int cmd_adm_shutdown(char* arg)
279 {
280     sendAdminES(Z_ESAdminOriginPartToKeep_shutdown,NULL,NULL);
281     return 2;
282 }
283
284 int cmd_adm_startup(char* arg)
285 {
286     sendAdminES(Z_ESAdminOriginPartToKeep_start,NULL,NULL);
287     return 2;
288 }
289