All patches
[idzebra-moved-to-github.git] / index / update_driver.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2009 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #include <stdio.h>
21 #include <assert.h>
22 #include <sys/types.h>
23 #ifdef WIN32
24 #include <io.h>
25 #define S_ISREG(x) (x & _S_IFREG)
26 #define S_ISDIR(x) (x & _S_IFDIR)
27 #endif
28 #if HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #include <direntz.h>
32 #include <fcntl.h>
33 #include <time.h>
34
35 #include "index.h"
36
37 /* plugin includes */
38 #include <sys/stat.h>
39 #include "indexplugin.h"
40 #include <stdlib.h>
41 #include <dlfcn.h>
42
43
44
45
46
47 zebra_index_plugin_object *pluginObj = NULL;
48
49 static zebra_index_plugin_object *newZebraPlugin(void)
50 {
51     zebra_index_plugin_object *newPlugin = malloc(sizeof(zebra_index_plugin_object));
52     return newPlugin;
53 }
54
55 static void destroyZebraPlugin(zebra_index_plugin_object *zebraIdxPlugin)
56 {
57     free(zebraIdxPlugin);
58 }
59
60 void addDriverFunction(indexList function)
61 {
62     /* Assign the function to the object */
63     pluginObj->idxList = function;
64 }
65
66
67 void zebraIndexBuffer(ZebraHandle zh, char *data, int dataLength, enum zebra_recctrl_action_t action, char *name)
68 {
69     zebra_buffer_extract_record(zh, data, dataLength, action, zh->m_record_type, NULL, NULL, name);
70 }
71
72
73 /* I'm not even sure what this is for */
74 void repositoryShowDriver(ZebraHandle zh, const char *path)
75 {
76     char src[1024];
77     int src_len;
78     struct dirs_entry *dst;
79     Dict dict;
80     struct dirs_info *di;
81
82     if (!(dict = dict_open_res(zh->reg->bfs, FMATCH_DICT, 50, 0, 0, zh->res)))
83     {
84         yaz_log(YLOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
85         return;
86     }
87     
88     strncpy(src, path, sizeof(src)-1);
89     src[sizeof(src)-1]='\0';
90     src_len = strlen(src);
91     
92     if (src_len && src[src_len-1] != '/')
93     {
94         src[src_len] = '/';
95         src[++src_len] = '\0';
96     }
97     
98     di = dirs_open(dict, src, zh->m_flag_rw);
99     
100     while ((dst = dirs_read(di)))
101         yaz_log(YLOG_LOG, "%s", dst->path);
102     dirs_free(&di);
103     dict_close(dict);
104 }
105
106
107 ZEBRA_RES zebra_update_from_driver(ZebraHandle zh, const char *path, 
108                                    enum zebra_recctrl_action_t action, char *useIndexDriver)
109 {
110     /* delcair something to hold out remote call */
111     void (*idxPluginRegister)(void);
112     char *dlError;
113     void *libHandle;
114     int pluginReturn;
115
116     char driverName[100];
117     sprintf(driverName, "mod-%s.so", useIndexDriver);
118
119     yaz_log(YLOG_LOG, "Loading driver %s", useIndexDriver);
120
121     libHandle = dlopen(driverName, RTLD_LAZY);
122     if (!libHandle)
123     {
124         yaz_log(YLOG_FATAL, "Unable to load index plugin %s", dlerror());
125         return ZEBRA_FAIL;
126     }
127     /* clear the error buffer */
128     dlerror();
129
130     idxPluginRegister = dlsym(libHandle, "indexPluginRegister");
131
132     if ((dlError = dlerror()) != NULL)
133     {
134         yaz_log(YLOG_FATAL, "Index plugin error: %s", dlError);
135
136         /* Although the documentation says this dlclose isn't needed 
137            it seems better to put it in, incase there were memory
138            allocations */
139         dlclose(libHandle);
140         return ZEBRA_FAIL;
141     }
142
143     pluginObj = newZebraPlugin();
144     
145     /* invoke the plugin starter */
146     idxPluginRegister();
147
148     pluginReturn = pluginObj->idxList(zh, path, action);
149     destroyZebraPlugin(pluginObj);
150     
151     /* close the plugin handle */
152     dlclose(libHandle);
153
154     /* repositoryExtract(zh, path, action);*/
155     return pluginReturn;
156 }
157
158 /*
159  * Local variables:
160  * c-basic-offset: 4
161  * c-file-style: "Stroustrup"
162  * indent-tabs-mode: nil
163  * End:
164  * vim: shiftwidth=4 tabstop=8 expandtab
165  */
166