IDZEBRA  2.2.7
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/snprintf.h>
39 #include <yaz/options.h>
40 #include <idzebra/version.h>
41 #include <idzebra/api.h>
42 
43 char *prog;
44 
45 static void filter_cb(void *cd, const char *name)
46 {
47  puts(name);
48 }
49 
50 static void show_filters(ZebraService zs)
51 {
53 }
54 
55 int main(int argc, char **argv)
56 {
57  int ret;
58  int cmd = 0;
59  char *arg;
60  char *configName = 0;
61  int enable_commit = 1;
62  char *database = 0;
63  Res res = res_open(0, 0);
64  Res default_res = res_open(0, 0);
65 
66  int trans_started = 0;
67 #ifndef WIN32
68  char nbuf[100];
69 #endif
70  ZebraService zs = 0;
71  ZebraHandle zh = 0;
72 
73 #ifdef WIN32
74 #else
75  yaz_snprintf(nbuf, sizeof(nbuf), "%s(%ld)", *argv, (long) getpid());
76  yaz_log_init_prefix(nbuf);
77 #endif
78  prog = *argv;
79  if (argc < 2)
80  {
81  fprintf(stderr, "%s [options] command <dir> ...\n"
82  "Commands:\n"
83  " update <dir> Update index with files below <dir>.\n"
84  " If <dir> is empty filenames are read from stdin.\n"
85  " delete <dir> Delete index with files below <dir>.\n"
86  " create <db> Create database <db>\n"
87  " drop <db> Drop database <db>\n"
88  " commit Commit changes\n"
89  " clean Clean shadow files\n"
90  " check mode Check register; mode is one of: default, full, quick\n"
91  "Options:\n"
92  " -t <type> Index files as <type> (grs or text).\n"
93  " -c <config> Read configuration file <config>.\n"
94  " -g <group> Index files according to group settings.\n"
95  " -d <database> Records belong to Z39.50 database <database>.\n"
96  " -m <mbytes> Use <mbytes> before flushing keys to disk.\n"
97  " -n Don't use shadow system.\n"
98  " -s Show analysis on stdout, but do no work.\n"
99  " -v <level> Set logging to <level>.\n"
100  " -l <file> Write log to <file>.\n"
101  " -L Don't follow symbolic links.\n"
102  " -f <n> Display information for the first <n> records.\n"
103  " -V Show version.\n", *argv
104  );
105  exit(1);
106  }
107  res_set(default_res, "profilePath", DEFAULT_PROFILE_PATH);
108  res_set(default_res, "modulePath", DEFAULT_MODULE_PATH);
109  while ((ret = options("sVt:c:g:d:m:v:nf:l:L", argv, argc, &arg)) != -2)
110  {
111  if (ret == 0)
112  {
113  if (cmd == 0) /* command */
114  {
115  if (!zs)
116  {
117  const char *config = configName ? configName : "zebra.cfg";
118  zs = zebra_start_res(config, default_res, res);
119  if (!zs)
120  {
121  yaz_log(YLOG_FATAL, "Cannot read config %s", config);
122  exit(1);
123  }
124  zh = zebra_open(zs, 0);
125  zebra_shadow_enable(zh, enable_commit);
126  }
127 
128  if (database &&
129  zebra_select_database(zh, database) == ZEBRA_FAIL)
130  {
131  yaz_log(YLOG_FATAL, "Could not select database %s "
132  "errCode=%d",
133  database, zebra_errCode(zh) );
134  exit(1);
135  }
136  if (!strcmp(arg, "update"))
137  cmd = 'u';
138  else if (!strcmp(arg, "update1"))
139  cmd = 'U';
140  else if (!strcmp(arg, "update2"))
141  cmd = 'm';
142  else if (!strcmp(arg, "dump"))
143  cmd = 's';
144  else if (!strcmp(arg, "del") || !strcmp(arg, "delete"))
145  cmd = 'd';
146  else if (!strcmp(arg, "adelete"))
147  cmd = 'a';
148  else if (!strcmp(arg, "init"))
149  {
150  zebra_init(zh);
151  }
152  else if (!strcmp(arg, "drop"))
153  {
154  cmd = 'D';
155  }
156  else if (!strcmp(arg, "create"))
157  {
158  cmd = 'C';
159  }
160  else if (!strcmp(arg, "commit"))
161  {
162  zebra_commit(zh);
163  }
164  else if (!strcmp(arg, "clean"))
165  {
166  zebra_clean(zh);
167  }
168  else if (!strcmp(arg, "stat") || !strcmp(arg, "status"))
169  {
171  }
172  else if (!strcmp(arg, "dumpdict"))
173  {
175  }
176  else if (!strcmp(arg, "compact"))
177  {
178  zebra_compact(zh);
179  }
180  else if (!strcmp(arg, "filters"))
181  {
182  show_filters(zs);
183  }
184  else if (!strcmp(arg, "check"))
185  {
186  cmd = 'K';
187  }
188  else
189  {
190  yaz_log(YLOG_FATAL, "unknown command: %s", arg);
191  exit(1);
192  }
193  }
194  else
195  {
196  ZEBRA_RES res = ZEBRA_OK;
197  if (!trans_started)
198  {
199  trans_started = 1;
200  if (zebra_begin_trans(zh, 1) != ZEBRA_OK)
201  exit(1);
202  }
203  switch (cmd)
204  {
205  case 'K':
206  res = zebra_register_check(zh, arg);
207  break;
208  case 'u':
209  res = zebra_repository_index(zh, arg, action_update);
210  break;
211  case 'd':
212  res = zebra_repository_index(zh, arg, action_delete);
213  break;
214  case 'a':
215  res = zebra_repository_index(zh, arg, action_a_delete);
216  break;
217  case 's':
218  res = zebra_repository_show(zh, arg);
219  break;
220  case 'C':
221  res = zebra_create_database(zh, arg);
222  break;
223  case 'D':
224  res = zebra_drop_database(zh, arg);
225  break;
226  }
227  if (res != ZEBRA_OK)
228  {
229  const char *add = zebra_errAdd(zh);
230  yaz_log(YLOG_FATAL, "Operation failed: %s %s",
231  zebra_errString(zh), add ? add : "");
232 
233  if (trans_started)
234  if (zebra_end_trans(zh) != ZEBRA_OK)
235  yaz_log(YLOG_WARN, "zebra_end_trans failed");
236 
237 
238  zebra_close(zh);
239  zebra_stop(zs);
240  exit(1);
241  }
242  log_event_end(NULL, NULL);
243  }
244  }
245  else if (ret == 'V')
246  {
247  char version_str[20];
248  char sys_str[80];
249  zebra_get_version(version_str, sys_str);
250 
251  printf("Zebra %s\n", version_str);
252  printf("(C) 1994-2023, Index Data\n");
253  printf("Zebra is free software, covered by the GNU General Public License, and you are\n");
254  printf("welcome to change it and/or distribute copies of it under certain conditions.\n");
255  printf("SHA1 ID: %s\n", sys_str);
256  if (strcmp(version_str, ZEBRAVER))
257  printf("zebraidx compiled version %s\n", ZEBRAVER);
258 #if YAZ_HAVE_ICU
259  printf("Using ICU\n");
260 #endif
261  }
262  else if (ret == 'v')
263  yaz_log_init_level(yaz_log_mask_str(arg));
264  else if (ret == 'l')
265  yaz_log_init_file(arg);
266  else if (ret == 'm')
267  res_set(res, "memMax", arg);
268  else if (ret == 'd')
269  database = arg;
270  else if (ret == 's')
271  res_set(res, "openRW", "0");
272  else if (ret == 'g')
273  res_set(res, "group", arg);
274  else if (ret == 'f')
275  res_set(res, "fileVerboseLimit", arg);
276  else if (ret == 'c')
277  configName = arg;
278  else if (ret == 't')
279  res_set(res, "recordType", arg);
280  else if (ret == 'n')
281  enable_commit = 0;
282  else if (ret == 'L')
283  res_set(res, "followLinks", "0");
284  else
285  yaz_log(YLOG_WARN, "unknown option '-%s'", arg);
286  } /* while arg */
287 
288  ret = 0;
289  if (trans_started)
290  if (zebra_end_trans(zh) != ZEBRA_OK)
291  {
292  yaz_log(YLOG_WARN, "zebra_end_trans failed");
293  ret = 1;
294  }
295 
296  zebra_close(zh);
297  zebra_stop(zs);
298 
299  res_close(res);
300  res_close(default_res);
301  exit(ret);
302  return 0;
303 }
304 
305 /*
306  * Local variables:
307  * c-basic-offset: 4
308  * c-file-style: "Stroustrup"
309  * indent-tabs-mode: nil
310  * End:
311  * vim: shiftwidth=4 tabstop=8 expandtab
312  */
313 
Zebra API.
ZEBRA_RES zebra_compact(ZebraHandle zh)
Definition: zebraapi.c:2187
ZEBRA_RES zebra_init(ZebraHandle zh)
Definition: zebraapi.c:2154
ZEBRA_RES zebra_end_trans(ZebraHandle zh) ZEBRA_GCC_ATTR((warn_unused_result))
Definition: zebraapi.c:1910
ZEBRA_RES zebra_close(ZebraHandle zh)
Destroys Zebra session handle.
Definition: zebraapi.c:657
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:197
ZEBRA_RES zebra_register_check(ZebraHandle zh, const char *spec)
Definition: zebraapi.c:2413
ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
Deletes a database (drop)
Definition: zebraapi.c:1518
int zebra_register_statistics(ZebraHandle zh, int dumpdict)
Definition: invstat.c:174
ZEBRA_RES zebra_clean(ZebraHandle zh)
Definition: zebraapi.c:2139
ZEBRA_RES zebra_commit(ZebraHandle zh)
Definition: zebraapi.c:2146
ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
Creates a database.
Definition: zebraapi.c:1562
ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw) ZEBRA_GCC_ATTR((warn_unused_result))
Definition: zebraapi.c:1710
int zebra_errCode(ZebraHandle zh)
Returns error code for last error.
Definition: zebraapi.c:1343
ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename) ZEBRA_GCC_ATTR((warn_unused_result))
Definition: zebraapi.c:939
const char * zebra_errString(ZebraHandle zh)
Returns error string for last error.
Definition: zebraapi.c:1354
ZEBRA_RES zebra_repository_index(ZebraHandle zh, const char *path, enum zebra_recctrl_action_t action)
Definition: zebraapi.c:2017
ZEBRA_RES zebra_stop(ZebraService zs)
stops a Zebra service.
Definition: zebraapi.c:627
void zebra_filter_info(ZebraService zs, void *cd, void(*cb)(void *cd, const char *name))
Lists enabled Zebra filters.
Definition: zebraapi.c:306
char * zebra_errAdd(ZebraHandle zh)
Returns additional info for last error.
Definition: zebraapi.c:1363
ZebraHandle zebra_open(ZebraService zs, Res res) ZEBRA_GCC_ATTR((warn_unused_result))
Creates a Zebra session handle within service.
Definition: zebraapi.c:113
void zebra_shadow_enable(ZebraHandle zh, int value)
Definition: zebraapi.c:2515
ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
Definition: zebraapi.c:2038
static void add(void *set_handle, int seqno, TERMID term)
Definition: rank1.c:168
@ action_a_delete
Definition: recctrl.h:97
@ action_delete
Definition: recctrl.h:93
@ action_update
Definition: recctrl.h:95
void res_set(Res r, const char *name, const char *value)
Definition: res.c:338
void res_close(Res r)
Definition: res.c:261
Res res_open(Res res_def, Res over_res)
Definition: res.c:234
Definition: res.c:46
#define ZEBRA_FAIL
Definition: util.h:81
#define ZEBRA_OK
Definition: util.h:82
short ZEBRA_RES
Common return type for Zebra API.
Definition: util.h:80
#define ZEBRAVER
Version as string.
Definition: version.h:28
void zebra_get_version(char *version_str, char *sha1_str)
Returns Zebra version and SHA1 ID as generated by Git.
Definition: version.c:33
int main(int argc, char **argv)
Definition: zebraidx.c:55
static void show_filters(ZebraService zs)
Definition: zebraidx.c:50
static void filter_cb(void *cd, const char *name)
Definition: zebraidx.c:45
char * prog
Definition: zebraidx.c:43