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