2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: admin.c,v 1.23 2007-01-03 08:42:13 adam Exp $
23 #include <yaz/yaz-util.h>
25 #include <yaz/tcpip.h>
27 #include <yaz/proto.h>
28 #include <yaz/marcdisp.h>
29 #include <yaz/diagbib1.h>
31 #include <yaz/pquery.h>
35 /* Helper functions to get to various statics in the client */
36 ODR getODROutputStream(void);
38 extern char *databaseNames[];
39 extern int num_databaseNames;
41 int sendAdminES(int type, char* param1)
43 ODR out = getODROutputStream();
44 char *dbname = odr_strdup (out, databaseNames[0]);
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;
51 Z_ESAdminOriginPartToKeep *toKeep;
52 Z_ESAdminOriginPartNotToKeep *notToKeep;
54 printf ("Admin request\n");
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;
62 oid_ent_to_oid (&update_oid, oid);
63 req->packageType = odr_oiddup(out,oid);
64 req->packageName = "1.Extendedserveq";
66 /* Allocate the external */
67 r = req->taskSpecificParameters = (Z_External *)
68 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 *)
74 odr_malloc(out, sizeof(*r->u.adminService));
75 r->u.adminService->which = Z_Admin_esRequest;
76 r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
77 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest));
79 toKeep = r->u.adminService->u.esRequest->toKeep =
80 (Z_ESAdminOriginPartToKeep *)
81 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->toKeep));
84 toKeep->databaseName = dbname;
87 case Z_ESAdminOriginPartToKeep_reIndex:
88 toKeep->u.reIndex=odr_nullval();
91 case Z_ESAdminOriginPartToKeep_truncate:
92 toKeep->u.truncate=odr_nullval();
94 case Z_ESAdminOriginPartToKeep_drop:
95 toKeep->u.drop=odr_nullval();
97 case Z_ESAdminOriginPartToKeep_create:
98 toKeep->u.create=odr_nullval();
100 case Z_ESAdminOriginPartToKeep_import:
101 toKeep->u.import = (Z_ImportParameters*)
102 odr_malloc(out, sizeof(*toKeep->u.import));
103 toKeep->u.import->recordType=param1;
104 /* Need to add additional setup of records here */
106 case Z_ESAdminOriginPartToKeep_refresh:
107 toKeep->u.refresh=odr_nullval();
109 case Z_ESAdminOriginPartToKeep_commit:
110 toKeep->u.commit=odr_nullval();
112 case Z_ESAdminOriginPartToKeep_shutdown:
113 toKeep->u.commit=odr_nullval();
115 case Z_ESAdminOriginPartToKeep_start:
116 toKeep->u.commit=odr_nullval();
119 /* Unknown admin service */
123 notToKeep = r->u.adminService->u.esRequest->notToKeep =
124 (Z_ESAdminOriginPartNotToKeep *)
125 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->notToKeep));
126 notToKeep->which=Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
127 notToKeep->u.recordsWillFollow=odr_nullval();
135 Ask the specified database to fully reindex itself */
136 int cmd_adm_reindex(const char *arg)
138 sendAdminES(Z_ESAdminOriginPartToKeep_reIndex, NULL);
143 Truncate the specified database, removing all records and index entries, but leaving
144 the database & it's explain information intact ready for new records */
145 int cmd_adm_truncate(const char *arg)
149 sendAdminES(Z_ESAdminOriginPartToKeep_truncate, NULL);
156 Create a new database */
157 int cmd_adm_create(const char *arg)
161 sendAdminES(Z_ESAdminOriginPartToKeep_create, NULL);
168 Drop (Delete) a database */
169 int cmd_adm_drop(const char *arg)
173 sendAdminES(Z_ESAdminOriginPartToKeep_drop, NULL);
179 /* cmd_adm_import <dbname> <rectype> <sourcefile>
180 Import the specified updated into the database
181 N.B. That in this case, the import may contain instructions to delete records as well as new or updates
182 to existing records */
185 int cmd_adm_import(const char *arg)
187 char type_str[20], dir_str[1024], pattern_str[1024];
194 Z_Segment *segment = 0;
195 ODR out = getODROutputStream();
197 if (arg && sscanf (arg, "%19s %1023s %1023s", type_str,
198 dir_str, pattern_str) != 3)
200 if (num_databaseNames != 1)
202 dir = opendir(dir_str);
206 sendAdminES(Z_ESAdminOriginPartToKeep_import, type_str);
208 printf ("sent es request\n");
209 if ((cp=strrchr(dir_str, '/')) && cp[1] == 0)
212 while ((ent = readdir(dir)))
214 if (fnmatch (pattern_str, ent->d_name, 0) == 0)
220 sprintf (fname, "%s%s%s", dir_str, sep, ent->d_name);
221 stat (fname, &status);
223 if (S_ISREG(status.st_mode) && (inf = fopen(fname, "r")))
225 Z_NamePlusRecord *rec;
226 Odr_oct *oct = (Odr_oct *) odr_malloc (out, sizeof(*oct));
230 apdu = zget_APDU(out, Z_APDU_segmentRequest);
231 segment = apdu->u.segmentRequest;
232 segment->segmentRecords = (Z_NamePlusRecord **)
233 odr_malloc (out, chunk * sizeof(*segment->segmentRecords));
235 rec = (Z_NamePlusRecord *) odr_malloc (out, sizeof(*rec));
236 rec->databaseName = 0;
237 rec->which = Z_NamePlusRecord_intermediateFragment;
238 rec->u.intermediateFragment = (Z_FragmentSyntax *)
239 odr_malloc (out, sizeof(*rec->u.intermediateFragment));
240 rec->u.intermediateFragment->which =
241 Z_FragmentSyntax_notExternallyTagged;
242 rec->u.intermediateFragment->u.notExternallyTagged = oct;
244 oct->len = oct->size = status.st_size;
245 oct->buf = (unsigned char *) odr_malloc (out, oct->size);
246 fread (oct->buf, 1, oct->size, inf);
249 segment->segmentRecords[segment->num_segmentRecords++] = rec;
251 if (segment->num_segmentRecords == chunk)
261 apdu = zget_APDU(out, Z_APDU_segmentRequest);
267 int cmd_adm_import(const char *arg)
269 printf ("not available on WIN32\n");
275 /* "Freshen" the specified database, by checking metadata records against the sources from which they were
276 generated, and creating a new record if the source has been touched since the last extraction */
277 int cmd_adm_refresh(const char *arg)
281 sendAdminES(Z_ESAdminOriginPartToKeep_refresh, NULL);
288 Make imported records a permenant & visible to the live system */
289 int cmd_adm_commit(const char *arg)
291 sendAdminES(Z_ESAdminOriginPartToKeep_commit, NULL);
295 int cmd_adm_shutdown(const char *arg)
297 sendAdminES(Z_ESAdminOriginPartToKeep_shutdown, NULL);
301 int cmd_adm_startup(const char *arg)
303 sendAdminES(Z_ESAdminOriginPartToKeep_start, NULL);
309 * indent-tabs-mode: nil
311 * vim: shiftwidth=4 tabstop=8 expandtab