Follow of symlinks can be controlled with config followLinks and option -L
[idzebra-moved-to-github.git] / index / main.c
1 /* $Id: main.c,v 1.96 2002-09-03 11:44:54 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 #include <yaz/data1.h>
39 #include "zebraapi.h"
40
41 char *prog;
42
43 int main (int argc, char **argv)
44 {
45     int ret;
46     int cmd = 0;
47     char *arg;
48     char *configName = 0;
49     int nsections = 0;
50     int disableCommit = 0;
51     size_t mem_max = 0;
52 #if HAVE_SYS_TIMES_H
53     struct tms tms1, tms2;
54     struct timeval start_time, end_time;
55     long usec;
56 #endif
57 #ifndef WIN32
58     char nbuf[100];
59 #endif
60     struct recordGroup rGroupDef;
61     ZebraService zs = 0;
62     ZebraHandle zh = 0;
63
64     nmem_init ();
65
66 #ifdef WIN32
67 #else
68     sprintf(nbuf, "%.40s(%d)", *argv, getpid());
69     yaz_log_init_prefix (nbuf);
70 #endif
71 #if HAVE_SYS_TIMES_H
72     times(&tms1);
73     gettimeofday(&start_time, 0);
74 #endif
75
76     rGroupDef.groupName = NULL;
77     rGroupDef.databaseName = NULL;
78     rGroupDef.path = NULL;
79     rGroupDef.recordId = NULL;
80     rGroupDef.recordType = NULL;
81     rGroupDef.flagStoreData = -1;
82     rGroupDef.flagStoreKeys = -1;
83     rGroupDef.flagRw = 1;
84     rGroupDef.databaseNamePath = 0;
85     rGroupDef.explainDatabase = 0;
86     rGroupDef.fileVerboseLimit = 100000;
87     rGroupDef.followLinks = -1;
88
89     prog = *argv;
90     if (argc < 2)
91     {
92         fprintf (stderr, "%s [options] command <dir> ...\n"
93         "Commands:\n"
94         " update <dir>  Update index with files below <dir>.\n"
95         "               If <dir> is empty filenames are read from stdin.\n"
96         " delete <dir>  Delete index with files below <dir>.\n"
97         " commit        Commit changes\n"
98         " clean         Clean shadow files\n"
99         "Options:\n"
100         " -t <type>     Index files as <type> (grs or text).\n"
101         " -c <config>   Read configuration file <config>.\n"
102         " -g <group>    Index files according to group settings.\n"
103         " -d <database> Records belong to Z39.50 database <database>.\n"
104         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
105         " -n            Don't use shadow system.\n"
106         " -s            Show analysis on stdout, but do no work.\n"
107         " -v <level>    Set logging to <level>.\n"
108         " -l <file>     Write log to <file>.\n"
109         " -L            Don't follow symbolic links.\n"
110         " -f <n>        Display information for the first <n> records.\n"
111         " -V            Show version.\n", *argv
112                  );
113         exit (1);
114     }
115     while ((ret = options ("sVt:c:g:d:m:v:nf:l:L"
116                            , argv, argc, &arg)) != -2)
117     {
118         if (ret == 0)
119         {
120             if(cmd == 0) /* command */
121             {
122                 if (!zs)
123                 {
124                     const char *config = configName ? configName : "zebra.cfg";
125                     logf (LOG_LOG, "Zebra version %s %s",
126                           ZEBRAVER, ZEBRADATE);
127                     zs = zebra_start (config);
128                     if (!zs)
129                     {
130                         yaz_log (LOG_FATAL, "Cannot read config %s", config);
131                         exit (1);
132                     }   
133                     zh = zebra_open (zs);
134                     if (disableCommit)
135                         zebra_shadow_enable (zh, 0);
136                 }
137                 if (rGroupDef.databaseName)
138                 {
139                     if (zebra_select_database (zh, rGroupDef.databaseName))
140                         exit (1);
141                 }
142                 else
143                 {
144                     if (zebra_select_database (zh, "Default"))
145                         exit (1);
146                 }
147
148                 if (!strcmp (arg, "update"))
149                     cmd = 'u';
150                 else if (!strcmp (arg, "update1"))
151                     cmd = 'U';
152                 else if (!strcmp (arg, "update2"))
153                     cmd = 'm';
154                 else if (!strcmp (arg, "dump"))
155                     cmd = 's';
156                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
157                     cmd = 'd';
158                 else if (!strcmp (arg, "init"))
159                 {
160                     zebra_init (zh);
161                 }
162                 else if (!strcmp (arg, "commit"))
163                 {
164                     zebra_commit (zh);
165                 }
166                 else if (!strcmp (arg, "clean"))
167                 {
168                     assert (!"todo");
169                 }
170                 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
171                 {
172                     zebra_register_statistics (zh,0);
173                 }
174                 else if (!strcmp (arg, "dump") || !strcmp (arg, "dumpdict"))
175                 {
176                     zebra_register_statistics (zh,1);
177                 }
178                 else if (!strcmp (arg, "compact"))
179                 {
180                     zebra_compact (zh);
181                 }
182                 else
183                 {
184                     logf (LOG_FATAL, "unknown command: %s", arg);
185                     exit (1);
186                 }
187             }
188             else
189             {
190                 rGroupDef.path = arg;
191                 zebra_set_group (zh, &rGroupDef);
192                 zebra_begin_trans (zh);
193
194                 switch (cmd)
195                 {
196                 case 'u':
197                     zebra_repository_update (zh);
198                     break;
199                 case 'd':
200                     zebra_repository_delete (zh);
201                     break;
202                 case 's':
203                     logf (LOG_LOG, "dumping %s", rGroupDef.path);
204                     zebra_repository_show (zh);
205                     nsections = 0;
206                     break;
207                 default:
208                     nsections = 0;
209                 }
210                 cmd = 0;
211                 zebra_end_trans (zh);
212                 log_event_end (NULL, NULL);
213             }
214         }
215         else if (ret == 'V')
216         {
217             fprintf (stderr, "Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
218             fprintf (stderr, " (C) 1994-2002, Index Data ApS\n");
219 #ifdef WIN32
220 #ifdef _DEBUG
221             fprintf (stderr, " WIN32 Debug\n");
222 #else
223             fprintf (stderr, " WIN32 Release\n");
224 #endif
225 #endif
226 #if HAVE_BZLIB_H
227             fprintf (stderr, "libbzip2\n"
228                      " (C) 1996-1999 Julian R Seward.  All rights reserved.\n");
229 #endif
230         }
231         else if (ret == 'v')
232             yaz_log_init_level (yaz_log_mask_str(arg));
233         else if (ret == 'l')
234             yaz_log_init_file (arg);
235         else if (ret == 'm')
236             mem_max = 1024*1024*atoi(arg);
237         else if (ret == 'd')
238             rGroupDef.databaseName = arg;
239         else if (ret == 's')
240             rGroupDef.flagRw = 0;
241         else if (ret == 'g')
242             rGroupDef.groupName = arg;
243         else if (ret == 'f')
244             rGroupDef.fileVerboseLimit = atoi(arg);
245         else if (ret == 'c')
246             configName = arg;
247         else if (ret == 't')
248             rGroupDef.recordType = arg;
249         else if (ret == 'n')
250             disableCommit = 1;
251         else if (ret == 'L')
252             rGroupDef.followLinks = 0;
253         else
254             logf (LOG_WARN, "unknown option '-%s'", arg);
255     }
256     zebra_close (zh);
257     zebra_stop (zs);
258 #if HAVE_SYS_TIMES_H
259     gettimeofday(&end_time, 0);
260     usec = (end_time.tv_sec - start_time.tv_sec) * 1000000L +
261             end_time.tv_usec - start_time.tv_usec;
262     times(&tms2);
263     yaz_log (LOG_LOG, "zebraidx times: %5.2f %5.2f %5.2f",
264                 (double) usec / 1000000.0,
265                 (double) (tms2.tms_utime - tms1.tms_utime)/100,
266                 (double) (tms2.tms_stime - tms1.tms_stime)/100);
267 #endif
268     exit (0);
269     return 0;
270 }
271