Implemented options -f <n> that limits the log to the first <n>
[idzebra-moved-to-github.git] / index / trav.c
1 /*
2  * Copyright (C) 1994-1996, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: trav.c,v $
7  * Revision 1.29  1997-02-12 20:39:47  adam
8  * Implemented options -f <n> that limits the log to the first <n>
9  * records.
10  * Changed some log messages also.
11  *
12  * Revision 1.28  1996/11/01 08:58:44  adam
13  * Interface to isamc system now includes update and delete.
14  *
15  * Revision 1.27  1996/10/29 14:06:56  adam
16  * Include zebrautl.h instead of alexutil.h.
17  *
18  * Revision 1.26  1996/06/04 10:19:01  adam
19  * Minor changes - removed include of ctype.h.
20  *
21  * Revision 1.25  1996/05/01  13:46:37  adam
22  * First work on multiple records in one file.
23  * New option, -offset, to the "unread" command in the filter module.
24  *
25  * Revision 1.24  1996/04/26  10:00:23  adam
26  * Added option -V to zebraidx to display version information.
27  * Removed stupid warnings from file update.
28  *
29  * Revision 1.23  1996/04/12  07:02:25  adam
30  * File update of single files.
31  *
32  * Revision 1.22  1996/04/09 06:50:50  adam
33  * Bug fix: bad reference in function fileUpdateR.
34  *
35  * Revision 1.21  1996/03/22 15:34:18  quinn
36  * Fixed bad reference
37  *
38  * Revision 1.20  1996/03/21  14:50:10  adam
39  * File update uses modify-time instead of change-time.
40  *
41  * Revision 1.19  1996/03/20  16:16:55  quinn
42  * Added diagnostic output
43  *
44  * Revision 1.18  1996/03/19  12:43:27  adam
45  * Bug fix: File update traversal didn't handle trailing slashes correctly.
46  * Bug fix: Update of sub directory groups wasn't handled correctly.
47  *
48  * Revision 1.17  1996/02/12  18:45:17  adam
49  * Changed naming of some functions.
50  *
51  * Revision 1.16  1996/02/05  12:30:02  adam
52  * Logging reduced a bit.
53  * The remaining running time is estimated during register merge.
54  *
55  * Revision 1.15  1995/12/07  17:38:48  adam
56  * Work locking mechanisms for concurrent updates/commit.
57  *
58  * Revision 1.14  1995/12/06  12:41:26  adam
59  * New command 'stat' for the index program.
60  * Filenames can be read from stdin by specifying '-'.
61  * Bug fix/enhancement of the transformation from terms to regular
62  * expressons in the search engine.
63  *
64  * Revision 1.13  1995/11/28  09:09:46  adam
65  * Zebra config renamed.
66  * Use setting 'recordId' to identify record now.
67  * Bug fix in recindex.c: rec_release_blocks was invokeded even
68  * though the blocks were already released.
69  * File traversal properly deletes records when needed.
70  *
71  * Revision 1.12  1995/11/24  11:31:37  adam
72  * Commands add & del read filenames from stdin if source directory is
73  * empty.
74  * Match criteria supports 'constant' strings.
75  *
76  * Revision 1.11  1995/11/22  17:19:19  adam
77  * Record management uses the bfile system.
78  *
79  * Revision 1.10  1995/11/21  15:01:16  adam
80  * New general match criteria implemented.
81  * New feature: document groups.
82  *
83  * Revision 1.9  1995/11/21  09:20:32  adam
84  * Yet more work on record match.
85  *
86  * Revision 1.8  1995/11/20  16:59:46  adam
87  * New update method: the 'old' keys are saved for each records.
88  *
89  * Revision 1.7  1995/11/20  11:56:28  adam
90  * Work on new traversal.
91  *
92  * Revision 1.6  1995/11/17  15:54:42  adam
93  * Started work on virtual directory structure.
94  *
95  * Revision 1.5  1995/10/17  18:02:09  adam
96  * New feature: databases. Implemented as prefix to words in dictionary.
97  *
98  * Revision 1.4  1995/09/28  09:19:46  adam
99  * xfree/xmalloc used everywhere.
100  * Extract/retrieve method seems to work for text records.
101  *
102  * Revision 1.3  1995/09/06  16:11:18  adam
103  * Option: only one word key per file.
104  *
105  * Revision 1.2  1995/09/04  12:33:43  adam
106  * Various cleanup. YAZ util used instead.
107  *
108  * Revision 1.1  1995/09/01  14:06:36  adam
109  * Split of work into more files.
110  *
111  */
112 #include <stdio.h>
113 #include <assert.h>
114 #include <unistd.h>
115 #include <dirent.h>
116 #include <sys/stat.h>
117 #include <sys/types.h>
118 #include <fcntl.h>
119 #include <time.h>
120
121 #include "index.h"
122
123 static int repComp (const char *a, const char *b, size_t len)
124 {
125     if (!len)
126         return 0;
127     return memcmp (a, b, len);
128 }
129
130 static void repositoryExtractR (int deleteFlag, char *rep,
131                                 struct recordGroup *rGroup)
132 {
133     struct dir_entry *e;
134     int i;
135     size_t rep_len = strlen (rep);
136
137     e = dir_open (rep);
138     if (!e)
139         return;
140     logf (LOG_LOG, "dir %s", rep);
141     if (rep[rep_len-1] != '/')
142         rep[rep_len] = '/';
143     else
144         --rep_len;
145     for (i=0; e[i].name; i++)
146     {
147         strcpy (rep +rep_len+1, e[i].name);
148         switch (e[i].kind)
149         {
150         case dirs_file:
151             fileExtract (NULL, rep, rGroup, deleteFlag);
152             break;
153         case dirs_dir:
154             repositoryExtractR (deleteFlag, rep, rGroup);
155             break;
156         }
157     }
158     dir_free (&e);
159
160 }
161
162 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
163                                const char *base, char *src,
164                                struct recordGroup *rGroup)
165 {
166     char tmppath[1024];
167     size_t src_len = strlen (src);
168
169     while (dst && !repComp (dst->path, src, src_len+1))
170     {
171         switch (dst->kind)
172         {
173         case dirs_file:
174             sprintf (tmppath, "%s%s", base, dst->path);
175             fileExtract (&dst->sysno, tmppath, rGroup, 1);
176              
177             strcpy (tmppath, dst->path);
178             dst = dirs_read (di); 
179             dirs_del (di, tmppath);
180             break;
181         case dirs_dir:
182             strcpy (tmppath, dst->path);
183             dst = dirs_read (di);
184             dirs_rmdir (di, tmppath);
185             break;
186         default:
187             dst = dirs_read (di);
188         }
189     }
190 }
191
192 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
193                                const char *base, char *src, 
194                                struct recordGroup *rGroup)
195 {
196     struct dir_entry *e_src;
197     int i_src = 0;
198     static char tmppath[1024];
199     size_t src_len = strlen (src);
200
201     sprintf (tmppath, "%s%s", base, src);
202     e_src = dir_open (tmppath);
203     logf (LOG_LOG, "dir %s", tmppath);
204
205 #if 0
206     if (!dst || repComp (dst->path, src, src_len))
207 #else
208     if (!dst || strcmp (dst->path, src))
209 #endif
210     {
211         if (!e_src)
212             return;
213
214         if (src_len && src[src_len-1] != '/')
215         {
216             src[src_len] = '/';
217             src[++src_len] = '\0';
218         }
219         dirs_mkdir (di, src, 0);
220         if (dst && repComp (dst->path, src, src_len))
221             dst = NULL;
222     }
223     else if (!e_src)
224     {
225         strcpy (src, dst->path);
226         fileDeleteR (di, dst, base, src, rGroup);
227         return;
228     }
229     else
230     {
231         if (src_len && src[src_len-1] != '/')
232         {
233             src[src_len] = '/';
234             src[++src_len] = '\0';
235         }
236         dst = dirs_read (di); 
237     }
238     dir_sort (e_src);
239
240     while (1)
241     {
242         int sd;
243
244         if (dst && !repComp (dst->path, src, src_len))
245         {
246             if (e_src[i_src].name)
247             {
248                 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
249                       e_src[i_src].name);
250                 sd = strcmp (dst->path + src_len, e_src[i_src].name);
251             }
252             else
253                 sd = -1;
254         }
255         else if (e_src[i_src].name)
256             sd = 1;
257         else
258             break;
259         logf (LOG_DEBUG, "trav sd=%d", sd);
260         if (sd == 0)
261         {
262             strcpy (src + src_len, e_src[i_src].name);
263             sprintf (tmppath, "%s%s", base, src);
264             
265             switch (e_src[i_src].kind)
266             {
267             case dirs_file:
268                 if (e_src[i_src].mtime > dst->mtime)
269                 {
270                     if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
271                     {
272                         dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
273                     }
274                     logf (LOG_DEBUG, "old: %s", ctime (&dst->mtime));
275                     logf (LOG_DEBUG, "new: %s", ctime (&e_src[i_src].mtime));
276                 }
277                 dst = dirs_read (di);
278                 break;
279             case dirs_dir:
280                 fileUpdateR (di, dst, base, src, rGroup);
281                 dst = dirs_last (di);
282                 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
283                 break;
284             default:
285                 dst = dirs_read (di); 
286             }
287             i_src++;
288         }
289         else if (sd > 0)
290         {
291             SYSNO sysno = 0;
292             strcpy (src + src_len, e_src[i_src].name);
293             sprintf (tmppath, "%s%s", base, src);
294
295             switch (e_src[i_src].kind)
296             {
297             case dirs_file:
298                 if (fileExtract (&sysno, tmppath, rGroup, 0))
299                     dirs_add (di, src, sysno, e_src[i_src].mtime);            
300                 break;
301             case dirs_dir:
302                 fileUpdateR (di, dst, base, src, rGroup);
303                 if (dst)
304                     dst = dirs_last (di);
305                 break;
306             }
307             i_src++;
308         }
309         else  /* sd < 0 */
310         {
311             strcpy (src, dst->path);
312             sprintf (tmppath, "%s%s", base, dst->path);
313
314             switch (dst->kind)
315             {
316             case dirs_file:
317                 fileExtract (&dst->sysno, tmppath, rGroup, 1);
318                 dirs_del (di, dst->path);
319                 dst = dirs_read (di);
320                 break;
321             case dirs_dir:
322                 fileDeleteR (di, dst, base, src, rGroup);
323                 dst = dirs_last (di);
324             }
325         }
326     }
327     dir_free (&e_src);
328 }
329
330 static void groupRes (struct recordGroup *rGroup)
331 {
332     char resStr[256];
333     char gPrefix[256];
334
335     if (!rGroup->groupName || !*rGroup->groupName)
336         *gPrefix = '\0';
337     else
338         sprintf (gPrefix, "%s.", rGroup->groupName);
339
340     sprintf (resStr, "%srecordId", gPrefix);
341     rGroup->recordId = res_get (common_resource, resStr);
342 }
343
344 void repositoryShow (struct recordGroup *rGroup)
345 {
346     char src[1024];
347     int src_len;
348     struct dirs_entry *dst;
349     Dict dict;
350     struct dirs_info *di;
351     
352     if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
353     {
354         logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
355         exit (1);
356     }
357     
358     assert (rGroup->path);    
359     strcpy (src, rGroup->path);
360     src_len = strlen (src);
361     
362     if (src_len && src[src_len-1] != '/')
363     {
364         src[src_len] = '/';
365         src[++src_len] = '\0';
366     }
367     
368     di = dirs_open (dict, src);
369     
370     while ( (dst = dirs_read (di)) )
371         logf (LOG_LOG, "%s", dst->path);
372     dirs_free (&di);
373     dict_close (dict);
374 }
375
376 static void fileUpdate (Dict dict, struct recordGroup *rGroup,
377                         const char *path)
378 {
379     struct dirs_info *di;
380     struct stat sbuf;
381     char src[1024];
382     char dst[1024];
383     int src_len;
384
385     assert (path);
386     strcpy (src, path);
387     src_len = strlen (src);
388
389     stat (src, &sbuf);
390     if (S_ISREG(sbuf.st_mode))
391     {
392         struct dirs_entry *e_dst;
393         di = dirs_fopen (dict, src);
394
395         e_dst = dirs_read (di);
396         if (e_dst)
397         {
398             if (sbuf.st_mtime > e_dst->mtime)
399                 if (fileExtract (&e_dst->sysno, src, rGroup, 0))
400                     dirs_add (di, src, e_dst->sysno, sbuf.st_mtime);
401         }
402         else
403         {
404             SYSNO sysno = 0;
405             if (fileExtract (&sysno, src, rGroup, 0))
406                  dirs_add (di, src, sysno, sbuf.st_mtime);
407         }
408         dirs_free (&di);
409     }
410     else if (S_ISDIR(sbuf.st_mode))
411     {
412         if (src_len && src[src_len-1] != '/')
413         {
414             src[src_len] = '/';
415             src[++src_len] = '\0';
416         }
417         di = dirs_open (dict, src);
418         *dst = '\0';
419         fileUpdateR (di, dirs_read (di), src, dst, rGroup);
420         dirs_free (&di);
421     }
422     else
423     {
424         logf (LOG_WARN, "Cannot handle file %s", src);
425     }
426 }
427
428
429 static void repositoryExtract (int deleteFlag, struct recordGroup *rGroup,
430                                const char *path)
431 {
432     struct stat sbuf;
433     char src[1024];
434
435     assert (path);
436     strcpy (src, path);
437
438     stat (src, &sbuf);
439     if (S_ISREG(sbuf.st_mode))
440         fileExtract (NULL, src, rGroup, deleteFlag);
441     else if (S_ISDIR(sbuf.st_mode))
442         repositoryExtractR (deleteFlag, src, rGroup);
443     else
444         logf (LOG_WARN, "Cannot handle file %s", src);
445 }
446
447 static void repositoryExtractG (int deleteFlag, struct recordGroup *rGroup)
448 {
449     if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
450     {
451         char src[1024];
452
453         while (scanf ("%s", src) == 1)
454             repositoryExtract (deleteFlag, rGroup, src);
455     }
456     else
457         repositoryExtract (deleteFlag, rGroup, rGroup->path);
458 }
459
460 void repositoryUpdate (struct recordGroup *rGroup)
461 {
462     groupRes (rGroup);
463     assert (rGroup->path);
464     if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
465     {
466         Dict dict;
467         if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
468         {
469             logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
470             exit (1);
471         }
472         if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
473         {
474             char src[1024];
475             while (scanf ("%s", src) == 1)
476                 fileUpdate (dict, rGroup, src);
477         }
478         else
479             fileUpdate (dict, rGroup, rGroup->path);
480         dict_close (dict);
481     }
482     else 
483         repositoryExtractG (0, rGroup);
484 }
485
486 void repositoryDelete (struct recordGroup *rGroup)
487 {
488     repositoryExtractG (1, rGroup);
489 }
490