1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
24 #include <yaz/yaz-util.h>
26 #include <yaz/tcpip.h>
28 #include <yaz/proto.h>
29 #include <yaz/marcdisp.h>
30 #include <yaz/diagbib1.h>
31 #include <yaz/oid_db.h>
32 #include <yaz/pquery.h>
36 /* Helper functions to get to various statics in the client */
37 ODR getODROutputStream(void);
39 extern char *databaseNames[];
40 extern int num_databaseNames;
42 static int sendAdminES(int type, char* param1)
44 ODR out = getODROutputStream();
45 char *dbname = odr_strdup (out, databaseNames[0]);
47 /* Type: 1=reindex, 2=truncate, 3=delete, 4=create, 5=import, 6=refresh, 7=commit */
48 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
49 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
52 Z_ESAdminOriginPartToKeep *toKeep;
53 Z_ESAdminOriginPartNotToKeep *notToKeep;
54 printf ("Admin request\n");
57 oid = odr_oiddup(out, yaz_oid_extserv_admin);
59 req->packageType = 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(const 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(const char *arg)
145 sendAdminES(Z_ESAdminOriginPartToKeep_truncate, NULL);
152 Create a new database */
153 int cmd_adm_create(const char *arg)
157 sendAdminES(Z_ESAdminOriginPartToKeep_create, NULL);
164 Drop (Delete) a database */
165 int cmd_adm_drop(const 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(const 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 = status.st_size;
241 oct->buf = (char *) odr_malloc (out, oct->len);
242 if (fread(oct->buf, 1, oct->len, inf) != (size_t) oct->len)
244 printf("Incomplete read of file %s\n", fname);
248 printf("Close failed for file %s\n", fname);
251 segment->segmentRecords[segment->num_segmentRecords++] = rec;
253 if (segment->num_segmentRecords == chunk)
263 apdu = zget_APDU(out, Z_APDU_segmentRequest);
269 int cmd_adm_import(const char *arg)
271 printf ("not available on WIN32\n");
277 /* "Freshen" the specified database, by checking metadata records against the sources from which they were
278 generated, and creating a new record if the source has been touched since the last extraction */
279 int cmd_adm_refresh(const char *arg)
283 sendAdminES(Z_ESAdminOriginPartToKeep_refresh, NULL);
290 Make imported records a permenant & visible to the live system */
291 int cmd_adm_commit(const char *arg)
293 sendAdminES(Z_ESAdminOriginPartToKeep_commit, NULL);
297 int cmd_adm_shutdown(const char *arg)
299 sendAdminES(Z_ESAdminOriginPartToKeep_shutdown, NULL);
303 int cmd_adm_startup(const char *arg)
305 sendAdminES(Z_ESAdminOriginPartToKeep_start, NULL);
311 * c-file-style: "Stroustrup"
312 * indent-tabs-mode: nil
314 * vim: shiftwidth=4 tabstop=8 expandtab