For auth failure, use diagnostic 1011: Init/AC: Bad User/password
[idzebra-moved-to-github.git] / index / main.c
1 /* $Id: main.c,v 1.112.2.4 2005-05-30 13:42:04 adam Exp $
2    Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
27 #ifdef WIN32
28 #include <io.h>
29 #else
30 #include <unistd.h>
31 #include <sys/time.h>
32 #endif
33 #include <time.h>
34 #if HAVE_SYS_TIMES_H
35 #include <sys/times.h>
36 #endif
37
38 #if HAVE_TCL_H
39 #include <tcl.h>
40 #endif
41
42 #if HAVE_EXPAT_H
43 #include <expat.h>
44 #endif
45
46 #include <data1.h>
47 #include "zebraapi.h"
48
49 char *prog;
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, 0);
61     
62     int trans_started=0;
63 #if HAVE_SYS_TIMES_H
64     struct tms tms1, tms2;
65     struct timeval start_time, end_time;
66     double usec;
67 #endif
68 #ifndef WIN32
69     char nbuf[100];
70 #endif
71     ZebraService zs = 0;
72     ZebraHandle zh = 0;
73
74     nmem_init ();
75
76 #ifdef WIN32
77 #else
78     sprintf(nbuf, "%.40s(%d)", *argv, getpid());
79     yaz_log_init_prefix (nbuf);
80 #endif
81 #if HAVE_SYS_TIMES_H
82     times(&tms1);
83     gettimeofday(&start_time, 0);
84 #endif
85     prog = *argv;
86     if (argc < 2)
87     {
88         fprintf (stderr, "%s [options] command <dir> ...\n"
89         "Commands:\n"
90         " update <dir>  Update index with files below <dir>.\n"
91         "               If <dir> is empty filenames are read from stdin.\n"
92         " delete <dir>  Delete index with files below <dir>.\n"
93         " create <db>   Create database <db>\n"
94         " drop <db>     Drop database <db>\n"
95         " commit        Commit changes\n"
96         " clean         Clean shadow files\n"
97         "Options:\n"
98         " -t <type>     Index files as <type> (grs or text).\n"
99         " -c <config>   Read configuration file <config>.\n"
100         " -g <group>    Index files according to group settings.\n"
101         " -d <database> Records belong to Z39.50 database <database>.\n"
102         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
103         " -n            Don't use shadow system.\n"
104         " -s            Show analysis on stdout, but do no work.\n"
105         " -v <level>    Set logging to <level>.\n"
106         " -l <file>     Write log to <file>.\n"
107         " -L            Don't follow symbolic links.\n"
108         " -f <n>        Display information for the first <n> records.\n"
109         " -V            Show version.\n", *argv
110                  );
111         exit (1);
112     }
113     while ((ret = options ("sVt:c:g:d:m:v:nf:l:L"
114                            , argv, argc, &arg)) != -2)
115     {
116         if (ret == 0)
117         {
118             if(cmd == 0) /* command */
119             {
120                 if (!zs)
121                 {
122                     const char *config = configName ? configName : "zebra.cfg";
123                     zs = zebra_start_res (config, 0, res);
124                     if (!zs)
125                     {
126                         yaz_log (LOG_FATAL, "Cannot read config %s", config);
127                         exit (1);
128                     }   
129                     zh = zebra_open (zs);
130                     zebra_shadow_enable (zh, enable_commit);
131                 }
132
133                 if (database && zebra_select_database (zh, database))
134                 {
135                     logf(LOG_FATAL, "Could not select database %s errCode=%d",
136                          database, zebra_errCode(zh) );
137                     exit (1);
138                 }
139                 if (!strcmp (arg, "update"))
140                     cmd = 'u';
141                 else if (!strcmp (arg, "update1"))
142                     cmd = 'U';
143                 else if (!strcmp (arg, "update2"))
144                     cmd = 'm';
145                 else if (!strcmp (arg, "dump"))
146                     cmd = 's';
147                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
148                     cmd = 'd';
149                 else if (!strcmp (arg, "init"))
150                 {
151                     zebra_init (zh);
152                 }
153                 else if (!strcmp(arg, "drop"))
154                 {
155                     cmd = 'D';
156                 }
157                 else if (!strcmp(arg, "create"))
158                 {
159                     cmd = 'C';
160                 }
161                 else if (!strcmp (arg, "commit"))
162                 {
163                     zebra_commit (zh);
164                 }
165                 else if (!strcmp (arg, "clean"))
166                 {
167                     zebra_clean (zh);
168                 }
169                 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
170                 {
171                     zebra_register_statistics (zh,0);
172                 }
173                 else if (!strcmp (arg, "dumpdict"))
174                 {
175                     zebra_register_statistics (zh,1);
176                 }
177                 else if (!strcmp (arg, "compact"))
178                 {
179                     zebra_compact (zh);
180                 }
181                 else
182                 {
183                     logf (LOG_FATAL, "unknown command: %s", arg);
184                     exit (1);
185                 }
186             }
187             else
188             {
189                 if (!trans_started)
190                 {
191                     trans_started=1;
192                     if (zebra_begin_trans (zh, 1))
193                         exit(1);
194                 }
195                 switch (cmd)
196                 {
197                 case 'u':
198                     zebra_repository_update (zh, arg);
199                     break;
200                 case 'd':
201                     zebra_repository_delete (zh, arg);
202                     break;
203                 case 's':
204                     zebra_repository_show (zh, arg);
205                     nsections = 0;
206                     break;
207                 case 'C':
208                     zebra_create_database(zh, arg);
209                     break;
210                 case 'D':
211                     zebra_drop_database(zh, arg);
212                     break;
213                 default:
214                     nsections = 0;
215                 }
216                 log_event_end (NULL, NULL);
217             }
218         }
219         else if (ret == 'V')
220         {
221             printf("Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
222             printf(" (C) 1994-2005, Index Data ApS\n");
223 #ifdef WIN32
224 #ifdef _DEBUG
225             printf(" WIN32 Debug\n");
226 #else
227             printf(" WIN32 Release\n");
228 #endif
229 #endif
230 #if HAVE_BZLIB_H
231             printf("Using: libbzip2, (C) 1996-1999 Julian R Seward.  All rights reserved.\n");
232 #endif
233 #if HAVE_TCL_H
234             printf("Using: Tcl %s\n", TCL_VERSION);
235 #endif
236 #if HAVE_EXPAT_H
237             if (1)
238             {
239                 XML_Expat_Version v = XML_ExpatVersionInfo();
240                 printf ("Using: Expat %d.%d.%d\n",
241                         v.major, v.minor, v.micro);
242             }
243 #endif
244         }
245         else if (ret == 'v')
246             yaz_log_init_level (yaz_log_mask_str(arg));
247         else if (ret == 'l')
248             yaz_log_init_file (arg);
249         else if (ret == 'm')
250             res_set(res, "memMax", arg);
251         else if (ret == 'd')
252             database = arg;
253         else if (ret == 's')
254             res_set(res, "openRW", "0");
255         else if (ret == 'g')
256             res_set(res, "group", arg);
257         else if (ret == 'f')
258             res_set(res, "fileVerboseLimit", arg);
259         else if (ret == 'c')
260             configName = arg;
261         else if (ret == 't')
262             res_set(res, "recordType", arg);
263         else if (ret == 'n')
264             enable_commit = 0;
265         else if (ret == 'L')
266             res_set(res, "followLinks", "0");
267         else
268             logf (LOG_WARN, "unknown option '-%s'", arg);
269     } /* while arg */
270
271     if (trans_started)
272         zebra_end_trans (zh);
273
274     zebra_close (zh);
275     zebra_stop (zs);
276 #if HAVE_SYS_TIMES_H
277     if (trans_started)
278     {
279         gettimeofday(&end_time, 0);
280         usec = (end_time.tv_sec - start_time.tv_sec) * 1000000.0 +
281             end_time.tv_usec - start_time.tv_usec;
282         times(&tms2);
283         yaz_log (LOG_LOG, "zebraidx times: %5.2f %5.2f %5.2f",
284                 usec / 1000000,
285                 (double) (tms2.tms_utime - tms1.tms_utime)/100,
286                 (double) (tms2.tms_stime - tms1.tms_stime)/100);
287     }
288 #endif
289     nmem_exit();
290     exit (0);
291     return 0;
292 }
293