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