c5c8b4fcab691c06557c5f9e148b2979bfab80e2
[idzebra-moved-to-github.git] / index / zebraidx.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 <stdlib.h>
22 #include <string.h>
23 #include <assert.h>
24 #ifdef WIN32
25 #include <io.h>
26 #endif
27 #if HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30
31 #if YAZ_HAVE_ICU
32 #include <yaz/icu.h>
33 #endif
34 #include <yaz/log.h>
35 #include <yaz/options.h>
36 #include <idzebra/version.h>
37 #include <idzebra/api.h>
38
39 char *prog;
40
41 static void filter_cb(void *cd, const char *name)
42 {
43     puts(name);
44 }
45
46 static void show_filters(ZebraService zs)
47 {
48     zebra_filter_info(zs, 0, filter_cb);
49 }
50
51 int main(int argc, char **argv)
52 {
53     int ret;
54     int cmd = 0;
55     char *arg;
56     char *configName = 0;
57     int nsections = 0;
58     int enable_commit = 1;
59     char *database = 0;
60     Res res = res_open(0, 0);
61     Res default_res = res_open(0, 0);
62     
63     int trans_started = 0;
64 #ifndef WIN32
65     char nbuf[100];
66 #endif
67     ZebraService zs = 0;
68     ZebraHandle zh = 0;
69
70 #ifdef WIN32
71 #else
72     sprintf(nbuf, "%.40s(%ld)", *argv, (long) getpid());
73     yaz_log_init_prefix(nbuf);
74 #endif
75     prog = *argv;
76     if (argc < 2)
77     {
78         fprintf(stderr, "%s [options] command <dir> ...\n"
79         "Commands:\n"
80         " update <dir>  Update index with files below <dir>.\n"
81         "               If <dir> is empty filenames are read from stdin.\n"
82         " delete <dir>  Delete index with files below <dir>.\n"
83         " create <db>   Create database <db>\n"
84         " drop <db>     Drop database <db>\n"
85         " commit        Commit changes\n"
86         " clean         Clean shadow files\n"
87         "Options:\n"
88         " -t <type>     Index files as <type> (grs or text).\n"
89         " -c <config>   Read configuration file <config>.\n"
90         " -g <group>    Index files according to group settings.\n"
91         " -d <database> Records belong to Z39.50 database <database>.\n"
92         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
93         " -n            Don't use shadow system.\n"
94         " -s            Show analysis on stdout, but do no work.\n"
95         " -v <level>    Set logging to <level>.\n"
96         " -l <file>     Write log to <file>.\n"
97         " -L            Don't follow symbolic links.\n"
98         " -f <n>        Display information for the first <n> records.\n"
99         " -V            Show version.\n", *argv
100                  );
101         exit(1);
102     }
103     res_set(default_res, "profilePath", DEFAULT_PROFILE_PATH);
104     res_set(default_res, "modulePath", DEFAULT_MODULE_PATH);
105     while ((ret = options("sVt:c:g:d:m:v:nf:l:L", argv, argc, &arg)) != -2)
106     {
107         if (ret == 0)
108         {
109             if(cmd == 0) /* command */
110             {
111                 if (!zs)
112                 {
113                     const char *config = configName ? configName : "zebra.cfg";
114                     zs = zebra_start_res(config, default_res, res);
115                     if (!zs)
116                     {
117                         yaz_log(YLOG_FATAL, "Cannot read config %s", config);
118                         exit(1);
119                     }   
120                     zh = zebra_open(zs, 0);
121                     zebra_shadow_enable(zh, enable_commit);
122                 }
123
124                 if (database &&
125                     zebra_select_database(zh, database) == ZEBRA_FAIL)
126                 {
127                     yaz_log(YLOG_FATAL, "Could not select database %s "
128                             "errCode=%d",
129                             database, zebra_errCode(zh) );
130                     exit(1);
131                 }
132                 if (!strcmp(arg, "update"))
133                     cmd = 'u';
134                 else if (!strcmp(arg, "update1"))
135                     cmd = 'U';
136                 else if (!strcmp(arg, "update2"))
137                     cmd = 'm';
138                 else if (!strcmp(arg, "dump"))
139                     cmd = 's';
140                 else if (!strcmp(arg, "del") || !strcmp(arg, "delete"))
141                     cmd = 'd';
142                 else if (!strcmp(arg, "adelete"))
143                     cmd = 'a';
144                 else if (!strcmp(arg, "init"))
145                 {
146                     zebra_init(zh);
147                 }
148                 else if (!strcmp(arg, "drop"))
149                 {
150                     cmd = 'D';
151                 }
152                 else if (!strcmp(arg, "create"))
153                 {
154                     cmd = 'C';
155                 }
156                 else if (!strcmp(arg, "commit"))
157                 {
158                     zebra_commit(zh);
159                 }
160                 else if (!strcmp(arg, "clean"))
161                 {
162                     zebra_clean(zh);
163                 }
164                 else if (!strcmp(arg, "stat") || !strcmp(arg, "status"))
165                 {
166                     zebra_register_statistics(zh, 0);
167                 }
168                 else if (!strcmp(arg, "dumpdict"))
169                 {
170                     zebra_register_statistics(zh, 1);
171                 }
172                 else if (!strcmp(arg, "compact"))
173                 {
174                     zebra_compact(zh);
175                 }
176                 else if (!strcmp(arg, "filters"))
177                 {
178                     show_filters(zs);
179                 }
180                 else
181                 {
182                     yaz_log(YLOG_FATAL, "unknown command: %s", arg);
183                     exit(1);
184                 }
185             }
186             else
187             {
188                 ZEBRA_RES res = ZEBRA_OK;
189                 if (!trans_started)
190                 {
191                     trans_started=1;
192                     if (zebra_begin_trans(zh, 1) != ZEBRA_OK)
193                         exit(1);
194                 }
195                 switch (cmd)
196                 {
197                 case 'u':
198                     res = zebra_repository_index(zh, arg, action_update);
199                     break;
200                 case 'd':
201                     res = zebra_repository_index(zh, arg, action_delete);
202                     break;
203                 case 'a':
204                     res = zebra_repository_index(zh, arg, action_a_delete);
205                     break;
206                 case 's':
207                     res = zebra_repository_show(zh, arg);
208                     nsections = 0;
209                     break;
210                 case 'C':
211                     res = zebra_create_database(zh, arg);
212                     break;
213                 case 'D':
214                     res = zebra_drop_database(zh, arg);
215                     break;
216                 default:
217                     nsections = 0;
218                 }
219                 if (res != ZEBRA_OK)
220                 {
221                     const char *add = zebra_errAdd(zh);
222                     yaz_log(YLOG_FATAL, "Operation failed: %s %s",
223                             zebra_errString(zh), add ? add : "");
224                     
225                     if (trans_started)
226                         if (zebra_end_trans(zh) != ZEBRA_OK)
227                             yaz_log(YLOG_WARN, "zebra_end_trans failed");
228
229
230                     zebra_close(zh);
231                     zebra_stop(zs);
232                     exit(1);
233                 }
234                 log_event_end(NULL, NULL);
235             }
236         }
237         else if (ret == 'V')
238         {
239             char version_str[20];
240             char sys_str[80];
241             zebra_get_version(version_str, sys_str);
242
243             printf("Zebra %s\n", version_str);
244             printf("(C) 1994-2009, Index Data ApS\n");
245             printf("Zebra is free software, covered by the GNU General Public License, and you are\n");
246             printf("welcome to change it and/or distribute copies of it under certain conditions.\n");
247             printf("SHA1 ID: %s\n", sys_str);
248             if (strcmp(version_str, ZEBRAVER))
249                 printf("zebraidx compiled version %s\n", ZEBRAVER);
250 #if YAZ_HAVE_ICU
251             printf("Using ICU\n");
252 #endif
253         }
254         else if (ret == 'v')
255             yaz_log_init_level(yaz_log_mask_str(arg));
256         else if (ret == 'l')
257             yaz_log_init_file(arg);
258         else if (ret == 'm')
259             res_set(res, "memMax", arg);
260         else if (ret == 'd')
261             database = arg;
262         else if (ret == 's')
263             res_set(res, "openRW", "0");
264         else if (ret == 'g')
265             res_set(res, "group", arg);
266         else if (ret == 'f')
267             res_set(res, "fileVerboseLimit", arg);
268         else if (ret == 'c')
269             configName = arg;
270         else if (ret == 't')
271             res_set(res, "recordType", arg);
272         else if (ret == 'n')
273             enable_commit = 0;
274         else if (ret == 'L')
275             res_set(res, "followLinks", "0");
276         else
277             yaz_log(YLOG_WARN, "unknown option '-%s'", arg);
278     } /* while arg */
279
280     if (trans_started)
281         if (zebra_end_trans(zh) != ZEBRA_OK)
282             yaz_log(YLOG_WARN, "zebra_end_trans failed");
283
284     zebra_close(zh);
285     zebra_stop(zs);
286
287     res_close(res);
288     res_close(default_res);
289     exit(0);
290     return 0;
291 }
292
293 /*
294  * Local variables:
295  * c-basic-offset: 4
296  * indent-tabs-mode: nil
297  * End:
298  * vim: shiftwidth=4 tabstop=8 expandtab
299  */
300