X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=client%2Fadmin.c;h=54167f60f4da46efb9f9797a0f5c1205d3325ab7;hp=e20825c1d61bfb3970495bc8e4b83ffa10d715da;hb=eb4631c82f3b57fb016234447443268d4f46b933;hpb=fd4adcc80d442f84c6a1894e890b1b6ccd02615e diff --git a/client/admin.c b/client/admin.c index e20825c..54167f6 100644 --- a/client/admin.c +++ b/client/admin.c @@ -1,45 +1,45 @@ -/* - * Copyright (c) 1995-2001, Index Data +/* This file is part of the YAZ toolkit. + * Copyright (C) Index Data * See the file LICENSE for details. - * - * $Id: admin.c,v 1.11 2001-11-13 23:00:42 adam Exp $ */ +#if HAVE_CONFIG_H +#include +#endif #include #include #include #include -#ifdef WIN32 - -#else +#if HAVE_DIRENT_H #include +#endif +#if HAVE_FNMATCH_H #include +#endif +#if HAVE_SYS_STAT_H #include #endif #include #include -#ifdef USE_XTIMOSI -#include -#endif #include #include #include - +#include #include +#include "admin.h" /* Helper functions to get to various statics in the client */ -ODR getODROutputStream(); -void send_apdu(Z_APDU *a); +ODR getODROutputStream(void); extern char *databaseNames[]; extern int num_databaseNames; -int sendAdminES(int type, char* param1) +static int sendAdminES(int type, char* param1) { ODR out = getODROutputStream(); char *dbname = odr_strdup (out, databaseNames[0]); @@ -48,126 +48,126 @@ int sendAdminES(int type, char* param1) Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest ); Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest; Z_External *r; - int oid[OID_SIZE]; + Odr_oid *oid; Z_ESAdminOriginPartToKeep *toKeep; Z_ESAdminOriginPartNotToKeep *notToKeep; - oident update_oid; printf ("Admin request\n"); fflush(stdout); - /* Set up the OID for the external */ - update_oid.proto = PROTO_Z3950; - update_oid.oclass = CLASS_EXTSERV; - update_oid.value = VAL_ADMINSERVICE; + oid = odr_oiddup(out, yaz_oid_extserv_admin); - oid_ent_to_oid (&update_oid, oid); - req->packageType = odr_oiddup(out,oid); + req->packageType = oid; req->packageName = "1.Extendedserveq"; /* Allocate the external */ - r = req->taskSpecificParameters = (Z_External *) odr_malloc (out, sizeof(*r)); + r = req->taskSpecificParameters = (Z_External *) + odr_malloc (out, sizeof(*r)); r->direct_reference = odr_oiddup(out,oid); r->indirect_reference = 0; r->descriptor = 0; r->which = Z_External_ESAdmin; - r->u.adminService = (Z_Admin *) odr_malloc(out, sizeof(*r->u.adminService)); + r->u.adminService = (Z_Admin *) + odr_malloc(out, sizeof(*r->u.adminService)); r->u.adminService->which = Z_Admin_esRequest; - r->u.adminService->u.esRequest = (Z_AdminEsRequest *) odr_malloc(out, sizeof(*r->u.adminService->u.esRequest)); + r->u.adminService->u.esRequest = (Z_AdminEsRequest *) + odr_malloc(out, sizeof(*r->u.adminService->u.esRequest)); - toKeep = r->u.adminService->u.esRequest->toKeep = (Z_ESAdminOriginPartToKeep *) - odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->toKeep)); + toKeep = r->u.adminService->u.esRequest->toKeep = + (Z_ESAdminOriginPartToKeep *) + odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->toKeep)); toKeep->which=type; toKeep->databaseName = dbname; switch ( type ) { case Z_ESAdminOriginPartToKeep_reIndex: - toKeep->u.reIndex=odr_nullval(); - break; - + toKeep->u.reIndex=odr_nullval(); + break; + case Z_ESAdminOriginPartToKeep_truncate: - toKeep->u.truncate=odr_nullval(); - break; + toKeep->u.truncate=odr_nullval(); + break; case Z_ESAdminOriginPartToKeep_drop: - toKeep->u.drop=odr_nullval(); - break; + toKeep->u.drop=odr_nullval(); + break; case Z_ESAdminOriginPartToKeep_create: - toKeep->u.create=odr_nullval(); - break; + toKeep->u.create=odr_nullval(); + break; case Z_ESAdminOriginPartToKeep_import: - toKeep->u.import = (Z_ImportParameters*)odr_malloc(out, sizeof(*toKeep->u.import)); - toKeep->u.import->recordType=param1; - /* Need to add additional setup of records here */ - break; + toKeep->u.import = (Z_ImportParameters*) + odr_malloc(out, sizeof(*toKeep->u.import)); + toKeep->u.import->recordType=param1; + /* Need to add additional setup of records here */ + break; case Z_ESAdminOriginPartToKeep_refresh: - toKeep->u.refresh=odr_nullval(); - break; + toKeep->u.refresh=odr_nullval(); + break; case Z_ESAdminOriginPartToKeep_commit: - toKeep->u.commit=odr_nullval(); - break; + toKeep->u.commit=odr_nullval(); + break; case Z_ESAdminOriginPartToKeep_shutdown: - toKeep->u.commit=odr_nullval(); - break; + toKeep->u.commit=odr_nullval(); + break; case Z_ESAdminOriginPartToKeep_start: - toKeep->u.commit=odr_nullval(); - break; + toKeep->u.commit=odr_nullval(); + break; default: - /* Unknown admin service */ - break; + /* Unknown admin service */ + break; } notToKeep = r->u.adminService->u.esRequest->notToKeep = - (Z_ESAdminOriginPartNotToKeep *) - odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->notToKeep)); + (Z_ESAdminOriginPartNotToKeep *) + odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->notToKeep)); notToKeep->which=Z_ESAdminOriginPartNotToKeep_recordsWillFollow; notToKeep->u.recordsWillFollow=odr_nullval(); - + send_apdu(apdu); - + return 0; } /* cmd_adm_reindex Ask the specified database to fully reindex itself */ -int cmd_adm_reindex(char* arg) +int cmd_adm_reindex(const char *arg) { sendAdminES(Z_ESAdminOriginPartToKeep_reIndex, NULL); return 2; } /* cmd_adm_truncate - Truncate the specified database, removing all records and index entries, but leaving + Truncate the specified database, removing all records and index entries, but leaving the database & it's explain information intact ready for new records */ -int cmd_adm_truncate(char* arg) +int cmd_adm_truncate(const char *arg) { if ( arg ) { sendAdminES(Z_ESAdminOriginPartToKeep_truncate, NULL); - return 2; + return 2; } return 0; } /* cmd_adm_create Create a new database */ -int cmd_adm_create(char* arg) +int cmd_adm_create(const char *arg) { if ( arg ) { sendAdminES(Z_ESAdminOriginPartToKeep_create, NULL); - return 2; + return 2; } return 0; } /* cmd_adm_drop Drop (Delete) a database */ -int cmd_adm_drop(char* arg) +int cmd_adm_drop(const char *arg) { if ( arg ) { sendAdminES(Z_ESAdminOriginPartToKeep_drop, NULL); - return 2; + return 2; } return 0; } @@ -177,16 +177,8 @@ int cmd_adm_drop(char* arg) N.B. That in this case, the import may contain instructions to delete records as well as new or updates to existing records */ -#ifdef WIN32 -int cmd_adm_import(char *arg) -{ - printf ("not available on WIN32\n"); - return 0; -} - -#else - -int cmd_adm_import(char *arg) +#if HAVE_FNMATCH_H +int cmd_adm_import(const char *arg) { char type_str[20], dir_str[1024], pattern_str[1024]; char *cp; @@ -199,164 +191,126 @@ int cmd_adm_import(char *arg) ODR out = getODROutputStream(); if (arg && sscanf (arg, "%19s %1023s %1023s", type_str, - dir_str, pattern_str) != 3) - return 0; + dir_str, pattern_str) != 3) + return 0; if (num_databaseNames != 1) - return 0; + return 0; dir = opendir(dir_str); if (!dir) - return 0; - + return 0; + sendAdminES(Z_ESAdminOriginPartToKeep_import, type_str); printf ("sent es request\n"); if ((cp=strrchr(dir_str, '/')) && cp[1] == 0) - sep=""; - + sep=""; + while ((ent = readdir(dir))) { - if (fnmatch (pattern_str, ent->d_name, 0) == 0) - { - char fname[1024]; - struct stat status; - FILE *inf; - - sprintf (fname, "%s%s%s", dir_str, sep, ent->d_name); - stat (fname, &status); - - if (S_ISREG(status.st_mode) && (inf = fopen(fname, "r"))) - { - Z_NamePlusRecord *rec; - Odr_oct *oct = (Odr_oct *) odr_malloc (out, sizeof(*oct)); - - if (!apdu) - { - apdu = zget_APDU(out, Z_APDU_segmentRequest); - segment = apdu->u.segmentRequest; - segment->segmentRecords = (Z_NamePlusRecord **) - odr_malloc (out, chunk * sizeof(*segment->segmentRecords)); - } - rec = (Z_NamePlusRecord *) odr_malloc (out, sizeof(*rec)); - rec->databaseName = 0; - rec->which = Z_NamePlusRecord_intermediateFragment; - rec->u.intermediateFragment = (Z_FragmentSyntax *) - odr_malloc (out, sizeof(*rec->u.intermediateFragment)); - rec->u.intermediateFragment->which = - Z_FragmentSyntax_notExternallyTagged; - rec->u.intermediateFragment->u.notExternallyTagged = oct; - - oct->len = oct->size = status.st_size; - oct->buf = (unsigned char *) odr_malloc (out, oct->size); - fread (oct->buf, 1, oct->size, inf); - fclose (inf); - - segment->segmentRecords[segment->num_segmentRecords++] = rec; - - if (segment->num_segmentRecords == chunk) - { - send_apdu (apdu); - apdu = 0; - } - } - } + if (fnmatch (pattern_str, ent->d_name, 0) == 0) + { + char fname[1024]; + struct stat status; + FILE *inf; + + sprintf (fname, "%s%s%s", dir_str, sep, ent->d_name); + stat (fname, &status); + + if (S_ISREG(status.st_mode) && (inf = fopen(fname, "r"))) + { + Z_NamePlusRecord *rec; + Odr_oct *oct = (Odr_oct *) odr_malloc (out, sizeof(*oct)); + + if (!apdu) + { + apdu = zget_APDU(out, Z_APDU_segmentRequest); + segment = apdu->u.segmentRequest; + segment->segmentRecords = (Z_NamePlusRecord **) + odr_malloc (out, chunk * sizeof(*segment->segmentRecords)); + } + rec = (Z_NamePlusRecord *) odr_malloc (out, sizeof(*rec)); + rec->databaseName = 0; + rec->which = Z_NamePlusRecord_intermediateFragment; + rec->u.intermediateFragment = (Z_FragmentSyntax *) + odr_malloc (out, sizeof(*rec->u.intermediateFragment)); + rec->u.intermediateFragment->which = + Z_FragmentSyntax_notExternallyTagged; + rec->u.intermediateFragment->u.notExternallyTagged = oct; + + oct->len = status.st_size; + oct->buf = (char *) odr_malloc (out, oct->len); + if (fread(oct->buf, 1, oct->len, inf) != (size_t) oct->len) + { + printf("Incomplete read of file %s\n", fname); + } + if (fclose(inf)) + { + printf("Close failed for file %s\n", fname); + } + + segment->segmentRecords[segment->num_segmentRecords++] = rec; + + if (segment->num_segmentRecords == chunk) + { + send_apdu (apdu); + apdu = 0; + } + } + } } if (apdu) - send_apdu(apdu); + send_apdu(apdu); apdu = zget_APDU(out, Z_APDU_segmentRequest); send_apdu (apdu); closedir(dir); return 2; } -#endif - -int cmd_adm_import2(char* arg) +#else +int cmd_adm_import(const char *arg) { - /* Buffer for reading chunks of data from import file */ - char chunk_buffer[8192]; - - if ( arg ) - { - char rectype_buff[32]; - char filename_buff[32]; - FILE* pImportFile = NULL; - - if (sscanf (arg, "%s %s", rectype_buff, filename_buff) != 3) - { - printf("Must specify database-name, record-type and filename for import\n"); - return 0; - } - - /* Attempt to open the file */ - - pImportFile = fopen(filename_buff,"r"); - - /* This chunk of code should move into client.c sometime soon for sending files via the update es */ - /* This function will then refer to the standard client.c one for uploading a file using es update */ - if ( pImportFile ) - { - int iTotalWritten = 0; - - /* We opened the import file without problems... So no we send the es request, ready to - start sending fragments of the import file as segment messages */ - sendAdminES(Z_ESAdminOriginPartToKeep_import, rectype_buff); - - while ( ! feof(pImportFile ) ) - { - /* Read buffer_size bytes from the file */ - size_t num_items = fread((void*)chunk_buffer, 1, sizeof(chunk_buffer), pImportFile); - - /* Write num_bytes of data to */ - - if ( feof(pImportFile ) ) - { - /* This is the last chunk... Write it as the last fragment */ - printf("Last segment of %d bytes\n", num_items); - } - else if ( iTotalWritten == 0 ) - { - printf("First segment of %d bytes\n",num_items); - } - else - { - printf("Writing %d bytes\n", num_items); - } - - iTotalWritten += num_items; - } - } - return 2; - } + printf ("not available on WIN32\n"); return 0; } +#endif -/* "Freshen" the specified database, by checking metadata records against the sources from which they were + +/* "Freshen" the specified database, by checking metadata records against the sources from which they were generated, and creating a new record if the source has been touched since the last extraction */ -int cmd_adm_refresh(char* arg) +int cmd_adm_refresh(const char *arg) { if ( arg ) { sendAdminES(Z_ESAdminOriginPartToKeep_refresh, NULL); - return 2; + return 2; } return 0; } -/* cmd_adm_commit +/* cmd_adm_commit Make imported records a permenant & visible to the live system */ -int cmd_adm_commit(char* arg) +int cmd_adm_commit(const char *arg) { sendAdminES(Z_ESAdminOriginPartToKeep_commit, NULL); return 2; } -int cmd_adm_shutdown(char* arg) +int cmd_adm_shutdown(const char *arg) { sendAdminES(Z_ESAdminOriginPartToKeep_shutdown, NULL); return 2; } -int cmd_adm_startup(char* arg) +int cmd_adm_startup(const char *arg) { sendAdminES(Z_ESAdminOriginPartToKeep_start, NULL); return 2; } +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ +