Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
[idzebra-moved-to-github.git] / index / main.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: main.c,v $
7  * Revision 1.39  1996-04-09 10:05:21  adam
8  * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
9  *
10  * Revision 1.38  1996/03/26  16:01:14  adam
11  * New setting lockPath: directory of various lock files.
12  *
13  * Revision 1.37  1996/03/19  12:43:26  adam
14  * Bug fix: File update traversal didn't handle trailing slashes correctly.
15  * Bug fix: Update of sub directory groups wasn't handled correctly.
16  *
17  * Revision 1.36  1996/02/12  18:45:37  adam
18  * New fileVerboseFlag in record group control.
19  *
20  * Revision 1.35  1996/02/12  15:56:11  adam
21  * New code command: unread.
22  *
23  * Revision 1.34  1996/02/07  14:06:39  adam
24  * Better progress report during register merge.
25  * New command: clean - removes temporary shadow files.
26  *
27  * Revision 1.33  1996/02/06  17:11:18  adam
28  * Minor changes.
29  *
30  * Revision 1.32  1996/02/01  20:50:04  adam
31  * Bug fix: zebraIndexUnlock was always called even though zebraIndexLock
32  * was never called - happens when no commands are specified.
33  *
34  * Revision 1.31  1996/01/08  19:15:46  adam
35  * New input filter that works!
36  *
37  * Revision 1.30  1995/12/12  16:00:59  adam
38  * System call sync(2) used after update/commit.
39  * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
40  * and F_RDLCK.
41  *
42  * Revision 1.29  1995/12/11  11:43:30  adam
43  * Locking based on fcntl instead of flock.
44  * Setting commitEnable removed. Command line option -n can be used to
45  * prevent commit if commit setting is defined in the configuration file.
46  *
47  * Revision 1.28  1995/12/08  16:22:56  adam
48  * Work on update while servers are running. Three lock files introduced.
49  * The servers reload their registers when necessary, but they don't
50  * reestablish result sets yet.
51  *
52  * Revision 1.27  1995/12/07  17:38:47  adam
53  * Work locking mechanisms for concurrent updates/commit.
54  *
55  * Revision 1.26  1995/12/06  12:41:23  adam
56  * New command 'stat' for the index program.
57  * Filenames can be read from stdin by specifying '-'.
58  * Bug fix/enhancement of the transformation from terms to regular
59  * expressons in the search engine.
60  *
61  * Revision 1.25  1995/12/01  16:24:39  adam
62  * Commit files use separate meta file area.
63  *
64  * Revision 1.24  1995/11/30  17:01:38  adam
65  * New setting commitCache: points to commit directories/files.
66  * New command commit: commits at the end of a zebraidx run.
67  *
68  * Revision 1.23  1995/11/30  08:34:31  adam
69  * Started work on commit facility.
70  * Changed a few malloc/free to xmalloc/xfree.
71  *
72  * Revision 1.22  1995/11/28  09:09:42  adam
73  * Zebra config renamed.
74  * Use setting 'recordId' to identify record now.
75  * Bug fix in recindex.c: rec_release_blocks was invokeded even
76  * though the blocks were already released.
77  * File traversal properly deletes records when needed.
78  *
79  * Revision 1.21  1995/11/27  14:27:39  adam
80  * Renamed 'update' command to 'dir'.
81  *
82  * Revision 1.20  1995/11/27  13:58:53  adam
83  * New option -t. storeStore data implemented in server.
84  *
85  * Revision 1.19  1995/11/25  10:24:06  adam
86  * More record fields - they are enumerated now.
87  * New options: flagStoreData flagStoreKey.
88  *
89  * Revision 1.18  1995/11/22  17:19:17  adam
90  * Record management uses the bfile system.
91  *
92  * Revision 1.17  1995/11/21  15:01:16  adam
93  * New general match criteria implemented.
94  * New feature: document groups.
95  *
96  * Revision 1.16  1995/11/20  11:56:27  adam
97  * Work on new traversal.
98  *
99  * Revision 1.15  1995/11/01  16:25:51  quinn
100  * *** empty log message ***
101  *
102  * Revision 1.14  1995/10/17  18:02:09  adam
103  * New feature: databases. Implemented as prefix to words in dictionary.
104  *
105  * Revision 1.13  1995/10/10  12:24:39  adam
106  * Temporary sort files are compressed.
107  *
108  * Revision 1.12  1995/10/04  16:57:20  adam
109  * Key input and merge sort in one pass.
110  *
111  * Revision 1.11  1995/09/29  14:01:45  adam
112  * Bug fixes.
113  *
114  * Revision 1.10  1995/09/28  14:22:57  adam
115  * Sort uses smaller temporary files.
116  *
117  * Revision 1.9  1995/09/14  07:48:24  adam
118  * Record control management.
119  *
120  * Revision 1.8  1995/09/06  16:11:18  adam
121  * Option: only one word key per file.
122  *
123  * Revision 1.7  1995/09/05  15:28:39  adam
124  * More work on search engine.
125  *
126  * Revision 1.6  1995/09/04  12:33:43  adam
127  * Various cleanup. YAZ util used instead.
128  *
129  * Revision 1.5  1995/09/04  09:10:39  adam
130  * More work on index add/del/update.
131  * Merge sort implemented.
132  * Initial work on z39 server.
133  *
134  * Revision 1.4  1995/09/01  14:06:36  adam
135  * Split of work into more files.
136  *
137  * Revision 1.3  1995/09/01  10:57:07  adam
138  * Minor changes.
139  *
140  * Revision 1.2  1995/09/01  10:30:24  adam
141  * More work on indexing. Not working yet.
142  *
143  * Revision 1.1  1995/08/31  14:50:24  adam
144  * New simple file index tool.
145  *
146  */
147 #include <stdio.h>
148 #include <assert.h>
149 #include <unistd.h>
150
151 #include <alexutil.h>
152 #include <data1.h>
153 #include "index.h"
154
155 char *prog;
156 size_t mem_max = 4*1024*1024;
157 extern char *data1_tabpath;
158
159 static void abort_func (int level, const char *msg, void *info)
160 {
161     if (level & LOG_FATAL)
162         abort ();
163 }
164
165 int main (int argc, char **argv)
166 {
167     int ret;
168     int cmd = 0;
169     char *arg;
170     char *configName = NULL;
171     int nsections;
172     int disableCommit = 0;
173
174     struct recordGroup rGroupDef;
175     
176     rGroupDef.groupName = NULL;
177     rGroupDef.databaseName = NULL;
178     rGroupDef.path = NULL;
179     rGroupDef.recordId = NULL;
180     rGroupDef.recordType = NULL;
181     rGroupDef.flagStoreData = -1;
182     rGroupDef.flagStoreKeys = -1;
183     rGroupDef.fileVerboseFlag = 1;
184
185     prog = *argv;
186     if (argc < 2)
187     {
188         fprintf (stderr, "zebraidx [options] command <dir> ...\n"
189         "Commands:\n"
190         " update <dir>  Update index with files below <dir>.\n"
191         "               If <dir> is empty filenames are read from stdin.\n"
192         " delete <dir>  Delete index with files below <dir>.\n"
193         " commit        Commit changes\n"
194         " clean         Clean shadow files\n"
195         "Options:\n"
196         " -t <type>     Index files as <type> (grs or text).\n"
197         " -c <config>   Read configuration file <config>.\n"
198         " -g <group>    Index files according to group settings.\n"
199         " -d <database> Records belong to Z39.50 database <database>.\n"
200         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
201         " -n            Don't use shadow system\n"
202         " -v <level>    Set logging to <level>.\n");
203         exit (1);
204     }
205     log_event_end (abort_func, NULL);
206     while ((ret = options ("t:c:g:d:m:v:n", argv, argc, &arg)) != -2)
207     {
208         if (ret == 0)
209         {
210             const char *rval;
211             if(cmd == 0) /* command */
212             {
213                 if (!common_resource)
214                 {
215                     common_resource = res_open (configName ?
216                                                 configName : FNAME_CONFIG);
217                     if (!common_resource)
218                     {
219                         logf (LOG_FATAL, "Cannot open resource `%s'",
220                               configName);
221                         exit (1);
222                     }
223                     data1_tabpath = res_get (common_resource, "profilePath");
224                     bf_lockDir (res_get (common_resource, "lockPath"));
225                 }
226                 if (!strcmp (arg, "update"))
227                     cmd = 'u';
228                 else if (!strcmp (arg, "update1"))
229                     cmd = 'U';
230                 else if (!strcmp (arg, "update2"))
231                     cmd = 'm';
232                 else if (!strcmp (arg, "dump"))
233                     cmd = 's';
234                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
235                     cmd = 'd';
236                 else if (!strcmp (arg, "commit"))
237                 {
238                     zebraIndexLock (1);
239                     rval = res_get (common_resource, "shadow");
240                     if (rval && *rval)
241                         bf_cache (1);
242                     else
243                     {
244                         logf (LOG_FATAL, "Cannot perform commit");
245                         logf (LOG_FATAL, "No shadow area defined");
246                         exit (1);
247                     }
248                     if (bf_commitExists ())
249                     {
250                         logf (LOG_LOG, "Commit start");
251                         zebraIndexLockMsg ("c");
252                         zebraIndexWait (1);
253                         logf (LOG_LOG, "Commit execute");
254                         bf_commitExec ();
255                         sync ();
256                         zebraIndexLockMsg ("d");
257                         zebraIndexWait (0);
258                         logf (LOG_LOG, "Commit clean");
259                         bf_commitClean ();
260                     }
261                     else
262                         logf (LOG_LOG, "Nothing to commit");
263                 }
264                 else if (!strcmp (arg, "clean"))
265                 {
266                     zebraIndexLock (1);
267                     if (bf_commitExists ())
268                     {
269                         zebraIndexLockMsg ("d");
270                         zebraIndexWait (0);
271                         logf (LOG_LOG, "Commit clean");
272                         bf_commitClean ();
273                     }
274                     else
275                         logf (LOG_LOG, "Nothing to clean");
276                 }
277                 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
278                 {
279                     zebraIndexLock (0);
280                     rval = res_get (common_resource, "shadow");
281                     if (rval && *rval)
282                     {
283                         bf_cache (1);
284                         zebraIndexLockMsg ("r");
285                     }
286                     rec_prstat ();
287                 }
288                 else
289                 {
290                     logf (LOG_FATAL, "Unknown command: %s", arg);
291                     exit (1);
292                 }
293             }
294             else
295             {
296                 struct recordGroup rGroup;
297
298                 zebraIndexLock (0);
299                 rval = res_get (common_resource, "shadow");
300                 if (rval && *rval && !disableCommit)
301                 {
302                     bf_cache (1);
303                     zebraIndexLockMsg ("r");
304                 }
305                 else
306                 {
307                     bf_cache (0);
308                     zebraIndexLockMsg ("w");
309                 }
310                 zebraIndexWait (0);
311                 
312                 memcpy (&rGroup, &rGroupDef, sizeof(rGroup));
313                 rGroup.path = arg;
314                 switch (cmd)
315                 {
316                 case 'u':
317                     key_open (mem_max);
318                     logf (LOG_LOG, "Updating %s", rGroup.path);
319                     repositoryUpdate (&rGroup);
320                     nsections = key_close ();
321                     break;
322                 case 'U':
323                     key_open (mem_max);
324                     logf (LOG_LOG, "Updating (pass 1) %s", rGroup.path);
325                     repositoryUpdate (&rGroup);
326                     key_close ();
327                     nsections = 0;
328                     break;
329                 case 'd':
330                     key_open (mem_max);
331                     logf (LOG_LOG, "Deleting %s", rGroup.path);
332                     repositoryDelete (&rGroup);
333                     nsections = key_close ();
334                     break;
335                 case 's':
336                     logf (LOG_LOG, "Dumping %s", rGroup.path);
337                     repositoryShow (&rGroup);
338                     nsections = 0;
339                     break;
340                 case 'm':
341                     nsections = -1;
342                     break;
343                 default:
344                     nsections = 0;
345                 }
346                 cmd = 0;
347                 if (nsections)
348                 {
349                     logf (LOG_LOG, "Merging with index");
350                     key_input (FNAME_WORD_DICT, FNAME_WORD_ISAM, nsections,
351                                60);
352                     sync ();
353                 }
354             }
355         }
356         else if (ret == 'v')
357         {
358             log_init (log_mask_str(arg), prog, NULL);
359         }
360         else if (ret == 'm')
361         {
362             mem_max = 1024*1024*atoi(arg);
363         }
364         else if (ret == 'd')
365         {
366             rGroupDef.databaseName = arg;
367         }
368         else if (ret == 'g')
369         {
370             rGroupDef.groupName = arg;
371         }
372         else if (ret == 'c')
373             configName = arg;
374         else if (ret == 't')
375             rGroupDef.recordType = arg;
376         else if (ret == 'n')
377             disableCommit = 1;
378         else
379         {
380             logf (LOG_FATAL, "Unknown option '-%s'", arg);
381             exit (1);
382         }
383     }
384     if (common_resource)
385         zebraIndexUnlock ();
386     exit (0);
387 }
388