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