Changed naming of some functions.
[idzebra-moved-to-github.git] / index / trav.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: trav.c,v $
7  * Revision 1.17  1996-02-12 18:45:17  adam
8  * Changed naming of some functions.
9  *
10  * Revision 1.16  1996/02/05  12:30:02  adam
11  * Logging reduced a bit.
12  * The remaining running time is estimated during register merge.
13  *
14  * Revision 1.15  1995/12/07  17:38:48  adam
15  * Work locking mechanisms for concurrent updates/commit.
16  *
17  * Revision 1.14  1995/12/06  12:41:26  adam
18  * New command 'stat' for the index program.
19  * Filenames can be read from stdin by specifying '-'.
20  * Bug fix/enhancement of the transformation from terms to regular
21  * expressons in the search engine.
22  *
23  * Revision 1.13  1995/11/28  09:09:46  adam
24  * Zebra config renamed.
25  * Use setting 'recordId' to identify record now.
26  * Bug fix in recindex.c: rec_release_blocks was invokeded even
27  * though the blocks were already released.
28  * File traversal properly deletes records when needed.
29  *
30  * Revision 1.12  1995/11/24  11:31:37  adam
31  * Commands add & del read filenames from stdin if source directory is
32  * empty.
33  * Match criteria supports 'constant' strings.
34  *
35  * Revision 1.11  1995/11/22  17:19:19  adam
36  * Record management uses the bfile system.
37  *
38  * Revision 1.10  1995/11/21  15:01:16  adam
39  * New general match criteria implemented.
40  * New feature: document groups.
41  *
42  * Revision 1.9  1995/11/21  09:20:32  adam
43  * Yet more work on record match.
44  *
45  * Revision 1.8  1995/11/20  16:59:46  adam
46  * New update method: the 'old' keys are saved for each records.
47  *
48  * Revision 1.7  1995/11/20  11:56:28  adam
49  * Work on new traversal.
50  *
51  * Revision 1.6  1995/11/17  15:54:42  adam
52  * Started work on virtual directory structure.
53  *
54  * Revision 1.5  1995/10/17  18:02:09  adam
55  * New feature: databases. Implemented as prefix to words in dictionary.
56  *
57  * Revision 1.4  1995/09/28  09:19:46  adam
58  * xfree/xmalloc used everywhere.
59  * Extract/retrieve method seems to work for text records.
60  *
61  * Revision 1.3  1995/09/06  16:11:18  adam
62  * Option: only one word key per file.
63  *
64  * Revision 1.2  1995/09/04  12:33:43  adam
65  * Various cleanup. YAZ util used instead.
66  *
67  * Revision 1.1  1995/09/01  14:06:36  adam
68  * Split of work into more files.
69  *
70  */
71 #include <stdio.h>
72 #include <assert.h>
73 #include <unistd.h>
74 #include <dirent.h>
75 #include <sys/stat.h>
76 #include <sys/types.h>
77 #include <fcntl.h>
78 #include <ctype.h>
79
80 #include <alexutil.h>
81 #include "index.h"
82
83 static int repComp (const char *a, const char *b, size_t len)
84 {
85     if (!len)
86         return 0;
87     return memcmp (a, b, len);
88 }
89
90 static void repositoryExtractR (int deleteFlag, char *rep,
91                                 struct recordGroup *rGroup)
92 {
93     struct dir_entry *e;
94     int i;
95     size_t rep_len = strlen (rep);
96
97     e = dir_open (rep);
98     if (!e)
99         return;
100     logf (LOG_LOG, "Dir: %s", rep);
101     if (rep[rep_len-1] != '/')
102         rep[rep_len] = '/';
103     else
104         --rep_len;
105     for (i=0; e[i].name; i++)
106     {
107         strcpy (rep +rep_len+1, e[i].name);
108         switch (e[i].kind)
109         {
110         case dirs_file:
111             fileExtract (NULL, rep, rGroup, deleteFlag);
112             break;
113         case dirs_dir:
114             repositoryExtractR (deleteFlag, rep, rGroup);
115             break;
116         }
117     }
118     dir_free (&e);
119
120 }
121
122 static void stdinExtractR (int deleteFlag, struct recordGroup *rGroup)
123 {
124     char tmppath[1024];
125
126     logf (LOG_LOG, "stdinExtractR");
127     while (scanf ("%s", tmppath) == 1)
128         fileExtract (NULL, tmppath, rGroup, deleteFlag);
129 }
130
131 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
132                                const char *base, char *src,
133                                struct recordGroup *rGroup)
134 {
135     char tmppath[1024];
136     size_t src_len = strlen (src);
137
138     while (dst && !repComp (dst->path, src, src_len+1))
139     {
140         switch (dst->kind)
141         {
142         case dirs_file:
143             sprintf (tmppath, "%s%s", base, dst->path);
144             fileExtract (&dst->sysno, tmppath, rGroup, 1);
145              
146             strcpy (tmppath, dst->path);
147             dst = dirs_read (di); 
148             dirs_del (di, tmppath);
149             break;
150         case dirs_dir:
151             strcpy (tmppath, dst->path);
152             dst = dirs_read (di);
153             dirs_rmdir (di, tmppath);
154             break;
155         default:
156             dst = dirs_read (di);
157         }
158     }
159 }
160
161 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
162                                const char *base, char *src, 
163                                struct recordGroup *rGroup)
164 {
165     struct dir_entry *e_src;
166     int i_src = 0;
167     static char tmppath[1024];
168     size_t src_len = strlen (src);
169
170     sprintf (tmppath, "%s%s", base, src);
171     e_src = dir_open (tmppath);
172     logf (LOG_LOG, "Dir: %s", tmppath);
173 #if 1
174     if (!dst || repComp (dst->path, src, src_len))
175 #else
176     if (!dst || strcmp (dst->path, src))
177 #endif
178     {
179         if (!e_src)
180             return;
181         if (src_len && src[src_len-1] == '/')
182             --src_len;
183         else
184             src[src_len] = '/';
185         src[src_len+1] = '\0';
186         dirs_mkdir (di, src, 0);
187         dst = NULL;
188     }
189     else if (!e_src)
190     {
191         strcpy (src, dst->path);
192         fileDeleteR (di, dst, base, src, rGroup);
193         return;
194     }
195     else
196     {
197         if (src_len && src[src_len-1] == '/')
198             --src_len;
199         else
200             src[src_len] = '/';
201         src[src_len+1] = '\0';
202         dst = dirs_read (di); 
203     }
204     dir_sort (e_src);
205
206     while (1)
207     {
208         int sd;
209
210         if (dst && !repComp (dst->path, src, src_len+1))
211         {
212             if (e_src[i_src].name)
213             {
214                 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len+1, 
215                       e_src[i_src].name);
216                 sd = strcmp (dst->path + src_len+1, e_src[i_src].name);
217             }
218             else
219                 sd = -1;
220         }
221         else if (e_src[i_src].name)
222             sd = 1;
223         else
224             break;
225         logf (LOG_DEBUG, "trav sd=%d", sd);
226         if (sd == 0)
227         {
228             strcpy (src + src_len+1, e_src[i_src].name);
229             sprintf (tmppath, "%s%s", base, src);
230             
231             switch (e_src[i_src].kind)
232             {
233             case dirs_file:
234                 if (e_src[i_src].ctime > dst->ctime)
235                 {
236                     if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
237                     {
238                         dirs_add (di, src, dst->sysno, e_src[i_src].ctime);
239                     }
240                 }
241                 dst = dirs_read (di);
242                 break;
243             case dirs_dir:
244                 fileUpdateR (di, dst, base, src, rGroup);
245                 dst = dirs_last (di);
246                 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
247                 break;
248             default:
249                 dst = dirs_read (di); 
250             }
251             i_src++;
252         }
253         else if (sd > 0)
254         {
255             SYSNO sysno = 0;
256             strcpy (src + src_len+1, e_src[i_src].name);
257             sprintf (tmppath, "%s%s", base, src);
258
259             switch (e_src[i_src].kind)
260             {
261             case dirs_file:
262                 if (fileExtract (&sysno, tmppath, rGroup, 0))
263                     dirs_add (di, src, sysno, e_src[i_src].ctime);            
264                 break;
265             case dirs_dir:
266                 fileUpdateR (di, dst, base, src, rGroup);
267                 if (dst)
268                     dst = dirs_last (di);
269                 break;
270             }
271             i_src++;
272         }
273         else  /* sd < 0 */
274         {
275             strcpy (src, dst->path);
276             sprintf (tmppath, "%s%s", base, dst->path);
277
278             switch (dst->kind)
279             {
280             case dirs_file:
281                 fileExtract (&dst->sysno, tmppath, rGroup, 1);
282                 dirs_del (di, dst->path);
283                 dst = dirs_read (di);
284                 break;
285             case dirs_dir:
286                 fileDeleteR (di, dst, base, src, rGroup);
287                 dst = dirs_last (di);
288             }
289         }
290     }
291     dir_free (&e_src);
292 }
293
294 static void groupRes (struct recordGroup *rGroup)
295 {
296     char resStr[256];
297     char gPrefix[256];
298
299     if (!rGroup->groupName || !*rGroup->groupName)
300         *gPrefix = '\0';
301     else
302         sprintf (gPrefix, "%s.", rGroup->groupName);
303
304     sprintf (resStr, "%srecordId", gPrefix);
305     rGroup->recordId = res_get (common_resource, resStr);
306 }
307
308 void repositoryUpdate (struct recordGroup *rGroup)
309 {
310     char src[256];
311
312     groupRes (rGroup);
313     if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
314     {
315         Dict dict;
316         struct dirs_info *di;
317
318         if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
319         {
320             logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
321             exit (1);
322         }
323         assert (rGroup->path);
324         di = dirs_open (dict, rGroup->path);
325         strcpy (src, "");
326         fileUpdateR (di, dirs_read (di), rGroup->path, src, rGroup);
327         dirs_free (&di);
328         dict_close (dict);
329     }
330     else 
331     {
332         strcpy (src, rGroup->path);
333         if (*src == '\0' || !strcmp (src, "-"))
334             stdinExtractR (0, rGroup);
335         else
336             repositoryExtractR (0, src, rGroup);
337     }
338 }
339
340 void repositoryDelete (struct recordGroup *rGroup)
341 {
342     char src[256];
343
344     assert (rGroup->path);
345     groupRes (rGroup);
346     strcpy (src, rGroup->path);
347     if (*src == '\0' || !strcmp(src, "-"))
348         stdinExtractR (1, rGroup);
349     else
350         repositoryExtractR (1, src, rGroup);
351 }
352