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