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