2 * Copyright (c) 1995-2001, Index Data
3 * See the file LICENSE for details.
5 * $Id: admin.c,v 1.11 2001-11-13 23:00:42 adam Exp $
21 #include <yaz/yaz-util.h>
23 #include <yaz/tcpip.h>
25 #include <yaz/xmosi.h>
28 #include <yaz/proto.h>
29 #include <yaz/marcdisp.h>
30 #include <yaz/diagbib1.h>
32 #include <yaz/pquery.h>
35 /* Helper functions to get to various statics in the client */
36 ODR getODROutputStream();
37 void send_apdu(Z_APDU *a);
39 extern char *databaseNames[];
40 extern int num_databaseNames;
42 int sendAdminES(int type, char* param1)
44 ODR out = getODROutputStream();
45 char *dbname = odr_strdup (out, databaseNames[0]);
47 /* Type: 1=reindex, 2=truncate, 3=delete, 4=create, 5=import, 6=refresh, 7=commit */
48 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
49 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
52 Z_ESAdminOriginPartToKeep *toKeep;
53 Z_ESAdminOriginPartNotToKeep *notToKeep;
55 printf ("Admin request\n");
58 /* Set up the OID for the external */
59 update_oid.proto = PROTO_Z3950;
60 update_oid.oclass = CLASS_EXTSERV;
61 update_oid.value = VAL_ADMINSERVICE;
63 oid_ent_to_oid (&update_oid, oid);
64 req->packageType = odr_oiddup(out,oid);
65 req->packageName = "1.Extendedserveq";
67 /* Allocate the external */
68 r = req->taskSpecificParameters = (Z_External *) odr_malloc (out, sizeof(*r));
69 r->direct_reference = odr_oiddup(out,oid);
70 r->indirect_reference = 0;
72 r->which = Z_External_ESAdmin;
73 r->u.adminService = (Z_Admin *) odr_malloc(out, sizeof(*r->u.adminService));
74 r->u.adminService->which = Z_Admin_esRequest;
75 r->u.adminService->u.esRequest = (Z_AdminEsRequest *) odr_malloc(out, sizeof(*r->u.adminService->u.esRequest));
77 toKeep = r->u.adminService->u.esRequest->toKeep = (Z_ESAdminOriginPartToKeep *)
78 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->toKeep));
81 toKeep->databaseName = dbname;
84 case Z_ESAdminOriginPartToKeep_reIndex:
85 toKeep->u.reIndex=odr_nullval();
88 case Z_ESAdminOriginPartToKeep_truncate:
89 toKeep->u.truncate=odr_nullval();
91 case Z_ESAdminOriginPartToKeep_drop:
92 toKeep->u.drop=odr_nullval();
94 case Z_ESAdminOriginPartToKeep_create:
95 toKeep->u.create=odr_nullval();
97 case Z_ESAdminOriginPartToKeep_import:
98 toKeep->u.import = (Z_ImportParameters*)odr_malloc(out, sizeof(*toKeep->u.import));
99 toKeep->u.import->recordType=param1;
100 /* Need to add additional setup of records here */
102 case Z_ESAdminOriginPartToKeep_refresh:
103 toKeep->u.refresh=odr_nullval();
105 case Z_ESAdminOriginPartToKeep_commit:
106 toKeep->u.commit=odr_nullval();
108 case Z_ESAdminOriginPartToKeep_shutdown:
109 toKeep->u.commit=odr_nullval();
111 case Z_ESAdminOriginPartToKeep_start:
112 toKeep->u.commit=odr_nullval();
115 /* Unknown admin service */
119 notToKeep = r->u.adminService->u.esRequest->notToKeep =
120 (Z_ESAdminOriginPartNotToKeep *)
121 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->notToKeep));
122 notToKeep->which=Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
123 notToKeep->u.recordsWillFollow=odr_nullval();
131 Ask the specified database to fully reindex itself */
132 int cmd_adm_reindex(char* arg)
134 sendAdminES(Z_ESAdminOriginPartToKeep_reIndex, NULL);
139 Truncate the specified database, removing all records and index entries, but leaving
140 the database & it's explain information intact ready for new records */
141 int cmd_adm_truncate(char* arg)
145 sendAdminES(Z_ESAdminOriginPartToKeep_truncate, NULL);
152 Create a new database */
153 int cmd_adm_create(char* arg)
157 sendAdminES(Z_ESAdminOriginPartToKeep_create, NULL);
164 Drop (Delete) a database */
165 int cmd_adm_drop(char* arg)
169 sendAdminES(Z_ESAdminOriginPartToKeep_drop, NULL);
175 /* cmd_adm_import <dbname> <rectype> <sourcefile>
176 Import the specified updated into the database
177 N.B. That in this case, the import may contain instructions to delete records as well as new or updates
178 to existing records */
181 int cmd_adm_import(char *arg)
183 printf ("not available on WIN32\n");
189 int cmd_adm_import(char *arg)
191 char type_str[20], dir_str[1024], pattern_str[1024];
198 Z_Segment *segment = 0;
199 ODR out = getODROutputStream();
201 if (arg && sscanf (arg, "%19s %1023s %1023s", type_str,
202 dir_str, pattern_str) != 3)
204 if (num_databaseNames != 1)
206 dir = opendir(dir_str);
210 sendAdminES(Z_ESAdminOriginPartToKeep_import, type_str);
212 printf ("sent es request\n");
213 if ((cp=strrchr(dir_str, '/')) && cp[1] == 0)
216 while ((ent = readdir(dir)))
218 if (fnmatch (pattern_str, ent->d_name, 0) == 0)
224 sprintf (fname, "%s%s%s", dir_str, sep, ent->d_name);
225 stat (fname, &status);
227 if (S_ISREG(status.st_mode) && (inf = fopen(fname, "r")))
229 Z_NamePlusRecord *rec;
230 Odr_oct *oct = (Odr_oct *) odr_malloc (out, sizeof(*oct));
234 apdu = zget_APDU(out, Z_APDU_segmentRequest);
235 segment = apdu->u.segmentRequest;
236 segment->segmentRecords = (Z_NamePlusRecord **)
237 odr_malloc (out, chunk * sizeof(*segment->segmentRecords));
239 rec = (Z_NamePlusRecord *) odr_malloc (out, sizeof(*rec));
240 rec->databaseName = 0;
241 rec->which = Z_NamePlusRecord_intermediateFragment;
242 rec->u.intermediateFragment = (Z_FragmentSyntax *)
243 odr_malloc (out, sizeof(*rec->u.intermediateFragment));
244 rec->u.intermediateFragment->which =
245 Z_FragmentSyntax_notExternallyTagged;
246 rec->u.intermediateFragment->u.notExternallyTagged = oct;
248 oct->len = oct->size = status.st_size;
249 oct->buf = (unsigned char *) odr_malloc (out, oct->size);
250 fread (oct->buf, 1, oct->size, inf);
253 segment->segmentRecords[segment->num_segmentRecords++] = rec;
255 if (segment->num_segmentRecords == chunk)
265 apdu = zget_APDU(out, Z_APDU_segmentRequest);
272 int cmd_adm_import2(char* arg)
274 /* Buffer for reading chunks of data from import file */
275 char chunk_buffer[8192];
279 char rectype_buff[32];
280 char filename_buff[32];
281 FILE* pImportFile = NULL;
283 if (sscanf (arg, "%s %s", rectype_buff, filename_buff) != 3)
285 printf("Must specify database-name, record-type and filename for import\n");
289 /* Attempt to open the file */
291 pImportFile = fopen(filename_buff,"r");
293 /* This chunk of code should move into client.c sometime soon for sending files via the update es */
294 /* This function will then refer to the standard client.c one for uploading a file using es update */
297 int iTotalWritten = 0;
299 /* We opened the import file without problems... So no we send the es request, ready to
300 start sending fragments of the import file as segment messages */
301 sendAdminES(Z_ESAdminOriginPartToKeep_import, rectype_buff);
303 while ( ! feof(pImportFile ) )
305 /* Read buffer_size bytes from the file */
306 size_t num_items = fread((void*)chunk_buffer, 1, sizeof(chunk_buffer), pImportFile);
308 /* Write num_bytes of data to */
310 if ( feof(pImportFile ) )
312 /* This is the last chunk... Write it as the last fragment */
313 printf("Last segment of %d bytes\n", num_items);
315 else if ( iTotalWritten == 0 )
317 printf("First segment of %d bytes\n",num_items);
321 printf("Writing %d bytes\n", num_items);
324 iTotalWritten += num_items;
332 /* "Freshen" the specified database, by checking metadata records against the sources from which they were
333 generated, and creating a new record if the source has been touched since the last extraction */
334 int cmd_adm_refresh(char* arg)
338 sendAdminES(Z_ESAdminOriginPartToKeep_refresh, NULL);
345 Make imported records a permenant & visible to the live system */
346 int cmd_adm_commit(char* arg)
348 sendAdminES(Z_ESAdminOriginPartToKeep_commit, NULL);
352 int cmd_adm_shutdown(char* arg)
354 sendAdminES(Z_ESAdminOriginPartToKeep_shutdown, NULL);
358 int cmd_adm_startup(char* arg)
360 sendAdminES(Z_ESAdminOriginPartToKeep_start, NULL);