2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.21 1996-03-22 15:34:18 quinn
10 * Revision 1.20 1996/03/21 14:50:10 adam
11 * File update uses modify-time instead of change-time.
13 * Revision 1.19 1996/03/20 16:16:55 quinn
14 * Added diagnostic output
16 * Revision 1.18 1996/03/19 12:43:27 adam
17 * Bug fix: File update traversal didn't handle trailing slashes correctly.
18 * Bug fix: Update of sub directory groups wasn't handled correctly.
20 * Revision 1.17 1996/02/12 18:45:17 adam
21 * Changed naming of some functions.
23 * Revision 1.16 1996/02/05 12:30:02 adam
24 * Logging reduced a bit.
25 * The remaining running time is estimated during register merge.
27 * Revision 1.15 1995/12/07 17:38:48 adam
28 * Work locking mechanisms for concurrent updates/commit.
30 * Revision 1.14 1995/12/06 12:41:26 adam
31 * New command 'stat' for the index program.
32 * Filenames can be read from stdin by specifying '-'.
33 * Bug fix/enhancement of the transformation from terms to regular
34 * expressons in the search engine.
36 * Revision 1.13 1995/11/28 09:09:46 adam
37 * Zebra config renamed.
38 * Use setting 'recordId' to identify record now.
39 * Bug fix in recindex.c: rec_release_blocks was invokeded even
40 * though the blocks were already released.
41 * File traversal properly deletes records when needed.
43 * Revision 1.12 1995/11/24 11:31:37 adam
44 * Commands add & del read filenames from stdin if source directory is
46 * Match criteria supports 'constant' strings.
48 * Revision 1.11 1995/11/22 17:19:19 adam
49 * Record management uses the bfile system.
51 * Revision 1.10 1995/11/21 15:01:16 adam
52 * New general match criteria implemented.
53 * New feature: document groups.
55 * Revision 1.9 1995/11/21 09:20:32 adam
56 * Yet more work on record match.
58 * Revision 1.8 1995/11/20 16:59:46 adam
59 * New update method: the 'old' keys are saved for each records.
61 * Revision 1.7 1995/11/20 11:56:28 adam
62 * Work on new traversal.
64 * Revision 1.6 1995/11/17 15:54:42 adam
65 * Started work on virtual directory structure.
67 * Revision 1.5 1995/10/17 18:02:09 adam
68 * New feature: databases. Implemented as prefix to words in dictionary.
70 * Revision 1.4 1995/09/28 09:19:46 adam
71 * xfree/xmalloc used everywhere.
72 * Extract/retrieve method seems to work for text records.
74 * Revision 1.3 1995/09/06 16:11:18 adam
75 * Option: only one word key per file.
77 * Revision 1.2 1995/09/04 12:33:43 adam
78 * Various cleanup. YAZ util used instead.
80 * Revision 1.1 1995/09/01 14:06:36 adam
81 * Split of work into more files.
89 #include <sys/types.h>
97 static int repComp (const char *a, const char *b, size_t len)
101 return memcmp (a, b, len);
104 static void repositoryExtractR (int deleteFlag, char *rep,
105 struct recordGroup *rGroup)
109 size_t rep_len = strlen (rep);
114 logf (LOG_LOG, "Dir: %s", rep);
115 if (rep[rep_len-1] != '/')
119 for (i=0; e[i].name; i++)
121 strcpy (rep +rep_len+1, e[i].name);
125 fileExtract (NULL, rep, rGroup, deleteFlag);
128 repositoryExtractR (deleteFlag, rep, rGroup);
136 static void stdinExtractR (int deleteFlag, struct recordGroup *rGroup)
140 logf (LOG_LOG, "stdinExtractR");
141 while (scanf ("%s", tmppath) == 1)
142 fileExtract (NULL, tmppath, rGroup, deleteFlag);
145 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
146 const char *base, char *src,
147 struct recordGroup *rGroup)
150 size_t src_len = strlen (src);
152 while (dst && !repComp (dst->path, src, src_len+1))
157 sprintf (tmppath, "%s%s", base, dst->path);
158 fileExtract (&dst->sysno, tmppath, rGroup, 1);
160 strcpy (tmppath, dst->path);
161 dst = dirs_read (di);
162 dirs_del (di, tmppath);
165 strcpy (tmppath, dst->path);
166 dst = dirs_read (di);
167 dirs_rmdir (di, tmppath);
170 dst = dirs_read (di);
175 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
176 const char *base, char *src,
177 struct recordGroup *rGroup)
179 struct dir_entry *e_src;
181 static char tmppath[1024];
182 size_t src_len = strlen (src);
184 sprintf (tmppath, "%s%s", base, src);
185 e_src = dir_open (tmppath);
186 logf (LOG_LOG, "Dir: %s", tmppath);
189 if (!dst || repComp (dst->path, src, src_len))
191 if (!dst || strcmp (dst->path, src))
197 if (src_len && src[src_len-1] != '/')
200 src[++src_len] = '\0';
202 dirs_mkdir (di, src, 0);
203 if (repComp (dst->path, src, src_len))
208 strcpy (src, dst->path);
209 fileDeleteR (di, dst, base, src, rGroup);
214 if (src_len && src[src_len-1] != '/')
217 src[++src_len] = '\0';
219 dst = dirs_read (di);
227 if (dst && !repComp (dst->path, src, src_len))
229 if (e_src[i_src].name)
231 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
233 sd = strcmp (dst->path + src_len, e_src[i_src].name);
238 else if (e_src[i_src].name)
242 logf (LOG_DEBUG, "trav sd=%d", sd);
245 strcpy (src + src_len, e_src[i_src].name);
246 sprintf (tmppath, "%s%s", base, src);
248 switch (e_src[i_src].kind)
251 if (e_src[i_src].mtime > dst->mtime)
253 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
255 dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
257 logf (LOG_LOG, "old: %s", ctime (&dst->mtime));
258 logf (LOG_LOG, "new: %s", ctime (&e_src[i_src].mtime));
260 dst = dirs_read (di);
263 fileUpdateR (di, dst, base, src, rGroup);
264 dst = dirs_last (di);
265 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
268 dst = dirs_read (di);
275 strcpy (src + src_len, e_src[i_src].name);
276 sprintf (tmppath, "%s%s", base, src);
278 switch (e_src[i_src].kind)
281 if (fileExtract (&sysno, tmppath, rGroup, 0))
282 dirs_add (di, src, sysno, e_src[i_src].mtime);
285 fileUpdateR (di, dst, base, src, rGroup);
287 dst = dirs_last (di);
294 strcpy (src, dst->path);
295 sprintf (tmppath, "%s%s", base, dst->path);
300 fileExtract (&dst->sysno, tmppath, rGroup, 1);
301 dirs_del (di, dst->path);
302 dst = dirs_read (di);
305 fileDeleteR (di, dst, base, src, rGroup);
306 dst = dirs_last (di);
313 static void groupRes (struct recordGroup *rGroup)
318 if (!rGroup->groupName || !*rGroup->groupName)
321 sprintf (gPrefix, "%s.", rGroup->groupName);
323 sprintf (resStr, "%srecordId", gPrefix);
324 rGroup->recordId = res_get (common_resource, resStr);
328 void repositoryShow (struct recordGroup *rGroup)
332 struct dirs_entry *dst;
334 struct dirs_info *di;
336 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
338 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
342 assert (rGroup->path);
343 strcpy (src, rGroup->path);
344 src_len = strlen (src);
346 if (src_len && src[src_len-1] != '/')
349 src[++src_len] = '\0';
352 di = dirs_open (dict, src);
354 while ( (dst = dirs_read (di)) )
355 logf (LOG_LOG, "%s", dst->path);
360 void repositoryUpdate (struct recordGroup *rGroup)
367 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
370 struct dirs_info *di;
372 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
374 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
377 assert (rGroup->path);
379 strcpy (src, rGroup->path);
380 src_len = strlen (src);
382 if (src_len && src[src_len-1] != '/')
385 src[++src_len] = '\0';
388 di = dirs_open (dict, src);
391 fileUpdateR (di, dirs_read (di), src, dst, rGroup);
397 strcpy (src, rGroup->path);
398 if (*src == '\0' || !strcmp (src, "-"))
399 stdinExtractR (0, rGroup);
401 repositoryExtractR (0, src, rGroup);
405 void repositoryDelete (struct recordGroup *rGroup)
409 assert (rGroup->path);
411 strcpy (src, rGroup->path);
412 if (*src == '\0' || !strcmp(src, "-"))
413 stdinExtractR (1, rGroup);
415 repositoryExtractR (1, src, rGroup);