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