2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: admin.c,v 1.14 2002-09-24 08:05:41 adam Exp $
19 #include <yaz/yaz-util.h>
21 #include <yaz/tcpip.h>
23 #include <yaz/proto.h>
24 #include <yaz/marcdisp.h>
25 #include <yaz/diagbib1.h>
27 #include <yaz/pquery.h>
31 /* Helper functions to get to various statics in the client */
32 ODR getODROutputStream();
34 extern char *databaseNames[];
35 extern int num_databaseNames;
37 int sendAdminES(int type, char* param1)
39 ODR out = getODROutputStream();
40 char *dbname = odr_strdup (out, databaseNames[0]);
42 /* Type: 1=reindex, 2=truncate, 3=delete, 4=create, 5=import, 6=refresh, 7=commit */
43 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
44 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
47 Z_ESAdminOriginPartToKeep *toKeep;
48 Z_ESAdminOriginPartNotToKeep *notToKeep;
50 printf ("Admin request\n");
53 /* Set up the OID for the external */
54 update_oid.proto = PROTO_Z3950;
55 update_oid.oclass = CLASS_EXTSERV;
56 update_oid.value = VAL_ADMINSERVICE;
58 oid_ent_to_oid (&update_oid, oid);
59 req->packageType = odr_oiddup(out,oid);
60 req->packageName = "1.Extendedserveq";
62 /* Allocate the external */
63 r = req->taskSpecificParameters = (Z_External *)
64 odr_malloc (out, sizeof(*r));
65 r->direct_reference = odr_oiddup(out,oid);
66 r->indirect_reference = 0;
68 r->which = Z_External_ESAdmin;
69 r->u.adminService = (Z_Admin *)
70 odr_malloc(out, sizeof(*r->u.adminService));
71 r->u.adminService->which = Z_Admin_esRequest;
72 r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
73 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest));
75 toKeep = r->u.adminService->u.esRequest->toKeep =
76 (Z_ESAdminOriginPartToKeep *)
77 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->toKeep));
80 toKeep->databaseName = dbname;
83 case Z_ESAdminOriginPartToKeep_reIndex:
84 toKeep->u.reIndex=odr_nullval();
87 case Z_ESAdminOriginPartToKeep_truncate:
88 toKeep->u.truncate=odr_nullval();
90 case Z_ESAdminOriginPartToKeep_drop:
91 toKeep->u.drop=odr_nullval();
93 case Z_ESAdminOriginPartToKeep_create:
94 toKeep->u.create=odr_nullval();
96 case Z_ESAdminOriginPartToKeep_import:
97 toKeep->u.import = (Z_ImportParameters*)
98 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 char type_str[20], dir_str[1024], pattern_str[1024];
190 Z_Segment *segment = 0;
191 ODR out = getODROutputStream();
193 if (arg && sscanf (arg, "%19s %1023s %1023s", type_str,
194 dir_str, pattern_str) != 3)
196 if (num_databaseNames != 1)
198 dir = opendir(dir_str);
202 sendAdminES(Z_ESAdminOriginPartToKeep_import, type_str);
204 printf ("sent es request\n");
205 if ((cp=strrchr(dir_str, '/')) && cp[1] == 0)
208 while ((ent = readdir(dir)))
210 if (fnmatch (pattern_str, ent->d_name, 0) == 0)
216 sprintf (fname, "%s%s%s", dir_str, sep, ent->d_name);
217 stat (fname, &status);
219 if (S_ISREG(status.st_mode) && (inf = fopen(fname, "r")))
221 Z_NamePlusRecord *rec;
222 Odr_oct *oct = (Odr_oct *) odr_malloc (out, sizeof(*oct));
226 apdu = zget_APDU(out, Z_APDU_segmentRequest);
227 segment = apdu->u.segmentRequest;
228 segment->segmentRecords = (Z_NamePlusRecord **)
229 odr_malloc (out, chunk * sizeof(*segment->segmentRecords));
231 rec = (Z_NamePlusRecord *) odr_malloc (out, sizeof(*rec));
232 rec->databaseName = 0;
233 rec->which = Z_NamePlusRecord_intermediateFragment;
234 rec->u.intermediateFragment = (Z_FragmentSyntax *)
235 odr_malloc (out, sizeof(*rec->u.intermediateFragment));
236 rec->u.intermediateFragment->which =
237 Z_FragmentSyntax_notExternallyTagged;
238 rec->u.intermediateFragment->u.notExternallyTagged = oct;
240 oct->len = oct->size = status.st_size;
241 oct->buf = (unsigned char *) odr_malloc (out, oct->size);
242 fread (oct->buf, 1, oct->size, inf);
245 segment->segmentRecords[segment->num_segmentRecords++] = rec;
247 if (segment->num_segmentRecords == chunk)
257 apdu = zget_APDU(out, Z_APDU_segmentRequest);
263 int cmd_adm_import(char *arg)
265 printf ("not available on WIN32\n");
271 /* "Freshen" the specified database, by checking metadata records against the sources from which they were
272 generated, and creating a new record if the source has been touched since the last extraction */
273 int cmd_adm_refresh(char* arg)
277 sendAdminES(Z_ESAdminOriginPartToKeep_refresh, NULL);
284 Make imported records a permenant & visible to the live system */
285 int cmd_adm_commit(char* arg)
287 sendAdminES(Z_ESAdminOriginPartToKeep_commit, NULL);
291 int cmd_adm_shutdown(char* arg)
293 sendAdminES(Z_ESAdminOriginPartToKeep_shutdown, NULL);
297 int cmd_adm_startup(char* arg)
299 sendAdminES(Z_ESAdminOriginPartToKeep_start, NULL);
308 * vim600: sw=4 ts=8 fdm=marker