2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: admin.c,v 1.25 2007-04-16 21:53:08 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>
30 #include <yaz/oid_db.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;
53 printf ("Admin request\n");
56 oid = odr_oiddup(out, yaz_oid_extserv_admin);
58 req->packageType = oid;
59 req->packageName = "1.Extendedserveq";
61 /* Allocate the external */
62 r = req->taskSpecificParameters = (Z_External *)
63 odr_malloc (out, sizeof(*r));
64 r->direct_reference = odr_oiddup(out,oid);
65 r->indirect_reference = 0;
67 r->which = Z_External_ESAdmin;
68 r->u.adminService = (Z_Admin *)
69 odr_malloc(out, sizeof(*r->u.adminService));
70 r->u.adminService->which = Z_Admin_esRequest;
71 r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
72 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest));
74 toKeep = r->u.adminService->u.esRequest->toKeep =
75 (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*)
97 odr_malloc(out, sizeof(*toKeep->u.import));
98 toKeep->u.import->recordType=param1;
99 /* Need to add additional setup of records here */
101 case Z_ESAdminOriginPartToKeep_refresh:
102 toKeep->u.refresh=odr_nullval();
104 case Z_ESAdminOriginPartToKeep_commit:
105 toKeep->u.commit=odr_nullval();
107 case Z_ESAdminOriginPartToKeep_shutdown:
108 toKeep->u.commit=odr_nullval();
110 case Z_ESAdminOriginPartToKeep_start:
111 toKeep->u.commit=odr_nullval();
114 /* Unknown admin service */
118 notToKeep = r->u.adminService->u.esRequest->notToKeep =
119 (Z_ESAdminOriginPartNotToKeep *)
120 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->notToKeep));
121 notToKeep->which=Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
122 notToKeep->u.recordsWillFollow=odr_nullval();
130 Ask the specified database to fully reindex itself */
131 int cmd_adm_reindex(const char *arg)
133 sendAdminES(Z_ESAdminOriginPartToKeep_reIndex, NULL);
138 Truncate the specified database, removing all records and index entries, but leaving
139 the database & it's explain information intact ready for new records */
140 int cmd_adm_truncate(const char *arg)
144 sendAdminES(Z_ESAdminOriginPartToKeep_truncate, NULL);
151 Create a new database */
152 int cmd_adm_create(const char *arg)
156 sendAdminES(Z_ESAdminOriginPartToKeep_create, NULL);
163 Drop (Delete) a database */
164 int cmd_adm_drop(const char *arg)
168 sendAdminES(Z_ESAdminOriginPartToKeep_drop, NULL);
174 /* cmd_adm_import <dbname> <rectype> <sourcefile>
175 Import the specified updated into the database
176 N.B. That in this case, the import may contain instructions to delete records as well as new or updates
177 to existing records */
180 int cmd_adm_import(const char *arg)
182 char type_str[20], dir_str[1024], pattern_str[1024];
189 Z_Segment *segment = 0;
190 ODR out = getODROutputStream();
192 if (arg && sscanf (arg, "%19s %1023s %1023s", type_str,
193 dir_str, pattern_str) != 3)
195 if (num_databaseNames != 1)
197 dir = opendir(dir_str);
201 sendAdminES(Z_ESAdminOriginPartToKeep_import, type_str);
203 printf ("sent es request\n");
204 if ((cp=strrchr(dir_str, '/')) && cp[1] == 0)
207 while ((ent = readdir(dir)))
209 if (fnmatch (pattern_str, ent->d_name, 0) == 0)
215 sprintf (fname, "%s%s%s", dir_str, sep, ent->d_name);
216 stat (fname, &status);
218 if (S_ISREG(status.st_mode) && (inf = fopen(fname, "r")))
220 Z_NamePlusRecord *rec;
221 Odr_oct *oct = (Odr_oct *) odr_malloc (out, sizeof(*oct));
225 apdu = zget_APDU(out, Z_APDU_segmentRequest);
226 segment = apdu->u.segmentRequest;
227 segment->segmentRecords = (Z_NamePlusRecord **)
228 odr_malloc (out, chunk * sizeof(*segment->segmentRecords));
230 rec = (Z_NamePlusRecord *) odr_malloc (out, sizeof(*rec));
231 rec->databaseName = 0;
232 rec->which = Z_NamePlusRecord_intermediateFragment;
233 rec->u.intermediateFragment = (Z_FragmentSyntax *)
234 odr_malloc (out, sizeof(*rec->u.intermediateFragment));
235 rec->u.intermediateFragment->which =
236 Z_FragmentSyntax_notExternallyTagged;
237 rec->u.intermediateFragment->u.notExternallyTagged = oct;
239 oct->len = oct->size = status.st_size;
240 oct->buf = (unsigned char *) odr_malloc (out, oct->size);
241 fread (oct->buf, 1, oct->size, inf);
244 segment->segmentRecords[segment->num_segmentRecords++] = rec;
246 if (segment->num_segmentRecords == chunk)
256 apdu = zget_APDU(out, Z_APDU_segmentRequest);
262 int cmd_adm_import(const char *arg)
264 printf ("not available on WIN32\n");
270 /* "Freshen" the specified database, by checking metadata records against the sources from which they were
271 generated, and creating a new record if the source has been touched since the last extraction */
272 int cmd_adm_refresh(const char *arg)
276 sendAdminES(Z_ESAdminOriginPartToKeep_refresh, NULL);
283 Make imported records a permenant & visible to the live system */
284 int cmd_adm_commit(const char *arg)
286 sendAdminES(Z_ESAdminOriginPartToKeep_commit, NULL);
290 int cmd_adm_shutdown(const char *arg)
292 sendAdminES(Z_ESAdminOriginPartToKeep_shutdown, NULL);
296 int cmd_adm_startup(const char *arg)
298 sendAdminES(Z_ESAdminOriginPartToKeep_start, NULL);
304 * indent-tabs-mode: nil
306 * vim: shiftwidth=4 tabstop=8 expandtab