1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
21 #include <yaz/yaz-util.h>
23 #include <yaz/tcpip.h>
25 #include <yaz/proto.h>
26 #include <yaz/marcdisp.h>
27 #include <yaz/diagbib1.h>
28 #include <yaz/oid_db.h>
29 #include <yaz/pquery.h>
33 /* Helper functions to get to various statics in the client */
34 ODR getODROutputStream(void);
36 extern char *databaseNames[];
37 extern int num_databaseNames;
39 int sendAdminES(int type, char* param1)
41 ODR out = getODROutputStream();
42 char *dbname = odr_strdup (out, databaseNames[0]);
44 /* Type: 1=reindex, 2=truncate, 3=delete, 4=create, 5=import, 6=refresh, 7=commit */
45 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
46 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
49 Z_ESAdminOriginPartToKeep *toKeep;
50 Z_ESAdminOriginPartNotToKeep *notToKeep;
51 printf ("Admin request\n");
54 oid = odr_oiddup(out, yaz_oid_extserv_admin);
56 req->packageType = oid;
57 req->packageName = "1.Extendedserveq";
59 /* Allocate the external */
60 r = req->taskSpecificParameters = (Z_External *)
61 odr_malloc (out, sizeof(*r));
62 r->direct_reference = odr_oiddup(out,oid);
63 r->indirect_reference = 0;
65 r->which = Z_External_ESAdmin;
66 r->u.adminService = (Z_Admin *)
67 odr_malloc(out, sizeof(*r->u.adminService));
68 r->u.adminService->which = Z_Admin_esRequest;
69 r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
70 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest));
72 toKeep = r->u.adminService->u.esRequest->toKeep =
73 (Z_ESAdminOriginPartToKeep *)
74 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->toKeep));
77 toKeep->databaseName = dbname;
80 case Z_ESAdminOriginPartToKeep_reIndex:
81 toKeep->u.reIndex=odr_nullval();
84 case Z_ESAdminOriginPartToKeep_truncate:
85 toKeep->u.truncate=odr_nullval();
87 case Z_ESAdminOriginPartToKeep_drop:
88 toKeep->u.drop=odr_nullval();
90 case Z_ESAdminOriginPartToKeep_create:
91 toKeep->u.create=odr_nullval();
93 case Z_ESAdminOriginPartToKeep_import:
94 toKeep->u.import = (Z_ImportParameters*)
95 odr_malloc(out, sizeof(*toKeep->u.import));
96 toKeep->u.import->recordType=param1;
97 /* Need to add additional setup of records here */
99 case Z_ESAdminOriginPartToKeep_refresh:
100 toKeep->u.refresh=odr_nullval();
102 case Z_ESAdminOriginPartToKeep_commit:
103 toKeep->u.commit=odr_nullval();
105 case Z_ESAdminOriginPartToKeep_shutdown:
106 toKeep->u.commit=odr_nullval();
108 case Z_ESAdminOriginPartToKeep_start:
109 toKeep->u.commit=odr_nullval();
112 /* Unknown admin service */
116 notToKeep = r->u.adminService->u.esRequest->notToKeep =
117 (Z_ESAdminOriginPartNotToKeep *)
118 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->notToKeep));
119 notToKeep->which=Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
120 notToKeep->u.recordsWillFollow=odr_nullval();
128 Ask the specified database to fully reindex itself */
129 int cmd_adm_reindex(const char *arg)
131 sendAdminES(Z_ESAdminOriginPartToKeep_reIndex, NULL);
136 Truncate the specified database, removing all records and index entries, but leaving
137 the database & it's explain information intact ready for new records */
138 int cmd_adm_truncate(const char *arg)
142 sendAdminES(Z_ESAdminOriginPartToKeep_truncate, NULL);
149 Create a new database */
150 int cmd_adm_create(const char *arg)
154 sendAdminES(Z_ESAdminOriginPartToKeep_create, NULL);
161 Drop (Delete) a database */
162 int cmd_adm_drop(const char *arg)
166 sendAdminES(Z_ESAdminOriginPartToKeep_drop, NULL);
172 /* cmd_adm_import <dbname> <rectype> <sourcefile>
173 Import the specified updated into the database
174 N.B. That in this case, the import may contain instructions to delete records as well as new or updates
175 to existing records */
178 int cmd_adm_import(const char *arg)
180 char type_str[20], dir_str[1024], pattern_str[1024];
187 Z_Segment *segment = 0;
188 ODR out = getODROutputStream();
190 if (arg && sscanf (arg, "%19s %1023s %1023s", type_str,
191 dir_str, pattern_str) != 3)
193 if (num_databaseNames != 1)
195 dir = opendir(dir_str);
199 sendAdminES(Z_ESAdminOriginPartToKeep_import, type_str);
201 printf ("sent es request\n");
202 if ((cp=strrchr(dir_str, '/')) && cp[1] == 0)
205 while ((ent = readdir(dir)))
207 if (fnmatch (pattern_str, ent->d_name, 0) == 0)
213 sprintf (fname, "%s%s%s", dir_str, sep, ent->d_name);
214 stat (fname, &status);
216 if (S_ISREG(status.st_mode) && (inf = fopen(fname, "r")))
218 Z_NamePlusRecord *rec;
219 Odr_oct *oct = (Odr_oct *) odr_malloc (out, sizeof(*oct));
223 apdu = zget_APDU(out, Z_APDU_segmentRequest);
224 segment = apdu->u.segmentRequest;
225 segment->segmentRecords = (Z_NamePlusRecord **)
226 odr_malloc (out, chunk * sizeof(*segment->segmentRecords));
228 rec = (Z_NamePlusRecord *) odr_malloc (out, sizeof(*rec));
229 rec->databaseName = 0;
230 rec->which = Z_NamePlusRecord_intermediateFragment;
231 rec->u.intermediateFragment = (Z_FragmentSyntax *)
232 odr_malloc (out, sizeof(*rec->u.intermediateFragment));
233 rec->u.intermediateFragment->which =
234 Z_FragmentSyntax_notExternallyTagged;
235 rec->u.intermediateFragment->u.notExternallyTagged = oct;
237 oct->len = oct->size = status.st_size;
238 oct->buf = (unsigned char *) odr_malloc (out, oct->size);
239 fread (oct->buf, 1, oct->size, inf);
242 segment->segmentRecords[segment->num_segmentRecords++] = rec;
244 if (segment->num_segmentRecords == chunk)
254 apdu = zget_APDU(out, Z_APDU_segmentRequest);
260 int cmd_adm_import(const char *arg)
262 printf ("not available on WIN32\n");
268 /* "Freshen" the specified database, by checking metadata records against the sources from which they were
269 generated, and creating a new record if the source has been touched since the last extraction */
270 int cmd_adm_refresh(const char *arg)
274 sendAdminES(Z_ESAdminOriginPartToKeep_refresh, NULL);
281 Make imported records a permenant & visible to the live system */
282 int cmd_adm_commit(const char *arg)
284 sendAdminES(Z_ESAdminOriginPartToKeep_commit, NULL);
288 int cmd_adm_shutdown(const char *arg)
290 sendAdminES(Z_ESAdminOriginPartToKeep_shutdown, NULL);
294 int cmd_adm_startup(const char *arg)
296 sendAdminES(Z_ESAdminOriginPartToKeep_start, NULL);
302 * indent-tabs-mode: nil
304 * vim: shiftwidth=4 tabstop=8 expandtab