2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: admin.c,v 1.24 2007-04-12 13:52:57 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 = yaz_string_to_oid_odr(yaz_oid_std(),
62 req->packageType = oid;
63 req->packageName = "1.Extendedserveq";
65 /* Allocate the external */
66 r = req->taskSpecificParameters = (Z_External *)
67 odr_malloc (out, sizeof(*r));
68 r->direct_reference = odr_oiddup(out,oid);
69 r->indirect_reference = 0;
71 r->which = Z_External_ESAdmin;
72 r->u.adminService = (Z_Admin *)
73 odr_malloc(out, sizeof(*r->u.adminService));
74 r->u.adminService->which = Z_Admin_esRequest;
75 r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
76 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest));
78 toKeep = r->u.adminService->u.esRequest->toKeep =
79 (Z_ESAdminOriginPartToKeep *)
80 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->toKeep));
83 toKeep->databaseName = dbname;
86 case Z_ESAdminOriginPartToKeep_reIndex:
87 toKeep->u.reIndex=odr_nullval();
90 case Z_ESAdminOriginPartToKeep_truncate:
91 toKeep->u.truncate=odr_nullval();
93 case Z_ESAdminOriginPartToKeep_drop:
94 toKeep->u.drop=odr_nullval();
96 case Z_ESAdminOriginPartToKeep_create:
97 toKeep->u.create=odr_nullval();
99 case Z_ESAdminOriginPartToKeep_import:
100 toKeep->u.import = (Z_ImportParameters*)
101 odr_malloc(out, sizeof(*toKeep->u.import));
102 toKeep->u.import->recordType=param1;
103 /* Need to add additional setup of records here */
105 case Z_ESAdminOriginPartToKeep_refresh:
106 toKeep->u.refresh=odr_nullval();
108 case Z_ESAdminOriginPartToKeep_commit:
109 toKeep->u.commit=odr_nullval();
111 case Z_ESAdminOriginPartToKeep_shutdown:
112 toKeep->u.commit=odr_nullval();
114 case Z_ESAdminOriginPartToKeep_start:
115 toKeep->u.commit=odr_nullval();
118 /* Unknown admin service */
122 notToKeep = r->u.adminService->u.esRequest->notToKeep =
123 (Z_ESAdminOriginPartNotToKeep *)
124 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->notToKeep));
125 notToKeep->which=Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
126 notToKeep->u.recordsWillFollow=odr_nullval();
134 Ask the specified database to fully reindex itself */
135 int cmd_adm_reindex(const char *arg)
137 sendAdminES(Z_ESAdminOriginPartToKeep_reIndex, NULL);
142 Truncate the specified database, removing all records and index entries, but leaving
143 the database & it's explain information intact ready for new records */
144 int cmd_adm_truncate(const char *arg)
148 sendAdminES(Z_ESAdminOriginPartToKeep_truncate, NULL);
155 Create a new database */
156 int cmd_adm_create(const char *arg)
160 sendAdminES(Z_ESAdminOriginPartToKeep_create, NULL);
167 Drop (Delete) a database */
168 int cmd_adm_drop(const char *arg)
172 sendAdminES(Z_ESAdminOriginPartToKeep_drop, NULL);
178 /* cmd_adm_import <dbname> <rectype> <sourcefile>
179 Import the specified updated into the database
180 N.B. That in this case, the import may contain instructions to delete records as well as new or updates
181 to existing records */
184 int cmd_adm_import(const char *arg)
186 char type_str[20], dir_str[1024], pattern_str[1024];
193 Z_Segment *segment = 0;
194 ODR out = getODROutputStream();
196 if (arg && sscanf (arg, "%19s %1023s %1023s", type_str,
197 dir_str, pattern_str) != 3)
199 if (num_databaseNames != 1)
201 dir = opendir(dir_str);
205 sendAdminES(Z_ESAdminOriginPartToKeep_import, type_str);
207 printf ("sent es request\n");
208 if ((cp=strrchr(dir_str, '/')) && cp[1] == 0)
211 while ((ent = readdir(dir)))
213 if (fnmatch (pattern_str, ent->d_name, 0) == 0)
219 sprintf (fname, "%s%s%s", dir_str, sep, ent->d_name);
220 stat (fname, &status);
222 if (S_ISREG(status.st_mode) && (inf = fopen(fname, "r")))
224 Z_NamePlusRecord *rec;
225 Odr_oct *oct = (Odr_oct *) odr_malloc (out, sizeof(*oct));
229 apdu = zget_APDU(out, Z_APDU_segmentRequest);
230 segment = apdu->u.segmentRequest;
231 segment->segmentRecords = (Z_NamePlusRecord **)
232 odr_malloc (out, chunk * sizeof(*segment->segmentRecords));
234 rec = (Z_NamePlusRecord *) odr_malloc (out, sizeof(*rec));
235 rec->databaseName = 0;
236 rec->which = Z_NamePlusRecord_intermediateFragment;
237 rec->u.intermediateFragment = (Z_FragmentSyntax *)
238 odr_malloc (out, sizeof(*rec->u.intermediateFragment));
239 rec->u.intermediateFragment->which =
240 Z_FragmentSyntax_notExternallyTagged;
241 rec->u.intermediateFragment->u.notExternallyTagged = oct;
243 oct->len = oct->size = status.st_size;
244 oct->buf = (unsigned char *) odr_malloc (out, oct->size);
245 fread (oct->buf, 1, oct->size, inf);
248 segment->segmentRecords[segment->num_segmentRecords++] = rec;
250 if (segment->num_segmentRecords == chunk)
260 apdu = zget_APDU(out, Z_APDU_segmentRequest);
266 int cmd_adm_import(const char *arg)
268 printf ("not available on WIN32\n");
274 /* "Freshen" the specified database, by checking metadata records against the sources from which they were
275 generated, and creating a new record if the source has been touched since the last extraction */
276 int cmd_adm_refresh(const char *arg)
280 sendAdminES(Z_ESAdminOriginPartToKeep_refresh, NULL);
287 Make imported records a permenant & visible to the live system */
288 int cmd_adm_commit(const char *arg)
290 sendAdminES(Z_ESAdminOriginPartToKeep_commit, NULL);
294 int cmd_adm_shutdown(const char *arg)
296 sendAdminES(Z_ESAdminOriginPartToKeep_shutdown, NULL);
300 int cmd_adm_startup(const char *arg)
302 sendAdminES(Z_ESAdminOriginPartToKeep_start, NULL);
308 * indent-tabs-mode: nil
310 * vim: shiftwidth=4 tabstop=8 expandtab