2 * Copyright (c) 1995-2001, Index Data
3 * See the file LICENSE for details.
5 * $Id: admin.c,v 1.12 2002-01-17 21:04:43 adam Exp $
19 #include <yaz/yaz-util.h>
21 #include <yaz/tcpip.h>
23 #include <yaz/xmosi.h>
26 #include <yaz/proto.h>
27 #include <yaz/marcdisp.h>
28 #include <yaz/diagbib1.h>
30 #include <yaz/pquery.h>
33 /* Helper functions to get to various statics in the client */
34 ODR getODROutputStream();
35 void send_apdu(Z_APDU *a);
37 extern char *databaseNames[];
38 extern int num_databaseNames;
40 int sendAdminES(int type, char* param1)
42 ODR out = getODROutputStream();
43 char *dbname = odr_strdup (out, databaseNames[0]);
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;
50 Z_ESAdminOriginPartToKeep *toKeep;
51 Z_ESAdminOriginPartNotToKeep *notToKeep;
53 printf ("Admin request\n");
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;
61 oid_ent_to_oid (&update_oid, oid);
62 req->packageType = odr_oiddup(out,oid);
63 req->packageName = "1.Extendedserveq";
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;
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));
75 toKeep = r->u.adminService->u.esRequest->toKeep = (Z_ESAdminOriginPartToKeep *)
76 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->toKeep));
79 toKeep->databaseName = dbname;
82 case Z_ESAdminOriginPartToKeep_reIndex:
83 toKeep->u.reIndex=odr_nullval();
86 case Z_ESAdminOriginPartToKeep_truncate:
87 toKeep->u.truncate=odr_nullval();
89 case Z_ESAdminOriginPartToKeep_drop:
90 toKeep->u.drop=odr_nullval();
92 case Z_ESAdminOriginPartToKeep_create:
93 toKeep->u.create=odr_nullval();
95 case Z_ESAdminOriginPartToKeep_import:
96 toKeep->u.import = (Z_ImportParameters*)odr_malloc(out, sizeof(*toKeep->u.import));
97 toKeep->u.import->recordType=param1;
98 /* Need to add additional setup of records here */
100 case Z_ESAdminOriginPartToKeep_refresh:
101 toKeep->u.refresh=odr_nullval();
103 case Z_ESAdminOriginPartToKeep_commit:
104 toKeep->u.commit=odr_nullval();
106 case Z_ESAdminOriginPartToKeep_shutdown:
107 toKeep->u.commit=odr_nullval();
109 case Z_ESAdminOriginPartToKeep_start:
110 toKeep->u.commit=odr_nullval();
113 /* Unknown admin service */
117 notToKeep = r->u.adminService->u.esRequest->notToKeep =
118 (Z_ESAdminOriginPartNotToKeep *)
119 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->notToKeep));
120 notToKeep->which=Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
121 notToKeep->u.recordsWillFollow=odr_nullval();
129 Ask the specified database to fully reindex itself */
130 int cmd_adm_reindex(char* arg)
132 sendAdminES(Z_ESAdminOriginPartToKeep_reIndex, NULL);
137 Truncate the specified database, removing all records and index entries, but leaving
138 the database & it's explain information intact ready for new records */
139 int cmd_adm_truncate(char* arg)
143 sendAdminES(Z_ESAdminOriginPartToKeep_truncate, NULL);
150 Create a new database */
151 int cmd_adm_create(char* arg)
155 sendAdminES(Z_ESAdminOriginPartToKeep_create, NULL);
162 Drop (Delete) a database */
163 int cmd_adm_drop(char* arg)
167 sendAdminES(Z_ESAdminOriginPartToKeep_drop, NULL);
173 /* cmd_adm_import <dbname> <rectype> <sourcefile>
174 Import the specified updated into the database
175 N.B. That in this case, the import may contain instructions to delete records as well as new or updates
176 to existing records */
179 int cmd_adm_import(char *arg)
181 char type_str[20], dir_str[1024], pattern_str[1024];
188 Z_Segment *segment = 0;
189 ODR out = getODROutputStream();
191 if (arg && sscanf (arg, "%19s %1023s %1023s", type_str,
192 dir_str, pattern_str) != 3)
194 if (num_databaseNames != 1)
196 dir = opendir(dir_str);
200 sendAdminES(Z_ESAdminOriginPartToKeep_import, type_str);
202 printf ("sent es request\n");
203 if ((cp=strrchr(dir_str, '/')) && cp[1] == 0)
206 while ((ent = readdir(dir)))
208 if (fnmatch (pattern_str, ent->d_name, 0) == 0)
214 sprintf (fname, "%s%s%s", dir_str, sep, ent->d_name);
215 stat (fname, &status);
217 if (S_ISREG(status.st_mode) && (inf = fopen(fname, "r")))
219 Z_NamePlusRecord *rec;
220 Odr_oct *oct = (Odr_oct *) odr_malloc (out, sizeof(*oct));
224 apdu = zget_APDU(out, Z_APDU_segmentRequest);
225 segment = apdu->u.segmentRequest;
226 segment->segmentRecords = (Z_NamePlusRecord **)
227 odr_malloc (out, chunk * sizeof(*segment->segmentRecords));
229 rec = (Z_NamePlusRecord *) odr_malloc (out, sizeof(*rec));
230 rec->databaseName = 0;
231 rec->which = Z_NamePlusRecord_intermediateFragment;
232 rec->u.intermediateFragment = (Z_FragmentSyntax *)
233 odr_malloc (out, sizeof(*rec->u.intermediateFragment));
234 rec->u.intermediateFragment->which =
235 Z_FragmentSyntax_notExternallyTagged;
236 rec->u.intermediateFragment->u.notExternallyTagged = oct;
238 oct->len = oct->size = status.st_size;
239 oct->buf = (unsigned char *) odr_malloc (out, oct->size);
240 fread (oct->buf, 1, oct->size, inf);
243 segment->segmentRecords[segment->num_segmentRecords++] = rec;
245 if (segment->num_segmentRecords == chunk)
255 apdu = zget_APDU(out, Z_APDU_segmentRequest);
261 int cmd_adm_import(char *arg)
263 printf ("not available on WIN32\n");
268 int cmd_adm_import2(char* arg)
270 /* Buffer for reading chunks of data from import file */
271 char chunk_buffer[8192];
275 char rectype_buff[32];
276 char filename_buff[32];
277 FILE* pImportFile = NULL;
279 if (sscanf (arg, "%s %s", rectype_buff, filename_buff) != 3)
281 printf("Must specify database-name, record-type and filename for import\n");
285 /* Attempt to open the file */
287 pImportFile = fopen(filename_buff,"r");
289 /* This chunk of code should move into client.c sometime soon for sending files via the update es */
290 /* This function will then refer to the standard client.c one for uploading a file using es update */
293 int iTotalWritten = 0;
295 /* We opened the import file without problems... So no we send the es request, ready to
296 start sending fragments of the import file as segment messages */
297 sendAdminES(Z_ESAdminOriginPartToKeep_import, rectype_buff);
299 while ( ! feof(pImportFile ) )
301 /* Read buffer_size bytes from the file */
302 size_t num_items = fread((void*)chunk_buffer, 1, sizeof(chunk_buffer), pImportFile);
304 /* Write num_bytes of data to */
306 if ( feof(pImportFile ) )
308 /* This is the last chunk... Write it as the last fragment */
309 printf("Last segment of %d bytes\n", num_items);
311 else if ( iTotalWritten == 0 )
313 printf("First segment of %d bytes\n",num_items);
317 printf("Writing %d bytes\n", num_items);
320 iTotalWritten += num_items;
328 /* "Freshen" the specified database, by checking metadata records against the sources from which they were
329 generated, and creating a new record if the source has been touched since the last extraction */
330 int cmd_adm_refresh(char* arg)
334 sendAdminES(Z_ESAdminOriginPartToKeep_refresh, NULL);
341 Make imported records a permenant & visible to the live system */
342 int cmd_adm_commit(char* arg)
344 sendAdminES(Z_ESAdminOriginPartToKeep_commit, NULL);
348 int cmd_adm_shutdown(char* arg)
350 sendAdminES(Z_ESAdminOriginPartToKeep_shutdown, NULL);
354 int cmd_adm_startup(char* arg)
356 sendAdminES(Z_ESAdminOriginPartToKeep_start, NULL);