IDZEBRA  2.1.2
zebraidx.c
Go to the documentation of this file.
1 /* This file is part of the Zebra server.
2  Copyright (C) Index Data
3 
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8 
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 
18 */
19 
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
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 
50 {
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 enable_commit = 1;
61  char *database = 0;
62  Res res = res_open(0, 0);
63  Res default_res = res_open(0, 0);
64 
65  int trans_started = 0;
66 #ifndef WIN32
67  char nbuf[100];
68 #endif
69  ZebraService zs = 0;
70  ZebraHandle zh = 0;
71 
72 #ifdef WIN32
73 #else
74  sprintf(nbuf, "%.40s(%ld)", *argv, (long) getpid());
75  yaz_log_init_prefix(nbuf);
76 #endif
77  prog = *argv;
78  if (argc < 2)
79  {
80  fprintf(stderr, "%s [options] command <dir> ...\n"
81  "Commands:\n"
82  " update <dir> Update index with files below <dir>.\n"
83  " If <dir> is empty filenames are read from stdin.\n"
84  " delete <dir> Delete index with files below <dir>.\n"
85  " create <db> Create database <db>\n"
86  " drop <db> Drop database <db>\n"
87  " commit Commit changes\n"
88  " clean Clean shadow files\n"
89  " check mode Check register; mode is one of: default, full, quick\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  {
170  }
171  else if (!strcmp(arg, "dumpdict"))
172  {
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 if (!strcmp(arg, "check"))
184  {
185  cmd = 'K';
186  }
187  else
188  {
189  yaz_log(YLOG_FATAL, "unknown command: %s", arg);
190  exit(1);
191  }
192  }
193  else
194  {
195  ZEBRA_RES res = ZEBRA_OK;
196  if (!trans_started)
197  {
198  trans_started = 1;
199  if (zebra_begin_trans(zh, 1) != ZEBRA_OK)
200  exit(1);
201  }
202  switch (cmd)
203  {
204  case 'K':
205  res = zebra_register_check(zh, arg);
206  break;
207  case 'u':
208  res = zebra_repository_index(zh, arg, action_update);
209  break;
210  case 'd':
211  res = zebra_repository_index(zh, arg, action_delete);
212  break;
213  case 'a':
214  res = zebra_repository_index(zh, arg, action_a_delete);
215  break;
216  case 's':
217  res = zebra_repository_show(zh, arg);
218  break;
219  case 'C':
220  res = zebra_create_database(zh, arg);
221  break;
222  case 'D':
223  res = zebra_drop_database(zh, arg);
224  break;
225  }
226  if (res != ZEBRA_OK)
227  {
228  const char *add = zebra_errAdd(zh);
229  yaz_log(YLOG_FATAL, "Operation failed: %s %s",
230  zebra_errString(zh), add ? add : "");
231 
232  if (trans_started)
233  if (zebra_end_trans(zh) != ZEBRA_OK)
234  yaz_log(YLOG_WARN, "zebra_end_trans failed");
235 
236 
237  zebra_close(zh);
238  zebra_stop(zs);
239  exit(1);
240  }
241  log_event_end(NULL, NULL);
242  }
243  }
244  else if (ret == 'V')
245  {
246  char version_str[20];
247  char sys_str[80];
248  zebra_get_version(version_str, sys_str);
249 
250  printf("Zebra %s\n", version_str);
251  printf("(C) 1994-2017, Index Data\n");
252  printf("Zebra is free software, covered by the GNU General Public License, and you are\n");
253  printf("welcome to change it and/or distribute copies of it under certain conditions.\n");
254  printf("SHA1 ID: %s\n", sys_str);
255  if (strcmp(version_str, ZEBRAVER))
256  printf("zebraidx compiled version %s\n", ZEBRAVER);
257 #if YAZ_HAVE_ICU
258  printf("Using ICU\n");
259 #endif
260  }
261  else if (ret == 'v')
262  yaz_log_init_level(yaz_log_mask_str(arg));
263  else if (ret == 'l')
264  yaz_log_init_file(arg);
265  else if (ret == 'm')
266  res_set(res, "memMax", arg);
267  else if (ret == 'd')
268  database = arg;
269  else if (ret == 's')
270  res_set(res, "openRW", "0");
271  else if (ret == 'g')
272  res_set(res, "group", arg);
273  else if (ret == 'f')
274  res_set(res, "fileVerboseLimit", arg);
275  else if (ret == 'c')
276  configName = arg;
277  else if (ret == 't')
278  res_set(res, "recordType", arg);
279  else if (ret == 'n')
280  enable_commit = 0;
281  else if (ret == 'L')
282  res_set(res, "followLinks", "0");
283  else
284  yaz_log(YLOG_WARN, "unknown option '-%s'", arg);
285  } /* while arg */
286 
287  ret = 0;
288  if (trans_started)
289  if (zebra_end_trans(zh) != ZEBRA_OK)
290  {
291  yaz_log(YLOG_WARN, "zebra_end_trans failed");
292  ret = 1;
293  }
294 
295  zebra_close(zh);
296  zebra_stop(zs);
297 
298  res_close(res);
299  res_close(default_res);
300  exit(ret);
301  return 0;
302 }
303 
304 /*
305  * Local variables:
306  * c-basic-offset: 4
307  * c-file-style: "Stroustrup"
308  * indent-tabs-mode: nil
309  * End:
310  * vim: shiftwidth=4 tabstop=8 expandtab
311  */
312 
ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw) ZEBRA_GCC_ATTR((warn_unused_result))
Definition: zebraapi.c:1709
ZEBRA_RES zebra_close(ZebraHandle zh)
Destroys Zebra session handle.
Definition: zebraapi.c:656
ZEBRA_RES zebra_init(ZebraHandle zh)
Definition: zebraapi.c:2153
Zebra API.
#define ZEBRA_OK
Definition: util.h:82
ZEBRA_RES zebra_commit(ZebraHandle zh)
Definition: zebraapi.c:2145
int zebra_errCode(ZebraHandle zh)
Returns error code for last error.
Definition: zebraapi.c:1342
#define ZEBRA_FAIL
Definition: util.h:81
ZebraHandle zh
Definition: zebrash.c:64
ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
Creates a database.
Definition: zebraapi.c:1561
#define ZEBRAVER
Version as string.
Definition: version.h:28
int main(int argc, char **argv)
Definition: zebraidx.c:54
ZebraService zebra_start_res(const char *configName, Res def_res, Res over_res) ZEBRA_GCC_ATTR((warn_unused_result))
Creates a Zebra service with resources.
Definition: zebraapi.c:196
static void show_filters(ZebraService zs)
Definition: zebraidx.c:49
char * zebra_errAdd(ZebraHandle zh)
Returns additional info for last error.
Definition: zebraapi.c:1362
Definition: res.c:46
ZEBRA_RES zebra_compact(ZebraHandle zh)
Definition: zebraapi.c:2186
static void add(void *set_handle, int seqno, TERMID term)
Definition: rank1.c:168
ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
Deletes a database (drop)
Definition: zebraapi.c:1517
void res_set(Res r, const char *name, const char *value)
Definition: res.c:338
ZEBRA_RES zebra_stop(ZebraService zs)
stops a Zebra service.
Definition: zebraapi.c:626
void zebra_filter_info(ZebraService zs, void *cd, void(*cb)(void *cd, const char *name))
Lists enabled Zebra filters.
Definition: zebraapi.c:305
Res res_open(Res res_def, Res over_res)
Definition: res.c:234
ZebraService zs
Definition: zebrash.c:63
ZEBRA_RES zebra_end_trans(ZebraHandle zh) ZEBRA_GCC_ATTR((warn_unused_result))
Definition: zebraapi.c:1909
void zebra_shadow_enable(ZebraHandle zh, int value)
Definition: zebraapi.c:2514
void res_close(Res r)
Definition: res.c:261
ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename) ZEBRA_GCC_ATTR((warn_unused_result))
Definition: zebraapi.c:938
static void filter_cb(void *cd, const char *name)
Definition: zebraidx.c:44
ZEBRA_RES zebra_repository_index(ZebraHandle zh, const char *path, enum zebra_recctrl_action_t action)
Definition: zebraapi.c:2016
ZEBRA_RES zebra_clean(ZebraHandle zh)
Definition: zebraapi.c:2138
int zebra_register_statistics(ZebraHandle zh, int dumpdict)
Definition: invstat.c:174
char * prog
Definition: zebraidx.c:42
void zebra_get_version(char *version_str, char *sha1_str)
Returns Zebra version and SHA1 ID as generated by Git.
Definition: version.c:33
const char * zebra_errString(ZebraHandle zh)
Returns error string for last error.
Definition: zebraapi.c:1353
short ZEBRA_RES
Common return type for Zebra API.
Definition: util.h:80
ZebraHandle zebra_open(ZebraService zs, Res res) ZEBRA_GCC_ATTR((warn_unused_result))
Creates a Zebra session handle within service.
Definition: zebraapi.c:112
ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
Definition: zebraapi.c:2037
ZEBRA_RES zebra_register_check(ZebraHandle zh, const char *spec)
Definition: zebraapi.c:2412