3 * Revision 1.10 2001-07-19 19:51:41 adam
4 * Added typecasts to make C++ happy.
6 * Revision 1.9 2001/02/21 13:46:53 adam
9 * Revision 1.8 2000/04/17 14:21:38 adam
12 * Revision 1.7 2000/04/05 07:39:54 adam
13 * Added shared library support (libtool).
15 * Revision 1.6 2000/03/20 19:06:25 adam
16 * Added Segment request for fronend server. Work on admin for client.
18 * Revision 1.5 2000/03/17 12:47:02 adam
19 * Minor changes to admin client.
21 * Revision 1.4 2000/03/16 13:55:49 ian
22 * Added commands for sending shutdown and startup admin requests via the admin ES.
24 * Revision 1.3 2000/03/14 15:23:17 ian
25 * Removed unwanted ifdef and include of zes-admin.h
27 * Revision 1.2 2000/03/14 14:06:04 ian
28 * Minor change to order of debugging output for send_apdu,
29 * fixed encoding of admin request.
31 * Revision 1.1 2000/03/14 09:27:07 ian
32 * Added code to enable sending of admin extended service requests
50 #include <yaz/yaz-util.h>
52 #include <yaz/tcpip.h>
54 #include <yaz/xmosi.h>
57 #include <yaz/proto.h>
58 #include <yaz/marcdisp.h>
59 #include <yaz/diagbib1.h>
61 #include <yaz/pquery.h>
65 /* Helper functions to get to various statics in the client */
66 ODR getODROutputStream();
67 void send_apdu(Z_APDU *a);
69 extern char *databaseNames[];
70 extern int num_databaseNames;
72 int sendAdminES(int type, char* param1)
74 ODR out = getODROutputStream();
75 char *dbname = odr_strdup (out, databaseNames[0]);
77 /* Type: 1=reindex, 2=truncate, 3=delete, 4=create, 5=import, 6=refresh, 7=commit */
78 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
79 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
82 Z_ESAdminOriginPartToKeep *toKeep;
83 Z_ESAdminOriginPartNotToKeep *notToKeep;
85 printf ("Admin request\n");
88 /* Set up the OID for the external */
89 update_oid.proto = PROTO_Z3950;
90 update_oid.oclass = CLASS_EXTSERV;
91 update_oid.value = VAL_ADMINSERVICE;
93 oid_ent_to_oid (&update_oid, oid);
94 req->packageType = odr_oiddup(out,oid);
95 req->packageName = "1.Extendedserveq";
97 /* Allocate the external */
98 r = req->taskSpecificParameters = (Z_External *) odr_malloc (out, sizeof(*r));
99 r->direct_reference = odr_oiddup(out,oid);
100 r->indirect_reference = 0;
102 r->which = Z_External_ESAdmin;
103 r->u.adminService = (Z_Admin *) odr_malloc(out, sizeof(*r->u.adminService));
104 r->u.adminService->which = Z_Admin_esRequest;
105 r->u.adminService->u.esRequest = (Z_AdminEsRequest *) odr_malloc(out, sizeof(*r->u.adminService->u.esRequest));
107 toKeep = r->u.adminService->u.esRequest->toKeep = (Z_ESAdminOriginPartToKeep *)
108 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->toKeep));
111 toKeep->databaseName = dbname;
114 case Z_ESAdminOriginPartToKeep_reIndex:
115 toKeep->u.reIndex=odr_nullval();
118 case Z_ESAdminOriginPartToKeep_truncate:
119 toKeep->u.truncate=odr_nullval();
121 case Z_ESAdminOriginPartToKeep_drop:
122 toKeep->u.drop=odr_nullval();
124 case Z_ESAdminOriginPartToKeep_create:
125 toKeep->u.create=odr_nullval();
127 case Z_ESAdminOriginPartToKeep_import:
128 toKeep->u.import = (Z_ImportParameters*)odr_malloc(out, sizeof(*toKeep->u.import));
129 toKeep->u.import->recordType=param1;
130 /* Need to add additional setup of records here */
132 case Z_ESAdminOriginPartToKeep_refresh:
133 toKeep->u.refresh=odr_nullval();
135 case Z_ESAdminOriginPartToKeep_commit:
136 toKeep->u.commit=odr_nullval();
138 case Z_ESAdminOriginPartToKeep_shutdown:
139 toKeep->u.commit=odr_nullval();
141 case Z_ESAdminOriginPartToKeep_start:
142 toKeep->u.commit=odr_nullval();
145 /* Unknown admin service */
149 notToKeep = r->u.adminService->u.esRequest->notToKeep =
150 (Z_ESAdminOriginPartNotToKeep *)
151 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->notToKeep));
152 notToKeep->which=Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
153 notToKeep->u.recordsWillFollow=odr_nullval();
161 Ask the specified database to fully reindex itself */
162 int cmd_adm_reindex(char* arg)
164 sendAdminES(Z_ESAdminOriginPartToKeep_reIndex, NULL);
169 Truncate the specified database, removing all records and index entries, but leaving
170 the database & it's explain information intact ready for new records */
171 int cmd_adm_truncate(char* arg)
175 sendAdminES(Z_ESAdminOriginPartToKeep_truncate, NULL);
182 Create a new database */
183 int cmd_adm_create(char* arg)
187 sendAdminES(Z_ESAdminOriginPartToKeep_create, NULL);
194 Drop (Delete) a database */
195 int cmd_adm_drop(char* arg)
199 sendAdminES(Z_ESAdminOriginPartToKeep_drop, NULL);
205 /* cmd_adm_import <dbname> <rectype> <sourcefile>
206 Import the specified updated into the database
207 N.B. That in this case, the import may contain instructions to delete records as well as new or updates
208 to existing records */
211 int cmd_adm_import(char *arg)
213 printf ("not available on WIN32\n");
219 int cmd_adm_import(char *arg)
221 char type_str[20], dir_str[1024], pattern_str[1024];
228 Z_Segment *segment = 0;
229 ODR out = getODROutputStream();
231 if (arg && sscanf (arg, "%19s %1023s %1023s", type_str,
232 dir_str, pattern_str) != 3)
234 if (num_databaseNames != 1)
236 dir = opendir(dir_str);
240 sendAdminES(Z_ESAdminOriginPartToKeep_import, type_str);
242 printf ("sent es request\n");
243 if ((cp=strrchr(dir_str, '/')) && cp[1] == 0)
246 while ((ent = readdir(dir)))
248 if (fnmatch (pattern_str, ent->d_name, 0) == 0)
254 sprintf (fname, "%s%s%s", dir_str, sep, ent->d_name);
255 stat (fname, &status);
257 if (S_ISREG(status.st_mode) && (inf = fopen(fname, "r")))
259 Z_NamePlusRecord *rec;
260 Odr_oct *oct = (Odr_oct *) odr_malloc (out, sizeof(*oct));
264 apdu = zget_APDU(out, Z_APDU_segmentRequest);
265 segment = apdu->u.segmentRequest;
266 segment->segmentRecords = (Z_NamePlusRecord **)
267 odr_malloc (out, chunk * sizeof(*segment->segmentRecords));
269 rec = (Z_NamePlusRecord *) odr_malloc (out, sizeof(*rec));
270 rec->databaseName = 0;
271 rec->which = Z_NamePlusRecord_intermediateFragment;
272 rec->u.intermediateFragment = (Z_FragmentSyntax *)
273 odr_malloc (out, sizeof(*rec->u.intermediateFragment));
274 rec->u.intermediateFragment->which =
275 Z_FragmentSyntax_notExternallyTagged;
276 rec->u.intermediateFragment->u.notExternallyTagged = oct;
278 oct->len = oct->size = status.st_size;
279 oct->buf = (unsigned char *) odr_malloc (out, oct->size);
280 fread (oct->buf, 1, oct->size, inf);
283 segment->segmentRecords[segment->num_segmentRecords++] = rec;
285 if (segment->num_segmentRecords == chunk)
295 apdu = zget_APDU(out, Z_APDU_segmentRequest);
302 int cmd_adm_import2(char* arg)
304 /* Buffer for reading chunks of data from import file */
305 char chunk_buffer[8192];
309 char rectype_buff[32];
310 char filename_buff[32];
311 FILE* pImportFile = NULL;
313 if (sscanf (arg, "%s %s", rectype_buff, filename_buff) != 3)
315 printf("Must specify database-name, record-type and filename for import\n");
319 /* Attempt to open the file */
321 pImportFile = fopen(filename_buff,"r");
323 /* This chunk of code should move into client.c sometime soon for sending files via the update es */
324 /* This function will then refer to the standard client.c one for uploading a file using es update */
327 int iTotalWritten = 0;
329 /* We opened the import file without problems... So no we send the es request, ready to
330 start sending fragments of the import file as segment messages */
331 sendAdminES(Z_ESAdminOriginPartToKeep_import, rectype_buff);
333 while ( ! feof(pImportFile ) )
335 /* Read buffer_size bytes from the file */
336 size_t num_items = fread((void*)chunk_buffer, 1, sizeof(chunk_buffer), pImportFile);
338 /* Write num_bytes of data to */
340 if ( feof(pImportFile ) )
342 /* This is the last chunk... Write it as the last fragment */
343 printf("Last segment of %d bytes\n", num_items);
345 else if ( iTotalWritten == 0 )
347 printf("First segment of %d bytes\n",num_items);
351 printf("Writing %d bytes\n", num_items);
354 iTotalWritten += num_items;
362 /* "Freshen" the specified database, by checking metadata records against the sources from which they were
363 generated, and creating a new record if the source has been touched since the last extraction */
364 int cmd_adm_refresh(char* arg)
368 sendAdminES(Z_ESAdminOriginPartToKeep_refresh, NULL);
375 Make imported records a permenant & visible to the live system */
376 int cmd_adm_commit(char* arg)
378 sendAdminES(Z_ESAdminOriginPartToKeep_commit, NULL);
382 int cmd_adm_shutdown(char* arg)
384 sendAdminES(Z_ESAdminOriginPartToKeep_shutdown, NULL);
388 int cmd_adm_startup(char* arg)
390 sendAdminES(Z_ESAdminOriginPartToKeep_start, NULL);