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