/*
* $Log: admin.c,v $
- * Revision 1.3 2000-03-14 15:23:17 ian
+ * Revision 1.6 2000-03-20 19:06:25 adam
+ * Added Segment request for fronend server. Work on admin for client.
+ *
+ * Revision 1.5 2000/03/17 12:47:02 adam
+ * Minor changes to admin client.
+ *
+ * Revision 1.4 2000/03/16 13:55:49 ian
+ * Added commands for sending shutdown and startup admin requests via the admin ES.
+ *
+ * Revision 1.3 2000/03/14 15:23:17 ian
* Removed unwanted ifdef and include of zes-admin.h
*
* Revision 1.2 2000/03/14 14:06:04 ian
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
-
+#include <assert.h>
+#include <dirent.h>
+#include <fnmatch.h>
+#include <sys/stat.h>
#include <yaz/yaz-util.h>
#include <yaz/tcpip.h>
-int sendAdminES(int type, char* dbname)
+int sendAdminES(int type, char* dbname, char* param1)
{
ODR out = getODROutputStream();
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 1:
- toKeep->which=Z_ESAdminOriginPartToKeep_reIndex;
- toKeep->u.reIndex=odr_nullval();
- break;
- case 2:
- toKeep->which=Z_ESAdminOriginPartToKeep_truncate;
- toKeep->u.truncate=odr_nullval();
- break;
- case 3:
- toKeep->which=Z_ESAdminOriginPartToKeep_delete;
- toKeep->u.delete=odr_nullval();
- break;
- case 4:
- toKeep->which=Z_ESAdminOriginPartToKeep_create;
- toKeep->u.create=odr_nullval();
- break;
- case 5:
- toKeep->which=Z_ESAdminOriginPartToKeep_import;
- toKeep->u.import=odr_nullval();
- break;
- case 6:
- toKeep->which=Z_ESAdminOriginPartToKeep_refresh;
- toKeep->u.refresh=odr_nullval();
- break;
- case 7:
- toKeep->which=Z_ESAdminOriginPartToKeep_commit;
- toKeep->u.commit=odr_nullval();
- break;
+ case Z_ESAdminOriginPartToKeep_reIndex:
+ toKeep->u.reIndex=odr_nullval();
+ break;
+
+ case Z_ESAdminOriginPartToKeep_truncate:
+ toKeep->u.truncate=odr_nullval();
+ break;
+ case Z_ESAdminOriginPartToKeep_delete:
+ toKeep->u.delete=odr_nullval();
+ break;
+ case Z_ESAdminOriginPartToKeep_create:
+ 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;
+ toKeep->databaseName = dbname;
+ /* Need to add additional setup of records here */
+ break;
+ case Z_ESAdminOriginPartToKeep_refresh:
+ toKeep->u.refresh=odr_nullval();
+ toKeep->databaseName = dbname;
+ break;
+ case Z_ESAdminOriginPartToKeep_commit:
+ toKeep->u.commit=odr_nullval();
+ break;
+ case Z_ESAdminOriginPartToKeep_shutdown:
+ toKeep->u.commit=odr_nullval();
+ break;
+ case Z_ESAdminOriginPartToKeep_start:
+ toKeep->u.commit=odr_nullval();
+ break;
+ default:
+ /* Unknown admin service */
+ break;
}
- toKeep->databaseName = dbname;
-
-
- notToKeep = r->u.adminService->u.esRequest->notToKeep = (Z_ESAdminOriginPartNotToKeep *)
+ notToKeep = 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 2;
+
+ return 0;
}
/* cmd_adm_reindex <dbname>
Ask the specified database to fully reindex itself */
int cmd_adm_reindex(char* arg)
{
- sendAdminES(1,arg);
+ sendAdminES(Z_ESAdminOriginPartToKeep_reIndex,arg,NULL);
+ return 2;
}
/* cmd_adm_truncate <dbname>
the database & it's explain information intact ready for new records */
int cmd_adm_truncate(char* arg)
{
- sendAdminES(2,arg);
+ if ( arg )
+ {
+ sendAdminES(Z_ESAdminOriginPartToKeep_truncate,arg,NULL);
+ return 2;
+ }
+ return 0;
}
/* cmd_adm_create <dbname>
Create a new database */
int cmd_adm_create(char* arg)
{
- sendAdminES(4,arg);
+ if ( arg )
+ {
+ sendAdminES(Z_ESAdminOriginPartToKeep_create,arg,NULL);
+ return 2;
+ }
+ return 0;
}
/* cmd_adm_delete <dbname>
Delete a database */
int cmd_adm_delete(char* arg)
{
- sendAdminES(3,arg);
+ if ( arg )
+ {
+ sendAdminES(Z_ESAdminOriginPartToKeep_delete,arg,NULL);
+ return 2;
+ }
+ return 0;
}
/* cmd_adm_import <dbname> <rectype> <sourcefile>
Import the specified updated into the database
N.B. That in this case, the import may contain instructions to delete records as well as new or updates
to existing records */
-int cmd_adm_import(char* arg)
+
+extern char *databaseNames[];
+extern int num_databaseNames;
+
+int cmd_adm_import(char *arg)
{
- sendAdminES(5,arg);
+ char type_str[20], dir_str[1024], pattern_str[1024];
+ char *cp;
+ char *sep = "/";
+ DIR *dir;
+ struct dirent *ent;
+ int chunk = 10;
+ Z_APDU *apdu = 0;
+ ODR out = getODROutputStream();
+
+ if (arg && sscanf (arg, "%19s %1023s %1023s", type_str,
+ dir_str, pattern_str) != 3)
+ return 0;
+ if (num_databaseNames != 1)
+ return 0;
+ dir = opendir(dir_str);
+ if (!dir)
+ return 0;
+
+ sendAdminES(Z_ESAdminOriginPartToKeep_import,*databaseNames,
+ type_str);
+
+ printf ("sent es request\n");
+ if ((cp=strrchr(dir_str, '/')) && cp[1] == 0)
+ 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_Segment *segment;
+ Z_NamePlusRecord *rec;
+ Odr_oct *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 = 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 (apdu)
+ {
+ printf ("sending last packet\n");
+ send_apdu(apdu);
+ }
+ apdu = zget_APDU(out, Z_APDU_segmentRequest);
+ printf ("sending end of sequence packet\n");
+ send_apdu (apdu);
+ closedir(dir);
+ return 2;
+}
+
+int cmd_adm_import2(char* arg)
+{
+ /* Size of chunks we wish to read from import file */
+ size_t chunk_size = 8192;
+
+ /* Buffer for reading chunks of data from import file */
+ char chunk_buffer[chunk_size];
+
+ if ( arg )
+ {
+ char dbname_buff[32];
+ char rectype_buff[32];
+ char filename_buff[32];
+ FILE* pImportFile = NULL;
+
+ if (sscanf (arg, "%s %s %s", dbname_buff, 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,arg,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;
+ }
+ return 0;
}
/* "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)
{
- sendAdminES(6,arg);
+ if ( arg )
+ {
+ sendAdminES(Z_ESAdminOriginPartToKeep_refresh,arg,NULL);
+ return 2;
+ }
+ return 0;
}
/* cmd_adm_commit
Make imported records a permenant & visible to the live system */
int cmd_adm_commit(char* arg)
{
- sendAdminES(7,NULL);
+ sendAdminES(Z_ESAdminOriginPartToKeep_commit,NULL,NULL);
+ return 2;
+}
+
+int cmd_adm_shutdown(char* arg)
+{
+ sendAdminES(Z_ESAdminOriginPartToKeep_shutdown,NULL,NULL);
+ return 2;
+}
+
+int cmd_adm_startup(char* arg)
+{
+ sendAdminES(Z_ESAdminOriginPartToKeep_start,NULL,NULL);
+ return 2;
}